Addition/Subtraction operations on sparse arrays

I think broadcasting checks whether the function maps zero to zero, and if so, the result is sparse. Subtracting 2 doesn’t obey that. You can easily write a function which manipulates only the nonzero values, if for some reason this makes sense:

julia> broadcast(x -> 100x, a)
2×2 SparseMatrixCSC{Float64, Int64} with 1 stored entry:
  ⋅    ⋅ 
  ⋅   4.64159

julia> sparsemap(f, x::SparseMatrixCSC) = SparseMatrixCSC(x.m, x.n, x.colptr, x.rowval, map(f, x.nzval));

julia> sparsemap(x -> x + 100, a)
2×2 SparseMatrixCSC{Int64, Int64} with 1 stored entry:
 ⋅    ⋅
 ⋅  101
1 Like