Numpy ndarray to C array

E

Enrico

I am trying to pass a multi-dimensional ndarray to C as a multi-
dimensional C array for the purposes of passing it to mathematica.
They already have a wrapper for a 1-D Python list. where the list is
copied to "list". Shown below:

static PyObject * mathlink_PutIntegerList(mathlink_Link *self,
PyObject *args)
{
PyObject* seq;
PyObject* obj;
long i, len, result;
int* list;

len = PyObject_Length(seq);

list = PyMem_New(int, len);
for(i = 0; i < len; i++)
{
obj = PySequence_GetItem(seq, i);
list = PyInt_AsLong(obj);
}

CheckForThreadsAndRunLink(self,result = MLPutIntegerList(self->lp,
list, len));

PyMem_Free(list);
CHECKNOTEQUAL(result,MLSUCCESS,self);

Py_INCREF(Py_None);
return Py_None;
}

I would like to create a similar wrapper which accepts an ndarray and
provides the array laid out in memory like a C array declared
explicitly as "int a[m][n]...". I also need to pass the length of the
array at each level i as dim. Since this is pretty much the only
function I plan to wrap, I'd like to avoid using boost, swig, etc.

Any help would be appreciated.
 
R

Robert Kern

I am trying to pass a multi-dimensional ndarray to C as a multi-
dimensional C array for the purposes of passing it to mathematica.
They already have a wrapper for a 1-D Python list. where the list is
copied to "list". Shown below:
I would like to create a similar wrapper which accepts an ndarray and
provides the array laid out in memory like a C array declared
explicitly as "int a[m][n]...". I also need to pass the length of the
array at each level i as dim. Since this is pretty much the only
function I plan to wrap, I'd like to avoid using boost, swig, etc.


You will find it better to ask numpy questions on the numpy mailing list:

http://www.scipy.org/Mailing_Lists

In this case, you are looking for the PyArray_AsCArray() function:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top