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
BUG: changing the complex struct has implications for Cython #26029
Comments
Just to clarify a bit: The Cython wrappers for Numpy describe the structure as: numpy/numpy/__init__.cython-30.pxd Lines 70 to 72 in dda030f
which doesn't match any more. One of the places that it affects is in the Cython tests. We can change that fine because it's something we control. The bigger problem is if other people are using it via this interface. |
One could do
Could you describe the problem in a bit more detail? I'm not very familiar with how Cython does things here, and while I understand conceptually that having a different definition in C and in Cython is not ideal, I don't get what exact problem users would hit. |
TL;DR: code that used to access the Longer version: There is a usage pattern in cython, which is reflected in a test, that looks like
This test and usage pattern fails on NumPy2 since the complex dtypes are no longer a struct with the The recommended way to handle complex dtypes is via So there are two problems:
|
Hmmmm, annoying, we would have to ask users to copy the definition of Is there any way we can keep exporting the struct definition for Cython (or some other flag)!? |
What we did on SciPy was something along the lines of: cdef extern from "npy_2_complexcompat.h":
void NPY_CSETREAL(np.npy_cdouble *c, double real) nogil
void NPY_CSETIMAG(np.npy_cdouble *c, double imag) nogil
cdef inline double_complex zpack(double zr, double zi) noexcept nogil:
cdef np.npy_cdouble z
NPY_CSETREAL(&z, zr)
NPY_CSETIMAG(&z, zi)
return double_complex_from_npy_cdouble(z) This should work with both NumPy 1 and NumPy 2. Here's the relevant piece of code. |
I marked this as a blocker for 2.0.0rc1. The pxd files we ship with NumPy need fixing: at a minimum they should expose |
What if we just define the complex types to:
(without any |
Fixes #26029 declare npy_cfloat and friends as empty structs declare cfloat_t and friends as ctypedef float complex cfloat_t, without being directly connected to npy_cfloat. This allows still using .imag and .real attributes, which Cython wraps with macros add a test for BUG: changing the complex struct has implications for Cython #26029, it passes since we use C compilation for cython. The inplace += will fail on C++. resync the two cython pxd files. At what point do we declare NumPy requires Cython3? I checked that scipy and cython can use the pxd file. --------- Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net> Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Fixes numpy#26029 declare npy_cfloat and friends as empty structs declare cfloat_t and friends as ctypedef float complex cfloat_t, without being directly connected to npy_cfloat. This allows still using .imag and .real attributes, which Cython wraps with macros add a test for BUG: changing the complex struct has implications for Cython numpy#26029, it passes since we use C compilation for cython. The inplace += will fail on C++. resync the two cython pxd files. At what point do we declare NumPy requires Cython3? I checked that scipy and cython can use the pxd file. --------- Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net> Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Cython turns the property access increment-in-place in something like
into this C code (cleaned up and simplified a bit from the actual code)
but
npy_cfloat
on NumPy2.0 no longer is a struct withreal
andimag
fields. See cython/cython#6076 (comment). Can we give easy left-hand access to theimag
part of a complex type? The migration guide mentionsnpy_csetreal
andnpy_csetimag
, but then the codegen will need to do something very different.cc @lysnikolaou
This can be hit by running
python runtests.py numpy_test
after applying the changes in cython/cython#6076The text was updated successfully, but these errors were encountered: