Eigen求特征值与特征向量
程序员文章站
2024-03-07 17:46:51
...
这里列举三种方式求矩阵的特征值与特征向量
#include <stdio.h>
#include <stdlib.h>
#include<Eigen/Eigen>
using namespace std;
using namespace Eigen;
void main()
{
MatrixXd m(3, 3);
m << 1,-2,2,
-2,-2,4,
2,4,-2;
cout << m << endl << endl;
EigenSolver<MatrixXd> es(m);
cout << "第一种:" << endl;
cout << "特征值为:" << endl;
cout << es.eigenvalues() << endl;
cout << "特征向量为:" <<endl;
cout << es.eigenvectors() << endl << endl;
cout << "第二种:" << endl;
Matrix3d D = es.pseudoEigenvalueMatrix();
Matrix3d V = es.pseudoEigenvectors();
cout << D << endl << endl;
cout << V << endl << endl;
cout << "第三种:" << endl;
MatrixXd value= es.eigenvalues().real();
MatrixXd vector= es.eigenvectors().real();
cout << value<< endl << endl;
cout << vector<< endl << endl;
}
运行结果