# SparseMatrix or SArray, MArray?

**URL:** <https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [May 18, 2021, 10:26pm UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401 "2021-05-18T22:26:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [May 18, 2021, 10:26pm UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401/1 "2021-05-18T22:26:16Z")

</div>

I’m just getting used to Julia and therefore have very basic questions:

1. SparseMatrix  
I have a little sparse matrix (only 11 Rows). Since the number of entries for storage as SparseMatrixCSC must be known: Will such a small matrix be stored on the stack or do I have to weigh up whether the SparseMatrix or SArray will bring me greater benefit?

2. MArray  
There is also MArray in the package StaticArrays. When does one use MArray?

I’m looking forward to your answers 😉

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 18, 2021, 10:41pm UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401/2 "2021-05-18T22:41:51Z")

</div>

From those options I would bet on the static array. Or, maybe, define a custom immutable struct with two static vectors for the values and indices.

Concerning the MArray, I cannot say much. Whenever I thought they would be useful the actual immutable arrays resulted to be faster.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 19, 2021, 1:54am UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401/3 "2021-05-19T01:54:06Z")

</div>

Well, MArrays are useful when you want to mutate vectors in place, e.g.:

```julia
# calculate the drag of one tether segment
function calc_drag(s, v_segment, unit_vector, rho, last_tether_drag, v_app_perp, area)
    s.v_apparent .= s.v_wind_tether - v_segment
    v_app_norm = norm(s.v_apparent)
    v_app_perp .= s.v_apparent .- dot(s.v_apparent, unit_vector) .* unit_vector
    last_tether_drag .= -0.5 * set.cd_tether * rho * norm(v_app_perp) * area .* v_app_perp
    v_app_norm
end 

```

You cannot easily do that with SArrays.

And MArray and SArray are good for up to about 100 elements. I would not use a sparse matrix if I have less than 100 elements.

---

<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:** [May 19, 2021, 2:15am UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401/4 "2021-05-19T02:15:38Z")

</div>

specifically, you shouldn’t use `SparseMatrixCSC` with less than around 10000 elements. If your matrix is small enough to fit in L2 cache, it’s probably not worth the 10x overhead to index. Special types of `Sparse` matrices (like `Diagonal` `Banded` etc) on the other hand are worth it at much smaller sizes.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 19, 2021, 11:33am UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401/5 "2021-05-19T11:33:54Z")

</div>

For convenience, yes, but this is what I mean:

```julia
using StaticArrays, BenchmarkTools, Setfield

function mut!(A)
  A .= A .+ 1
end
Base.setindex!(m::SMatrix,value,i,j) = @set! m[i,j] = value

A1 = rand(3,3)

A2 = rand(MMatrix{3,3,Float64})

A3 = rand(SMatrix{3,3,Float64})

@btime mut!($A1)

@btime mut!($A2)

@btime mut!($A3)

```

Results:

```julia
  14.548 ns (0 allocations: 0 bytes)
  4.733 ns (0 allocations: 0 bytes)
  1.791 ns (0 allocations: 0 bytes)

```

This actually makes me wonder how and why `MMatrix` are actually implemented as they are, and if that could not be improved.

(it has not escaped to me that the function above does not _mutate_ the matrix if it is a static matrix, and one may have to appeal to `MMatrix` if passing it to another code over which one does not have access to define `A = mut!(A)`) .

---

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [May 19, 2021, 3:13pm UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401/6 "2021-05-19T15:13:08Z")

</div>

Thanks for your help.  
I’m afraid I’ve left out an important piece of information:  
I have an 11 \* 11 matrix with entries only on 3 diagonals. It’s a SparseMatrixCSC but I created it with spdiagm, that’s what I should have said about it. The overhead should therefore be significantly lower.  
It’s kind of a diffusion matrix that never gets changed and I was wondering if I couldn’t take advantage of this fact by making it somehow immutable.

---

<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:** [May 19, 2021, 3:27pm UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401/7 "2021-05-19T15:27:29Z")

</div>

> [@hardy](#):
>
> I’m afraid I’ve left out an important piece of information:  
> I have an 11 \* 11 matrix with entries only on 3 diagonals. It’s a SparseMatrixCSC but I created it with spdiagm, that’s what I should have said about it. The overhead should therefore be significantly lower.  
> It’s kind of a diffusion matrix that never gets changed and I was wondering if I couldn’t take advantage of this fact by making it somehow immutable.

You have a tiny matrix with a low sparsity (30% nonzero). A dense matrix will be faster than a sparse CSC matrix here.

---

<div class="post-metadata">

**Author:** ![hardy](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hardy](https://discourse.julialang.org/u/hardy)\
**Post date:** [May 19, 2021, 3:42pm UTC](https://discourse.julialang.org/t/sparsematrix-or-sarray-marray/61401/8 "2021-05-19T15:42:41Z")

</div>

Okay, then a SArray is probably the right choice.  
Thank you for your prompt reply
