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 中矩阵点乘

// 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()

Eigen 中矩阵特征值

// Eigen                          // Matlab
A.eigenvalues();                  // eig(A);特征值
EigenSolver<Matrix3d> eig(A);     // [vec val] = eig(A)
eig.eigenvalues();                // diag(val)与前边的是一样的结果
eig.eigenvectors();               // vec 特征值对应的特征向量

Eigen中产生正交向量

Eigen::Vector3d A;
Eigen::Vector3d u_aixs;
Eigen::Vector3d v_aixs;u_aixs = A.unitOrthogonal();
v_aixs = A.cross(u_aixs);

【Eigen中基本和常用函数】相关推荐

  1. ipad php mysql_PHP中的MYSQL常用函数

    PHP中的MYSQL常用函数 1.mysql_connect()-建立数据库连接 格式: resource mysql_connect([string hostname [:port] [:/path ...

  2. C++中有关queue常用函数的用法及其注意要项

    11:C++中有关queue常用函数的用法及其注意要项 #include<bits/stdc++.h> using namespace std; int main(){queue < ...

  3. C++string类常用函数 c++中的string常用函数用法总结

    string类的构造函数: string(const char *s);    //用c字符串s初始化 string(int n,char c);     //用n个字符c初始化 此外,string类 ...

  4. (numpy)python中Array的常用函数

    python中Array的常用函数 1.unique 2.sum 3.max 1.unique a = np.random.randint(10, size=20).reshape(4,5) a &g ...

  5. C++中string类函数常用函数大全

    最近,写各种关于字符串的问题,遇到不少题目解法或多或少的调用各种方法,今个发现了个大佬的总结,自己码一下保存(自己的编译器慢慢看,看文档,这谁看的进去这么多..) 大佬原文链接在最下面 string类 ...

  6. PP实施经验分享(5)——SAP中MD04显示常用函数(读取SAP MRP运行数据)

    PP实施经验分享(5)--SAP中MD04显示常用函数(读取SAP MRP运行数据) SAP实施过程中,经常会遇到用户对于现有MD04标准功能展示有一定的抱怨,不符合我们查看的习惯,经常会提出相关报表 ...

  7. oracle中110个常用函数

    主要介绍了oracle中110个常用函数,方便大家编写出更强大的sql语句,需要的朋友可以参考下. ASCII 返回与指定的字符对应的十进制数; SQL> select ascii(A) A,a ...

  8. Python中random模块常用函数/方法(2)——random.random(),random.randint()和random.uniform()

    1.random.random():生成一个0到1的随机符点数: 0 <= n < 1.0 语法:random.random() #生成一个0~1之间的随机浮点数 print(" ...

  9. 【Eigen】基本和常用函数

    文章目录 简介 找不到头文件 Eigen 中矩阵的定义 Eigen 中矩阵的使用方法 Eigen 中常用矩阵生成 Eigen 中矩阵分块 Eigen 中矩阵元素交换 Eigen 中矩阵转置 Eigen ...

  10. SQL Server中截取字符串常用函数

    SQL Server 中截取字符串常用的函数: 1.LEFT ( character_expression , integer_expression ) 函数说明:LEFT ( '源字符串' , '要 ...

最新文章

  1. 研究人员利用脑电ErrP信号实时控制机器人
  2. 中兴zxr10路由器重启命令_中兴交换机常用命令
  3. 【GitHub】GitHub 的 Pull Request 和 GitLab 的 Merge Request 有区别吗?
  4. 系统崩溃mysql怎么保存表_第09问:MySQL 莫名崩溃,如何保留现场?
  5. 自定义View时,用到Paint Canvas的一些温故,简单的帧动画(动画一 ,quot;掏粪男孩Gifquot;顺便再提提onWindowFocusChanged)...
  6. 计算机基础高一知识点,计算机基础全部知识点_.doc
  7. eclipse导入html页面乱码,Eclipse导入项目乱码问题(中文乱码)
  8. html可编辑段落,javascript – HTML5内容列表后的可编辑段落
  9. 从Outlook到python都可以使您的工作减少一半
  10. Kali [CobaltStrike]CS神器
  11. 牛客 数据库SQL实战 获取员工其当前的薪水比其manager当前薪水还高的相关信息
  12. 只会Python可造不出iPhone
  13. 全球133种语言自动翻译mishop大米外贸商城系统
  14. word如何设置上标形式_word怎么设置上标表示形式
  15. 《网络安全——网上生活要保护》主题班会
  16. Js实现轮盘抽奖功能,一招帮你解决选择困难症
  17. 【Java定时任务】浅谈CronTrigger的用法和在线Cron表达式生成网址
  18. C语言中switch和case之间的语句是否执行?(答案:不执行)
  19. 浅谈DelayQueue
  20. 大学计算机网络复习题

热门文章

  1. matlab如何求状态方程,matlab状态方程解
  2. 使用大麦网抢票工具的一些心得体会
  3. mac电脑如何设置开机启动项
  4. Beyond Compare 激活解决办法
  5. 如何将两张图片合成一张?
  6. 搜索引擎关键字热度估算查询
  7. The user specified as a definer (‘skip-grants user‘@‘skip-grants host‘) does not exist
  8. 难崩日记——从入门到入土的求生之路(二):文件上传中的路径问题
  9. Linux开机自动启动python脚本程序,或 Jetson nano或Jetson Xavier NX开机自动启动python脚本程序
  10. 【python技巧】RGB值组合三元色(红绿蓝)