[Numpy-discussion] Scalar values with a matrix

Colin J. Williams cjw at sympatico.ca
Mon Jan 1 19:51:02 EST 2007


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))

Output:
>>>
array([[ 42,  48,  54],
       [114, 136, 158],
       [186, 224, 262]])
[[ 42  48  54]
[114 136 158]
[186 224 262]]
Traceback (most recent call last):
  File "<string>", line 74, in run_nodebug
  File "C:\Documents and Settings\cjw\My
Documents\PyDev\PyMatrix\tMiul.py", line 11, in <module>
    print _n.dot(amat, _n.mat(5))
ValueError: objects are not aligned

>>>






More information about the NumPy-Discussion mailing list