Re: Sorting Eigenvalues/EigenVectors
- To: mathgroup at smc.vnet.net
- Subject: [mg8170] Re: [mg8099] Sorting Eigenvalues/EigenVectors
- From: Olivier Gerard <jacquesg at pratique.fr>
- Date: Mon, 18 Aug 1997 03:06:54 -0400
- Sender: owner-wri-mathgroup at wolfram.com
> 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. > > Thanks, Joe Slater > Joe, indeed this is quite easy. I will show you on an example: In[1]:= Eigensystem[{{1,-1,2,3},{-1,3,-2,1},{1,3,2,-1},{2,1,-1,3}}]//N Out[1]= {{3.04572 - 2.29366 I, 3.04572 + 2.29366 I, -1.7659, 4.67445}, {{-4.09241 + 0.641624 I, 6.56081 + 3.54537 I, -1.66973 + 7.12228 I, 1.}, {-4.09241 - 0.641624 I, 6.56081 - 3.54537 I, -1.66973 - 7.12228 I, 1.}, {-1.82145, -0.208052, 0.914949, 1.},{0.806267, 0.0864262, 0.0245081, 1.}}} In[2]:= Transpose at Sort@Transpose at % Out[2]= {{-1.7659, 3.04572 - 2.29366 I, 3.04572 + 2.29366 I, 4.67445}, {{-1.82145, -0.208052, 0.914949, 1.}, {-4.09241 + 0.641624 I, 6.56081 + 3.54537 I, -1.66973 + 7.12228 I, 1.}, {-4.09241 - 0.641624 I, 6.56081 - 3.54537 I, -1.66973 - 7.12228 I, 1.}, {0.806267, 0.0864262, 0.0245081, 1.}}} You can observe that when there are both complex and real eigenvalues, Sort use the Real part then the Imaginary part to sort them. To have a custom sorting, for instance on module, you can do something like that: In[3]:= Transpose[Sort[Transpose[mysys], (Abs[#1[[1]]]>Abs[#2[[1]]])&]] Out[3]= {{4.67445, 3.04572 + 2.29366 I, 3.04572 - 2.29366 I, -1.7659}, {{0.806267, 0.0864262, 0.0245081, 1.}, {-4.09241 - 0.641624 I, 6.56081 - 3.54537 I, -1.66973 - 7.12228 I, 1.}, {-4.09241 + 0.641624 I, 6.56081 + 3.54537 I, -1.66973 + 7.12228 I, 1.}, {-1.82145, -0.208052, 0.914949, 1.}}} which gives a sorting by decreasing absolute value. Note the use of the sorting function in Sort. The [[1]] part taking in this function is necessary because at the stage where it operates, the system is a list of list whose first element (the eigen value) is the criteria for sorting and the other is the eigenvector. You can dispose of the final Transpose if you prefer this layout. Just reverse the inequality to have increasing order. Olivier Gerard Mathematica developer