Skip to content

VectorOfVectors flatview to nestedview #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fintzij opened this issue Oct 13, 2022 · 2 comments
Closed

VectorOfVectors flatview to nestedview #28

fintzij opened this issue Oct 13, 2022 · 2 comments

Comments

@fintzij
Copy link

fintzij commented Oct 13, 2022

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
Copy link
Collaborator

oschulz commented Oct 14, 2022

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
Copy link
Author

fintzij commented Oct 14, 2022

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

@fintzij fintzij closed this as completed Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants