Eigen模板类The Matrix class
The Matrix class (for Dense Matrix and array manipulation)
Eigen中所有矩阵(matrices)和向量(vectors)都是Matrix模板类的实例化对象,其中向量为矩阵的特殊情况,一行或者一列。
我们关注Matrix模板类的前三个参数
Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>, 前三个参数分别为<数据类型, 行数, 列数>
- 数据类型
Scalar RowsAtCompileTime和ColsAtCompileTime
常用数据类型
- Eigen预设的一些数据类型都有哪些呢?https://eigen.tuxfamily.org/dox/namespaceEigen.html#title2
Eigen::Matrix3f:3x3方阵,数据类型:floattypedef Matrix<float, 4, 4> Matrix4f;shape: (4, 4), dtype: floattypedef Matrix<float, Dynamic, 2> MatrixX2f;shape: (N, 2), dtype: floatMatrix.row(i)返回矩阵的第i行向量
Vectors向量
Eigen中向量为矩阵的特殊形式,即一行或者一列。其中列向量更为常见(有时缩写为向量)
举例:
typedef Matrix<float, 3, 1> Vector3f;(列)向量:shape(3, 1)typedef Matrix<int, 1, 2> RowVector2i;行向量vec.norm();返回向量的二范数
Eigen中的特殊值Dynamic
TODO
#include <iostream>
#include <Eigen/Dense>
int main() {
// Eigen test code
// Matrix
Eigen::MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << "Here is the matrix m:\n" << m << std::endl;
// std::cout << m(0) << std::endl; // the first element (RowMajor)
// std::cout << m(1, 2) << std::endl;
// Eigen (Column) Vector Test code
Eigen::Vector3f p(1, 2, 3);
std::cout << p[0] << ' ' << p[1] << ' ' << p[2] << std::endl;
std::cout << "p.norm(): " << p.norm() << std::endl;
p.data() // return a pointer to the data array of this matrix
// Eigen Matrix & Vector Operation
Eigen::Matrix2f mat;
mat << 1, 2,
3, 4; // 矩阵数据以列主序的格式进行存储 (1, 3, 2, 4)
Eigen::Matrix2f matmul = mat*mat;
std::cout << "matmul: \n" << matmul << std::endl;
std::cout << "transpose: \n" << matmul.transpose() << std::endl;
return 0;
}
CMake
Eigen库只有头文件(header-only)
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIRS})

浙公网安备 33010602011771号