簡體   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()?

我有以下代碼,其中我使用命令scipy.linalg.lu()計算給定方陣的L矩陣,然后再次執行相同的操作,只是使用scipy將其應用於給定矩陣的稀疏形式。 sparse.linalg.splu()。 這是代碼:

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”分解需要對行和列進行排列(請參閱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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM