# Can I set the default operation of a sparse Matrix to not store zeros?

**URL:** <https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 25, 2022, 6:57pm UTC](https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007 "2022-04-25T18:57:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [April 25, 2022, 6:57pm UTC](https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007/1 "2022-04-25T18:57:28Z")

</div>

I am building a sparse matrix `II`, however, when I assign some of its elements by zeros they are being stored with zero values unless I use `dropzeros(II)`.

1- Why using `sparse(II)` below not dropping zeros from II?  
2- Is there a way to set (as default) at the first of my code that assigning zeros will not be stored into `II`?

```julia
II=sparse([1 0;1 1;1 1;1 1;1 1])
julia> II=sparse([1 0;1 1;1 1;1 1;1 1])
5×2 SparseMatrixCSC{Int64, Int64} with 9 stored entries:
 1 ⋅
 1 1
 1 1
 1 1
 1 1
II[1:2,1]=sparse([0 0]);
julia> II
5×2 SparseMatrixCSC{Int64, Int64} with 9 stored entries:
 0 ⋅
 0 1
 1 1
 1 1
 1 1
julia> sparse(II)
5×2 SparseMatrixCSC{Int64, Int64} with 9 stored entries:
 0 ⋅
 0 1
 1 1
 1 1
 1 1
julia> dropzeros(II)
5×2 SparseMatrixCSC{Int64, Int64} with 7 stored entries:
 ⋅ ⋅
 ⋅ 1
 1 1
 1 1
 1 1

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 25, 2022, 7:03pm UTC](https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007/2 "2022-04-25T19:03:53Z")

</div>

> [@Amro](#):
>
> Why using `sparse(II)` below not dropping zeros from II?

Because you might change them later, i.e. it could be intentional?

> [@Amro](#):
>
> Is there a way to set (as default) at the first of my code that assigning zeros will not be stored into `II` ?

Not a real answer, but you could filter them out beforehand?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [April 25, 2022, 7:26pm UTC](https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007/3 "2022-04-25T19:26:03Z")

</div>

> [@goerch](#):
>
> > [@Amro](#):
> >
> > Is there a way to set (as default) at the first of my code that assigning zeros will not be stored into `II` ?
> 
> Not a real answer, but you could filter them out beforehand?

Thanks for your reply!  
This is because I have a `for loop` and inside it I change the value of `II` and then calculate `X = A \II`. So, I want to avoid using `dropzeros(II)` in the loop.

```julia
for i:11000
# change in II
II = dropzeros(II)
X = A \II
end

```

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [April 26, 2022, 9:30am UTC](https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007/4 "2022-04-26T09:30:29Z")

</div>

Note that you should do `dropzeros!(II)` and not `II=dropzeros(II)`. In any case, this is not advisable if performance is a concern.  
The real reason is that removing/inserting a single entry in `SparseArrayCSC` is very slow (O(n)) where `n` is the number of nonzeros. So that loop would be reeeally slow (O(n^2)). True, there is an overhead in `dropzeros!` for finding where the new zero is, but it would still be O(n^2) without that. Do you really need remove it? It’s probably faster by just setting it to zero.

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [April 26, 2022, 1:29pm UTC](https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007/6 "2022-04-26T13:29:46Z")

</div>

Generally yes, but of course it can depend on many things. I would benchmark to be sure.

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [April 26, 2022, 1:46pm UTC](https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007/8 "2022-04-26T13:46:42Z")

</div>

well, constructing the sparse matrix would be also slow (possibly slower!), so I wouldn’t advise that. Note that `dropzeros!` would not be needed in this case. But it’s really easy to benchmark, just use the `BenchmarkTools` package with the `@btime` macro. e.g.

```julia
julia> @btime $A \ $II 

julia> @btime sparse($A) \ sparse($II)

```

etc 😄

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [April 26, 2022, 2:23pm UTC](https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007/10 "2022-04-26T14:23:47Z")

</div>

> [@Amro](#):
>
> put `dropzeros!` because in the WME above when I used only `sparse(II)` , it didnt drop the zeros.

That’s because the input in that case was already a sparse matrix. When the input is a dense array, it will drop the zeros on construction.

```julia
julia> sparse([0,0,1])
3-element SparseVector{Int64, Int64} with 1 stored entry:
  [3] = 1

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 26, 2022, 3:59pm UTC](https://discourse.julialang.org/t/can-i-set-the-default-operation-of-a-sparse-matrix-to-not-store-zeros/80007/11 "2022-04-26T15:59:58Z")

</div>

> [@Amro](#):
>
> This is because I have a `for loop` and inside it I change the value of `II`

In general, constructing sparse matrices element-by-element with `A[i,j] = val` is extremely slow. If you want to construct matrices element-by-element, use the `sparse(I,J,V)` constructor.

(Changing entries of a sparse matrix without changing the sparsity pattern is fast, though.)
