# NamedArrays vs NamedTuples vs Dict in objective function to optimization

**URL:** <https://discourse.julialang.org/t/namedarrays-vs-namedtuples-vs-dict-in-objective-function-to-optimization/42659>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [July 7, 2020, 6:36am UTC](https://discourse.julialang.org/t/namedarrays-vs-namedtuples-vs-dict-in-objective-function-to-optimization/42659 "2020-07-07T06:36:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [July 7, 2020, 6:36am UTC](https://discourse.julialang.org/t/namedarrays-vs-namedtuples-vs-dict-in-objective-function-to-optimization/42659/1 "2020-07-07T06:36:51Z")

</div>

I am optimizing a function that takes a vector of size ~300 as input. That objective function calls several other functions that each use a subset of the input vector. An example of that setup is below:

```julia
function mainprog(θ)
    α = θ[1:20]
    β = θ[21:35]
    ...
    fun1(α)
    fun2(β)
    ...
    return X
end

```

I do not want to have the `α` and `β` hardcoded like in the example above because of elegance, maintenance, and sanity reasons. I much rather prefer to have them as `NamedArrays`, `NamedTuples`, or `Dict`. What’s a good way of defining those subvectors `α` and `β`?

I’m worried about performance, and the only constraint that I have is that I’d like to be able to use `Optim` to optimize over `θ` without too much trouble. Does anyone have any suggestions on how to go about that?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [July 7, 2020, 7:32am UTC](https://discourse.julialang.org/t/namedarrays-vs-namedtuples-vs-dict-in-objective-function-to-optimization/42659/2 "2020-07-07T07:32:03Z")

</div>

I think there a package for exactly this.  
But the name is escaping me.

Its not NamedArrays or LabelledArrays or RecursiveArrayTools (though the lasst two are closer)

---

<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:** [July 7, 2020, 7:42am UTC](https://discourse.julialang.org/t/namedarrays-vs-namedtuples-vs-dict-in-objective-function-to-optimization/42659/3 "2020-07-07T07:42:04Z")

</div>

Could it be AxisArrays?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [July 7, 2020, 7:45am UTC](https://discourse.julialang.org/t/namedarrays-vs-namedtuples-vs-dict-in-objective-function-to-optimization/42659/4 "2020-07-07T07:45:59Z")

</div>

no what we are talking about is having a slice of an array labelled,  
not an axis (AxisArrays/NamedDims).

---

<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:** [July 7, 2020, 7:50am UTC](https://discourse.julialang.org/t/namedarrays-vs-namedtuples-vs-dict-in-objective-function-to-optimization/42659/5 "2020-07-07T07:50:34Z")

</div>

You might be looking for [https://github.com/jonniedie/ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl), announced [here](https://discourse.julialang.org/t/rfc-ann-componentarrays-jl-for-building-composable-models-without-a-modeling-language/38001).

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [July 7, 2020, 9:02am UTC](https://discourse.julialang.org/t/namedarrays-vs-namedtuples-vs-dict-in-objective-function-to-optimization/42659/6 "2020-07-07T09:02:59Z")

</div>

Yes, this might be it.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [July 7, 2020, 9:39am UTC](https://discourse.julialang.org/t/namedarrays-vs-namedtuples-vs-dict-in-objective-function-to-optimization/42659/7 "2020-07-07T09:39:25Z")

</div>

> [@mcabbott](#):
>
> You might be looking for [https://github.com/jonniedie/ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl), announced [here](https://discourse.julialang.org/t/rfc-ann-componentarrays-jl-for-building-composable-models-without-a-modeling-language/38001).

That’s the one, i was thinking of.  
(I’ve not use it, but sounds like the tool for the job)
