[Eigen]

2023-05-16

Eigen 库学习笔记
参考原文为:https://www.cnblogs.com/rainbow70626/p/8819119.html
为了方便查看,以防找不到,所以写了这篇博客。

目录

Eigen 矩阵定义
Eigen 基础使用
Eigen 特殊矩阵生成
Eigen 矩阵分块
Eigen 矩阵元素交换
Eigen 矩阵转置
Eigen 矩阵乘积
Eigen 矩阵单个元素操作
Eigen 矩阵化简
Eigen 矩阵点乘
Eigen 矩阵类型转换
Eigen 求解线性方程组 Ax = b
Eigen 矩阵特征值

Eigen 矩阵定义

#include <Eigen/Dense>

Matrix<double, 3, 3> A;               // Fixed rows and cols. Same as Matrix3d.
Matrix<double, 3, Dynamic> B;         // Fixed rows, dynamic cols.
Matrix<double, Dynamic, Dynamic> C;   // Full dynamic. Same as MatrixXd.
Matrix<double, 3, 3, RowMajor> E;     // Row major; default is column-major.
Matrix3f P, Q, R;                     // 3x3 float matrix.
Vector3f x, y, z;                     // 3x1 float matrix.
RowVector3f a, b, c;                  // 1x3 float matrix.
VectorXd v;                           // Dynamic column vector of doubles
// Eigen          // Matlab           // comments
x.size()          // length(x)        // vector size
C.rows()          // size(C,1)        // number of rows
C.cols()          // size(C,2)        // number of columns
x(i)              // x(i+1)           // Matlab is 1-based
C(i,j)            // C(i+1,j+1)       //

Eigen基本使用

// Basic usage
// Eigen        // Matlab           // comments
x.size()        // length(x)        // vector size
C.rows()        // size(C,1)        // number of rows
C.cols()        // size(C,2)        // number of columns
x(i)            // x(i+1)           // Matlab is 1-based
C(i, j)         // C(i+1,j+1)       //

A.resize(4, 4);   // Runtime error if assertions are on.
B.resize(4, 9);   // Runtime error if assertions are on.
A.resize(3, 3);   // Ok; size didn't change.
B.resize(3, 9);   // Ok; only dynamic cols changed.
                  
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.fill(10);       // Fill A with all 10's.

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 = zeros(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        // MatrixXd::Random returns uniform random numbers in (-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 矩阵分块

// Matrix slicing and blocks. All expressions listed here are read/write.
// Templated size versions are faster. Note that Matlab is 1-based (a size N
// vector is x(1)...x(N)).
// Eigen                           // Matlab
x.head(n)                          // x(1:n)
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)
P.block<rows, cols>(i, j)          // P(i+1 : i+rows, j+1 : j+cols)
P.row(i)                           // P(i+1, :)
P.col(j)                           // P(:, j+1)
P.leftCols<cols>()                 // P(:, 1:cols)
P.leftCols(cols)                   // P(:, 1:cols)
P.middleCols<cols>(j)              // P(:, j+1:j+cols)
P.middleCols(j, cols)              // P(:, j+1:j+cols)
P.rightCols<cols>()                // P(:, end-cols+1:end)
P.rightCols(cols)                  // P(:, end-cols+1:end)
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)
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)
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 矩阵元素交换

// Of particular note is Eigen's swap function which is highly optimized.
// 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)
R.conjugate()                      // conj(R)

Eigen 矩阵乘积

// All the same as Matlab, but matlab doesn't have *= style operators.
// 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
R.array() -= s;           // R = R - s
R.array() < Q.array();    // R < Q
R.array() <= Q.array();   // R <= Q
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)
R.rowwise().sum()         // sum(R, 2) or sum(R')'
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 矩阵点乘

// Dot products, norms, etc.
// Eigen                  // Matlab
x.norm()                  // norm(x).    Note that norm(R) doesn't work in Eigen.
x.squaredNorm()           // dot(x, x)   Note the equivalence is not true for complex
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));  // A sym. p.s.d.    #include <Eigen/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 矩阵特征值

// Eigenvalue problems
// Eigen                          // Matlab
A.eigenvalues();                  // eig(A);
EigenSolver<Matrix3d> eig(A);     // [vec val] = eig(A)
eig.eigenvalues();                // diag(val)
eig.eigenvectors();               // vec
// For self-adjoint matrices use SelfAdjointEigenSolver<>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[Eigen] 的相关文章

  • OpenCV CV::Mat 和 Eigen::Matrix

    有没有一种可逆的方式来转换 OpenCVcv Mat反对Eigen Matrix 例如 某种做法 cv Mat cvMat Eigen Matrix eigMat camera gt retrieve cvMat magic to conv
  • 如何使用 Eigen 库计算零空间的基础?

    如何计算零空间的基础带有特征库的矩阵 我试图找到显式函数名计算空基 并且作为解决方法 找到方法计算矩阵的 rref 因为我们能够从 rref 获得零基础 但我找不到任何相关的函数名称 我认为必须有解决方案 但我对 Eigen 库了解不多 而
  • 按欧拉角输入旋转四元数

    我正在编写一段代码来控制 3D 空间中的机械臂 机械臂通过四元数处理旋转 但我希望用户通过改变偏航 俯仰和滚动来控制它 因为人类使用这些更明智 我编写了函数来获取用户想要在每个方向 滚动 俯仰 偏航 旋转手臂的量并输出新的四元数 我将 cu
  • 在 Apple M1 上使用 clang 出现“致命错误:找不到‘omp.h’文件”

    Clang 找不到omp h每当我尝试使用 openMP 标志进行编译时 这就是我想做的 clang dynamiclib I opt homebrew Cellar eigen 3 3 9 include eigen3 Xpreproce
  • int p 不是 lpNorm

    中的常量表达式

    我写了这个函数 template
  • Eigen 将旋转和平移组合成一个矩阵

    我有一个旋转矩阵rot Eigen Matrix3d 和平移向量transl Eigen Vector3d 我希望它们一起出现在 4x4 变换矩阵中 我只是为了我的生活不知道如何在 Eigen 中做到这一点 我认为仿射可以以某种方式使用 但
  • 如何在 CUDA 内核中使用 Eigen

    Eigen 是一个 C 线性代数库http eigen tuxfamily org http eigen tuxfamily org 使用基本数据类型 例如基本浮点数组 很容易 只需将其复制到设备内存并将指针传递给 cuda 内核即可 但是
  • 元素矩阵乘法:R 与 Rcpp(如何加速此代码?)

    我是新来的C 编程 使用Rcpp无缝集成到R 并且我希望得到一些有关如何加快某些计算速度的建议 考虑以下示例 testmat lt matrix 1 9 nrow 3 testvec lt 1 3 testmat testvec 1 2 3
  • 在特征中混合标量类型

    include
  • 根据任意分布设置 Eigen::Matrix 的系数

    Eigen Matrix 有一个 setRandom 方法 它将矩阵的所有系数设置为随机值 但是 是否有一种内置方法可以将所有矩阵系数设置为随机值 同时指定要使用的分布 有没有办法实现类似以下内容 Eigen Matrix3f myMatr
  • Eigen::Ref<> 类的正确用法

    Eigen 引入了 Ref 类 以便在不需要编写模板函数时以 Eigen 对象作为参数编写函数 而无需使用不必要的临时变量 人们可以读到这一点here http eigen tuxfamily org dox TopicFunctionTa
  • Eigen::MatrixXd typedef 的替换

    全部更换最简单的方法是什么Eigen MatrixXds and Eigen VectorXd具有向量和矩阵long double元素 我的代码中的每个基本浮点变量都是类型long double 另外 每次使用矩阵或向量时 我都会使用以下类
  • C++ 对齐的未来:按值传递?

    阅读 Eigen 库文档 我注意到有些对象不能按值传递 http eigen tuxfamily org dox TopicPassingByValue html C 11 中是否有任何开发或计划开发可以安全地按值传递此类对象 另外 为什么
  • Eigen static libaligned_free“双重释放或损坏”

    这是一个延续较早的帖子 https stackoverflow com questions 70788173 eigen static lib memory align 但这一次希望有一个更好的例子 设置向量时 这个简单的测试会崩溃 我正在
  • 如何传递特征矩阵行引用以将其视为向量?

    我有一个对向量引用进行操作的函数 例如 void auto bias const Eigen VectorXf v Eigen Ref
  • cygwin_exception::open_stackdumpfile:将堆栈跟踪转储到 *.exe.stackdump

    我收到 cygwin exception open stackdumpfile 将堆栈跟踪转储到 TestProject exe stackdump 错误 我的项目只不过是一个 C HalloWorld 项目 其中包含一个附加类 我在其中设
  • 重用 Eigen::SimplicialLLT 的符号分解

    我在 Eigen 库的 API 上遇到了一些困难 即用于稀疏矩阵 Cholesky 分解的 SimplcialLLT 类 我需要分解三个矩阵 然后用它们来求解许多方程组 仅更改右侧 因此我只想将这些矩阵分解一次 然后重新使用它们 此外 它们
  • Eigen 如何沿特定维度连接矩阵?

    我有两个特征矩阵 我想将它们连接起来 就像在 matlab 中一样cat 0 A B eigen 有等价物吗 Thanks 您可以使用逗号初始值设定项语法 水平方向 MatrixXd C A rows A cols B cols C lt
  • Eigen 和 OpenMP:由于错误共享和线程开销而没有并行化

    系统规格 Intel Xeon E7 v3 处理器 4 插槽 16 核 插槽 2 线程 核心 Eigen 系列和 C 的使用 以下是代码片段的串行实现 Eigen VectorXd get Row const int j const int
  • 如何查看Eigen C++模板库的版本号?

    我添加了几个不同版本的 Eigen 默认值 包括 Visual C 的目录 但是我在使用的时候遇到了崩溃的问题LDLT Cholesky 分解 一些测试数值示例 所以我想在调试代码时确定哪个版本实际上是活动的 是否有任何函数可以指示当前活动

随机推荐

  • 【推荐书籍】C语言深度解剖

    她想要统治 xff0c 同时又要享受 xff1b 她想要王后的权柄 xff0c 还要女人的自由 xff1b 她伸出玉手 xff0c 抓起王冠 xff0c 就像拿起一件意想不到的礼物 她那时还太年轻 xff0c 不知道所有命运赠送的礼物 xf
  • 【荐书】电子设计从零开始

    人生就是不断的放下 xff0c 但最遗憾的是 xff0c 我们来不及好好告别 少年派的奇幻漂流 推荐理由 如果你想成为一名优秀的嵌入式工程师 xff0c 那么硬件的基础知识是一定要有的 xff0c 因为我们在编程的时候要经常跟硬件打交道 x
  • ROS安装教程(ubuntu18.04+melodic版本)

    1 ROS版本选择 ROS是一个用于编写机器人软件的灵活框架 xff0c 它集成了大量的工具 库 协议 xff0c 提供了类似操作系统所提供的功能 xff0c 包括硬件抽象描述 底层驱动程序管理 公用功能的执行 程序间的消息传递 程序发行包
  • 使用realsense D435i实现机械臂对物体的自动抓取总结

    1 开发环境搭建 Intel RealSense D435环境搭建之安装pyrealsense2 ModuleNotFoundError No module named 39 apt pkg 39 on Ubuntu 秃头小宝贝ec的博客
  • ZOJ - 2313 Chinese Girls' Amusement

    You must have heard that the Chinese culture is quite different from that of Europe or Russia So some Chinese habits see
  • ROS通信机制与TCP

    http blog exbot net archives 1605 http wiki ros org ROS TCPROS
  • 花两天时间写的stm32f103串口BootLoader(有keil工程)

    因为在论坛和官网都没搜到完全合适的BootLoader xff0c 所以自己移植完成了一个BootLoader工程 另外附APP文件工程 xff0c 可做实验 用良心保证 xff0c 看完后可以做一个成功的实验 上位机选用SecureCrt
  • 信号量与消息队列的区别

    出现信号量与消息队列的原因 xff1a 全局变量可以承载通信的内容 xff0c 但接受方任务需要不断检测此全局变量的值 所以产生了信号量与消息队列 信号量 xff1a 可以通知接收方某个事件的发生 xff0c 但无法传递具体事件内容 xff
  • <Unity>局部坐标(localPosition) && 世界坐标(Position)

    局部坐标 amp amp 世界坐标 的区别 1 官方文档介绍1 1 Transform Position1 2 Transform localPosition 2 问题2 1 Position真的不会受到父物体的影响吗 1 官方文档介绍 1
  • ADRC与Matlab/Similink/C++实现

    写在前面 ADRC控制算法主要分为三部分 xff0c 跟踪微分器TD 观测器ESO和状态误差反馈控制器 xff0c 其中控制器分为线性控制器PD和非线性状态误差反馈控制器NLSEF xff0c 观测器分为线性观测器LESO和非线性观测器NL
  • 状态空间方程MATLAB语句

    1 连续系统 xff08 1 xff09 使用系数矩阵获得传递函数 num den 61 ss2tf A B C D xff08 2 xff09 将传递函数写成因式分解 xff08 零极点 xff09 形式 z p k 61 ss2zp A
  • 力扣刷题

    提示 xff1a 文章写完后 xff0c 目录可以自动生成 xff0c 如何生成可参考右边的帮助文档 H指数 题目介绍一 实现思路方法一 xff1a 排序分析复杂度分析 方法二 xff1a 计数分析算法 复杂度分析 二 使用算法1 pyth
  • [markdown语法]公式篇--整理总结了常用的公式语法全

    一 公式部分 前言 行内公式 xff1a 公式 行间公式 xff1a 公式 1 1 常用符号标注 1 1 1 上下标 数学符号实际效果语法向量 a vec a
  • ubuntu mate18.04+树莓派4B+ROS安装详细教程

    前记 最近项目需要 xff0c 需要给树莓派4B 安装Ubuntu mate xff0c 本来是一件很简单的事情 xff0c 因为Ubuntu mate官网已经开始支持树莓派4B了 xff0c 但是实际操作后 xff0c 才发现烧录官方的桌
  • QGC参数表

    QGC parameters 1 Battery Callbration ParametersDetail含义BAT A PER VBattery current per volt A V 每伏电池电流BAT CAPACITYBattery
  • GAZEBO构建室外环境地图之创建高度图

    GAZEBO构建室外环境地图之创建高度图 前言下载真实地形数据处理下载好的高度图 前言 最近在搭建gazebo的室外仿真环境用于室外机器人的室外建图算法研究 xff0c 发现网上说这方面的文章非常少 xff0c 走dem来创建地形的路 xf
  • [无人机学习]无人机学习概论

    一 无人机简介 1 1 无人机与航空模型分类 机型优点缺点固定翼飞机续航时间长 xff0c 速度快需要跑道 xff0c 不能垂直起降单旋翼飞机可以垂直起降 xff0c 空中悬停续航时间段 xff0c 机械结构复杂 xff0c 操控难度大多旋
  • YOLO使用

    第一步 下载预训练模型 span class token builtin class name cd span darknet span class token function wget span https pjreddie com m
  • CMakeLists.txt文件编写

    重点 1 cmake版本要求 cmake minimum required VERSION 3 15 2 工程名 project algorithm 3 支持C 43 43 11标准 set CMAKE CXX STANDARD 11 4
  • [Eigen]

    Eigen 库学习笔记 参考原文为 xff1a https www cnblogs com rainbow70626 p 8819119 html 为了方便查看 xff0c 以防找不到 xff0c 所以写了这篇博客 目录 Eigen 矩阵定