[Numpy-discussion] Scalar values with a matrix
Charles R Harris
charlesr.harris at gmail.com
Mon Jan 1 22:44:48 EST 2007
On 1/1/07, Colin J. Williams <cjw at sympatico.ca> wrote:
>
> What is the best way to treat scalar values from a matrix?
>
> It seems that it's best to treat them as a simple Python values and not
> to leave them wrapped up in the ndarray structure. I would welcome
> advice.
>
> The problem is illustrated below.
>
> Colin W.
>
> # tMul.py
> import numpy.core as _n
> a= _n.arange(12).reshape((3, 4))
> b= _n.arange(12).reshape((4, 3))
> c= _n.dot(a, b)
> print `c`
> amat= _n.mat(a)
> bmat= _n.mat(b)
> cmat= amat*bmat
> print cmat
> print _n.dot(amat, _n.mat(5))
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]])
This is an example where using arrays, as opposed to matrices, is perhaps
less confusing.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070101/0ab0f73e/attachment.html>
More information about the NumPy-Discussion
mailing list