📜  Python程序检查矩阵是否为上三角形

📅  最后修改于: 2022-05-13 01:55:16.994000             🧑  作者: Mango

Python程序检查矩阵是否为上三角形

给定一个方阵,任务是检查矩阵是否为上三角形式。如果主对角线以下的所有项都为零,则方阵称为上三角阵。

例子:

Input : mat[4][4] = {{1, 3, 5, 3},
                     {0, 4, 6, 2},
                     {0, 0, 2, 5},
                     {0, 0, 0, 6}};
Output : Matrix is in Upper Triangular form.

Input : mat[4][4] = {{5, 6, 3, 6},
                     {0, 4, 6, 6},
                     {1, 0, 8, 5},
                     {0, 1, 0, 6}};
Output : Matrix is not in Upper Triangular form.

Python3
# Python3 Program to check upper 
# triangular matrix.
  
# Function to check matrix 
# is in upper triangular
def isuppertriangular(M):
    for i in range(1, len(M)):
        for j in range(0, i):
            if(M[i][j] != 0): 
                    return False
    return True
      
# Driver function.
M = [[1,3,5,3],
    [0,4,6,2],
    [0,0,2,5],
    [0,0,0,6]]
  
if isuppertriangular(M):
    print ("Yes")
else:
    print ("No")
  
# This code is contributed by Anurag Rawat


输出:

Yes

有关更多详细信息,请参阅有关程序的完整文章以检查矩阵是否为上三角形!