#include
Matrix
Matrix
Matrix
Matrix
Matrix3f P, Q, R; // 3x3 float 矩阵.
Vector3f x, y, z; // 3x1 float 列向量.
RowVector3f a, b, c; // 1x3 float 行向量.
VectorXd v; // 动态长度double型列向量
// Eigen // Matlab // comments
x.size() // length(x) // 向量长度
C.rows() // size(C,1) // 矩阵行数
C.cols() // size(C,2) // 矩阵列数
x(i) // x(i+1) // 下标0开始
C(i,j) // C(i+1,j+1) //
A.resize(4, 4); // 如果越界触发运行时错误.
B.resize(4, 9); // 如果越界触发运行时错误.
A.resize(3, 3); // Ok; 没有越界.
B.resize(3, 9); // Ok; 没有越界.A <<1, 2, 3, // Initialize A. The elements can also be4, 5, 6, // matrices, which are stacked along cols7, 8, 9; // and then the rows are stacked.
B <A.fill(10); // Fill A with all 10&#39;s. 全10
// Eigen // Matlab
MatrixXd::Identity(rows,cols) // eye(rows,cols) 单位矩阵
C.setIdentity(rows,cols) // C &#61; eye(rows,cols) 单位矩阵
MatrixXd::Zero(rows,cols) // zeros(rows,cols) 零矩阵
C.setZero(rows,cols) // C &#61; ones(rows,cols) 零矩阵
MatrixXd::Ones(rows,cols) // ones(rows,cols)全一矩阵
C.setOnes(rows,cols) // C &#61; ones(rows,cols)全一矩阵
MatrixXd::Random(rows,cols) // rand(rows,cols)*2-1 // 元素随机在-1->1
C.setRandom(rows,cols) // C &#61; rand(rows,cols)*2-1 同上
VectorXd::LinSpaced(size,low,high) // linspace(low,high,size)&#39;线性分布的数组
v.setLinSpaced(size,low,high) // v &#61; linspace(low,high,size)&#39;线性分布的数组
// Eigen // Matlab
x.head(n) // x(1:n) 用于数组提取前n个[vector]
x.head
x.tail(n) // x(end - n &#43; 1: end)同理
x.tail
x.segment(i, n) // x(i&#43;1 : i&#43;n)同理
x.segment
P.block(i, j, rows, cols) // P(i&#43;1 : i&#43;rows, j&#43;1 : j&#43;cols)i,j开始&#xff0c;rows行cols列
P.block
P.row(i) // P(i&#43;1, :)i行
P.col(j) // P(:, j&#43;1)j列
P.leftCols
P.leftCols(cols) // P(:, 1:cols)左边cols列
P.middleCols
P.middleCols(j, cols) // P(:, j&#43;1:j&#43;cols)中间从j数cols列
P.rightCols
P.rightCols(cols) // P(:, end-cols&#43;1:end)右边cols列
P.topRows
P.topRows(rows) // P(1:rows, :)同列
P.middleRows
P.middleRows(i, rows) // P(i&#43;1:i&#43;rows, :)同列
P.bottomRows
P.bottomRows(rows) // P(end-rows&#43;1:end, :)同列
P.topLeftCorner(rows, cols) // P(1:rows, 1:cols)上左角rows行&#xff0c;cols列
P.topRightCorner(rows, cols) // P(1:rows, end-cols&#43;1:end)上右角rows行&#xff0c;cols列
P.bottomLeftCorner(rows, cols) // P(end-rows&#43;1:end, 1:cols)下左角rows行&#xff0c;cols列
P.bottomRightCorner(rows, cols) // P(end-rows&#43;1:end, end-cols&#43;1:end)下右角rows行&#xff0c;cols列
P.topLeftCorner
P.topRightCorner
P.bottomLeftCorner
P.bottomRightCorner
// Eigen // Matlab
R.row(i) &#61; P.col(j); // R(i, :) &#61; P(:, i)交换列为行
R.col(j1).swap(mat1.col(j2)); // R(:, [j1 j2]) &#61; R(:, [j2, j1]) 交换列
// Views, transpose, etc; all read-write except for .adjoint().
// Eigen // Matlab
R.adjoint() // R&#39; 伴随矩阵
R.transpose() // R.&#39; or conj(R&#39;)转置
R.diagonal() // diag(R)对角
x.asDiagonal() // diag(x)对角阵(没有重载<<)
R.transpose().colwise().reverse(); // rot90(R)所有元素逆时针转了90度
R.conjugate() // conj(R)共轭矩阵
// 与Matlab一致, 但是matlab不支持*&#61;等形式的运算.
// Matrix-vector. Matrix-matrix. Matrix-scalar.
y &#61; M*x; R &#61; P*Q; R &#61; P*s;
a &#61; b*M; R &#61; P - Q; R &#61; s*P;
a *&#61; M; R &#61; P &#43; Q; R &#61; P/s;R *&#61; Q; R &#61; s*P;R &#43;&#61; Q; R *&#61; s;R -&#61; Q; R /&#61; s;
对于Vector&#xff0c;norm返回的是向量的二范数&#xff0c;即 对于Matrix&#xff0c;norm返回的是矩阵的弗罗贝尼乌斯范数&#xff08;Frobenius Norm&#xff09;&#xff0c;即 即 例如&#xff1a; 清楚了norm()的定义后&#xff0c;normalize()其实就是把自身的各元素除以它的范数。返回值为void。 例如&#xff1a; 而normalized()与normalize()类似&#xff0c;只不过normalize()是对自身上做修改&#xff0c;而normalized()返回的是一个新的Vector/Matrix&#xff0c;并不改变原有的矩阵。 使用动态矩阵 使用普通矩阵 TriangularView gives a view on a triangular part of a dense matrix and allows to perform optimized operations on it. The opposite triangular part is never referenced and can be used to store other information. This class represents a triangular part of a matrix, not necessarily square. Strictly speaking, for rectangular matrices one should speak of "trapezoid" parts. This class is the return type of MatrixBase::triangularView() and SparseMatrixBase::triangularView(), and most of the time this is the only way it is used. 例如 输出结果为// Vectorized operations on each element independently
// Eigen // Matlab
R &#61; P.cwiseProduct(Q); // R &#61; P .* Q 对应点相乘
R &#61; P.array() * s.array();// R &#61; P .* s 对应点相乘
R &#61; P.cwiseQuotient(Q); // R &#61; P ./ Q 对应点相除
R &#61; P.array() / Q.array();// R &#61; P ./ Q对应点相除
R &#61; P.array() &#43; s.array();// R &#61; P &#43; s对应点相加
R &#61; P.array() - s.array();// R &#61; P - s对应点相减
R.array() &#43;&#61; s; // R &#61; R &#43; s全加s
R.array() -&#61; s; // R &#61; R - s全减s
R.array()
R.cwiseInverse(); // 1 ./ P
R.array().inverse(); // 1 ./ P
R.array().sin() // sin(P)
R.array().cos() // cos(P)
R.array().pow(s) // P .^ s
R.array().square() // P .^ 2
R.array().cube() // P .^ 3
R.cwiseSqrt() // sqrt(P)
R.array().sqrt() // sqrt(P)
R.array().exp() // exp(P)
R.array().log() // log(P)
R.cwiseMax(P) // max(R, P) 对应取大
R.array().max(P.array()) // max(R, P) 对应取大
R.cwiseMin(P) // min(R, P) 对应取小
R.array().min(P.array()) // min(R, P) 对应取小
R.cwiseAbs() // abs(P) 绝对值
R.array().abs() // abs(P) 绝对值
R.cwiseAbs2() // abs(P.^2) 绝对值平方
R.array().abs2() // abs(P.^2) 绝对值平方
(R.array() 9. 矩阵化简
// Reductions.
int r, c;
// Eigen // Matlab
R.minCoeff() // min(R(:))最小值
R.maxCoeff() // max(R(:))最大值
s &#61; R.minCoeff(&r, &c) // [s, i] &#61; min(R(:)); [r, c] &#61; ind2sub(size(R), i);
s &#61; R.maxCoeff(&r, &c) // [s, i] &#61; max(R(:)); [r, c] &#61; ind2sub(size(R), i);
R.sum() // sum(R(:))求和
R.colwise().sum() // sum(R)列求和1×N
R.rowwise().sum() // sum(R, 2) or sum(R&#39;)&#39;行求和N×1
R.prod() // prod(R(:))所有乘积
R.colwise().prod() // prod(R)列乘积
R.rowwise().prod() // prod(R, 2) or prod(R&#39;)&#39;行乘积
R.trace() // trace(R)迹
R.all() // all(R(:))且运算
R.colwise().all() // all(R) 且运算
R.rowwise().all() // all(R, 2) 且运算
R.any() // any(R(:)) 或运算
R.colwise().any() // any(R) 或运算
R.rowwise().any() // any(R, 2) 或运算10. 矩阵点乘
// Dot products, norms, etc.
// Eigen // Matlab
x.norm() // norm(x). 模
x.squaredNorm() // dot(x, x) 平方和
x.dot(y) // dot(x, y)
x.cross(y) // cross(x, y) Requires #include 11. 矩阵类型转换
Type conversion
// Eigen // Matlab
A.cast
A.cast
A.cast
A.real(); // real(A)
A.imag(); // imag(A)
// if the original type equals destination type, no work is done12. 向量的模
norm()
Vector2d vec(3.0,4.0);
cout <Matrix2d mat;mat <<1,23,4;cout <
normalize()
vec.normalize();
cout <normalized()
Eigen::Vector3d dir;# 返回向量的模
dir.norm()# 返回归一化后的向量
dir.normalized()13. LLT分解
Eigen::MatrixXd matrix;Eigen::MatrixXd l_matrix &#61; Eigen::LLT
Eigen::Matrix
14. 本征值分解
Eigen::MatrixXd covariance(rows, cols);
Eigen::SelfAdjointEigenSolver
Eigen::VectorXd eigenvalues &#61; solver.eigenvalues();
Eigen::MatrixXd eigenvectors &#61; solver.eigenvectors(); // 列矢量15. SVD分解
JacobiSVD
U &#61; svd.matrixU();
V &#61; svd.matrixV();
A &#61; svd.singularValues();16. TriangularView
Eigen::Matrix
m <<1, 2, 3,4, 5, 6,7, 8, 9;m.triangularView1 2 3
0 5 6
0 0 917. 求解
x &#61; A.solve(b); // x &#61; inv(A) * bA.solveInPlace(b); // b &#61; inv(A) * b
L1.triangularView
L1.triangularView
U1.triangularView
M1.selfadjointView
M1.selfadjointView
参考文献