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.

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

- Compute the transpose of the matrix using the
`.T`

attribute. - Compute the dot product of the matrix with this transpose using the
`numpy.dot()`

method. - Create an identity matrix of the same shape as the original square matrix.
- 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** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

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

- Numpy – Check If a Matrix is Invertible
- How to check if a matrix is symmetric in Numpy?
- How to check if a matrix is a square matrix in Numpy?

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