Sparse broadcasting division with a dense vector yields a dense array

Here is an example of the issue:

julia> using SparseArrays

julia> test = spzeros(4,4)
4×4 SparseMatrixCSC{Float64,Int64} with 0 stored entries

julia> test[2,2]=4
4

julia> test ./ 2
4×4 SparseMatrixCSC{Float64,Int64} with 1 stored entry:
  [2, 2]  =  2.0

julia> test ./ [2,2,2,2]
4×4 SparseMatrixCSC{Float64,Int64} with 16 stored entries:
  [1, 1]  =  0.0
  [2, 1]  =  0.0
  [3, 1]  =  0.0
  [4, 1]  =  0.0
  [1, 2]  =  0.0
  [2, 2]  =  2.0
  [3, 2]  =  0.0
  [4, 2]  =  0.0
  [1, 3]  =  0.0
  [2, 3]  =  0.0
  [3, 3]  =  0.0
  [4, 3]  =  0.0
  [1, 4]  =  0.0
  [2, 4]  =  0.0
  [3, 4]  =  0.0
  [4, 4]  =  0.0

Multiplication works just fine:

julia> test .* [2,2,2,2]
4×4 SparseMatrixCSC{Float64,Int64} with 1 stored entry:
  [2, 2]  =  8.0

Is this a bug, or am I missing something?

Interestingly, there are also different results for these two operations:

julia> test .* ([1]./[2,2,2,2])
4×4 SparseMatrixCSC{Float64,Int64} with 16 stored entries:
  [1, 1]  =  0.0
  [2, 1]  =  0.0
  [3, 1]  =  0.0
  [4, 1]  =  0.0
  [1, 2]  =  0.0
  [2, 2]  =  2.0
  [3, 2]  =  0.0
  [4, 2]  =  0.0
  [1, 3]  =  0.0
  [2, 3]  =  0.0
  [3, 3]  =  0.0
  [4, 3]  =  0.0
  [1, 4]  =  0.0
  [2, 4]  =  0.0
  [3, 4]  =  0.0
  [4, 4]  =  0.0

julia> other = [1]./[2,2,2,2]
4-element Array{Float64,1}:
 0.5
 0.5
 0.5
 0.5

julia> test .* other
4×4 SparseMatrixCSC{Float64,Int64} with 1 stored entry:
  [2, 2]  =  2.0

It looks like this is a known issue. See
https://github.com/JuliaLang/julia/issues/26561

It’s unclear whether or not anyone is working on it at the moment. There was a PR opened last year
https://github.com/JuliaLang/julia/pull/21747
but it hasn’t been touched in 2018 as far as I can tell.

1 Like

Thanks! I did search but didn’t come up with that issue, which describes what I’ve observed exactly.