Table of contents
1.
Introduction
2.
Overview of Slicing and Indexing
2.1.
Basic slicing
2.2.
Compile time size and increment
2.3.
Reverse order
2.4.
Array of indices
3.
Custom Index list 
4.
Frequently asked questions
4.1.
What is Slicing?
4.2.
How can you say Eigen is versatile?
4.3.
What does Matrix2d signify?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Overview of Slicing and Indexing

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

Introduction

Welcome Ninjas! Are you curious about the Eigen Library used as a linear algebra library for C++ users? 

This article gives a detailed overview of Slicing and Indexing in Eigen

Let us begin with the introduction of Eigen.

Eigen is an open-source high-level standard template c++ library for implementing linear algebra, matrix operations, vector operations, numerical solvers, and related algorithms. It works fast and is used in many tasks, from lengthy and complex numerical computations to simple vector algebra.

Eigen Logo

    

Overview of Slicing and Indexing

Slicing means taking a set of rows, columns, or elements, evenly spaced within a matrix achieved through the Eigen::seq or Eigen::seqN functions 

Basic slicing

"seq" signifies Arithmetic Sequence. Let’s look at the description of the following functions below:-

Table

It is a bit cumbersome to reference the last n elements. Hence we use  Eigen::placeholders::lastN(size), and   Eigen::placeholders::lastN(size,incr). Check out the following table:-

table

Compile time size and increment

Eigen, along with its compiler takes full advantage of the compile-time size and Increment. 

Take a look at the following example:-

v(seq(last-fix<7>, last-fix<2>))
You can also try this code with Online C++ Compiler
Run Code

In the next example, Eigen knows at compile-time that the returned expression consists of 5 elements. It is equivalent to:

v(seqN(last-7, fix<2>))
You can also try this code with Online C++ Compiler
Run Code

We can even revisit the even columns of A example like;

A(all, seq(0,last-fix<2>))     
You can also try this code with Online C++ Compiler
Run Code

Reverse order

We use negative increment row/column indices to enumerate the arays.

To add one over two columns of A from column 20 to 10:

A(all, seq(20,10, fix<2>))
You can also try this code with Online C++ Compiler
Run Code

The last n rows starting from the last one can be represented using

We can even use the ArithmeticSequence::reverse() method to reverse the order of an array. 

Array of indices

The generic operator() takes an arbitrary list of row or column indices as input stored as either an ArrayXi, a vector<int>, array<int,N>, etc.

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namepsace Eigen;
int main(){
   vector<int> ind{4,2,5,5,3};
   MatrixXi A = Random(4,6);
   cout <<"Initial A:"<<A<<endl;
   cout <<A(Eigen::placeholders::all,ind)<<endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

Code Output

 

Even a static array can be passed directly or

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namepsace Eigen;
int main(){
	 MatrixXi A = MatrixXi::Random(4,6);
	 cout<<"Initial A"<<endl<<A<<endl;
	 cout<<"A(all,{4,2,5,5,3}):"<<endl<<A(Eigen::placeholders::all,{4,2,5,5,3}) <<endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

Code Output

Even operations can be passed directly

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namepsace Eigen;
int main(){
	 ArrayXi ind(5);
	 ind<<4,2,5,5,3;
	 MatrixXi A = MatrixXi::Random(4,6);
	 cout<<"Initial matrix A:"<<endl<<A<<"\n";
	 cout<<"A(all,ind-1):"<<endl<<A(Eigen::placeholders::all,ind-1) << "\n\n";
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT:

Output

Custom Index list 

The operator() can accept any object of Type T as input.

Index s = ind.size(); or Index s = size(ind);
Index i;
i = ind[i];
You can also try this code with Online C++ Compiler
Run Code

Own fancy sequence generator can be built and passed to the operator().

Frequently asked questions

What is Slicing?

Slicing means taking a set of columns, rows, or elements, evenly spaced within a matrix or indexed from an array of indices.

How can you say Eigen is versatile?

Eigen supports various domains like matrix sizes, standard numeric types, including complex, and integers,matrix decomposition along with geometry features.

What does Matrix2d signify?

Matrix2d signifies a 2 x 2  square matrix of doubles - Matrix<double, 2, 2>

Conclusion

We hope you successfully understood the concept of Slicing & Indexing in Eigen. The blog firstly introduces Eigen and later cover up the topics of Slicing and Indexing. If you found the subject intriguing, you ab refer some similar blogs:

Catalogue of decompositions offered by eigen

Advanced methods for initializing matrices

Refer to the Basics of C++ with Data StructureDBMS, and Operating System by Coding Ninjas, and keep practicing on our platform Coding Ninjas Studio. You can check out the mock test series on code studio.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more! Refer to the interview bundle if you want to prepare for placement interviews. Check out interview experiences to understand various companies' interview questions.

Give your career an edge over others by considering our premium courses!

Happy Learning! 

 

Thankyou image

 

 

Live masterclass