Numpy ndarray to C array

Enrico enricong at gmail.com
Thu Dec 8 14:56:42 EST 2011


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[i] = 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[i].  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.



More information about the Python-list mailing list