Scipy 简明教程

SciPy - Linalg

SciPy 是基于经过优化的 ATLAS LAPACKBLAS 库构建的。它具有非常快的线性代数功能。所有这些线性代数例程都预期使用可转换为二维阵列的对象。这些例程的输出也是一个二维阵列。

SciPy is built using the optimized ATLAS LAPACK and BLAS libraries. It has very fast linear algebra capabilities. All of these linear algebra routines expect an object that can be converted into a two-dimensional array. The output of these routines is also a two-dimensional array.

SciPy.linalg vs NumPy.linalg

scipy.linalg 包含 numpy.linalg 中的所有函数。另外,scipy.linalg 还有 numpy.linalg 中没有的某些高级函数。与 numpy.linalg 相比,使用 scipy.linalg 的另一个优势在于它始终与 BLAS/LAPACK 支持一起编译,而对于 NumPy 则是可选的。因此,根据 NumPy 的安装方式,使用 SciPy 的版本可能会更快。

A scipy.linalg contains all the functions that are in numpy.linalg. Additionally, scipy.linalg also has some other advanced functions that are not in numpy.linalg. Another advantage of using scipy.linalg over numpy.linalg is that it is always compiled with BLAS/LAPACK support, while for NumPy this is optional. Therefore, the SciPy version might be faster depending on how NumPy was installed.

Linear Equations

scipy.linalg.solve 特性用于求解线性方程 a * x + b * y = Z,未知数为 x、y。

The scipy.linalg.solve feature solves the linear equation a * x + b * y = Z, for the unknown x, y values.

举例来说,假设希望求解下列联立方程组:

As an example, assume that it is desired to solve the following simultaneous equations.

x + 3y + 5z = 10

x + 3y + 5z = 10

2x + 5y + z = 8

2x + 5y + z = 8

2x + 3y + 8z = 3

2x + 3y + 8z = 3

为了对 x、y、z 求解上述方程,我们可以使用矩阵逆来找到解向量,如下所示:

To solve the above equation for the x, y, z values, we can find the solution vector using a matrix inverse as shown below.

\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 命令。

However, it is better to use the linalg.solve command, which can be faster and more numerically stable.

solve 函数接受两个输入“a”和“b”,其中“a”表示系数,“b”表示右侧相应的值,并返回解数组。

The solve function takes two inputs ‘a’ and ‘b’ in which ‘a’ represents the coefficients and ‘b’ represents the respective right hand side value and returns the solution array.

让我们考虑以下示例。

Let us consider the following example.

#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

上述程序将生成以下输出。

The above program will generate the following output.

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

Finding a Determinant

方阵 A 的行列式通常表示为 |A|,是线性代数中经常使用的量。在 SciPy 中,使用 det() 函数计算该行列式。它将矩阵作为输入,并返回标量值。

The determinant of a square matrix A is often denoted as |A| and is a quantity often used in linear algebra. In SciPy, this is computed using the det() function. It takes a matrix as input and returns a scalar value.

让我们考虑以下示例。

Let us consider the following example.

#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

上述程序将生成以下输出。

The above program will generate the following output.

-2.0

Eigenvalues and Eigenvectors

特征值-特征向量问题是使用最普遍的线性代数运算之一。我们可以通过考虑以下关系来找到方阵(A)的特征值(λ)和相应的特征向量(v)−

The eigenvalue-eigenvector problem is one of the most commonly employed linear algebra operations. We can find the Eigen values (λ) and the corresponding Eigen vectors (v) of a square matrix (A) by considering the following relation −

Av = λv

Av = λv

scipy.linalg.eig 计算常规特征值问题或广义特征值问题的特征值。该函数返回特征值和特征向量。

scipy.linalg.eig computes the eigenvalues from an ordinary or generalized eigenvalue problem. This function returns the Eigen values and the Eigen vectors.

让我们考虑以下示例。

Let us consider the following example.

#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

上述程序将生成以下输出。

The above program will generate the following output.

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

Singular Value Decomposition

奇异值分解 (SVD) 可被视为将特征值问题扩展到非方阵的矩阵。

A Singular Value Decomposition (SVD) can be thought of as an extension of the eigenvalue problem to matrices that are not square.

scipy.linalg.svd 将矩阵“a”分解为两个酉矩阵“U”和“Vh”以及奇异值(实数、非负数)的一维数组“s”,使得 a == U*S*Vh,其中“S”是以主对角线“s”为适形矩阵的零矩阵。

The scipy.linalg.svd factorizes the matrix ‘a’ into two unitary matrices ‘U’ and ‘Vh’ and a 1-D array ‘s’ of singular values (real, non-negative) such that a == U*S*Vh, where ‘S’ is a suitably shaped matrix of zeros with the main diagonal ‘s’.

让我们考虑以下示例。

Let us consider the following example.

#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

上述程序将生成以下输出。

The above program will generate the following output.

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