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

**URL:** https://discourse.julialang.org/t/is-it-possible-to-do-a-mapreduce-with-multiple-arrays-while-broadcasting-over-so/57433
**Category:** General Usage
**Created:** [March 18, 2021, 3:24am UTC](https://discourse.julialang.org/t/is-it-possible-to-do-a-mapreduce-with-multiple-arrays-while-broadcasting-over-so/57433 "2021-03-18T03:24:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)
#### Post date: [March 18, 2021, 3:24am UTC](https://discourse.julialang.org/t/is-it-possible-to-do-a-mapreduce-with-multiple-arrays-while-broadcasting-over-so/57433/1 "2021-03-18T03:24:17Z")

</div>

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

```julia
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_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1616034042074800?thread_ts=1616034042.074800&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

### Author: ![PolarizedPoutine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/polarizedpoutine/32/6067_2.png) [@PolarizedPoutine](https://discourse.julialang.org/u/PolarizedPoutine)
#### Post date: [March 18, 2021, 3:39am UTC](https://discourse.julialang.org/t/is-it-possible-to-do-a-mapreduce-with-multiple-arrays-while-broadcasting-over-so/57433/2 "2021-03-18T03:39:44Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

### Author: ![PolarizedPoutine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/polarizedpoutine/32/6067_2.png) [@PolarizedPoutine](https://discourse.julialang.org/u/PolarizedPoutine)
#### Post date: [March 18, 2021, 3:54am UTC](https://discourse.julialang.org/t/is-it-possible-to-do-a-mapreduce-with-multiple-arrays-while-broadcasting-over-so/57433/3 "2021-03-18T03:54:01Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)
#### Post date: [March 18, 2021, 7:00am UTC](https://discourse.julialang.org/t/is-it-possible-to-do-a-mapreduce-with-multiple-arrays-while-broadcasting-over-so/57433/4 "2021-03-18T07:00:29Z")

</div>

There is also [this workaround](https://discourse.julialang.org/t/is-there-something-like-broadcast-mapreduce/6076/15) where I made a non-allocating `broadcast_reduce`, but the tullio solution looks very elegant.

---

<div class="post-metadata">

### Author: ![PolarizedPoutine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/polarizedpoutine/32/6067_2.png) [@PolarizedPoutine](https://discourse.julialang.org/u/PolarizedPoutine)
#### Post date: [March 18, 2021, 1:44pm UTC](https://discourse.julialang.org/t/is-it-possible-to-do-a-mapreduce-with-multiple-arrays-while-broadcasting-over-so/57433/5 "2021-03-18T13:44:57Z")

</div>

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