A = randn(5,4,2)
B = randn(5,1,1)
C = zeros(5,4,1)
sum!(C, A .* B) # avoid forming `A.*B` ?
Is there a simple way to avoid forming the intermediate array A.*B? I was looking into the reducing functions in Base but I couldn’t find a suitable function for this.
I know I can write a kernel by hand, but if there is something in Base for this that I’m missing I would like to exploit it.
Or possibly now sum!(C, LazyArray(@~ A .* B)) after a recent change. The difference is that sum(::Broadcasted) is not especially quick, although this will be fixed: #31020.
Looking at that issue, it seems like dot (docs) will do the job for this case also (unless the arrays are complex in which case there will be an extra complex conjugate on the first argument).
That was badly worded, I just meant that my solution was not in Base. Although how easily it can be done with only Base I don’t know. Writing sum(Base.broadcasted(*, A, B), dims=3) is also an error, which is the equivalent of sum!(C,... without a pre-allocated destination.
But even when reducing on all dimensions, there is still an issue of which methods of sum get used; LazyArray makes something <: AbstractArray which gets summed more efficiently right now, but there is a PR to fix that eventually.