[Numpy-discussion] A question about ctypes and numpy
william ratcliff
william.ratcliff@gmail....
Tue Jan 15 04:40:00 CST 2008
Hi! I have been having some difficulty understanding how to deal with
multidimensional ndarrays in structs with ctypes. Namely, I want to the
define numpy arrays in python and have them operated on in C, and have them
updated on the python side. Here is a toy example:
c code fragment:
foo.c
typedef struct {
double X[4][4];
double Y[4][4];
} matrices; //there could be other elements in the struct between the
arrays, after them, etc.
int correct(matrices* pt){
int i=2,j=1;
pt->Y[i][j]=5.0;
}
python:
import numpy as N
import ctypes
xdata=N.zeros((4,4),dtype='float64')
ydata=N.ones((4,4),dtype='float64')
class fiduc(ctypes.Structure):
_fields_=[("X",some_ctype),
("Y",some_ctype)]
pt=fiduc()
pt.X=xdata.ctypes.data
pt.Y=ydata.ctypes.data
foo = N.ctypeslib.load_library('foo.dll', '.')
foo.correct(ctypes.byref(pt))
print xdata
print ydata
My question is:
If I want to define a class in python and later to have my c code operate on
an instance of it, then I need to know the c_type representation for the
ndarray. I can't simply blast the data into the structure because due to
memory alignment, structs in C are not guaranteed to have contiguous
elements in memory--I also don't want to take a risk with for example my
mingw generated dll having a different alignment for a structure than what a
ctypes made using msvc expects. Thanks!!!!!
Cheers,
William
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080115/ee08241f/attachment.html
More information about the Numpy-discussion
mailing list