# Idiomatic way to specialise method over array shape/dims

**URL:** <https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131>\
**Category:** General Usage\
**Tags:** array\
**Created:** [August 2, 2020, 1:23pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131 "2020-08-02T13:23:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Plop](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@Plop](https://discourse.julialang.org/u/Plop)\
**Post date:** [August 2, 2020, 1:23pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131/1 "2020-08-02T13:23:34Z")

</div>

It seems that array dimensions is not included in the type definition (which is really reasonable). I was wondering if there was a mechanism in Julia to force method specialisation based on array dimensions.

Typically, the code would be fed with matrices of dimension (n, M) where n can be any number but M is a fixed integer my code is constantly looping over.

I could create my own type (or something similar) but:

- Julia is not supporting inheritance and implementing proxy methods myself would be ludicrous (not a fan of introducing boilerplate and checking the Array interface hasn’t changed in each release).
- I really don’t want to hardcode things. It would be a bit of a shame to manually create `Matrix1D`, `Matrix2D`, `Matrix3D`, etc. when the JIT can do it itself, especially if it involves manually creating methods with the only difference being the upper range in a single for loop.

The motivation for this is performance: my code involves a lot of looping over 1:M and knowing M from the start allows LLVM to perform more aggressive optimisations.

---

<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:** [August 2, 2020, 1:58pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131/2 "2020-08-02T13:58:03Z")

</div>

StaticArrays include the size in their type, so maybe use those? Not that you should not use them for arrays with more than ~ 100 elements. If this does not cut it for you, you could create a wrapper which contains `M` as type parameter and which is `<:AbstractArray`.

---

<div class="post-metadata">

**Author:** ![Plop](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@Plop](https://discourse.julialang.org/u/Plop)\
**Post date:** [August 2, 2020, 2:06pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131/3 "2020-08-02T14:06:14Z")

</div>

My usage is typically involving huge heap allocated arrays, so StaticArray looks like a bad idea in this case. That being said, is there a way to use a literal integer as a type parameter?

The doc seems to imply it there:

[https://docs.julialang.org/en/v1/manual/types/#Parametric-Composite-Types-1](https://docs.julialang.org/en/v1/manual/types/#Parametric-Composite-Types-1)

but without examples.

---

<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:** [August 2, 2020, 2:14pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131/4 "2020-08-02T14:14:55Z")

</div>

You might like [https://github.com/mateuszbaran/HybridArrays.jl](https://github.com/mateuszbaran/HybridArrays.jl), which treats some dimensions like StaticArrays, while leaving others like ordinary Arrays.

---

<div class="post-metadata">

**Author:** ![Plop](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@Plop](https://discourse.julialang.org/u/Plop)\
**Post date:** [August 2, 2020, 2:41pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131/5 "2020-08-02T14:41:42Z")

</div>

Quite like that. Thanks for the pointer.

---

<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:** [August 2, 2020, 2:51pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131/6 "2020-08-02T14:51:55Z")

</div>

> [@Plop](#):
>
> That being said, is there a way to use a literal integer as a type parameter?

Yes, type parameters can be any isbits value (plus also symbols). Your wrapper would look something like:

```julia
struct MyArr{M, T, N} <: AbstractArray{T,N}
  ar::Array{T,N}
end
MyArr(ar::Array{T,N}) where {T,N} = MyArr{size(ar,1), T, N}(ar)

```

Note, you will need to define the necessary interface functions for AbstractArrays, see the docs.

But the mentioned HybridArrays sound like well suited.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 2, 2020, 3:25pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131/7 "2020-08-02T15:25:35Z")

</div>

> [@Plop](#):
>
> The motivation for this is performance: my code involves a lot of looping over 1:M and knowing M from the start allows LLVM to perform more aggressive optimisation.

Wouldn’t it be better for performance to have the _columns_ have a fixed size, instead of the rows? Then the innermost loop would be super fast. A version of this is quite common: to represent a matrix as a vector of StaticArrays.SVector.

---

<div class="post-metadata">

**Author:** ![Plop](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@Plop](https://discourse.julialang.org/u/Plop)\
**Post date:** [August 2, 2020, 3:30pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131/8 "2020-08-02T15:30:02Z")

</div>

I’m very new to julia. About row vs. columns, yes: I’m still thinking/defaulting to C-like convention but that was definitely on my checklist.

The vector of static vector sounds like a very good idea! Thanks!

---

<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:** [August 3, 2020, 2:58pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specialise-method-over-array-shape-dims/44131/9 "2020-08-03T14:58:54Z")

</div>

`StaticArrays.Size` can be used as a trait for dispatch, see also the `SizedArray` decorator.

[https://juliaarrays.github.io/StaticArrays.jl/latest/pages/api/#The-Size-trait-1](https://juliaarrays.github.io/StaticArrays.jl/latest/pages/api/#The-Size-trait-1)
