# Numpy linalg.inv – Compute the (multiplicative) inverse of a matrix

Scientific calculations can sometimes be extremely tedious when done manually especially when they involve matrices of humongous sizes and complicated values. The Numerical Python or simply the Numpy library already contains built-in functions involving matrices and linear algebra that make computing faster and more accurate. In this article, we will dissect and look at the working of one such function, namely, linalg.inv.

Also read: NumPy linalg.matrix_power: Computing the power of a square matrix

## The Linalg.inv function

The NumPy `linalg.inv `function which can be used to determine the multiplicative inverse of a matrix of order n. The multiplicative inverse of a matrix is the reciprocal of a regular matrix just like the reciprocal of any other number in arithmetic. The inverse of a matrix helps us find out unknown variables in a system of linear equations using the matrix method and the formula given below:

AX = B => X = A-1 B

where, A= the coefficient matrix, A-1 is the inverse of the coefficient matrix, X is the matrix containing the unknown variables and B is the ordinate or determinant matrix.

Also read: NumPy linalg.det – Compute the determinant of the given array

## Necessary condition and Formula for the inverse of a matrix:

The inverse of a matrix can only exist when the determinant of the matrix is not equal to zero, |A| !=0 that is, the matrix is non-singular. This property of a matrix is often referred to as invertibility. A matrix whose inverse exists is termed an invertible matrix.

The formula for finding out the inverse of a matrix is given below:

A-1 = (adjoint of matrix A) / | A |

If we multiply a matrix A of rank n with its inverse, A-1, then we get an identity matrix of rank n. An identity matrix of order 3 is shown below:

## Syntax of the numpy.linalg.inv() function:

The function has the following syntax:

`linalg.inv(A)`

Parameters: A : (data type = ndarray or arraylike) It is the matrix whose inverse is to be found.

Returns: Ainv : (datatype = the same as of A) It is the inverse of A .

Raises: LinAlgError: If the matrix is not square or if it’s non-singular.

### Example 1: Finding inverse of a matrix

Let’s take a look at the first example where we’ll simply find out the inverse of a matrix using the function.

```# Import required package
import numpy as py

# Taking a 3rd order matrix
A = py.array([[2, 3, 4],
[-3, -3, -2],
[-2, 1, -1]])
# Calculating the inverse of the given matrix
Ainv= py.linalg.inv(A)
print("the inverse of the matrix is= ", Ainv)
```

The output of the code will be the following:

```the inverse of the matrix is=  [[-0.2173913  -0.30434783 -0.26086957]
[-0.04347826 -0.26086957  0.34782609]
[ 0.39130435  0.34782609 -0.13043478]]
```

Also read: Numpy linalg.eig – Compute the eigenvalues and right eigenvectors of a square array

### Example 2: Implementing inverse with a user input matrix

Now, let’s take a user input matrix and implement the function for the given user input:

```#importing required modules
import numpy as py
# Taking user input for matrix
Row = int(input("Enter the number of row for the matrix:"))
Col = int(input("Enter the number of columns for the matrix:"))
# Initializing the matrix
A= []
print("Enter the entries for A row-wise:")
for i in range(Row): #for loop for row entries
entry =[]
for j in range(Col): #for loop for column entries
entry.append(int(input()))
A.append(entry)
print("the matrix A is =")
print(A)

# Calculating the inverse of the given matrix
Ainv= py.linalg.inv(A)
print("the inverse of the matrix is= ", Ainv)
```

you will be prompted to enter the values for the matrix row-wise and then the output will look like the one given below:

```Enter the number of row for the matrix:4
Enter the number of columns for the matrix:4
Enter the entries for A row-wise:
2
5
0
8
1
4
2
6
7
8
9
3
1
5
7
8
the matrix A is =
[[2, 5, 0, 8], [1, 4, 2, 6], [7, 8, 9, 3], [1, 5, 7, 8]]
the inverse of the matrix is=  [[ 0.96089385 -1.91620112  0.07821229  0.44692737]
[-1.03351955  2.3575419   0.06703911 -0.75977654]
[-0.00558659 -0.27374302  0.01117318  0.20670391]
[ 0.53072626 -0.99441341 -0.06145251  0.36312849]]
```

## Conclusion

The `numpy.linalg.inv()`` `function is extremely helpful when doing rigorous calculations. Knowing the correct syntax and modifying the code to accommodate user input helps when there is no static input for a particular program. The numpy library contains many such other functions which make the life of a programmer easier. These basic inbuilt programs contained in the numpy package are free to use which makes them economical as well. To know more about numpy, visit the official documentation. Another very similar function is the `scipy.linalg.inv.`