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

Stephan Hoyer shoyer at gmail.com
Thu Dec 11 13:53:36 EST 2014


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).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141211/a73c2e4a/attachment.html>


More information about the NumPy-Discussion mailing list