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

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

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

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

例子:

Input : mat[4][4] = {{1, 0, 0, 0},
                     {1, 4, 0, 0},
                     {4, 6, 2, 0},
                     {0, 4, 7, 6}};
Output : Matrix is in lower triangular form.

Input : mat[4][4] = {{1, 0, 0, 0},
                     {4, 3, 0, 1},
                     {7, 9, 2, 0},
                     {8, 5, 3, 6}};
Output : Matrix is not in lower triangular form.

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


输出:

Yes

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