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.

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.

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

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

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.

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

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

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

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-