【发布时间】:2018-06-02 08:49:12
【问题描述】:
现在,我正在调用 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
【问题讨论】:
-
了解更多关于您所拥有的 COO 格式的详细信息(最好还有一些粘贴的代码)会有所帮助
-
我希望这会有所帮助!请看我的编辑。谢谢...
-
使用
nonzeros(K) .= val实现神奇的功能甚至直接随机化nonzeros(K)使用rand! -
该功能有帮助!谢谢你。但是,仍有部分需要处理。我还编辑了问题以澄清一些内容。事实上,
val并不是一个随机向量。它是对应于[I,J]坐标的新计算值的数组。我想需要总结重复项才能使用您的建议!我会试一下!再次感谢。 -
这就是神奇的稀疏函数!非常感谢!感谢患者对问题的编辑:)
标签: julia sparse-matrix csr csc