【问题标题】:COO to CSC format with the same I,J vectors具有相同 I、J 向量的 COO 到 CSC 格式
【发布时间】: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


【解决方案1】:

当前问题的更新

根据问题的变化,新的解决方案是:

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

for k=365
  rand!(nonzeros(K))
  # compute eigs(K,...)
end

应该可以解决问题。

使用?rand!?nonzeros 获取有关各自功能的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-06
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2012-10-20
    相关资源
    最近更新 更多