# Static Vectors with Named Fields?

**URL:** <https://discourse.julialang.org/t/static-vectors-with-named-fields/74005>\
**Category:** General Usage\
**Tags:** linearalgebra, tuple, namedtuple, staticarrays\
**Created:** [January 3, 2022, 10:24pm UTC](https://discourse.julialang.org/t/static-vectors-with-named-fields/74005 "2022-01-03T22:24:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [January 3, 2022, 10:24pm UTC](https://discourse.julialang.org/t/static-vectors-with-named-fields/74005/1 "2022-01-03T22:24:01Z")

</div>

Say for example that I would like to represent a three dimensional point and I would like it to be easy to look up the values with the names `x`, `y`, `z`.

I could use a named tuple like,

```julia
t = (x=0.0, y=0.0, z=0.0)

```

This all works great util I would like to do some linear algebra. For example,

```julia
t = (x=1.0, y=2.0, z=3.0)
m = diagm(ones(3))
m*t

```

Any easy work around would be something like,

```julia
m*[t...]

```

but I presume this would reallocate new memory for the temporary vector `[t...]`, when it is not really required? Is there a better way? Maybe a static array with some named fields provides an alternate option?

Looking for suggestions on the best way to approach something like this. I generally prefer solutions that only require Julia standard libraries.

---

<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:** [January 3, 2022, 10:25pm UTC](https://discourse.julialang.org/t/static-vectors-with-named-fields/74005/2 "2022-01-03T22:25:56Z")

</div>

See [`FieldVector` in the StaticArrays.jl docs](https://juliaarrays.github.io/StaticArrays.jl/stable/pages/api/#FieldVector-1), which seems to be exactly what you want.

```julia
julia> using StaticArrays, LinearAlgebra

julia> struct Point3D{T<:Number} <: FieldVector{3, T}
           x::T
           y::T
           z::T
       end

julia> t = Point3D(1.0,2.0,3.0)
3-element Point3D{Float64} with indices SOneTo(3):
 1.0
 2.0
 3.0

julia> M = diagm(@SVector [4,5,6])
3×3 SMatrix{3, 3, Int64, 9} with indices SOneTo(3)×SOneTo(3):
 4 0 0
 0 5 0
 0 0 6

julia> M * t
3-element Point3D{Float64} with indices SOneTo(3):
  4.0
 10.0
 18.0

```

---

<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:** [January 3, 2022, 10:49pm UTC](https://discourse.julialang.org/t/static-vectors-with-named-fields/74005/3 "2022-01-03T22:49:33Z")

</div>

StaticArrays.jl added access via `.x` etc recently, [this PR](https://github.com/JuliaArrays/StaticArrays.jl/pull/980), so:

```julia
julia> t = (x=0.1, y=2.0, z=3.3);

julia> using StaticArrays

julia> v = SA[t...]
3-element SVector{3, Float64} with indices SOneTo(3):
 0.1
 2.0
 3.3

julia> v.x
0.1

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 3, 2022, 11:31pm UTC](https://discourse.julialang.org/t/static-vectors-with-named-fields/74005/4 "2022-01-03T23:31:05Z")

</div>

[https://github.com/SciML/LabelledArrays.jl](https://github.com/SciML/LabelledArrays.jl)

FieldVector has the unfortunate drawback that because it requires a strict you can only define them in global scope. StaticArrays has built in .x .y and .z but it’s not customizable. LabelledArrays is a StaticArray with zero overhead static naming that just converts the names to indices at compile time, and the index list is type parameter information so they can be defined anywhere.

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [January 4, 2022, 1:04am UTC](https://discourse.julialang.org/t/static-vectors-with-named-fields/74005/5 "2022-01-04T01:04:04Z")

</div>

Thanks all for the suggestions. I will try these out and see how they go!
