Python程式檢查對合矩陣


在本文中,我們將學習一個Python程式來檢查對合矩陣。

假設我們已經輸入了一個矩陣。我們現在將使用以下方法檢查輸入矩陣是否為對合矩陣

使用的方法

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

  • 使用巢狀迴圈

  • 使用NumPy模組

什麼是對合矩陣?

如果一個矩陣與其自身相乘得到單位矩陣,則稱其為對合矩陣。其逆矩陣就是對合矩陣

如果A * A = I,則矩陣A被稱為對合矩陣。此處I表示單位矩陣

方法1:使用巢狀迴圈

演算法(步驟)

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

  • 建立一個變數來儲存矩陣的行數。

  • 建立一個函式multiplyMatrix(),透過接受輸入矩陣來執行矩陣乘法。

  • 執行矩陣乘法。

  • 使用return語句返回矩陣乘法的結果輸出。

  • 建立一個函式checkInvolutoryMat(),透過接受輸入矩陣作為引數來檢查輸入矩陣是否為對合矩陣。

  • 建立一個大小為rows*rows的矩陣,所有元素都為0,並將其儲存為outputMatrix

  • 呼叫上述multiplyMatrix()函式執行矩陣乘法,並將結果儲存在上述outputMatrix變數中。

  • 遍歷outputMatrix的每個元素。

  • 使用if條件語句檢查對角線元素是否不為1,如果是,則返回False,即不是對合矩陣。

  • 使用if條件語句檢查非對角線元素是否不為0,如果是,則返回False,即不是對合矩陣。

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

  • 使用if條件語句檢查上述定義的checkInvolutoryMat()函式是否返回True,並將輸入矩陣作為引數傳遞。

  • 如果條件為真,則列印對合矩陣

  • 否則列印非對合矩陣。

示例

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

# input no of rows
rows = 3
# creating a function to perform matrix multiplication
def multiplyMatrix(inputMatrix, output):
   # traversing through the rows of a matrix
      for p in range(rows):
         # traversing through all the columns of a current row
         for q in range(rows):
            # assuming the current element is 0
               output[p][q] = 0
               for x in range(rows):
                  # performing matrix multiplication
                  output[p][q] += inputMatrix[p][x] * inputMatrix[x][q]
      # returning the resultant matrix multiplication output
      return output
# creating a function to check whether the input matrix
# is an involuntary  matrix by accepting matrix as an argument
def checkInvolutoryMat(inputMatrix):
   # Creating a rows*rows size matrix with all elements as 0
      output = [[0 for p in range(rows)]
              for q in range(rows)]
      # calling the above multiplyMatrix() function
      output = multiplyMatrix(inputMatrix, output)
   # Iterating in the rows of the output matrix
      for p in range(rows):
         # Iterating in the diagonals of the output matrix
            for q in range(rows):
               # If it is a diagonal element and if it is not 1 then return False(Not Involuntary)
               if (p == q and output[p][q] != 1):
                  return False
               # If it is a non-diagonal element and if it is not 1 then return False(Not Involuntary)
               if (p != q and output[p][q] != 0):
                  return False
      # It is an involuntary matrix so return True
      return True
# input matrix
inputMatrix = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
# checking whether the above checkInvolutoryMat() function returns true
# by passing the input matrix as an argument
if (checkInvolutoryMat(inputMatrix)):
   # printing involuntary matrix if the condition is true
      print("The input matrix is an Involutory matrix")
else:
   # else printing NOT involuntary matrix
      print("The input matrix is NOT an Involutory matrix")

輸出

The input matrix is an Involutory matrix

時間複雜度 - O(N^3)

輔助空間 - O(N^2)

方法2:使用NumPy模組

利用numpy庫是確定矩陣是否為對合矩陣的另一種方法。這可以透過使用numpy.allclose()函式比較矩陣與其逆矩陣來實現。

演算法(步驟)

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

  • 使用import關鍵字匯入numpy模組。

  • 建立一個函式checkInvolutoryMat(),透過接受輸入矩陣作為引數來檢查輸入矩陣是否為對合矩陣。

  • 使用numpy.linalg.inv()函式(計算矩陣的逆)獲取輸入矩陣的逆。

  • 使用numpy.allclose()函式檢查輸入矩陣是否等於其逆矩陣,將輸入矩陣及其逆矩陣作為引數傳遞並返回結果。

  • 如果兩個陣列在容差範圍內元素級相等,則allclose()函式將返回True。

  • 容差值是正數,通常具有非常小的值。

示例

以下程式使用numpy.linalg.inv()、numpy.allclose()函式檢查輸入矩陣是否為對合矩陣:

# using the numpy module
import numpy as np
# creating a function to check whether the input matrix
# is an involuntary  matrix by accepting matrix as an argument
def checkInvolutoryMat(inputMatrix):
   # Getting the inverse of an input matrix using the numpy linalg module
      inverseMat = np.linalg.inv(inputMatrix)
      # checking whether the input matrix is equal to its inverse matrix
      # using the numpy.allclose() function and returning the result
      return np.allclose(inputMatrix, inverseMat)
# input matrix
inputMatrix = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
# calling the checkInvolutoryMat() function by passing the input matrix as an argument
# The output True indicates an INVOLUNTARY matrix
print(checkInvolutoryMat(inputMatrix))

輸出

True

在上面的示例中,輸出True表示給定的輸入矩陣是對合矩陣。

此方法利用了numpy的最佳化線性代數例程,並且具有更短、更容易理解的優點。此方法的時間複雜度取決於矩陣逆計算的複雜度,對於稠密矩陣通常為O(N^3)。空間複雜度為O(N^2)。

時間複雜度 - O(N^3)

空間複雜度 - O(N^2)

結論

在本文中,我們學習瞭如何使用兩種不同的方法來確定給定矩陣是否為對合矩陣。我們學習瞭如何使用NumPy模組的linalg.inv()函式獲取給定矩陣的逆。

更新於: 2023年1月31日

103 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.