Passing Numpy arrays to C code wrapped with Cython

8 views
Skip to first unread message

Joshua

unread,
Dec 20, 2010, 8:34:19 PM12/20/10
to cython-users
I feel like this should be a fairly straightforward application of
Cython, but I'm stumped and can't seem to find the proper example or
piece of documentation to do this. I'm trying to wrap a small amount
of existing c code that does a bunch of operations on arrays. I want
to be able to set up a number of numpy arrays, and then pass those
arrays as arguments to the C code whose functions take standard c
arrays (1d and 2d). I'm a little stuck in terms of figuring out how to
write the proper .pyx code to properly handle things.

There are a handful of functions, but a typical function in the file
funcs.h looks something like:

double func1(double *A, double **coords1, double **coords2, const int
len)

I have a .pyx file that has a corresponding line:

cdef extern from "funcs.h":
double func1(double *A, double **coords1, double **coords2, int
len)

where I got rid of the const because cython doesn't support it
(correct?). Where I'm stuck is what the wrapper code should then look
like to pass a MxN numpy array to the **coords1 and **coords2
arguments and a Kx1 numpy array to *A.

Any suggestions or pointers to concrete examples would be most
appreciated.

Robert Kern

unread,
Dec 21, 2010, 12:35:35 PM12/21/10
to cython...@googlegroups.com

http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_AsCArray

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Reply all
Reply to author
Forward
0 new messages