Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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]]]
Advertisements
