Hoyt Koepke wrote:
> Hello,
> 
> I'm trying to use the new contiguous array support and I've hit a bit
> of a problem.  I'm curious if this is a bug or is intentional.  When I
> do
> 
> cdef ndarray[float, mode="c"] Xc = X
> 
> where X is a 2 dimensional numpy array, it raises an exception:
> 
> File "/home/hoytak/workspace/gravimetrics/spatial/gravity.pyx", line
> 101, in spatial.gravity._setGFFromSamples (spatial/gravity.c:1242)
>     cdef ndarray[float, mode="c"] Xc = X
> ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

Yes, this is intentional. The "mode" parameter doesn't affect how you 
access the array, so you still need to specify the "ndim" parameter 
(which defaults to 1). So:

cdef np.ndarray[float, mode="c", ndim=2] Xc = X
print Xc[2,3]

The only difference is that the second line will be very slightly faster 
than if mode were set to the default "strided", but at the cost than an 
exception is raised if the array is not contiguous.

If you want access the underlying buffer, you can do it like this:

cdef np.ndarray[float, mode="c", ndim=2] Xc = X
cdef float* buf = <float*>Xc.data

Or if you do not know the number of dimensions:

cdef np.ndarray Xc = X
if Xc.dtype != np.float or not Xc.flags["C_CONTIGUOUS"]:
     raise something
cdef float* buf = <float*>Xc.data

-- 
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to