SciPy - 線性代數



SciPy 使用經過最佳化的ATLAS LAPACKBLAS 庫構建。它具有非常快速的線性代數功能。所有這些線性代數例程都期望一個可以轉換為二維陣列的物件。這些例程的輸出也是一個二維陣列。

SciPy.linalg 與 NumPy.linalg

scipy.linalg 包含 numpy.linalg 中的所有函式。此外,scipy.linalg 還有一些 numpy.linalg 中沒有的其他高階函式。與 numpy.linalg 相比,使用 scipy.linalg 的另一個優勢在於,它始終使用 BLAS/LAPACK 支援進行編譯,而對於 NumPy,這是可選的。因此,SciPy 版本的速度可能會更快,具體取決於 NumPy 的安裝方式。

線性方程

scipy.linalg.solve 功能求解線性方程 a * x + b * y = Z,以求解未知的 x、y 值。

例如,假設需要求解以下聯立方程組。

x + 3y + 5z = 10

2x + 5y + z = 8

2x + 3y + 8z = 3

為了求解上述方程的 x、y、z 值,我們可以使用如下所示的矩陣逆法找到解向量。

$$\begin{bmatrix} x\\ y\\ z \end{bmatrix} = \begin{bmatrix} 1 & 3 & 5\\ 2 & 5 & 1\\ 2 & 3 & 8 \end{bmatrix}^{-1} \begin{bmatrix} 10\\ 8\\ 3 \end{bmatrix} = \frac{1}{25} \begin{bmatrix} -232\\ 129\\ 19 \end{bmatrix} = \begin{bmatrix} -9.28\\ 5.16\\ 0.76 \end{bmatrix}.$$

但是,最好使用linalg.solve 命令,它可能更快且數值穩定性更好。

solve 函式接受兩個輸入“a”和“b”,其中“a”表示係數,“b”表示相應的右側值,並返回解陣列。

讓我們考慮以下示例。

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy arrays
a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
b = np.array([2, 4, -1])

#Passing the values to the solve function
x = linalg.solve(a, b)

#printing the result array
print x

以上程式將生成以下輸出。

array([ 2., -2., 9.])

求行列式

方陣 A 的行列式通常表示為 |A|,是線性代數中經常使用的量。在 SciPy 中,這是使用det() 函式計算的。它接受一個矩陣作為輸入並返回一個標量值。

讓我們考慮以下示例。

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy array
A = np.array([[1,2],[3,4]])

#Passing the values to the det function
x = linalg.det(A)

#printing the result
print x

以上程式將生成以下輸出。

-2.0

特徵值和特徵向量

特徵值-特徵向量問題是最常用的線性代數運算之一。我們可以透過考慮以下關係找到方陣 (A) 的特徵值 (λ) 和相應的特徵向量 (v):

Av = λv

scipy.linalg.eig 計算普通或廣義特徵值問題的特徵值。此函式返回特徵值和特徵向量。

讓我們考慮以下示例。

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy array
A = np.array([[1,2],[3,4]])

#Passing the values to the eig function
l, v = linalg.eig(A)

#printing the result for eigen values
print l

#printing the result for eigen vectors
print v

以上程式將生成以下輸出。

array([-0.37228132+0.j, 5.37228132+0.j]) #--Eigen Values
array([[-0.82456484, -0.41597356], #--Eigen Vectors
       [ 0.56576746, -0.90937671]])

奇異值分解

奇異值分解 (SVD) 可以被認為是將特徵值問題擴充套件到非方陣的矩陣。

scipy.linalg.svd 將矩陣“a”分解為兩個酉矩陣“U”和“Vh”以及一個包含奇異值(實數,非負)的一維陣列“s”,使得 a == U*S*Vh,其中“S”是一個形狀合適的零矩陣,其主對角線為“s”。

讓我們考慮以下示例。

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy array
a = np.random.randn(3, 2) + 1.j*np.random.randn(3, 2)

#Passing the values to the eig function
U, s, Vh = linalg.svd(a)

# printing the result
print U, Vh, s

以上程式將生成以下輸出。

(
   array([
      [ 0.54828424-0.23329795j, -0.38465728+0.01566714j,
      -0.18764355+0.67936712j],
      [-0.27123194-0.5327436j , -0.57080163-0.00266155j,
      -0.39868941-0.39729416j],
      [ 0.34443818+0.4110186j , -0.47972716+0.54390586j,
      0.25028608-0.35186815j]
   ]),

   array([ 3.25745379, 1.16150607]),

   array([
      [-0.35312444+0.j , 0.32400401+0.87768134j],
      [-0.93557636+0.j , -0.12229224-0.33127251j]
   ])
)
廣告

© . All rights reserved.