Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if the matrix is lower triangular using Python
In this article, we will learn a Python program to check if a matrix is lower triangular. A lower triangular matrix is a square matrix where all elements above the main diagonal are zero.
What is a Lower Triangular Matrix?
A square matrix is called lower triangular if all entries above the main diagonal are zero. Elements on or below the main diagonal can be any value.
Example of Lower Triangular Matrix
1 0 0 0 2 3 0 0 1 3 7 0 0 4 8 4
In the above matrix, all elements above the diagonal (positions [0,1], [0,2], [0,3], [1,2], [1,3], [2,3]) are zero.
Method: Using Nested For Loop
We can check if a matrix is lower triangular by iterating through elements above the main diagonal and verifying they are all zero.
Algorithm
The steps to check if a matrix is lower triangular ?
Create a function to print the matrix for visualization
Create a function to check the lower triangular condition
Traverse through rows and columns above the diagonal
Return False if any element above diagonal is non-zero
Return True if all elements above diagonal are zero
Example
The following program checks whether a given matrix is lower triangular ?
# Function to print the given matrix
def print_matrix(matrix):
for row in matrix:
for element in row:
print(element, end=" ")
print()
# Function to check if matrix is lower triangular
def is_lower_triangular(matrix):
rows = len(matrix)
# Check all elements above the main diagonal
for i in range(rows):
for j in range(i + 1, rows):
if matrix[i][j] != 0:
return False
return True
# Test with a lower triangular matrix
matrix1 = [[1, 0, 0, 0],
[2, 3, 0, 0],
[1, 3, 7, 0],
[0, 4, 8, 4]]
print("Matrix 1:")
print_matrix(matrix1)
if is_lower_triangular(matrix1):
print("Yes, the matrix is lower triangular!")
else:
print("No, the matrix is NOT lower triangular!")
print("\n" + "="*40 + "\n")
# Test with a non-lower triangular matrix
matrix2 = [[1, 2, 0],
[3, 4, 0],
[5, 6, 7]]
print("Matrix 2:")
print_matrix(matrix2)
if is_lower_triangular(matrix2):
print("Yes, the matrix is lower triangular!")
else:
print("No, the matrix is NOT lower triangular!")
Matrix 1: 1 0 0 0 2 3 0 0 1 3 7 0 0 4 8 4 Yes, the matrix is lower triangular! ======================================== Matrix 2: 1 2 0 3 4 0 5 6 7 No, the matrix is NOT lower triangular!
How It Works
The algorithm checks elements above the main diagonal using nested loops ?
Outer loop (i): Iterates through each row
Inner loop (j): Starts from column (i+1) to check elements above diagonal
Condition: If any element above diagonal is non-zero, return False
Result: If all elements above diagonal are zero, return True
Complexity Analysis
| Complexity | Value | Description |
|---|---|---|
| Time Complexity | O(n²) | Checking upper triangular elements |
| Space Complexity | O(1) | No extra space required |
Conclusion
A lower triangular matrix has all elements above the main diagonal as zero. Using nested loops to check elements above the diagonal provides an efficient O(n²) solution for verification.
---