使用Python檢查矩陣是否為下三角矩陣


在這篇文章中,我們將學習一個Python程式來檢查矩陣是否為下三角矩陣。

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

使用的方法

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

  • 使用巢狀for迴圈

什麼是下三角矩陣?

如果所有位於主對角線以上的元素都為零,則稱方陣為下三角矩陣。

示例

1 0 0 0 
2 3 0 0 
1 3 7 0 
0 4 8 4 

方法 1:使用巢狀for迴圈

演算法(步驟)

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

  • 建立一個函式printGivenMatrix() 來列印給定的矩陣。

  • 在printGivenMatrix()函式內部,使用For迴圈遍歷給定矩陣的行。

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

  • 列印當前行和列對應的矩陣元素。

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

  • 使用for迴圈len()函式(返回物件中的專案數)遍歷矩陣的行長度。

  • 使用另一個巢狀for迴圈從(row + 1)列遍歷到列的末尾。

  • 使用if條件語句檢查當前元素是否不等於0(因此下三角條件失敗)

  • 如果條件為真,則使用return語句返回False

  • 如果所有巢狀迴圈在不返回任何值的情況下執行完畢,則給定的矩陣為下三角矩陣,因此返回True。

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

  • 將此作為引數傳遞給printGivenMatrix()來列印給定矩陣。

  • 使用if條件語句檢查上面定義的函式checkLowerTriangular()函式在將輸入矩陣作為引數傳遞時是否返回true

  • 如果條件為真,則列印輸入矩陣為下三角矩陣

  • 否則,列印輸入矩陣不是下三角矩陣。

示例

以下程式使用巢狀For迴圈返回給定矩陣是否為下三角矩陣:

# creating a function to print the given matrix
def printGivenMatrix(inputMatrix):
   # Traversing in the rows of the input matrix
      for p in range(len(inputMatrix)):
         # Traversing in the columns corresponding to the current row
         # of the input matrix
            for q in range(len(inputMatrix)):
               # printing the element at the current row and column
                  print(inputMatrix[p][q], end=" ")
            # Printing a new line to separate the rows
            print()
# createing a function to check whether the matrix
# is lower triangular by accepting the input matrix as an argument
def checkLowerTriangular(inputMatrix):
   # traversing through the rows of a matrix
      for p in range(0, len(inputMatrix)):
         # Traversing from row +1 column to last column
         for q in range(p + 1, len(inputMatrix)):
            # checking whether the current element is not equal to 0
               if(inputMatrix[p][q] != 0):
                  # If the element is not zero then it cannot be considered as lower triangular
                  # Hence returning false if the condition is true
                     return False
   # If the above nested loops get executed without returning False
   # Hence the matrix is lower triangular so return True
      return True
# input matrix
inputMatrix = [[1, 0, 0, 0],
               [2, 3, 0, 0],
               [1, 3, 7, 0],
               [0, 4, 8, 4]]
# Printing the given input Matrix by passing the
# given matrix as an argument to printGivenMatrix() function
printGivenMatrix(inputMatrix)
# checking whether the checkLowerTriangular() returns true
if checkLowerTriangular(inputMatrix):
   # If the function returns True then the matrix is lower triangular
      print("Yes, the input matrix is lower triangular!!!")
else:
   # else printing NOT lower triangular matrix
      print("No, the input matrix is Not lower triangular!!!")

輸出

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

1 0 0 0 
2 3 0 0 
1 3 7 0 
0 4 8 4 
Yes, the input matrix is lower triangular!!!

時間複雜度 - O(n^2)。其中n表示指定矩陣中行和列的數目。

輔助空間 - O(1)。因為不需要額外的空間,所以它是常數。

結論

在本文中,我們學習了什麼是下三角矩陣以及一個示例,最後,我們編寫了程式碼以及如何確定給定矩陣是否為下三角矩陣的說明。

更新於:2023年1月23日

302 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.