[Numpy-discussion] Ctypes support in NumPy

Albert Strasheim fullung at gmail.com
Mon Jul 3 07:59:11 CDT 2006


Hello all

Travis Oliphant wrote:
> The ctypes-conversion object has attributes which return c_types aware
> objects so that the information can be passed directly to c-code (as an
> integer, the number of dimensions can already be passed using c-types).
> 
> The information available and it's corresponding c_type is
> 
> data           -  c_void_p
> shape, strides -  c_int * nd  or c_long * nd or c_longlong * nd
> depending on platform

Stefan and I did some more experiments and it seems like .ctypes.strides
isn't doing the right thing for subarrays.

For example:

In [52]: x = N.rand(3,4)
In [57]: [x.ctypes.strides[i] for i in range(x.ndim)]
Out[57]: [32, 8]

This looks fine. But for this subarray:

In [56]: [x[1:3,1:4].ctypes.strides[i] for i in range(x.ndim)]
Out[56]: [32, 8]

In this case, I think one wants strides[0] (the row stride) to return 40.

.ctypes.data already seems to do the right thing:

In [60]: x.ctypes.data
Out[60]: c_void_p(31685288)

In [61]: x[1:3,1:4].ctypes.data
Out[61]: c_void_p(31685328)

In [62]: 31685288-31685328
Out[62]: 40

What would be a good way of dealing with discontiguous arrays? It seems like
one might want to disable their .ctypes attribute.

Regards,

Albert





More information about the Numpy-discussion mailing list