Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Involutory Matrix
2.1.
Example
3.
Algorithm
4.
Program
4.1.
C++ Code
4.1.1.
Output 
4.2.
Java Code
4.2.1.
Output
5.
Frequently Asked Questions
5.1.
Is it possible to tell if a matrix is idempotent?
5.2.
Is it true that the matrix is orthogonal?
5.3.
Is it true that a null matrix is nilpotent?
5.4.
What is the involutory matrix's inverse?
6.
Conclusion
Last Updated: Mar 27, 2024

Program to Check Involutory Matrix

Author SHIVANGI MALL
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction 

"Check Involutory Matrix" is a simple problem of a matrix where we need to check whether a given matrix is an involutory matrix or not.

Matrices can be divided into three categories:

  1. Identity Matrix: The term "identity matrix" refers to a type of matrix if it is a square matrix of size N*N with all diagonal elements set to 1 and all other diagonal elements set to 0.
  2. Idempotent Matrix: An Idempotent matrix is a matrix that, when multiplied by itself, produces the same matrix.
  3. Involutory Matrix: An involutory matrix is a matrix that, when multiplied by itself, gives an identity matrix.

 

In this article with an example, an algorithm, and a program, we will learn how to check whether a matrix is an involutory matrix or not.

Involutory Matrix

If and only if a matrix is multiplied by itself and the result is an identity matrix, it is called an involutory matrix. A matrix I is called an identity matrix if and only if its main diagonal is one and all other members are zero. So, if and only if M*M=I, where is some matrix, and I is an Identity Matrix, we can call a matrix an Involutory Matrix.
 

Example

Input: mat[N][N] = { 
{ 1, 0, 0},
{0, -1, 0},
{0,  0, -1}}
Output : Involutory Matrix

Input: mat[N][N] = { 
{ 3, 0, 0},
{0, 2, 0},
{0,  0, 3} }
Output : Not Involutory Matrix

Input: mat[N][N] = { 
{1, 0, 0},
{0, 1, 0},
{0,  0, 1} }
Output : Involutory Matrix
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Algorithm

Start

Step 1 -> define macro as #define size 3

Step 2 -> declare function for matrix multiplication.

   void multiplyMatrix(int arr[][size], int res[][size])

      Loop For int i = 0 and i < size and i++

         Loop For int j = 0 and j < size and j++

            Set res[i][j] = 0

            Loop For int k = 0 and k < size and k++

                 res[i][j] = res[i][j] +( arr[i][k] * arr[k][j])

            End

         End

   End

Step 3 -> declare function to check involutory matrix or not

   bool check(int arr[size][size])

   declare int res[size][size]

   Call multiply(arr, res)

   Loop For int i = 0 and i < size and i++

      Loop For int j = 0 and j < size and j++

         IF (i == j && res[i][j] != 1)

            return false

         End

         If (i != j && res[i][j] != 0)

            return false

         End

      End

   End

   Return true

Step 4 -> In main()

   Declare a matrix int arr[size][size] = { { 1, 0, 0 },

      { 0, -1, 0 },

      { 0, 0, -1 } }

   If (check(arr))

      Print its an involutory matrix

   Else

      Print its not an involutory matrix

Stop

Program

Now we are going to see programs to Check Involutory Matrix. We will implement it in C++ and Java language by using the algorithm mentioned above. Below is the program to Check Involutory Matrix is as follows:

C++ Code

// Program to check involutory matrix.
#include <bits/stdc++.h>
#define N 3
using namespace std;

// Function to multiply matrix
void multiply(int mat[][N], int mult[][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
mult[i][j] = 0;
for (int k = 0; k < N; k++)
mult[i][j] += mat[i][k] * mat[k][j];
}
}
}

// Function to check involutory matrix.
bool InvolutoryMatrix(int mat[N][N])
{
int res[N][N];

// multiply function call.
multiply(mat, res);

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j && res[i][j] != 1)
return false;
if (i != j && res[i][j] != 0)
return false;
}
}
return true;
}

// Driver function.
int main()
{
int mat[N][N] = { { 1, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, -1 } };

// Function call. If function return
// true then if part will execute otherwise
// else part will execute.
if (InvolutoryMatrix(mat))
cout << "Given matrix is an involutory Matrix";
else
cout << "Given matrix is not an involutory Matrix";

return 0;

} 

Output 

Given matrix is an involutory Matrix

Java Code

// Java Program to implement
// involutory matrix.
import java.io.*;
class CodingNinjas {

static int N = 3;

// Function for matrix multiplication.
static void multiply(int mat[][], int res[][])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
res[i][j] = 0;
for (int k = 0; k < N; k++)
res[i][j] += mat[i][k] * mat[k][j];
}
}
}

// Function to check involutory matrix.
static boolean InvolutoryMatrix(int mat[][])
{
int res[][] = new int[N][N];

// multiply function call.
multiply(mat, res);

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j && res[i][j] != 1)
return false;
if (i != j && res[i][j] != 0)
return false;
}
}
return true;
}

// Driver function.
public static void main (String[] args)
{

int mat[][] = { { 1, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, -1 } };

// Function call. If function return
// true then if part will execute
// otherwise else part will execute.
if (InvolutoryMatrix(mat))
System.out.println ( "Given matrix is an involutory Matrix");
else
System.out.println ( "Given matrix is not an involutory Matrix");
}
}

Output

Given matrix is an involutory Matrix

Time Complexity: Time complexity is O(n3) where is the size of the square matrix.

Also see,  Rabin Karp Algorithm

Frequently Asked Questions

Is it possible to tell if a matrix is idempotent?

If and only if the matrix 'M' multiplied by itself returns the same matrix 'M,' i.e. M * M = M, a matrix 'M' is called an idempotent matrix.

Is it true that the matrix is orthogonal?

If the transpose of a square matrix with real numbers or elements equals the inverse matrix, the matrix is said to be orthogonal.

Is it true that a null matrix is nilpotent?

A nilpotent matrix is a square matrix in which the product of the matrix and itself is a null matrix.

What is the involutory matrix's inverse?

An involutory matrix is a square matrix that is its own inverse in mathematics. That is, if and only if A^2 = I, where I is the n*n identity matrix, multiplication by the matrix A is an involution.

Conclusion

This article extensively discussed how to check whether a matrix is an involutory matrix or not.

We hope you have learned something new from this blog. And if you're interested in learning more, see our posts on Operations On MatricesConstruct a Linked List from a 2D MatrixMatrix Chain MultiplicationTypes of Matrix. Please vote for our blog to help other ninjas succeed.

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!
 

Previous article
Squares of Diagonals of a Matrix
Next article
Program to Check Idempotent Matrix
Live masterclass