B are two long arrays of numbers of same length. Then
sum(A .* B) allocates the intermediary array
A .* B, and then sums it (the reduction).
I know I can write this as a loop or using generators, but at the price of losing the terseness of the dotted broadcast syntax. So is there a way to combine dotted calls with a reducing operation without intermediate allocations?
For this specific case you can also use
dot(A,B) in the
This was just an example. I have a more complex scenario in mind where
dot doesn’t help.
This is very clever: Is there something like broadcast_mapreduce?. Perhaps a solution can come out of the
ArrayReduce struct suggested there.
Actually, you can do non-allocating reduction by
sum(@~ A .* B) using
@~ macro from LazyArrays.jl. See:
(But note that I’m actually suggesting to modify API a bit: https://github.com/JuliaArrays/LazyArrays.jl/pull/31#issuecomment-487468147)