Publish AI, ML & data-science insights to a global community of data professionals.

Linear Algebra: Finding Inverse Matrix, with Python

Part 3: A comprehensive step-by-step guide to finding the inverse matrix using elementary row operations and determinants of the matrix

Photo by Raimond Klavins on Unsplash
Photo by Raimond Klavins on Unsplash

The previous article explained the different matrix operations and corresponding operations. This article will dive deep into two different methods on how to get the inverse matrix using elementary row operations and using determinants of the matrix.

Inverse Matrix

An inverse matrix is similar to the reciprocal of a number such that the multiplication of a number by its reciprocal gives 1 and the multiplication of a matrix by its inverse gives an identity matrix. However, to find the inverse of the matrix, the matrix must be a square matrix with the same number of rows and columns. There are two main methods to find the inverse of the matrix:

Method 1: Using elementary row operations

Recalled the 3 types of rows operation used to solve linear systems: swapping, rescaling, and pivoting. Those operations can be written in elementary matrices. And left multiplication with an augmented linear system matrix represents elementary row operations.

Example augmented matrix of the linear system. (Image by Author)
Example augmented matrix of the linear system. (Image by Author)

Swapping matrix: interchange row i and j of an identity matrix.

Interchange row 1 and row 2 of matrix A. (Image by Author)
Interchange row 1 and row 2 of matrix A. (Image by Author)

Rescaling matrix: if the operation is k times equation i, put the number k at the position (row=i, col=i) of an identity matrix.

Row 1 multiply by constant 3. (Image by Author)
Row 1 multiply by constant 3. (Image by Author)

Pivoting matrix: if a multiple of equation i is added to equation j, put the number k at the position (row=j, col=i) of an identity matrix.

5 multiply row 2 added to row 1. (Image by Author)
5 multiply row 2 added to row 1. (Image by Author)

We now can use the elementary matrices to find an inverse matrix.

  • If A is invertible, then Eₖ…E₂E₁A = I
  • Multiply both sides by A inverse yields:
  • A sequence of elementary row operations can reduce A to I and the same sequence of elementary row operations turns I into the inverse of matrix A.
  • If A is an invertible matrix, then for each column vector b, the system of equations, Ax = b has exactly one solution.

Example of finding the inverse of A using row operations:

Finding A inverse using row operations. (Image by Author)
Finding A inverse using row operations. (Image by Author)
Finding A inverse using elementary matrices. (Image by Author)
Finding A inverse using elementary matrices. (Image by Author)

Method 2: Using the determinant of the matrix

Determinant and inverse of a 2 x 2 Matrix

The determinant of a matrix, det(A) or |A|, is a useful value for a square matrix. For a 2 by 2 matrix A below, det(A) = ad – bc, and A is invertible if det(A) ≠ 0. For a 2 x 2 matrix, an inverse matrix is given by:

However, to calculate the determinant of matrix size bigger than 2 x 2, we need to get its minors and cofactors.

Minors and Cofactors

For a square matrix A, the minor of entry aᵢⱼ, Mᵢⱼ, is defined to be the determinant of the submatrix formed by deleting the ith row and jth column from matrix A and cofactor of entry aᵢⱼ, Cᵢⱼ, is as follows:

Cofactors equation from minors. (Image by Author)
Cofactors equation from minors. (Image by Author)
Illustration on how to get minors and cofactors from a square matrix A. (Image by Author)
Illustration on how to get minors and cofactors from a square matrix A. (Image by Author)

Cofactor Expansion to get the Determinant of a matrix

The determinant of a square matrix can be computed by multiplying entries in any row or columns by corresponding cofactors and adding the resulting products:

Finally, the inverse matrix can be calculated by multiplying the transpose of the cofactor matrix by 1/determinant.

Finding A inverse using determinant and cofactor matrix. (Image by Author)
Finding A inverse using determinant and cofactor matrix. (Image by Author)

Summary

The inverse matrix is a useful tool to solve various problems in linear algebra. One of the applications shown in this post is to solve the system of linear equations. In this post, you will learn how to get the inverse matrix step by step using 2 different methods, using elementary row operations and using determinants of the matrix.


Recommended Reading

Linear Algebra: Systems of Linear Equations and Matrices, with Python

Linear Algebra: Matrix Operations and their Properties, with Python

Linear Algebra: LU Decomposition, with Python

Linear Algebra: Euclidean Vector Space

Linear Algebra: Orthogonal Vectors

Linear Algebra: General Vector Space

Linear Algebra: Discovering Eigenvalues and Eigenvectors for Diagonalization


References

[1] Elementary matrix – Wikipedia

[2] Minor / Cofactor (linear algebra) – Wikipedia

[3] Determinant – Wikipedia

[4] Invertible Matrix – Wikipedia


Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.

Write for TDS

Related Articles