About alternatives to Matlab

sturlamolden sturlamolden at yahoo.no
Thu Nov 16 16:09:03 EST 2006


Boris wrote:
> Hi, is there any alternative software for Matlab? Although Matlab is
> powerful & popular among mathematical & engineering guys, it still
> costs too much & not publicly open. So I wonder if there's similar
> software/lang that is open & with comparable functionality, at least
> for numerical aspect. Thanks!

I have used Matlab for years, and has recently changed to Python. In
addition one needs NumPy and Matplotlib, and perhaps SciPy. Other
useful packages are PyGTK for GUI, PyGame for multimedia, PyOpenGL for
3D graphics, Mpi4Py for parallel computation, etc. You will find python
packages for nearly any conceivable task. Unlike Matlab, Python is a
general purpose programming language, and a distant cousin of Lisp. The
Python language is fare more expressive and productive than Matlab, yet
even more easy to use.

The NumPy package is the core requirement for numerical work in Python.
It is quite different form Matlab, but I think it is more powerful.
Particularly, arrays are passed by reference (not by value), and
indexing creates view matrices.

To compare Matlab with NumPy we can e.g. use the D4 discrete wavelet
transform. I have here coded it in Matlab and Python/NumPy using Tim
Swelden's lifting scheme.

First the Matlab version (D4_Transform.m):

function x = D4_Transform(x)
   % D4 Wavelet transform in Matlab
   % (C) Sturla Molden
   C1 =  1.7320508075688772;
   C2 =  0.4330127018922193;
   C3 = -0.066987298107780702;
   C4 =  0.51763809020504137;
   C5 =  1.9318516525781364;
   s1 = zeros(ceil(size(x)/2));
   d1 = zeros(ceil(size(x)/2));
   d2 = zeros(ceil(size(x)/2));
   odd = x(2:2:end);
   even = x(1:2:end-1);
   d1(:) = odd - C2*even;
   s1(1) = even(1) + C2*d1(1) + C3*d1(end);
   s1(2:end) = even(2:end) + C2*d1(2:end) + C3*d1(1:end-1);
   d2(1) = d1(1) + s1(end);
   d2(2:end) = d1(2:end) + s1(1:end-1);
   x(1:2:end-1) = C4*s1;
   x(2:2:end) = C5*d2;
   if (length(x) >2)
      x(1:2:end-1) = D4_Transform(x(1:2:end-1));
   end


Then the Python version (D4.py):

import numpy
import time

def D4_Transform(x, s1=None, d1=None, d2=None):
   """
   D4 Wavelet transform in NumPy
   (C) Sturla Molden
   """
   C1 = 1.7320508075688772
   C2 = 0.4330127018922193
   C3 = -0.066987298107780702
   C4 = 0.51763809020504137
   C5 = 1.9318516525781364
   if d1 == None:
      d1 = numpy.zeros(x.size/2)
      s1 = numpy.zeros(x.size/2)
      d2 = numpy.zeros(x.size/2)
   odd = x[1::2]
   even = x[:-1:2]
   d1[:] = odd[:] - C1*even[:]
   s1[0] = even[0] + C2*d1[0] + C3*d1[-1]
   s1[1:] = even[1:] + C2*d1[1:] + C3*d1[:-1]
   d2[0] = d1[0] + s1[-1]
   d2[1:] = d1[1:] + s1[:-1]
   even[:] = C4 * s1[:]
   odd[:] = C5 * d2[:]
   if x.size > 2:

D4_Transform(even,s1[0:even.size/2],d1[0:even.size/2],d2[0:even.size/2])

if __name__ == "__main__":
   x = numpy.random.rand(2**23)
   t0 = time.clock()
   D4_Transform(x)
   t = time.clock()
   print "Elapsed time is %.6f seconds" % (t-t0)


Now let's do benchmark on my laptop (1.73 GHz Pentium M, 0.99 GB RAM).
I have stopped paying for Matlab maintenance (for reasons that will be
obvious), so I only have R14 Service Pack 2 for comparison.

First we try Matlab (R14 Service Pack 2):

>> x = rand(2^23,1);
>> tic; D4_Transform(x); toc
Elapsed time is 27.145438 seconds.


Then we Python 2.4.4 with NumPy 1.0:

C:\develop\python\D4>python D4.py
Elapsed time is 3.364887 seconds

That is quite astonishing.

If anyone wonders why I think Travis Oliphant and the NumPy team should
be knighted, then this is the answer. The Mathworks' product only
achieved 100% * 3/27 = 11% the speed of Python/NumPy, and is infinitely
more expensive.

Does anyone wonder why I am not paying for Matlab maintenance anymore?

Sorry Mathworks, I have used your product for years, but you cannot
compete with NumPy.


Cheers,
Sturla Molden




More information about the Python-list mailing list