Scipy 教程-SciPy 线性代数
SciPy 建立在 ATLAS LAPACK 和 BLAS 库之上,提供了非常快速的线性代数功能。线性代数例程接受二维数组对象,并且输出也是二维数组。如果我们想要更快的计算速度,那么我们必须在这方面深入研究。
linalg.solve()
线性方程
linalg.solve 用于求解线性方程 a * x + b * y = Z 的未知 x, y 值。
x + 3y + 10z = 10
2x + 12y + 7z = 18
5x + 8y + 8z = 30
import numpy as npfrom scipy import linalg# We are trying to solve a linear algebra system which can be given as# x + 3y +10z = 10# 2x + 12y + 7z = 18# 5x + 8y + 8z = 30# Creating input arraya = np.array([[1, 3, 10], [2, 12, 7], [5, 8, 8]])# Solution Arrayb = np.array([[10], [18], [30]])# Solve the linear algebrax = linalg.solve(a, b)# Print resultsprint(x)# Checking Resultsprint("\n Checking results, Vectors must be zeros")print(a.dot(X) - b)
[[4.55393586][0.51311953][0.39067055]]Checking results, Vectors must be zeros[[0.][0.][0.]]
在上述程序中,我们声明了 a 和 b 作为变量,其中 a 存储方程的系数,b 存储等式右边的值。变量 x 存储评估的解。
专属福利 👉点击领取:最全Python资料合集
求行列式
使用 linalg.det() 函数可以找到方阵的行列式。线性代数中的行列式 A 通常表示为 |A|。它接受一个矩阵并返回一个标量值。
from scipy import linalgimport numpy as np#Declaring the numpy arrayA = np.array([[5,9],[8,4]])#Passing the values to the det functionx = linalg.det(A)#printing the resultprint(x)
输出:
-52
特征值和特征向量
在线性代数中,寻找特征值和特征向量问题是最常见的问题。我们可以使用 linalg.eig() 函数找到方阵 (A) 的特征值 (?) 和相应的特征向量 (v)。考虑以下示例:
#importing the scipy and numpy packagesfrom scipy import linalgimport numpy as np#Declaring the numpy arraya = np.array([[3,2],[4,6]])#Passing the values to the eig functionl, v = linalg.eig(a)#printing the result for eigenvaluesprint(l)#printing the result for eigenvectorsprint(v)
[-0.37228132+0.j 5.37228132+0.j][[-0.82456484 -0.41597356][ 0.56576746 -0.90937671]]
SciPy svd
热门推荐