[Eigen中文文档] 块操作

专栏总目录

英文原文(Block operations)

本文介绍了块操作。块是matrixarray的部分矩形元素。块表达式既可以用作右值也可以用作左值。与Eigen表达式一样,如果让编译器进行优化,则块操作的运行时间成本为零。

使用块操作

在Eigen中最常见的块操作是.block() ,这有两个版本,语法如下:

块操作构建一个动态大小的块表达式构建一个固定大小的块表达式
大小为 (p,q), 起始于 (i,j)的块matrix.block(i,j,p,q);matrix.block<p,q>(i,j);

Eigen的索引是以0开始的。

这两个版本都可以用在固定大小和动态大小的matricesarray上。两种表达式在语义上是一致的,唯一的区别是,固定大小的版本会在块比较小的时候快一点,但要求块大小在编译的时候就知道。

以下程序使用动态大小和固定大小版本打印matrix中的几个块:

#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;
  }
}

输出如下:

Block in the middle
 6  7
10 11

Block of size 1x1
1

Block of size 2x2
1 2
5 6

Block of size 3x3
 1  2  3
 5  6  7
 9 10 11

在上述的例子中.block()函数被用作右值,即它只被读取。但块也可以用作左值,这意味着可以给块赋值。

以下示例中进行了说明。此示例还演示了数组中的块,其工作方式与上面演示的矩阵中的块完全相同。

#include <Eigen/Dense>
#include <iostream>

int main()
{
 Eigen::Array22f m;
 m << 1,2,
      3,4;
 Eigen::Array44f a = Eigen::Array44f::Constant(0.6);
 std::cout << "Here is the array a:\n" << a << "\n\n";
 a.block<2,2>(1,1) = m;
 std::cout << "Here is now a with m copied into its central 2x2 block:\n" << a << "\n\n";
 a.block(0,0,2,3) = a.block(2,1,2,3);
 std::cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:\n" << a << "\n\n";
}

输出如下:

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   1   2 0.6
0.6   3   4 0.6
0.6 0.6 0.6 0.6

Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:
  3   4 0.6 0.6
0.6 0.6 0.6 0.6
0.6   3   4 0.6
0.6 0.6 0.6 0.6

虽然 .block() 方法可用于任何块操作,但还有其他方法用于特殊情况,可以提供更好的性能。在性能表现上,最重要的是能在编译时给Eigen尽可能多的信息。例如,所使用的块是矩阵的一整列,那么使用.col()函数可以让Eigen知道这只是一列,从而给更多的优化机会。

以下部分描述了这些特殊方法。

列和行

单独的列和行是特殊的块,Eigen提供了更便捷的方法.col().row()处理这些块。

块操作方法
第 i 行matrix.row(i);
第 i 列matrix.col(j);

row()col()的参数是需要访问的行和列的索引,在Eigen中是从0开始的。

示例如下:

#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;
}

输出如下:

Here is the matrix m:
1 2 3
4 5 6
7 8 9
2nd Row: 4 5 6
After adding 3 times the first column into the third column, the matrix m is:
 1  2  6
 4  5 18
 7  8 30

如该示例所示块表达式可以像任何其他表达式一样用于算术。

关于角的操作

Eigen还对位于矩阵或数组的角或边的块提供了特殊方法,例如:.topLeftCorner() 可以用于引用一个矩阵左上角的块。

下表列出了各种各样的可能:

块操作构建一个动态大小的块表达式构建一个固定大小的块表达式
左上角的一个大小为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 行的块matrix.bottomRows(q);matrix.bottomRows();
包含前 p 列的块matrix.leftCols§;matrix.leftCols

();

包含后 p 列的块matrix.rightCols(q);matrix.rightCols();
包含从第 i 列开始的 q 列的块matrix.middleCols(i,q);matrix.middleCols(i);
包含从第 i 行开始的 q 行的块matrix.middleRows(i,q);matrix.middleRows(i);

下面是一个简单的例子,描述了上面介绍的操作的使用:

#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;
}

输出如下:

m.leftCols(2) =
 1  2
 5  6
 9 10
13 14

m.bottomRows<2>() =
 9 10 11 12
13 14 15 16

After assignment, m = 
 8 12 16  4
 5  6  7  8
 9 10 11 12
13 14 15 16

向量的块操作

Eigen提供了一组专门为向量和一维数组的特殊情况设计的块操作:

块操作构建一个动态大小的块表达式构建一个固定大小的块表达式
包含前n个元素的块vector.head(n);vector.head();
包含后n个元素的块vector.tail(n);vector.tail();
包含从第 i 个元素开始的 n 个元素的块vector.segment(i,n);vector.segment(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;
}

输出如下:

v.head(3) =
1
2
3

v.tail<3>() = 
4
5
6

after 'v.segment(1,4) *= 2', v =
 1
 4
 6
 8
10
 6
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万俟淋曦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值