c++slam库相关

Eigen库使用指南

2018-07-18  本文已影响0人  zhaoQiang012

Eigen库使用指南

1.模块和头文件

2. Matrix类

Matrix<typename Scalar, 
       int RowsAtCompileTime, 
       int ColsAtCompileTime,
       int Options = 0,
       int MaxRowsAtCompileTime = RowsAtCompileTime,
       int MaxColsAtCompileTime = ColsAtCompileTime>
# Scalar 元素类型
# RowsAtCompileTime 行
# ColsAtCompileTime 列
# 例 typedef Matrix<int, 3, 3> Matrix3i;
# Options 比特标志位
# MaxRowsAtCompileTime和MaxColsAtCompileTime表示在编译阶段矩阵的上限。

# 列向量
typedef Matrix<double, 3, 1> Vector3d;
# 行向量
typedef Matrix<float, 1, 3> RowVector3f;

# 动态大小
typedef Matrix<double, Dynamic, Dynamic> MatrixXd;
typedef Matrix<float, Dynamic, 1> VectorXf;
type

3. 矩阵与向量的运算

MatrixXcf a = MatrixXcf::Random(3,3);
a.transpose();  # 转置
a.conjugate();  # 共轭
a.adjoint();       # 共轭转置(伴随矩阵)
# 对于实数矩阵,conjugate不执行任何操作,adjoint等价于transpose
a.transposeInPlace() #原地转置

Vector3d v(1,2,3);
Vector3d w(4,5,6);
v.dot(w);    # 点积
v.cross(w);  # 叉积

Matrix2d a;
a << 1, 2, 3, 4;
a.sum();      # 所有元素求和
a.prod();      # 所有元素乘积
a.mean();    # 所有元素求平均
a.minCoeff();    # 所有元素中最小元素
a.maxCoeff();   # 所有元素中最大元素
a.trace();      # 迹,对角元素的和
# minCoeff和maxCoeff还可以返回结果元素的位置信息
int i, j;
a.minCoeff(&i, &j);

4. Array类

Array<typename Scalar,
      int RowsAtCompileTime,
      int ColsAtCompileTime>
# 常见类定义
typedef Array<float, Dynamic, 1> ArrayXf
typedef Array<float, 3, 1> Array3f
typedef Array<double, Dynamic, Dynamic> ArrayXXd
typedef Array<double, 3, 3> Array33d

ArrayXf a = ArrayXf::Random(5);
a.abs();    # 绝对值
a.sqrt();    # 平方根
a.min(a.abs().sqrt());  # 两个array相应元素的最小值

4. 块操作

块是matrixarray中的矩形子块。

// 方法1
.block(i, j, p, q)    //起点(i, j),块大小(p, q),构建一个动态尺寸的block
.block<p, q>(i, j)  // 构建一个固定尺寸的block
operater dynamic-size block fixed_size block
左上角 matrix.topLeftCorner(p,q) matrix.topLeftCorner<p,q>()
左下角 matrix.bottomLeftCorner(p,q) matrix.bottomLeftCorner<p,q>()
右上角 matrix.topRightCorner(p,q) matrix.topRightCorner<p,q>()
右下角 你猜 你猜
前q行 matrix.topRows(q) matrix.topRows<q>()
后q行 matrix.bottomRows(q) matrix.bottomRows<q>()
左p列 matrix.leftCols(p) matrix.leftCols<p>()
右p列 matrix.rightCols(p) matrix.rightCols<p>()
operater dynamic_size block fixed_size block
前n个 vector.head(n) vector.head<n>()
后n个 vector.tail(n) vector.tail<n>()
从i开始的n个元素 vector.segment(i,n) vector.segment<n>(i)

5. 矩阵初始化

// 初始化列表除数字外也可以是vectors或matrix
RowVectorXd vec1(3);
vec1 << 1,2,3;
RowVectorXd vec2(2);
vec2 << 4,5;
RowVectorXd vec3(5);
vec3 << vec1, vec2;
// 也可以使用block结构初始化 

6. 归约,迭代器,广播

// sample
ArrayXXf A(2, 2);
A << 1,2,3,4;
(A > 0).all();
(A > 0).any();
(A > 0).count();
// sample
Eigen::MatrixXf m(2,2);
m << 1,2,3,4;
MatrixXf::Index maxRow, maxCol;
float max = m.maxCoeff(&minRow, &minCol);
// sample
Eigen::MatrixXf mat(2,3);
mat << 1,2,3,
       4,5,6;
std::cout << mat.colwise().maxCoeff();
// output: 4, 5, 6
// mat.rowWise() the same as before
// sample
Eigen::MatrixXf mat(2,3);
Eigen::VectorXf v(2);

mat << 1,2,3,4,5,6;
v << 0,1;
mat.colwise() += v;
// output: 1, 2, 3, 5, 6, 7

7. Map类


8. 混淆问题

MatrixXi mat(3,3);
mat << 1,2,3, 4,5,6, 7,8,9;
mat.bottomRightCorner(2,2) = mat.topLeftCorner(2,2).eval();
普通函数 inplace函数
MatrixBase::adjoint() MatrixBase::adjointInPlace()
DenseBase::reverse() DenseBase::reverseInPlace()
LDLT::solve() LDLT::solveInPlace()
LLT::solve() LLT::solveInPlace()
TriangularView::solve() TriangularView::solveInPlace()
DenseBase::transpose() DenseBase::transposeInPlace()

上一篇 下一篇

猜你喜欢

热点阅读