# How to check if a matrix is a diagonal matrix in Numpy?

In this tutorial, we will look at how to check if a numpy matrix (a 2d numpy array) is a diagonal matrix or not with the help of some examples.

### What is a diagonal matrix?

A square matrix (n x n matrix) is said to be a diagonal matrix if all the elements above and below the main diagonal are zero. That is, non-zero elements are only allowed on the main diagonal. The following image shows a diagonal matrix.

Notice that all the elements above and below the main diagonal are zero.

## How to check if a matrix is a diagonal matrix in Numpy?

To check if a matrix is a diagonal matrix or not, compare the original matrix with the diagonal matrix generated from the original matrix, if both the matrices are equal, we can say that the original matrix is a diagonal matrix.

Use the following steps –

1. Pass the original matrix (array) as an argument to the `numpy.diag()` function. This will return a 1d array of the main diagonal elements.
2. Pass the resulting 1d array to the `numpy.diag()` function. This will return a diagonal matrix using the diagonal elements passed.
3. 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, then, the original matrix is diagonal.

Note that if you pass a 2d array to the `numpy.diag()` function, it returns the diagonal elements as a 1d array and if you pass a 1d array to the `numpy.diag()` function, it returns the diagonal matrix using the given diagonal elements.

The following is the syntax –

```import numpy as np

# check if the matrix ar is diagonal
np.array_equal(ar, np.diag(np.diag(ar)))```

Let’s now look at some examples of using the above syntax –

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

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

### Example – Check if a square matrix is a diagonal matrix

Let’s create a diagonal matrix and check if the above method identifies it correctly or not.

```import numpy as np

# create a diagonal matrix
ar = np.array([
[1, 0, 0],
[0, 2, 0],
[0, 0, 3]
])

# check if the matrix ar is diagonal
print(np.array_equal(ar, np.diag(np.diag(ar))))```

Output:

`True`

We get `True` as the output which indicates that the array `ar` is a diagonal matrix. Notice that all the elements above and below the main diagonal, 1-2-3 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, 1, 0],
[0, 2, 0],
[0, 0, 3]
])

# check if the matrix ar is diagonal
print(np.array_equal(ar, np.diag(np.diag(ar))))```

Output:

`False`

We get `False` as the output since in the above matrix there are non-zero elements above or below the main diagonal, that is, it is not a diagonal matrix.

Keep in mind that a diagonal matrix is defined as a square matrix with non-diagonal elements as zero. Now, if you use the above method on a non-square matrix, you’d get `False` which is the expected response.

```import numpy as np

# create a matrix
ar = np.array([
[1, 0, 0],
[0, 2, 0]
])

# check if the matrix ar is diagonal
print(np.array_equal(ar, np.diag(np.diag(ar))))```

Output:

`False`

Now, if you look under the hood. You’ll find that, `np.diag(ar)` returns a 1d array with `[1, 2]` and then passing this to `numpy.diag()` returns a 2×2 diagonal matrix `[[1, 0], [0, 2]]` which is then compared with the original matrix which is a 2×3 matrix.

You might also be interested in –