# How to broadcast this function call?

**URL:** <https://discourse.julialang.org/t/how-to-broadcast-this-function-call/124190>\
**Category:** New to Julia\
**Created:** [December 26, 2024, 5:37pm UTC](https://discourse.julialang.org/t/how-to-broadcast-this-function-call/124190 "2024-12-26T17:37:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [December 26, 2024, 5:37pm UTC](https://discourse.julialang.org/t/how-to-broadcast-this-function-call/124190/1 "2024-12-26T17:37:58Z")

</div>

I want to broadcast the function `g(u,t)` over the argument `u`:

```julia
module mwe

t = 1.0
xs = rand(4)
u = sin.(xs) # therefore, length(xs) is always equal to length(u)

function Φ(x, t)
    return 2 * exp(t) + exp(t) * (1 - x) * x - (1 + exp(2 * t) * (1 - x)^2 * x^2)^(-1)
end

function g(u, t)
    return 1 / (1 + u^2) + Φ.(xs, t)
end

@show g.(u, t)

end

```

However, it returns

```julia
ERROR: MethodError: no method matching +(::Float64, ::Vector{Float64})
For element-wise addition, use broadcasting with dot syntax: scalar .+ array
The function `+` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...)
   @ Base operators.jl:596
  +(::ChainRulesCore.ZeroTangent, ::Any)
   @ ChainRulesCore ~/.julia/packages/ChainRulesCore/6Pucz/src/tangent_arithmetic.jl:99
  +(::Any, ::ChainRulesCore.NotImplemented)
   @ ChainRulesCore ~/.julia/packages/ChainRulesCore/6Pucz/src/tangent_arithmetic.jl:25
  ...

Stacktrace:
 [1] g(u::Float64, t::Float64)
   @ Main.mwe ~/examples/mwe2.jl:11
 [2] _broadcast_getindex_evalf
   @ ./broadcast.jl:673 [inlined]
 [3] _broadcast_getindex
   @ ./broadcast.jl:646 [inlined]
 [4] getindex
   @ ./broadcast.jl:605 [inlined]
 [5] copy
   @ ./broadcast.jl:906 [inlined]
 [6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{…}, Nothing, typeof(Main.mwe.g), Tuple{…}})
   @ Base.Broadcast ./broadcast.jl:867
 [7] macro expansion
   @ show.jl:1232 [inlined]
 [8] top-level scope
   @ ~/examples/mwe2.jl:14
Some type information was truncated. Use `show(err)` to see complete types.

```

How can I fix it?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 26, 2024, 5:44pm UTC](https://discourse.julialang.org/t/how-to-broadcast-this-function-call/124190/2 "2024-12-26T17:44:03Z")

</div>

> [@homocomputeris](#):
>
> ```julia
> 1 / (1 + u^2) + Φ.(xs, t)
> end
> 
> ```

The problem isn’t the broadcast of `g`, but this here. Since you’re broadcasting \Phi, you get a vector back, and then the addition with `1 / (1 + u^2)` fails since that is a scalar. Dot the `+` there as well, or use `@.`.

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [December 26, 2024, 5:50pm UTC](https://discourse.julialang.org/t/how-to-broadcast-this-function-call/124190/3 "2024-12-26T17:50:39Z")

</div>

> [@Sukera](#):
>
> Dot the `+` there as well, or use `@.`.

The former doesn’t really work (returns a vector of vectors or an error), the latter does.

Thanks.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 26, 2024, 5:59pm UTC](https://discourse.julialang.org/t/how-to-broadcast-this-function-call/124190/4 "2024-12-26T17:59:18Z")

</div>

> [@homocomputeris](#):
>
> ```julia
> function g(u, t)
> return 1 / (1 + u^2) + Φ.(xs, t)
> end
> 
> @show g.(u, t)
> 
> ```

A good way to solve would be to pass `xs` as a parameter to `g`:

```julia
function g(x, u, t)
    return 1 / (1 + u^2) + Φ(x, t)
end

@show g.(xs, u, t)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 27, 2024, 2:50am UTC](https://discourse.julialang.org/t/how-to-broadcast-this-function-call/124190/5 "2024-12-27T02:50:34Z")

</div>

> [@Dan](#):
>
> A good way to solve would be to pass `xs` as a parameter to `g`:

(It’s also good practice in general to avoid passing data via global variables, and to use parameters instead.)

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [December 27, 2024, 12:27pm UTC](https://discourse.julialang.org/t/how-to-broadcast-this-function-call/124190/6 "2024-12-27T12:27:18Z")

</div>

> [@Dan](#):
>
> A good way to solve would be to pass `xs` as a parameter to `g`:

This is reasonable, but it’s not very convenient to have semantically close functions with different signatures like `(x,u,t) ` and `(u,t)`. Adding dummy variable `x,...` to all the rest of the functions with `u,t` doesn’t seem to be an elegant solution.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 27, 2024, 2:58pm UTC](https://discourse.julialang.org/t/how-to-broadcast-this-function-call/124190/7 "2024-12-27T14:58:00Z")

</div>

But are they really that similar?

You’re just hiding the fact that `g` is a function of 3 arguments by silently depending on a global variable. This makes the similarity perhaps a bit artificial.

This is not only misleading, but it’s also highly inefficient _and_ can cause catastrophic bugs if `xs` is ever mutated
