
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
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.