# ArraysOfArrays with named fields

**URL:** https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281
**Category:** General Usage
**Tags:** componentarrays, arraysofarrays
**Created:** [February 16, 2024, 8:49am UTC](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281 "2024-02-16T08:49:12Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [February 16, 2024, 8:49am UTC](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281/1 "2024-02-16T08:49:12Z")

</div>

Let’s say I have a vector `a = [x1, x2, x3, q1, q2, q3, q4]`. What’s a nice way to use it in algorithms that expect a flat vector like that, but at the same time have a nice interface like `a.x`, `a.q` to extract `[x1, x2, x3]` and `[q1, q2, q3, q4]`?

I thought of

```julia
a = NamedArray(VectorOfVectors([[1,2,3], [1,2,3,4]]), [:x, :q])

```

but that requires writing `a[:x]` instead of `a.x`, and `flatview(a.array)[i]` instead of `a[i]`.

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 16, 2024, 8:52am UTC](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281/2 "2024-02-16T08:52:48Z")

</div>

There are packages that convert back and forth between struct and vector.

ParameterHandling.jl, TransformVariables.jl, Bijectors.jl, Flatten.jl, etc

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [February 19, 2024, 1:17pm UTC](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281/3 "2024-02-19T13:17:18Z")

</div>

Thanks. It seems there’s room for another small package to provide named views to sections of an existing vector…

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [February 19, 2024, 2:13pm UTC](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281/4 "2024-02-19T14:13:59Z")

</div>

This is sort of what StructArrays.jl does?

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [February 19, 2024, 2:38pm UTC](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281/5 "2024-02-19T14:38:54Z")

</div>

StructArrays is for a quite different use case I think:

- each column with the same number of elements
- columns can be of different types
- data collected in StructArray columns
- convenient constructors for vector of named tuples and list of columns

I’m looking for an interface to different sections of a vector:

- sections can have different lengths
- all values of the same type
- all data stored together as a single vector
- convenient constructor for named sections

For example, it could be a lightweight wrapper that works like this:

```julia
a = [x1, x2, x3, q1, q2, q3, q4]

a2 = Sections(a, x=1:3, q=4:7)

a2[1] # a[1] (getindex simply forwards to `a`)
a2.x # view(a, 1:3)
a2.q # view(a, 4:7)

```

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [February 19, 2024, 3:51pm UTC](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281/6 "2024-02-19T15:51:51Z")

</div>

> [@ArraysOfArrays with named fields](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281/3):
>
> Thanks. It seems there’s room for another small package to provide named views to sections of an existing vector…

This is what ComponentArrays was designed for.

You can do

```julia
using ComponentArrays
a = ComponentArray(x = [1, 2, 3], q = [1, 2, 3, 4])

# Access position component
a.x

# Access quaternion component
a.q

```

See [this example](https://github.com/jonniedie/ComponentArrays.jl?tab=readme-ov-file#differential-equation-example) for how you can use ComponentArrays for simulating hierarchical models.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 19, 2024, 3:59pm UTC](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281/7 "2024-02-19T15:59:37Z")

</div>

Are you looking for [GitHub - JuliaHEP/AwkwardArray.jl: Awkward Array in Julia mirrors the Python library, enabling effortless zero-copy data exchange between Julia and Python](https://github.com/jpivarski/AwkwardArray.jl) ? it’s nowhere near completion yet but maybe you come from Python’s [GitHub - scikit-hep/awkward: Manipulate JSON-like data with NumPy-like idioms.](https://github.com/scikit-hep/awkward)

```python
In [8]: array = ak.Array([
   ...: [{"x": 1.1, "y": [1]}, {"x": 2.2, "y": [2, 2]}],
   ...: [],
   ...: [{"x": 3.3, "y": [3, 3, 3]}]
   ...: ])

In [9]: array.x
Out[9]: <Array [[1.1, 2.2], [], [3.3]] type='3 * var * float64'>

In [10]: array.y
Out[10]: <Array [[[1], [2, 2]], [], [[3, 3, 3]]] type='3 * var * var * int64'>

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [February 19, 2024, 4:38pm UTC](https://discourse.julialang.org/t/arraysofarrays-with-named-fields/110281/8 "2024-02-19T16:38:15Z")

</div>

Thanks, ComponentArrays is exactly what I had in mind. You can even do the following:

```julia
a = ComponentArray([1,2,3,1,2,3,4], Axis(x=1:3, q=4:7))

```

which is very close to the API I had imagined 🙂
