Introduction
Welcome Ninjas! Are you interested in learning about the Eigen Library used as a linear algebra library for C++ users? Do you want to know how one can perform Eiigen’s reductions, visitors, and broadcasting? Well, you are at the right place.
Let’s start with the introduction of Eigen first.

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's reductions, visitors, and broadcasting
Without any further ado, let’s start with the topic - Eigen’s Reductions, Visitors, and Broadcasting.
Reductions
In reduction, a function/method takes a matrix or an array as a parameter and returns a single scalar value.
We have functions like - .sum(), which returns the sum of all the coefficients in a given matrix, .prod(), which returns the product of all the coefficients of a given matrix, .mean(), which returns the mean of all matrix coefficients, .trace(), which returns the sum of all the diagonal coefficients of the matrix and many more.
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namepsace Eigen;
int main(){
Eigen::Matrix3d m;
m<< 1, 2,3,
4, 5, 6,
7, 8, 9;
cout<<m.sum()<< endl;
cout<<m.prod()<< endl;
cout<<m.mean()<< endl;
cout<<m.minCoeff()<< endl;
cout<<m.maxCoeff()<<endl;
cout<<m.trace()<<endl;
}
OUTPUT
Boolean Reductions
In boolean reductions, we have a few methods mentioned below.
- .any() method returns true if at least one of the matrix's coefficients evaluates to true.
- .all() method returns true if all the matrix’s coefficients evaluate to true.
- .count() returns the number of coefficients in a Matrix that evaluate to true.
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main(){
Eigen::ArrayXXf m(3,3);
m<< 1,2,3
4,5,6,
7,8,9;
cout<<(m>>0).all()<<endl;
cout<<(m>0).any()<<endl;
cout<<(m>0).count()<<endl;
cout<<(m>1).all()<<endl;
cout<<(m>3).any()<<endl;
cout<<(m>3).count()<<endl;
}
OUTPUT
Partial reductions
They operate either column-wise or row-wise on a Matrix, returning a column or row vector with the corresponding values. We apply Partial reductions with the help of colwise() or rowwise().
Take a look at the following example:-
#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main(){
Eigen::MatrixXf mat(3,4);
mat << 1, 2, 6, 9,
3, 1, 7, 2,
6, 4, 0, 5;
cout <<"Row's maximum: " <<endl<<mat.rowwise().maxCoeff()<<endl;
}
OUTPUT
It’s important to note that row-wise operations return a column vector, while column-wise operations return a row vector.
Norm computations
- We can obtain the squared norm of a vector using the function squaredNorm(). This method returns the dot product of the vector by itself and is equivalent to the sum of squared values of its coefficients.
- There is another method norm() which returns the square root of the value returned from squaredNorm()
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main(){
Eigen::VectorXf v(2);
Eigen::MatrixXf m(2,2), n(2,2);
m << 1,-2,
-3,4;
cout<<m.squaredNorm()<<endl;
cout<<m.norm()<<endl;
}
OUTPUT
Visitors
Visitors help in finding the location of a coefficient in a Matrix.
For example, we use maxCoeff(&x,&y) for finding the location of the greatest coefficient in the matrix and minCoeff(&x,&y) for finding the location of the smallest coefficient in a Matrix wherein the arguments x & y to a visitor point to the variables in which the row and column positions are to be stored.
#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main(){
Eigen::MatrixXf m(3,3);
m << 1, 2, 3,
4, 5, 6,
7, 8, 9;
//find the location of maximum
Eigen::Index maxRow, maxCol;
float max = m.maxCoeff(&maxRow, &maxCol);
//find the location of minimum
Eigen::Index minRow, minCol;
float min = m.minCoeff(&minRow, &minCol);
cout<<"Max Value : "<<max<<"at :"<<maxRow<<","<<maxCol<<endl;
cout<<"Min Value : "<<min<<"at :"<<minRow<<","<<minCol<<endl;
}
OUTPUT
Broadcasting
The broadcasting concept intends to form an expression where a column vector or row vector is interpreted as a matrix by duplicating it in one direction.
For example, we can add a row vector with a column vector. Have a look:-#include <iostream
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main(){
Eigen::MatrixXf m(3,4);
Eigen::VectorXf v(2);
m << 1, 2, 6, 9,
3, 1, 7, 2,
5, 7, 10, 0;
v << 2,
1,
0;
m.colwise()= m.colwise()+v;
cout<<"Result:"<<endl;
cout<<m<<endl;
}
OUTPUT
In this, the expression m=m.colwise()+v tends to add the vector v to each column of the matrix m.
We can use the operators such as =, +, and - column-wise and row-wise. Essentially, note that the vector we add, either column-wise or row-wise should be of type Vector and not a Matrix; else you will get a compile-time error. Hence, broadcasting operations can be applied only if the type of object is Vector when operating with a Matrix.
We can do the same operation row-wise:-
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main(){
Eigen::MatrixXf m(2,4);
Eigen::VectorXf v(4);
m << 2, 4, 7, 10,
3, 3, 0, 11;
v << 2,3,1,0;
//add v to each row of m
m.rowwise()= m.rowwise()+ v.transpose();
cout <<"Result " <<m<<endl;
}
OUTPUT