# Sparse broadcasting division with a dense vector yields a dense array

**URL:** <https://discourse.julialang.org/t/sparse-broadcasting-division-with-a-dense-vector-yields-a-dense-array/15005>\
**Category:** General Usage\
**Created:** [September 15, 2018, 10:07pm UTC](https://discourse.julialang.org/t/sparse-broadcasting-division-with-a-dense-vector-yields-a-dense-array/15005 "2018-09-15T22:07:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mboratko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mboratko/32/5377_2.png) [@mboratko](https://discourse.julialang.org/u/mboratko)\
**Post date:** [September 15, 2018, 10:07pm UTC](https://discourse.julialang.org/t/sparse-broadcasting-division-with-a-dense-vector-yields-a-dense-array/15005/1 "2018-09-15T22:07:44Z")

</div>

Here is an example of the issue:

```julia
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
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?

---

<div class="post-metadata">

**Author:** ![mboratko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mboratko/32/5377_2.png) [@mboratko](https://discourse.julialang.org/u/mboratko)\
**Post date:** [September 15, 2018, 10:33pm UTC](https://discourse.julialang.org/t/sparse-broadcasting-division-with-a-dense-vector-yields-a-dense-array/15005/2 "2018-09-15T22:33:41Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [September 16, 2018, 1:01am UTC](https://discourse.julialang.org/t/sparse-broadcasting-division-with-a-dense-vector-yields-a-dense-array/15005/3 "2018-09-16T01:01:23Z")

</div>

It looks like this is a known issue. See  
[https://github.com/JuliaLang/julia/issues/26561](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](https://github.com/JuliaLang/julia/pull/21747)  
but it hasn’t been touched in 2018 as far as I can tell.

---

<div class="post-metadata">

**Author:** ![mboratko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mboratko/32/5377_2.png) [@mboratko](https://discourse.julialang.org/u/mboratko)\
**Post date:** [September 16, 2018, 12:30pm UTC](https://discourse.julialang.org/t/sparse-broadcasting-division-with-a-dense-vector-yields-a-dense-array/15005/4 "2018-09-16T12:30:49Z")

</div>

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