Note: This tutorial assumes that you have completed the previous tutorials: Using Eigen.

# Eigen Extensions

Description: Some extensions the ecl has made in and around eigen.

Keywords: ecl eigen

Tutorial Level: INTERMEDIATE

## Eigen Plugin

The eigen matrixbase class provides a way to extend its api via a macro-based plugin. The trick is enabled by the following code in eigen's MatrixBase class:

```   1 class MatrixBase {
2     // ...
3     #ifdef EIGEN_MATRIXBASE_PLUGIN
4       #include EIGEN_MATRIXBASE_PLUGIN
5     #endif
6 };
```

• conservativeResize(int rows, int cols) : matrix resizing, carrying over existing elements
• conservativeResize(int rows) : vector resizing, carrying over existing elements

This will be brought in if you do one of the following before you include any eigen headers:

```   1 #define EIGEN_MATRIXBASE_PLUGIN <ecl/linear_algebra/eigen_plugin.hpp>
2 // OR the ecl bundle
3 #include <ecl/linear_algebra.hpp>
4 // OR any one of the ecl-eigen3 modules
5 #include <ecl/linear_algebra/core.hpp>
6
```

Note that if you wish to customise the eigen plugin differently, simply define the macro, pointing it at your own customisation before including ecl/linear_algebra.hpp. The ecl plugin makes a check and won't override any existing definitions.

## Formatters

Formatters have been included in the plugin for float and double type matrices (may expand to integral types later if we need them). This is achieved by use of the ecl_formatters framework.

There are several ways you can call these formatters, the most convenient methods are outlined below:

```   1 #include <linear_algebra.hpp>
2 Matrix2d m; m << 1.0, 2.0, 3.0, 4.0;
3 Matrix2d::Formatter formatter(3,6); // prec 3, width 6
4 formatter.precision(2); formatter.width(5);
5 std::cout << formatter(m) << std::cout;     // format using the stored settings
6 std::cout << formatter(m,3,6) << std::cout; // format using one-shot settings
7
```

```   1 #include <ecl/formatters.hpp>
2 #include <ecl/linear_algebra.hpp>
3 Matrix2d m; m << 1.0, 2.0, 3.0, 4.0;
4 Format<Matrix2d> format(3,6); // prec 3, width 6
5 format.precision(2); format.width(5);
6 std::cout << format(m) << std::cout;     // format using the stored settings
7 std::cout << format(m,3,6) << std::cout; // format using one-shot settings
8
```

2020-01-25 12:36