[Numpy-discussion] Help with wrapper ndarray to c array

Enrico Ng mail at enricong.com
Thu Dec 8 20:14:48 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] and the depth.  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, looks like I should use PyArray_AsCArray but
I'm having trouble finding examples that work for me.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20111208/de8888c5/attachment.html>


More information about the NumPy-Discussion mailing list