# Compact way to store Diagonal static matrices?

**URL:** https://discourse.julialang.org/t/compact-way-to-store-diagonal-static-matrices/59715
**Category:** General Usage
**Tags:** question, staticarrays
**Created:** [April 21, 2021, 7:18am UTC](https://discourse.julialang.org/t/compact-way-to-store-diagonal-static-matrices/59715 "2021-04-21T07:18:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [April 21, 2021, 7:18am UTC](https://discourse.julialang.org/t/compact-way-to-store-diagonal-static-matrices/59715/1 "2021-04-21T07:18:17Z")

</div>

I want to store a large number of Diagonal 3x3 matrices. I am considering using a `HybridArray` whose individual elements are these 3x3 matrices. In my case I know that the matrices are `Diagonal`. I’m not sure which of these two are the best choices:

1. `Diagonal(::SVector{3})`: preserves structure, but not static. It might be possible to wrap the size around this to obtain that information in at compile time.
2. `SMatrix{3,3}`: Static, but stores unnecessary zeros and does not preserve structure.

Assuming that memory isn’t the main limitation, is it better to use the `SMatrix` approach over the `Diagonal` one? Of course the specific choice will depend on the limiting factor in the problem, but I just wanted to know if there is any obvious pitfall with either of these approaches when it comes to performance.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [April 21, 2021, 7:23am UTC](https://discourse.julialang.org/t/compact-way-to-store-diagonal-static-matrices/59715/2 "2021-04-21T07:23:00Z")

</div>

> [@jishnub](#):
>
> `Diagonal(::SVector{3})` : preserves structure, but not static. It might be possible to wrap the size around this to obtain that information in at compile time.

`Diagonal(::SVector{3})` should still be completely static. Thanks to constant prop, the size information will even be known at compile time in a lot of cases, i.e.:

```julia

julia> using StaticArrays, LinearAlgebra

julia> m = Diagonal(SA[1., 2., 3.])
3×3 Diagonal{Float64, SVector{3, Float64}}:
 1.0 ⋅ ⋅ 
  ⋅ 2.0 ⋅ 
  ⋅ ⋅ 3.0

julia> @code_typed size(m)
CodeInfo(
1 ─ return (3, 3)
) => Tuple{Int64, Int64}

```

---

<div class="post-metadata">

### Author: ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)
#### Post date: [April 21, 2021, 8:00am UTC](https://discourse.julialang.org/t/compact-way-to-store-diagonal-static-matrices/59715/3 "2021-04-21T08:00:34Z")

</div>

If you need to store a lot of diagonal 3x3 matrices then an array of diagonal static matrices looks like a good choice, something like `[Diagonal(SA[i, 1.0, 2.0]) for i in 1:10]`. Size information should be correctly propagated, and if it’s not for some operation, please report it 🙂 . By the way, `HybridArray` doesn’t currently support representing this kind of structure.
