Hi Monte,

This strikes me as a slightly strange request; ctypes is intended to
interface with the C memory model, which has no native representation of
fortran arrays.
The underlying workings of `as_array` is to cast your ctype pointer into a
ctypes array object, and then pass that into numpy.
That approach doesn't work when there is no ctypes representation to begin
with

If you were writing C code to work with fortran arrays, probably you would
flatten your data into a single 1D array. You can use the same approach
here:

>>> np.ctypeslib.as_array(a_ptr, shape=(a.size,)).reshape(a.shape,
order='F')
array([[0, 1, 2],
       [3, 4, 5]])

Eric

On Thu, 23 Mar 2023 at 17:04, <monte.b.hoo...@gmail.com> wrote:

> Would it be okay to add an argument to ctypeslib.as_array() that allowed
> specifying that a pointer references column-major memory layout?
>
> Currently if we use ndarray.ctypes.data_as() to get a pointer to a
> Fortran-ordered array and then we use ctypeslib.as_array() to read that
> same array back in, we don't have a way of doing the round trip correctly.
>
> For example:
> >>> import ctypes as ct
> >>> a = np.arange(6).reshape(2,3)
> >>> a = np.asfortranarray(a)
> >>> a
> array([[0, 1, 2],
>        [3, 4, 5]])
> >>> a_ptr = a.ctypes.data_as(ct.POINTER(ct.c_int))
> >>> b = np.ctypeslib.as_array(a_ptr, shape=a.shape)
> >>> b
> array([[0, 3, 1],
>        [4, 2, 5]])
>
> The proposed function signature would be something like:
> numpy.ctypeslib.as_array(obj, shape=None, order='None'), with order{ā€˜C’,
> ā€˜F’}, optional
>
> Thanks,
> Monte
> _______________________________________________
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: wieser.eric+nu...@gmail.com
>
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to