# Generate function to build a static array from vector of vector

**URL:** <https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862>\
**Category:** Performance\
**Tags:** metaprogramming\
**Created:** [April 23, 2021, 11:37am UTC](https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862 "2021-04-23T11:37:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [April 23, 2021, 11:37am UTC](https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862/1 "2021-04-23T11:37:36Z")

</div>

I have a Vector of Vectors (actually Vector of SVectors, but I do not believe this is relevant to the question.) All vectors, inner and outer, have length N. I wish to generate an SMatrix from it

- in a typestable manner
- without allocation  
('tis better t’was done quickly)

So I create a generated function, here’s the MWE

```julia
using StaticArrays, Printf
@generated function foo(a,::Val{N}) where{N} 
    els = :()
    for j = 1:N,i=1:N
        els = :($els...,a[$i][$j])
    end
    return quote
        SMatrix{N,N}($els) 
    end
end
@code_warntype foo([randn(2),randn(2)],Val(2))

```

which returns values as expected and is typestable, but performance is atrocious.

On the other hand if I hardcode

```julia
foo(a,::Val{2}) = @SMatrix{2,2}(a[1][1], ... ,a[2][2])

```

(in the real world, N=8, fun!) I get great performance. Hence  
a) the code I want to generate is the right code  
b) but that is not what I do generate.  
c) afterthought: or generation is triggered more often that I though - this would then not be reflected in the MWE.

I suspect I am generating at each call, a code containing the actual numerical values (The Julia manual does not guarantee that a generated function is compiled _only_ when called with new signature)

@macroexpand does not work here - I there a way I can visualize my generated code? And where did I go wrong in my metacode?

---

<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:** [April 23, 2021, 12:45pm UTC](https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862/2 "2021-04-23T12:45:25Z")

</div>

I would just use `SizedArray` to hint this:

```julia
foo(a, ::Val{N}) where N = reduce(hcat, SizedVector{N}(a))

```

---

<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:** [April 23, 2021, 1:04pm UTC](https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862/3 "2021-04-23T13:04:51Z")

</div>

> [@Philippe\_Maincon1](#):
>
> I have a Vector of Vectors (actually Vector of SVectors, but I do not believe this is relevant to the question.) All vectors, inner and outer, have length N. I wish to generate an SMatrix from it
> 
> - in a typestable manner
> - without allocation  
> ('tis better t’was done quickly)

Not possible, since the length of a `Vector` is not part of its type, while the size of an `SMatrix` _is_ part of its type. You need a different type of container to achieve this (such as the `SizedArray` mentioned by @Tamas_Papp above), but of course that is only possible of the length of your `Vector` is known statically.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [April 23, 2021, 1:08pm UTC](https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862/4 "2021-04-23T13:08:12Z")

</div>

> [@stevengj](#):
>
> that is only possible of the length of your `Vector` is known statically

I forgot to mention - the size is known staticaly - actualy my subvector are Static, and I know the outer Vector to have the same length.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [April 23, 2021, 1:09pm UTC](https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862/5 "2021-04-23T13:09:28Z")

</div>

`foo(a, ::Val{N}) where N = reduce(hcat, SizedVector{N}(a))`

Works like a charm, very fast, and _elegant_. Brilliant, thanks!

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [April 26, 2021, 9:17am UTC](https://discourse.julialang.org/t/generate-function-to-build-a-static-array-from-vector-of-vector/59862/6 "2021-04-26T09:17:10Z")

</div>

Just in case someone reads this in the future: As coded above, the border case N==1 fails: hcat is never called, and the function returns a SVector{1} instead of the wanted SMatrix{1,1}.

This is easily fixed:  
`foo(a, ::Val{N}) where N = SMatrix{N,N}(reduce(hcat, SizedVector{N}(a)))`
