2 块操作的一般使用方法

``#include <iostream>#include "Eigen/Dense"using namespace std;using namespace Eigen;int main(){    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  710 11Block of size 1x11Block of size 2x21 25 6Block of size 3x3 1  2  3 5  6  7 9 10 11``

``#include <iostream>#include "Eigen/Dense"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.60.6 0.6 0.6 0.60.6 0.6 0.6 0.60.6 0.6 0.6 0.6Here is now a with m copied into its central 2x2 block:0.6 0.6 0.6 0.60.6   1   2 0.60.6   3   4 0.60.6 0.6 0.6 0.6Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:  3   4 0.6 0.60.6 0.6 0.6 0.60.6   3   4 0.60.6 0.6 0.6 0.6``

`.block()`方法是一种非常通用的块操作方法。除了这个通用的方法以外，Eigen中还为一些常用的特殊操作提供了特殊的函数。从运行速度的方面来看，你应该在编译阶段尽可能的提供更多的信息。比如，如果你需要操作的块是一个列，那么你可以使用`.col()`函数。这样Eigen可以得知这个信息以便进行更多的优化。

3 行子式和列子式

``#include <iostream>#include "Eigen/Dense"using namespace std;using namespace Eigen;int main(){    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 third column,the matrix m is:\n";    cout<<m<<endl;}``

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

4 边角子矩阵

Eigen提供了从边角开始提取子矩阵的方法，比如`.topLeftCorner()`表示从左上角开始提取子矩阵。这些操作总结如下：

``#include <iostream>#include "Eigen/Dense"using namespace std;using namespace Eigen;int main(){    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 1013 14m.bottomRows<2>()= 9 10 11 1213 14 15 16After assignment,m= 8 12 16  4 5  6  7  8 9 10 11 1213 14 15 16``

5 向量的子向量操作

Eigen中同样也为向量提供了一些子式的操作方法，总结如下：

``#include <iostream>#include "Eigen/Dense"using namespace std;using namespace Eigen;int main(){    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)=123v.tail<3>()=456after 'v.segment(1,4) *= 2',v= 1 4 6 810 6``