Sorting eigenvalues and eigenvectors

The numpy linalg package does not sort eigenvalues and eigenvectors. Sometimes it is useful to put the eigenvalues in ascending order. But when we do, we might also want to rearrange the eigenvectors so they still go with the eigenvalues. We do this using an indirect sort, provided by the numpy argsort() function. An indirect sort generates a list of indices that wouuld put the values in order, but it doesn't actually do the sort. So, for example if we want to sort the numbers w = [4.2, 2.1, 5.7] in ascending order, taking terms with indices [1, 0, 2], that is [w[1], w[0], w[2]], puts them in order. The indirect sort gives us just the list of indices. Here is how we can use it:

>>> w, v = LA.eig(A)
>>> idx = np.argsort(w)
>>> w = w[idx]
>>> v = v[:,idx]

The indices go into the numpy array idx, and we use them to rearrange the eigenvalues and eigenvectors at the same time so we keep the correspondence between eigenvalues and their eigenvectors.