[Numpy-discussion] Add a function to broadcast arrays to a given shape to numpy's stride_tricks?

Jaime Fernández del Río jaime.frio at gmail.com
Fri Dec 12 09:25:03 EST 2014


On Fri, Dec 12, 2014 at 5:57 AM, Sebastian Berg <sebastian at sipsolutions.net>
wrote:
>
> On Fr, 2014-12-12 at 05:48 -0800, Jaime Fernández del Río wrote:
> > On Thu, Dec 11, 2014 at 10:53 AM, Stephan Hoyer <shoyer at gmail.com>
> > wrote:
> >         On Thu, Dec 11, 2014 at 8:17 AM, Sebastian Berg
> >         <sebastian at sipsolutions.net> wrote:
> >                 One option
> >                 would also be to have something like:
> >
> >                 np.common_shape(*arrays)
> >                 np.broadcast_to(array, shape)
> >                 # (though I would like many arrays too)
> >
> >                 and then broadcast_ar rays could be implemented in
> >                 terms of these two.
> >
> >
> >         It looks like np.broadcast let's us write the common_shape
> >         function very easily;
> >
> >
> >         def common_shape(*args):
> >             return np.broadcast(*args).shape
> >
> >
> >         And it's also very fast:
> >         1000000 loops, best of 3: 1.04 µs per loop
> >
> >         So that does seem like a feasible refactor/simplification for
> >         np.broadcast_arrays.
> >
> >
> >         Sebastian -- if you're up for writing np.broadcast_to in C,
> >         that's great! If you're not sure if you'll be able to get
> >         around to that in the near future, I'll submit my PR with a
> >         Python implementation (which will have tests that will be
> >         useful in any case).
> >
> >
> > np.broadcast is the Python object of the old iterator. It may be a
> > better idea to write all of these functions using the new one,
> > np.nditer:
> >
> >
> > def common_shape(*args):
> >     return np.nditer(args).shape[::-1]  # Yes, you do need to reverse
> > it!
> >
> >
> > And in writing 'broadcast_to', rather than rewriting the broadcasting
> > logic, you could check the compatibility of the shape with something
> > like:
> >
> >
> > np.nditer((arr,), itershape=shape)  # will raise ValueError if shapes
> > incompatible
> >
> >
> >
> > After that, all that would be left is some prepending of zero strides,
> > and some zeroing of strides of shape 1 dimensions before calling
> > as_strided
> >
>
> Hahaha, right there is the 32 limitation, but you can also (ab)use it:
>
> np.nditer(np.arange(10), itershape=(5, 10)).itviews[0]


That's neat! But itviews is not even listed in the attributes of nditer in
the docs, we should fix that.

Is the 32 argument limitation really a concern? Because that aside, it
seems that all the functionality that has been discussed are one-liners
using nditer: do we need new functions, or better documentation?

Jaime

-- 
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141212/de43cf72/attachment.html>


More information about the NumPy-Discussion mailing list