• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
December 21, 2022 |3.2K Views

Python Program to Check Identity Matrix

  Share  5 Likes
Description
Discussion

What is an Identity Matrix?

A matrix whose all the non-diagonal elements are zero and the diagonal elements are equal to 1. 

In this video, we are going to learn how can we implement a Python Program to check whether a matrix is an Identity or not. 

The logic behind the program that we will implement to identify an identity matrix is that if i==j then if the element corresponding to that index is not equal to 1 then we return false and if i is not equal to j then we check if the element corresponding to these elements is 0 or not if it's not zero then we return false. Here i and j are the indexes for rows and columns of the matrix. 

The approach we are following here will need three nested loops running from 0 to n hence, the time complexity of this algorithm will be O(N^2). And to store the results obtained from each multiplication we are using another temp matrix to store the results for the final comparison hence we are using extra space of the same size hence space complexity of the algorithm will be O(1). 

Time Complexity - O(N^1) 
Space Complexity - O(1) 

Finally, we just need to check if the diagonal elements are equal to one and if non-diagonal elements must be equal to zero if the above two conditions are satisfied then we can say that the matrix is Identity. 

Python program for Identity Matrix:
https://www.geeksforgeeks.org/python-program-for-identity-matrix/