Table of contents
1.
Introduction
2.
Advanced methods for initializing matrices
2.1.
Comma Initialiser 
2.2.
Special matrices and arrays
2.3.
Usage as temporary objects
3.
Frequently asked questions
3.1.
What are the benefits of using Eigen?
3.2.
What header do we use in Eigen-specific programs?
3.3.
Name the compilers which support Eigen?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Advanced methods for initializing matrices

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 initialize matrices? Well, you are at the right place. Let’s begin with the introduction of Eigen and matrices 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 arithmetics. 

Matrices are a collection of rows and columns. They can be created using arrays or vectors.

Eigen Logo                       Matrix

Advanced methods for initializing matrices

In this section, we will look into the primary concern of this blog. So, let’s start without further ado and learn the methods for initializing matrices.

Comma Initialiser 

  • Comma Initialiser provides an easy way to set the coefficients of a matrix(vector/array). For that, we need to enlist the coefficients starting from the first-row first column, moving column-wise, and then row-wise. Note that it’s essential to specify the object's size, or else the Eigen might complain.

 

#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main(){
  Matrix4f mx;
  mx<<1,2,3,4
        5,6,7,8
        9,10,11,12;
  cout<<mx;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

code output

  • Another way is to use vectors for the initialization of matrices. We can use two-row vectors and join them together. Look at the following example:

 

#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main(){
  RowVectorXd v1(3);
  RowVectorXd v2(3);
  v1 << 10, 11, 12;
  cout<<"v1:"<<v1<<endl;
  v2 << 21,24,19;
  cout<<"v2:"<<v2<<endl;
  RowVectorXd join(6);
  join<<v1,v2;
  cout <<"join"<<join<<endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

code output

  • Comma Initialiser can even initialize the matrices with a block structure. Take a look at the following example:

 

#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main(){
  MatrixXf m1(2, 2);
  m1<<1,2,3,4;
  MatrixXf m2(4, 4);
  m2<<m1,m1*10,m1*10,m1;
  cout<<m2<<endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT 

code output

  • The comma initializer is also used to fill the inbuilt block expressions like m.row(i). 

Special matrices and arrays

1.We have static methods like Zero(), which initializes the coefficients to zero  

We have three variants:

  • The first one takes no arguments and is used for fixed-size objects only. 
  • The second variant uses one argument and is used for one-dimensional dynamic-size objects.
  •  The third variant takes two arguments and is used for two-dimensional objects.

 

#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main(){
//fixed size array
Array33f a=Array33f::Zero();
cout<<a<<endl;

//1-D dynamic-size array
ArrayXf b = ArrayXf::Zero(3);
cout<<b<<endl;
//2-D dynamic-size array
ArrayXXf c = ArrayXXf::Zero(3, 4);
cout<<c<<endl;
}
You can also try this code with Online C++ Compiler
Run Code

OUTPUT

code output

2. The next static method is Constant(value). It sets all the coefficients to the value given as a parameter in the method.

3.The next method is - Random() which fills the matrix with random matrix coefficients.

4. We have the Identity() method matrix to obtain the identity matrix. 

5.The method Linspaced(size, low, high) is available only for vectors and 1-D arrays. It forms a vector whose coefficients are equally spaced between low and high.

Usage as temporary objects

Methods like Zero() and Constant() return expression objects which evaluate to a matrix or an array so that the syntax does not incur any overhead.

These expressions can be used as temporary objects. Look at the following example:

 

#include<iostream>
#include<Eigen/Dense>
using namespace Eigen;
using namespace std;
int main(){
  MatrixXd m1= MatrixXd::Random(3,3);
  m1=m1(m1 + MatrixXd::Constant(3,3,1.2))*50;
  cout<<"m1:"<<m1<<endl;
  VectorXd vec(3);
  vec<<1,2,3;
  cout<<m1*vec<<endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

code output

m + MatrixXf::Constant(3,3,1.2) forms a 3X3 matrix expression with all its coefficients equal to 2.2 plus the corresponding coefficient of m.

The comma initializer can also be used for constructing temporary objects.

 

#include<iostream>
#include<Eigen/Dense>
using namespace Eigen;
using namespace std;
int main(){
  MatrixXf m = MatrixXf::Random(2, 3);
  m= (MatrixXf(2,2) << 1, 0, 1, 0).finished()*m;
  cout<<m<<endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

OUTPUT

code output

The finished() method helps to get the actual matrix object after the comma initialization of our temporary submatrix.

Frequently asked questions

What are the benefits of using Eigen?

Eigen is one easy-to-use open source c++ library for using linear algebra, matrices, etc. It is updated frequently and has simple API syntax. There are no storage issues; you can use either column-wise or row-wise.

What header do we use in Eigen-specific programs?

Eigen/Dense header is used.

Name the compilers which support Eigen?

The compilers that support Eigen are Intel C++ Compiler, MinGW, MSVC, GCC, etc.

Conclusion

We hope this blog helped you understand how to initialize the matrices. The blog introduces the Eigen and matrices to establish the concepts and concludes with the Advanced ways 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.

Check out this problem - Matrix Median

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