# Why does Julia promote full array to sparse array?

**URL:** <https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205>\
**Category:** Internals & Design\
**Created:** [August 3, 2020, 4:49pm UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205 "2020-08-03T16:49:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [August 3, 2020, 4:49pm UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205/1 "2020-08-03T16:49:09Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 3, 2020, 5:09pm UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205/2 "2020-08-03T17:09:30Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [August 3, 2020, 5:12pm UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205/3 "2020-08-03T17:12:33Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [August 4, 2020, 3:01am UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205/4 "2020-08-04T03:01:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [August 7, 2020, 3:42pm UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205/5 "2020-08-07T15:42:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [August 7, 2020, 3:50pm UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205/6 "2020-08-07T15:50:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [August 7, 2020, 9:57pm UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205/7 "2020-08-07T21:57:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 8, 2020, 7:46am UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205/8 "2020-08-08T07:46:22Z")

</div>

> [@lostella](#):
>
> will be closed if it’s intended behavior

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

---

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [August 10, 2020, 2:34pm UTC](https://discourse.julialang.org/t/why-does-julia-promote-full-array-to-sparse-array/44205/9 "2020-08-10T14:34:56Z")

</div>

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