Eigen study notes 2-Matrix class

  In Eigen, all matrices Matrix and vector Vector are constructed by the Matrix class. A vector is nothing but a special form of a matrix, with only one column (column vector) or one row.

  The Matrix template class has 6 parameters, the first three of which are required. The first three parameters are as follows:

  Matrix<typename Scalar,int RowsAtCompileTime,int ColsAtCompileTime >

  Scalar is a scalar type, and the value can be float, int double, etc.

  RowsAtCompileTime and ColsAtCompileTime are the number of rows and columns of the matrix known at program compilation time.

  Eigen provides some commonly used well-defined types. for example:

  typedef Matrix<float,4,4> Matrix4f .

  

  In Eigen, the column vector is the default vector. Unless otherwise specified, the vector Vector refers to the column vector. Column vectors are defined in Eigen:

  typedef Matrix<float,3,1> Vector3f ;

  Eigen also defines row vectors:

  typedef Matrix<int ,1,2 > RowVector2i ;

 

  If the size of the matrix is ​​indeterminate at compile time and can only be determined at runtime, Eigen provides a way to define a dynamic size. For example, it is very useful:

  typedef Matrix<double ,Dynamic,Dynamic > MatrixXd;  

  MatrixXd defines a matrix with any number of rows and columns, which can be determined at runtime.

  Similarly, for vectors we have:

  typedef Matrix<int ,Dynamic ,1> VectorXi ;

  It is also possible to determine for one dimension and specify that the other dimension is dynamically sized.

  Matrix<float,3,Dynamic> The number of rows of the matrix is ​​3, and the number of columns is undefined.

 

  For the construction of matrices, Eigen provides a default constructor.

  Matrix3f a;

  MatirxXf b;

  a is a 3 x 3 matrix, each element is an uninitialized float.

  b is currently a 0 x 0 matrix.

 

  Constructor with arguments, for matrices, the number of rows comes before the number of columns, and for vectors, only the size of the vector.

  MatrixXf a(10,15);

  VectorXf b(30);

  a is a 10 x 15 dynamically sized matrix, memory allocated but not initialized.

  b is a dynamically sized vector of size 30, allocated but not initialized.

  Fixed-size types are defined for matrices and vectors with dimensions below 4.

  For example you can use,

  Vector2d ; Vector3d ; Vector4d; etc. to define a vector.

  Matrix2f ; Matrix3f ; Matrix4f ; etc. define matrices.

 

  Matrices and vectors can be assigned values ​​using comma initialization. E.g:

  Matrix3f m;

  m << 1,2,3,

     4,5,6,

                 7,8,9;

     In this way, the above value is assigned to the matrix. The default storage method of the matrix in Eigen is row first, that is, the row is stored first.

 

  Eigen supports resizing of dynamically sized matrices and vectors.

  rows() , cols() , size() return the number of rows, columns and elements respectively.

  resize() can resize the matrix.

  Examples are as follows;

  

 1 #include <iostream>
 2 #include <eigen3/Eigen/Dense>
 3 
 4 using namespace Eigen;
 5 
 6 
 7 int main(int argc ,char** argv)
 8 {
 9     MatrixXd m(2,5);
10     m.resize(3,4);
11     std::cout<<"The matrix m is of size "
12              <<m.rows()<<" x "<<m.cols()<<std::endl;
13     std::cout<<"It has"<<m.size()<<" coefficients"<<std::endl;
14 
15     VectorXd v(2;
16     v.resize(5;
17     std::cout<<"The vector v is of size "<<v.size()<<std::endl;
18     std::cout<<"As a matrix, v is of size "
19             <<v.rows()<<" x "<<v.cols()<<std::endl;
20     
21     return 0;
22 }

 operation result:

  

assignment and resize

For dynamically sized matrices, when the operator = is used, the size of the matrix on the left changes according to the size of the matrix on the right.

E.g:

 1 #include <iostream>
 2 #include <eigen3/Eigen/Dense>
 3 
 4 using namespace Eigen;
 5 
 6 
 7 int main(int argc ,char** argv)
 8 {
 9     Matrix2f a(2,2);
10     std::cout<<"a is of size "<<a.rows()<<"x"<<a.cols()<<std::endl;
11     MatrixXf b(4,4);
12     a = b;
13     std::cout<<"a is now of size "<<a.rows()<<"x"<<a.cols()<<std::endl;
14     
15     return 0;
16 }

The results are as follows:

 

Fixed size and dynamic size

  For small-sized matrices, the time overhead is much smaller using the fixed-size approach.

 

Eigen defines quite a few convenient types, including complex types.

 MatrixNt ,VectorNt ,RowVectorNt 。

N  : 2,3,4,X ;

t : i ,f d ,cf cd.

 

  

  

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324674833&siteId=291194637