Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DOC: Migration guide should mention complex number API changes #25294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
djhoese opened this issue Dec 1, 2023 · 8 comments · Fixed by #25929
Closed

DOC: Migration guide should mention complex number API changes #25294

djhoese opened this issue Dec 1, 2023 · 8 comments · Fixed by #25929

Comments

@djhoese
Copy link
Contributor

djhoese commented Dec 1, 2023

Describe the issue:

I'm helping the h5py project migrate to Cython 3 and while doing so also tested with numpy 2.0 (main). I discovered some compilation errors which you can read in detail here:

h5py/h5py#2348

Basically h5py uses a macro from the HDF5 C++ library to get the offset into a struct for the "real" and "imag" members, but now complex types in numpy's C API are not structs (#24085). So I'm wondering what the best long term solution is to work around this. The h5py code doing this looks like this:

 #define h5py_offset_n64_real (HOFFSET(npy_complex64, real)) 

Reproduce the code example:

Not a python problem so no code example. See above for compilation information. Sorry for filing this as a bug but the "support" page on the docs lead to gitter, reddit, and IRC which all seemed inactive. This seems like a possibly specific case to h5py so I didn't really want to put it on stackoverflow either. And I wasn't really sure what else to categorize this as.

Error message:

/tmp/pip-req-build-3skh7xs0/h5py/h5t.c: In function ‘__pyx_f_4h5py_3h5t__c_complex’:
      /tmp/pip-req-build-3skh7xs0/h5py/api_compat.h:38:31: error: request for member ‘real’ in something not a structure or union
         38 | #define h5py_offset_n64_real (HOFFSET(npy_complex64, real))
            |                               ^~~~~~~
      /tmp/pip-req-build-3skh7xs0/h5py/h5t.c:26702:21: note: in expansion of macro ‘h5py_offset_n64_real’
      26702 |     __pyx_v_off_r = h5py_offset_n64_real;
            |                     ^~~~~~~~~~~~~~~~~~~~

Python and NumPy Versions:

Python 3.11
Numpy 2.0 (main)

Runtime Environment:

No response

Context for the issue:

Currently affecting h5py compatibility with numpy 2.0.

@seberg
Copy link
Member

seberg commented Dec 1, 2023

Every standard defines this to be ABI equivalent to the struct. So the offsets are trivially 0 and sizeof(floating_type)? I mean, there is also a macro version to get the imag/real, but I doubt it isn't easier to just hardcode something

@djhoese
Copy link
Contributor Author

djhoese commented Dec 2, 2023

Makes sense. So sanity check: complex64 is made up of 2 32-bit floats float, right? So complex128 is two double and complex256 is two long double?

@ngoldbaum
Copy link
Member

Should we maybe have a paragraph on this in the migration guide that we can point people to? ping @lysnikolaou too since they drove most of the effort on this.

@djhoese
Copy link
Contributor Author

djhoese commented Dec 2, 2023

Oh yes I meant to mention that. I didn't notice the complex change in the C-API issue (#25292 #API) nor in the version 2 release notes.

@lysnikolaou
Copy link
Member

There's a section on all of this on https://numpy.org/devdocs/reference/c-api/coremath.html#support-for-complex-numbers. We should probably link to it from the release notes and/or the migration guide though.

@ngoldbaum ngoldbaum changed the title BUG: Help migrating C npy_complexN struct usage for numpy 2.0 DOC: Migration guide should mention complex number API changes Dec 5, 2023
@ngoldbaum
Copy link
Member

Changed the title to reflect that the new docs linked to above just need to be a bit more discoverable for people porting code for numpy 2.0.

@seberg
Copy link
Member

seberg commented Dec 5, 2023

One thing the section might grow, is that it is perfectly fine to vendor the struct definition or the std one especially on C++ (who don't use npy_math anyway). Often that seems simpler than using the new macros.

@lucascolley
Copy link
Contributor

There's a section on all of this on https://numpy.org/devdocs/reference/c-api/coremath.html#support-for-complex-numbers. We should probably link to it from the release notes and/or the migration guide though.

+1 for linking to this from the 2.0 migration guide, we just bumped into this in scipy/scipy#19587 and that was the first place I checked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants