- 头文件:
#include <Eigen/Core> //Eigen部分
#include <Eigen/Dense> //稠密矩阵的代数运算(逆、特征值等)
- 声明一个矩阵:
//声明一个2*3的float矩阵
Eigen::Matrix<float , 2 ,3> = materix_23;
//定义一个动态大小的矩阵
Eigen::Matrix<double, Eigen::Dynamix, Eigen::Dynamic> matrix_dynamix;
//Vector3d实质上是Eigen::Matrix<double,3,1>;
Eigen::Vector3d v_3d; //列向量
//Rosvector实质上是Eigen::Matrix<double,1,3>;
Eigen::RowVector3f r_3d; //行向量
//Matrix3d实质为Eigen::matrix<double, 3, 3>
EIgen::Materix3d materix_33
//几种Eigen的形式
旋转矩阵(3*3): Eigen::Matreix3d;
旋转向量(3*1): Eigen::AngleAxisd;
欧拉角(3*1): Eigen::Vector3d;
四元数(4*1): Eigen::Quaterniond;
欧式变换矩阵(4*4): Eigen::Isometry3d;
仿射变换(4*4): Eigen::Affine3d;
射影变换(4*4): Eigen::Projective3d;
//Eigen特殊矩阵
MatrixXd::Identity(rows,cols) // eye(rows,cols) 单位矩阵
C.setIdentity(rows,cols) // C = eye(rows,cols) 单位矩阵
MatrixXd::Zero(rows,cols) // zeros(rows,cols) 零矩阵
C.setZero(rows,cols) // C = ones(rows,cols) 零矩阵
MatrixXd::Ones(rows,cols) // ones(rows,cols)全一矩阵
C.setOnes(rows,cols) // C = ones(rows,cols)全一矩阵
MatrixXd::Random(rows,cols) // rand(rows,cols)*2-1 // 元素随机在-1->1
C.setRandom(rows,cols) // C = rand(rows,cols)*2-1 同上
VectorXd::LinSpaced(size,low,high) // linspace(low,high,size)'线性分布的数组
v.setLinSpaced(size,low,high) // v = linspace(low,high,size)'线性分布的数组
- 输入矩阵:
matrix_23 << 1,2,3,4,5,6; //输入一个2*3的矩阵
//结果//
1 2 3
4 5 6
- 输出矩阵:
for(int i=0; i<1 ; i++)
for(int j=0; j<1 ; j++)
cout << matrix_23(i,j) << endl;
///结果
1
2
x.size() // length(x) // 向量长度
C.rows() // size(C,1) // 矩阵行数
C.cols() // size(C,2) // 矩阵列数
- 类型转换
//matrix_23为float型,转换为double,其中v_3d为double的3,2,1;
Eigen::Matrix<double ,2 ,1 > result = matrix_23.cast<double>() * v_3d
/结果/
10
28
- 产生随机数
matrix = Eigen::matrix3d::Random(); //产生一个随机的3*3的矩阵
- 矩阵的运算:
matrix_33.transpose() //转置矩阵
matrix_33.sum() //各元素和
matrix_33.trace() //迹
10*matrix_33 //数乘
matrix_33.inverse() //逆矩阵
matrix_33.determinant() //行列式
matrix_33.adjoint() //伴随矩阵
matrix_33.conjugate() //共轭矩阵