Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting Eigenvalues/EigenVectors

828 views
Skip to first unread message

Joseph C. Slater

unread,
Aug 12, 1997, 3:00:00 AM8/12/97
to

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

__________________________________________________________________
Dr. Joseph C. Slater mailto:jsl...@cs.wright.edu
Mechanical and Materials Engineering office:+1 (937) 775-5085
Wright State University fax:+1 (937) 775-5009

Dayton, OH 45435 http://www.cs.wright.edu/people/faculty/jslater


Tom Burton

unread,
Aug 16, 1997, 3:00:00 AM8/16/97
to

On 12 Aug 1997 00:48:10 -0400, in comp.soft-sys.math.mathematica you =
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.
>

An eigensolution bundles the eigenvalues and vectors separately:
In[14]:=3D sol=3DEigensystem[Table[Random[],{3},{3}]]
Out[14]=3D {{1.78548,-0.331954, 0.175583},
{{0.399021, 0.721989, 0.565255},
{-0.78619, 0.386882, 0.4819},
{-0.196281,-0.325106, 0.925084}}}

One easy way is to rebundle each eigenvalue with its vector before =
sorting:

In[15]:=3D Sort[Transpose[sol]]
Out[15]=3D {{-0.331954,{-0.78619, 0.386882, 0.4819}},
{ 0.175583,{-0.196281,-0.325106, 0.925084}},
{ 1.78548, { 0.399021, 0.721989, 0.565255}}}
Tom Burton


Robert Knapp

unread,
Aug 16, 1997, 3:00:00 AM8/16/97
to

Joseph C. Slater 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.
>
> Thanks, Joe Slater
>

You could try

Transpose[Sort[Transpose[Eigensystem[matrix]]]]

For example:

First find the eigenvalues and eigenvectors (by default they are sorted
in order of complex absolute value largest to smallest)

In[1]:= es = Eigensystem[{{1.,2.,3.},{4.,5.,6.},{7.,8.,9.}}]
-17
Out[1]= {{16.1168, -1.11684, -4.0926 10 },
> {{0.231971, 0.525322, 0.818673}, {0.78583, 0.0867513, -0.612328},
> {0.408248, -0.816497, 0.408248}}}

This pairs each of the eigenvalues with the corresponding eigenvector.

In[2]:= tes = Transpose[es]
Out[2]= {{16.1168, {0.231971, 0.525322, 0.818673}},
> {-1.11684, {0.78583, 0.0867513, -0.612328}},
-17
> {-4.0926 10 , {0.408248, -0.816497, 0.408248}}}

This sorts based on the first element of each list (the eigenvalue)

In[3]:= stes = Sort[tes]

Out[3]= {{-1.11684, {0.78583, 0.0867513, -0.612328}},
-17
> {-4.0926 10 , {0.408248, -0.816497, 0.408248}},
> {16.1168, {0.231971, 0.525322, 0.818673}}}

This puts the sorted list back into a separate list of {eigenvalues,
eigenvectors}

In[4]:= Transpose[stes]
-17
Out[4]= {{-1.11684, -4.0926 10 , 16.1168},
> {{0.78583, 0.0867513, -0.612328}, {0.408248, -0.816497, 0.408248},
> {0.231971, 0.525322, 0.818673}}}


I'm not quite sure what you wnat to do if any of the values are complex,
but you could modify the Order function in Sort to get what you want.

Rob Knapp
Wolfram Research, Inc.


Murray Eisenberg

unread,
Aug 16, 1997, 3:00:00 AM8/16/97
to

Joseph C. Slater (jsl...@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: mur...@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


Paul Abbott

unread,
Aug 16, 1997, 3:00:00 AM8/16/97
to

Joseph C. Slater 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.

One solution was given in the Mathematica Journal 3(1): 19. I have
attached a 3.0 Notebook on this topic.

Cheers,
Paul

____________________________________________________________________
Paul Abbott Phone: +61-8-9380-2734
Department of Physics Fax: +61-8-9380-1014
The University of Western Australia
Nedlands WA 6907 mailto:pa...@physics.uwa.edu.au
AUSTRALIA http://www.pd.uwa.edu.au/Paul

God IS a weakly left-handed dice player
____________________________________________________________________

Content-Description: Mathematica 3.0 Document
Content-Disposition: inline; filename="EvalsEvecs.nb"

<encoded_portion_removed>
KioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiopDQ0=
--------------690418065DE--


Tom Chwastyk

unread,
Aug 16, 1997, 3:00:00 AM8/16/97
to

Joseph C. Slater <jsl...@cs.wright.edu> wrote in article
<5sopua$g...@smc.vnet.net>...

> 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.

One easy way is to transpose the eigensystem from

{{val1,val2,...},{vec1,vec2...}}

to

{{val1,vec1},{val2,vec2},...}

, sort the transpose, then transpose back. The sort goes by first
differences - it doesn't even look at the vectors unless the eigenvalues
are exactly equal, and then it goes by first differences in the vectors.

Remember, if your eigenvalues are complex, the default sort is not by
complex magnitude but by first differences on the real and then complex
parts. You can add an order function to Sort to change that if you like.

This same problem bothered me when I first came to MMa from APL; I wanted
to get a permutation vector like that resulting from the APL primitives
grade up or grade down, then subscript the parts of the eigensystem result
by that permutation vector to get a sorted eigensystem result. It took a
while to just relax and do it the way it's more natural in MMa.

The example below generates a random positive semidefinite matrix, then
changes two of the eigenvalues to be equal, just to demonstrate that the
sorting works as described for the real case...

x=Table[Random[Real,{-1,1}],{10},{6}];
xx=Transpose[x].x;
es=Eigensystem[xx];es[[1,{1,2}]]=1.5;
Table[es[[1,i]],es[[2,i,{1,2}]],{i,6}]
ses=Transpose[Sort[Transpose[es]]];
Table[ses[[1,i]],ses[[2,i,{1,2}]],{i,6}]

--
Tom Chwastyk ["Fosdick". Polish: CH='H, W=F (here),
A=AH, Y=I. Soften strict 'HFAH-stik.... :-) ]
Naval Research Laboratory Code 6383
Wash. DC 20375-5343 (202) 767 2567

0 new messages