<br><br><div><span class="gmail_quote">On 1/1/07, <b class="gmail_sendername">Colin J. Williams</b> <<a href="mailto:cjw@sympatico.ca">cjw@sympatico.ca</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
What is the best way to treat scalar values from a matrix?<br><br>It seems that it's best to treat them as a simple Python values and not<br>to leave them wrapped up in the ndarray structure.  I would welcome advice.<br>
<br>The problem is illustrated below.<br><br>Colin W.<br><br># tMul.py<br>import numpy.core as _n<br>a= _n.arange(12).reshape((3, 4))<br>b= _n.arange(12).reshape((4, 3))<br>c= _n.dot(a, b)<br>print `c`<br>amat= _n.mat(a)<br>
bmat= _n.mat(b)<br>cmat= amat*bmat<br>print cmat<br>print _n.dot(amat, _n.mat(5))</blockquote><div><br>mat(5) is a 1x1 matrix:<br><br><span style="font-family: courier new,monospace;">In [5]: mat(5)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Out[5]: matrix([[5]])</span><br><br>so the error is valid. If you want to do a scalar multiply try<br><br><span style="font-family: courier new,monospace;">In [6]: amat*5</span>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Out[6]: </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">matrix([[ 0,  5, 10, 15],
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        [20, 25, 30, 35],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
        [40, 45, 50, 55]])</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span></div></div>This is an example where using arrays, as opposed to matrices, is perhaps less confusing.
<br><br>Chuck<br>