# Eigen Linear Algebra in C++

Eigen YouTube Tutorial

To install download the .zip file from the website. Extract to a folder (C:/Eigen).

To include Eigen in a Cmake project add:

find_package (Eigen3 3.3 NO_MODULE)

if (TARGET Eigen3::Eigen)
  # Use the imported target
endif (TARGET Eigen3::Eigen)
1
2
3
4
5

TIP

If don't work just include

#include "C:/eigen/Eigen/Dense"
#include "C:/eigen/Eigen/Sparse"
1
2

First matrix:

int main()
{
    Eigen::MatrixXd m;
}
1
2
3
4

Capital X means the size of the matrix in unknown and d is the type, double; or we could have f for float.

This is what we call a dynamic matrix, because the size is unknown, and the size is not set, you can reset during runtime of the program.

TIP

using namespace Eigen;

On the other hand we have the Fixed size matrix


Eigen has a defined 2x2 and 4x4 matrix, so just put a 2 or a 4. After that comes the letter for the type.

Matrix4d
1

TIP

Dynamic matrices are stored on the heap and fixed size on the stack. To access the stack memory is faster

To find the size of the matrix use f.size for a matrix called f.

Resizing a Matrix:

d.resize(4, 6);
1

Initializing a matrix

// Initializing the matrix

f << 1, 2, 3,
     4, 5, 6,
     7, 8, 9;
1
2
3
4
5

Initializing a matrix randomly

f = Matrix3d::Random();
1

To give a clean format to the matrix

    IOFormat CleanFmt(4, 0, ", ", "\n", "[", "]");
    std::cout <<  f.format(CleanFmt) << std::endl;
1
2