Table of Contents

## Motivation

Working with matrices and vectors in C++ can be a bit cumbersome. The Eigen library is a C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms. It is used in many scientific and engineering applications. Eigen is versatile, efficient, and easy to use. It supports all matrix sizes, from small fixed-size matrices to arbitrarily large dense matrices, and even sparse matrices.

## Installation

Eigen is a header-only library. You can download it from the official website and include it in your project.

Under Windows, you can use nuget to install Eigen. Just search for Eigen3 in the nuget package manager and install it.

Under Linux, you can install Eigen with the package manager:

```
sudo apt-get install libeigen3-dev
```

## Usage

Here is a simple example of how to use Eigen in your C++ code:

`#include `
#include <Eigen/Dense>
int main()
{
Eigen::MatrixXd m(2, 2);
m(0, 0) = 3;
m(1, 0) = 2.5;
m(0, 1) = -1;
m(1, 1) = 4.0;
std::cout << m << std::endl;
}

The matrix `m`

is a 2×2 matrix.

The d in the `MatrixXd`

stands for double. You can also use `MatrixXf`

for float or `MatrixXi`

for int.

Dynamic sizes are indicated by the `X`

in the type name.

Fixed-size matrices are defined with the number of rows and columns like `Matrix2d`

for a 2×2 matrix.

You can access the elements of the matrix with the `()`

operator.

## Operands

An interesting feature of Eigen is the operator overloading.

You can use the standard arithmetic operators like `+`

, `-`

, `*`

, `/`

with Eigen matrices and vectors.

## Initialization

To initialize a matrix you can also use the `<<`

operator, which provides a more concise way to initialize a matrix:

```
Eigen::MatrixXd m2(2, 2);
m2 << 1, 2,
3, 4;
```

## Transpose

You can transpose a matrix with the `transpose()`

method:

```
Eigen::MatrixXd m3(2, 3);
m3 << 1, 2, 3,
4, 5, 6;
std::cout << "Here is the matrix m:" << std::endl << m3 << std::endl;
std::cout << "Here is the matrix m^T:" << std::endl << m3.transpose() << std::endl;
```

## Determinant

You can calculate the determinant of a matrix with the `determinant()`

method:

```
Eigen::MatrixXd m5(2, 2);
m5 << 1, 2,
3, 4;
std::cout << "Here is the matrix m:" << std::endl << m5 << std::endl;
std::cout << "Here is the determinant of m:" << std::endl << m5.determinant() << std::endl;
```

## Inverse

You can calculate the inverse of a matrix with the `inverse()`

method:

```
Eigen::MatrixXd m4(2, 2);
m4 << 1, 2,
3, 4;
std::cout << "Here is the matrix m:" << std::endl << m4 << std::endl;
std::cout << "Here is the inverse of m:" << std::endl << m4.inverse() << std::endl;
```