[Numpy-discussion] Matrix computation in Matlab/Octave style
Huaiyu Zhu
HZhu at knowledgetrack.com
Mon Jun 5 20:02:43 CDT 2000
Are you craving for Matlab/Octave style expressions in Python? (For example,
A*B is matrix multiplication, not elementwise.) Now you can.
I've just made a package MatPy and started a SourceForge project for it.
It is implemented as wrappers around the Numeric and Gnuplot packages.
You can find the source codes, tests and docs on the home page
http://MatPy.sourceforge.net
The main reason I have written this package is that I'm tired of dealing
with NewAxis and have "Frame not aligned" exceptions. Now matrices and
vectors behave as one would expect from linear algebra.
Examples:
>>> from MatPy.Matrix import *
>>> A = rand((20,5))
>>> x = rand((5,1))
>>> y = A*x
>>> b = solve(A,y)
>>> norm(b-x)
1.16043535672e-15
>>> print x
[ 0.276
0.553
0.733
0.388
0.5 ]
>>> print x.T()
[ 0.276 0.553 0.733 0.388 0.5 ]
>>> print x.T()*x
[ 1.32 ]
>>> print x*x.T()
[ 0.0763 0.153 0.203 0.107 0.138
0.153 0.306 0.406 0.214 0.277
0.203 0.406 0.538 0.284 0.367
0.107 0.214 0.284 0.15 0.194
0.138 0.277 0.367 0.194 0.25 ]
>>> z = x + rand(x.shape)*1j
>>> z.H()
[ 0.276-0.606j 0.553-0.376j 0.733-0.933j 0.388-0.636j 0.5-0.314j ]
>>> z.H()*z
[ 3.2+0j ]
>>> norm(z)**2
3.2026003449
There are also matrix and elementwise versions of functions:
expm and exp, sqrtm and sqrt, etc.
Questions, comments, suggestions and helps are all very welcome.
It is a future plan to have an efficient interface to Octave to leverage its
large code base.
Enjoy!
Huaiyu <hzhu at users.sourceforge.net>
More information about the Numpy-discussion
mailing list