A year or so ago, I published a blog post called The best Matlab Alternative. In that post, I discussed the merits of Scilab, Octave and Freemat, and decided that for me, Octave suited me best. However, Scilab is probably the more full featured and powerful.
I’ve since discovered that there’s another contender, in some ways better than all of the above: Python. My requirement for a “Matlab alternative” was that:
- It must install under Windows. This is absolutely necessary for me, at least, since most of my students use Windows. A very few use OSX, and hardly any use Linux.
- It must, as far as possible, be identical in use with Matlab. The commands should be the same, and simple scripts and functions should transfer between the two with no effort.
- It should have a reasonably nice interface, as similar as possible to Matlab’s own. A basic command line only interface running in a terminal won’t cut it with my students.
Suppose we drop condition 2, and require software which can do pretty much most of what Matlab can do, in a nice visual environment, we find Python coming out looking very good indeed. Now, I have been a lover of Matlab, and its open source alternatives, for many years, and I do like the rapid prototyping possible with Matlab, as well as its ease for dealing with large data types. And vectorization (where a single command is automatically applied to every element of a multi-dimensional array) provides some wonderfully elegant and simple code. (It can also of course provide for dense obtuseness.)
However, I have also become aware of some criticisms of Matlab. A major one is vendor lock-in: the code, the language, the IDE, is all owned by The Mathworks. If they decide to radically change something, you have no alternative but keep paying the license fees to keep up. Second is that the language has no object-oriented capabilities, at least in a natural way;(see comment below) that the language has no standard as such – it’s just whatever The Mathworks decide to provide. Thirdly is the namespace: all functions exist as top-level objects, and all functions (“m-files”) in the Matlab path (which usually will include multiple directories) are available when you start up. Fourth is that an m-file can only contain one function (and maybe some sub-functions), whose name must be the same name as the file. This means that if you create some new functions – and most users do – you need to be sure that it doesn’t conflict with any other functions. This problem is exacerbated if you include lots of toolboxes. The Image Processing Toolbox alone adds 270 new functions to the Matlab namespace and there are 38 other toolboxes available. That’s a lot of functions.
One list of criticisms is available here; another (called “Abandon Matlab”) is here. Note that the second site recommends (as well as Python), R, which the first site also pans. There’s no accounting for tastes.
So, back to Python.
Python is a deservedly popular language, supported by a huge user base, tons of books, articles, websites, conferences, seminars, and so on. It is fully open source, and it is managed by a non-profit organization called the Python Software Foundation.
Python has lots of libraries for scientific computing, starting with numpy (“numeric python”) and scipy (“scientific python”). And there are others – such as scikits-image which extends scipy’s image processing capabilities (the Python Imaging Library does not seem to be currently in active development). Many more are listed here.
What’s more, Python has lots of IDEs which mean that you can use it pretty much as you would use Matlab, Octave or Scilab. For a nice visual IDE with all the trimmings, there are Eric IDE, Spyder, or PyDev, which is an Eclipse plugin, plus lots more, both open source and commercial.
For those of us who are satisfied with less, there’s iPython, an enhanced shell (which is what I like to use). And there are again lots more, including editors and all sorts of other goodies for development, here.
Let’s finish with a tiny example: to create a 1500 x 1500 random matrix, invert it, and check the trace of the product of the matrix and its inverse. I’ll do all this in the iPython shell, which I invoke from my linux command line with ipython.
Once in the iPython shell, we import the pylab modules; this includes numpy, scipy, and the plotting library matplotlib:
In[1]: from pylab import *
(In fact this can be done from the command line with iPython, but this command should work in any Python environment.)
Now to define the matrix, compute its inverse, and check the trace of their product:
In [2]: m = mat(rand(1500,1500)) In [3]: mi = linalg.inv(m) In [4]: np.trace(m*mi) Out[4]: 1500.0
And of course all this is nice and fast (on my linux laptop):
In [5]: %time m = mat(rand(1500,1500)); mi = linalg.inv(m); np.trace(m*mi) CPU times: user 6.52 s, sys: 0.21 s, total: 6.73 s Wall time: 3.78 s
I’ve read and appreciated your blog for quite some time now. In particular, I enjoy your take on the art of teaching math. In full disclosure, I’m an engineer at MathWorks responsible for supporting users in academia.
I’m prompted to write to both point out that I appreciate your posts and that MATLAB has had object-oriented programming since 2008 (and before that but I’m not as happy with the earlier incarnation).
http://www.mathworks.com/company/newsletters/articles/introduction-to-object-oriented-programming-in-matlab.html
Thank you for your comment, and for the correction – I have updated my blog post accordingly. I appreciate also your willingness to enter into a discussion, given your commercial position!
Why not try Sage? Julia will probably also be very nice once it matures
I use Sage a lot (see previous blog posts). However, Sage includes a lot of linux-only packages, and running it natively under Windows is still non-trivial. Python, however, runs under Windows with no trouble. Sage is also huge! I do know about Julia, and have looked at it, and I like what I’ve seen, but I also believe it needs more maturity before it can be used extensively – especially in education.
Aw, this was a really nice post. Taking the time and actual effort to make a top notch article… but what can I say… I procrastinate a lot and don’t manage to get anything done.
You’re so awesome! I don’t suppose I have read anything like that before.
So good to discover someone with a few original
thoughts on this subject. Seriously.. thanks for starting this up.
This site is one thing that is needed on the web, someone with a little originality!
Hmm is anyone else having problems with the pictures on this blog
loading? I’m trying to determine if its a problem on my end or if it’s the blog.
Any feedback would be greatly appreciated.
I all the time used to study piece of writing in news papers but now as
I am a user of net therefore from now I am using
net for content, thanks to web.
What’s up, everything is going fine here and ofcourse every one is sharing data, that’s really fine,
keep up writing.
What’s up, after reading this amazing paragraph i am too glad to share my familiarity here with colleagues.