       Re: Sorting eigenvectors by eigenvalue

• To: mathgroup at smc.vnet.net
• Subject: [mg2070] Re: [mg2054] Sorting eigenvectors by eigenvalue
• From: Peder Thusgaard Ruhoff <ptk at imada.ou.dk>
• Date: Sat, 23 Sep 1995 20:35:35 -0400

```On Mon, 18 Sep 1995, William F. Campbell <valentin at wam.umd.edu> wrote:

>   I'm trying to use Sort to sort eigenvectors by eigenvalue.  The eignevectors
> are in a list of lists called vecs, and the eigenvalues in vals after the
> following function call on matrix aa:
>
>   {vals,vecs}=Eigensystem[aa]
>
>   Then I want to define an ordering function p so I can use Sort[vecs,p].
> I tried the following:
>
>   p[vecs[[i_]],vecs[[j_]]]:=Greater[Abs[vals[[i]],Abs[vals[[j]]]]
>
>   Mathematica wont parse vecs[[i_]] saying that i is neither an integer nor
> a list of integers.  How can I accomplish what I want to do?
> ...
>

Dear Bill Campbell,

I don't know if this is what you need, but here is a function that
sorts the eigenvalues and eigenvectors using Sort.

SortEigensystem[eigsys_] := Transpose[Sort[Transpose[eigsys]]]

SortEigensystem[eigsys_, p_] := Transpose[Sort[Transpose[eigsys], p]]

Note that you can specify you own ordering function (the second argument)
which is passed directly to Sort.

EXAMPLE:
========

In:= SortEigensystem[eigsys_] := Transpose[Sort[Transpose[eigsys]]]

In:= SortEigensystem[eigsys_, p_] := Transpose[Sort[Transpose[eigsys], p]]

In:= mat = N[{{1,2,0},{2,3,4},{0,4,5}}]

Out= {{1., 2., 0}, {2., 3., 4.}, {0, 4., 5.}}

In:= MatrixForm[%]

Out//MatrixForm= 1.   2.   0

2.   3.   4.

0    4.   5.

In:= eigsys = Eigensystem[mat]

Out= {{8.33816, 1.95205, -1.29021},

>    {{0.172027, 0.631179, 0.75632}, {0.786436, 0.374362, -0.491296},

>     {0.593233, -0.679313, 0.431981}}}

In:= SortEigensystem[eigsys]

Out= {{-1.29021, 1.95205, 8.33816},

>    {{0.593233, -0.679313, 0.431981}, {0.786436, 0.374362, -0.491296},

>     {0.172027, 0.631179, 0.75632}}}

In:= SortEigensystem[eigsys, N[#1 > #2]&]

Out= {{8.33816, 1.95205, -1.29021},

>    {{0.172027, 0.631179, 0.75632}, {0.786436, 0.374362, -0.491296},

>     {0.593233, -0.679313, 0.431981}}}

===============================================================================

Best wishes,

Peder

------------------------------------------------------------------------------
Peder Thusgaard Ruhoff                     Phone: (+45) 66 15 86 96, ext. 2411
Dept. of Mathematics and Computer Science  Fax:   (+45) 65 93 26 91
Odense University, Campusvej 55            Email: ptk at imada.ou.dk
DK-5230 Odense M, DENMARK

"It is important for him who wants to discover not to confine himself
to one chapter of science, but to keep in touch with various others."