jonniedie / ComponentArrays.jl

Arrays with arbitrarily nested named components.
MIT License
290 stars 35 forks source link

How to get index range of subarray? #240

Open vpuri3 opened 8 months ago

vpuri3 commented 8 months ago

In the case below, how do I get the range of indices spanned by x.a?

x = (; a = ones(2), b = ones(2)) |> ComponentArray

idx_a = ?? # want 1:2
vpuri3 commented 8 months ago

@jonniedie is that possible? TIA

jonniedie commented 8 months ago

It's a little clunky, but you can do

only(getaxes(x))[:a].idx
vpuri3 commented 7 months ago

Thanks, that works. Is there a similar way to get the global indices of nested arrays? eg.

ca = (; a = ones(2), b = (; x = 2 * ones(2), y = 3 * ones(2))) |> ComponentArray
idx_b_x = getindices(ca.b.x) # want 3:4