# How to check Involutory Matrix in C++

Hello, in this tutorial, we will learn how to **check whether a matrix is an involuntary matrix or not in C++** with an example, Algorithm, and a program.

A matrix is said to be an involutory matrix if the multiplication of that matrix with itself results an identity matrix. That is a matrix **A** is said to be an Involutory matrix if and only if **A*A = I**. where **I** is an identity matrix.

**Note: **it is important that an involutory matrix should be a square matrix. Because for the multiplication of two matrices the size of the row of the first matrix should be equal to the size of the column of the second matrix. And vice-versa. so, for multiplication of a matrix with itself, it is only possible when a matrix is a square matrix.

## Check if a matrix is an Involutory matrix or not in C++

let’s take an example and solve it manually to understand the procedure to check the involutory matrix.

**Example: **

Consider a matrix of 3*3 input matrix: 0 4 3 1 -3 -3 -1 4 4 multiplying the input matrix with itself: | 0 4 3 | | 0 4 3 | | 1 -3 -3 | x | 1 -3 -3 | |-1 4 4 | |-1 4 4 | |0*0+4*1+3*(-1) 4*0+4*(-3)+3*4 3*0+4*(-3)+3*4 | = |1*0+(-3)*1+(-3)(-1) 4*1+(-3)(-3)+4(-3) 3*1+(-3)(-3)+4(-3) | |0*(-1)+4(1)+4(-1) (-1)(4)+4(-3)+4(4) 3(-1)+(-3)(4)+(4) | |0+4-3 0-12+12 0-12+12| = |0-3+3 4+9-12 3+9-12 | |0+4-1 4+9-12 3+9-12 | |1 0 0| = |0 1 0| |0 0 1| since, the multiplication of matrix with itself results the same matrix, Therefore, The matrix is an involutory matrix.

**Approach: **

First, we will multiply the input matrix with itself and save to multiplication in another matrix. Then we will check all the elements of the primary diagonal is one or not. if not then the matrix is not an involutory matrix. else, if all the elements of the primary diagonal are one then we will check whether remaining elements are zero or not. if all remaining elements are zero then the matrix is involutory. otherwise, the matrix is not an involutory matrix.

## Algorithm to check Involutory matrix

- declare and initialize a matrix (input matrix) of size
**n*m**. (n and m are the lengths of row and column of matrix) - check the equality of
**n**and**m**. if equal then proceed for nest steps, else exit the program. - declare another matrix of the same size as the input matrix, perform multiplication of the input matrix with itself and save the multiplication to another matrix declared before. (consider another matrix as a resultant matrix).
- Check whether all the primary diagonal elements of the resultant matrix is one or not. if not, then return 0. Else proceed for the next steps.
- check whether the remaining element of the matrix is zero. if not, then return 0, otherwise, return 1.
- if the function returns zero, then the matrix is not an involutory matrix. And if the function returns one, then the matrix is an involutory matrix.

## C++ program to check Involutory matrix

#include<bits/stdc++.h> using namespace std; //Function for matrix multiplication. void multiply(int matrix[50][50], int mult[50][50], int n, int m) { 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] += matrix[i][k] * matrix[k][j]; } } } //Function to check involutory matrix int isInvolutoryMatrix(int matrix[50][50], int n, int m) { int mult[50][50]; //calling function multiply multiply(matrix, mult, n, m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i == j && mult[i][j] != 1) return 0; if (i != j && mult[i][j] != 0) return 0; } } return 1; } int main(){ int matrix[50][50], n, m; //taking length of row and column as input in n and m cout<<"Enter the length of row of the matrix: "; cin >> m; cout<<"Enter the length of column of the matrix: "; cin >> n; //checking for the square matrix if(n != m){ cout<<"Length of row and column should be equal."; exit(0); } //taking input in the matrix cout << "\nInput the element of matrix: \n"; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) cin>>matrix[i][j]; if (isInvolutoryMatrix(matrix, n, m)) cout << "\nGiven matrix is an involutory Matrix"; else cout << "\nGiven matrix is not an involutory Matrix."; return 0; }

**Output: **

Enter the length of row of the matrix: 3 Enter the length of column of the matrix: 3 Input the element of matrix: 0 4 3 1 -3 -3 -1 4 4 Given matrix is an involutory Matrix.

**Time Complexity: **O(n^3) where **n **is the size of the square matrix.

**you may also read: **

## Leave a Reply