Sparsevector .+=

A = sparsevec([0, 0, 1, 1])
B = sparsevec([1, 1, 0, 0])
A .+= B
full(A)

gives

4-element Array{Int64,1}:
1
1
1
0

which doesnt look right?

In general you cannot guarantee that the sum (or broadcasted function) of 2 (or more) sparse vectors is sparse.

This is a really bad bug that we need to fix: https://github.com/JuliaLang/julia/issues/21693.

The problem is that you’re effectively doing A .= A .+ B — both reading from and mutating A at the same time. We simply don’t support that at the moment, but it needs to be an error.

2 Likes

I think in the issue it’s not decided yet whether it should be an error or not. There are strong arguments (IMHO) to make this work, doing a copy under the hood if needed to ensure that this generic syntax works for sparse arrays.