How to check Idempotent Matrix in Python?

An idempotent matrix is a square matrix that produces the same result when multiplied by itself. Mathematically, a matrix M is idempotent if and only if M × M = M.

Understanding Idempotent Matrix

Consider this example matrix:

M = [1 0 0]
    [0 1 0]
    [0 0 0]

When we multiply M by itself:

[1 0 0]   [1 0 0]   [1 0 0]
[0 1 0] × [0 1 0] = [0 1 0]
[0 0 0]   [0 0 0]   [0 0 0]

Since M × M = M, this is an idempotent matrix.

Algorithm for Checking Idempotent Matrix

To check if a matrix is idempotent:

  1. Multiply the matrix by itself using matrix multiplication
  2. Compare the result with the original matrix
  3. If all corresponding elements are equal, the matrix is idempotent

Implementation

Here's a complete Python implementation to check if a matrix is idempotent:

def multiply_matrix(matrix1, matrix2):
    """Multiply two matrices and return the result"""
    rows = len(matrix1)
    result = [[0] * rows for _ in range(rows)]
    
    for i in range(rows):
        for j in range(rows):
            for k in range(rows):
                result[i][j] += matrix1[i][k] * matrix2[k][j]
    
    return result

def is_idempotent_matrix(matrix):
    """Check if a matrix is idempotent (M * M = M)"""
    # Multiply matrix by itself
    squared_matrix = multiply_matrix(matrix, matrix)
    
    # Compare original matrix with its square
    rows = len(matrix)
    for i in range(rows):
        for j in range(rows):
            if matrix[i][j] != squared_matrix[i][j]:
                return False
    
    return True

# Test with an idempotent matrix
matrix1 = [[2, -2, -4], 
           [-1, 3, 4], 
           [1, -2, -3]]

print("Matrix 1:")
for row in matrix1:
    print(row)

if is_idempotent_matrix(matrix1):
    print("Matrix 1 is an idempotent matrix")
else:
    print("Matrix 1 is NOT an idempotent matrix")

# Test with a non-idempotent matrix
matrix2 = [[1, 2], 
           [3, 4]]

print("\nMatrix 2:")
for row in matrix2:
    print(row)

if is_idempotent_matrix(matrix2):
    print("Matrix 2 is an idempotent matrix")
else:
    print("Matrix 2 is NOT an idempotent matrix")
Matrix 1:
[2, -2, -4]
[-1, 3, 4]
[1, -2, -3]
Matrix 1 is an idempotent matrix

Matrix 2:
[1, 2]
[3, 4]
Matrix 2 is NOT an idempotent matrix

Using NumPy for Matrix Operations

For larger matrices, NumPy provides more efficient matrix operations:

import numpy as np

def is_idempotent_numpy(matrix):
    """Check if a matrix is idempotent using NumPy"""
    matrix = np.array(matrix)
    squared_matrix = np.dot(matrix, matrix)
    return np.allclose(matrix, squared_matrix)

# Test with NumPy
matrix = [[1, 0, 0],
          [0, 1, 0],
          [0, 0, 0]]

print("Testing with NumPy:")
print("Matrix:")
print(np.array(matrix))

if is_idempotent_numpy(matrix):
    print("This is an idempotent matrix")
else:
    print("This is NOT an idempotent matrix")
Testing with NumPy:
Matrix:
[[1 0 0]
 [0 1 0]
 [0 0 0]]
This is an idempotent matrix

Key Properties

Property Description
Definition M² = M
Matrix Type Must be square
Common Examples Identity matrix, zero matrix, projection matrices

Conclusion

An idempotent matrix satisfies the condition M × M = M. You can check this property by multiplying the matrix by itself and comparing the result with the original matrix. NumPy provides efficient tools for matrix operations when working with larger datasets.

Updated on: 2026-03-27T12:50:12+05:30

299 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements