Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to check Involutory Matrix
In this article, we will learn a Python program to check whether a matrix is an Involutory Matrix. An involutory matrix is a special type of square matrix that is its own inverse.
What is an Involutory Matrix?
An involutory matrix is a square matrix that, when multiplied by itself, gives the identity matrix. In mathematical terms, if A * A = I, then matrix A is called an involutory matrix, where I represents the Identity matrix.
Methods Used
The following are the various methods to accomplish this task
Using Nested For Loop
Using NumPy module
Method 1: Using Nested For Loop
This approach manually performs matrix multiplication and checks if the result equals the identity matrix ?
# Function to perform matrix multiplication
def multiplyMatrix(inputMatrix, rows):
# Create result matrix
result = [[0 for _ in range(rows)] for _ in range(rows)]
# Perform matrix multiplication
for i in range(rows):
for j in range(rows):
for k in range(rows):
result[i][j] += inputMatrix[i][k] * inputMatrix[k][j]
return result
# Function to check if matrix is involutory
def checkInvolutoryMatrix(inputMatrix):
rows = len(inputMatrix)
# Multiply matrix by itself
result = multiplyMatrix(inputMatrix, rows)
# Check if result is identity matrix
for i in range(rows):
for j in range(rows):
# Diagonal elements should be 1
if i == j and result[i][j] != 1:
return False
# Non-diagonal elements should be 0
if i != j and result[i][j] != 0:
return False
return True
# Test with an involutory matrix
inputMatrix = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
print("Input Matrix:")
for row in inputMatrix:
print(row)
if checkInvolutoryMatrix(inputMatrix):
print("The input matrix is an Involutory matrix")
else:
print("The input matrix is NOT an Involutory matrix")
Input Matrix: [1, 0, 0] [0, -1, 0] [0, 0, -1] The input matrix is an Involutory matrix
Time Complexity: O(N³) due to three nested loops for matrix multiplication
Space Complexity: O(N²) for storing the result matrix
Method 2: Using NumPy Module
This method uses NumPy's optimized linear algebra functions to compute the matrix inverse and compare it with the original matrix ?
import numpy as np
def checkInvolutoryMatrix(inputMatrix):
# Convert to numpy array
matrix = np.array(inputMatrix)
# Calculate the inverse of the matrix
try:
inverseMatrix = np.linalg.inv(matrix)
# Check if matrix equals its inverse (within tolerance)
return np.allclose(matrix, inverseMatrix)
except np.linalg.LinAlgError:
# Matrix is singular (not invertible)
return False
# Test with different matrices
inputMatrix1 = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
inputMatrix2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("Matrix 1:", inputMatrix1)
print("Is Involutory:", checkInvolutoryMatrix(inputMatrix1))
print("\nMatrix 2:", inputMatrix2)
print("Is Involutory:", checkInvolutoryMatrix(inputMatrix2))
Matrix 1: [[1, 0, 0], [0, -1, 0], [0, 0, -1]] Is Involutory: True Matrix 2: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Is Involutory: False
Time Complexity: O(N³) dominated by matrix inverse calculation
Space Complexity: O(N²) for storing the inverse matrix
Comparison
| Method | Pros | Cons | Best For |
|---|---|---|---|
| Nested For Loop | No external dependencies, educational | More code, manual implementation | Learning matrix operations |
| NumPy | Optimized, handles edge cases, concise | Requires NumPy library | Production code, larger matrices |
Conclusion
We explored two methods to check involutory matrices: manual matrix multiplication and NumPy's inverse calculation. The NumPy approach is more robust and efficient for practical applications, while the manual method provides better understanding of the underlying mathematics.
