使用Python檢查矩陣是否對稱


在本文中,我們將學習一個Python程式,用於檢查矩陣是否對稱。

什麼是對稱矩陣?

如果方陣的轉置與其原矩陣相同,則稱該矩陣為對稱矩陣。 透過將行變為列,列變為行,可以得到對稱矩陣。

例如

2 6 8
6 1 3
8 3 9     

假設我們已經得到一個NxN的輸入矩陣。 我們現在將使用以下方法檢查該矩陣是否對稱。

使用的方法

以下是完成此任務的各種方法

  • 使用巢狀For迴圈

  • 無需轉置矩陣的高效解決方案

  • 使用列表推導式

方法1:使用巢狀For迴圈

演算法(步驟)

以下是執行所需任務的演算法/步驟:−

  • 建立一個函式transposeMatrix()來獲取矩陣的轉置。

  • 使用for迴圈遍歷矩陣的行。

  • 使用另一個巢狀for迴圈遍歷當前行的列。

  • 獲取輸入矩陣的轉置,即交換行和列。

  • 建立一個函式checkingSymmetric(),該函式透過接受輸入矩陣和行數作為引數,如果矩陣對稱則返回true,否則返回false。

  • 呼叫上述transposeMatrix()函式以獲取矩陣的轉置。

  • 遍歷矩陣

  • 使用if條件語句檢查當前元素是否不等於轉置矩陣元素。

  • 如果上述條件為真,則返回False

  • 如果在上述巢狀迴圈中沒有返回False,則它是對稱的,因此返回True

  • 建立一個變數來儲存輸入矩陣。

  • 呼叫上述checkingSymmetric()函式,並使用if條件語句透過傳遞輸入矩陣和行數作為引數來檢查該函式是否返回true。

  • 如果條件為真,即如果函式返回true,則列印“對稱矩陣”

  • 否則列印“不是對稱矩陣”。

示例

以下程式使用巢狀For迴圈檢查輸入矩陣是否對稱:−

# 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")

輸出

執行上述程式後,將生成以下輸出:−

Input matrix is a Symmetric matrix

時間複雜度 − O(N x N)

輔助空間 − O(N x N)

方法2:無需轉置矩陣的高效解決方案

為了快速確定矩陣是否對稱,請比較其元素而不進行轉置。 在此方法中,我們將比較matrix[i][j]和matrix[j][i]。

示例

以下程式使用比較來檢查輸入矩陣是否對稱:−

# 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")

輸出

Input matrix is a Symmetric matrix

時間複雜度 − O(N x N)

輔助空間 − O(1)

方法3:使用列表推導式

示例

以下程式使用列表推導式檢查輸入矩陣是否對稱:−

# 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")

輸出

Input matrix is a Symmetric matrix

時間複雜度 − O(N*N)

輔助空間 − O(N*N)

結論

在本文中,我們首先學習了什麼是對稱矩陣,然後學習瞭如何使用三種不同的方法來實現一個程式,以確定給定的矩陣是否對稱。此外,我們學習了一種有效的方法來確定給定的矩陣是否對稱,而無需進行轉置,這節省了空間並將空間複雜度降低到O(1)。

更新於:2023年1月23日

3K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.