xref: /aosp_15_r20/external/eigen/doc/examples/class_Block.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li #include <Eigen/Core>
2*bf2c3715SXin Li #include <iostream>
3*bf2c3715SXin Li using namespace Eigen;
4*bf2c3715SXin Li using namespace std;
5*bf2c3715SXin Li 
6*bf2c3715SXin Li template<typename Derived>
7*bf2c3715SXin Li Eigen::Block<Derived>
topLeftCorner(MatrixBase<Derived> & m,int rows,int cols)8*bf2c3715SXin Li topLeftCorner(MatrixBase<Derived>& m, int rows, int cols)
9*bf2c3715SXin Li {
10*bf2c3715SXin Li   return Eigen::Block<Derived>(m.derived(), 0, 0, rows, cols);
11*bf2c3715SXin Li }
12*bf2c3715SXin Li 
13*bf2c3715SXin Li template<typename Derived>
14*bf2c3715SXin Li const Eigen::Block<const Derived>
topLeftCorner(const MatrixBase<Derived> & m,int rows,int cols)15*bf2c3715SXin Li topLeftCorner(const MatrixBase<Derived>& m, int rows, int cols)
16*bf2c3715SXin Li {
17*bf2c3715SXin Li   return Eigen::Block<const Derived>(m.derived(), 0, 0, rows, cols);
18*bf2c3715SXin Li }
19*bf2c3715SXin Li 
main(int,char **)20*bf2c3715SXin Li int main(int, char**)
21*bf2c3715SXin Li {
22*bf2c3715SXin Li   Matrix4d m = Matrix4d::Identity();
23*bf2c3715SXin Li   cout << topLeftCorner(4*m, 2, 3) << endl; // calls the const version
24*bf2c3715SXin Li   topLeftCorner(m, 2, 3) *= 5;              // calls the non-const version
25*bf2c3715SXin Li   cout << "Now the matrix m is:" << endl << m << endl;
26*bf2c3715SXin Li   return 0;
27*bf2c3715SXin Li }
28