Brian Blais wrote: > Hello, > > If I have an array of ints, as in numpy: > > a=numpy.zeros(5,int) > > and I call a cython function like: > > myfun(a) > > and in the cython I have: > > cpdef myfun(c_numpy.ndarray A): > > # stuff here > > > how can I access the data pointer? I tried: > > cdef int *ap=<int *> A.data > > but that doesn't seem to work (it works for double *, but not int > *). I must be missing something really simple. > 1) Be aware that the "int" is really two different types (the latter one being a C "int", the former an identifier for numpy). Better use "numpy.int32" for numpy.zeros and a datatype that you know is 32 bit int in the cdef. I think that what you have written will fail on 64- bit machines but I'm not sure (could imagine that NumPy would choose int64 as the default "int", while the C compiler would stick with 32 bit *shrug*). There are some npy_int32 etc. typedefs/defines in the NumPy header files that are better used for C types rather than plain "int".
2) It will still only work if you have exactly the array created by zero above. I.e., if you instead pass something like numpy.zeros(10, int)[::-2] for instance, then the buffer isn't modified -- instead, something called "strides" is set in the struct which tells you to iterator "-2" for every step along the 1st dimension. Same goes for numpy.zeros((5,5,5), int)[1,:,2] and so on. So a) Either make absolutely sure you have a C-contiguous array or b) Assign ap as you do, but access it like this: a[i * A.strides[0]] rather than a[i]. (a[i*A.strides[0] + j*A.strides[1]] will give you 2D access and so on) Does this answer your question? Dag Sverre _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
