对从 Eigen 获得的特征值和特征向量进行排序的有效方法

Efficient way of sorting eigenvalues and eigenvectors obtained from Eigen

我正在使用 Eigen 来求解对称矩阵的特征系统 m, 给出的示例如下:

#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
using namespace std;
using namespace Eigen;
int main()
{
  Matrix3f m(3,3);
  EigenSolver<Matrix3f> 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 << "matrix is: " << m << endl;
  cout << "The eigenvalues of A are: " << es.eigenvalues() << endl;
  cout << "The eigenvalues of A are: " << es.eigenvectors() << endl;
}

输出为:

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)

问题:

  1. 知道我的矩阵是对称的,这是对 EigenSolver 的有效使用吗?

  2. 如何对特征值和相应的特征向量进行排序? (最终提取最大特征值和相应的 vec)可以做一个类似于 Python 中常见的构造吗?

namely:

idx = eigenValues.argsort()[::-1]   
eigenValues = eigenValues[idx]
eigenVectors = eigenVectors[:,idx]
  1. Is this an efficient use of EigenSolver knowing that my matrix is symmetric?

不,在这种情况下您应该使用 SelfAdjointEigenSolverhttp://eigen.tuxfamily.org/dox/classEigen_1_1SelfAdjointEigenSolver.html

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

SelfAdjointEigenSolver已经对特征值进行了排序(从最低到最高),即要获得最高的Eigenvalue/vector,您需要取最后一个。

这里可以对特征值进行排序,因为所有特征值都保证为 real-valued(不对称 EigenSolver 不保证)。 另一个优点是特征向量保证形成Ortho-Normal基(即对应的矩阵是unitary/orthogonal)。