General cross-platform code to create a Numpy dtype for a given Cython struct?

9 views
Skip to first unread message

Toon Verstraelen

unread,
Aug 9, 2017, 6:02:17 AM8/9/17
to cython-users
Hi,

I've posted this question before as an issue on github but that was apparently not the right place: https://github.com/cython/cython/issues/1809

I'll repeat it below for convenience. Say I have the following C struct (in dlist.h):

typedef struct {
  double dx, dy, dz;  // relative vector coordinates.
  long i, j;          // involved atoms. vector points from i to j.
  double gx, gy, gz;  // derivative of energy towards relative vector coordinates.
} dlist_row_type;

My corresponding Cython struct is:

cdef extern from "dlist.h":
    ctypedef struct dlist_row_type:
        double dx, dy, dz
        long i, j
        double gx, gy, gz

I'm currently creating a compatible Numpy dtype as follows:

cdef dlist.dlist_row_type _dlist_row_tmp
delta_dtype = np.asarray(<dlist.dlist_row_type[:1]>(&_dlist_row_tmp)).dtype

This is a reasonable cross-platform solution but it involves a temporary variable. Is there a way to get this dtype without a temporary variable? I'd like to avoid manually entering all the fields in the dtype, which I did before. That is difficult to get to work on different platforms with the same code. Thanks!

Best Regards,

Toon
Reply all
Reply to author
Forward
0 new messages