- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Python Program to check Involutory Matrix

In this article, we will learn a python program to check Involutory Matrix.

Assume we have taken an **input matrix**. We will now check whether the input matrix is an **Involutory Matrix** using the below methods.

### Methos Used

The following are the various methods to accomplish this task −

Using Nested For Loop

Using NumPy module

## What is an Involutory Matrix?

If a matrix multiplies by itself and gives the identity matrix, it is said to be an involutory matrix. The matrix that is its inverse is the **involutory matrix**.

If **A * A = I**, matrix A is referred to as an involutory matrix. Here the I represents the **Identity matrix**.

## Method 1: Using Nested For Loop

### Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

Create a variable to store the number of rows of a matrix.

Create a function

**multiplyMatrix()**to perform matrix multiplication by accepting the input matrixPerform matrix multiplication.

Use the

**return**statement to return the resultant matrix multiplication output.Create a function

**checkInvolutoryMat()**to check whether the input matrix is an**involuntary**matrix by accepting the input matrix as an argument.Create a

**rows*rows**size matrix with all elements as 0 and store this as a**outputMatrix**.Call the above

**multiplyMatrix()**function to perform matrix multiplication and save the result in the above outputMatrix Variable.Iterate through every element of the outputMatrix.

Check if the diagonal element is not 1 using if conditional statement then if it is true so return False i.e not an involuntary Matrix.

Check if the non-diagonal element is not 0 using if conditional statement then if it is true so return False i.e, not an involuntary Matrix.

Create a variable to store the

**input matrix**.Use the if conditional statement to check whether the above-defined

**checkInvolutoryMat()**function returns true by passing the input matrix as an argument.Print an

**involuntary matrix**if the condition is true.Else printing NOT involuntary matrix

### Example

The following program checks whether the input matrix is an involuntary matrix using the nested for loops −

# input no of rows rows = 3 # creating a function to perform matrix multiplication def multiplyMatrix(inputMatrix, output): # traversing through the rows of a matrix for p in range(rows): # traversing through all the columns of a current row for q in range(rows): # assuming the current element is 0 output[p][q] = 0 for x in range(rows): # performing matrix multiplication output[p][q] += inputMatrix[p][x] * inputMatrix[x][q] # returning the resultant matrix multiplication output return output # creating a function to check whether the input matrix # is an involuntary matrix by accepting matrix as an argument def checkInvolutoryMat(inputMatrix): # Creating a rows*rows size matrix with all elements as 0 output = [[0 for p in range(rows)] for q in range(rows)] # calling the above multiplyMatrix() function output = multiplyMatrix(inputMatrix, output) # Iterating in the rows of the output matrix for p in range(rows): # Iterating in the diagonals of the output matrix for q in range(rows): # If it is a diagonal element and if it is not 1 then return False(Not Involuntary) if (p == q and output[p][q] != 1): return False # If it is a non-diagonal element and if it is not 1 then return False(Not Involuntary) if (p != q and output[p][q] != 0): return False # It is an involuntary matrix so return True return True # input matrix inputMatrix = [[1, 0, 0], [0, -1, 0], [0, 0, -1]] # checking whether the above checkInvolutoryMat() function returns true # by passing the input matrix as an argument if (checkInvolutoryMat(inputMatrix)): # printing involuntary matrix if the condition is true print("The input matrix is an Involutory matrix") else: # else printing NOT involuntary matrix print("The input matrix is NOT an Involutory matrix")

### Output

The input matrix is an Involutory matrix

**Time Complexity** − O(N^3)

**Auxiliary Space** − O(N^2)

## Method 2: Using the NumPy module

Utilizing the numpy library is another method for determining whether the matrix is involutory. This can be achieved by comparing the matrix with its inverse using the **numpy.allclose()** function.

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

Use the import keyword to import the

**numpy**module.Create a function

**checkInvolutoryMat()**to check whether the input matrix is an**involuntary**matrix by accepting the input matrix as an argument.Use the

**numpy.linalg.inv()**function(calculates the inverse of a matrix) to get the inverse of an input matrix.Use the

**numpy.allclose()**function to check whether the input matrix is equal to its inverse by passing the input matrix and its inverse matrix as arguments and return the result.If two arrays are element-wise equal within a

**tolerance**, the**allclose()**function will return True.The

**tolerance**values are positive and frequently have very small values.

### Example

The following program checks whether the input matrix is an involuntary matrix using numpy.linalg.inv(), numpy.allclose() functions −

# using the numpy module import numpy as np # creating a function to check whether the input matrix # is an involuntary matrix by accepting matrix as an argument def checkInvolutoryMat(inputMatrix): # Getting the inverse of an input matrix using the numpy linalg module inverseMat = np.linalg.inv(inputMatrix) # checking whether the input matrix is equal to its inverse matrix # using the numpy.allclose() function and returning the result return np.allclose(inputMatrix, inverseMat) # input matrix inputMatrix = [[1, 0, 0], [0, -1, 0], [0, 0, -1]] # calling the checkInvolutoryMat() function by passing the input matrix as an argument # The output True indicates an INVOLUNTARY matrix print(checkInvolutoryMat(inputMatrix))

### Output

True

In the above example, the output **True** represents the given input matrix is an Involuntary matrix.

This method makes use of the numpy's optimized **linear algebra** routines and has the advantage of being shorter and simpler to comprehend. The time complexity of this approach is dependent on the complexity of the matrix inverse calculation, which is typically O(N^3) for dense matrices. The space complexity is O(N^2).

**Time Complexity** − O(N^3)

**Space Complexity** − O(N^2)

## Conclusion

In this article, we learned how to use two distinct ways to determine whether a given matrix is involuntary or not. We learned how to utilize the NumPy module's linalg.inv() function to get the inverse of a given matrix.

- Related Articles
- Program to check Involutory Matrix in C++
- Check if a Matrix is Involutory or not in Java?
- Program to check whether given matrix is Toeplitz Matrix or not in Python
- JavaScript Program to check idempotent matrix
- Program to check diagonal matrix and scalar matrix in C++
- Program to check idempotent matrix in C++
- Program to check some elements in matrix forms a cycle or not in python
- C++ Program to Check if a Matrix is Invertible
- JavaScript Program to Check if Matrix is Upper Triangular
- JavaScript program to check if a matrix is symmetric
- Program to check if a matrix is Binary matrix or not in C++
- Swift program to check if a given square matrix is an Identity Matrix
- Python Program to Convert Matrix to String
- Program to check words can be found in matrix character board or not in Python
- Python – Check Similar elements in Matrix rows