Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Matrix
3.
What Are Eigenvalues
4.
Matrix Operations
4.1.
Addition and Subtraction
4.2.
Scalar Multiplication and Division
4.3.
Transposition and Conjugation
4.4.
Matrix-Matrix and Matrix-Vector Multiplication
4.5.
Dot Product and Cross Product
4.6.
Basic arithmetic reduction operations
5.
Frequently Asked Questions
5.1.
Explain the Cofactor Term.
5.2.
What are the steps to find the adjoint of the matrix?
5.3.
What is the function of trace()?
5.4.
What is the Conjugate of the Matrix?
5.5.
Differentiate between minCoeff() and maxCoeff().
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

What are the Different Matrix Operations

Author Geetika Dua
0 upvote

Introduction

Are you a mathematics enthusiast? Do you know what a matrix is, and are you curious about different operations in the matrix? 
If yes, then let us explore the concept of eigenvalues in a matrix and perform different matrix operations.

Introduction

Keep reading!

What is a Matrix

A matrix is an ordered rectangular arrangement of numbers (real or complex) or functions. A matrix that has ‘m’ rows and ‘n’ columns is called a matrix of order m x n.

matrix

The above picture shows an example of a 3 x 2 Matrix.

What Are Eigenvalues

Eigenvalues are a unique set of scalars related to a linear system of equations or matrix equations. We can perform matrix operations through familiar C++ arithmetic operators such as +, -, * or methods such as dot(), cross(), etc.

The basic equation is:

Ax = mx

The number or scalar value “m” is an eigenvalue of A.

Matrix Operations

Let us study various matrix operations one by one.

Addition and Subtraction

There are specific rules for addition and subtraction. 

  • The left and right sides must have the same rows and columns.
  • They must have the same scalar type.
     

The operators allowed here are listed below-

  • binary operator (a+b) and (a-b)
  • unary operator (-a)
  • compound operator (a+=b) or (a-=b)
     

Example:

#include <iostream>
#include <Eigen/Dense>
Using namespace std;
int main()
{   
	// Define two matrices mata and matb (2x2) 
	Eigen::Matrix2d mata;
	mata << 10, 20,
        	30, 40;
	Eigen::MatrixXd matb(2, 2);	
	matb << 20, 30,
        	10, 40;
        	
	// Perform addition, subtraction and compound operator on them.
	cout << "mata + matb =\n" << mata + matb << endl;
	cout << "mata - matb =\n" << mata - matb << endl;
	cout << " mata += matb;" << endl;
	mata += matb;
	cout << "Now mata =\n" << mata << endl;
	Eigen::Vector3d v(1, 2, 3);
	Eigen::Vector3d w(1, 0, 0);
   
}
You can also try this code with Online C++ Compiler
Run Code


Input

input

Output

output

In this output above, we see that mata+matb performs the addition,mata-matb performs the subtraction, while mata+=matb, which means mata=mata+matb. It adds mata and matb and assigns this value to the mata matrix.

Scalar Multiplication and Division

Multiplying or dividing a scalar is quite simple. The matrix operations that we can perform are-

  • binary operator *  (matrix*scalar or scalar*matrix)
  • binary operator / ( matrix/scalar)
  • compound operator *= (matrix*=scalar)
  • compound operator /= (matrix/=scalar)
     

Example:

#include <iostream>
#include <Eigen/Dense>
Using namespace std;
int main()
{ 
	// First define a 2x2 Matrix.
	Eigen::Matrix2d cn;
	cn << 10, 2, 3, 14;
    	
	// Here we define a vector using Eigen.
	Eigen::Vector3d v(7, 2, 13);
    	
	// Matrix with scalar
	cout << "cn * 2 =\n" << cn * 2 << endl;
    	
	// Scalar with vector
	cout << "0.12 * v =\n" << 0.12 * v << endl;
	cout << "Doing v *= 3;" << endl;
	v *= 2;
	cout << "Now v =\n" << v << endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

Input

input

Output

output

In the above output, we have tried to perform scalar multiplication, which multiplies the scalar by a vector and a scaler by a matrix.

For example, 0.12 * (7, 2, 13) = 0.84, 0,24, 0.56.

Transposition and Conjugation

Predefined matrix operations are available to perform a matrix's Transpose, conjugate, and adjoint. 

Transpose- We can obtain the transpose of a matrix by changing its rows into columns or vice versa.

Transpose Matrix

Conjugate- If we replace the elements of a matrix with its corresponding complex conjugate numbers, we obtain the conjugate of a matrix.

Conjugate Matrix

Adjoint- The transpose of the cofactor matrix is the adjoint of the matrix.

Adjoint Matrix

The Pseudo Code for the same would be

MatrixXcf a = MatrixXcf::Random(2,2);
cout << Matrix" << a << endl;
cout << "Transpose" << a.transpose() << endl;
cout << "Conjugate" << a.conjugate() << endl;
cout << "Adjoint" << a.adjoint() << endl;
You can also try this code with Online C++ Compiler
Run Code


If we look at the above code, the random function generates the matrix with random elements of size 2x2. Then, transpose, adjoint, and conjugate functions are performed.

example

Matrix-Matrix and Matrix-Vector Multiplication

The Multiplication of the Matrix-matrix is done with operator*. We mainly have two operators to handle this case.

  • binary operator * (a*b)
  • compound operator *= (a*=b)
     

Example:

#include <iostream>
#include <Eigen/Dense>
Using namespace std;
int main()
{ 
	// First define a 2x2 matrix.
	Eigen::Matrix2d cn;
	cn << 3, 2, 1, 5;
   	
	// Here we define two vectors u and v
	Eigen::Vector2d u(-1, 1), v(2, 0);
	cout << "Here is cn*cn:\n" << cn * cn << endl;
	cout << "Here is cn*u:\n" << cn * u << endl;
	cout << "Here is u^T*cn:\n" << u.transpose() * cn << endl;
	cout << "Here is u^T*v:\n" << u.transpose() * v << endl;
	cout << "Here is u*v^T:\n" << u * v.transpose() << endl;
  
}
You can also try this code with Online C++ Compiler
Run Code


Input

input

Output

output

Here in the above output, we perform the matrix to matrix multiplication and matrix-to-vector multiplication.

Dot Product and Cross Product

We have the functions for performing the dot and cross matrix operation. We need to use dot() and cross() methods for performing dot and cross product. Let us look at the implementation of the same. 

#include <iostream>
#include <Eigen/Dense>
Using namespace std;
int main()
{ 
	// We shall define 2 matrices cn1 and cn2 
	Eigen::Vector3d cn1(4, 2, 7); 
	Eigen::Vector3d cn2(0, 9, 1);

	// dot() function gives the dot product.
	cout << "Dot product: " << cn1.dot(cn2) << endl;

	/* 
	The method to find a dot product using the matrix product is 
	done by finding the adjoint.
	*/
	double dp = cn1.adjoint() * cn2; 
	cout << "Dot product using matrix product " << dp << endl;

	// The cross() method generates the cross product of the matrices.
	cout << "Cross product:\n" << cn1.cross(cn2) << endl;
}
You can also try this code with Online C++ Compiler
Run Code


Input

input

Output

output

The above output generates dot and cross product of matrices using inbuilt function of dot and cross.

Basic arithmetic reduction operations

There is also some predefined reduction operations present. They boil down the value of the matrix to a single value. We shall understand this with the help of a code.

#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main()
{
	// This generates a 2x2 matrix named cn.
	Eigen::Matrix2d cn; 

	// This is how we define the matrix.
	cn << 10, 20, 30, 40;
	cout << "The result of cn.sum():       " << cn.sum()       << endl;
	cout << "The result of cn.prod():      " << cn.prod()      << endl;
	cout << "The result of cn.mean():      " << cn.mean()      << endl;
	cout << "The result of cn.minCoeff():  " << cn.minCoeff()  << endl;
	cout << "The result of cn.maxCoeff():  " << cn.maxCoeff()  << endl;
	cout << "The result of cn.trace():     " << cn.trace()     << endl;
}
You can also try this code with Online C++ Compiler
Run Code


Input 

input

Output

output

We can obtain the sum, product, mean, trace, minimum and maximum coefficient as shown in the output above. There are inbuilt functions present in eigen library to support them.

Frequently Asked Questions

Explain the Cofactor Term.

We obtain the cofactor by removing the row and column of a specific element that looks like a  square or rectangle. The sign of the cofactor term depends on the position of the component.

What are the steps to find the adjoint of the matrix?

To find the adjoint of the matrix, we first need to find the cofactor, then transpose it by transposing; we mean assigning rows to columns and vice versa.

What is the function of trace()?

This is a predefined method in the Eigen library that supports the calculation of trace. The sum of its diagonal elements is called the trace of a matrix.

What is the Conjugate of the Matrix?

If we replace the elements of a matrix with their corresponding complex conjugate numbers, we obtain the conjugate of a matrix.

Differentiate between minCoeff() and maxCoeff().

minCoeff() function lists the most negligible value of the coefficients in the matrix, while the maxCoeff() displays the maximum value among the coefficients present in the matrix.

Conclusion

This blog studied various matrix operations. We hope this blog proved to be insightful and you got clarity on this topic. Need more information on related topics? We got you covered! Refer to these blogs-

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass