Introduction
Do you know why Eigen is gaining popularity nowadays?
The reasons are many, but we will learn a few prominent ones. Being open-source, it is accessible to all. It requires codes in C++, one of the most used programming languages. It works very fast and finds applications in many tasks. It transforms tedious calculations into feasible solutions. You can use Eigen’s library in Linear Algebra, Matrix Operations, Numericals, Algorithms, etc.

This article will discuss two of Eigen's most necessary foundational concepts. That is Map Classes and Aliasing and several modules of it. Every module has been explained using detailed examples to help you understand better.
Map Classes
Sometimes there arises a situation where you want to use raw arrays as an Eigenvector or matrix. You are given two choices- make a copy of the data or import the data. Which one would you choose? Obviously, the first one because you can reuse the memory that way. This can be extremely useful in numerous contexts. And fortunately, importing becomes very convenient when you use Map Classes. The term ‘Map’ suggests that you pick a certain part of data and paste it somewhere else.

Map Types and Declaration
Every Map Object has a type defined by its Eigen equivalent. The default type requires only one template parameter whose syntax is:
Map<Matrix<typename Scalar, int Rows, int Cols> >
You need two pieces of information to construct any Map variable.
- The data you want to map in the form of an array, vector, or matrix.
- A pointer to the memory region where you want to map the copied data.
For example, if you want to map a matrix of datatype ‘int’ with sizes defined at the compile time, the syntax will be:
Map<MatrixXi> mint(pi,rows,columns);
where pi is an int* pointing to the region where you want to map the copied data.
If you want to map a matrix of datatype ‘float’ with a pre-defined size, the syntax will be:
Map<const Vector4f> mf(pf);
where, pf is a float* pointing to the region where you want to map the copied data. In this case, you do not have to pass the size as a parameter in the constructor. This is because the size is already defined in the syntax.
Ensure that you pass a pointer to initialize any Map Variable or Object. This is because Map Class does not have a default constructor.
The Map class is flexible enough to form various data representations. The two template additional parameters provided with Map Classes are:
- MapOptions
This parameter specifies if the pointer is Aligned or Unaligned. The default is set to Unaligned.
- StrideType
This parameter is used when you want your output memory array in a customized layout. You have a complete set of Stride methods you can use as per need.
An example demonstrating both these parameters is given below.
int array[8];
for (int i = 0; i < 5; ++i) {
arr[i] = i;
}
cout << "Column Major:\n" << Map < Matrix < int, 2, 3 > > (array) << endl;
cout << "Row Major using stride:\n" << Map < Matrix < int, 2, 3 > , Unaligned, Stride < 1, 3 > > (array) << endl;
Output:
Column Major
0 2 4
1 3 5
Row Major using stride:
0 1 2
3 4 5
Using Map Variables
Map Variables or Objects are used like any other Eigen Variable or Object. All in-built Eigen functions are designed to allow Map Variables or Objects like other Eigen Variables or Objects.
Use the example below for a better understanding.
typedef Matrix < fint, 1, Dynamic > MatrixType;
typedef Map < MatrixType > MapType;
typedef Map <
const MatrixType > MapTypeConst;
const int n = 4;
MatrixType mone(n), mtwo(n);
mone.setRandom();
mtwo.setRandom();
int * p = & mtwo(0);
MapType mtwomap(p, mtwo.size());
MapTypeConst mtwomapconst(p, mtwo.size());
cout << mone << endl;
cout << mtwo << endl;
cout << "Squared distance: " << (mone - mtwo).squaredNorm() << endl;
cout << "Squared distance using map: " << (mone - mtwomap).squaredNorm() << endl;
mtwomap(2) = 7;
cout << "Updated: " << mtwo << endl;
Output:
68 21 56 59 82
60 33 53 44 10
Squared distance: 4.52
Squared distance using map: 4.52
Updated: 60 33 7 44 10
However, this does not happen automatically when you write your functions using Eigen’s library. This is because any Map type is not similar to its Dense equivalent. To read in detail about this, refer to the next module of this blog.
Updating the Mapped Array
You can update the array of already declared Map objects. For that, you have to use C++ ‘placement new’ syntax. There are two cases in which you can use this syntax.
- When you know the location of the Mapped Array
It changes the appearance of the array but does not invoke the memory allocator. This is because you already know the location where you want to store the result array.
You can use the example below for a better understanding.
int data[] = {
1,
2,
3,
4,
5,
6,
7
};
Map < RowVectorXi > v1(data, 3);
cout << v1 << "\n";
new( & v1) Map < RowVectorXi > (data + 3, 5);
cout << v << "\n";
Output:
1 2 3
4 5 6 7
- When you don’t know the location of a Mapped Array
It changes the appearance of the array and also invokes the memory allocator. This is because the syntax has yet to specify the location where you want to store the result array.
You can use the example below for a better understanding.
Map < Matrix3f > A(NULL);
VectorXf b(n);
for (int i = 0; i < n; i++) {
new( & A) Map < Matrix3f > (get_matrix_pointer(i));
b(i) = A.trace();
}





