gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:28:52 56 4
gpt4 key购买 nike

我有下面的代码,我使用命令 scipy.linalg.lu() 计算给定方阵的 L 矩阵,然后我再次执行相同的操作,除了然后应用于给定矩阵的稀疏形式使用 scipy.sparse.linalg.slu()。这是代码:

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

返回以下内容:

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

正如我们所见,这些矩阵并不相同。我是否误解了这两个命令的作用还是有其他问题?任何帮助表示赞赏。谢谢!

最佳答案

我认为这里的答案是稀疏矩阵的“SuperLU”分解需要行和列的排列(参见 the docs ):

Pr * A * Pc = L * U

这些由 perm_rperm_c 属性中的索引映射提供。所以,

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

根据需要给出:

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

与非稀疏结果相同,仅需要 L 矩阵的排列,P @ L @ U

关于python - 为什么 scipy.linalg.lu() 不返回与 scipy.sparse.linalg.splu() 相同的 L 矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53665503/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com