You must have thought about why we must manually do all the tricky mathematical calculations, as it is sometimes very boring. They invented computers to make things easier, did they not?
Well, someone else thought it too, and Eigen the beautiful thing that came out of their mind.
In this article, we will discuss the various symbols in the namespace of the Eigen library.
Eigen Library
It is hard to code mathematical operations like matrices, linear algebra, vectors, geometric functions, etc., for any data in C++. To overcome this, they developed the Eigen library. It consists of all the mathematical operations you can use by adding the library using preprocessors in C++ expressions.
Eigen is a C++-based open-source linear algebra library. It is quick and effective for various applications, from complex vector arithmetic to numerical computing. It includes linear algebra, geometrical transformations, numerical solvers, matrix, vector operation, and other related algorithms.
Preprocessing Syntax
#include<Eigen/<eigenclass>>
Notice the “eigen class” in the syntax given above. Eigenclasses are hidden classes for each specific operation, for ex., dense, core, etc. Classifying an eigenclass in the preprocessor makes it easier for the compiler to understand the code.
Benefits of Using Eigen
Using the Eigen library has many benefits, such as:
The Eigen library is continuously developed and releases new versions regularly.
Eigen's API syntax is clear, straightforward, and well-known.
It provides dynamic matrices, which allow us to create matrices whose sizes are determined at runtime.
Eigen has significant "battle testing" and, consequently, few bugs.
Storage can be either row-major or column-major.
Structures that are optimized - Both dense and sparse matrices are accessible.
Templates for expressions - Lazy evaluation enables complex matrix arithmetic while preserving performance.
About Namespace
A namespace is a defined area that gives the names of the types, functions, variables, etc., within its scope.
Namespaces are used to classify code logically and avoid name clashes, which are more likely to happen when your code base contains several libraries.
The library is organized based on the identifier's functionality by classifying these identifiers(types, functions, variables, etc.) using a namespace.
The picture above shows how the classification is executed. The identifiers related to colours are grouped, and then they can be called using functions defined in the group. These groups have different namespace names.
Example
Let's look into an ex. of namespace calling in C++ for a better understanding.
#include<iostream.h>
int main()
{
std::cout << "Enter a number";
int n1 = 0;
std::cin>> n1;
std::cout << "The entered number is "<< n1;
return 0;
}
Explanation
The above code is a basic code for taking a value from the user and displaying it on the screen. Observe that we have used "std::" before "cin" and "cout". The code indicates that the "cin" and "cout" are defined inside the namespace "std." Similarly, we can use namespace names for specific groups to use functions or any identifier in our program.
Every time we write "cin" or "cout", we need to specify this namespace_name::prefix which is "std::" in this case. To avoid this, we use namespace declarations.
Namespace Declaration
A namespace declaration is a name used as a prefix for names declared later. Other members of the namespace can be considered when defining a member. They can be found without the namespace prefix using the name lookup.
Using the namespace declaration, we can use the namespace name without specifying the namespace_name::prefix.
Syntax
The syntax used to declare the namespace :
Using namespace <namespace name>;
Breaking the syntax apart:
The "Using" is a declarative method used to declare anything, such as a namespace in C++.
We write a namespace to instruct the compiler that we will be using a namespace.
We then write the namespace name that we want to use.
Implementation
Let's look into the basic example of code for taking a value from the user and displaying it on the screen again.
#include<iostream.h>
using namespace std;
int main()
{
cout << "Enter a number";
int n1 = 0;
cin>> n1;
cout << "The entered number is "<< n1;
return 0;
}
Explanation
In the above program, we can observe that we have declared the namespace "std". We no longer need to write "std" with "cout" as it is already declared. We also don't need to write the "std" with "cin", as we have declared that we will be using the namespace "std" in our program.
Eigen Library Namespace
In the previous section, we looked into an example of a namespace in C++. Now, we will discuss the namespace in the Eigen library of C++.
There are a total of 3 namespaces in the Eigen library, which are as follows:
Eigen namespace.
Eigen indexing namespace.
Eigen symbolic namespace.
The eigen namespace contains all the classifications of groups. A question must have popped into your head. Isn't that the role of the Eigen library? What is the purpose of an eigen namespace? The answer to this question is that the eigen namespace contains all the grouped namespaces, so you only need to declare one namespace instead of declaring all the namespaces separately. It will ensure code optimization, an essential achievement for any programmer or developer.
Eigen Namespace Syntax
The syntax to use the Eigen namespace:
Using namespace Eigen;
We have used the namespace declaration that we learned earlier in this article.
In the following sections, we will learn about the other two namespaces.
Eigen Indexing Namespace
Eigen indexing namespace in the Eigen library is used for the identifiers that can use inside the operator(). It contains all the functions and symbols, such as fix(), placeholders(), etc. It is a substitute for declaring all these functions separately using another namespace declaration method.
The syntax for declaring namespace for identifiers separately is:
Using Eigen::<identifier name>;
Breaking the syntax apart:
The "Using" is a declarative method used to declare anything, such as a namespace in C++.
We write “eigen namespace” to instruct the compiler that we will be using it.
“::” is a scope resolution operator used to access global identifiers in C++.
We then write the identifier name that we want to use.
This type of declaration has a disadvantage: we can only use it for the specific function mentioned. Also, it creates a scope of application only for the specified identifier. The compiler will give an error if we use the namespace for any other function or identifier. Thus it is advised to declare the eigen namespace in the Eigen library.
Syntax
The syntax for declaring an eigen indexing namespace in the Eigen library is below.
Using namespace Eigen::indexing;
As you must have observed, this syntax is a mixture of the two types of namespace declaration that we learned about. We have declared the namespace in the Eigen library and then further declared that we will use a scope resolution operator to use the indexing namespace from the Eigen namespace.
Eigen indexing namespace in the Eigen library is an advancement into the Eigen namespace that we learned about in the previous section.
One thing is recommended if we declare the different types of documentation in a program to include them all at once and aim for code optimization.
Eigen Symbolic Namespace
Eigen symbolic namespace in the Eigen library is used for the symbols in the Eigen library. This namespace in the Eigen library also contains classes and functions.
It is currently used only for symbols in the Eigen library defined in the following functions or classes:
placeholders-last
placeholders-lastp1
seq
seqN
NOTE: It is only used for symbols in the Eigen library of scalar type index.
Syntax
The syntax for declaring the eigen symbolic namespace in the Eigen library is as follows:
Class Eigen::symbolic
Notice that we have not declared it like the other namespaces in the Eigen library. This is because it is currently only used for a minimal number of symbols in the Eigen library. Therefore, this is declared a class inside the library under a template keyword.
Classes Within Namespace
User-defined types used as a blueprint for objects are known as classes.
Why are classes called blueprints?
We say so because classes consist of the state and behaviour of objects which later can be used by calling out the class for various purposes.
We can use three different types of classes with the symbolic namespace in the Eigen library to declare and use classes and symbols in the Eigen library.
The three classes are as follows:
BaseExpr
BaseExpr class within the namespace is declared to be used for any base class to express symbols in the Eigen library. It is useful for the "derived" type parameter of the template.
The syntax :
class Eigen::symbolic::BaseExpr< Derived >
SymbolExpr
This class within the namespace is declared to be used for symbols in the Eigen library, which are identified only by the "tag" parameter of the template.
The syntax:
class Eigen::symbolic::SymbolExpr< tag >
SymbolValue
This class within the namespace is declared to be used when we want to evaluate the actual value of symbols in the Eigen library. It identifies the "tag" type parameter of the template.
It returns the type of SymbolValue::operator= symbol.
What are some common operations that can be performed using the Eigen Library?
Functions such as the definition of vectors, matrices, and quaternions, along with some mathematical functions such as addition/subtraction of vectors, dot product, and cross product, can also be completed using the Eigen’s Geometry Library.
What are the Primary Fields where the Eigen Library is used?
The Eigen's Geometry Library is very useful for Robotics and other real-world applications. Eigen's library is used to achieve many algebraic computations quickly.
What are Euler Angles?
Euler Rotations are the set of three angles in the matrix that can be rotated. Euler Angles represent a rotation in three dimensions space as three Euler Angles.
What are the different types of libraries in C++?
Static libraries and dynamic libraries are the two types of libraries.
Is Eigen open source?
Since version 3.1, Eigen has been open-source software covered by Mozilla Public License 2.0. The GNU Lesser General Public License applied to earlier iterations. Version 1.0 became available in December 2006.
Conclusion
In this article, we discussed namespaces and different ways to declare namespaces. We also learned about the different types of namespaces in the Eigen library, which are used for functions, classes and symbols in the Eigen library.
For more information on Eigen and related topics, refer to the following articles: