I use the idiom
size_x, size_y, size_z = some_array.shape
quite often when dealing with numpy arrays. The same thing doesn't seem to work in Cython when the array in question has a type, e.g.
def someFunc(np.ndarray[np.float32_t, ndim=2] arr):
sx, sy = arr.shape
we end up with a compile error like
Cannot convert 'npy_intp *' to Python object
which is possibly the result of the fact that "shape" gets converted to a C array (for faster access), so it's no longer a tuple.
Is it possible to extract this tuple somehow even in Cython? (Or should I just stick with sx, sy = arr.shape[0], arr.shape[1]
?)
I believe that you are correct that the straight forward way to deal with this is something like:
cdef int sx, sy
sx = arr.shape[0]
sy = arr.shape[1]
I am not aware of another way to do this, and this is the convention I use in my own code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With