Eigen中基本和常⽤函数Eigen 中矩阵的定义
#include<Eigen/Dense>// 基本函数只需要包含这个头⽂件
Matrix<double,3,3> A;// 固定了⾏数和列数的矩阵和Matrix3d⼀致.
Matrix<double,3, Dynamic> B;// 固定⾏数.
Matrix<double, Dynamic, Dynamic> C;// 和MatrixXd⼀致.
Matrix<double,3,3, RowMajor> E;// 按⾏存储; 默认按列存储.
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)          // 向量长度
x(i)// x(i+1)            // 下标0开始
C(i,j)// C(i+1,j+1)        // 下标0开始
Eigen 中矩阵的使⽤⽅法
A <<1,2,3,// Initialize A. The elements can also be
4,5,6,// matrices, which are stacked along cols
7,8,9;// and then the rows are stacked.
B << A, A, A;// B is three horizontally stacked A's.  三⾏A
A.fill(10);// Fill A with all 10's.                  全10
Eigen 中常⽤矩阵⽣成
// Eigen                            // Matlab
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)'线性分布的数组
Eigen 中矩阵分块
// Eigen                          // Matlab
x.head(n)// x(1:n)    ⽤于数组提取前n个[vector]
x.head<n>()// x(1:n)    同理
x.tail(n)// x(end - n + 1: end)同理
x.tail<n>()// x(end - n + 1: end)同理
x.segment(i, n)// x(i+1 : i+n)同理
x.segment<n>(i)// x(i+1 : i+n)同理
P.block(i, j, rows, cols)// P(i+1 : i+rows, j+1 : j+cols)i,j开始,rows⾏cols列
P.block<rows, cols>(i, j)// P(i+1 : i+rows, j+1 : j+cols)i,j开始,rows⾏cols列
P.leftCols<cols>()// P(:, 1:cols)左边cols列
P.leftCols(cols)// P(:, 1:cols)左边cols列
P.middleCols<cols>(j)// P(:, j+1:j+cols)中间从j数cols列
P.middleCols(j, cols)// P(:, j+1:j+cols)中间从j数cols列
P.rightCols<cols>()// P(:, end-cols+1:end)右边cols列
P.rightCols(cols)// P(:, end-cols+1:end)右边cols列
P.middleRows<rows>(i)// P(i+1:i+rows, :)同列
P.middleRows(i, rows)// P(i+1:i+rows, :)同列
P.bottomRows<rows>()// P(end-rows+1:end, :)同列
resize函数c++
P.bottomRows(rows)// P(end-rows+1:end, :)同列
P.bottomLeftCorner(rows, cols)// P(end-rows+1:end, 1:cols)下左⾓rows⾏,cols列
P.bottomRightCorner(rows, cols)// P(end-rows+1:end, end-cols+1:end)下右⾓rows⾏,cols列P.topLeftCorner<rows,cols>()// P(1:rows, 1:cols)同上
P.bottomLeftCorner<rows,cols>()// P(end-rows+1:end, 1:cols)同上
P.bottomRightCorner<rows,cols>()// P(end-rows+1:end, end-cols+1:end)同上
Eigen 中矩阵元素交换
// Eigen                          // Matlab
Eigen 中矩阵转置
// Views, transpose, etc; all read-write except for .adjoint().
// Eigen                          // Matlab
R.adjoint()// R' 伴随矩阵
R.diagonal()// diag(R)对⾓
x.asDiagonal()// diag(x)对⾓阵(没有重载<<)
Eigen 中矩阵乘积
// 与Matlab⼀致, 但是matlab不⽀持*=等形式的运算.
// Matrix-vector.  Matrix-matrix.  Matrix-scalar.
y  = M*x;          R  = P*Q;        R  = P*s;
a  = b*M;          R  = P - Q;      R  = s*P;
a *= M;            R  = P + Q;      R  = P/s;
R *= Q;          R  = s*P;
R += Q;          R *= s;
R -= Q;          R /= s;
Eigen 中矩阵元素操作
// Vectorized operations on each element independently
// Eigen                  // Matlab
R = P.cwiseProduct(Q);// R = P .* Q 对应点相乘
R = P.array()* s.array();// R = P .* s 对应点相乘
R = P.cwiseQuotient(Q);// R = P ./ Q 对应点相除
R = P.array()/ Q.array();// R = P ./ Q对应点相除
R = P.array()+ s.array();// R = P + s对应点相加
R = P.array()- s.array();// R = P - s对应点相减
R.array()+= s;// R = R + s全加s
R.array()-= s;// R = R - s全减s
R.array()< Q.array();// R < Q 以下的都是针对矩阵的单个元素的操作R.array()<= Q.array();// R <= Q矩阵元素⽐较,会在相应位置置0或1 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()< s).select(P,Q);// (R < s ? P : Q)这个也是单个元素的操作Eigen 中矩阵化简
// Reductions.
int r, c;
// Eigen                  // Matlab
R.minCoeff()// min(R(:))最⼩值
R.maxCoeff()// max(R(:))最⼤值
s = R.minCoeff(&r,&c)// [s, i] = min(R(:)); [r, c] = ind2sub(size(R), i); s = R.maxCoeff(&r,&c)// [s, i] = max(R(:)); [r, c] = ind2sub(size(R), i); R.sum()// sum(R(:))求和
R.prod()// prod(R(:))所有乘积
R.all()// all(R(:))且运算
R.any()// any(R(:)) 或运算
Eigen 中矩阵点乘
// Dot products, norms, etc.
// Eigen                  // Matlab
<()// norm(x).    模
x.squaredNorm()// dot(x, x)  平⽅和
x.dot(y)// dot(x, y)
Eigen 中矩阵类型转换
Type conversion
// Eigen                          // Matlab
A.cast<double>();// double(A)
A.cast<float>();// single(A)
A.cast<int>();// int32(A) 向下取整
A.imag();// imag(A)
// if the original type equals destination type, no work is done
Eigen 中求解线性⽅程组 Ax = b
// Solve Ax = b. Result stored in x. Matlab: x = A \ b.
x = A.ldlt().solve(b));// #include <Eigen/Cholesky>LDLT分解法实际上是Cholesky分解法的改进x = A.llt().solve(b));// A sym. p.d.      #include <Eigen/Cholesky>
x = A.lu().solve(b));// Stable and fast. #include <Eigen/LU>
x = A.qr().solve(b));// No pivoting.    #include <Eigen/QR>
x = A.svd().solve(b));// Stable, slowest. #include <Eigen/SVD>
// .ldlt() -> .matrixL() and .matrixD()
// .llt()  -> .matrixL()
// .lu()  -> .matrixL() and .matrixU()
// .qr()  -> .matrixQ() and .matrixR()
// .svd()  -> .matrixU(), .singularValues(), and .matrixV()
Eigen 中矩阵特征值
// Eigen                          // Matlab
A.eigenvalues();// eig(A);特征值
EigenSolver<Matrix3d>eig(A);// [vec val] = eig(A)
eig.eigenvalues();// diag(val)与前边的是⼀样的结果
eig.eigenvectors();// vec 特征值对应的特征向量

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。