我试图更好地理解对称()函数如何作用于sparseCSC矩阵。
我存储了对称矩阵A的下三角部分,然后创建了sA = Symmetric(A),以允许Julia库函数"\“和"eigs”将矩阵视为对称矩阵。
如果我想有效地修改sA的元素,我是不是必须在A上操作?我的意思是,在A中,我可以访问结构属性nzval并更改它的值,但是在sA中,我似乎只能使用密集矩阵的标准切片操作来访问数据。
例如,让我们假设我想要在位置7,3添加某个值X。使用稀疏矩阵表示法,我将只对与A的第3列相关联的行使用二进制搜索,然后将该值添加到A.nzval的适当条目。另一方面,在sA上,我只能调用sA7,3 = X。
有没有办法直接从sA访问结构属性?将A和sA都作为对同一对象的引用看起来不像是一个干净的想法,但我不确定如何避免这种情况。
发布于 2021-07-18 06:30:03
虽然这可能是也可能不是公共API的一部分,但看起来“保留A
”确实是Symmetric(A)
在内部所做的事情:
julia> A = sprand(10,10,0.03)
10×10 SparseMatrixCSC{Float64, Int64} with 2 stored entries:
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ 0.437621 ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ 0.913864 ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
julia> sA = Symmetric(A)
10×10 Symmetric{Float64, SparseMatrixCSC{Float64, Int64}}:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.437621 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.437621 0.0 0.0 0.913864 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.913864 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
julia> sA.<TAB>
data uplo
julia> sA.data
10×10 SparseMatrixCSC{Float64, Int64} with 2 stored entries:
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ 0.437621 ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ 0.913864 ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
julia> sA.data === A
true
julia> sA.data.<TAB>
colptr m n nzval rowval
考虑到这一点,我想您可以忘记A
并对sA.data
进行操作--但是出于同样的原因,对于这个实现,保留您自己对A
的引用并不比同一个变量有两个名称更糟糕,这通常不是一个大问题。
https://stackoverflow.com/questions/68421625
复制相似问题