[英]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_r
和perm_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.