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集群中数据块存放位置 我 ...
随机推荐
- JVM优化系列之一(-Xss调整Stack Space的大小)
Java程序中,每个线程都有自己的Stack Space(堆栈).这个Stack Space不是来自Heap的分配.所以Stack Space的大小不会受到-Xmx和-Xms的影响,这2个JVM参数仅 ...
- 获得驱动器信息卷设备&&Ring3得到磁盘文件系统(NTFS WIN10)
// GetLogicalDriveStrings.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows ...
- django模型系统(三)
1,自定义主键字段的创建 AutoFiled(pirmary_key=True) # 一般不会自定义 2,order_by asc desc 表关系 OneToOne student = mod ...
- Activiti流程设计工具
在Actitivi工程的src/main/resources新建一个文件夹diagrams 然后右键,创建一个activiti Diagram 取名为helloWorld后finish 中间区域,是我 ...
- sweetalert弹窗的使用
之前接触到layer弹出层,今天又发现了一个非常实用的弹出层插件,它的名字叫做sweetalert. 官网地址:http://t4t5.github.io/sweetalert/ npm下载方式:np ...
- attr 和 prop 的区别
在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...
- profile default1
DEVPISAP01:/sapmnt/ISD/profile # more ISD_J20_SHADEVEAIAP01 SAPSYSTEMNAME = ISD SAPSYSTEM = 20 INSTA ...
- centos下安装djangobb
曾经在freenas虚拟环境下安装过djangobb,因为要安装的依赖文件太多,最后没有安装成功. 今晚在centos6.9 下,先创建了虚拟环境,然后照着官方网站的快速安装指南,安装后也运行不了,后 ...
- CSS: Grid homework redact.
The web homework: Finished design: (I use six block with different color to show this homework and I ...
- CentOS下安装Docker-CE
1.安装最新版本的话可以使用阿里云的自动安装脚本: curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun 2.安装指定的 ...