0

Say I have three numpy.ndarray's a,b,c such that when I multiply them a broadcasting happens.

Does the result depend on the order of the multiplication?

In other words, do there exist a,b,c such that:

(a * b) * c != a * (b * c)

?

nivniv
  • 3,421
  • 5
  • 33
  • 40
  • 2
    I haven't seen formal claims or proof that broadcasting is associative, but if it wasn't it would invalidate the associativity of that multiplication, and give rise to bug reports and SO questions. – hpaulj Jul 02 '18 at 16:06

1 Answers1

0

Yes, it's associative. Broadcasting rules say that

  • The rank (number of dimensions) of the result is the max of ranks of the inputs (left-padding by 1 is used if needed).
  • The dimension along each axis is the max of dimensions along that axis (provided that the max does not involve two distinct numbers both greater than 1, in which case an error is thrown).

The function max is associative: max(a, max(b, c)) = max(max(a, b), c). Thus, the shape of the output is the same regardless of parentheses. Also, the condition under which "operands could not be broadcast" error is thrown amounts to: for each axis, all dimensions that are greater than 1 are equal; this condition does not need parentheses at all.

  • This proves that the output shape is associative. What about the outputs's elements? I think that because you are only allowed to broadcast along axes of size "1" that means that the broadcasting can only operate on one axis at a time, which makes it independent of the order of the axes, right? – nivniv Jul 04 '18 at 10:12