Open In App

Python Program for Identity Matrix

Last Updated : 22 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Introduction to Identity Matrix :

 The dictionary definition of an Identity Matrix is a square matrix in which all the elements of the principal or main diagonal are 1’s and all other elements are zeros. In the below image, every matrix is an Identity Matrix. 
 

In linear algebra, this is sometimes called as a Unit Matrix, of a square matrix (size = n x n) with ones on the main diagonal and zeros elsewhere. The identity matrix is denoted by “ I “. Sometimes U or E is also used to denote an Identity Matrix. 
A property of the identity matrix is that it leaves a matrix unchanged if it is multiplied by an Identity Matrix.

Examples:  

Input  : 2
Output : 1 0
         0 1

Input :  4
Output : 1 0 0 0
         0 1 0 0
         0 0 1 0
         0 0 0 1
The explanation is simple. We need to make all
the elements of principal or main diagonal as 
1 and everything else as 0.

Program to print Identity Matrix : 
The logic is simple. You need to the print 1 in those positions where row is equal to column of a matrix and make all other positions as 0. 

Python3




# Python code to print identity matrix
 
# Function to print identity matrix
def Identity(size):
    for row in range(0, size):
        for col in range(0, size):
 
            # Here end is used to stay in same line
            if (row == col):
                print("1 ", end=" ")
            else:
                print("0 ", end=" ")
        print()
 
# Driver Code       
size = 5
Identity(size)


Output: 

1  0  0  0  0  
0  1  0  0  0  
0  0  1  0  0  
0  0  0  1  0  
0  0  0  0  1  

Time complexity: O(R*C) where R and C is no of rows and column in matrix respectively

Space Complexity: O(1) as no extra space has been used.

Program to check if a given square matrix is Identity Matrix : 

Python3




# Python3 program to check
# if a given matrix is identity
MAX = 100;
def isIdentity(mat, N):
    for row in range(N):
        for col in range(N):
            if (row == col and
                mat[row][col] != 1):
                return False;
            elif (row != col and
                  mat[row][col] != 0):
                return False;
    return True;
 
# Driver Code
N = 4;
mat = [[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]];
if (isIdentity(mat, N)):
    print("Yes ");
else:
    print("No ");
 
# This code is contributed
# by mits


Output:

Yes

Time complexity: O(N2) where N is number of rows and columns of matrix

Auxiliary Space: O(1)

Using Numpy:

Note: Before running the code please install the Numpy library using the command below

pip install numpy

Use the numpy library to check if a matrix is an identity matrix. This can be done by using the numpy.allclose function to compare the matrix with the identity matrix of the same size.

For example, the following code snippet checks if a matrix is an identity matrix:

Python3




import numpy as np
 
def is_identity(matrix):
    # Get the size of the matrix
    size = matrix.shape[0]
     
    # Create the identity matrix of the same size
    identity = np.eye(size)
     
    # Check if the matrix is equal to the identity matrix using numpy.allclose
    return np.allclose(matrix, identity)
 
# Example usage
matrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
print(is_identity(matrix)) # prints True
#This code is contributed by Edula Vinay Kumar Reddy


Output: True

This approach has the advantage of being more concise and easier to read, and it also takes advantage of the optimized array operations provided by numpy. The time complexity of this approach will depend on the complexity of the numpy.allclose function, which is generally O(N^2) for dense matrices and space complexity is O(N^2)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads