# Is it possible to do a `mapreduce` with multiple arrays while broadcasting over so

Is it possible to do a `mapreduce` with multiple arrays while broadcasting over some dimensions? Something like this

``````U = rand(5, 5, 5)
Δ = rand(1, 1, 5)
mapreduce((u, Δx) -> u / Δx, max, U, Δ)
``````

where I really want to find the maximum of `U[i, j, k] / Δ[k]`.

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.

Answering my own question lol: Came up a double `mapreduce` but maybe there’s a better solution out there

``````julia> A = rand(5, 5);

julia> B = rand(1, 5);

julia> extrema(A ./ B)
(0.00770023760419805, 13.798242177927923)

julia> mapreduce(i -> mapreduce((x, y) -> x / y, min, A[i, :], B), min, 1:5)
0.00770023760419805

julia> mapreduce(i -> mapreduce((x, y) -> x / y, max, A[i, :], B), max, 1:5)
13.798242177927923
``````

@Mason suggested a beautiful solution using Tullio.jl. Thank you!

``````julia> let U = rand(3, 3, 3), Δ = rand(3)
@tullio (max) out := U[i, j, k] / Δ[k]
end
4.490419859484241
``````
1 Like

There is also this workaround where I made a non-allocating `broadcast_reduce`, but the tullio solution looks very elegant.

Ah that looks quite useful, thanks @fabiangans! Gonna experiment with what works best on large CuArrays so might end up needing `broadcast_reduce`.