check matrix is orthogonal in numpy

Numpy – Check If a Matrix is Orthogonal

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

When is a matrix orthogonal?

A real square matrix, for example, M is said to be orthogonal, if the dot product of the matrix with its transpose is an identity matrix. We can also say that a matrix is orthogonal if its transpose is equal to its inverse. The following image shows an orthogonal matrix.

example of an orthogonal matrix

How to check if a matrix is invertible in Numpy?

To check if a square matrix is invertible or not in Numpy, check if it the dot product of the matrix with its transpose is an identity matrix of the same shape or not.

Use the following steps –

  1. Compute the transpose of the matrix using the .T attribute.
  2. Compute the dot product of the matrix with this transpose using the numpy.dot() method.
  3. Create an identity matrix of the same shape as the original square matrix.
  4. Check if the resulting matrix from the dot product is equal to the identity matrix using the numpy.allclose() method.

Alternatively, you can check if the transpose of the matrix is equal to its inverse.

Let’s now look at the above method with the help of some examples. First, we will create two matrices that we will use throughout this tutorial, one orthogonal and the other not orthogonal.

import numpy as np

# create matrices
ar1 = np.array([
    [0, 1, 0],
    [-1, 0, 0],
    [0, 0, 1]
])

ar2 = np.array([
    [1, 2, 3],
    [0, 0, 2],
    [4, 5, 6]
])

Here, the matrix ar1 is orthogonal and the matrix ar2 is not orthogonal.

Example 1 – Using dot product with the transpose

Here, we compare the dot product of the matrix and its inverse to an identity matrix to check whether the matrix is orthogonal or not. Let’s check if the array ar1 created above is transpose 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.

# dot product of matrix and its transpose
dot_product = np.dot(ar1, ar1.T)

# create an identity matrix of the same shape as ar1
identity_matrix = np.identity(len(ar1))

# check if matrix is orthogonal
print(np.allclose(dot_product, identity_matrix))

Output:

True

We get True as the output. Here, we are using numpy.allclose() function to compare the values in the matrices for equality because this function lets you define a tolerance 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().

Alternatively, you can use the numpy.array_equal() method to compare two matrices for equality but it’ll check for the exact equality of the values which may be a problem if the float values in the machine are represented with high precision.

Let’s now apply this method on ar2 which is not an orthogonal matrix.

# dot product of matrix and its transpose
dot_product = np.dot(ar2, ar2.T)

# create an identity matrix of the same shape as ar2
identity_matrix = np.identity(len(ar2))

# check if matrix is orthogonal
print(np.allclose(dot_product, identity_matrix))

Output:

False

We get False as the output.

Example 2 – Compare the transpose with the inverse

If a matrix is orthogonal, its transpose will be equal to its inverse. Thus, we can compute the transpose and the inverse of the matrix and check if they are equal or not to determine if the matrix is orthogonal.

Use the numpy.linalg.inv() function to get the inverse of a matrix. If the matrix cannot be inverted, it raises a LinAlgError.

Let’s apply this method on the array ar1.

# check if matrix is orthogonal
print(np.allclose(ar1.T, np.linalg.inv(ar1)))

Output:

True

We get the correct result.

Let’s apply this method on ar2 now.

# check if matrix is orthogonal
print(np.allclose(ar2.T, np.linalg.inv(ar2)))

Output:

False

We get the correct result.

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