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)
?
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)
?
Yes, it's associative. Broadcasting rules say that
max
of ranks of the inputs (left-padding by 1
is used if needed). 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.