# How to add a dense and a sparse vector and get a dense vector as result

**URL:** <https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802>\
**Category:** New to Julia\
**Created:** [June 12, 2021, 4:30pm UTC](https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802 "2021-06-12T16:30:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [June 12, 2021, 4:30pm UTC](https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802/1 "2021-06-12T16:30:47Z")

</div>

Hello,  
I’d like to add a sparse vector and a dense one and get a dense vector as result.

```julia
S=SparseVector(11,[1;11],[1;1])
D=ones(11)
display(S+D)
D+S

```

In both cases I get an 11-element SparseVector with 11 stored entries.  
How can I improve this?

---

<div class="post-metadata">

**Author:** ![swishmas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swishmas/32/8803_2.png) [@swishmas](https://discourse.julialang.org/u/swishmas)\
**Post date:** [June 12, 2021, 4:55pm UTC](https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802/2 "2021-06-12T16:55:26Z")

</div>

I’m not completely sure what you’d like here, but I might suggest

```julia
using SparseArrays
A = sparse(rand(100))
B = rand(100)
pos,vals = findnz(A)
@simd for i = 1:length(pos)
  @inbounds B[pos[i]] = vals[i]
end

```

Now, I don’t know if the addition operator between dense and sparse arrays could already do some of the obtaining elements and adding them to the dense elements. But the general strategy of finding elements with the `findnz` function could work here.

To use this on matrices, it should work similarly.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 12, 2021, 5:25pm UTC](https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802/3 "2021-06-12T17:25:19Z")

</div>

That looks like an awful lot of code for an addition of two vectors. `+(::Vector, ::SparseVector)` should just do the right thing imo.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [June 12, 2021, 5:36pm UTC](https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802/4 "2021-06-12T17:36:47Z")

</div>

You can call `Array` on the result to convert it to dense. I’m not well-versed in the sparse arrays ecosystem, so I’m not sure what the rationale is for dense + sparse returning sparse.

---

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [June 12, 2021, 5:58pm UTC](https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802/5 "2021-06-12T17:58:40Z")

</div>

` +(D,S)` returns a SparseVector too 🙁

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 12, 2021, 6:02pm UTC](https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802/6 "2021-06-12T18:02:41Z")

</div>

Yeah, `+(a, b)` is just another way to write `a + b`. I was just agreeing with you that this is unexpected behaviour.

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [June 12, 2021, 6:19pm UTC](https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802/7 "2021-06-12T18:19:32Z")

</div>

Another workaround is to store the result directly in-place:

```julia
julia> R = similar(D);

julia> @. R = D + S;

julia> R
11-element Vector{Float64}:
 2.0
 1.0
⋮

```

I’m not an expert on this but I think the reason for this behavior is because the addition falls back to broadcasted addition, and sparse array broadcasting has to deal with arbitrarily complex expressions (eg `2 .* D .* S .+ 3`, etc…) and also be type stable (return type doesn’t depend on the values of the arguments) so its not always trivial or maybe even possible to choose the “best” return type. It does seem though than in an expression involving only +/-'s, as soon as at least one argment is dense, the result should be dense. Maybe worth filing a Issue.

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [June 13, 2021, 4:32am UTC](https://discourse.julialang.org/t/how-to-add-a-dense-and-a-sparse-vector-and-get-a-dense-vector-as-result/62802/8 "2021-06-13T04:32:27Z")

</div>

I think this is related to [https://github.com/JuliaLang/julia/issues/36988](https://github.com/JuliaLang/julia/issues/36988)
