Eigen-Block块操作

块是矩阵或数组的矩形部分。块表达式既可以用作右值,也可以用作左值。与通常的Eigen表达式一样,只要让编译器进行优化,这种抽象的运行时成本为零。优化都是自动的无需我们考虑太多。

一、使用块操作

Eigen中最通用的块操作称为.block()。有两个版本,语法如下:

块操作动态大小的块表达式固定大小块表达式
大小为(p,q)的块,从(i,j)开始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(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()方法可用于任何块操作,但还有其他方法用于特殊情况,提供更专门的API和/或更好的性能。关于性能的话题,最重要的是在编译时向Eigen提供尽可能多的信息。例如,如果您的块是矩阵中的单个完整列,那么使用下面描述的专门的 .col() 函数可以让Eigen知道这一点,这可以为其提供优化机会。

本页的其余部分将描述这些专门的方法。

二、列和行

单独的列和行是块的特殊情况。Eigen提供了方便地解决这些问题的方法:.col()和.row()。

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

col()和row()的参数是要访问的列或行的索引。在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

该示例还演示了块表达式(这里是列)可以像任何其他表达式一样用于算术。

三、Corner-related操作

Eigen还为在矩阵或数组的一个角或边上刷新的块提供了特殊方法。例如,. topleftcorner()可用于引用矩阵左上角的块。

不同的可能性总结如下表:

块操作动态大小的块表达式固定大小块表达式
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>();
Block containing the q columns starting from i *matrix.middleCols(i,q);matrix.middleCols<q>(i);
Block containing the q rows starting from i *matrix.middleRows(i,q);matrix.middleRows<q>(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 > ();
包含最后n个元素的块*vector.tail (n);vector.tail < n > ();
包含n个元素的块,从位置i *开始vector.segment (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;
}

输出

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

太阳风暴

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

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

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

打赏作者

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

抵扣说明:

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

余额充值