title: Efficient way of sorting eigenvalues and eigenvectors obtained from Eigen
body: <p>I am using Eigen in order to solve the eigensystem for a symmetric matrix <code>m,</code> an example given as follows:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;Eigen/Dense&gt;
  #include &lt;Eigen/Eigenvalues&gt;
  using namespace std;
  using namespace Eigen;
  int main()
  {
    Matrix3f m(3,3);
    EigenSolver&lt;Matrix3f&gt; es;
    m(0,0) = -0.386087;
    m(1,1) = -0.390147;
    m(2,2) = 0.776234;
    m(0,1) = 0.00813956;
    m(0,2) = 0.0781361;
    m(1,0) = 0.0781361;
    m(1,2) = 0.0986476;
    m(2,0) = 0.0781361;
    m(2,1) = 0.0986476;
    es.compute(m,true);
    cout &lt;&lt; "matrix is: " &lt;&lt; m &lt;&lt; endl;
    cout &lt;&lt; "The eigenvalues of A are: " &lt;&lt; es.eigenvalues() &lt;&lt; endl;
    cout &lt;&lt; "The eigenvalues of A are: " &lt;&lt; es.eigenvectors() &lt;&lt; endl;
  }
  </code></pre>
  
  <p>and the output is:</p>
  
  <pre><code>matrix is:  -0.386087 0.00813956  0.0781361
  0.00813956  -0.390147  0.0986476
   0.0781361  0.0986476   0.776234
  The eigenvalues of A are: (-0.391002,0)
   (0.789765,0)
  (-0.398762,0)
  The eigenvalues of A are:   (0.976246,0) (-0.0666485,0)   (0.206158,0)
    (0.200429,0) (-0.0835865,0)  (-0.976136,0)
    (-0.08229,0)  (-0.994269,0)  (0.0682429,0)
  </code></pre>
  
  <hr>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li><p>Is this an efficient use of EigenSolver knowing that my matrix is symmetric?</p></li>
  <li><p>How could I sort the eigenvalues and accordingly the eigenvectors? (to eventually extract the max eigenval and corresponding vec) Could one do a similar construct as is common in Python?</p></li>
  </ol>
  
  <p><a href="https://stackoverflow.com/a/8093043/7685268">namely:</a></p>
  
  <pre><code>idx = eigenValues.argsort()[::-1]   
  eigenValues = eigenValues[idx]
  eigenVectors = eigenVectors[:,idx]
  </code></pre>