Table of contents
1.
Introduction
2.
Eigen's reductions, visitors, and broadcasting
2.1.
Reductions
2.1.1.
Boolean Reductions
2.1.2.
Partial reductions
2.1.3.
Norm computations
2.2.
Visitors
2.3.
Broadcasting
3.
Frequently asked questions
3.1.
How can the diagonal sum be calculated apart from the .trace() method?
3.2.
Which operators can not be used in matrices?
3.3.
How can we obtain the current size of the matrix?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Eigen's reductions, visitors and broadcasting

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

OG Image

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

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;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

Code 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;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

Code 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;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

Code 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;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

Code 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;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

Code 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;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

Code 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;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

Code output

Frequently asked questions

How can the diagonal sum be calculated apart from the .trace() method?

Sum of the diagonal coefficients can alternatively be calculated using a.diagonal().sum().

Which operators can not be used in matrices?

The operators such as  *=, /=, *, andare not available on matrices because their function is not clear in matrices.

How can we obtain the current size of the matrix?

We can obtain the current size of the matrix using the size() method.

Conclusion

We hope you have successfully understood the blog. The blog explains how to perform Eigen’s Reductions. The blog also briefs you about the visitors and broadcasting. If you are intrigued by this topic, you can refer to 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