How to check Idempotent Matrix in Python?


If a matrix produces the same matrix when multiplied by itself, it is said to be an idempotent matrix. If and only if M * M = M, the matrix M is considered to be an idempotent matrix. Where M is a square matrix in the idempotent matrix.

Matrix M:

1 0 0
0 1 0
0 0 0

Performing M*M

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

As the result is M so it is idempotent matrix

Assume we have taken an M*M input matrix in Python. In this article We will learn how check whether the input matrix is an Idempotent Matrix using the Nested For loop method.

Algorithm (Steps)

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

  • Use the import keyword to import the math module.

  • The function multiplyMatrix(inputMatrix, output) is defined to perform matrix multiplication. It takes the input matrix and an empty output matrix as arguments.

  • The variable rows is assigned the length of the input matrix, which represents the number of rows.

  • Two nested for loops iterate over the rows and columns of the input matrix.

  • The output matrix element at position [p][q] is initialized to 0.

  • Another nested for loop iterates over the range of rows, performing the matrix multiplication by summing the product of corresponding elements from the input matrix.

  • The function isIdempotentMat(inputMatrix) is defined to check if the input matrix is an Idempotent Matrix. It takes the input matrix as an argument.

  • The variable output is created as a zero matrix with dimensions rows x rows.

  • The multiplyMatrix() function is called with the input matrix and output matrix as arguments, resulting in the matrix multiplication being performed and stored in the output matrix.

  • Two nested for loops iterate over the rows and columns of the input matrix.

  • The current element of the input matrix is compared with the corresponding element in the output matrix. If they are not equal, False is returned, indicating that the matrix is not idempotent.

  • If all elements are equal, True is returned, indicating that the matrix is idempotent.

  • The input matrix inputMatrix is defined.

  • The isIdempotentMat() function is called with inputMatrix as an argument to check if it is an Idempotent Matrix.

  • If the function returns True, "The input matrix is an Idempotent Matrix" is printed. Otherwise, "The input matrix is NOT an Idempotent Matrix" is printed.

Example

The following example contains two functions for matrix multiplication and checking the Idempotent Matrix property. The multiplyMatrix() function iterates through each element of the input matrix, initializes the corresponding element in the output matrix to zero, and performs matrix multiplication by summing the product of corresponding elements from the input matrix. The isIdempotentMat() function creates an output matrix by calling multiplyMatrix(), compares each element of the input matrix with the corresponding element in the output matrix, and returns False if any element differs. If all elements match, it returns True. By utilizing these functions, the code allows users to perform matrix multiplication and determine whether a given matrix satisfies the criteria of an Idempotent Matrix.

# importing math module with an alias name
import math
# creating a function to perform matrix multiplication
def multiplyMatrix(inputMatrix, output):
    #  getting len of matrix i.e, rows
    rows = len(inputMatrix)
    # traversing through each row of a matrix
    for p in range(0, rows):
        # traversing through all the columns of a current row
        for q in range(0, rows):
            # assuming the current element as 0
            output[p][q] = 0
            # performing matrix multiplication
            for x in range(0, rows):
                output[p][q] += inputMatrix[p][x] * inputMatrix[x][q]


# creating a function to check whether the input matrix
# is Idempotent Matrix by accepting the input matrix as an argument
def isIdempotentMat(inputMatrix):
    #  getting len of matrix i.e, rows
    rows = len(inputMatrix)
    # Creating a zero matrix of given length
    output = [[0] * rows for p in range(0, rows)]
    # calling the above multiplyMatrix() function
    multiplyMatrix(inputMatrix, output)
    # traversing through each row of a matrix
    for p in range(0, rows):
        # traversing through all the columns of a current row
        for q in range(0, rows):
            # Checking whether the current element is not equal to ouput matrix element
            if inputMatrix[p][q] != output[p][q]:
                # retutning false if the condition is satisfied so it is not idempotent
                return False
        # else returning true
    return True


# input matrix
inputMatrix = [[2, -2, -4], [-1, 3, 4], [1, -2, -3]]
# calling the above defined isIdempotentMat() function by passing
# input matrix to it and checking whether the function returns true
if isIdempotentMat(inputMatrix):
    # printing Idempotent Matrix if the function returns true
    print("The input matrix is an Idempotent Matrix")
else:
    # else printing Not Idempotent Matrix
    print("The input matrix is NOT an Idempotent Matrix")

Output

The input matrix is an Idempotent Matrix

Conclusion

To summarize, this article provided guidance on checking for an Idempotent Matrix using Python. The step-by-step explanations and code examples offer a systematic approach to handle matrix manipulations and validate the Idempotent Matrix property in Python. This article serves as a valuable resource for individuals interested in implementing matrix operations and exploring matrix properties within the Python programming language. By applying the knowledge gained from this article, developers can confidently work with matrices, conduct matrix operations, and assess whether a given matrix meets the criteria of an Idempotent Matrix, thus strengthening their grasp of linear algebra concepts in Python.

Updated on: 17-Aug-2023

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements