[SciPy-user] array += matrix leads to ValueError

Pauli Virtanen pav@iki...
Fri Dec 19 12:08:28 CST 2008


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



More information about the SciPy-user mailing list