[Numpy-discussion] Fortran ordering
Dag Sverre Seljebotn
dagss@student.matnat.uio...
Sat Apr 17 03:41:17 CDT 2010
Paul Northug wrote:
> I'd like to use numpy fortran ordering in order to use some external
> libraries more easily, but it looks like I don't understand how it
> works and it is causing transposes that are confusing me.
>
> When I define an array as:
>
> a = np.array([[1.,2.],[3.,4.]], order='F', type=np.float32)
>
> how is it stored in memory, as [1, 2, 3, 4] or [1, 3, 2, 4]?
>
> a.strides == (4, 8), suggesting [1, 3, 2, 4]. I can also check by
> stepping through:
>
> ap = a.ctypes.data_as(POINTER(c_float))
> ap[:4] == [1., 3., 2., 4.]
>
> But when I call external libraries through ctypes with a.ctypes.data
> as argument, the external function gets [1, 2, 3, 4] (I think).
> Eventually, I plan to use cython's buffer interface and a.data. Will
> it be [1, 2, 3, 4] there as well? Or is my external code broken.
Using Cython's a.data, it will definitely be [1, 3, 2, 4].
Declare your array in Cython as np.ndarray[float, mode='fortran'], that
way you'll get a ValueError unless the ordering is correct.
--
Dag Sverre
More information about the NumPy-Discussion
mailing list