且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Julia:将 CHOLMOD 因子转换为稀疏矩阵并再次转换回来

更新时间:2023-02-17 20:59:45

提取 ldltfact 的相关分解矩阵可能有点乏味.以下示例显示了与问题中的示例类似的示例,最终测试提取的矩阵恢复了原始分解矩阵:

Extracting the relevant factorization matrices of ldltfact can be a little tedious. The following example shows an example similar to the one in the question with a final test that the extracted matrices recover the original factorized one:

srand(1)
pre = sprand(10,10,0.5)
H = pre + pre' + speye(10,10)

fac = ldltfact(H; shift=0.0)
P = sparse(1:size(H,1),fac[:p],ones(size(H,1)))
LD = sparse(fac[:LD]) # this matrix contains both D and L embedded in it

L = copy(LD)
for i=1:size(L,1)
  L[i,i] = 1.0
end

D = sparse(1:size(L,1),1:size(L,1),diag(LD))

PHP = P*H*P'
LDL = L*D*L'

using Base.Test
@test PHP ≈ LDL

预期输出(以及 Julia v0.6.3 上的实际输出):

The expected output (and actual on Julia v0.6.3):

julia> @test PHP ≈ LDL
Test Passed

希望这会有所帮助.