Modifying a sparse matrix non-zero indices


#1

I’m working in a problem where I have a sparse Jacobian matrix with 16 values in each row. I would like to update the matrix by just modifying its non-zero values. The problem is I would like to update both the non-zero values and indices, but the indices are immutable. How should I approach this?


#2

You can update the colptr and rowval vectors directly, as in the example below, but it’s not very user friendly.

julia> S = sprand(3, 3, 0.25)
3×3 SparseMatrixCSC{Float64,Int64} with 2 stored entries:
  [2, 1]  =  0.00723938
  [3, 3]  =  0.9488

julia> Matrix(S)
3×3 Array{Float64,2}:
 0.0         0.0  0.0
 0.00723938  0.0  0.0
 0.0         0.0  0.9488

julia> S.colptr
4-element Array{Int64,1}:
 1
 2
 2
 3

julia> S.rowval
2-element Array{Int64,1}:
 2
 3

julia> S.colptr[3] = 3; S.rowval[2] = 1;

julia> S
3×3 SparseMatrixCSC{Float64,Int64} with 2 stored entries:
  [2, 1]  =  0.00723938
  [1, 2]  =  0.9488

julia> Matrix(S)
3×3 Array{Float64,2}:
 0.0         0.9488  0.0
 0.00723938  0.0     0.0
 0.0         0.0     0.0