MATLAB2Python

S

Sarge

Hi, everybody!
I'm a moderately experienced programmer in Matlab, and looking for a
free computational language (Matlab is *REALLY* expensive for an
individual, like me).
I came across Python (actually Scipy) and immediately felt
comfortable with the interface and syntax.
I'm still a newbe, so, before attempting a serious work on it, I'd
like to hear any opinion about migration from Matlab to Python, and
also a rough comparison between these two languages.

Thanx in advance, and excuse me for my very bad English (I'm not a
native English speaker),

Sarge
 
C

Cameron Laird

Hi, everybody!
I'm a moderately experienced programmer in Matlab, and looking for a
free computational language (Matlab is *REALLY* expensive for an
individual, like me).
I came across Python (actually Scipy) and immediately felt
comfortable with the interface and syntax.
I'm still a newbe, so, before attempting a serious work on it, I'd
like to hear any opinion about migration from Matlab to Python, and
also a rough comparison between these two languages.
.
.
.
1. I run into quite a few people making a transition
from Matlab to Python; invariably they say that
the change is working out better than they expected.
Very roughly speaking, Python allows for more
abstraction and expressive power that expands their
horizons beyond what they achieved with Matlab.
2. They're different, though, and Matlab certainly
boasts a massive collection of special-purpose
add-ons that will take a while to replace. Also,
for some functions, Matlab is much faster (and for
others, Python is much faster).
3. For the most conservative transition, you'll want
to learn about Octave.

A reference to Octave and more appear at <URL:
http://phaseit.net/claird/comp.programming/open_source_science.html >.
 
E

Edward C. Jones

Cameron said:
.
.
.
1. I run into quite a few people making a transition
from Matlab to Python; invariably they say that
the change is working out better than they expected.
Very roughly speaking, Python allows for more
abstraction and expressive power that expands their
horizons beyond what they achieved with Matlab.
2. They're different, though, and Matlab certainly
boasts a massive collection of special-purpose
add-ons that will take a while to replace. Also,
for some functions, Matlab is much faster (and for
others, Python is much faster).
3. For the most conservative transition, you'll want
to learn about Octave.

A reference to Octave and more appear at <URL:
http://phaseit.net/claird/comp.programming/open_source_science.html >.

Also check out numarray and PIL.
 
J

John Hunter

Nick> Also look at Matplotlib for Matlab-like plotting:
Nick> http://matplotlib.sourceforge.net/

I find python + numeric/numarray + MLAb + scipy + matplotlib to be a
very workable replacement for matlab, but I'm biased :)

Actually, I used to work all the time in matlab and wrote some fairly
complex applications in it. I just sort of hit the wall at some point
when I was trying to do networking, dbases, complex data structures,
and so on, in matlab. You *can* do it with the matlab + the built-in
JVM, but it's not easy, pretty, or fast.

At some point I found myself doing all my work in python and dumping
the results to data files for plotting in matlab. Since that is a
frustrating solution, I bit the bullet and wrote matplotlib, with the
goal of making plots that look as good as matlab's, and were as easy
to create.

Here is a little comparison of a script to generate some white noise,
convolve it with a low pass filter, and make two plots, one of the
time series and one of the power spectrum.

First in matlab

dt = 0.01;
t = [0:dt:10];
nse = randn(size(t));
r = exp(-t/0.05);
cnse = conv(nse, r)*dt;
cnse = cnse(1:length(t));
s = 0.1*sin(2*pi*t) + cnse;

figure(1)
plot(t,s)

figure(2)
psd(s, 512, 1/dt)

And then in matplotlib with a little help from numeric and friends

from matplotlib.matlab import *

dt = 0.01
t = arange(0,10,dt)
nse = randn(len(t))
r = exp(-t/0.05)

cnse = convolve(nse, r, mode=2)*dt
cnse = cnse[:len(t)]
s = 0.1*sin(2*pi*t) + cnse

figure(1)
plot(t,s)

figure(2)
psd(s, 512, 1/dt)

show()

Cheers,
John Hunter
 
B

beliavsky

Sarge said:
Hi, everybody!
I'm a moderately experienced programmer in Matlab, and looking for a
free computational language (Matlab is *REALLY* expensive for an
individual, like me).

You should consider the (compiled) F programming language, which is a
free subset of Fortran 95 -- see http://www.fortran.com/F/index.html.
F and F95 both have array operations comparable to Matlab. You can get
a syntax comparison of Matlab and Fortran at
http://www.owlnet.rice.edu/~mech517/F90_Overview.html . Fortran 95
compilers are not in general free but much are less expensive than
Matlab. The Intel Fortran 95 compiler is free on Linux for
noncommercial use, and the open-source G95 compiler on Linux is usable
(but not yet mature) -- see http://g95.sourceforge.net/ .
I came across Python (actually Scipy) and immediately felt
comfortable with the interface and syntax.
I'm still a newbe, so, before attempting a serious work on it, I'd
like to hear any opinion about migration from Matlab to Python, and
also a rough comparison between these two languages.

I have found numerous cases where Python programs run 10-100 times
slower or more than a comparable Fortran 95 program. Look up my
previous posts here. Although both Matlab and Python are scripting
languages, much more effort has gone into optimizing the former for
speed. If execution time is more important than scripting convenience,
I recommend Fortran over Python.

An indication that Python takes the interests of beginning programmers
more seriously than those of experienced scientific programmers is
that integer division is going to be redefined in future versions of
the language -- 2/3 = 1 now but will equal 1.5 in the future. (I think
you are supposed to use 2//3). The Fortran standards committee takes
backwards compatibility much more seriously, so that code you write
now will not take on a new meaning in future versions of the language.
 
P

Peter Hansen

The Fortran standards committee takes
backwards compatibility much more seriously, so that code you write
now will not take on a new meaning in future versions of the language.

This is an unfair characterization. They most certainly take
backwards compatibility *seriously*, but perhaps they put a
higher value on making changes that, in their opinion, make
significant improvements to the language, or fix serious
mistakes they made in the original. Maybe that's a reason
that Python is being adopted more, while FORTRAN growth is, uh,
somewhat flat.

I really doubt they sit around in their plush leather chairs,
stroking their long-haired Persian cats, and saying "But won't
that break old code? Yes, but screw the scientific programmers,
BWAHAHAHAHA...."

But I could be wrong.

-Peter
 
C

Colin J. Williams

Sarge said:
Hi, everybody!
I'm a moderately experienced programmer in Matlab, and looking for a
free computational language (Matlab is *REALLY* expensive for an
individual, like me).
I came across Python (actually Scipy) and immediately felt
comfortable with the interface and syntax.
I'm still a newbe, so, before attempting a serious work on it, I'd
like to hear any opinion about migration from Matlab to Python, and
also a rough comparison between these two languages.

Thanx in advance, and excuse me for my very bad English (I'm not a
native English speaker),

Sarge
Sarge,

You might like to take a look at PyMatrix. It is still very much a
development project, but I would appreciate your thoughts on the areas
where it is deficient, as compared with MatLab.
http://www3.sympatico.ca/cjw/PyMatrix

Colin W.
 
J

John Hunter

Colin> You might like to take a look at PyMatrix. It is still
Colin> very much a development project, but I would appreciate
Colin> your thoughts on the areas where it is deficient, as
Colin> compared with MatLab.
Colin> http://www3.sympatico.ca/cjw/PyMatrix

I looked over your site and examples, but didn't see any explanation
of what PyMatrix provides over the required numarray and all the
helpful linear algebra and summary functions it provides in
numarray.linear_algebra and numarray.linear_algebra.mlab. With the
exception of Hilbert, most of the code seems like a wrapper of
numarray functionality.

Is the main difference how you define the default behavior of
operators? Or is this just a starting point for a much larger
package?

Thanks,
JDH
 
J

John J. Lee

Peter Hansen said:
This is an unfair characterization. They most certainly take
backwards compatibility *seriously*, but perhaps they put a
higher value on making changes that, in their opinion, make
significant improvements to the language, or fix serious
mistakes they made in the original.

Yeah. I was initially shocked that they'd even consider such a
change, but I was persuaded that a) it was significantly useful, b) it
was taken after very careful consideration of what the Right Thing
was, c) it's not as scary a change as it looks.

Maybe that's a reason
that Python is being adopted more, while FORTRAN growth is, uh,
somewhat flat.
[...]

I was going to claim that's a weak point: Fortran growth is flat
because everyone who could benefit from a fast, numerical
analysis-friendly compiler is already using Fortran. On reflection,
though, I guess that's not true: there are a lot of people using
languages like C++ to write this sort of numerical code. Swapping
Fortran for C++ certainly seems a questionable decision now: keep the
Fortran, and add some Python.


John
 
A

Alex Yakovlev

I'm a moderately experienced programmer in Matlab, and looking for a
free computational language (Matlab is *REALLY* expensive for an
individual, like me).
I came across Python (actually Scipy) and immediately felt
comfortable with the interface and syntax.

May be SciLab ? It's not Python, it's free Mathlab-compatible pakage.
http://scilabsoft.inria.fr/
 
S

Sarge

I have found numerous cases where Python programs run 10-100
times slower or more than a comparable Fortran 95 program.

Uh-oh. This is not fine.
I want to switch to py in order to achieve more flexibility
especially in the areas of GUIs, distributed computing, and oo-
programming. But I don't want my programs run 100 times slower!
Is it possible to integrate some fortran compiled routines in order
to speed up the more time-consuming steps?

Thanx,
Sarge

P.S. I have already tried Fortran95 for a while, but I found it's a
very old-fashioned language, more than Matlab itself, I didn't like
it very much.
 
S

Sarge

Il 29 apr 2004, John Hunter ha scritto:
I find python + numeric/numarray + MLAb + scipy + matplotlib to
be a very workable replacement for matlab,

[cut]

I'm impressed. Especially I liked the scipts, awesome!

Please help me a little more, would you?

Now, I work on WindosXP and on MDK Linux, I have found on the
Internet and downloaded all the packages you were talking about:
python, numeric, MLAb, scipy, matplotlib, but I can't figure out in
which order (and where) I have to install them. I don't want to mess
things up, because the Windoze box is at work and I have to ask for
administrator priviledges in order to install/uninstall.

Thanx,
Sarge
 
R

Robert Kern

Sarge said:
Il 29 apr 2004, John Hunter ha scritto:

I find python + numeric/numarray + MLAb + scipy + matplotlib to
be a very workable replacement for matlab,


[cut]

I'm impressed. Especially I liked the scipts, awesome!

Please help me a little more, would you?

Now, I work on WindosXP and on MDK Linux, I have found on the
Internet and downloaded all the packages you were talking about:
python, numeric, MLAb, scipy, matplotlib, but I can't figure out in
which order (and where) I have to install them. I don't want to mess
things up, because the Windoze box is at work and I have to ask for
administrator priviledges in order to install/uninstall.

Do you mind a hefty download? If not, then try Enthought's distribution
of Python[1]. matplotlib seems to have an exe installer for Windows, so
I assume it's straightforward.

But, in short, that order is correct (the last three all depend on Numeric).

[1] http://www.enthought.com/python/
Thanx,
Sarge

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
R

Robert Kern

Sarge said:
Uh-oh. This is not fine.
I want to switch to py in order to achieve more flexibility
especially in the areas of GUIs, distributed computing, and oo-
programming. But I don't want my programs run 100 times slower!
Is it possible to integrate some fortran compiled routines in order
to speed up the more time-consuming steps?

Why, yes! http://cens.ioc.ee/projects/f2py2e/

In my experience, the 10-100 factor only arises when you are doing all
the looping in Python. If you can utilize Numeric arrays sufficiently,
most of the looping goes down into fast C. So be wary of premature
optimization: sometimes the Python code will run just as fast or fast
enough (or faster!) than calling out to FORTRAN or C or C++.
Thanx,
Sarge

P.S. I have already tried Fortran95 for a while, but I found it's a
very old-fashioned language, more than Matlab itself, I didn't like
it very much.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
J

John Hunter

Robert> Do you mind a hefty download? If not, then try Enthought's
Robert> distribution of Python[1]. matplotlib seems to have an exe
Robert> installer for Windows, so I assume it's straightforward.

Robert> But, in short, that order is correct (the last three all
Robert> depend on Numeric).

Robert> [1] http://www.enthought.com/python/

Seconded - that is what I recommend on the matplotlib web site for
windows users - enthought then matplotlib. See the windows section on
http://matplotlib.sourceforge.net/installing.html. enthought will
give you numeric and scipy (and VTK which is awesome for 3D) and
almost all of matplotlib will work out of the box with enthought on
windows.

matplotlib supports different GUI environments. On windows tkagg or
wxagg are the natural choices and they come with enthought. On a
modern linux box, gtkagg is a natural choice, but may require you to
upgrade your pygtk (grkagg also runs great on windows but require a
couple of extra packages). Switching between backends on different
platforms is mainly seamless - your matplotlib scripts are unchanged
and the navigation controls are the same. The choice is made in a
configuration file
http://matplotlib.sourceforge.net/faq.html#MATPLOTLIBRC See
http://matplotlib.sourceforge.net/backends.html and
http://matplotlib.sourceforge.net/faq.html#WHICHBACKEND for details.

As for install order on linux, something like

python2.2 or later
a python GUI (wxpython, tkinter, or pygtk)
numeric or numarray
scipy (optional)
matplotlib # set the numerix var to reflect your numarray/numpy choice

matplotlib provides a numeric/numarray compatibility interface
(written by Todd Miller, one of lead numarray developers) so you can
work with either numeric or numarray transparently. Be forewarned
that scipy uses numeric so you may want to go with this if you plan on
using scipy heavily.

JDH
 
J

John J. Lee

Sarge said:
Uh-oh. This is not fine.
I want to switch to py in order to achieve more flexibility
especially in the areas of GUIs, distributed computing, and oo-
programming. But I don't want my programs run 100 times slower!

Don't mean to sound grumpy, but anybody who takes a bald statement
like that you quote above as meaningful, without thinking about what
it means, deserves what they get.

Is it possible to integrate some fortran compiled routines in order
to speed up the more time-consuming steps?

Who doesn't, in this field?

Despite the fact that one often writes more lines of Python code than
of Fortran / C / C++, many apps that do this kind of work can be
regarded, for the purposes of CPU-efficiency, to be a bunch of Fortran
/ C / C++ code being driven by Python. The idea is that you do the
heavy lifting with Fortran, wrap it (as SciPy does, for example), and
then solve your problem using Python, thus getting the best of both
worlds. Exactly what you need to wrap to achieve acceptable
performance, is a "how long is a piece of string" question, which is
why the quote about '10-100' times is close to meaningless if
unqualified.

Lots of people have heavy numerical applications that they've written
mostly in Python. This state of affairs is thanks to the work of many
clever people who've written good Fortran libraries and compilers, of
course. And maybe you'll be unlucky and have to write a relatively
large amount of code in Fortran. Who cares? Are you going to write
*everything* in Fortran and work twice as hard as a result? Maybe, if
you really care as much about performance as most people mistakenly
P.S. I have already tried Fortran95 for a while, but I found it's a
very old-fashioned language, more than Matlab itself, I didn't like
it very much.

Good luck <wink>


John
 
B

beliavsky

Sarge said:
Hi, everybody!
I'm a moderately experienced programmer in Matlab, and looking for a
free computational language (Matlab is *REALLY* expensive for an
individual, like me).
I came across Python (actually Scipy) and immediately felt
comfortable with the interface and syntax.
I'm still a newbe, so, before attempting a serious work on it, I'd
like to hear any opinion about migration from Matlab to Python, and
also a rough comparison between these two languages.

Thanx in advance, and excuse me for my very bad English (I'm not a
native English speaker),

Sarge

In both Matlab and Fortran arrays indices start with 1 (at least by
default), and x(3:5) references elements x(3),x(4),x(5), whereas in
Python arrays start with 0 and x[3:5] = [x[3],x[4]] -- the element
corresponding to the upper bound is not included in the slice. That is
one issue to watch for.
 
C

Colin J. Williams

John said:
Colin> You might like to take a look at PyMatrix. It is still
Colin> very much a development project, but I would appreciate
Colin> your thoughts on the areas where it is deficient, as
Colin> compared with MatLab.
Colin> http://www3.sympatico.ca/cjw/PyMatrix

I looked over your site and examples, but didn't see any explanation
of what PyMatrix provides over the required numarray
You are right, the documentation needs improving.

The following is illustrative of matrix usage:
# tRegrn.py To use linear regresssion to illustrate matrix usage
# Note: PyMatrix provides a function to handle this sort of thing
import PyMatrix.matrix as m

# Suppose we have A*x= b, where A (a matrix) and b (a column vector) are
observed and we wish to
# obtain a least squares estimate of x.

# To illustrate we generate a test case:
x= m.M([1, 2, 3], type= m._nt.Float64).T
A= m.random(shape= (10, 3))
error= (m.random(shape= (10, 1)) - 0.5)/10 # ie. -0.05 .. 0.05
b= A * x + error
# solution
xEst= (A.T * A).I * A.T * b
print 'xEstimate:', xEst
and all the
helpful linear algebra and summary functions it provides in
numarray.linear_algebra and numarray.linear_algebra.mlab. With the
exception of Hilbert, most of the code seems like a wrapper of
numarray functionality.
There are a few other functions and methods, but the documentation makes
it clear that PyMatrix is based on numarray, in fact M (the main matrix
class) is a a sub-class of NumArray.

Why based on numarray, rather than numeric? Because, at the time the
project was started, numarray seemed to be set to replace numeric.

For a full list, see:
http://www3.sympatico.ca/cjw/PyMatrix/Doc/PackageEntry.html
Is the main difference how you define the default behavior of
operators?
The main differences are:
- PyMatrix is focused on two dimensional numeric structures,
row and column vectors are handled as matrices.
- It permits matrix arithmetic, for compatible matrices A
and B and integer n, the matrix operations A+B, A-B, A*B,
A/B and A**n are recognized.
- It uses properties to provide basic matrix operations such as:
I Inverse
T Transpose
EValues Eigenvalues
SVD Singular Value Decomposition
Det Determinant
- It provides a method to build sub-matrices into a larger
matrix
- It provides methods to aggregate data by row or column
Or is this just a starting point for a much larger
package?
No, quantiles are likely to be added soon and possibly symmetric
matrices, although it is not clear that the potential storage saving
justifies the additional processing. I am certainly not seeking the
bulk of SciPy.

c.l.p has a number of postings similar to "MATLAB2Python" from Sarge,
see below. If something like Huaiyu Zhu's MatPy were available then, it
seems to me, it could be of help to the Python user who works with matrices.

Colin W.
Thanks,
JDH

Hi, everybody!
I'm a moderately experienced programmer in Matlab, and looking for a
free computational language (Matlab is *REALLY* expensive for an
individual, like me).
I came across Python (actually Scipy) and immediately felt
comfortable with the interface and syntax.
I'm still a newbe, so, before attempting a serious work on it, I'd
like to hear any opinion about migration from Matlab to Python, and
also a rough comparison between these two languages.

Thanx in advance, and excuse me for my very bad English (I'm not a
native English speaker),

Sarge
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top