Skip to content
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

Implementation of a merge function #186

Open
vboussange opened this issue Feb 5, 2023 · 4 comments
Open

Implementation of a merge function #186

vboussange opened this issue Feb 5, 2023 · 4 comments

Comments

@vboussange
Copy link

Hey there,
I thought it'd be very cool to have a specialisation of the merge function for ComponentArrays, with a similar behaviour as with Dictionarys or NamedTuples.

For instance, consider

N = 2
p = (r = ones(N), b = 3 * ones(N))
p2 = (r = 2 * ones(N))
merge(p, p2) #outputs (r = [2.0, 2.0], b = [3.0, 3.0])

It could be useful to have a similar functionality with p and p2 being ComponentArrays.
I am not sure whether this could be efficiently implemented though.

@jonniedie
Copy link
Owner

Yeah, that would be nice. You can also accomplish this by doing

julia> p = ComponentArray(r = ones(N), b = 3 * ones(N))
ComponentVector{Float64}(r = [1.0, 1.0], b = [3.0, 3.0])

julia> p2 = ComponentArray(r = 2 * ones(N))
ComponentVector{Float64}(r = [2.0, 2.0])

julia> ComponentArray(p; p2...)
ComponentVector{Float64}(r = [2.0, 2.0], b = [3.0, 3.0])

@jonniedie
Copy link
Owner

I don't know that it is especially efficient, though

@vboussange
Copy link
Author

The problem with the code that you propose is that it is not compatible with Zygote. Here is a version that works with Zygote:

import Base
function Base.merge(ca::ComponentArray{T}, ca2::ComponentArray{T}) where T
    ax = getaxes(ca)
    ax2 = getaxes(ca2)
    vks = valkeys(ax[1])
    vks2 = valkeys(ax2[1])
    _p = Vector{T}()
    for vk in vks
        if vk in vks2
            _p = vcat(_p, ca2[vk])
        else
            _p = vcat(_p, ca[vk])
        end
    end
    ComponentArray(_p, ax)
end

However, here it is assumed that ca and ca2 are ComponentVector without nested fields. One needs to improve this piece of code so that it works in the general case where ca and ca2 are general ComponentArrays, and have nested fields.

@scheidan
Copy link
Contributor

see also #69

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

3 participants