# ComponentArray of ArrayPartition

**URL:** <https://discourse.julialang.org/t/componentarray-of-arraypartition/106048>\
**Category:** Modelling & Simulations\
**Created:** [November 10, 2023, 7:07pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048 "2023-11-10T19:07:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sand](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@sand](https://discourse.julialang.org/u/sand)\
**Post date:** [November 10, 2023, 7:07pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/1 "2023-11-10T19:07:44Z")

</div>

Hello,

I am trying to use a `ArrayPartition` inside a `ComponentArray`.

Here is a MRE:

```julia
using ComponentArrays

using RecursiveArrayTools

var1 = ArrayPartition(randn(10), randn(10));

var2 = ComponentArray(a=randn(10), b=var1);

```

I like `ArrayPartition` because of the indexing: `var1.x[1]`, `var1.x[2]`

However, I don’t get this ability when the `ArrayPartition` inside of a `ComponentArray`.

`var2.b` will show the whole array (no longer partitioned).

Is there a way to make it sustain the functionality inside a `ComponentArray`?  
Is it possible to index a `ComponentArray` in the same way as a `ArrayPartition` (like: `var2.x[1]`, `var2.x[2]`)?

Alternatively, can I create a `ComponentArray` from list comprehension?

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [November 10, 2023, 7:21pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/2 "2023-11-10T19:21:28Z")

</div>

My expectation is that you should be able to access the partition with

`var2.b.x`

I wonder whether that is a bug.

**Update**

I just realized the accessor `var2.a` returns a view rather than a copy (don’t remember if that has always been the behavior). So maybe the behavior is expected because the view is of the data rather than the indices.

---

<div class="post-metadata">

**Author:** ![sand](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@sand](https://discourse.julialang.org/u/sand)\
**Post date:** [November 10, 2023, 7:25pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/3 "2023-11-10T19:25:46Z")

</div>

`var2.b.x` yields `ERROR: type SubArray has no field x`

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [November 10, 2023, 7:29pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/4 "2023-11-10T19:29:29Z")

</div>

Yeah. I agree. Initially, I thought adding var1 would mean having another layer of nesting and so I expected `var2.b.x` to work just as `var1.x`. But I think the issue is that it returns a view of the data rather than a copy, which means the indices of `var1` are not available. At least that is what I think.

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [November 10, 2023, 7:34pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/5 "2023-11-10T19:34:08Z")

</div>

The following code appears to do what you want:

```julia
using ComponentArrays
using RecursiveArrayTools

var1 = ArrayPartition(randn(10), randn(10))
var2 = ComponentArray(a=randn(10), b = [xi for xi ∈ var1.x] )

```

You can then access `var1.x[1]` as `var2.b[1]` and `var1.x[2]` as `var2.b[2]`.

---

<div class="post-metadata">

**Author:** ![sand](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@sand](https://discourse.julialang.org/u/sand)\
**Post date:** [November 10, 2023, 7:38pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/6 "2023-11-10T19:38:54Z")

</div>

Will the line ` b = [xi for xi ∈ var1.x]` create a copy of each vector in `var.x`?

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [November 10, 2023, 7:42pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/7 "2023-11-10T19:42:06Z")

</div>

Yes. You can see that `var1.x[1] === var2.b[1]` returns false. Do you not want a copy?

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [November 10, 2023, 7:45pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/8 "2023-11-10T19:45:45Z")

</div>

One of the main purposes of ComponentArrays is to have a single flat array as the storage for the data. Thus, I don’t think it’s possible to construct a ComponentArray that does not copy data into this flat structure.

---

<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:** [November 10, 2023, 9:04pm UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/9 "2023-11-10T21:04:59Z")

</div>

This. I’m not sure it makes sense. ArrayPartition of ComponentArrays, maybe, but the point of ComponentArrays is that it has a flat vector so this direction that you want will have lots of oddities.

---

<div class="post-metadata">

**Author:** ![sand](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@sand](https://discourse.julialang.org/u/sand)\
**Post date:** [November 11, 2023, 9:56am UTC](https://discourse.julialang.org/t/componentarray-of-arraypartition/106048/10 "2023-11-11T09:56:56Z")

</div>

The idea is to have `ComponentArrays` with `ArrayPartition`. Not the other way around. For my purposes I think I can use both `ArrayPartition` and `ComponentArrays` interchangeably. However, It would be nice If `ComponentArrays` could have a similar interface to `ArrayPartitions`. If no name is provided, use a uniform syntax like `.x[1]`, `.x[2]`.
