简体   繁体   English

那个scipy.linalg.lu()为什么不返回与scipy.sparse.linalg.splu()相同的L矩阵?

[英]How come that scipy.linalg.lu() does not return the same L matrix as scipy.sparse.linalg.splu()?

I have the following piece of code where I compute the L matrix of a given square matrix using the command scipy.linalg.lu() and then I do the same thing again except then applied to the sparse form of the given matrix using scipy.sparse.linalg.splu(). 我有以下代码,其中我使用命令scipy.linalg.lu()计算给定方阵的L矩阵,然后再次执行相同的操作,只是使用scipy将其应用于给定矩阵的稀疏形式。 sparse.linalg.splu()。 This is the code: 这是代码:

import numpy as np
from scipy.sparse.linalg import splu
from scipy.sparse import csc_matrix
import scipy.linalg
A1 = csc_matrix([[1., 0, 0.], [5., 0, 2], [0, -1., 0]])
A2 = np.array([[1., 0, 0.], [5., 0, 2], [0, -1., 0]])
B = splu(A1)
P,L,U = scipy.linalg.lu(A2)
print(L);print(csr_matrix.todense(B.L))

Which returns the following: 返回以下内容:

[[ 1.   0.   0. ]
 [ 0.   1.   0. ]
 [ 0.2 -0.   1. ]]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

As we can see, these matrices are not the same. 我们可以看到,这些矩阵并不相同。 Am I misunderstanding what both commands do or is something else going wrong? 我是误解了这两个命令的作用还是其他地方出错了? Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

I think the answer here is that the "SuperLU" decomposition of a sparse matrix requires permutations of both the rows and columns (see the docs ): 我认为答案是,稀疏矩阵的“ SuperLU”分解需要对行和列进行排列(请参阅docs ):

Pr * A * Pc = L * U

These are provided by the mapping of indices in the perm_r and perm_c attributes. 这些是通过perm_rperm_c属性中的索引映射提供的。 So, 所以,

Pr = csc_matrix((3,3))
Pr[B.perm_r, np.arange(3)] = 1
Pc = csc_matrix((3,3))
Pc[np.arange(3), B.perm_c] = 1

(Pr.T @ B.U @ B.L @ Pc.T).A

gives, as required: 根据需要给出:

array([[ 1.,  0.,  0.],
       [ 5.,  0.,  2.],
       [ 0., -1.,  0.]])

The same as the non-sparse result which requires only permutation of the L-matrix, P @ L @ U . 与仅需要对L矩阵进行排列的非稀疏结果相同,即P @ L @ U

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 scipy.linalg.lu 返回什么? - What does scipy.linalg.lu return? 为什么稀疏矩阵的“scipy.sparse.linalg.spilu”效率低于“scipy.linalg.lu”? - Why is 'scipy.sparse.linalg.spilu' less efficient than 'scipy.linalg.lu' for sparse matrix? 没有足够的内存来执行分解 expm scipy.sparse.linalg.slu - Not enough memory to perform factorization expm scipy.sparse.linalg.splu 为什么scipy.linalg.lu()在此代码中返回错误的方阵B分解矩阵? - Why does scipy.linalg.lu() return the wrong decomposition matrices of square matrix B in this code? 函数scipy.linalg.lu中的上三角矩阵是否总是呈行梯形形式? - Is the upper triangular matrix in function scipy.linalg.lu always in row echelon form? 如何理解scipy.linalg.lu_factor的pivot矩阵? - How to understand the pivot matrix of scipy.linalg.lu_factor? scipy.sparse.linalg.eigsh 返回相同矩阵的不同特征值 - scipy.sparse.linalg.eigsh returns different eigenvalues for the same matrix 为什么 scipy.linalg.LU 重复求解 Ax = b 如此缓慢? - Why is scipy.linalg.LU so slow for solving Ax = b repeatedly? Python特征向量:numpy.linalg,scipy.linalg和scipy.sparse.linalg之间的差异 - Python eigenvectors: differences among numpy.linalg, scipy.linalg and scipy.sparse.linalg 带有固定种子的 scipy.sparse.linalg.eigsh - scipy.sparse.linalg.eigsh with fixed seed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM