[Numpy-discussion] isn't it a bug? (matrix multiplication)

2008-02-07 Thread dmitrey
from numpy import array
a = array((1.0, 2.0))

b = c = 15
b = b*a#ok
c *= a#ok

d = array(15)
e = array(15)
d = d*a#this works ok
e *= a#this intended to be same as prev line, but yields error:
Traceback (innermost last):
 File stdin, line 1, in module
ValueError: invalid return array shape
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] isn't it a bug? (matrix multiplication)

2008-02-07 Thread Christopher Barker
Alan G Isaac wrote:
 On Thu, 07 Feb 2008, dmitrey apparently wrote:
 a = array((1.0, 2.0))
 e = array(15)
 e *= a # ... yields error:
 
 You are trying to stuff in two values where
 you have only allocated space for 1.

Exactly. but to expound a bit more:

The ?= operators are in-place operators -- they attempt to modify the 
left hand side in-place. The regular math operators create a new array, 
which can be a different size than either of the two operands, thanks to 
array broadcasting.

x *= y should be the same as x = x*y iff the size of x*y is the same 
size as x.

That's why:

  e
array(15)
  a
array([ 1.,  2.])

  e*=a
fails, but:

  a*=e
  a
array([ 15.,  30.])

works.

One more note: you're doing element-wise array multiplication, not 
matrix multiplication -- a distinction that does matter sometimes.

-Chris

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/ORR(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

[EMAIL PROTECTED]
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion