Julia:将 CHOLMOD 因子转换为稀疏矩阵并再次转换回来 [英] Julia: converting CHOLMOD factor to sparse matrix and back again
问题描述
我有一个稀疏矩阵 H
的 CHOLMOD 分解,我想编辑上、下和块对角因子的稀疏表示.我怎样才能做到这一点?当我运行以下命令时,最后一行不起作用.
I have a CHOLMOD factorization of a sparse matrix H
, and I want to edit the sparse representation of the upper, lower, and block diagonal factors. How can I do this? When I run the below, the last line doesn't work.
H = sprand(10,10,0.5)
fac = ldltfact(H; shift=0.0)
fD = fac[:D]
D = Base.SparseArrays.CHOLMOD.Sparse(fD)
有没有什么方法可以从一个稀疏矩阵反向转到一个CHOLMOD.factor
?
And is there any way to go in the reverse direction from a sparse matrix to a CHOLMOD.factor
?
推荐答案
提取 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
希望这会有所帮助.
这篇关于Julia:将 CHOLMOD 因子转换为稀疏矩阵并再次转换回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!