Hello Ninja, Welcome back. Libraries are prewritten pieces of code that programmers use. Have you ever wondered which libraries are the best for doing linear algebra? Well, it turns out that Eigen is a wonderful library for this.

In this blog, we will be going to study what happens inside Eigen.

What is Eigen?

A C++-based open-source linear algebra library is called Eigen. It is quick and efficient for a variety of tasks, from complex vector arithmetic to rigorous numerical calculation. The purpose of this lesson is to inform readers with a basic understanding of C++, linear algebra, and computer graphics about what happens inside Eigen while building graphics programmes.

It is possible to compute eigenvalues in an optimised way as well. Calculating geometric quantities, such as transforms, rotations, scaling, rotations, and quaternions is possible with the Geometry module.

So, Let’s start with Design then, we will see What happens inside Eigen?

Design

The following is the list of design elements for the Eigen library:

The expression templates metaprogramming approach is used in Eigen's design, which creates expression trees at compile time and produces custom code to evaluate them.

The library handles its own loop unrolling and vectorisation with expression templates as well as a cost model of floating-point operations.

The code for the Eigen library is in header files, so you do not need to install or build it.

Compiler Assistance

Since Eigen is a library, a compiler must be used to compile it. Major compilers like

LLVM - low-level virtual machine

MinGW - Minimalist GNU for Windows

GCC - GNU Compiler Collection

Intel C++ Compiler

MSVC - Microsoft Visual C++

Usage in Real Life

The following outstanding software use Eigen in its production-level code:

Tensorflow: One of the most well-liked artificial intelligence frameworks.

Matplotlib: In order to plot 2D charts.

Koffice2: The workspace for KDE (K Desktop Environment).

Let’s start with the main topic of the blog what happens inside Eigen.

Understanding the Program

Here with the below example, we will understand what happens inside Eigen.

We will understand the below code step by step:

Example

#include<C:\vcpkg\vcpkg\buildtrees\eigen3\src\3.4.0-74a8d62212.clean\Eigen\Core>
#include<iostream>
using namespace std;
int main()
{
int var = 1;
// A vector of floats with a dynamic size is called VectorXf.
Eigen::VectorXf a(var), b(var), c(var);
a = b + c;
cout << a;
}

Output

Take a look at the code.

a = b + c; // line 1

The line of code from the line1 that results from creating a simple C++ library with a VectorXf class that contains an operator (+) producing a VectorXf would look like this:

VectorXf tmp = b + c;
VectorXf a = tmp;

This is a pointless usage of the temporary tmp. It has a significant negative impact on speed because there are now two for loops and the construction of tmp necessitates a dynamic memory allocation in this situation (in case of for loop):

for(int i = 0; i < length; i++) tmp[i] = m[i] + n[i];
for(int i = 0; i < length; i++) a[i] = tmp[i];

Performance suffers greatly when we traverse arrays more than once since it requires several duplicate memory accesses.

The class template Matrix has six template parameters stated in src/Core/util/ForwardDeclarations.h, but the last three are automatically decided by the first three. So, for the time being, you shouldn't worry about them. In this context, a matrix of floats with a dynamic number of rows and 1 column is referred to as Matrix<float, Dynamic,1>.

The matrix class inherits the foundation class MatrixBase. It is sufficient to note that MatrixBase unifies matrices, vectors, and all expression types.

Construction of the Sum Expression

Let's go on to the subsequent line now that our vectors have been built:

a = b + c;

The short version is that operator+ returns an expression for the "sum of vectors" but does not actually carry out the calculation. The calculation is made by the operator (=), whose call comes next.

Let's see what Eigen does next after observing this:

a + b

Here, ‘b’ and ‘c’ are of type VectorXf, a typedef for a subclass of MatrixBase that is a specialisation of Matrix (as we mentioned previously).

MatrixBase::operator+(const MatrixBase&)

This operator's return type is

CwiseBinaryOp, VectorXf, VectorXf>

We come across an expression template for the first time in the CwiseBinaryOp class. As we previously said, the operator (+) just provides an expression that represents the "sum of vectors" without performing any calculation on its own.

We group them all together as "coefficient-wise binary operations," which we abbreviate as "CwiseBinaryOp," because there are other equations for "difference of vectors" and "coefficient-wise product of vectors". Coefficient by coefficient is what is meant when anything is done "coefficient-wise". When we add two vectors together, we are adding two operands, which is referred to as a binary operation.

The Assignment

In the process of compiling the line of code, the equation b + c has now completed evaluation.

a= b + c;

The operator (=) is now entered.

What operator is being invoked in this case? The vector ‘a’ is an object of the Matrix-specific class VectorXf. In the definition of the class Matrix in src/Core/Matrix.h, we find the following:

template<typename OtherDerived>
inline Matrix& operator=(const MatrixBase<OtherDerived>& other)
{
eigen_assert(m_storage.data()!=0 && "you cannot use operator= with a non initialized matrix (instead use set()");
return Base::operator=(other.derived());
}

The base is a typedef for MatrixBase in this case. The operator= of MatrixBase is hence what is being referred to. Check out its prototype in MatrixBase.h in src:

Since ‘a’ is a VectorXf in this instance, Derived is a VectorXf, and OtherDerived is a CwiseBinaryOp. According to the previous section, OtherDerived is more specifically:

Eigen is a powerful open-source C++ framework for numerical solvers, geometric transformations, matrix and vector operations, and related algorithms.

Is Eigen C++ fast?

Eigen can manage and globally optimise a whole action, making it quicker than any BLAS implementation for actions requiring complex expressions.

Is C++ language good for multithreading?

This not only makes use of many CPU cores, but it also gives the developer the ability to control the number of tasks executed by adjusting the thread pool size. The computer resources may then be used by the software properly without getting overloaded.

What are some common operations that can be performed using the Eigen Library?

Operations such as the declaration of vectors, matrices, and quaternions, along with some mathematical operations such as addition/subtraction of vectors, dot product, and cross product, can also be performed using the Eigen’s Library.

What are the Primary Fields where the Eigen Library is used?

The Eigen Geometry Library is very useful for Robotics and other real-world applications. Eigen’s Library is used to perform a wide range of algebraic computations rather easily.

Conclusion

In this article, we briefly discussed what happens inside Eigen. Like designing compiler vectors and sum expressions. Ans also the Construction of the Sum Expression.

For more information on Eigen and related topics, refer to the following articles: