# Addition/Subtraction operations on sparse arrays

**URL:** <https://discourse.julialang.org/t/addition-subtraction-operations-on-sparse-arrays/64241>\
**Category:** General Usage\
**Tags:** broadcast, sparse\
**Created:** [July 7, 2021, 11:03pm UTC](https://discourse.julialang.org/t/addition-subtraction-operations-on-sparse-arrays/64241 "2021-07-07T23:03:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [July 7, 2021, 11:03pm UTC](https://discourse.julialang.org/t/addition-subtraction-operations-on-sparse-arrays/64241/1 "2021-07-07T23:03:35Z")

</div>

If I have a sparse array, is there a way to only broadcast operations onto the non-zero values in that array? I find myself needing this operation quite often.

A broadcast will obviously perform the operation on every element, including the zero elements as such:

```julia
using SparseArrays
a = sparse([2], [2], [1])

```

> 2×2 SparseMatrixCSC{Int64, Int64} with 1 stored entry:  
> ⋅ ⋅  
> ⋅ 1

```julia
a .- 2

```

> 2×2 SparseMatrixCSC{Int64, Int64} with 4 stored entries:  
> -2 -2  
> -2 -1

What if instead I wanted to ignore the non-zero elements in the broadcast? Is there some sort of `sparsebroadcast` mechanism? Something like:

```julia
sparsebroadcast(-, a, 2)

```

> 2×2 SparseMatrixCSC{Int64, Int64} with 1 stored entry:  
> ⋅ ⋅  
> ⋅ -1

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 7, 2021, 11:38pm UTC](https://discourse.julialang.org/t/addition-subtraction-operations-on-sparse-arrays/64241/2 "2021-07-07T23:38:45Z")

</div>

TLDR is that this would require a different type, since `SparseMatrixCSC` only allows `0` as the sparse element. This is because for linear algebra applications (ie matrix multiplication etc) knowing that `0*x=0` is what allows the operations to be efficient.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 7, 2021, 11:52pm UTC](https://discourse.julialang.org/t/addition-subtraction-operations-on-sparse-arrays/64241/3 "2021-07-07T23:52:20Z")

</div>

I think broadcasting checks whether the function maps zero to zero, and if so, the result is sparse. Subtracting 2 doesn’t obey that. You can easily write a function which manipulates only the nonzero values, if for some reason this makes sense:

```julia
julia> broadcast(x -> 100x, a)
2×2 SparseMatrixCSC{Float64, Int64} with 1 stored entry:
  ⋅ ⋅ 
  ⋅ 4.64159

julia> sparsemap(f, x::SparseMatrixCSC) = SparseMatrixCSC(x.m, x.n, x.colptr, x.rowval, map(f, x.nzval));

julia> sparsemap(x -> x + 100, a)
2×2 SparseMatrixCSC{Int64, Int64} with 1 stored entry:
 ⋅ ⋅
 ⋅ 101

```

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [July 7, 2021, 11:58pm UTC](https://discourse.julialang.org/t/addition-subtraction-operations-on-sparse-arrays/64241/4 "2021-07-07T23:58:33Z")

</div>

Thanks, that’s exactly what I was looking for.

The reason I’m looking for this is because I’m implementing something in the form `a ./ (b .+ c)` where `a` and `b` are sparse and `c` is a scalar, dense vector, or dense matrix. So skipping addition operations in the denominator can be a big savings because they are ignored in the division anyway.
