[Numpy-discussion] Giving numpy the ability to multi-iterate excluding an axis

Mark Wiebe 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], [[0],[1]])
for x in i:
    print "inner:"
    for y in j:
        print y[0], y[1]


0 0
0 1
0 2
1 0
1 1
1 2

and C code for nested iteration looks something like this:

        NpyIter *iter1, *iter1;
        NpyIter_IterNext_Fn iternext1, iternext2;
        char **dataptrs1;

         * 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);

        do {
            NpyIter_ResetBasePointers(iter2, dataptrs1);
            do {
                /* Use the iter2 values */
            } while (iternext2(iter2));
        } while (iternext1(iter1));

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110104/2ac13e81/attachment.html>

More information about the NumPy-Discussion mailing list