I think I got it:
using RecursiveArrayTools
A = (rand(5),rand(5))
p = ArrayPartition(A)
B = (rand(5),rand(5))
p2 = ArrayPartition(B)
add_idxs(x,expr) = expr
add_idxs{T<:ArrayPartition}(::Type{T},expr) = :($(expr).x[i])
@generated function Base.broadcast!(f,A::ArrayPartition,B...)
@show B
println(@which (add_idxs(B[1],:(B[1]))))
exs = ((add_idxs(B[i],:(B[$i])) for i in eachindex(B))...)
@show exs
res = :(
for i in eachindex(A.x)
broadcast!(f,A.x[i],$(exs...))
end
)
@show res
res
end
@generated function Base.broadcast(f,B::Union{Number,ArrayPartition}...)
@show B
println(@which (add_idxs(B[1],:(B[1]))))
exs = ((add_idxs(B[i],:(B[$i])) for i in eachindex(B))...)
@show exs
res = :(
for i in eachindex(A.x)
broadcast(f,$(exs...))
end
)
@show res
res
end
p .= (*).(p,5)
julia> p
RecursiveArrayTools.ArrayPartition{Tuple{Array{Float64,1},Array{Float64,1}}}(([2.59588,2.3586,3.54189,1.29322,3.55353],[2.92912,3.07754,4.3945,3.74696,2.87257]))
julia> p .= (*).(p,5)
B = (RecursiveArrayTools.ArrayPartition{Tuple{Array{Float64,1},Array{Float64,1}}},)
add_idxs{T<:RecursiveArrayTools.ArrayPartition{T}}(::Type{T}, expr) at REPL[20]:1
exs = (:(B[1].x[i]),)
res = :(for i = eachindex(A.x) # REPL[21], line 8:
broadcast!(f,A.x[i],B[1].x[i])
end)
julia> p
RecursiveArrayTools.ArrayPartition{Tuple{Array{Float64,1},Array{Float64,1}}}(([12.9794,11.793,17.7095,6.46608,17.7677],[14.6456,15.3877,21.9725,18.7348,14.3628]))
My only problem is that the Number
s are now showing up. Other ArrayPartition
s do. But multiplying by a number seems to occur anyways. Is that being fused and just doesn’t show up here? I am confused.