JuliaArrays / ArraysOfArrays.jl

Efficient storage and handling of nested arrays in Julia
Other
43 stars 9 forks source link

VectorOfVectors flatview to nestedview #28

Closed fintzij closed 2 years ago

fintzij commented 2 years ago

Hello,

Is there a way to easily reconsitute a VectorOfVectors once it has been flattened?

using ArraysOfArrays

# original VectorOfVectors 
VV = VectorOfVectors{Float64}()
push!(VV, rand(3))
push!(VV, rand2())

# need to flatten for AD 
VV_flat = flatview(VV)

# wish I could do this
VV_nested = nestedview(VV_flat)
VV_nested == VV # please be true

In my use case, it is convenient to use a VectorOfVectors since it simplifies indexing groups of parameters that relate to different aspects of a model. Sadly, ForwardDiff doesn't play nice with this structure as it wants a single vector, hence the initial flattening. I would think this is not so problematic, except that reconsituting the nested view within the function to be optimized seems complicated once dual numbers are involved.

Thanks

oschulz commented 2 years ago

Hi @fintzij,

I'm working on a more extensive generic interface for nested arrays that will also support the upcoming Base.Slices, that'll likely end up in ArrayInterface and will come with a general way or "inverting" a flatten operation.

For now, you can do this as a workaround:

VV_nested = VectorOfVectors(VV_flat, VV.elem_ptr)
VV_nested == VV # true

A nicer API to handle elem_ptr is also on my to-do list.

fintzij commented 2 years ago

Thanks for the quick reply @oschulz! I'll implement your suggestion and keep an eye out for updates to the interface.