Why does Julia promote full array to sparse array?

Why does Julia promote full arrays to sparse arrays? If I try to subtract a sparse array from a full array, why does the full array become a sparse array? This doesn’t make any sense to me. It makes much more sense in my head to keep it as a full array and efficiently do the subtraction as opposed to promoting the full array?

An example is below. The normal array arr is converted to a sparse array during the subtraction.

julia> spArr = sparsevec([1,3,10], [.1,.3,.9]);

julia> arr = ones(10)
10-element Array{Float64,1}:
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0

julia> arr -= spArr
10-element SparseVector{Float64,Int64} with 10 stored entries:
  [1 ]  =  0.9
  [2 ]  =  1.0
  [3 ]  =  0.7
  [4 ]  =  1.0
  [5 ]  =  1.0
  [6 ]  =  1.0
  [7 ]  =  1.0
  [8 ]  =  1.0
  [9 ]  =  1.0
  [10]  =  0.1
2 Likes

Arr isn’t mutated, it is simply reassigned. Your question is just that why does the substraction return a sparse array. Someone else will be more familiar with that heuristic…

Try broadcasting the operation. This should return the expected result.

julia> arr .-= spArr
10-element Array{Float64,1}:
 0.9
 1.0
 0.7
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 0.09999999999999998
4 Likes

It’s a good question though, why the elementwise operation ends up sparse. That’s not what I would have expected. Seems like a bug to me.

14 Likes

I was just passing by and noticed this thread: is this tracked in an issue on GitHub? If so, could you link it here?

No, I have not created an issue on GitHub yet. I was unsure whether it was bug or just a design choice. Should I create one?

I think it’s better to open one: worst case the issue will be closed if it’s intended behavior

3 Likes

This should be documented then, at least as a comment in the code.

1 Like

Sure thing. Here is the link to the issue https://github.com/JuliaLang/julia/issues/36988

2 Likes