Fortran-style support in ctypeslib.as_array()?
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
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.hoover@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-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: wieser.eric+numpy@gmail.com
Eric, Thanks for the reply. My use case is using ctypes as an interface between Numpy and Fortran in cases where I can't/don't want to use f2py. It's easy enough for me to put a wrapper around ctypeslib.as_array() so that I can read in multidimensional Fortran arrays correctly (I reverse the shape dimensions and transpose), but I just thought adding explicit Fortran support as an argument would add to the API. I'm happy to contribute a pull request if you think it's a worthwhile add. Best, Monte
Hi Monte, I did send an email around a few weeks also on this https://mail.python.org/archives/list/numpy-discussion@python.org/message/R6... and have a working example at https://github.com/rjfarmer/numpy/tree/ctypelibs_as_array_flags but haven't gotten around to submitting as a pr yet. Maybe you could test out my suggestion and see if it does what you need? Rob On Tue, 28 Mar 2023 at 15:33, <monte.b.hoover@gmail.com> wrote:
Eric,
Thanks for the reply. My use case is using ctypes as an interface between Numpy and Fortran in cases where I can't/don't want to use f2py. It's easy enough for me to put a wrapper around ctypeslib.as_array() so that I can read in multidimensional Fortran arrays correctly (I reverse the shape dimensions and transpose), but I just thought adding explicit Fortran support as an argument would add to the API. I'm happy to contribute a pull request if you think it's a worthwhile add.
Best, Monte _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: robert.j.farmer37@gmail.com
participants (3)
-
Eric Wieser
-
monte.b.hoover@gmail.com
-
Rob Farmer