Props to numpy programmers!

A

Andrew Felch

So I wanted to matrixmultiply

[[a,b],
[c,d]]

*

[e,f]

Of course this is impossible, because the number of columns in the
first factor is not equal to the number of rows in the second. Wrong!
It is impossible because the second matrix is rank1! So numpy
happily converts it to a column vector so the multiplication will
work, and converts the answer back into a rank1 vector!!!!

I love NUMPY!!!!!!!!!!!

I was reading my code and thought I had a bug, I couldn't figure out
why the code was still working right! It's because the numpy people
are geniouses!

Hooray numpy!!!!!!!!!! Numpy is smarter than me!
 
D

Duncan Smith

Andrew Felch said:
So I wanted to matrixmultiply

[[a,b],
[c,d]]

*

[e,f]

Of course this is impossible, because the number of columns in the
first factor is not equal to the number of rows in the second. Wrong!
It is impossible because the second matrix is rank1! So numpy
happily converts it to a column vector so the multiplication will
work, and converts the answer back into a rank1 vector!!!!

I love NUMPY!!!!!!!!!!!

I was reading my code and thought I had a bug, I couldn't figure out
why the code was still working right! It's because the numpy people
are geniouses!

Hooray numpy!!!!!!!!!! Numpy is smarter than me!

I love Numpy too. But I wouldn't want this multiplication to work if the
second array was a row vector (and there's no explicit distinction because
of the array's shape).
array([[0, 1],
[2, 3]])
b array([4, 5])
Numeric.matrixmultiply(a,b) array([ 5, 23])
Numeric.matrixmultiply(b,a)
array([10, 19])

One of these multiplications should fail, depending on whether b is a column
or row vector.

Make it a column vector by reshaping,
array([[4],
[5]])

and it does the right thing.
array([[ 5],
[23]])
Traceback (most recent call last):
File "<pyshell#60>", line 1, in -toplevel-
Numeric.matrixmultiply(b,a)
File "C:\Python23\lib\site-packages\Numeric\Numeric.py", line 335, in dot
return multiarray.matrixproduct(a, b)
ValueError: matrices are not aligned
Much safer IMHO.

Duncan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top