
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Assume we have taken an input matrix. We will now check whether the input matrix is lower triangular using the below methods.
Methos Used
The following are the various methods to accomplish this task −
Using nested for loop
What is a Lower Triangular Matrix?
If all the entries above the main/principal diagonal are zero, a square matrix is said to be lower triangular.
Example
1 0 0 0 2 3 0 0 1 3 7 0 0 4 8 4
Method 1: Using Nested for Loop
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. −
Create a function printGivenMatrix() that prints the given matrix.
Inside the printGivenMatrix() function Traverse through the rows of the given Matrix using For loop.
Using another Nested For loop traverse through the columns of the current row.
Print the corresponding matrix element at the current row and column.
Create a function checkLowerTriangular() to check whether the input matrix is lower triangular by accepting the input matrix as an argument.
Use the for loop to traverse through the length of rows of a matrix using the len() function(returns the number of items in an object).
Use another nested for loop to traverse through the from (row +1) column to the end of the columns.
Use the if conditional statement to check whether the current element is not equal to 0(So the lower triangular Condition Fails)
Return False if the condition is true using the return statement.
If all the nested loops get executed without returning anything then the given matrix is lower triangular so return True.
Create a variable to store the input matrix.
Print the given matrix by passing this as an argument to printGivenMatrix()
Use the if conditional statement to check whether the above-defined function checkLowerTriangular() function returns true by passing the input matrix as an argument.
Print the input matrix as a lower triangular matrix if the condition is true.
Else print the input matrix is NOT a lower triangular matrix.
Example
The following program returns whether the given matrix is lower triangular or not using nested For loops −
# 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!!!")
Output
On executing, the above program will generate the following output −
1 0 0 0 2 3 0 0 1 3 7 0 0 4 8 4 Yes, the input matrix is lower triangular!!!
Time Complexity − O(n^2). In which n indicates the number of rows and columns in the specified matrix.
Auxiliary Space − O(1). as no extra space is required, hence it is a constant.
Conclusion
We learned what a lower triangular matrix is in this article along with an example, and at the end, we wrote code with instructions on how to determine whether a given matrix is a lower triangular matrix or not.
- Related Articles
- JavaScript Program to check if the matrix is lower Triangular
- Program to check if matrix is lower triangular in C++
- JavaScript Program to Check if Matrix is Upper Triangular
- C# Program to Illustrate Lower Triangular Matrix
- Swift Program to Display Lower Triangular Matrix
- Program to check if matrix is upper triangular in C++
- Check if a matrix is symmetric using Python
- How to replace upper triangular matrix with lower triangular matrix and vice versa in R?
- Program to print Lower triangular and Upper triangular matrix of an array in C
- How to solve triangular matrix equations using Python SciPy?
- Print lower triangular matrix pattern from given array in C Program.
- How to create a heatmap for lower triangular matrix in R?
- Create an array with zero above the main diagonal forming a lower triangular matrix in Numpy
- Check if a Matrix is Identity Matrix or not in Java?
- Check if a Matrix is Markov Matrix or not in Java
