Table of contents
1.
Introduction
2.
Eigen Library
3.
Installing Eigen
4.
Writing First Program
5.
Matrices and Vectors
6.
Real-Life Use of Eigen
7.
Frequently Asked Questions
7.1.
What are the different types of libraries in C++?
7.2.
Is Eigen open source?
7.3.
Which is better, Eigen or BLAS/LAPACK?
7.4.
Does Eigen library employ CUDA?
7.5.
Does Eigen make use of OpenMP?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Getting Started with Eigen

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When you step into the world of programming using C++, you will be introduced to many types of libraries. As we know, libraries are prewritten code used by programmers. Ever wonder which libraries are best for doing linear algebra?

getting started with eigen

This article will discuss getting started with Eigen library. We will discuss the installation and implementation of this library through a program. So without further ado, let’s get started!

Eigen Library

For getting started with Eigen, let's first know about the Eigen library. 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. Using the Eigen library, we can ease the work of programmers by far.

After getting started with Eigen, you'll have 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 optimised - Both dense and sparse matrices are accessible.
     
  • Templates for expressions - Lazy evaluation enables complex matrix arithmetic while preserving performance.

Installing Eigen

You can download Eigen from its official website

eigen website

The source code for the Eigen library will be downloaded using Git or extracted from the zip file into the user's home directory. Of course, you can use whatever directory you choose; simply change the tilde ~ in the following commands to the download path you like.

$ cd ~ 
$ git clone https :// github .com/ eigenteam /eigen -git - mirror


We don't need to create anything here, so if you're nervous about building libraries from the source like I am, don't be. All of Eigen's source code is contained in header files because it employs pure header libraries. 

Now open Visual Studio. If you have not installed Visual Studio on your system, we recommend installing it.

vs home screen

You’ll probably see this screen when you open Visual Studio.

Now right-click on the project, and you’ll see the following sections.

properties

Now click on the properties. The following dialogue box will appear.

project property page

Make sure the path of the additional file that you cloned/extracted is in the additional include directories section. Click on Ok if the path is correct; otherwise, mention the path of cloned/extracted files.

The function and class definitions are all contained in .H files; there are no separate .CPP files for these purposes. Because of this, using the Eigen library is very easy. Simply #include these header files at the start of your own code to use it.

Now that you had successfully installed Eigen in your system, you are up for getting started with Eigen library.

Writing First Program

Following code given below is your first program which will help you getting started with Eigen library.

#include <iostream>
#include <Eigen/Dense>

using Eigen::MatrixXd;
using namespace std;

int main()
{
	 MatrixXd m(2,2);
		 m(0,0) = 4;	
		 m(1,0) = 6;
		 m(0,1) = 5;
		 m(1,1) = m(1,0) + m(0,1);
	 cout << m << endl;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

output of first program

Explanation:

We use "using namespace Eigen" to simplify the syntax and "#include<Eigen/Dense>" to include the Eigen library header files. If you don't, you'll need to use "Eigen::" in front of each command. The MatrixXd represents the matrix of arbitrary size in which every entry is of double type.

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

Matrices and Vectors

Now, let’s have a look at an example where matrices are combined with vectors when we set the size at run time. 

#include <iostream>
#include <Eigen/Dense>
using namespace std;

using Eigen::MatrixXd;
using Eigen::VectorXd;

int main()
{
	 MatrixXd m = MatrixXd::Random(3,3);
       m = (m + MatrixXd::Constant(3,3,2.2)) * 40;
	 cout << "m =" << endl << m << endl;
	   VectorXd v(3);
	   v << 1, 2, 3;
	 cout << "m * v =" << endl << m * v << endl;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

output for matrices and vector program

Explanation:

The example begins by creating a 3-by-3 matrix m, which is initialised with random values between -1 and 1 using the Random() method. The values are mapped linearly to fall between 20 and 120 in the following line. The MatrixXd::Constant was used. A 3-by-3 matrix expression with all coefficients equal to 2.2 is produced using the function constant(3,3,2.2). The rest is simple math.

Also see, Application of Oops

Real-Life Use of Eigen

After getting started with Eigen library, you should know some real-life use of Eigen too. The following programs use the Eigen library in their production-level code:

  • Tensorflow: One of the most widely used machine learning frameworks.
  • Matplotlib: It is used to plot 2D charts.
  • Koffice2: The KDE office program.

 

Check out most important Git Interview Questions here.

Frequently Asked Questions

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.

Which is better, Eigen or BLAS/LAPACK?

BLAS/LAPACK covers only a few topics that Eigen does. Fixed-size matrices and vectors, which are quite popular, are handled by Eigen. Eigen supports sparse matrices and vectors by default. Also, the Eigen library offers numerous practical capabilities that are also quite popular (see the Array module, Geometry module, etc.).

Does Eigen library employ CUDA?

Eigen library is now been used inside CUDA kernels since version 3.3.

Does Eigen make use of OpenMP?

Eigen utilises the number of threads supplied by OpenMP unless setNbThreads is called.

Conclusion

This article briefly discussed getting started with eigen library. We discussed the Eigen library, its installation and its implementation. We hope this blog has helped you getting started with Eigen. You can also refer to our blogs, What is Matrix base and how to inherit from a matrix and Eigen sparse matrix example, to have a better understanding of matrix operations. 

If you like to learn more, you can check out our articles: 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptAWS and many more! If you wish to test your competency in coding, check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

If you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

Nevertheless, consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass