使用 SciPy 库查找方阵的逆矩阵

SciPy 库有 scipy. 求方阵逆的函数。让我们了解如何使用此函数来计算矩阵的逆 -linalg.inv()

示例

2 x 2 矩阵的逆矩阵

#Importing the scipy package
import scipy.linalg

#Importing the numpy package
import numpy as np

#Declaring the numpy array (Square Matrix)
A = np.array([[3, 3.5],[3.2, 3.6]])

#Passing the values to scipy.linalg.inv() function
M = scipy.linalg.inv(A)

#Printing the result
print('Inverse of \n{} \n is {}'.format(A,M))
输出结果
Inverse of
[[3. 3.5]
[3.2 3.6]]
is [[-9. 8.75]
[ 8. -7.5 ]]

示例

3 x 3 矩阵的逆矩阵

import scipy
import numpy as np
A = np.array([[2,1,-2],[1,0,0],[0,1,0]])
M = scipy.linalg.inv(A)
print('Inverse of \n{} \n is {}'.format(A,M))
输出结果
Inverse of
[[ 2 1 -2]
[ 1 0 0]
[ 0 1 0]]
is [[ 0. 1. 0. ]
[ 0. -0. 1. ]
[-0.5 1. 0.5]]