views:

66

answers:

2

Hi,

The numpy docs recommend using array instead of matrix for working with matrices. However, unlike octave (which I was using till recently), * doesn't perform matrix multiplication, you need to use the function matrixmultipy(). I feel this makes the code very unreadable.

Does anybody share my views, and has found a solution?

Thanks!

A: 

It should be same, check here http://www.scipy.org/NumPy_for_Matlab_Users (think of MATLAB as Octave clone)

Do you really have matrix type or just list of lists from python?;

ralu
A: 

The main reason to avoid using the matrix class is that a) it's inherently 2-dimensional, and b) there's additional overhead compared to a "normal" numpy array. If all you're doing is linear algebra, then by all means, feel free to use the matrix class... Personally I find it more trouble than it's worth, though.

For arrays, use dot instead of matrixmultiply.

E.g.

import numpy as np
x = np.arange(9).reshape((3,3))
y = np.arange(3)

print np.dot(x,y)

Or in newer versions of numpy, simply use x.dot(y)

Personally, I find it much more readable than the * operator implying matrix multiplication...

Joe Kington
Its unreadable when you have a stack of multiplications, for instance x'*A'*A*x.
elexhobby
@elexhobby - `x.T.dot(A.T).dot(A).dot(x)` isn't that unreadable, i.m.o. To each his own, though. If you're primarily doing matrix multiplication, then by all means, use `numpy.matrix`!
Joe Kington