unable to pass numpy 2D double complex array to cython as memoryview for writing
See original GitHub issueThe following code fails in a jupyter notebook. However if I work with double instead of double complex, everything works correctly.
%%cython -a
'''
test cython
'''
cpdef test_array(double complex [:,::1] test):
cdef int x1,y1
x1,y1 = test.shape[0],test.shape[1]
for i in range(x1):
for j in range(y1):
print(i,j,test[i][j])
test[i][j]+=0
return
The problem is due to test[i][j]+=0
. The error I get during compilation is :
/home/sajid/.cache/ipython/cython/_cython_magic_da49badc209af5635b09816e8df29646.c: In function '__pyx_f_46_cython_magic_da49badc209af5635b09816e8df29646_test_array':
/home/sajid/.cache/ipython/cython/_cython_magic_da49badc209af5635b09816e8df29646.c:2138:183: error: invalid operands to binary + (have '__pyx_t_double_complex {aka struct <anonymous>}' and '__pyx_t_double_complex {aka struct <anonymous>}')
*((__pyx_t_double_complex *) ( /* dim=1 */ ((char *) (((__pyx_t_double_complex *) ( /* dim=0 */ (__pyx_v_test.data + __pyx_t_17 * __pyx_v_test.strides[0]) )) + __pyx_t_18)) )) += __pyx_t_double_complex_from_parts(0, 0);
^~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/sajid/.cache/ipython/cython/_cython_magic_da49badc209af5635b09816e8df29646.c: In function '__pyx_f_46_cython_magic_da49badc209af5635b09816e8df29646_test_array':
/home/sajid/.cache/ipython/cython/_cython_magic_da49badc209af5635b09816e8df29646.c:2138:183: error: invalid operands to binary + (have '__pyx_t_double_complex {aka struct <anonymous>}' and '__pyx_t_double_complex {aka struct <anonymous>}')
*((__pyx_t_double_complex *) ( /* dim=1 */ ((char *) (((__pyx_t_double_complex *) ( /* dim=0 */ (__pyx_v_test.data + __pyx_t_17 * __pyx_v_test.strides[0]) )) + __pyx_t_18)) )) += __pyx_t_double_complex_from_parts(0, 0);
followed by disutils error message.
Everything works if I use test[i][j] = test[i][j] + (0+0*1j)
. Is this failure of casting to double complex expected behavior or a bug ?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
pass numpy 2D double complex array to cython as memoryview
The following code fails in a jupyter notebook. However if I work with double instead of double complex, everything works correctly.
Read more >Typed Memoryviews — Cython 3.0.0a11 documentation
Typed memoryviews allow efficient access to memory buffers, such as those underlying NumPy arrays, without incurring any Python overhead.
Read more >Best Practices for passing numpy data pointer to C ?
Hi folks, We need to be able to pass the data pointer from a numpy array to C -- so that the data...
Read more >Using Python as glue — NumPy v1.25.dev0 Manual
The syntax double complex[:] denotes a one-dimensional array (vector) of ... are of note: firstly, it is impossible to return a memory view...
Read more >Python: Computing with Cython - Bitbucket
Cython has a more complex compilation process than Python and is not usually ... method (i.e. array access using [ ]) of NumPy...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t see why this shouldn’t work, so I vote for bug. Thanks for the report.
Yes, as stated in the original post : “Everything works if I use test[i][j] = test[i][j] + (0+0*1j).”