[Numpy-discussion] Should concatenate broadcast shapes?

Jaime Fernández del Río jaime.frio at gmail.com
Wed Aug 27 12:44:58 EDT 2014


After reading this stackoverflow question:

http://stackoverflow.com/questions/25530223/append-a-list-at-the-end-of-each-row-of-2d-array

I was reminded that the `np.concatenate` family of functions do not
broadcast the shapes of their inputs:

>>> import numpy as np
>>> a = np.arange(6).reshape(3, 2)
>>> b = np.arange(6, 8)
>>> np.concatenate((a, b), axis=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: all the input arrays must have same number of dimensions
>>> np.concatenate((a, b[None]), axis=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: all the input array dimensions except for the concatenation
axis must match exactly
>>> np.concatenate((a, np.tile(b[None], (a.shape[0], 1))), axis=1)
array([[0, 1, 6, 7],
       [2, 3, 6, 7],
       [4, 5, 6, 7]])

But there doesn't seem to be any fundamental reason why they shouldn't:

>>> from numpy.lib.stride_tricks import as_strided
>>> b_ = as_strided(b, (a.shape[0],)+b.shape, (0,)+b.strides)
>>> np.concatenate((a, b_), axis=1)
array([[0, 1, 6, 7],
       [2, 3, 6, 7],
       [4, 5, 6, 7]])

Is there any fundamental interface design reason why things are the way
they are? Or is it simply that no one has implemented broadcasting for
these functions? Without thinking much about it, I am +1 on doing this...
At the least, it would probably be good to add a note to the docs
explaining why broadcasting is not implemented.

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/20140827/a7ddc88e/attachment.html>


More information about the NumPy-Discussion mailing list