Eigen Library Getting Started Tutorial

 The Eigen library is definitely familiar to many people who need to do SLAM, image, automatic driving, etc.

First of all, students who are capable, please read the official documents. Here is the most complete place to find tutorial materials:

Eigen https://eigen.tuxfamily.org/index.php?title=Main_Page The main content of this article also comes from the official website.

The most notable feature of the Eigen library is that it only needs to include header files when using it (it is a library built purely with header files), and there is no need to link the library. Therefore, when using tools such as cmake, you only need to add    find_package(Eigen3 REQUIRED)  without link. Or add it through include_directories(\eigen-3.4.0) (often used in PC).

Ignore the installation process here (very simple), but please pay attention to the Eigen version that the project depends on.

First a short example:

#include <iostream>
#include <eigen3/Eigen/Dense>   //Dense是最常用的

using namespace std;
using namespace Eigen;

int main()
{
    MatrixXd m(2,2);
    MatrixXd n(2,2);//MatrixXd  代表X维度(Dynamic)的矩阵
    m(0,0) = 3;
    m(1,0) = 2.5;
    m(0,1) = -1;
    m(1,1) = m(1,0) + m(0,1);
    n<<1, 1,
            1,1;     //通过操作符重载赋值
    cout << m <<endl;
    cout << n <<endl;
}


//输出为
// 3  -1
//2.5 1.5
//1 1
//1 1

 It means that our Eigen has been used. So the Dense written above is the most commonly used, how do we choose when we include it?

 The explanation is as above, so why is Dense often used? Because a Dense includes Core, Geometry, LU, Cholesky, SVD, QR, and Eigenvalues ​​header files.

Ok, let's continue. Eigen provides two kinds of dense object matrix matrix and vector vector, which are represented by the template class Matrix . For one-dimensional and two-dimensional arrays, they are represented by the template Array array. The syntax is as follows:

typedef Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options> MyMatrixType;
typedef Array<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options> MyArrayType;

 Where Scalar represents the type of constant, for example floatdoubleboolint等等。

RowsAtCompileTime And ColsAtCompileTime  represent the number of rows and columns, or use Dynamic, dynamic

OPtions can choose ColMajor or RowMajor  默认是col列排列(也就是行排列还是列排列,互为转置)

合法的写法可以是:

Matrix<double, 6, Dynamic>  m1                // 列是动态的,调用堆
Matrix<double, Dynamic, 2>  m2                // 行是动态的,调用堆
Matrix<double, Dynamic, Dynamic, RowMajor> m3 // 全动态,调用堆
Matrix<double, 13, 3>  m4                     // 全是确定的(通常保存在栈上)

 Usually, we don't need to bother when using it, the library itself has provided many typedefs:

 We all know that dimensions and operations are particularly important for matrices, so what transformations do matrices and arrays have?

Array44f a1, a1;                  //定义两个数组(二维)和两个矩阵
Matrix4f m1, m2;
m1 = a1 * a2;                     // 共轭乘积,这样两个向量之积为矩阵(4*4)
a1 = m1 * m2;                     // 两个矩阵相乘,将矩阵转换为向量
a2 = a1 + m1.array();             // 矩阵和向量是不能直接运算的,使用.array()转换
m2 = a1.matrix() + m1;            // 或者.Matrix()的显示转换
ArrayWrapper<Matrix4f> m1a(m1);   // m1a是m1的指代

It is worth noting that different types of data types cannot be directly calculated, and cast conversion is required, such as m1.cast<double>() .  

Basic matrix operations:

 It should be noted that MatrixXf::Zero(m, n) and MatrixXf::Identity(m, n), respectively represent a matrix of all 0s and a matrix with a diagonal of 1.

Let’s write this for the time being, and update it later~

Recommended reading: (The blogger sums it up very well!)

Eigen Linear Algebra Library Learning Daquan - Programmer Sought

Guess you like

Origin blog.csdn.net/m0_46611008/article/details/128006586