1.4eigen中的块运算
1.4 块运算
块是矩阵或数组的一个矩形部分。块表达式既可以做左值也可以作右值。和矩阵表达式一样,块分解具有零运行时间成本,对你的程序进行优化。
1.使用块运算
最常用的块运算是.block()成员函数。以下是两个版本的块定义:
|
块运算 |
动态大小的块定义版本 |
指定大小的块定义版本 |
|---|---|---|
|
定义从第i行第j列开始的大小为PxQ的块 |
matrix.block(i,j,p,q); |
matrix.block<p,q>(i,j); |
在eigen中行数和列数都是从0开始的!
这两个版本可以用于指定大小和动态大小的矩阵和数组类。这两种表达语法上是一样的,唯一不同的是对于小尺寸块而言,指定大小的版本能显著地提供代码运算能力,但是需要在编译时知道其大小。
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main()
{
Eigen::MatrixXf m(,);
m << , , , ,
, , , ,
,,,,
,,,;
cout << "Block in the middle" << endl;
cout << m.block<,>(,) << endl << endl;
for (int i = ; i <= ; ++i)
{
cout << "Block of size " << i << "x" << i << endl;
cout << m.block(,,i,i) << endl << endl;
}
}
//output
Block in the middle Block of size 1x1 Block of size 2x2 Block of size 3x3
在上面的例子中,块函数被看作了右值,即它仅读数据。但是块也可以作为左值,也就是说你可以对它进行赋值:
#include <Eigen/Dense>
#include <iostream>
using namespace std;
using namespace Eigen;
int main()
{
Array22f m;
m << ,,
,;
Array44f a = Array44f::Constant(0.6);
cout << "Here is the array a:" << endl << a << endl << endl;
a.block<,>(,) = m;
cout << "Here is now a with m copied into its central 2x2 block:" << endl << a << endl << endl;
a.block(,,,) = a.block(,,,);
cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:" << endl << a << endl << endl;
}
//output
Here is the array a:
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
Here is now a with m copied into its central 2x2 block:
0.6 0.6 0.6 0.6
0.6 0.6
0.6 0.6
0.6 0.6 0.6 0.6
Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:
0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6
0.6 0.6 0.6 0.6
2.列和行
单列和单行是块的特例。eigen提供更简单的方法获得单行和单列:分别是.row()和.col()
以下为操作语法:
|
Block operation |
Method |
|---|---|
|
第i行 |
matrix.row(i); |
|
第j列 |
matrix.col(j); |
这里的行列数与eigen矩阵内部的行列数相同,都是从0开始的。
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main()
{
Eigen::MatrixXf m(,);
m << ,,,
,,,
,,;
cout << "Here is the matrix m:" << endl << m << endl;
cout << "2nd Row: " << m.row() << endl;
m.col() += * m.col();
cout << "After adding 3 times the first column into the third column, the matrix m is:\n";
cout << m << endl;
}
//output
Here is the matrix m: 2nd Row:
After adding times the first column into the third column, the matrix m is:
3.对角相关运算
Eigen还提供了针对矩阵或数组的某个角或边齐平的块的特殊方法。比如,成员函数.topLeftCorner()表示块的左上角的矩阵。
以下是不同方向的对角阵的表示方法:
|
Block |
动态大小版本 |
指定大小版本 |
|---|---|---|
|
从左上角数包含p行q列的块 |
matrix.topLeftCorner(p,q); |
matrix.topLeftCorner<p,q>(); |
|
从左下角数包含p行q列的块 |
matrix.bottomLeftCorner(p,q); |
matrix.bottomLeftCorner<p,q>(); |
|
从右上角数包含p行q列的块 |
matrix.topRightCorner(p,q); |
matrix.topRightCorner<p,q>(); |
|
从右下角数包含p行q列的块 |
matrix.bottomRightCorner(p,q); |
matrix.bottomRightCorner<p,q>(); |
|
包含前q行的块 |
matrix.topRows(q); |
matrix.topRows<q>(); |
|
包含后q行的块 |
matrix.bottomRows(q); |
matrix.bottomRows<q>(); |
|
包含前p列的块 |
matrix.leftCols(p); |
matrix.leftCols<p>(); |
|
包含后p列的块 |
matrix.rightCols(q); |
matrix.rightCols<q>(); |
以下为实例:
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main()
{ Eigen::Matrix4f m;
m << , , , ,
, , , ,
, ,,,
,,,;
cout << "m.leftCols(2) =" << endl << m.leftCols() << endl << endl;//取左边两列
cout << "m.bottomRows<2>() =" << endl << m.bottomRows<>() << endl << endl;//取底部两行
m.topLeftCorner(,) = m.bottomRightCorner(,).transpose();
cout << "After assignment, m = " << endl << m << endl;
}
//output
m.leftCols() = m.bottomRows<>() = After assignment, m =
4.向量的块运算
eigen提供给向量和一维数组一系列的特定块操作:
|
块运算 |
动态大小版本 |
指定大小版本 |
|---|---|---|
|
包含前n个元素的块 |
vector.head(n); |
vector.head<n>(); |
|
包含后n个元素的块 |
vector.tail(n); |
vector.tail<n>(); |
|
包含从第i个元素开始后n个元素的块 |
vector.segment(i,n); |
vector.segment<n>(i); |
以下是使用实例:
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main()
{
Eigen::ArrayXf v();
v << , , , , , ;
cout<< "v.head(3) =" << endl << v.head() << endl << endl;
cout << "v.tail<3>() = " << endl << v.tail<>() << endl << endl;
v.segment(,) *= ;
cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl;
}
//output
v.head() = v.tail<>() = after 'v.segment(1,4) *= 2', v =
1.4eigen中的块运算的更多相关文章
- 聊聊 C 语言中的 sizeof 运算
聊聊 sizeof 运算 在这两次的课上,同学们已经学到了数组了.下面几节课,应该就会学习到指针.这个速度的确是很快的. 对于同学们来说,暂时应该也有些概念理解起来可能会比较的吃力. 先说一个概念叫内 ...
- Java中的递归运算
Java中的递归运算是一种在自己的方法内部调用自己的方法 递归的设计思想是:把一个复杂的问题,分解为若干个等同的子问题,重复执行,直到之问题能够简单到直接求解,这样复杂的问题就得以解决. 递归运算有两 ...
- CAD二次开发---导入外部文件中的块并输出预览图形(五)
思路: 1)首先要定义一个数据库对象来表示包含块的文件,改数据库对象会被加载到内存中,但不会被显示在CAD窗口中. 2)调用Database类的ReadDwgFile函数将外部文件DWG文件读入到新创 ...
- 在IE6、IE7中实现块元素的inline-block效果
在IE6.IE7中实现块元素的inline-block效果有以下两种方法: 1先使用display:inline-block属性触发layout,然后再定义display:inline让块元素呈现内联 ...
- PHP中的位运算与位移运算(其它语言通用)
/* PHP中的位运算与位移运算 ======================= 二进制Binary:0,1 逢二进1,易于电子信号的传输 原码.反码.补码 二进制最高位是符号位:0为正数,1为负数( ...
- html中的块元素(Block)和内联元素(Inline)(转)
我们首先要了解,所有的html元素,都要么是块元素(block).要么是内联元素(inline).下面了解一下块元素.内联元素各自的特点: 块元素(block)的特点: 1.总是在新行上开始:2.高度 ...
- html 中的块级元素 内联元素
上一个礼拜,做crm项目,使用的大部分都是js,nodejs,ajax 的内容,但是今天我想写写关于html中的块级元素和内联元素 的东西. 首先,html 中的块级元素 内联元素 我们可以看到,这两 ...
- shell脚本中的数学运算
shell中的赋值和操作默认都是字符串处理,在此记下shell中进行数学运算的几个特殊方法.以后用到的时候能够来看,呵呵 1.错误方法举例 a) var=1+1 echo $var 输出的结果是1+1 ...
- Hadoop(八)Java程序访问HDFS集群中数据块与查看文件系统
前言 我们知道HDFS集群中,所有的文件都是存放在DN的数据块中的.那我们该怎么去查看数据块的相关属性的呢?这就是我今天分享的内容了 一.HDFS中数据块概述 1.1.HDFS集群中数据块存放位置 我 ...
随机推荐
- windows配置远程桌面连接到ubuntu
最近在用nodejs开发项目,同时也在做一些区块链相关的工作,公司给配的办公电脑着实不错,都是自家品牌的工作站,市场价都是15K+了.但是在win10上装虚拟机,还是不太顺畅的.因为公司电脑是五年到期 ...
- [转][C#]压缩解压
{ internal static class Compressor { public static Stream Decompress(Stream source, bool bidiStream) ...
- aspose.cells 插入图片
,,"d:\\1.jpg"); Aspose.Cells.Drawing.Picture pic = worksheet.Pictures[iIndex]; pic.Placeme ...
- SAS LOGISTIC 逻辑回归中加(EVENT='1')和不加(EVENT='1')区别
区别在于:最大似然估计分析中估计是刚好正负对调加上EVENT:%LET DVVAR = Y;%LET LOGIT_IN = S.T3;%LET LOGIT_MODEL = S.Model_Params ...
- 安装系统,用cmd进行分区
步骤1:在“您想将Windows安装在何处”的界面按住“Shift+F10”,调出命令行窗口,输入diskpart并点击回车 步骤2: 输入list disk点击回车,列出所有磁盘磁盘是从0开始排序的 ...
- 使用IntelliJ IDEA创建简单的Dubbo实例
这个博客是在https://blog.csdn.net/Crazer_cy/article/details/80397649篇文章上的基础上,自己学习用的. Zookeeper为dubbo的注册中心, ...
- 子页面调整父亲页面的iframe元素
$('iframe', parent.document).attr('scrolling','no');
- .NET MVC 过滤器使用
来看以下两种情况 1. 如果我们需要对某个模块做权限控制,通常的做法是写一个基类(BaseController),让这个基类继承Controller类,在BaseController的构造方法中进行权 ...
- 关于six.with_metaclass(ABCMeta, object)的理解
在学习Python过程中,看到了生成虚基类的方式, class PeopleBase(six.with_metaclass(ABCMeta, object)): @abstractmethod def ...
- setTimeout应用例子-移入移出div显示和隐藏
效果:移入div1,div2保持显示,移出div1,div2消失. 移入div2,div2保持显示,移出div2,div2消失. 一.HTML代码 <div id='div1'></ ...