Inverse matrix in python

Inverse matrix in Python

The inverse of a matrix in simple mathematics can be defined as a matrix, which when multiplied by the original matrix, gives out the Identity matrix. The inverse of a matrix can also be calculated in Python.

This tutorial demonstrates the different ways available to find the inverse of a matrix in Python.

Python makes use of the NumPy module, which is an abbreviation for Numerical Python, in dealing with matrices and arrays in Python. All the matrices are stored and worked on using functions from this huge library.

We can easily determine the inverse of a matrix by using simple mathematical concepts. However, there is also an in-built function that makes things quicker and easier and will be explained further in the article below.

The one important condition to note while finding an inverse of a matrix is that an inverse only exists for a particular matrix only if its determinant is zero. This condition may help in saving some time and energy if you get any unexpected errors during the runtime of any of the codes mentioned below.

Using the Gauss-Jordan method to find the inverse of a given matrix in Python.

The Gauss-Jordan method is an algorithm that exists in simple mathematics which is utilized to find out the inverse of any matrix whose inverse exists. The implementation of this method relies on elementary row operations. This method can be implemented in Python by simply making user-defined functions.

The NumPy library is used and needs to be imported to the working code to run the program without any errors.

The following code uses the Gauss-Jordan method to find the inverse of a matrix in Python.

The above code provides the following output:

The order of matrix is? -> 2
Enter the matrix values ->
a[0][0]=5
a[0][1]=6
a[1][0]=7
a[1][1]=8
INVERSE MATRIX IS:
-4.0 3.0
3.5 -2.5
Explanation
  • The order of the matrix is stored in the variable x.
  • A new matrix is initialized using the np.zeros() function according to the desired order entered by the user.
  • Then, the values of all the coefficients of the matrix are made to input using the for loop.
  • Then, the process of the Gauss-Jordan elimination method is initiated with the help of custom user-defined functions.
  • The inverse matrix is then printed out in the end.

Using the numpy.linalg.inv() function to find the inverse of a given matrix in Python.

The NumPy module contains a function numpy.linalg.inv(), which is utilized to directly find the inverse of a given NumPy matrix.

This functioned is contained within the NumPy module, which manually needs to be imported to the working code to run the program without any errors at runtime.

The following code uses the numpy.linalg.inv() function to find the inverse of a given matrix in Python.

The above code provides the following output:

Original matrix:
[[5 6] [7 8]] Inverse matrix:
[[-4. 3. ] [ 3.5 -2.5]]
Explanation
  • The NumPy module is imported to the Python code.
  • A matrix of any specific order is initialized and its value is stocked in the variable a.
  • The numpy.linalg.inv() function is implemented on the original matrix and the result is stored in the variable b.
  • Both variables a and b are printed and the results are shown.

That’s all about how to inverse matrix in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *