#include <iostream>
using namespace std;
#include <ctime>
#include <Eigen/Core>
// 稠密矩阵的代数运算(逆,特征值)
#include <Eigen/Dense>
using namespace Eigen;
#define MATRIX_SIZE 50
int main(int argc,char **argv){
// Eigen中所有的向量和矩阵都是Eigen::Matrix
// 声明一个2*3的float矩阵
Matrix<float,2,3> matrix_23;
Vector3d v_3d;
Matrix<float,3,1> vd_3d;//与Vector3d相同
Matrix3d matrix_33=Matrix3d::Zero();
// 如果不确定矩阵的大小,可以用动态大小的矩阵
Matrix<double,Dynamic,Dynamic> matrix_dynamic;
//对于Eigen库的操作
matrix_23<<1,2,3,4,5,6;
cout<<"matrix 2*3 form 1 to 6:\n"<<matrix_23<<endl;
//用()访问矩阵中的元素
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
cout<<matrix_23(i,j)<<"\t";
}
cout<<endl;
}
//矩阵和向量相乘
v_3d<<3,2,1;
vd_3d<<4,5,6;
Matrix<double,2,1> result=matrix_23.cast<double>()*v_3d;
cout<<"[1,2,3;4,5,6]*[3,2,1]:"<<result.transpose()<<endl;
Matrix<float,2,1> result2=matrix_23*vd_3d;
cout<<"[1,2,3;4,5,6]*[4,5,6]:"<<result2.transpose()<<endl;
// 随机数矩阵
matrix_33=Matrix3d::Random();
cout<<"random matrixL \n"<<matrix_33<<endl;
cout<<"transpose: \n"<<matrix_33.transpose()<<endl;
cout<<"sum:\n"<<matrix_33.sum()<<endl;
cout<<"trace:"<<matrix_33.trace()<<endl;//迹
cout<<"times 10:\n"<<10*matrix_33<<endl;//数乘
cout<<"inverse:\n"<<matrix_33.inverse()<<endl;//逆
cout<<"det:"<<matrix_33.determinant()<<endl;//行列式
//特征值
//实对称矩阵可以保证对角化成功
SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose()*matrix_33);
cout<<"Eigen values=\n"<<eigen_solver.eigenvalues()<<endl;
cout<<"Eigen vactors=\n"<<eigen_solver.eigenvectors()<<endl;
//解方程
}
03-04
595
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-05
1174
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-01