# Why does Julia require a . for broadcasting?

**URL:** <https://discourse.julialang.org/t/why-does-julia-require-a-for-broadcasting/61180>\
**Category:** Internals & Design\
**Created:** [May 15, 2021, 12:40am UTC](https://discourse.julialang.org/t/why-does-julia-require-a-for-broadcasting/61180 "2021-05-15T00:40:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://discourse.julialang.org/u/Beliavsky)\
**Post date:** [May 15, 2021, 12:40am UTC](https://discourse.julialang.org/t/why-does-julia-require-a-for-broadcasting/61180/1 "2021-05-15T00:40:44Z")

</div>

In Fortran and some other languages, you can write `exp(x)` whether x is a scalar or an array. In Julia, you need to write `exp.(x)` if x is an array. What is the benefit of requiring the dot?

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [May 15, 2021, 12:48am UTC](https://discourse.julialang.org/t/why-does-julia-require-a-for-broadcasting/61180/2 "2021-05-15T00:48:15Z")

</div>

There are plenty of functions that would be ambiguous, such as multiplication between two matrices of the same shape – do you want that to be matrix multiplication or elementwise? You need both for e.g. convolutional neural networks. Languages like Python with numpy generally make those choices on an ad-hoc basis, and avoid ambiguity by assigning multiple operators or just not letting you broadcast most functions.

Julia gives you the ability to broadcast any function, and has the programmer explicitly specify when you want to broadcast in order to resolve the ambiguity.

ETA: making broadcasting part of the syntax also allows certain optimizations, check out [More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/)

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [May 15, 2021, 12:52am UTC](https://discourse.julialang.org/t/why-does-julia-require-a-for-broadcasting/61180/3 "2021-05-15T00:52:41Z")

</div>

There is a well defined function usually called “exponential” of a matrix. It is not equal generally to the exponential of each element of the matrix. You can see this:

```julia
julia> a = rand(10,10)
julia> exp(a) == exp.(a)
false

```

If `exp(a)` did the exponential of every element of the matrix, how would you express the matrix exponentiation? You would then have to define some `mat_exp` which would lose many of the advantages that multiple dispatch brings us.

Oh! @Satvik is a much faster typer!

---

<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:** [May 15, 2021, 1:57am UTC](https://discourse.julialang.org/t/why-does-julia-require-a-for-broadcasting/61180/4 "2021-05-15T01:57:38Z")

</div>

> [@Satvik](#):
>
> There are plenty of functions that would be ambiguous, such as multiplication between two matrices of the same shape – do you want that to be matrix multiplication or elementwise?

I would say that this kind of disambiguation is a useful side effect of requiring dots for broadcasting functions like `exp`, but it was not the primary motivation. The primary motivations were:

1. You want the _caller_ to decide what functions to apply elementwise, and it should work for _any_ function. The function implementor should not need to decide this in advance, as [explained here](https://julialang.org/blog/2017/01/moredots/#which_functions_are_vectorized).

2. Having the caller indicate elementwise functions _syntactically_ allows us to guarantee that nested elementwise calls are “fused” into a single loop without temporary arrays, as [explained here](https://julialang.org/blog/2017/01/moredots/#why_does_julia_need_dots_to_fuse_the_loops). This is not a mere micro-optimization—fusion can have order-of-magnitude effects on performance and memory usage.

In early versions of Julia , functions like `exp` and `sqrt` did operate elementwise on array arguments, and we had separate functions `expm` and `sqrtm` for the “matrix algebra” versions (similar to Matlab and Numpy). After dot broadcasting was introduced in Julia 0.6, the latter functions could be renamed to `exp` and `sqrt` as a happy side effect.

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [May 15, 2021, 5:57am UTC](https://discourse.julialang.org/t/why-does-julia-require-a-for-broadcasting/61180/5 "2021-05-15T05:57:21Z")

</div>

> [@stevengj](#):
>
> You want the _caller_ to decide what functions to apply elementwise, and it should work for _any_ function. The function implementor should not need to decide this in advance, as [explained here](https://julialang.org/blog/2017/01/moredots/#which_functions_are_vectorized).

Can’t overstate how much how a great decision that was. I’m regularly amazed when writing plot(some\_complicated\_function.(1:N)). In Matlab I had to constantly worry about the shape of the input to a function whenever I thought I could want to apply it elementwise later.

---

<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:** [May 18, 2021, 3:14pm UTC](https://discourse.julialang.org/t/why-does-julia-require-a-for-broadcasting/61180/6 "2021-05-18T15:14:09Z")

</div>

14 posts were split to a new topic: [When does `exp(A) ≈ exp.(A)`?](https://discourse.julialang.org/t/when-does-exp-a-exp-a/61369)
