Julia 具有相同I,J向量的COO到CSC格式
现在,我调用K=sparse(I,J,V,n,n)函数来创建Julia中的稀疏(对称)K矩阵。我做了很多步骤 出于内存和效率考虑,我想修改K.nzval值,而不是创建新的稀疏K矩阵。注意,I和J向量对于每一步都是相同的,但非零值(V)在每一步都在变化。基本上,我们可以说我们知道COO格式的稀疏模式。(I和J不是有序的,可能有多个(I[I],J[I])条目) 我试图将我的COO格式向量与CSC/CSR格式存储相关联。然而,我发现它并不琐碎(至少目前如此) 有没有办法做到这一点或神奇的“稀疏”功能?谢谢 下面是一个与我的问题相关的示例代码Julia 具有相同I,J向量的COO到CSC格式,julia,sparse-matrix,csr,csc,Julia,Sparse Matrix,Csr,Csc,现在,我调用K=sparse(I,J,V,n,n)函数来创建Julia中的稀疏(对称)K矩阵。我做了很多步骤 出于内存和效率考虑,我想修改K.nzval值,而不是创建新的稀疏K矩阵。注意,I和J向量对于每一步都是相同的,但非零值(V)在每一步都在变化。基本上,我们可以说我们知道COO格式的稀疏模式。(I和J不是有序的,可能有多个(I[I],J[I])条目) 我试图将我的COO格式向量与CSC/CSR格式存储相关联。然而,我发现它并不琐碎(至少目前如此) 有没有办法做到这一点或神奇的“稀疏”功能?
n=19 # this is much bigger in reality ~ 100000. It is the dimension of a global stiffness matrix in finite element method, and it is highly sparse!
I = rand(1:n,12)
J = rand(1:n,12)
#
for k=365
I,J,val = computeVal() # I,J are the same as before, val is different, and might have duplicates in it.
K = sparse(I,J,val,19,19)
# compute eigs(K,...)
end
# instead I would like to decrease the memory/cost of these operations with following
# we know I,J
for k=365
I,J,val = computeVal() # I,J are the same as before, val is different, and might have duplicates in it.
# note that nonzeros(K) and val might have different size due to dublicate entries.
magical_sparse!(K,val)
# compute eigs(K,...)
end
# what I want to implement
function magical_sparse!(K::SparseMatrixCSC,val::Vector{Float64}) #(Note that this is not a complete function)
# modify K
K.nzval[some_array] = val
end
编辑:
这里给出了一个更具体的例子
n=4 # dimension of sparse K matrix
I = [1,1,2,2,3,3,4,4,1,4,1]
J = [1,2,1,2,3,4,4,3,2,4,2]
# note that the (I,J) -> (1,2) and (4,4) are duplicates.
V = [1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.]
function computeVal!(V)
# dummy function
# return modified V
rand!(V) # this part is involed, so I will just use rand to represent that we compute new values at each step for V vector.
end
for k=1:365
computeVal!(V)
K = sparse(I,J,V,n,n)
# do things with K
end
# Things to notice:
# println(length(V)) -> 11
# println(length(K.nzval)) -> 8
# I don't want to call sparse function at each step.
# instead I would like to decrease the cost of these operations with following
# we know I,J
for k=1:365
computeVal!(V)
magical_sparse!(K,V)
# do things with K
end
# what I want to implement
function magical_sparse!(K::SparseMatrixCSC,V::Vector{Float64}) #(Note that this is not a complete function)
# modify nonzeros of K and return K
end
当前问题的更新 根据问题的变化,新的解决方案是:
for k=365
computeVal!(V)
foldl((x,y)->(x[y[1],y[2]]+=y[3];x),fill!(K, 0.0), zip(I,J,V))
# do things with K
end
此解决方案使用了一些技巧,例如,使用fill将K
归零
,默认情况下返回K
,然后将其用作foldl
的初始值。同样,使用?foldl
应该可以弄清楚这里发生了什么
对老问题的回答
替换
for k=365
val = rand(12)
magical_sparse!(K,val)
# compute eigs(K,...)
end
与
我们应该做到这一点
使用
?兰德
和?非零
以获取有关各自功能的帮助。了解有关您的COO格式的更多详细信息(最好还有一些粘贴的代码)会有所帮助,我希望这会有所帮助!请看我的编辑。谢谢…使用nonzeros(K)。=val
来实现神奇的功能,甚至可以使用rand直接随机化nonzeros(K)
代码>该函数有帮助!非常感谢。但是,仍有一些部分需要改进。我还对问题进行了编辑,以澄清一些问题。实际上,val
不是一个随机向量。它是与[I,J]
坐标的新计算值相对应的数组。我想一个人需要把重复的部分加起来才能使用你的建议!我要试试看!再次感谢。这就是神奇的稀疏函数!非常感谢你!感谢患者对问题的编辑:)
for k=365
rand!(nonzeros(K))
# compute eigs(K,...)
end