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.
In: 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 : m = mat(rand(1500,1500)) In : mi = linalg.inv(m) In : np.trace(m*mi) Out: 1500.0
And of course all this is nice and fast (on my linux laptop):
In : %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