check matrix is lower triangular numpy

Numpy – Check if Matrix is a Lower Triangular Matrix

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.

example of 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 –

  1. Use the numpy.tril() function to generate the lower triangular matrix resulting from the original matrix.
  2. 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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top