[Date Index]
[Thread Index]
[Author Index]
Re: Re: question about sorting lists
*To*: mathgroup at smc.vnet.net
*Subject*: [mg90922] Re: [mg90871] Re: question about sorting lists
*From*: DrMajorBob <drmajorbob at att.net>
*Date*: Wed, 30 Jul 2008 03:50:41 -0400 (EDT)
*References*: <22839460.1217143622348.JavaMail.root@m08>
*Reply-to*: drmajorbob at longhorns.com
First of all, this is contradictory:
> If
> the list is {1, 2, -3} then it needs to be ordered as {2, 1, -3}. So
> that the middle element in the list has an intermediate absolute value,
> and the distance between the second and the third elements in the list
> is the largest.
1 does NOT have intermediate absolute value in the list {1,2,-3}. 2 is
intermediate, and I use that interpretation in the code below (as did
others, I believe):
Clear[eigenOrdering, eigenSort]
eigenOrdering[{a_?NumericQ, b_?NumericQ, c_?NumericQ}] :=
Module[{a1, b1, c1, a2, b2, c2},
{a1, b1, c1} = Ordering@Abs@{a, b, c};
{a2, b2, c2} = {a, b, c}[[{a1, b1, c1}]];
If[Abs[a2 - b2] > Abs[b2 - c2], {c1, b1, a1}, {a1, b1, c1}]]
eigenSort[{a_?NumericQ, b_?NumericQ, c_?NumericQ}] := {a, b,
c}[[eigenOrdering@{a, b, c}]]
eigenSort[{val_List, vec_List}] :=
Transpose[Transpose[{val, vec}][[eigenOrdering@val]]]
First your two examples:
evs = {-2, 1, -3}
eigenSort@evs
{-2, 1, -3}
{-3, -2, 1}
evs = {1, 2, -3}
eigenSort@evs
{1, 2, -3}
{1, 2, -3}
An arbitrarily chosen matrix:
eigenSort@Eigensystem[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]
{{0, 1/2 (15 - 3 Sqrt[33]),
1/2 (15 + 3 Sqrt[33])}, {{1, -2,
1}, {-(13/11) + 1/22 (15 - 3 Sqrt[33]), -(1/11) +
1/44 (15 - 3 Sqrt[33]),
1}, {-(13/11) + 1/22 (15 + 3 Sqrt[33]), -(1/11) +
1/44 (15 + 3 Sqrt[33]), 1}}}
Random examples:
evs = RandomInteger[100, 3]
eigenSort@evs
{43, 14, 60}
{60, 43, 14}
or
evs = RandomComplex[10 + 5 I, 3]
eigenSort@evs
{0.334128+ 4.85947 I, 8.38226+ 4.66865 I, 5.33541+ 4.67186 I}
{8.38226+ 4.66865 I, 5.33541+ 4.67186 I, 0.334128+ 4.85947 I}
The code is NOT a one liner; it compartmentalizes the logic, in case you
need to understand or modify it someday. The first definition of eigenSort
(for eigenvalues, not systems) is handy for testing... against your
examples, for instance.
Bobby
On Sun, 27 Jul 2008 01:31:29 -0500, Tatyana Polenova
<tpolenov at mail.chem.udel.edu> wrote:
> Jean-Marc,
> thank you very much for the tip.
>
> Indeed, I forgot to specify that the eigenvalues have to also be
> arranged in order of increasing or decreasing their values depending on
> what the distance is between the adjacent two. Say, the eigenvalues list
> is {-2, 1, -3}. The way this list needs to be ordered is {-3, -2, 1}. If
> the list is {1, 2, -3} then it needs to be ordered as {2, 1, -3}. So
> that the middle element in the list has an intermediate absolute value,
> and the distance between the second and the third elements in the list
> is the largest.
>
> The way I used to do the ordering before is in two steps:
> first, the regular Sort operation to arrange the eigenvalues in order.
> Then, reassign the order if necessary according to the distance
> criterion. Something like:
>
> CS={1,2,-3}
> Eigensort = Sort[Eigenvalues[CS]];
> If[Abs[Eigensort[[1]] - Eigensort[[2]]] > Abs[Eigensort[[3]] -
> Eigensort[[2]]], Vxx = Eigensort[[3]]; Vyy = Eigensort[[2]]; Vzz =
> Eigensort[[1]], Vxx = Eigensort[[1]]; Vyy = Eigensort[[2]]; Vzz =
> Eigensort[[3]]];
>
> But then the problem arises when I need to pull out the corresponding
> eigenvectors, in some instances the Vxx, Vyy, and Vzz are not recognized
> by Mathematica as identical numbers to the original values stored in the
> list.
>
> Is this a precision problem?
>
> But in any event, I thought it may be a more concise way to do the
> sorting using a one-line expression, like you suggested.
> What would you recommend? Thanks for your time!
>
> With best regards
> Tatyana
>
>
>
>
> ----- Original Message -----
> From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
> To: "Tatyana Polenova" <tpolenov at mail.chem.udel.edu>,
> mathgroup at smc.vnet.net
> Sent: Saturday, July 26, 2008 12:29:32 PM (GMT-0500) America/New_York
> Subject: [mg90871] Re: question about sorting lists
>
> Tatyana Polenova wrote:
>
>> I have the following numerical list:
>>
>> {{{x1, x2, x3}, {{x11, x12, x13}, {x21, x22, x23}, {x31, x32, x33}}}
>>
>> where x1, x2, and x3 are some eigenvalues, and {x11, x12, x13}, {x21,
>> x22, x23}, and {x31, x32, x33} are their corresponding eigenvectors.
>>
>> I need to sort the list so that the eigenvalues are arranged in the
>> order of decreasing the absolute value of the difference between any
>> of the two eigenvalues. (and that the eigenvectors still correspond to
>> the original eigenvalues).
>>
>> What would be the correct syntax for this operation in Mathematica 5.2?
>
> First the easy part of your question. Say we want to sort the
> eigenvalues in increasing order and keep their respective eigenvector in
> order. One possible way of doing that is as follows:
>
> eigsys = Eigensystem[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}] // N
> {eigsys[[1, #]], eigsys[[2, #]]} &[Ordering[eigsys[[1]]]]
>
>
> {{16.1168, -1.11684, 0.}, {{0.283349, 0.641675, 1.},
> {-1.28335, -0.141675, 1.},
> {1., -2., 1.}}}
>
> {{-1.11684, 0., 16.1168}, {{-1.28335, -0.141675, 1.},
> {1., -2., 1.},
> {0.283349, 0.641675, 1.}}}
>
>
> (Note that functions such as Sort, Ordering, etc., all have a second or
> third optional argument that specifies the sort function -- relation --
> to use.)
>
> Although I might have missed the obvious when reading your
> specifications, your criterion is not strong enough to define an order
> relation (among other things, it is not antisymmetric).
>
> For instance, say we have the eigenvalues {-1, 0, 1}. How would you
> ordered them? You might say (-1, 1, 0) for the distances among adjacent
> pairs are 2 (Abs[(-1) - 1]) and 1 (Abs[1 - 0]). Now the triple (1, -1,
> 0) also matches the same "order". So which one to choose?
>
> Regards,
> -- Jean-Marc
>
>
>
--
DrMajorBob at longhorns.com
Prev by Date:
**Re: Re: Method Option**
Next by Date:
**Re: How do I set PlotRegion value permantly?**
Previous by thread:
**Re: question about sorting lists**
Next by thread:
**Calculating RMSE from 2D Data**
| |