<div dir="ltr"><div>/* obj[ind] */<br>PyObject* DoIndex(PyObject* obj, int ind)<br>{<br> PyObject *oind, *ret;<br> oind = PyLong_FromLong(ind);<br> if (!oind) {<br> return NULL;<br> }<br> ret = PyObject_GetItem(obj, oind);<br> Py_DECREF(oind);<br> return ret;<br>}<br><br>/* obj[inds[0], inds[1], ... inds[n_ind-1]] */<br>PyObject* DoMultiIndex(PyObject* obj, int *inds, int n_ind)<br>{<br> PyObject *ret, *oind, *temp;<br> oind = PyTuple_New(n_ind);<br> if (!oind)<br> return NULL;<br><br> for (int k = 0; k < n_ind; ++k)<br> {<br> temp = PyLong_FromLong(inds[k]);<br> if (!temp)<br> Py_DECREF(oind);<br> PyTuple_SET_ITEM(oind, k, temp);<br> }<br> ret = PyObject_GetItem(obj, oind);<br> Py_DECREF(oind);<br> return ret;<br>}<br><br>/* obj[b:e:step] */<br>PyObject* DoSlice(PyObject* obj, int b, int e, int step)<br>{<br> PyObject *oind, *ret, *ob, *oe, *ostep;<br> ob = PyLong_FromLong(b);<br> if (!ob)<br> return NULL;<br> oe = PyLong_FromLong(e);<br> if (!oe) {<br> Py_DECREF(ob);<br> return NULL;<br> }<br> ostep = PyLong_FromLong(step);<br> if (!ostep) {<br> Py_DECREF(ob);<br> Py_DECREF(oe);<br> return NULL;<br> }<br> oind = PySlice_New(ob, oe, ostep);<br> Py_DECREF(ob);<br> Py_DECREF(oe);<br> Py_DECREF(ostep);<br><br> if (!oind)<br> return NULL;<br><br> ret = PyObject_GetItem(obj, oind);<br> Py_DECREF(oind);<br> return ret;<br>}<br><br></div>-Eric<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Apr 4, 2016 at 1:35 PM, mpc <span dir="ltr"><<a href="mailto:matt.p.conte@gmail.com" target="_blank">matt.p.conte@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
is there a C-API function for numpy that can implement Python's<br>
multidimensional indexing?<br>
<br>
For example, if I had a 2d array:<br>
<br>
PyArrayObject * M;<br>
<br>
and an index<br>
<br>
int i;<br>
<br>
how do I extract the i-th row M[i,:] or i-th column M[:,i]?<br>
<br>
Ideally it would be great if it returned another PyArrayObject* object (not<br>
a newly allocated one, but whose data will point to the correct memory<br>
locations of M).<br>
<br>
I've searched everywhere in the API documentation, Google, and SO, but no<br>
luck.<br>
<br>
Any help is greatly appreciated.<br>
<br>
Thank you.<br>
<br>
-Matthew<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://numpy-discussion.10968.n7.nabble.com/Multidimension-array-access-in-C-via-Python-API-tp42710.html" rel="noreferrer" target="_blank">http://numpy-discussion.10968.n7.nabble.com/Multidimension-array-access-in-C-via-Python-API-tp42710.html</a><br>
Sent from the Numpy-discussion mailing list archive at Nabble.com.<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div><br></div>