# Specializing broadcasting for a scalar function

**URL:** <https://discourse.julialang.org/t/specializing-broadcasting-for-a-scalar-function/59121>\
**Category:** General Usage\
**Created:** [April 12, 2021, 3:51pm UTC](https://discourse.julialang.org/t/specializing-broadcasting-for-a-scalar-function/59121 "2021-04-12T15:51:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [April 12, 2021, 3:51pm UTC](https://discourse.julialang.org/t/specializing-broadcasting-for-a-scalar-function/59121/1 "2021-04-12T15:51:17Z")

</div>

Let’s say I have a function

```julia
function f(a, b)
     temp = do_something_expensive(a)
     do_other_thing(temp, b)
end

```

I like writing scalar functions because they don’t obscure the logic with additional loops. But in this case, `do_something_expensive` can bite me performance-wise if I broadcast over a scalar `a` and a vector `b`:

```julia
# runs do_something_expensive on a_scalar for every b in b_vector
f.(a_scalar, b_vector)

```

Can one overload broadcasting for the case where a is a scalar and b is a vector in order to only run `do_something_expensive` once? Or would it make more sense to make another method for scalar and vector arguments and use that instead? I kind of like to just expose one scalar method to the user and do intelligent things in the background if it’s used in broadcasted style.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 12, 2021, 4:02pm UTC](https://discourse.julialang.org/t/specializing-broadcasting-for-a-scalar-function/59121/2 "2021-04-12T16:02:59Z")

</div>

Yes, this is possible by overloading `Base.broadcasted` specifically for `f`:

```julia
julia> do_something_expensive(x) = (println("don't run me twice!"); x)
do_something_expensive (generic function with 1 method)

julia> do_other_thing(x, y) = (println("this is fine"); (x, y))
do_other_thing (generic function with 1 method)

julia> function f(a, b)
            temp = do_something_expensive(a)
            do_other_thing(temp, b)
       end
f (generic function with 1 method)

julia> function Base.broadcasted(::typeof(f), a::Number, b::AbstractVector)
           temp = do_something_expensive(a)
           return Base.broadcasted(do_other_thing, temp, b)
       end

julia> f.(1, 1:3)
don't run me twice!
this is fine
this is fine
this is fine
3-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (1, 2)
 (1, 3)

```

Whether this is a good idea is another question though. This approach does make it more obfuscated to see what’s actually going on, so perhaps you just want to have the user call `do_something_expensive` and `do_other_thing` themselves.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [April 12, 2021, 4:06pm UTC](https://discourse.julialang.org/t/specializing-broadcasting-for-a-scalar-function/59121/3 "2021-04-12T16:06:01Z")

</div>

In this case the user is myself but I see your point 😉 For example `f` can be a projection function `project(scene, point)` which needs to precompute some matrix depending on `scene`, but it shouldn’t do that anew for every point I want to project.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 12, 2021, 4:08pm UTC](https://discourse.julialang.org/t/specializing-broadcasting-for-a-scalar-function/59121/4 "2021-04-12T16:08:18Z")

</div>

One potential answer is to use memoization on `project`, but that isn’t always a good solution.

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [April 12, 2021, 4:11pm UTC](https://discourse.julialang.org/t/specializing-broadcasting-for-a-scalar-function/59121/5 "2021-04-12T16:11:56Z")

</div>

That seems like a reasonable use of specializing `Base.broadcasted`: it sounds like you just want to optimize the computation without changing behavior. There are lots of examples of specializations like this in Base (e.g. specializing `map(vcat, array_of_arrays)` for efficient array concatenation).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 12, 2021, 4:37pm UTC](https://discourse.julialang.org/t/specializing-broadcasting-for-a-scalar-function/59121/6 "2021-04-12T16:37:13Z")

</div>

I think it would be nicer to break this up into

```julia
project = projection(scene) # create projector object or function 
p = project(point) 

```

and then

```julia
p = project.(points)

```

I would prefer that way of organizing the calculation. That way you can create several `project`ors for different `scene`s and pass them around, for example.
