# Sparse matrix of vectors

**URL:** <https://discourse.julialang.org/t/sparse-matrix-of-vectors/125636>\
**Category:** General Usage\
**Tags:** sparse\
**Created:** [February 6, 2025, 9:54pm UTC](https://discourse.julialang.org/t/sparse-matrix-of-vectors/125636 "2025-02-06T21:54:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pulk\_jain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pulk_jain/32/204900_2.png) [@pulk\_jain](https://discourse.julialang.org/u/pulk_jain)\
**Post date:** [February 6, 2025, 9:54pm UTC](https://discourse.julialang.org/t/sparse-matrix-of-vectors/125636/1 "2025-02-06T21:54:28Z")

</div>

Hi, I wanted to store some information as a sparse matrix of vectors (or a tuple) to save on memory requirements.  
I am simulating a system of N(~50) particles in a box. If two particles contact then there are interactions between them which requires me to store an 8 vector array for each contact.  
I wanted to create a sparse matrix S such than

```julia
S[particle_id1, particle_id2] = [1,2,3,..8] if contact
S[particle_id1, particle_id2] = empty if no contact

```

I am able to assign the values by `S[particle_id1, particle_id2] = [1,2,3,..8]` . But how do I delete a value that is no longer needed? I saw a function `dropzeros` but that would require defining a “zero”. Whenever I try to access any “zero” element in the matrix, I am faced with the following error:  
`MethodError: no method matching zero(::Type{Vector{Int64}})`  
I tried defining a function `zero(::Type{Vector{Int64}}) = 0` but that didn’t help.  
I am just interested in storing, and retrieving data and don’t want to do any algebra with the matrix. Having a sparse matrix makes it easy to retrieve things by doing `findnnz(S[particle_id1,:])`.

What I want to know is:

1. How do I define a “zero” in such scenario?
2. Is there a better approach to achieve my goals?

I haven’t worked with sparse arrays before, so any help is appreciated. Thanks

Edit: I realized I could just do `Base.zero(::Type{Vector{Int64}}) = 0` and that does the job.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 6, 2025, 10:22pm UTC](https://discourse.julialang.org/t/sparse-matrix-of-vectors/125636/2 "2025-02-06T22:22:57Z")

</div>

> [@pulk\_jain](#):
>
> of N(~50) particles in a box.

that’s quite small, can’t you just use an Array that is 50 by 50 by 8? And this would be a symmetric matrix (since contact is mutual?)

---

<div class="post-metadata">

**Author:** ![pulk\_jain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pulk_jain/32/204900_2.png) [@pulk\_jain](https://discourse.julialang.org/u/pulk_jain)\
**Post date:** [February 6, 2025, 10:24pm UTC](https://discourse.julialang.org/t/sparse-matrix-of-vectors/125636/3 "2025-02-06T22:24:28Z")

</div>

Thats true, but in future I might have to increase the box size and the number of particles. So I started looking into sparse matrices.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 6, 2025, 10:30pm UTC](https://discourse.julialang.org/t/sparse-matrix-of-vectors/125636/4 "2025-02-06T22:30:51Z")

</div>

> [@pulk\_jain](#):
>
> Edit: I realized I could just do `Base.zero(::Type{Vector{Int64}}) = 0` and that does the job.

btw this is a bad idea because this is type-piracy and this particular definition doesn’t make a lot of sense. The “zero” element of a matrix should be additive identity.

In any case, you want to initialize the sparse matrix to have element type of a statically-sized vector, check out StaticArrays.jl maybe, and then, you can probably just use `@SVector(0,0,0,0,0,0,0,0)` as your zero element

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [February 6, 2025, 11:09pm UTC](https://discourse.julialang.org/t/sparse-matrix-of-vectors/125636/5 "2025-02-06T23:09:10Z")

</div>

The `SVector{8,Int}` suggestion sounds like it would fit your needs.

Another option is to use a `Dict{Tuple{Int,Int}, Vector{Int}}` rather than `SparseMatrixCSC`. If particles i and j interact then set `dict[minmax(i,j)] = thevector` (the `minmax` is there to sort the values so that `(i,j)` and `(j,i)` refer to the same thing).

You can use `get(dict, minmax(i,j), VALUE_IF_MISSING)` to return the vector (if it’s there) or else whatever fallback value you want.

---

<div class="post-metadata">

**Author:** ![pulk\_jain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pulk_jain/32/204900_2.png) [@pulk\_jain](https://discourse.julialang.org/u/pulk_jain)\
**Post date:** [February 6, 2025, 11:59pm UTC](https://discourse.julialang.org/t/sparse-matrix-of-vectors/125636/6 "2025-02-06T23:59:56Z")

</div>

I feel using a dict would be better for my case, so that I can dynamically add and delete elements easily depending on when a contact forms or breaks.  
Thanks!
