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
operation

动态大小版本

指定大小版本

从左上角数包含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中的块运算的更多相关文章

  1. 聊聊 C 语言中的 sizeof 运算

    聊聊 sizeof 运算 在这两次的课上,同学们已经学到了数组了.下面几节课,应该就会学习到指针.这个速度的确是很快的. 对于同学们来说,暂时应该也有些概念理解起来可能会比较的吃力. 先说一个概念叫内 ...

  2. Java中的递归运算

    Java中的递归运算是一种在自己的方法内部调用自己的方法 递归的设计思想是:把一个复杂的问题,分解为若干个等同的子问题,重复执行,直到之问题能够简单到直接求解,这样复杂的问题就得以解决. 递归运算有两 ...

  3. CAD二次开发---导入外部文件中的块并输出预览图形(五)

    思路: 1)首先要定义一个数据库对象来表示包含块的文件,改数据库对象会被加载到内存中,但不会被显示在CAD窗口中. 2)调用Database类的ReadDwgFile函数将外部文件DWG文件读入到新创 ...

  4. 在IE6、IE7中实现块元素的inline-block效果

    在IE6.IE7中实现块元素的inline-block效果有以下两种方法: 1先使用display:inline-block属性触发layout,然后再定义display:inline让块元素呈现内联 ...

  5. PHP中的位运算与位移运算(其它语言通用)

    /* PHP中的位运算与位移运算 ======================= 二进制Binary:0,1 逢二进1,易于电子信号的传输 原码.反码.补码 二进制最高位是符号位:0为正数,1为负数( ...

  6. html中的块元素(Block)和内联元素(Inline)(转)

    我们首先要了解,所有的html元素,都要么是块元素(block).要么是内联元素(inline).下面了解一下块元素.内联元素各自的特点: 块元素(block)的特点: 1.总是在新行上开始:2.高度 ...

  7. html 中的块级元素 内联元素

    上一个礼拜,做crm项目,使用的大部分都是js,nodejs,ajax 的内容,但是今天我想写写关于html中的块级元素和内联元素 的东西. 首先,html 中的块级元素 内联元素 我们可以看到,这两 ...

  8. shell脚本中的数学运算

    shell中的赋值和操作默认都是字符串处理,在此记下shell中进行数学运算的几个特殊方法.以后用到的时候能够来看,呵呵 1.错误方法举例 a) var=1+1 echo $var 输出的结果是1+1 ...

  9. Hadoop(八)Java程序访问HDFS集群中数据块与查看文件系统

    前言 我们知道HDFS集群中,所有的文件都是存放在DN的数据块中的.那我们该怎么去查看数据块的相关属性的呢?这就是我今天分享的内容了 一.HDFS中数据块概述 1.1.HDFS集群中数据块存放位置 我 ...

随机推荐

  1. makefile或shell中的一些变量

    总是记不住,作个笔记 $@ 所有目标文件 $< 第一个依赖文件的名称 $? 所有的依赖文件,以空格分开,这些依赖文件的修改日期比目标的创建日期晚 $^ 所有的依赖文件,以空格分开,不包含重复的依 ...

  2. 在KVM里装个pfSense

    第一步:安装配置 virsh destroy router-wan1- virsh undefine router-wan1- qemu-img create -f qcow2 -o size=8G ...

  3. PUSU 拆分后发货和开票的时间节点问题

    项目做到现在业务突然说流程要变,心中顿时无数个草草草掠过.这公司业务也真是够奇葩了,一天一个样.原来流程是由PU把产品生产完后就发给SU,由SU再来决定什么时候对客户和开票.而现在马上要上线了,突然冒 ...

  4. Keras处理已保存模型中的自定义层(或其他自定义对象)

    如果要加载的模型包含自定义层或其他自定义类或函数,则可以通过 custom_objects 参数将它们传递给加载机制: from keras.models import load_model # 假设 ...

  5. Vue2.0学习笔记

    环境搭建 vue-cli@3    vue-cli@2.X npm i -g @vue/cli 模板语法 文本 <span>Message: {{ msg }}</span> ...

  6. 保存数据到Excel中

    调用的方法传值 Export(dt, "Cal_Report_" + DateTime.Now.ToString("yyyyMMddhhmmss") + &qu ...

  7. BZOJ 4872 luogu P3750 [六省联考2017]分手是祝愿

    4872: [Shoi2017]分手是祝愿 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description ...

  8. gentoo Cataclysm - Dark Days Ahead

    gentoo 中安装 Cataclysm - Dark Days Ahead,使用web 下载稳定版的安装包,使用 tar 进行解压. 安装需要共享库:sdl2-mixer, 未完待续

  9. spark java.lang.OutOfMemoryError: unable to create new native thread

    最近迁移集群,在hadoop-2.8.4 的yarn上跑 spark 程序 报了以下错误 java.lang.OutOfMemoryError: unable to create new native ...

  10. 1. redis安装(windows)

    Redis在windows下安装过程 转载自(http://www.cnblogs.com/M-LittleBird/p/5902850.html)   一.下载windows版本的Redis 去官网 ...