# Struct Parameterized by One Dimension of StaticArray Matrix Members

**URL:** <https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115>\
**Category:** Performance\
**Tags:** question\
**Created:** [November 13, 2020, 10:11pm UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115 "2020-11-13T22:11:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [November 13, 2020, 10:11pm UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/1 "2020-11-13T22:11:10Z")

</div>

I would like to preallocate some static arrays for use with DifferentialEquations.jl. My problem uses a lot of matrices that are 3 x N, where N is not known ahead of time. I would like to use StaticArrays.jl and do something like:

```julia
struct Memory{N}
	f::MArray{Tuple{3,N},Float64,2,3N}
	g::MArray{Tuple{3,N},Float64,2,3N}
	...
end

```

Unfortunately, 3N, the number of entries, does not work as N is not seen as an integer but a type. Is there a good way to do this?

---

<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:** [November 13, 2020, 10:41pm UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/2 "2020-11-13T22:41:31Z")

</div>

I have done something similar once, and I opted for a vector os static arrays, something like:

```julia
struct A{T<:SMatrix...}
  x_solute :: Vector{T}
end

```

Depending on the way the operations on these vectors are performed the individual static elements do not need to be mutable at all, they can be replaced without cost.

But anyway you could just use

> [@tawheeler](#):
>
> ```julia
> struct Memory{T}
> f::MArray{T}
> 
> ```

That will work well, I think.

---

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [November 13, 2020, 11:16pm UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/3 "2020-11-13T23:16:53Z")

</div>

Ah, if I understand correctly, we drop the need to define 3 ahead of time, but only ever initialize with matrices of size 3 by N.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 14, 2020, 8:00am UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/4 "2020-11-14T08:00:06Z")

</div>

Introduce an extra type parameter for `3*N`, and have an inner constructor calculate or verify it. Sketch for the first option:

```julia
struct Memory{N,M}
    f::MArray{Tuple{3,N},Float64,2,M}
    g::MArray{Tuple{3,N},Float64,2,M}
    function Memory{N}(...) where N
        M = 3 * N
        ...
    end
end

```

---

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [November 14, 2020, 6:27pm UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/5 "2020-11-14T18:27:45Z")

</div>

Thanks! I like this one a lot. It make sit easy to have other sizes too, like Vector{N}, etc. in the struct.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [November 14, 2020, 7:37pm UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/6 "2020-11-14T19:37:19Z")

</div>

You might also be interested in [https://github.com/mateuszbaran/HybridArrays.jl](https://github.com/mateuszbaran/HybridArrays.jl), which are static along some dimensions.

---

<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:** [November 14, 2020, 10:36pm UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/7 "2020-11-14T22:36:44Z")

</div>

Can this have any performance benefit relative to simply parameterizing the complete type?

And what are the possible benefits, or not, of defining a large static arrays versus defining a large array of small static elements?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 15, 2020, 7:08am UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/8 "2020-11-15T07:08:31Z")

</div>

> [@lmiq](#):
>
> Can this have any performance benefit relative to simply parameterizing the complete type?

I don’t understand what the latter means.

> [@lmiq](#):
>
> defining a large static arrays

That has no performance benefits and just exercises the compiler. See the StaticArrays.jl docs.

---

<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:** [November 15, 2020, 9:37am UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/9 "2020-11-15T09:37:41Z")

</div>

> [@Tamas\_Papp](#):
>
> I don’t understand what the latter means

Oh, sorry. In general I meant to ask if there is any reason for a code run slower if dealing with a struct defined as

```julia
struct A{T}
  x :: T
end

```

relative to

```julia
struct A
  x :: SArray{3,Float64}
end

```

If the instances of the first are initialized with the same type as that of the second.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 15, 2020, 9:38am UTC](https://discourse.julialang.org/t/struct-parameterized-by-one-dimension-of-staticarray-matrix-members/50115/10 "2020-11-15T09:38:51Z")

</div>

No.
