[Cython] Can't assign memview cast to memview slice

Dave Hirschfeld dave.hirschfeld at gmail.com
Tue Feb 26 13:47:54 CET 2013


The following works:

```
%%cython
cimport cython
import numpy as np
cimport numpy as np

def f(double[:,:] arr):
    cdef double[:] res = np.zeros(2*arr.size, dtype=np.float64)
    cdef double[:] tmp
    tmp = <double[:arr.size]>&arr[0,0]
    res[:arr.size] = tmp
    return res
```

whereas the following:

```
%%cython
cimport cython
import numpy as np
cimport numpy as np

def f(double[:,:] arr):
    cdef double[:] res = np.zeros(2*arr.size, dtype=np.float64)
    res[:arr.size] = <double[:arr.size]>&arr[0,0]
    return res
```

...gives the below error:

Error compiling Cython file:
------------------------------------------------------------
...
import numpy as np
cimport numpy as np

def f(double[:,:] arr):
    cdef double[:] res = np.zeros(2*arr.size, dtype=np.float64)
    res[:arr.size] = <double[:arr.size]>&arr[0,0]
                    ^
------------------------------------------------------------

d3ce.pyx:7:21: Cannot assign type 'double[::1]' to 'double'


It would be nice if cython could take care of the temporary 
itself though the workaround is certainly simple enough
that it's not a big issue at all.

Thanks,
Dave





More information about the cython-devel mailing list