Eigen库学习(五)块运算
学习 运算 Eigen
2023-09-27 14:27:31 时间
eigen中的矩阵块(block)既可以作为左值也可以作为右值,就和普通表达式一样,经过编译器优化能够达到运行时达到0抽象成本。
一、使用块运算
eigen中最常使用的块操作是block()方法,该方法有两种形式:
说明 | 动态块形式 | 固定块形式 |
---|---|---|
从(i,j)开始的大小为(p,q)的块 | matrix.block(i,j,p,q) | matrix.block<p,q>(i,j) |
matrix和array都有该方法。
注:如果总的大小<16,推荐使用fix-size以提高效率。
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main()
{
Eigen::MatrixXf m(4,4);
m << 1, 2, 3, 4,
5, 6, 7, 8,
9,10,11,12,
13,14,15,16;
cout << "Block in the middle" << endl;
cout << m.block<2,2>(1,1) << endl << endl;
for (int i = 1; i <= 3; ++i)
{
cout << "Block of size " << i << "x" << i << endl;
cout << m.block(0,0,i,i) << endl << endl;
}
}
eigen库在运行时知道的信息越多,就越有可能进行优化。
二、行列块运算
主要就是两个:row和col方法。
块运算 | 方法 |
---|---|
第i行 | matrix.row(i) |
第j行 | matrix.col(j) |
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main()
{
Eigen::MatrixXf m(3,3);
m << 1,2,3,
4,5,6,
7,8,9;
cout << "Here is the matrix m:" << endl << m << endl;
cout << "2nd Row: " << m.row(1) << endl;
m.col(2) += 3 * m.col(0);
cout << "After adding 3 times the first column into the third column, the matrix m is:\n";
cout << m << endl;
}
三、边角块元素
前面说到的选取要么是一行,要么是一列。我们可以根据方位来进行选取,如左上角数
p
×
q
p\times q
p×q矩阵matrix.toLeftCorner(p,q)
或者另外一种形式matrix.toLeftCorner<p,q>()
;再如左边起前
p
p
p列:matrix.leftCols(p)
,也可以写成固定块运算matrix.bo
块运算 | 动态块运算 | 固定块运算 |
---|---|---|
Top-left p by q block * | matrix.topLeftCorner(p,q) | matrix.topLeftCorner<p,q>() |
Bottom-left p by q block * | matrix.bottomLeftCorner(p,q) | matrix.bottomLeftCorner<p,q>() |
Top-right p by q block * | matrix.topRightCorner(p,q) | matrix.topRightCorner<p,q>() |
Bottom-right p by q block * | matrix.bottomRightCorner(p,q) | matrix.bottomRightCorner<p,q>() |
Block containing the first q rows * | matrix.topRows(q) | matrix.topRows<q>() |
Block containing the last q rows * | matrix.bottomRows(q) | matrix.bottomRows<q>() |
Block containing the first p columns * | matrix.leftCols (p) | matrix.leftCols<p>() |
Block containing the last q columns * | matrix.rightCols(q) | matrix.rightCols<q>() |
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main()
{
Eigen::Matrix4f m;
m << 1, 2, 3, 4,
5, 6, 7, 8,
9, 10,11,12,
13,14,15,16;
cout << "m.leftCols(2) =" << endl << m.leftCols(2) << endl << endl;
cout << "m.bottomRows<2>() =" << endl << m.bottomRows<2>() << endl << endl;
m.topLeftCorner(1,3) = m.bottomRightCorner(3,1).transpose();
cout << "After assignment, m = " << endl << m << endl;
}
四、vector的块运算
块运算 | 动态块运算 | 固定块运算 |
---|---|---|
Block containing the first n elements * | vector.head(n) | vector.head<n>() |
Block containing the last n elements * | vector.tail(n) | vector.tail<n>() |
Block containing n elements, starting at position i * | vector.segment(i,n) | vector.segment<n>(i) |
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main()
{
Eigen::ArrayXf v(6);
v << 1, 2, 3, 4, 5, 6;
cout << "v.head(3) =" << endl << v.head(3) << endl << endl;
cout << "v.tail<3>() = " << endl << v.tail<3>() << endl << endl;
v.segment(1,4) *= 2;
cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl;
}
相关文章
- Intel汇编语言程序设计学习-第四章 数据传送、寻址和算术运算-下
- Intel汇编语言程序设计学习-第四章 数据传送、寻址和算术运算-下
- Intel汇编语言程序设计学习-第四章 数据传送、寻址和算术运算-上
- <<Python基础教程>>学习笔记 | 第11章 | 文件和素材
- 【学习笔记40】DOM的节点操作
- COCOS2DX学习之Box2d物理引擎使用之------动态物体的创建
- 《深度学习导论及案例分析》一第2章预 备 知 识2.1矩阵运算
- java语言基础学习从零开始(第五节---java字符串操作与位运算)
- Caffe学习系列(13):数据可视化环境(python接口)配置
- (数据科学学习手札130)利用geopandas快捷绘制在线地图
- 大数据学习——上传本地文件到集群根目录下
- 大数据学习——linux系统的网卡配置步骤
- web前端关于 requestAnimationFrame() 学习笔记
- Deep Learning(深度学习)学习笔记整理系列
- java学习--高效的除模取余运算(n-1)&hash
- HMS Core Insights第四期直播回顾 – 机器学习,推开AI应用的门
- 什么是系统,什么是算法 -- lemon OA 系统学习总结
- 【AI学习笔记】TensorFlow 版本对应(参考表)和 Torch/Torchvision版本对应(参考表)
- Linux下汇编语言学习笔记26 ---
- latex学习笔记----基本知识、文档排版