# Allocating column of sparseArrays adds explicit zeros

**URL:** <https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011>\
**Category:** Performance\
**Created:** [March 18, 2019, 9:24pm UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011 "2019-03-18T21:24:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrien](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Adrien](https://discourse.julialang.org/u/Adrien)\
**Post date:** [March 18, 2019, 9:24pm UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011/1 "2019-03-18T21:24:58Z")

</div>

I’m not sure if this is a bug or a feature but copying a sparse vector into a column of a sparse matrix keeps (or rather adds) some of the zeros explicitly:

```julia
 julia> using SparseArrays
 julia> m=sparse([[0,1,2,0,0] [0,3,0,4,0]])
5×2 SparseMatrixCSC{Int64,Int64} with 4 stored entries:
  [2, 1] = 1
  [3, 1] = 2
  [2, 2] = 3
  [4, 2] = 4

julia> v=sparse([1,2,0,0,0])
5-element SparseVector{Int64,Int64} with 2 stored entries:
  [1] = 1
  [2] = 2

julia> m[:,1]=v
5-element SparseVector{Int64,Int64} with 2 stored entries:
  [1] = 1
  [2] = 2

julia> m
5×2 SparseMatrixCSC{Int64,Int64} with 5 stored entries:
  [1, 1] = 1
  [2, 1] = 2
  [3, 1] = 0 <-----
  [2, 2] = 3
  [4, 2] = 4

julia> findnz(m)
([1, 2, 3, 2, 4], [1, 1, 1, 2, 2], [1, 2, 0, 3, 4])

```

I understand explicitly allocating a particular entry of the matrix to 0 does just that, but here the right hand side is a sparse vector, so I don’t get why this is happening.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 18, 2019, 9:28pm UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011/2 "2019-03-18T21:28:36Z")

</div>

It didn’t allocate an explicit zero, it just chose not to drop the stored entry (that was there from the beginning). Use `dropzeros!` to drop eventual stored zeros.

---

<div class="post-metadata">

**Author:** ![Adrien](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Adrien](https://discourse.julialang.org/u/Adrien)\
**Post date:** [March 18, 2019, 9:32pm UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011/3 "2019-03-18T21:32:59Z")

</div>

I know about dropzeros, but the point is that it was not explicitly stored in v, so I wouldn’t say it was there from the beginning ! Besides, there were three zeros in v, and only one shows up in m so it’s definitely a strange behaviour don’t you think ?

---

<div class="post-metadata">

**Author:** ![Adrien](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Adrien](https://discourse.julialang.org/u/Adrien)\
**Post date:** [March 18, 2019, 9:38pm UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011/4 "2019-03-18T21:38:37Z")

</div>

Oh, I see, that’s because the 3 that was already there is replaced by 0 and not dropped ?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 18, 2019, 10:44pm UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011/5 "2019-03-18T22:44:37Z")

</div>

> [@Adrien](#):
>
> Oh, I see, that’s because the 3 that was already there is replaced by 0 and not dropped ?

Yes.

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [March 19, 2019, 12:19am UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011/6 "2019-03-19T00:19:01Z")

</div>

I find this behavior surprising. Since assignment has to handle allocating new elements, it seems odd that it wouldn’t also deallocate elements when appropriate.

---

<div class="post-metadata">

**Author:** ![Adrien](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Adrien](https://discourse.julialang.org/u/Adrien)\
**Post date:** [March 19, 2019, 6:52am UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011/7 "2019-03-19T06:52:55Z")

</div>

I do find this surprising too, at least as a default behavior, whenever I write A=B I’d expect A and B to be then, well, pretty much identical.

---

<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:** [March 19, 2019, 7:36am UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011/8 "2019-03-19T07:36:39Z")

</div>

> [@benninkrs](#):
>
> it seems odd that it wouldn’t also deallocate elements when appropriate.

Rearranging the sparsity structure is quite costly. If you are iteratively updating a matrix using a loop, doing this for each element would be very expensive. Better to have it performed explicitly (eg when you are done updating).

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [March 19, 2019, 10:53pm UTC](https://discourse.julialang.org/t/allocating-column-of-sparsearrays-adds-explicit-zeros/22011/9 "2019-03-19T22:53:56Z")

</div>

Sure. My point is, it already seemed to be rearranging the sparsity structure by allocating new elements. So I guess it’s just doing the minimal necessary restructuring (?).
