NumPy - Matrix Inversion



What is Matrix Inversion?

Matrix inversion is a process of finding a matrix, called the inverse matrix, which, when multiplied with the original matrix, produces the identity matrix. The identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere.

Not all matrices have inverses. A matrix must be square (having the same number of rows and columns) and its determinant must be non-zero to have an inverse.

If A is a square matrix, its inverse is denoted by A-1 and is defined by the following property −

A . A-1 = A-1 . A = I

Where I is the identity matrix of the same dimension as A. This property means that when a matrix is multiplied by its inverse, the result is the identity matrix.

Matrix Inversion in NumPy

NumPy provides the numpy.linalg.inv() function to compute the inverse of a matrix. Let us see how this function works.

Example

In the following example, the inverse of the matrix A is computed using the numpy.linalg.inv() function. The result is a new matrix that satisfies the property A . A-1 = I

import numpy as np

# Define a square matrix
A = np.array([[1, 2], [3, 4]])

# Compute the inverse of the matrix
A_inv = np.linalg.inv(A)
print(A_inv)

Following is the output obtained −

[[-2.   1. ]
 [ 1.5 -0.5]]

Verifying the Inverse

We can verify that the computed matrix is indeed the inverse by multiplying it with the original matrix and checking if the result is the identity matrix.

import numpy as np
A = np.array([[1, 2], [3, 4]])
A_inv = np.linalg.inv(A)

# Verifying the inverse
identity_matrix = np.dot(A, A_inv)
print(identity_matrix)

The output is the identity matrix, confirming that A-1 is the correct inverse of A

[[1.0000000e+00 0.0000000e+00]
 [0.0000000e+00 1.0000000e+00]]

Properties of Matrix Inversion

Matrix inversion has several important properties. They are as follows −

  • Uniqueness: If a matrix has an inverse, it is unique.
  • Product of Inverses: The inverse of a product of two matrices is the product of their inverses in reverse order: (AB)-1 = B-1A-1.
  • Inverse of Transpose: The inverse of the transpose of a matrix is the transpose of the inverse: (AT)-1 = (A-1)T.

Conditions for Matrix Inversion

Not all matrices can be inverted. For a matrix to have an inverse, it must meet the following conditions −

  • Square Matrix: The matrix must have the same number of rows and columns.
  • Non-zero Determinant: The determinant of the matrix must be non-zero. A matrix with a zero determinant is called singular and does not have an inverse.

Matrix Inversion in Linear Equations

Matrix inversion is often used to solve systems of linear equations. If we have a system of equations represented by AX = B, where A is the coefficient matrix, X is the vector of unknowns, and B is the constant vector, we can solve for X by multiplying both sides of the equation by A-1

X = A-1 . B

Following is an example to implement the same −

import numpy as np

# Coefficient matrix
A = np.array([[1, 2], [3, 4]])

# Constant vector
B = np.array([[5], [6]])

# Compute the inverse of A
A_inv = np.linalg.inv(A)

# Solve for X
X = np.dot(A_inv, B)
print(X)

This will produce the following result −

[[-4. ]
 [ 4.5]]

Handling Non-Invertible Matrices

Sometimes, we may encounter matrices that are not invertible. In such cases, attempting to compute the inverse will result in an error. Here is how we can handle such scenarios using NumPy −

import numpy as np

def invert_matrix(matrix):
   try:
      return np.linalg.inv(matrix)
   except np.linalg.LinAlgError:
      return "Matrix is not invertible."

# Non-invertible matrix
A = np.array([[1, 2], [2, 4]])

# Attempt to compute the inverse
result = invert_matrix(A)
print(result)

Following is the output obtained −

Matrix is not invertible.

Practical Applications of Matrix Inversion

Matrix inversion has many practical applications, they are −

  • Solving Systems of Linear Equations: As shown earlier, matrix inversion can be used to solve systems of linear equations.
  • Computer Graphics: In computer graphics, transformations such as rotation, scaling, and translation are often represented by matrices. Inverting these matrices can help revert the transformations.
  • Control Theory: In control theory, matrix inversion is used to solve state-space representations of dynamic systems.
Advertisements