Check if a matrix is symmetric using Python


In this article, we will learn a python program to check if a matrix is symmetric.

What is a Symmetric Matrix?

If the transpose of a square matrix matches the original matrix, the matrix is said to be symmetric. By changing from row to column and from column to row, the symmetric matrix may be obtained.

For Example

2 6 8
6 1 3
8 3 9     

Assume we have taken an NxN input matrix. We will now check whether the matrix is symmetric or not using the below methods.

Methos Used

The following are the various methods to accomplish this task

  • Using Nested For loops

  • Efficient solution Without Transposing Matrix

  • Using the List Comprehension

Method 1: Using Nested For Loops

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Create a function transposeMatrix() to get the transpose of a matrix.

  • Use the for loop, to traverse through the rows of a matrix.

  • Use another nested for loop, to traverse through the columns of a current row.

  • Get the transpose of an input matrix i.e, exchange the rows and cols.

  • Create a function checkingSymmetric() that returns true if the matrix is symmetric else false by accepting the input matrix, no of rows as arguments

  • Call the above transposeMatrix() function to get the transpose of a matrix.

  • Traverse through the matrix

  • Use the if conditional statement to check whether the current element is not equal transpose matrix element.

  • Return False if the above condition is true.

  • If no False is returned in the above-nested loops then it is symmetric hence return True.

  • Create a variable to store the input matrix.

  • Call the above checkingSymmetric() function and check whether the function returns true using the if conditional statement by passing the input matrix and no of rows as arguments.

  • Print "Symmetric matrix" if the condition is true i.e if the function returns true.

  • Else printing “NOT a Symmetric matrix”.

Example

The following program checks whether the input matrix is Symmetric or not using nested For loops −

# creating a function for getting the transpose of a matrix
def transposeMatrix(inputMatrix, t, rows):
   # traversing through the rows of a matrix
      for p in range(rows):
         # traversing the columns of a current row
         for q in range(rows):
            # transposing the matrix i.e, exchange the rows and cols
               t[p][q] = inputMatrix[q][p]
# creating a function that returns true if the matrix is symmetric
# else false by accepting the input matrix, no of rows as arguments
def checkingSymmetric(inputMatrix, rows):
   # Creating the new matrix with all 0 values
      t = [[0 for q in range(len(inputMatrix[0]))]
         for p in range(len(inputMatrix))]
      # calling the above transposeMatrix() function to transpose the given matrix
      transposeMatrix(inputMatrix, t, rows)
   # traversing through the rows of a matrix
      for p in range(rows):
         # traversing the columns of a current row
         for q in range(rows):
            # checking whether the current element is not equal transpose matrix element
               if (inputMatrix[p][q] != t[p][q]):
                  # returning False if the condition is true
                  return False
   # else returning True
      return True
# input matrix
inputMatrix = [[6, 3, 5], [3, 2, 4], [5, 4, 6]]
# checking whether above defined checkingSymmetric() function returns true
# by calling it by passing input matrix and no of rows as arguments
if (checkingSymmetric(inputMatrix, 3)):
   # printing "Symmetric matrix" if the function returns true
   print("Input matrix is a Symmetric matrix")
else:
   # else printing NOT a Symmetric matrix
   print("Input matrix is NOT a Symmetric matrix")

Output

On execution, the above program will generate the following output −

Input matrix is a Symmetric matrix

Time Complexity − O(N x N)

Auxiliary Space − O(N x N)

Method 2: Efficient Solution Without Transposing Matrix

To quickly determine whether a matrix is symmetric or not, compare its elements without transposing them. In This method, we will compare matrix[i][j] and matrix[j][i].

Example

The following program checks whether the input matrix is Symmetric or not using comparison −

# creating a function that returns true if the matrix is symmetric
# else false by accepting the input matrix, no of rows as arguments
def checkingSymmetric(inputMatrix, rows):
   # traversing through the rows of a matrix
      for p in range(rows):
         # traversing the columns of a current row
            for q in range(rows):
               # checking whether the current element is not equal to its transpose
                  if (inputMatrix[p][q] != inputMatrix[q][p]):
                     # returning False if the condition is true
                     return False
   # else returning True
      return True
# input matrix
inputMatrix = [[6, 3, 5], [3, 2, 4], [5, 4, 6]]
if (checkingSymmetric(inputMatrix, 3)):
   print("Input matrix is a Symmetric matrix")
else:
   print("Input matrix is NOT a Symmetric matrix")

Output

Input matrix is a Symmetric matrix

Time Complexity − O(N x N)

Auxiliary Space − O(1)

Method 3: Using the List Comprehension

Example

The following program checks whether the input matrix is Symmetric or not using List Comprehension −

# creating a function that returns true if the matrix is symmetric
# else false by accepting the input matrix, no of rows as arguments
def checkingSymmetric(inputMatrix, rows):
   # getting transpose of a matrix
   transpose_matrix = [[inputMatrix[q][p]
      for q in range(rows)] for p in range(rows)]
   # checking whether the input matrix is not equal to the transpose matrix
   if(inputMatrix == transpose_matrix):
      return True
   return False
# input matrix
inputMatrix = [[6, 3, 5], [3, 2, 4], [5, 4, 6]]
if (checkingSymmetric(inputMatrix, 3)):
   print("Input matrix is a Symmetric matrix")
else:
   print("Input matrix is NOT a Symmetric matrix")

Output

Input matrix is a Symmetric matrix

Time Complexity − O(N*N)

Auxiliary Space − O(N*N)

Conclusion

In this article, we first learned what a symmetric matrix is, and then we learned how to use 3 different ways to implement a program to determine whether a given matrix is symmetric or not. Additionally, we learned an efficient method to determine whether a given matrix is symmetric without transposing it, which saves space and lowers the space complexity to O(1).

Updated on: 23-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements