Python program to check if a given matrix is a lower triangular has been shown here. A square matrix is considered a lower triangular matrix if all the elements above the main diagonal are Zero. The elements in the main diagonal may or may not be 0.
Examples:
In the given examples, (i), (ii), (iv) and (v) are lower triangular matrices as every element above the principal diagonal is 0 in each of these matrices. The matrix shown in (iii) is not a lower triangular matrix as one element above the principal diagonal is non-zero. The dotted lines have been drawn over the principal diagonals.
1. Algorithm to Check If a Matrix Is a Lower Triangular Matrix
1. Take a matrix $A_{m\times n}$ as input
2. Check if $m=n$
3. If step [2] is false then display "Input matrix is not a square matrix!" and exit program
4. If step [2] is true, then
5. Check if $A_{i,j} \neq 0$ for each $i < j$ and $i \in [1, m]$ and $j \in [1, n]$
6. If step [5] is true for atleast one $A_{i,j}$ then display "The matrix is not a lower triangular matrix" and exit program.
7. If step [5] is false then display "The matrix is a lower triangular matrix" and exit program.
2. Pseudocode to Check If a Matrix Is a Lower Triangular Matrix
Input: A matrix $A_{m\times n}$
Output: A is lower triangular or not
1. Procedure lowerTriangularMatrix($A_{m\times n}$):
2.
3.
4.
5.
6.
7.
8.
9. End Procedure
3. Time Complexity to Check If a Matrix Is a Lower Triangular Matrix
Time Complexity: O($mn$)
Where $m$ is the number of rows and $n$ is the number of columns in the matrices.
4. Python Program to Check If a Matrix Is a Lower Triangular Matrix
# *************************************** # alphabetacoder.com # Python Program to check if a matrix # is a lower triangular matrix # *************************************** # declare and initialize array A = [[0 for j in range(10)] for i in range(10)] B = [[0 for j in range(10)] for i in range(10)] # take input of the order of the matrix m = int(input("Enter the number of rows of matrix = ")) n = int(input("Enter the number of columns of matrix = ")) # take input of the first matrix print("Enter the elements of matrix of order", m, "x", n, "=") for i in range(0, m): for j in range(0, n): A[i][j] = int( input("Enter element of row {} and column {}: ".format(i + 1, j + 1)) ) # check if the matrix is a square matrix or not # if it is square matrix, check if it is lower triangular or not flag = 0 if m != n: print("Input matrix is not a square matrix!") else: # check if A is lower triangular or not by # finding an non-zero element above main diagonal for i in range(0, m): for j in range(i + 1, n): if A[i][j] != 0: # non-zero element found flag = 1 break # break the loop as non-zero element # above main diagonal has been detected if flag == 1: break # display the result if flag == 0: print("It is a lower triangular matrix!") else: print("It is not a lower triangular matrix!")
Output
Enter the number of rows of matrix = 3
Enter the number of columns of matrix = 3
Enter the elements of matrix of order 3 x 3 =
Enter element of row 1 and column 1: 1
Enter element of row 1 and column 2: 0
Enter element of row 1 and column 3: 0
Enter element of row 2 and column 1: 2
Enter element of row 2 and column 2: 3
Enter element of row 2 and column 3: 0
Enter element of row 3 and column 1: 5
Enter element of row 3 and column 2: 1
Enter element of row 3 and column 3: 6
It is a lower triangular matrix!