# Trace of broadcasting

**URL:** <https://discourse.julialang.org/t/trace-of-broadcasting/35210>\
**Category:** General Usage\
**Created:** [February 27, 2020, 4:25am UTC](https://discourse.julialang.org/t/trace-of-broadcasting/35210 "2020-02-27T04:25:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 27, 2020, 4:25am UTC](https://discourse.julialang.org/t/trace-of-broadcasting/35210/1 "2020-02-27T04:25:30Z")

</div>

[docs](https://docs.julialang.org/en/v1.3/manual/arrays/#Broadcasting-1) said:

> `f.(args...)` is equivalent to `broadcast(f, args...)`

however, I found that `f.()` is **not** calling `broadcast(f, )`:

```julia
Base.broadcast(::typeof(exp), x) = println("exp ", x);
x = [1.0, 2.0];

julia> Base.broadcast(exp, x)
exp [1.0, 2.0]

julia> exp.(x)
2-element Array{Float64,1}:
 2.718281828459045 
 7.3890560989306495

```

in above, `exp.()` is **not** calling the `broadcast()` I tried to override. Is there any method to enforce it? thanks. 🙏

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 27, 2020, 6:15am UTC](https://discourse.julialang.org/t/trace-of-broadcasting/35210/2 "2020-02-27T06:15:34Z")

</div>

If you want to customize broadcasting, the relevant documentation is

[https://docs.julialang.org/en/v1.3/manual/interfaces/#man-interfaces-broadcasting-1](https://docs.julialang.org/en/v1.3/manual/interfaces/#man-interfaces-broadcasting-1)

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 27, 2020, 6:49am UTC](https://discourse.julialang.org/t/trace-of-broadcasting/35210/3 "2020-02-27T06:49:57Z")

</div>

I mean, why `f.()` not compiled into `broadcast(f, )` ?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 27, 2020, 6:57am UTC](https://discourse.julialang.org/t/trace-of-broadcasting/35210/4 "2020-02-27T06:57:09Z")

</div>

I don’t know the answer to that, but in case you didn’t know, here’s how to check what is being called:

```julia
julia> @code_lowered (()->f.(x))()
CodeInfo(
1 ─ %1 = Base.broadcasted(Main.f, Main.x)
│ %2 = Base.materialize(%1)
└── return %2

```

---

<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:** [February 27, 2020, 7:35am UTC](https://discourse.julialang.org/t/trace-of-broadcasting/35210/5 "2020-02-27T07:35:17Z")

</div>

Alternatively:

```julia
julia> Meta.@lower f.(x)
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Base.broadcasted(f, x)
│ %2 = Base.materialize(%1)
└── return %2
))))

```

> [@tomtom](#):
>
> why `f.()` not compiled into `broadcast(f, )` ?

`broadcast(f, x)` needs to return an array if `x` is an array. If `f.(g.(x))` is lowered to `broadcast(f, broadcast(g, x))`, it would need to create intermediate array. Instead, `julia` does

```julia
julia> Meta.@lower f.(g.(x))
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Base.broadcasted(g, x)
│ %2 = Base.broadcasted(f, %1)
│ %3 = Base.materialize(%2)
└── return %3
))))

```

If you read the manual you’d notice that `broadcasted` (note `ed`) does not do the actual computation. Rather, the actual computation is done in `materialize` without creating any intermediate arrays.

> [@tomtom](#):
>
> [docs](https://docs.julialang.org/en/v1.3/manual/arrays/#Broadcasting-1) said:
> 
> > `f.(args...)` is equivalent to `broadcast(f, args...)`
> 
> however, I found that `f.()` is **not** calling `broadcast(f, )`

It’s actually `broadcast(f, )` that calls (the lowered version of) `f.()`:

> <https://github.com/JuliaLang/julia/blob/c2cd601f01849aca85c843f3eb277a0342fee0a6/base/broadcast.jl#L758>
