Eigen学习笔记(5)-块操作

原文:Eigen官网-Block operations

Eigen 为 Matrix 、Array 和 Vector提供了块操作方法。块是matrix或array中的矩形子部分。块区域可以被用作 左值 和 右值。

1. 使用块

在Eigen中最常用的块操作函数是 .block() 。

Block operation Version constructing a dynamic-size block expression Version constructing a fixed-size block expression
Block of size (p,q), starting at (i,j) matrix.block(i,j,p,q); matrix.block<p,q>(i,j);

Eigen中,索引从0开始。

两个版本都可以用于固定尺寸和动态尺寸的matrix/array。功能是等价的,只是固定尺寸的版本在block较小时速度更快一些。

示例如下:

#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也可以被用作左值,即block可以进行赋值操作。
示例如下:

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

结果如下:

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 2x2 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

2. 行和列

单独的一行和一列是特殊的块。Eigen中提供了.col().row()方法实现取整行和取整列操作。

Block operation Method
ith row matrix.row(i);
jth colum matrix.col(j);

示例如下:

#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

3. 角块相关的操作

Eigen提供了一些方法实现对角块和边的提取。

Block operation Version constructing a dynamic-size block expression Version constructing a fixed-size block expression
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();
Block containing the last q rows * matrix.bottomRows(q); matrix.bottomRows();
Block containing the first p columns * matrix.leftCols§; matrix.leftCols

();

Block containing the last q columns * matrix.rightCols(q); matrix.rightCols();

示例如下:

#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

4. Vector相关的块操作

Block operation Version constructing a dynamic-size block expression Version constructing a fixed-size block expression
Block containing the first n elements * vector.head(n); vector.head();
Block containing the last n elements * vector.tail(n); vector.tail();
Block containing n elements, starting at position i * 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

参考:

“Eigen教程(5)”
“Eigen 学习之块操作”

© 版权声明
THE END
喜欢就支持一下吧
点赞794 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容