Re: Sorting Eigenvalues/EigenVectors
- To: mathgroup at smc.vnet.net
- Subject: [mg8173] Re: Sorting Eigenvalues/EigenVectors
- From: murray at math.umass.edu (Murray Eisenberg)
- Date: Mon, 18 Aug 1997 03:06:56 -0400
- Organization: University of Massachusetts, Amherst
- Sender: owner-wri-mathgroup at wolfram.com
Joseph C. Slater (jslater at cs.wright.edu) wrote: : Is there an easy way to sort the results of EigenSystem so that the : eigenvalues are in increasing order? I can pull them out of the results and : sort them, but the association of the eigenvalues with the eigenvectors is : lost. I've been RTFMing for quite a while, and I still can't figure this : out. Here's one way -- undoubtedly the most elegant. a = {{16,19,1},{18,10,20},{10,5,3}}; esys = N[Eigensystem[a]] evals = esys[[1]]; evect = esys[[2]] (* Sort[evals] gives the sorted list. That's not all you want. *) where = Flatten[Map[Position[Sort[evals], #]&, evals]] evals[[where]] evects[[where]] Now evals[[where]] and evects[[where]] will have corresponding eigenvalues and eigenspace bases in the same, sorted positions. Position tells you where a single item in a list is (among other uses), and then one Map's that to make it work on each element of the list evals. One of the design features of Mathematica I don't like is that Sort is the "basic" function. I prefer a language like APL, where the "position-of-in-sorted-order" is the more basic function; from that, an actual sort just takes idexing. This seems to be one of the design principles of APL that Wolfram ignored when he built upon his knowledge of APL in designing Mathematica. By the way, the eigenvalues in the example above consist of one real and two conjugate complex numbers. The sort order seems to be sort on real part first, then sort on imaginary part in case of ties. The Sort function allows a user-designated sorting function so that you could, e.g., sort on modulus (in the complex sense of the term), or even in the case of all real eigenvalues sort in order of decreasing absolute value -- the most common thing wanted, I suppose. -- Murray Eisenberg Internet: murray at math.umass.edu Mathematics & Statistics Dept. Voice: 413-545-2859 (W) University of Massachusetts 413-549-1020 (H) Amherst, MA 01003 Fax: 413-545-1801