question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unable to pass numpy 2D double complex array to cython as memoryview for writing

See original GitHub issue

The 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:open
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
scodercommented, Sep 23, 2018

I don’t see why this shouldn’t work, so I vote for bug. Thanks for the report.

0reactions
s-sajid-alicommented, Apr 10, 2020

Yes, as stated in the original post : “Everything works if I use test[i][j] = test[i][j] + (0+0*1j).”

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found