# Non-allocating reduced broadcast?

**URL:** <https://discourse.julialang.org/t/non-allocating-reduced-broadcast/23634>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 28, 2019, 11:17pm UTC](https://discourse.julialang.org/t/non-allocating-reduced-broadcast/23634 "2019-04-28T23:17:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [April 28, 2019, 11:17pm UTC](https://discourse.julialang.org/t/non-allocating-reduced-broadcast/23634/1 "2019-04-28T23:17:54Z")

</div>

Suppose `A` and `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?

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [April 28, 2019, 11:35pm UTC](https://discourse.julialang.org/t/non-allocating-reduced-broadcast/23634/2 "2019-04-28T23:35:19Z")

</div>

There is

[https://github.com/JuliaLang/julia/pull/31020](https://github.com/JuliaLang/julia/pull/31020)

which is blocked by

[https://github.com/JuliaLang/julia/issues/19198](https://github.com/JuliaLang/julia/issues/19198)

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [April 29, 2019, 5:51am UTC](https://discourse.julialang.org/t/non-allocating-reduced-broadcast/23634/3 "2019-04-29T05:51:35Z")

</div>

For this specific case you can also use `dot(A,B)` in the `LinearAlgebra` stdlib.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [April 29, 2019, 8:45am UTC](https://discourse.julialang.org/t/non-allocating-reduced-broadcast/23634/4 "2019-04-29T08:45:35Z")

</div>

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? - #15 by fabiangans](https://discourse.julialang.org/t/is-there-something-like-broadcast-mapreduce/6076/15). Perhaps a solution can come out of the `ArrayReduce` struct suggested there.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [April 29, 2019, 8:57am UTC](https://discourse.julialang.org/t/non-allocating-reduced-broadcast/23634/5 "2019-04-29T08:57:48Z")

</div>

Actually, you can do non-allocating reduction by `sum(@~ A .* B)` using `@~` macro from LazyArrays.jl. See:  
[https://github.com/JuliaArrays/LazyArrays.jl#broadcasting](https://github.com/JuliaArrays/LazyArrays.jl#broadcasting)

(But note that I’m actually suggesting to modify API a bit: [https://github.com/JuliaArrays/LazyArrays.jl/pull/31#issuecomment-487468147](https://github.com/JuliaArrays/LazyArrays.jl/pull/31#issuecomment-487468147))
