In [120]: shapes = [6], [4, 2, 3, 1], [2, 1, 1]
In [121]: arrs = np.broadcast_arrays(*[np.empty(shape,int) for shape in shapes])
...:
In [122]: [a.shape for a in arrs]
Out[122]: [(4, 2, 3, 6), (4, 2, 3, 6), (4, 2, 3, 6)]
In [124]: np.lib.stride_tricks._broadcast_shape(*[np.empty(shape,int) for shape
...: in shapes])
Out[124]: (4, 2, 3, 6)
In [131]: np.broadcast(*[np.empty(shape,int) for shape in shapes]).shape
Out[131]: (4, 2, 3, 6)
The second times quite a bit faster, 4.79 µs vs 42.4 µs. Third is a tad faster.
As I first commented, I started with broadcast_arrays
, and looked at the code. That me to _broadcast_shape
, and then to np.broadcast
.