scipy.sparse.linalg.

spsolve#

scipy.sparse.linalg.spsolve(A, b, permc_spec=None, use_umfpack=True)[源代码]#

求解稀疏线性系统 Ax=b,其中 b 可以是向量或矩阵。

参数:
Andarray 或稀疏数组或矩阵

方阵 A 将被转换为 CSC 或 CSR 形式

bndarray 或稀疏数组或矩阵

表示方程右边的矩阵或向量。如果是一个向量,b.shape 必须是 (n,) 或 (n, 1)。

permc_specstr,可选

如何置换矩阵的列以保留稀疏性。(默认:‘COLAMD’)

  • NATURAL:自然排序。

  • MMD_ATA:A^T A 结构的最小度排序。

  • MMD_AT_PLUS_A:A^T+A 结构的最小度排序。

  • COLAMD:近似最小度列排序 [1], [2].

use_umfpackbool,可选

如果为 True(默认),则使用 UMFPACK 进行求解 [3], [4], [5], [6] 。只有当 b 是向量并且安装了 scikits.umfpack 时才会被引用。

返回:
xndarray 或稀疏数组或矩阵

稀疏线性方程的解。如果 b 是一个向量,那么 x 是一个大小为 A.shape[1] 的向量。如果 b 是一个矩阵,那么 x 是一个大小为 (A.shape[1], b.shape[1]) 的矩阵。

备注

对于求解矩阵表达式 AX = B,此求解器假定结果矩阵 X 是稀疏的,这对于非常稀疏的输入来说通常是这种情况。如果得到的 X 是密集的,则构建此稀疏结果将相对昂贵。在这种情况下,考虑将 A 转换为密集矩阵并使用 scipy.linalg.solve 或其变体。

参考文献

[1]

T. A. Davis, J. R. Gilbert, S. Larimore, E. Ng, Algorithm 836: COLAMD, an approximate column minimum degree ordering algorithm, ACM Trans. on Mathematical Software, 30(3), 2004, pp. 377–380. DOI:10.1145/1024074.1024080

[2]

T. A. Davis, J. R. Gilbert, S. Larimore, E. Ng, A column approximate minimum degree ordering algorithm, ACM Trans. on Mathematical Software, 30(3), 2004, pp. 353–376. DOI:10.1145/1024074.1024079

[3]

T. A. Davis, Algorithm 832: UMFPACK - an unsymmetric-pattern multifrontal method with a column pre-ordering strategy, ACM Trans. on Mathematical Software, 30(2), 2004, pp. 196–199. https://dl.acm.org/doi/abs/10.1145/992200.992206

[4]

T. A. Davis, A column pre-ordering strategy for the unsymmetric-pattern multifrontal method, ACM Trans. on Mathematical Software, 30(2), 2004, pp. 165–195. https://dl.acm.org/doi/abs/10.1145/992200.992205

[5]

T. A. Davis and I. S. Duff, A combined unifrontal/multifrontal method for unsymmetric sparse matrices, ACM Trans. on Mathematical Software, 25(1), 1999, pp. 1–19. https://doi.org/10.1145/305658.287640

[6]

T. A. Davis and I. S. Duff, An unsymmetric-pattern multifrontal method for sparse LU factorization, SIAM J. Matrix Analysis and Computations, 18(1), 1997, pp. 140–158. https://doi.org/10.1137/S0895479894246905T.

示例

>>> import numpy as np
>>> from scipy.sparse import csc_array
>>> from scipy.sparse.linalg import spsolve
>>> A = csc_array([[3, 2, 0], [1, -1, 0], [0, 5, 1]], dtype=float)
>>> B = csc_array([[2, 0], [-1, 0], [2, 0]], dtype=float)
>>> x = spsolve(A, B)
>>> np.allclose(A.dot(x).toarray(), B.toarray())
True