使用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.