Hello ninjas, We all have faced problems while writing programs for matrix and vector operations in C++. The solution to most of the problems is to use the Eigen matrix base in C++ offered by the Eigen library.

This article is an in-depth learning of what an Eigen matrix base in C++ is and learn how to extend it using the inherit from the matrix method.

Eigen Library

The term “Eigen '' is derived from the set of values related to linear equations in a matrix equation called eigen values.

It is hard to code mathematical operations like matrices, linear algebra, vectors, geometric operations, etc., for any type of data in C++. To overcome this, Eigen library was developed. It consists of all the mathematical operations you can use by adding the library using preprocessors in C++ expressions.

Preprocessing Syntax

#include<Eigen/<eigenclass>>

Notice the “eigen class” in the syntax given above. Eigen classes are hidden classes for each specific operation, for ex., dense, core, etc. Classifying an eigen class in the preprocessor makes it easier for the compiler to understand the code.

Eigen Matrix Base in C++

As discussed above, there are eigen classes that we specify in the preprocessor. For Matrix operations, we use the “dense base class.” Dense base classes in C++ are core classes that consist of all the methods that allow operation on matrices or even any expression. Eigen matrix base in C++ is one of the base classes offered by the Eigen library.

What are base classes? Subclasses to the primary eigen class in the Eigen library are called base classes. Eigen matrix base in C++ is one such subclass of the “dense” class of the Eigen library. This class is used for operations related to matrices and vectors. It is the most used class for these operations among other classes like “Matrix” and “VectorwiseOp”.

For a better understanding of the above concept, let’s look into the below-given example. We use the template and typename keywords to use eigen MatrixBase

This code is for printing the first row of a matrix using the eigen matrix base in C++.

Output

Try and compile by yourself with the help of online C++ Compiler for better understanding.

Public Member Functions

Public member functions are functions that are attached to objects. We use them by calling out the function and setting the parameters. In the above example, "void MatrixBaseExample(const Eigen::MatrixBase<Derived>& x)” represents a public member function. We use parameters to pass these functions. These are termed as “Public” because we can call out these functions anytime throughout the program.

The below table shows a few important public member functions with their parameters and the eigen base classes they are used with.

Extending an Eigen Matrix Base in C++

Extending an eigen matrix base in C++ means adding elements in a class for all other expressions along with matrix and vectors. As we know, C++ does not provide the feature to add elements to a library class. Thus, to add elements in the eigen matrix base in C++, we use the plugin mechanism.

The preprocessor “EIGEN_MATRIXBASE_PLUGIN” is used in the plugin mechanism to extend an eigen matrix base in C++. In this method, you can replace the “matrixbase” with any other base class to extend that class by the plugin mechanism. For example, the EIGEN_ARRAYBASE_PLUGIN preprocessor will help you extend the array base class.

Syntax

class MatrixBase {
#ifdef EIGEN_MATRIXBASE_PLUGIN
#include EIGEN_MATRIXBASE_PLUGIN
#endif
};

Now, you can use the matrix base class to operate on any expression in C++.

Inherit From the Matrix

Inherit from the matrix method is used when you want to add a few elements to the matrix. You need to make sure you know the difference between adding elements to perform operations on any expression we use the plugin method and adding elements to the matrix operation, which is done using the inherit from matrix method. Both are very different problems with different solutions.

The inherit from the matrix method is used when a matrix resides deep inside a function. This means that the path to a matrix consists of more than three redirects.

Example

For a better understanding of the above concept, let’s look into the below-given example.

Code

#include <Eigen/Core>
#include <iostream>
class TypeOfVector : public Eigen::VectorXd
{
public:
TypeOfVector(void):Eigen::VectorXd()
{}
// Eigen expression is used to construct TypeOfVector
template<typename OtherDerived>
TypeOfVector(const Eigen::MatrixBase<OtherDerived>& other:Eigen::VectorXd(other)
{}
// Assign Eigen expressions to TypeOfVector
template<typename OtherDerived>
TypeOfVector & operator=(const Eigen::MatrixBase <OtherDerived>& other)
{
this->Eigen::VectorXd::operator=(other);
return *this;
}
};
int main()
{
TypeOfVector a = TypeOfVector::Ones(8);
a(4) += 10;
a = 4 * a;
std::cout << a.transpose() << std::end1;
}

Notice how several layers of inheritance exist in the above code.

Output

This example shows how you can use the inherit from the matrix method to operate on matrices with multiple path layers.

You can download the Eigen library for C++ just like any other application or library from the browser. Firstly, download the Eigen library from the official website and unzip the file at the desired location. Then download the modules, and you’ll be good to go.

Who is the developer of Eigen?

Benoît Jacob and Gaël Guennebaud developed the Eigen library. This library was released in December 2006.

What cannot be inherited in C++?

A base class with a friend function cannot be inherited in C++. The rest of the base classes can be inherited, for example, inherit from the matrix in the matrix base, etc.

What are the five types of inheritance in C++?

The five types of inheritance in C++ are single inheritance, multiple inheritances, multilevel inheritance, hierarchical inheritance, for example, inherit from the matrix and hybrid inheritance.

What are Affine Transformations?

Affine transformation is a linear method that preserves mathematical entities such as points, straight lines, and planes. It can also be described as a transformation that preserves collinearity and distance ratios.

Conclusion

This article discussed what Eigen matrix bases in C++ are. We also learned what Eigen libraries are and how to add elements to an eigen matrix base in C++. We also looked into how to inherit from the matrix in C++ with examples. To learn more about the Eigen library and its matrix operations, refer to