# How to unpack a vector to a struct / local variables

**URL:** <https://discourse.julialang.org/t/how-to-unpack-a-vector-to-a-struct-local-variables/65241>\
**Category:** New to Julia\
**Created:** [July 24, 2021, 5:38pm UTC](https://discourse.julialang.org/t/how-to-unpack-a-vector-to-a-struct-local-variables/65241 "2021-07-24T17:38:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lamont](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamont/32/27486_2.png) [@lamont](https://discourse.julialang.org/u/lamont)\
**Post date:** [July 24, 2021, 5:38pm UTC](https://discourse.julialang.org/t/how-to-unpack-a-vector-to-a-struct-local-variables/65241/1 "2021-07-24T17:38:24Z")

</div>

Is there a better way to write a function like this (which is called from an integrator so needs to have dx and x being Vector{Float64}'s and unpack them without excessive copying and mostly without all the magic numerical indexes that need to be maintained?

```julia
function eom!(dx,x,p,t,stage,scaling)
  r = x[1:3]
  v = x[4:6]
  pv = x[7:9]
  pr = x[10:12]
  m = x[13]

  rm = norm(r)
  r3 = rm^3
  r5 = rm^5

  u = normalize(pv)
  At = stage.thrust / ( m * scaling.g_bar )

  dx[1:3] = v
  dx[4:6] = - r/r3 + At * u
  dx[7:9] = - pr
  dx[10:12] = pv / r3 - 3 / r5 * r' * pv * r
  dx[13] = - stage.thrust / stage.c

  dx
end

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 24, 2021, 6:13pm UTC](https://discourse.julialang.org/t/how-to-unpack-a-vector-to-a-struct-local-variables/65241/2 "2021-07-24T18:13:37Z")

</div>

Clarification: `dx` and `x` needs to be `Vector{Float64}` or `AbstractVector{Float64}`? In second case there is a lot of thing that you can do. If it is `Vector` then it seems that the only thing you can do, is to reduce allocations with the help of `@.`.

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [July 24, 2021, 6:51pm UTC](https://discourse.julialang.org/t/how-to-unpack-a-vector-to-a-struct-local-variables/65241/3 "2021-07-24T18:51:17Z")

</div>

Welcome to the forum! I think this is pretty much the prototypical use-case for: [https://github.com/jonniedie/ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl)

---

<div class="post-metadata">

**Author:** ![lamont](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamont/32/27486_2.png) [@lamont](https://discourse.julialang.org/u/lamont)\
**Post date:** [July 24, 2021, 7:08pm UTC](https://discourse.julialang.org/t/how-to-unpack-a-vector-to-a-struct-local-variables/65241/4 "2021-07-24T19:08:45Z")

</div>

Ultimately this is being called by DifferentialEquations.jl which will be wrapped by MINPACK.jl which uses `Vector{Float64}` but I don’t mind answering the question about `AbstractVector{Float64}` (and that might drive me to use something other than MINPACK.jl?)

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 24, 2021, 7:12pm UTC](https://discourse.julialang.org/t/how-to-unpack-a-vector-to-a-struct-local-variables/65241/5 "2021-07-24T19:12:10Z")

</div>

I think, that marius311 already gave an answer for `AbstractVector`. Since DifferentialEquations.jl support abstract types, I think it makes sense to raise an issue in MINPACK.jl, to make type restrictions less tight (or you can fork this package and do it yourself).

---

<div class="post-metadata">

**Author:** ![lamont](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamont/32/27486_2.png) [@lamont](https://discourse.julialang.org/u/lamont)\
**Post date:** [July 24, 2021, 8:05pm UTC](https://discourse.julialang.org/t/how-to-unpack-a-vector-to-a-struct-local-variables/65241/6 "2021-07-24T20:05:27Z")

</div>

Issue submitted to MINPACK,jl
