[Numpy-discussion] Broadcasting rules (Ticket 76).

Sasha ndarray at mac.com
Mon Apr 24 11:49:04 CDT 2006


On 4/24/06, Travis Oliphant <oliphant.travis at ieee.org> wrote:
> [...]
> A copying operation is not handled using the same rules.   In this case,
> for example, Numeric used to raise an error but in NumPy the array will
> be copied as many times as possible into the array.  I don't believe
> ticket #76 is actually an error.
>
I disagree on the terminology.  In my view broadcasting means
repeating the values of the array to fit into a different shape no
matter what dictates the new shape an operand or the receiver.

IMHO the following is slightly confusing:

>>> a = zeros([5,2])
>>> a[...] += arange(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: shape mismatch: objects cannot be broadcast to a single shape

but
>>> a[...] = arange(5)

is ok.


> This behavior could be changed if somebody wants to write the code to
> change it but only until version 1.0.   It would be very difficult to
> change the other broadcasting behavior which was inherited from Numeric,
> however.  The only possibility I see is adding new useful functionality
> where Numeric used to raise an error.

In this category, I would suggest to allow broadcasting to any
multiple of the dimension even if the dimension is not 1.  I don't see
what makes 1 so special.


>>> x = zeros(4)
>>> x+(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: shape mismatch: objects cannot be broadcast to a single shape
>>> x+(1,)
array([1, 1, 1, 1])

I suggest that we make ufunc sonsistent with slice assignment.  Currently:

>>> x[:]=1,1
>>> x[:]=1,1,1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: number of elements in destination must be integer multiple
of number of elements in source




More information about the Numpy-discussion mailing list