使用 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 ]]

示例

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]]

更新於: 2021-11-24

237 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.