朱莉娅:将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
希望这会有所帮助.
这篇关于朱莉娅:将CHOLMOD因子转换为稀疏矩阵,然后再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!