使用 SciPy 库求解方阵的逆矩阵
SciPy 库具有 scipy.linalg.inv() 函数,用于求解方阵的逆矩阵。让我们了解一下如何使用此函数计算矩阵的逆矩阵 −
示例
2x2 矩阵的逆矩阵
#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 {} is {}'.format(A,M))
输出
Inverse of [[3. 3.5] [3.2 3.6]] is [[-9. 8.75] [ 8. -7.5 ]]
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例
3x3 矩阵的逆矩阵
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 {} 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]]
广告