Matrix inversion without NumPy in Python
This article teaches you how you can do matrix inversion without the use of NumPy in Python. The inversion of a matrix is useful in solving a system of linear equations. Though the method is useful in solving a system of linear equations easily it is quite a tough task to find an inverse of a matrix.
We can use NumPy to easily find out the inverse of a matrix. But what if we want to calculate it without using NumPy? In this tutorial, we would learn how to do this.
Solving the simultaneous equations
Consider two given matrixes A and B and an unknown matrix X in the form AX=B. To find the unknown matrix X, we can multiply both sides by the inverse of A, provided the inverse exists. We get inv(A).A.X=inv(A).B.
But inv(A).A=I, the identity matrix. Also, IX=X, because the multiplication of any matrix with an identity matrix leaves it unaltered.
So we get, X=inv(A).B. This way X can be found by multiplying B with the inverse of matrix A.
Calculating the inverse of a matrix
We can calculate the inverse of a matrix by following these steps.
- Check the determinant of the matrix.
- Transpose of the original matrix.
- Find the determinant of each of the 2×2 minor matrices.
- Create a matrix of cofactors.
- Divide each term of the disjoint(also called adjugate) matrix by the determinant.
Finding the inverse without NumPy
Now that you have learned how to calculate the inverse of the matrix, let us see the Python code to perform the task:
# This function returns the transpose of a matrix def transposeMatrix(m): transpose=list(map(list,zip(*m))) print('transpose of cofactor matrix->',transpose) return transpose # This function returns the minor matrix of the element # at postion i,j def getMatrixMinor(m,i,j): minor_matrix = [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])] return minor_matrix # This function find the determinant of the matrix def getMatrixDeternminant(m): #base case for 2x2 matrix if len(m) == 2: return m[0][0]*m[1][1]-m[0][1]*m[1][0] determinant = 0 for c in range(len(m)): determinant += ((-1)**c)*m[0][c]*getMatrixDeternminant(getMatrixMinor(m,0,c)) print('determinant=',determinant) return determinant def getMatrixInverse(m): determinant = getMatrixDeternminant(m) if(determinant==0): print('Error! Determinant of the matrix is zero') print('Inverse cannot be calculated') return #special case for 2x2 matrix: if len(m) == 2: return [[m[1][1]/determinant, -1*m[0][1]/determinant], [-1*m[1][0]/determinant, m[0][0]/determinant]] #find matrix of cofactors cofactors = [] for r in range(len(m)): cofactorRow = [] for c in range(len(m)): minor = getMatrixMinor(m,r,c) cofactorRow.append(((-1)**(r+c)) * getMatrixDeternminant(minor)) cofactors.append(cofactorRow) # Transposing the cofactor matrix to get disjoint Disjoint = transposeMatrix(cofactors) # Dividing each element of the adjoint matrix by the determinant for r in range(len(Disjoint)): for c in range(len(Disjoint)): Disjoint[r][c] = Disjoint[r][c]/determinant return Disjoint
In the above code, various functions are defined. Read the comments or function definitions to understand what each function does. The getMatrixInverse() function calculates and returns the inverse of the matrix. Note that getMatrixInverse(m) takes in an array of arrays as input (original matrix as a list of lists). However, if the determinant of the input matrix is zero, it gives an error message and returns None.
Let see some examples:
Example 1
# Input matrix as a list of lists a=[[1,2,3],[0,1,4],[5,6,0]] # Print the inverse print("Inverse of the matrix->",getMatrixInverse(a))
Example 2
# Input matrix as a list of lists b=[[1,2,3],[4,5,6],[7,8,9]] # Print the inverse print("Inverse of the matrix->",getMatrixInverse(b))
I hope you liked the article. Comment if you have any doubts or suggestions regarding this article.
Leave a Reply