1
$\begingroup$

When using NDEigensystem, the first eigenvalue corresponds to the first eigenvector, the second eigenvalue to the second eigenvectors, and so on. I would like to sort the eigenvalues from min to max, also so that the eigenvectors are also sorted accordingly. How can this be implemented?

in the code, I do not provide eigenvectors, because they are displayed as pictures

ClearAll["Global`*"]


{vals, funs} = 
 NDEigensystem[-Laplacian[u[x], {x}] + (-(1/x) + x^2)*u[x], 
  u[x], {x, 0, \[Pi]}, 10]

(*{{2.89816, 6.93897, -10.8147, 10.9162, 17.2964, 26.0667, 
  36.977, 49.9732, 65.0544, 
  82.2449}, {InterpolatingFunction[{{0., 3.141592653589793}}, {
    5, 4225, 0, {41}, {3}, 0, 0, 0, 0, Indeterminate& , {}, {}, 
     False},...*)

You can see that Mathematica outputs the eigenvalues in an arbitrary order (the eigenvectors are arranged in the same order). Now I would like to sort the eigenvalues from minimum to maximum, but in such a way that their corresponding eigenvectors are sorted in the appropriate order.

If it's simply applied Sort, then the eigenvalues are sorted from minimum to maximum, but the eigenvectors are not sorted accordingly.

Sort /@ 
 NDEigensystem[-Laplacian[u[x], {x}] + (-1/x + x^2)*u[x], 
  u[x], {x, 0, \[Pi]}, 10]

(*{{-10.8147, 2.89816, 6.93897, 10.9162, 17.2964, 26.0667, 
  36.977, 49.9732, 65.0544, 
  82.2449}, {InterpolatingFunction[{{0., 3.141592653589793}}, {
    5, 4225, 0, {41}, {3}, 0, 0, 0, 0, Indeterminate& , {}, {}, 
     False},...*)
$\endgroup$
5
  • $\begingroup$ Reverse /@ NDEigensystem[-Laplacian[u[x], {x}] + (1/x + x^2)*u[x], u[x], {x, 0, \[Pi]}, 10]? $\endgroup$ Commented Jun 5, 2023 at 5:32
  • $\begingroup$ @Henrik Schumacher, thanks! I edited the question, please take a look $\endgroup$
    – Mam Mam
    Commented Jun 5, 2023 at 7:16
  • 4
    $\begingroup$ Try: ord = Ordering[vals]; {vals,funs}={vals[[ord]], funs[[ord]]} $\endgroup$ Commented Jun 5, 2023 at 7:22
  • $\begingroup$ @Daniel Huber, thanks! $\endgroup$
    – Mam Mam
    Commented Jun 5, 2023 at 10:25
  • $\begingroup$ You could also look at SortBy $\endgroup$
    – MarcoB
    Commented Jun 5, 2023 at 13:11

1 Answer 1

1
$\begingroup$
{vals, funs} // Transpose // Sort // Transpose
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.