[SciPy-user] matrix inversion time (Python vs MATLAB)

Sturla Molden sturla@molden...
Fri Jan 9 15:20:06 CST 2009

> Despite the comparing of th t wo software is possible, but the fact is
> Python is designed for Array but Matlab is oruginally for matrix.

It doesn't matter. The internal representation is the same. The external
C/Fortran code in LAPACK can't tell the difference. That is, LAPACK is
based on Fortran and probably a bit more efficient when working on Fortran
ordered arrays (Matlab disallows C order, but NumPy allows both with 'C'
being default). That aside, NumPy has a Matrix class and Matlab har
element-wise (array) operators.

The difference that matters most performance wise is the BLAS version.
LAPACK makes calls int BLAS. Matlab ships with the best BLAS library for
Intel laptop and desktop computers by default (that is, Intel MKL). NumPy
does not.

1. Buy an MKL license from Intel
2. Compile LAPACK with MKL as BLAS
3. Build NumPy against LAPACK and MKL

If you do this, Matlab and NumPy should invert matrices equally fast. If
you cannot use MKL, build a version of ATLAS customized to your hardware.

There is also another difference: Matlab is 'smart'. Matlab's \ operator
and inv function call a LAPACK wrapper of ~80,000 lines of code that tries
to solve the linalg problem in the best possible way. With NumPy you must
know your linear algebra better, and select between Gauss-Newton, LU, QR,
SVD, Cholesky etc. manually. Just asking NumPy to invert a matrix
(numpy.linalg.inv) will work, but it will use a safe but not necessarily
efficient method (I think it defaults to backsubstitution). When it comes
to solving linear algebra on a personal computer, it is nearly impossible
to beat the performance of Matlab. It uses the best available libraries by
default and selects the methods intelligently. If that is what you want,
buy a Matlab license.

More information about the SciPy-user mailing list