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 –
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
- 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.
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.