array += matrix leads to ValueError
![](https://secure.gravatar.com/avatar/eba6f06b5dfa5885c37acd2eb2089798.jpg?s=120&d=mm&r=g)
I just stumbled across the following behaviour: from scipy import * a=array([0,1]) m=matrix([0,1]) # this works: a=a+m a #[Out]# matrix([[0, 2]]) a=array([0,1]) # this works too m+=a m #[Out]# matrix([[0, 2]]) # however, this does not work: a+=m I get: <type 'exceptions.ValueError'>: invalid return array shape Can anyone explain me why it is implemented this way? I mean, it is not that intuitive. Bastian
![](https://secure.gravatar.com/avatar/da3a0a1942fbdc5ee9a9b8115ac5dae7.jpg?s=120&d=mm&r=g)
Fri, 19 Dec 2008 18:31:33 +0100, Bastian Weber wrote:
I just stumbled across the following behaviour:
from scipy import * a=array([0,1]) m=matrix([0,1]) a=a+m m+=a a+=m <type 'exceptions.ValueError'>: invalid return array shape
Can anyone explain me why it is implemented this way? I mean, it is not that intuitive.
In-place operations require that the result is broadcastable [1] to the same size as the left-hand side. a.shape == (2,) m.shape == (1,2) (a+m).shape == (1,2) (2,) is broadcastable to (1,2), but (1,2) is not broadcastable to (2,). Hence, m += a is possible, but a += m is not. .. [1] http://docs.scipy.org/doc/numpy/reference/ufuncs.html#broadcasting -- Pauli Virtanen
![](https://secure.gravatar.com/avatar/eba6f06b5dfa5885c37acd2eb2089798.jpg?s=120&d=mm&r=g)
... Can anyone explain me why it is implemented this way? I mean, it is not that intuitive.
In-place operations require that the result is broadcastable [1] to the same size as the left-hand side.
OK, thanks for making that clear. Although I still dont think it is very intuitive, at least I understand the reason. Bastian
participants (2)
-
Bastian Weber
-
Pauli Virtanen