[Numpy-discussion] C-API: multidimensional array indexing?

75 views
Skip to first unread message

Johann Bauer

unread,
Jul 27, 2011, 5:37:50 PM7/27/11
to numpy-di...@scipy.org
Dear experts,

is there a C-API function for numpy which implements Python's
multidimensional indexing? Say, I have a 2d-array

PyArrayObject * M;

and an index

int i;

how do I extract the i-th row or column M[i,:] respectively M[:,i]?

I am looking for a function which gives again a PyArrayObject * and
which is a view to M (no copied data; the result should be another
PyArrayObject whose data and strides points to the correct memory
portion of M).

I searched the API documentation, Google and mailing lists for quite a
long time but didn't find anything. Can you help me?

Thanks, Johann
_______________________________________________
NumPy-Discussion mailing list
NumPy-Di...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Mark Wiebe

unread,
Jul 27, 2011, 6:25:10 PM7/27/11
to Discussion of Numerical Python
Probably the easiest way is to emulate what Python is doing in M[i,:] and M[:,i]. You can create the : with PySlice_New(NULL, NULL, NULL), and the i with PyInt_FromLong. Then create a tuple with Py_BuildValue and use PyObject_GetItem to do the slicing.

It is possible to do the same thing directly in C, but as you've found there aren't convenient APIs for this yet.

Cheers,
Mark

Johann Bauer

unread,
Jul 27, 2011, 7:21:33 PM7/27/11
to Discussion of Numerical Python
Thanks, Mark! Problem solved.
Reply all
Reply to author
Forward
0 new messages