# 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.

import_contacts

## Related Posts

• 17 May

• 03 May

### Create an Array of 1 to 10 in Python

Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

• 30 December

### How to Append to Array in Python

Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusionWas this post helpful? In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how […]

• 17 December

### Python array size: get size of array in Python

Table of ContentsPython array sizeWays to calculate the size of array in PythonUsing the len() functionUsing the numpy.size() attributeUsing the numpy.shape() attributeConclusionWas this post helpful? In this article, we will see how to get size of array in Python. Python array size An array stores a collection of similar elements in a contiguous memory location. […]

• 11 November

### Convert float array to int array in Python

Table of ContentsWhat are the types of arrays?Ways to convert float arrays to int in PythonUsing the numpy.asarray() functionUsing the numpy.astype() functionUsing the numpy.int_() functionUsing the numpy.round_() functionUsing the numpy.rint() functionUsing the numpy.floor() functionUsing the numpy.ceil() functionUsing the numpy.trunc() functionConclusionWas this post helpful? What are the types of arrays? An array is a common structure […]

• 01 November

### Convert Bytearray to String in Python

Table of ContentsHow to create a bytearray from a string?Using the str() Function to convert Bytearray to String in PythonUsing the decode() Function to convert Bytearray to String in PythonUsing the codecs Module to convert Bytearray to String in PythonConclusionWas this post helpful? Python supports different types of sequence objects to store data. One such […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.