In this tutorial, we will look at how to check if a numpy matrix (a 2d numpy array) is a lower triangular matrix or not with the help of some examples.
What is a lower triangular matrix?
A matrix is considered an upper triangular matrix if all the elements above the main diagonal are zero. The following image shows a lower triangular matrix.
Notice that all the elements above the main diagonal are zero.
How to check if a matrix is lower triangular in Numpy?
To check if a matrix is lower triangular or not, compare the original matrix with the lower triangular matrix generated from the matrix, if both the matrices are the same, we can say that the original matrix is a lower triangular matrix.
Use the following steps –
- Use the
numpy.tril()
function to generate the lower triangular matrix resulting from the original matrix. - Compare if the above-generated matrix is the same as the original matrix. You can use the
numpy.array_equal()
function to compare two arrays for equality. If the matrices are equal, the original matrix is lower triangular.
The following is the syntax –
import numpy as np # check if the matrix ar is lower triangular np.array_equal(ar, np.tril(ar))
Let’s now look at some examples of using the above syntax –
Example 1 – Check if a square matrix is lower triangular
Let’s create a lower triangular square matrix and check if the above method identifies it correctly or not.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
import numpy as np # create a lower triangular matrix ar = np.array([ [1, 0, 0], [4, 5, 0], [7, 8, 9] ]) # check if the above matrix is lower triangular print(np.array_equal(ar, np.tril(ar)))
Output:
True
We get True
as the output which indicates that the array ar
is a lower triangular matrix. Notice that all the elements above the main diagonal, 1-5-9 are zero.
Let’s look at an example where the matrix is not a lower triangular matrix.
import numpy as np # create a matrix ar = np.array([ [1, 0, 3], [4, 5, 0], [7, 8, 9] ]) # check if the above matrix is lower triangular print(np.array_equal(ar, np.tril(ar)))
Output:
False
We get False
as the output since in the above matrix there are non-zero elements above the main diagonal, that is, it is not a lower triangular matrix.
Example 2 – Check if a non-square matrix is upper triangular
We can also use this method on non-square matrices (where the number of rows and columns is not equal). Let’s look at an example.
import numpy as np # create a lower triangular matrix ar = np.array([ [1, 0, 0], [4, 5, 0], [7, 8, 9], [1, 1, 1] ]) # check if the above matrix is lower triangular print(np.array_equal(ar, np.tril(ar)))
Output:
True
The matrix in the above example is a 4×3 matrix (4 rows and 3 columns). We get True
as the output because it’s a lower triangular matrix (all the elements above the main diagonal 1-5-9 are zero).
Let’s look at another example.
import numpy as np # create a lower triangular matrix ar = np.array([ [1, 0, 0], [4, 5, 0], ]) # check if the above matrix is lower triangular print(np.array_equal(ar, np.tril(ar)))
Output:
True
Here, we create a 2×3 matrix. We get True
as the output because it’s a lower triangular matrix (all the elements above the main diagonal, 1-5 are zero).
You might also be interested in –
- Numpy – Check if Matrix is an Upper Triangular Matrix
- Numpy – Get the Lower Triangular Matrix (With Examples)
- Numpy – Create a Diagonal Matrix (With Examples)
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.