1

So I'm using NumPy's linear algebra routines to do some basic computational quantum mechanics. Say I have a matrix, hamiltonian, and I want its eigenvalues and eigenvectors

import numpy as np
from numpy import linalg as la

hamiltonian = np.zeros((N, N)) # N is some constant I have defined
# fill up hamiltonian here
energies, states = la.eig(hamiltonian)

Now, I want to sort the energies in increasing order, and I want to sort the states along with them. For example, if I do:

groundStateEnergy = min(energies)
groundStateIndex = np.where(energies == groundStateEnergy)
groundState = states[groundStateIndex, :]

I correctly plot the ground state (eigenvector with the lowest eigenvalue). However, if I try something like this:

energies, states = zip(*sorted(zip(energies, states)))

or even

energies, states = zip(*sorted(zip(energies, states), key = lambda pair:pair[0])))

plotting in the same way no longer plots the correct state.So how can I sort states alongside energies, but only by row? (i.e, I want to associate each row of states with a value in energies, and I want to rearrange the rows so that the ordering of the rows corresponds to the sorted ordering of the values in energies)

3
  • you should probably check numpy.argsort Commented Sep 17, 2014 at 12:02
  • Use the indicies from numpy.argsort(energies) to sort the states array. Commented Sep 17, 2014 at 12:02
  • So once I get the indices from numpy.argsort(energies), how do I rearrange only the rows of states using them? Commented Sep 17, 2014 at 12:20

1 Answer 1

2

You can use argsort as follows:

>>> x = np.random.random((1,10))

>>> x
array([ 0.69719108,  0.75828237,  0.79944838,  0.68245968,  0.36232211,
        0.46565445,  0.76552493,  0.94967472,  0.43531813,  0.22913607])
>>> y = np.random.random((10))
>>> y
array([ 0.64332275,  0.34984653,  0.55240204,  0.31019789,  0.96354724,
    0.76723872,  0.25721343,  0.51629662,  0.13096252,  0.86220311])
>>> idx = np.argsort(x)
>>> idx
array([9, 4, 8, 5, 3, 0, 1, 6, 2, 7])
>>> xsorted= x[idx]
>>> xsorted
array([ 0.22913607,  0.36232211,  0.43531813,  0.46565445,  0.68245968,
        0.69719108,  0.75828237,  0.76552493,  0.79944838,  0.94967472])
>>> ysordedbyx = y[idx]
>>> ysordedbyx
array([ 0.86220311,  0.96354724,  0.13096252,  0.76723872,  0.31019789,
        0.64332275,  0.34984653,  0.25721343,  0.55240204,  0.51629662])

and as suggested by the comments an example where we sort a 2d array by it's first collumn

>>> x=np.random.random((10,2))
>>> x
array([[ 0.72789275,  0.29404982],
       [ 0.05149693,  0.24411234],
       [ 0.34863983,  0.58950756],
       [ 0.81916424,  0.32032827],
       [ 0.52958012,  0.00417253],
       [ 0.41587698,  0.32733306],
       [ 0.79918377,  0.18465189],
       [ 0.678948  ,  0.55039723],
       [ 0.8287709 ,  0.54735691],
       [ 0.74044999,  0.70688683]])
>>> idx = np.argsort(x[:,0])
>>> idx
array([1, 2, 5, 4, 7, 0, 9, 6, 3, 8])
>>> xsorted = x[idx,:]
>>> xsorted
array([[ 0.05149693,  0.24411234],
       [ 0.34863983,  0.58950756],
       [ 0.41587698,  0.32733306],
       [ 0.52958012,  0.00417253],
       [ 0.678948  ,  0.55039723],
       [ 0.72789275,  0.29404982],
       [ 0.74044999,  0.70688683],
       [ 0.79918377,  0.18465189],
       [ 0.81916424,  0.32032827],
       [ 0.8287709 ,  0.54735691]])
Sign up to request clarification or add additional context in comments.

4 Comments

once you have the indices which sort the array use np.take() which showed to be faster than fancy indexing...
The answer is correct but in OP's example, y is 2-d. It might be better to show an example of using idx on a 2-d array.
@SaulloCastro It may still be faster yo use take, but in numpy 1.9 the performance of fancy indexing has been much improved, so that little trick may not be worth it anymore. With any older version, yes, absolutely, anywhere from 2x to 10x faster, well worth it.
@Jaime thank you for the update, its amazing the improvement that has been done in NumPy!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.