check matrix is symmetric in numpy

How to check if a matrix is symmetric in Numpy?

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

What is a symmetric matrix?

A matrix is said to be symmetric if it is equal to its transpose. That is, the matrix and its transpose are the same. The following image shows a symmetric matrix.

symmetric matrix example

You can see that if you take the transpose of the above matrix, you’ll get the same matrix as above.

How to check if a matrix is symmetric in Numpy?

To check if a matrix is symmetric, compare the matrix to its transpose, you can use the .T property or the numpy.transpose() function to get the transpose of the original matrix. You can use the numpy.array_equal() method to compare the two matrices for equality.

import numpy as np

# check if the matrix ar is symmetric
np.array_equal(ar, ar.T)

In the above syntax, we’re basically checking if matric, ar and its transpose, ar.T are equal or not.

There are other methods as well –

  • You can use the numpy.allclose() method to compare the two matrices for equality. Note that the numpy.allclose() function uses a tolerance parameter to determine how close two values need to be to be considered equal. By default, the tolerance is set to 1e-05, which means that two values are considered equal if they are within 0.00001 of each other. You can adjust this tolerance by passing a different value to the rtol (relative tolerance) or atol (absolute tolerance) parameters of numpy.allclose().

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

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

Example 1 – Using the .T attribute and numpy.array_equal()

Let’s create a square matrix and check if it is symmetric. For this, we’ll take the following steps –

  1. Get the transpose of the matrix using .T attribute.
  2. Compare the original matrix to its transpose for equality using the numpy.array_equal() method.
import numpy as np

# create a symmetric matrix
ar = np.array([[1, 2, 3],
               [2, 4, 5],
               [3, 5, 6]])

# check if the matrix ar is symmetric
print(np.array_equal(ar, ar.T))

Output:

True

Here, we create a 3×3 symmetric matrix and checked if it’s symmetric or not. We get True as the output indicating the array ar is a symmetric matrix.

Let’s look at an example where the matrix is not a symmetric matrix.

import numpy as np

# create a matrix
ar = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]])

# check if the matrix ar is symmetric
print(np.array_equal(ar, ar.T))

Output:

False

We get False as the output which indicates that the matrix ar is not symmetric.

Example 2 – Using .T attribute and numpy.allclose()

This method is similar to the above method, the only difference is that instead of numpy.array_equal() function, we use the numpy.allclose() function to compare the arrays. The following are the steps –

  1. Get the transpose of the matrix using the .T attribute.
  2. Compare the original matrix to its transpose for equality using the numpy.allclose() method.

In this method, you can define how close two values need to be to be considered equal using the tolerance parameter.

Let’s take the same examples as above.

import numpy as np

# create a symmetric matrix
ar = np.array([[1, 2, 3],
               [2, 4, 5],
               [3, 5, 6]])

# check if the matrix ar is symmetric
print(np.allclose(ar, ar.T))

Output:

True
import numpy as np

# create a matrix
ar = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]])

# check if the matrix ar is symmetric
print(np.allclose(ar, ar.T))

Output:

False

We get the same results as above.

Example 3 – Iterate through the matrix

Alternatively, you can iterate through the entire matrix and check whether each element satisfies the symmetric matrix property, ar[i][j] == ar[j][i].

import numpy as np

# create a symmetric matrix
ar = np.array([[1, 2, 3],
               [2, 4, 5],
               [3, 5, 6]])

# check if matrix is symmetric
def is_matrix_symmetric(a):
    for i in range(len(a)):
        for j in range(len(a[i])):
            if a[i][j] == a[j][i]:
                continue
            else:
                return False
    return True

# use the above function
is_matrix_symmetric(ar)

Output:

True

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