[Numpy-discussion] Scalar values with a matrix

Sven Schreiber svetosch at gmx.net
Tue Jan 2 06:05:38 EST 2007


Charles R Harris schrieb:

> 
> mat(5) is a 1x1 matrix:
> 
> In [5]: mat(5)
> Out[5]: matrix([[5]])
> 
> so the error is valid. If you want to do a scalar multiply try
> 
> In [6]: amat*5
> Out[6]:
> matrix([[ 0,  5, 10, 15],
>         [20, 25, 30, 35],
>         [40, 45, 50, 55]])

Or you can do multiply(amat, mat(5)), even when everything is a matrix,
and broadcasting will work.


> This is an example where using arrays, as opposed to matrices, is
> perhaps less confusing.
> 

I beg to differ. This only shows that with matrices dot() will not
automatically broadcast everything. IMHO that can be an advantage by
preventing subtle bugs. If you want to rely on broadcasting with
matrices, you could use multiply() (or kron(), depending on what you want).

-sven
(in his don-quijote-esque fight to keep the numpy list balanced between
arrays and matrices... ;-)




More information about the NumPy-Discussion mailing list