[Numpy-discussion] Giving numpy the ability to multi-iterate excluding an axis
mwwiebe at gmail.com
Tue Jan 4 15:04:34 EST 2011
On Sat, Jan 1, 2011 at 11:23 AM, John Salvatier
<jsalvati at u.washington.edu>wrote:
> This thread is a bit old, but since it's not possible to use the C-API is
> possible to accomplish this same thing with the Python API?
I've committed Python exposure for nested iteration to the new_iterator
branch. In doing so, I also changed the mechanism in C. I found that it
was simpler to expose to Python if I added a Reset function which gives new
base data pointers, and this also simplifies C code using nested iterators.
The Python code
a = arange(2).reshape(2,1)
b = arange(3).reshape(1,3)
i, j = np.nested_iters([a,b], [,])
for x in i:
for y in j:
print y, y
and C code for nested iteration looks something like this:
NpyIter *iter1, *iter1;
NpyIter_IterNext_Fn iternext1, iternext2;
* With the exact same operands, no copies allowed, and
* no axis in op_axes used both in iter1 and iter2.
* Buffering may be enabled for iter2, but not for iter1.
iter1 = ...; iter2 = ...;
iternext1 = NpyIter_GetIterNext(iter1);
iternext2 = NpyIter_GetIterNext(iter2);
dataptrs1 = NpyIter_GetDataPtrArray(iter1);
/* Use the iter2 values */
} while (iternext2(iter2));
} while (iternext1(iter1));
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion