empty
前提提示:该代码涵盖了Eigen3的部分基本操作,包括矩阵常用操作,线性矩阵的分解,稀疏矩阵的基本操作及分解和空间
转换的一些操作(如:旋转向量,欧式转换矩阵,四元数,旋转矩阵)。贴出的代码并没有涵盖Eigen3的所有操作,
更加深入的学习请参考Eigen官网。
代码亲测可用。
该页面分为四个板块:矩阵的基本操作,
矩阵的分解方法实例
稀疏矩阵的操作
稀疏矩阵的求解
几何模块(三维空间旋转)
各位可以有针对性的查看
1.矩阵的基本操作
#include<iostream>
#include <Eigen/Dense>
#include <Eigen/Core>
#include <Eigen/LU>
using namespace std;
void
base()
{
Eigen::Matrix3d m=Eigen::Matrix3d::Random(3,3);
cout<<"the matrixXd is"<<m<<endl;
Eigen::Matrix3d n=(m+Eigen::Matrix3d::Constant(3,3,1.2))*50;
cout<<"the matrix n is "<<n<<endl;
Eigen::Vector3d v(1,1,3);
v.asDiagonal();
//v<<1,2,3;
Eigen::RowVector2d vv(2,4);
cout<<" the vector is "<<v<<endl;
cout<<" the vector vv is "<<vv<<endl;
cerr<<"Matrix cols and row are "<<m.cols()<<" "<<m.rows()<<"\nthe size is "<<m.SizeAtCompileTime<<endl;
// float data[] = {1,2,3,4};
// Map<Vector3f> v1(data); // uses v1 as a Vector3f object
// Map<ArrayXf> v2(data,3); // uses v2 as a ArrayXf object
// Map<Array22f> m1(data); // uses m1 as a Array22f object
// Map<MatrixXf> m2(data,2,2); // uses m2 as a MatrixXf object
//cout<<v1<<endl;
//cout<<v2<<endl;cout<<m1<<endl;cout<<m2<<endl;
// Eigen::Matrix2cf mat;
// mat<<1.00256-3.256,2+0,
// 3.015-0.12,5.178-9541i;
// cerr<<"mat is" <<mat<<endl;
Eigen::Matrix3d m1;
m1<<1,2,1,8,0,9,7,33,47;
cout<<"m1 "<<m1<<endl;
cout<<"m1 transpose "<<m1.transpose()<<endl;
cout<<"m1 conjugate "<<m1.conjugate()<<endl;
cout<<"m1 adjoint "<<m1.adjoint()<<endl;
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver(m1.transpose()*m1);
cerr<<"eigen value "<<eigen_solver.eigenvalues()<<endl;
cerr<<"eigen vector "<<eigen_solver.eigenvectors()<<endl;
// Eigen::Array<float,1,2> a;
Eigen::Array<float,2,2>a;
a<<4, 5, 8, 1;
cout<<"a is \n"<<a<<endl;
cerr<<"a inverse"<<a.inverse()<<endl;
// cout<<"a array max "<<a.pow()<<endl<<endl;
Eigen::Matrix<float,2,2> mt,mt1;
mt<<4,-5,8,1;
mt1<<-4,5,8,-1;
mt.col(0).swap(mt1.col(0));
cout<<"mt min coefficient "<<mt.minCoeff()<<" "<<"mt max coeffcient "<<mt.maxCoeff()<<"cols max "<<mt.colwise().maxCoeff()<<"rows min"<<mt.rowwise().minCoeff()<<endl;
cout<<"mt is \n"<<mt<<endl;
cout<<"mt1 is "<<mt1<<endl;
cout<<"mt inverse is\n"<<mt.inverse()<<endl;;
cout<<" ABS suqare \n"<<mt.cwiseAbs2()<<endl;
cout<<" matrix inverse \n"<<mt.cwiseInverse()<<endl;
cout<<"matrix sqrt \n"<<mt.cwiseSqrt()<<endl;
cout<<"matrix square \n"<<mt.array().square()<<endl;
cout<<"matrix ABS IS\n"<<mt.cwiseAbs()<<endl;
cout<<" is queal \n"<<mt.cwiseEqual(mt1)<<endl;
Eigen::MatrixXd matrix1=Eigen::MatrixXd::Random(8,8);
cout<<"matrix1 is \n"<<matrix1<<endl;
cout<<"matrix1 block is \n"<<matrix1.block<1,1>(1,1)<<endl;
cout<<"matrix1 topLeftCorner is\n"<<matrix1.topLeftCorner(3,2)<<endl;
cout<<"matrix1 bottomLeftCorner is \n"<<matrix1.bottomLeftCorner(2,3)<<endl;
cout<<"matrix1 topRightCorner is \n"<<matrix1.topRightCorner(2,3)<<endl;
cout<<"matrix1 bottomRightCorner is \n"<<matrix1.bottomRightCorner(4,3)<<endl;
cout<<"matrix1 toprows is \n"<<matrix1.topRows(2)<<endl;
cout<<"matrix1 bottomrows is \n"<<matrix1.bottomRows(3)<<endl;
cout<<"matrix1 leftCols is \n"<<matrix1.leftCols(1)<<endl;
cout<<"matrix1 rightCols is \n"<<matrix1.rightCols(2)<<endl;
cout<<"replicate 1 time matrix1 \n"<<matrix1.replicate(1,2)<