# 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=5
a=6
a=7
a=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.

## Related Posts

• 21 August

### Convert list to matrix in Python

Table of ContentsUsing the while loop to convert list to matrix in Python.Use the numpy.array() method to convert list to matrix in Python.Using the numpy.asarray() method to convert list to matrix in Python. Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data […]

• 20 August

### Rotate image in Python

Table of ContentsUsing the image.rotate() function from the Pillow Imaging Library (Fork) to rotate image in Python.Using NumPy library functions to rotate image in Python.Using the OpenCV and imutils library functions to rotate image in Python.Using matplotlib and SciPy libraries to rotate image in Python. Python supports the use of images and sometimes a given […]

• 06 April

### Print Matrix in Python

In this article you will learn "How to print a matrix in Python?"

• 20 November

### How to initialize array in Python

Table of ContentsUsing for loop, range() function and append() method of listIntialize empty arrayIntialize array with default valuesIntialize array with valuesUsing list-comprehensionUsing product (*) operatorUsing empty() method of numpy module In this article, we will see a different ways to initialize an array in Python. Array is collection of elements of same type. In Python, […]

• 18 November

### Print Array in Python

Table of ContentsPrint ListUsing print()Using map()By unpacking listUsing loopPrint Numpy-ArrayUsing print()Using loop In this post, we will see how to print array in Python. As we know that, Python didn’t have an in-built array data type, so we try to use list data type as an array. We can also use the NumPy module for […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.