Eigen和Sophus 用法的详细介绍
一、Eigen
提供了快速的矩阵线性代数运算,矩阵、向量、旋转矩阵、变换矩阵。
Eigen是纯头文件搭建成的库,使用时只需引入Eigen的头文件即可,不需要链接库文件target_link。
#include <Eigen>
CMakeLists.txt需要添加头文件
include_directories("/user/include/eigen3");
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) // 向量长度 C.rows() // size(C,1) // 矩阵行数 C.cols() // size(C,2) // 矩阵列数 x(i) // x(i+1) // 下标0开始 C(i,j) // C(i+1,j+1) // 下标0开始
Eigen 中矩阵的使用方法
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, 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.row(i) // P(i+1, :)i行
P.col(j) // P(:, j+1)j列
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.topRows<rows>() // P(1:rows, :)同列
P.topRows(rows) // P(1:rows, :)同列
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, :)同列
P.bottomRows(rows) // P(end-rows+1:end, :)同列
P.topLeftCorner(rows, cols) // P(1:rows, 1:cols)上左角rows行,cols列
P.topRightCorner(rows, cols) // P(1:rows, end-cols+1:end)上右角rows行,cols列
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.topRightCorner<rows,cols>() // P(1:rows, end-cols+1:end)同上
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
R.row(i) = P.col(j); // R(i, :) = P(:, i)交换列为行
R.col(j1).swap(mat1.col(j2)); // R(:, [j1 j2]) = R(:, [j2, j1]) 交换列
Eigen 中矩阵转置
// Views, transpose, etc; all read-write except for .adjoint().
// Eigen // Matlab
R.adjoint() // R' 伴随矩阵
R.transpose() // R.' or conj(R')转置
R.diagonal() // diag(R)对角
x.asDiagonal() // diag(x)对角阵(没有重载<<)
R.transpose().colwise().reverse(); // rot90(R)所有元素逆时针转了90度
R.conjugate() // conj(R)共轭矩阵
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.colwise().sum() // sum(R)列求和1×N
R.rowwise().sum() // sum(R, 2) or sum(R')'行求和N×1
R.prod() // prod(R(:))所有乘积
R.colwise().prod() // prod(R)列乘积
R.rowwise().prod() // prod(R, 2) or prod(R')'行乘积
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) 或运算
Eigen 矩阵中值对应的位置
#include<Eigen/Core> #include<iostream> using namespace std; using namespace Eigen;int main {MatrixXd::Index maxRow, maxCol;MatrixXd::Index minRow, minCol;MatrixXd mMat(4,4);mMat << 11, 10, 13, 15,3, 24, 56, 1,2, 12, 45, 0,8, 5, 6, 4;double min = mMat.minCoeff(&minRow,&minCol);double max = mMat.maxCoeff(&maxRow,&maxCol);cout << "Max = \n" << max << endl;cout << "Min = \n" << min << endl;cout << "minRow = " << minRow << "minCol = " <<minCol<<endl;cout << "maxRow = " << maxRow << "maxCol = " << maxCol << endl;return 0; }
输出结果:
Eigen 中矩阵点乘
// 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 <Eigen/Geometry>
Eigen 中矩阵类型转换
Type conversion
// Eigen // Matlab
A.cast<double>(); // double(A)
A.cast<float>(); // single(A)
A.cast<int>(); // int32(A) 向下取整
A.real(); // real(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()//对mata进行cholesky分解并存储到 MatL和MatDEigen::MatrixXd mata;Eigen::VectorXd rk_; mata = Vk_vec[M_Td-1].inverse();LDLT<Eigen::MatrixXd> tmp(mata);Eigen::MatrixXd MatL = mata.llt().matrixL();// Eigen::MatrixXd MatU = mata.llt().matrixU();
Eigen 中矩阵特征值
// Eigen // Matlab
A.eigenvalues(); // eig(A);特征值
EigenSolver<Matrix3d> eig(A); // [vec val] = eig(A)
eig.eigenvalues(); // diag(val)与前边的是一样的结果
eig.eigenvectors(); // vec 特征值对应的特征向量
二、Sophus
李代数库,SO(3),SE(3),Sim(3)
#include <sophus>
SO(3)
//旋转向量转为旋转矩阵,沿Z轴转90度
Matrix3d R=AngleAxisd(M_PI/2,Vector3d(0,0,1)).toRotationMatrix();
//SO(3)由旋转矩阵构造
Sophus::SO3 SO3_R(R);
//由旋转向量构造
Sophus::SO3 SO3_v(0,0,M_PI/2);
//由四元数构造
Eigen::Quaterniond q(R);//先专成四元数
Sophus::SO3 SO3_q(q)
对数映射获得李代数
//由旋转矩阵取对数得到旋转向量
Vector3d so3=SO3_R.log();
cout<<"so3="<<so3.transpose()<<endl;
//向量so3到反对称矩阵hat^
cout<<"so3 hat"<<SO3::hat(so3)<<endl;
//反过来,反对称矩阵到向量vee
增加扰动模型的更新
Vector3d update_so3(1e-4,0,0);
SO3 SO3_updated=SO3::exp(update_so3)*SO3_R;//左乘更新
cout<<SO3_updated<<endl;
SE(3)
//平移向量t
Eigen::Vector3d t(1,0,0);
//构造SE(3)
Sophus::SE3 SE3_Rt(R,t);
Sophus::SE3 SE3_qt(q,t);
//对数映射获得李代数se3
typedef Eigen::Matrix<double,6,1> Vector6d;
Vector6d se3=SE3_Rt.log();
cout<<"se3="<<se3.transpose()<<endl;
//hat和vee
SE3::hat(se3)
SE3::vee(SE3::hat(se3)).transpose()
//扰动更新
Vector6d update_se3;
update_se3.setZero();
update_se3(0,0)=1e-4d;
SE3 SE3_updated=SE3::exp(update_se3)*SE3_Rt;
cout<<"SE3 updated="<<SE3_updated.matrix()<<endl;
Eigen和Sophus 用法的详细介绍相关推荐
- mysql raiserror_sql server数据库中raiserror函数用法的详细介绍
sql server数据库中raiserror函数的用法 server数据库中raiserror的作用就和asp.NET中的throw new Exception一样,用于抛出一个异常或错误.这个错误 ...
- 关于数据库having的用法的详细介绍
转自:微点阅读 https://www.weidianyuedu.com 数据库having的用法的用法你知道吗?下面小编就跟你们详细介绍下数据库having的用法的用法,希望对你们有用. 数据库h ...
- python or的用法_详细介绍Python中and和or实际用法
and 和 or 的特殊性质 在Python 中,and 和 or 执行布尔逻辑演算,但是它们并不返回布尔值:而是,返回它们实际进行比较的值之一.下面来看一下实例.>>> 'a' a ...
- java选择语句中switch的用法(详细介绍)
一.什么时候用switch? 在java中控制流程语句是由选择语句.循环语句.跳转语句构成.选择语句包括 if 和 switch,在过多的使用 if 语句嵌套会使程序很难阅读,这时利用 switch ...
- StringUtils常用方法+StringUtils详细介绍
StringUtils用法+StringUtils详细介绍 博文来源:http://yijianfengvip.blog.163.com/blog/static/1752734322012122219 ...
- php中sisson用法,详细介绍php中session的用法
PHP中的session默认情况下是使用客户端的Cookie.当客户端的Cookie被禁用时,会自动通过Query_String来传递. Php处理会话的函数一共有11个,我们详细介绍一下将要用到几个 ...
- 数据库having的用法详细介绍
转自:微点阅读 https://www.weidianyuedu.com 数据库having的用法的用法你知道吗?下面微点阅读小编就跟你们详细介绍下数据库having的用法的用法,希望对你们有用. ...
- 【MADDPG(MPE)——环境配置与用法详细介绍(多智能体强化学习))】
MADDPG(MPE)--环境配置与用法详细介绍(多智能体强化学习) MADDPG(MPE) 介绍 MPE环境安装教程 前期准备 MPE 安装包介绍 MPE 安装环境要求 开始安装 环境测试 MPE环 ...
- html table colgroup,关于HTML colgroup标签的用法你知道哪些?colgroup用法和col的详细介绍...
html中colgroup是什么意思?HTML colgroup标签的用法你又知道哪些?在这里,本篇文章就为大家介绍了关于HTML colgroup标签的用法详解,还有colgroup和col元素的详 ...
最新文章
- MySQL主从复制的常用拓扑结构
- ubuntu 12.04 添加 IP并配置DNS
- $.ajax data怎么处理_不会吧,不会吧,不愧是Ajax,jQuery Ajax啊
- 《精通 ASP.NET MVC 5》----1.8 本书所需的软件
- Python代码注释应该怎么写?
- Python可视化神器之pyecharts
- 20155307 2016-2017-2 《Java程序设计》第4周学习总结
- Required String parameter 'images' is not present
- 中文拼写纠错_58搜索拼写纠错
- 大数据分析软件具备哪些功能特点
- ViT (Vision Transformer) ---- Transformer Model(1)
- Camtasia 2021mac版
- 安装SQL Server 2012过程中出现“启用windows功能NetFx3时出错”
- 文件服务器 标签,别再「新建文件夹」了,这个标签管理器可以让你的硬盘更整洁...
- Atitit 华为基本法 attilax读后感
- PREEvision软件-汽车电子电气架构的开发工具
- SaaS前世今生:老树开新花
- html5 blockquote,html5 – 正确的HTML Blockquote
- 2021-2027全球与中国智能访客管理系统市场现状及未来发展趋势
- docker容器启动后无法访问宿主机host