# Why are duplicate entries in sparse matrices allowed?

**URL:** <https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159>\
**Category:** General Usage\
**Tags:** question, sparse\
**Created:** [February 17, 2017, 10:06pm UTC](https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159 "2017-02-17T22:06:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirestrepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mirestrepo/32/617_2.png) [@mirestrepo](https://discourse.julialang.org/u/mirestrepo)\
**Post date:** [February 17, 2017, 10:06pm UTC](https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159/1 "2017-02-17T22:06:41Z")

</div>

Why does the representation of sparse matrices allow for duplicates?

```julia
a = spzeros(2, 3)
a[[1,1],2]=1
a[[1],2]=2

```

Leads to

```julia
2×3 sparse matrix with 2 Float64 nonzero entries:
	[1, 2] = 2.0
	[1, 2] = 1.0

```

This behavior seems prone to computation errors. Why is this allowed? What is the use case?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 17, 2017, 10:11pm UTC](https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159/2 "2017-02-17T22:11:52Z")

</div>

> [@mirestrepo](#):
>
> a[[1],2]=2

just writes a 2 into a slot where a 1 was before. This is normal for any array, no?

---

<div class="post-metadata">

**Author:** ![mirestrepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mirestrepo/32/617_2.png) [@mirestrepo](https://discourse.julialang.org/u/mirestrepo)\
**Post date:** [February 17, 2017, 10:14pm UTC](https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159/3 "2017-02-17T22:14:00Z")

</div>

yes - but the internal representation of sparse matrix is now keeping two values for the same entry

```julia
2×3 sparse matrix with 2 Float64 nonzero entries:
	[1, 2] = 2.0
	[1, 2] = 1.0

```

---

<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:** [February 17, 2017, 10:15pm UTC](https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159/4 "2017-02-17T22:15:18Z")

</div>

```julia
julia> a = spzeros(2, 3)
2×3 SparseMatrixCSC{Float64,Int64} with 0 stored entries

julia> a[[1,1,1,1,1,1],2]=1
1

julia> a
2×3 SparseMatrixCSC{Float64,Int64} with 6 stored entries:
  [1, 2] = 1.0
  [1, 2] = 1.0
  [1, 2] = 1.0
  [1, 2] = 1.0
  [1, 2] = 1.0
  [1, 2] = 1.0

```

Looks likes a bug to me. It should remove duplicates.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 17, 2017, 10:21pm UTC](https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159/5 "2017-02-17T22:21:49Z")

</div>

Ah, yes, you’re right! Please file an issue (if there is non yet).

---

<div class="post-metadata">

**Author:** ![mirestrepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mirestrepo/32/617_2.png) [@mirestrepo](https://discourse.julialang.org/u/mirestrepo)\
**Post date:** [February 17, 2017, 10:23pm UTC](https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159/6 "2017-02-17T22:23:46Z")

</div>

I googled a little and it seems python and matlab do the same… I’m not sure if there are performance issues with removing duplicates. But allowing duplicates can cause many unwanted bugs in one’s code. Since this is not allowed in full matrices, I see no reason why it would make sense in sparse.

Sparse matrices are part of base… Is the main JuliaLang repo the place to file an issue?

I just wanted to make sure, it’s in fact an issue and not a know behavior

---

<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:** [February 17, 2017, 10:26pm UTC](https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159/7 "2017-02-17T22:26:32Z")

</div>

It is definitely a bug since other methods will compute incorrectly on this malformed matrix:

```julia
julia> a
2×3 SparseMatrixCSC{Float64,Int64} with 4 stored entries:
  [1, 2] = 2.0
  [2, 2] = 2.0
  [2, 2] = 2.0
  [2, 2] = 2.0

julia> countnz(a)
4

```

Yes, the julia main repo is the correct place to report the issue.

---

<div class="post-metadata">

**Author:** ![dorn-gerhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dorn-gerhard/32/27153_2.png) [@dorn-gerhard](https://discourse.julialang.org/u/dorn-gerhard)\
**Post date:** [February 1, 2023, 4:26pm UTC](https://discourse.julialang.org/t/why-are-duplicate-entries-in-sparse-matrices-allowed/2159/8 "2023-02-01T16:26:12Z")

</div>

Maybe this relates to duplicates in sparse matrices - how they are treated now:

Per default they are summed up

```julia
julia> A = sparse([2,2,2,2], [1,1,1,1], [2,1,3,4])
2×1 SparseMatrixCSC{Int64, Int64} with 1 stored entry:
  ⋅
 10

```

or aggregated using the function chosen as `combine` argument:

```julia
julia> A = sparse([2,2,2,2], [1,1,1,1], [2,1,3,4], 2, 1, max)
2×1 SparseMatrixCSC{Int64, Int64} with 1 stored entry:
 ⋅
 4

```

if only first (or last) argument shall be used use `(a,b) -> a` or `(a,b), -> b`

```julia
julia> A = sparse([2,2,2,2], [1,1,1,1], [2,1,3,4], 2, 1, (a,b) -> a)
2×1 SparseMatrixCSC{Int64, Int64} with 1 stored entry:
 ⋅
 2

```
