SparseArrays.jl icon indicating copy to clipboard operation
SparseArrays.jl copied to clipboard

Broadcast operations across multiple dimensions materialize zeros in sparse matrices

Open kescobo opened this issue 4 years ago • 0 comments

Broadcasting division where the second argument has multiple dimensions causes the zeros in the matrix to be materialized

julia> using SparseArrays

julia> m = sparse([0 2; 1 0])
2×2 SparseMatrixCSC{Int64, Int64} with 2 stored entries:
 ⋅  2
 1  ⋅

julia> m ./ 2
2×2 SparseMatrixCSC{Float64, Int64} with 2 stored entries:
  ⋅   1.0
 0.5   ⋅

julia> m ./ [1 2]
2×2 SparseMatrixCSC{Float64, Int64} with 4 stored entries:
 0.0  1.0
 1.0  0.0

julia> m ./ [1, 2]
2×2 SparseMatrixCSC{Float64, Int64} with 4 stored entries:
 0.0  2.0
 0.5  0.0

But doesn't happen with multiplication or addition

julia> m .+ [0 0]
2×2 SparseMatrixCSC{Int64, Int64} with 2 stored entries:
 ⋅  2
 1  ⋅

julia> m .* [2 2]
2×2 SparseMatrixCSC{Int64, Int64} with 2 stored entries:
 ⋅  4
 2  ⋅

julia> m .* [2. 2]
2×2 SparseMatrixCSC{Float64, Int64} with 2 stored entries:
  ⋅   4.0
 2.0   ⋅

Not sure it's related, but another weird case was identified by @mcabbott on slack eg

julia> m .* begin inv.([1 2]) end
2×2 SparseMatrixCSC{Float64, Int64} with 2 stored entries:
  ⋅   1.0
 1.0   ⋅

julia> m .* inv.([1 2])
2×2 SparseMatrixCSC{Float64, Int64} with 4 stored entries:
 0.0  1.0
 1.0  0.0

I did some searching around, and couldn't tell if it's related to JuliaLang/julia#36551, but it doesn't seem to matter if dividing by a sparse array, eg

julia> julia> m ./ sparse([1 2])
2×2 SparseMatrixCSC{Float64, Int64} with 4 stored entries:
 0.0  1.0
 1.0  0.0

This was checked on 1.5 release, the release-1.6 branch from a couple of days ago, and a few hours old master.

kescobo avatar Dec 28 '20 21:12 kescobo