So the SciPy package for Python is full of lots of goodness. Be very careful using the eigh() routine to get eigenvalues, though. There's a "gotcha" buried in it.

The syntax is:

(eigenvalues, eigenvectors) = eigh(matrix)

This returns an array of eigenvalues and a 2D array of eigenvectors (each eigenvector consists of many components).

And there's the gotcha. Let's say you want the nth eigenvalue and eigenvector. I would write:

eigenvalues[n]

eigenvectors[n]

And I would be horribly wrong. The eigenvectors and eigenvalues do share an index, but the index on the eigenvector is SECOND column:

eigenvectors[:,n]

Seriously WTF.

**Update:** It turns out that this is due to eigh() being part of LAPACK, which is a non-Python library (Fortran?) that has a different internal storage model for 2D arrays. So possibly less WTF, but still dangerous as all hell.

In my hands that's not how it behaves, but maybe it was fixed since you tried it?

Posted by: WeBru | January 03, 2010 at 11:09 PM

Correction: I was using the numpy.linalg.eigh. It works like you expect. I see in the scipy.linalg.eigh that it works the WTF way... but if you are used to SVD this way also makes sense.

Posted by: WeBru | January 03, 2010 at 11:23 PM

Actually, in my hands, the scipy.linalg.eigh documentation is wrong and the behavior is like you expected. But numpy.linalg.svd is the WTF way, but I think it's traditional for SVD.

Posted by: WeBru | January 03, 2010 at 11:50 PM