Python – scipy.linalg.inv


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 :
", a) # Finding the inverse of a square matrix x = linalg.inv(a) print("
Inverse of Square Matrix A :
", 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 :
", q) # Finding the inverse of the square matrix m = np.linalg.inv(q) print("
Inverse of the Square Matrix Q :
", 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

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements