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

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.

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:-

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:-

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>))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>))We can even revisit the even columns of A example like;
A(all, seq(0,last-fix<2>)) 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>))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;
}
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;
}
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";
}
OUTPUT:







