Open vboussange opened 1 year ago
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])
I don't know that it is especially efficient, though
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 ComponentArray
s, and have nested fields.
see also #69
Hey there, I thought it'd be very cool to have a specialisation of the
merge
function forComponentArray
s, with a similar behaviour as withDictionary
s orNamedTuple
s.For instance, consider
It could be useful to have a similar functionality with
p
andp2
beingComponentArray
s. I am not sure whether this could be efficiently implemented though.