# Python – scipy.linalg.inv

ScipyScientific ComputingProgramming

The scipy.linalg package contains a of different functionalities that are used for Linear Algebra. One of them is the inv() function, which is used to find the inverse of a square matrix.

## Syntax

scipy.linalg.inv(x)

Where x is a square matrix.

## Example 1

Let us consider the following example −

# Import the required libraries
from scipy import linalg
import numpy as np

# defines the array
a = np.array([[5, 3], [6, 4]])
print("Input matrix :\n", a)

# Finding the inverse of a square matrix
x = linalg.inv(a)
print("\n Inverse of Square Matrix A :\n", x)

## Output

The above program will generate the following output −

Input matrix :
[[5 3]
[6 4]]

Inverse of Square Matrix A :
[[ 2. -1.5]
[-3. 2.5]]

## Example 2

Let us take another example −

# Import the required libraries
from scipy import linalg
import numpy as np

# defines the matrix
q = np.array([[[5., 6.], [7., 8.]], [[2, 4], [6, 8]]])
print("Input matrix :\n", q)

# Finding the inverse of the square matrix
m = np.linalg.inv(q)
print("\n Inverse of the Square Matrix Q :\n", m)

## Output

The above program will generate the following output −

Input matrix :
[[[5. 6.]
[7. 8.]]

[[2. 4.]
[6. 8.]]]

Inverse of the Square Matrix Q :
[[[-4. 3. ]
[ 3.5 -2.5 ]]

[[-1. 0.5 ]
[ 0.75 -0.25]]]
Updated on 22-Dec-2021 10:02:40