Table of contents
1.
Introduction
2.
What is an Eigen Array Class?
3.
Eigen Array Class Types
4.
Assigning Values inside an Array
5.
Addition and Subtraction
6.
Array Multiplication
7.
Coefficient-wise Operation
8.
Frequently Asked Questions
8.1.
Is Eigen column or a row-major?
8.2.
How can I determine the Eigen version?
8.3.
Does Eigen utilise Lapack?
8.4.
Is Eigen header only library?
8.5.
Does TensorFlow use the Eigen library?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

How to Use Eigen's Array Class

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

Introduction

We generally use the Eigen library for linear algebra, matrix and vector operations, geometrical transformations, and related algorithms. In this article, we will discuss the Eigen array class. Due to the similarity between the Eigen Array class and the Eigen matrix class, we will discuss the Eigen array class by taking the matrix class in context.

how to use eigen array class

We will be discussing what an Eigen array class is. Afterwards, we will discuss Eigen array class types and how to access values inside the class. At last, we will see operations like add, subtract, multiply and coefficient-wise operations. So without any further ado, let’s get started!

What is an Eigen Array Class?

While using Eigen, we generally talk about arrays and matrices. So let’s discuss the Eigen array class by taking the matrix class in context. 

The Eigen Array class and Matrix class are quite similar. The Eigen array class provides general-purpose arrays, while the matrix class are intended for linear algebra. The API for the Eigen array class makes it simple to access coefficient-wise operations, but the API for the Matrix class makes it simple to access linear-algebra operations. 

You can refer to our article overview of slicing and indexing to have a better understanding of slicing and indexing, which is of great importance in Eigen's array.

Eigen Array Class Types

As the word "array" refers to both 1-dimensional and 2-dimensional arrays, Eigen typedefs are similar to those in Matrix but with a few minor changes. We follow the tradition that 1-dimensional arrays are represented by typedefs of the form ArrayNt, where N and t are the size and scalar type, respectively. We make use of typedefs of the form ArrayNNt for 2-dimensional arrays. 

The table below provides a few examples:

Type

Typedef

Array<float,Dynamic,1>

ArrayXf

Array<float,4,1>

Array4f

Array<double,Dynamic,Dynamic>

ArrayXXd

Array<double,2,2>

Array22d

Assigning Values inside an Array

To assign values inside an Eigen array class, we can assign values coefficients by a coefficient or by using a comma initialiser. 

The following example shows the assignment example for the two cases said above,

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

int main()
{
	 Eigen::ArrayXXf  m(2,2);
	  // Assign some values coefficient by a coefficient
		 m(0,0) = 5.0; m(0,1) = 6.0;
		 m(1,0) = 7.0; m(1,1) = m(0,1) + m(1,0);
			 
	  // Print values to standard output
		 cout << m << endl << endl;
			 
	  // Using the comma initialiser is also allowed
		 m << 5.0,6.0,
    		  7.0,13.0;
   
 	  // Print values to standard output
 		 cout << m << endl;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

output

Explanation:

We assign values of the coefficient by a coefficient and then print it accordingly. Afterwards, we use comma initialiser to assign values to the coefficient.

Addition and Subtraction

The operations, such as adding or subtracting, are similar for arrays and matrices. When we do these operations, we should keep in mind that the size of both arrays should be the same.

Unlike matrices, we can also add or subtract a scalar value in the array.

Let’s take a look at an example to understand these operations,

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

int main()
{
	 Eigen::ArrayXXf a(3,3);
	 Eigen::ArrayXXf b(3,3);
		 a << 9,8,7,
   			  6,5,4,
   			  3,2,1;
 
		 b << 1,2,3,
    		  4,5,6,
    		  7,8,9;
     
 	// Adding two arrays
		cout << "a + b = " << endl << a + b << endl << endl;

	// Subtracting a scalar from an array
		cout << "a - 1 = " << endl << a - 1 << endl;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

output

Explanation:

To do add and subtract operations on the array, the arrays must have the same size. The 3d array is of size (3,3) in the above example. This is why we can perform the add and subtract operations with ease.

The addition is being performed on a[i,j,k] and b[i,j,k]. Along with subtracting a scalar value from a[] in the above example.  

Array Multiplication

As we know, we can multiply an array by a scalar. From our knowledge of matrices, we know that there is a fundamental difference between matrices and arrays. Matrix multiplication is interpreted as matrix product, while array multiplication is interpreted as coefficients-wise products. So we can only do the array multiplication if their dimensions are the same.

Let’s consider an example to have a better understanding of array multiplication.

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

int main()
{
	 Eigen::ArrayXXf a(2,2);
	 Eigen::ArrayXXf b(2,2);
		 a << 3,5,
		      5,6;
		 b << 7,8,
   			  9,10;
	 cout << "a * b = " << endl << a * b << endl;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

output

Explanation:

The only condition we need to take care of while doing array multiplication is the array should have the same dimensions. In the above example, a[] and b[] have the same dimensions of (2x2). This is why array multiplication is possible.

Now for  a*b(0,0)= 3*7= 21, a*b(0,1)= 5*8= 40, a*b(1,0)= 5*9= 45 and a*b(1,1)= 6*10= 60.

Coefficient-wise Operation

Apart from the addition, subtraction, and multiplication operators, the Eigen array class also have various coefficient-wise operations. 

For instance, the .abs() method determines each coefficient's absolute value. The .sqrt() determines the coefficients' square roots. We can also use the function .min() to create an array whose coefficients are the minimum of the corresponding coefficients of the two identically sized arrays.

Also read - abs in C++

Let’s have a look at the following example which shows these operations,

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

int main()
{
	 Eigen::ArrayXf a = Eigen::ArrayXf::Random(5);
	 a *= 2;
	 cout << "a =" << endl << a << endl;
	 cout << "a.abs() =" << endl << a.abs() << endl;
	 cout << "a.abs().sqrt() =" << endl << a.abs().sqrt() << endl;
	 cout << "a.min(a.abs().sqrt()) =" << endl << a.min(a.abs().sqrt()) << endl;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

output

Explanation:

The .abs() method determines each coefficient's absolute value. The .sqrt() determines the coefficients' square roots. At last, the .min() function returns the minimum value among the absolute and square root values in the above example.

You can also refer to how to use Block Operation to have a look at another important operation in Eigen. 
Check out this problem - Maximum Product Subarray

Frequently Asked Questions

Is Eigen column or a row-major?

Eigen is column major default. Naturally, column-major matrices are used for most of the Eigen package development and testing. This means that the Eigen library might perform better with column-major matrices, even though we want to support both row-major and column-major storage ordering publicly.

How can I determine the Eigen version?

You may find the version on Debian or comparable systems by running cat/user/include/eigen3/Eigen/src/Core/util/Macros. h | grep VERSION.

Does Eigen utilise Lapack?

Many of Eigen's algorithms are silently replaced by calls to the LAPACK or BLAS routines. Only objects that are dynamic or large enough and have one of the four common scalar types—float, double, complexfloat>, and complexdouble>—are eligible for these substitutions.

Is Eigen header only library?

In actuality, the only files needed to compile programmes using Eigen are those found in the Eigen subfolder. For all platforms, the header files are the same. Neither installing anything nor using CMake is required.

Does TensorFlow use the Eigen library?

Yes, TensorFlow uses the tensor module of the Eigen library.

Conclusion

This article briefly discussed how to use the Eigen array class. We discussed the Eigen Array class along with its types. We also discussed how to perform various operations on it. Thus concluding our discussion on the Eigen array class.

We hope this blog has helped you get started with Eigen. If you like to learn more, you can check out our articles: 

Recommended problems -

 

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