Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 40 ยท Collections

Broadcasting and the Dot Syntax

Understand Julia broadcasting: dot calls on any function, fused loops with @., in-place broadcast!, shape rules and how it differs from NumPy vectorization.

In many languages you apply a function to every element of an array by calling a special vectorized version, or by writing a loop. Julia uses a single, general tool for this: put a dot after the function name (f.(x)) or before an operator (x .+ y). That is called broadcasting. It works for any function, including ones you wrote yourself, and the compiler turns a whole chain of dotted operations into one fused loop with no temporary arrays.

This lesson explains what the dot does, how arrays of different shapes are stretched to fit, how @. and .= avoid allocations, and why this is different from NumPy-style vectorization. The Julia code is read-only on this site. The runnable examples use NumPy to demonstrate shape rules, and a small pure-Python model of fusion to show what the Julia compiler gains.

The dot call

Writing f.(x) means "call f on each element of x and collect the results in a new array of the same shape".

JULIA
x = [1.0, 4.0, 9.0]

sqrt.(x)             # [1.0, 2.0, 3.0]
x .+ 1               # [2.0, 5.0, 10.0]
x .^ 2               # [1.0, 16.0, 81.0]
x .* [1, 2, 3]       # [1.0, 8.0, 27.0]

sqrt(x)              # MethodError: no method matching sqrt(::Vector{Float64})

The last line is an important design point. In Julia sqrt means the square root of one number. There is no built-in vectorized sqrt, and the function would not know whether you want the square root of each element or the matrix square root. The dot makes your choice explicit. For the same reason A * B is a matrix product while A .* B multiplies entry by entry, as in Matrices and Linear Algebra.

Because the dot works on any function, your own functions are included for free. You do not write an array version.

JULIA
clamp01(v) = v < 0 ? 0.0 : (v > 1 ? 1.0 : v)

clamp01.([-0.5, 0.25, 3.0])      # [0.0, 0.25, 1.0]
parse.(Int, ["10", "20", "30"])  # [10, 20, 30]
uppercase.(["a", "b"])           # ["A", "B"]

What the dot means

f.(x) Call f on every element of x.
x .+ y Operators get a dot in front.
Any function Your own functions work too.
1 / 2

Fusion

When several dotted operations are chained in one expression, Julia does not compute them one at a time. It fuses them into a single loop that reads each input once and builds the output directly.

JULIA
x = collect(0.0:0.5:2.0)       # [0.0, 0.5, 1.0, 1.5, 2.0]

y = 3 .* x .^ 2 .+ 2 .* x .- 1
# one loop, one output array. Without fusion there would be separate
# temporary arrays for x.^2, 3 .* (that), 2 .* x, and each + or -

Typing a dot on every operator gets tiring, so the macro @. adds a dot to every call and operator in an expression:

JULIA
y = @. 3x^2 + 2x - 1           # same as the line above

(Numeric literals directly before a name, as in 3x, are multiplication.) The result is the same array, computed by the same fused loop, with much easier reading.

Fusion applies only when the whole expression is dotted. If you call a non-dotted function in the middle, like sum(x .^ 2), the part inside the parentheses is its own fused broadcast and then sum runs on the result. Writing sum(abs2, x) with a function argument avoids even that temporary.

Writing into an existing array

Even a fused broadcast allocates the output. In a loop that runs millions of times you can reuse an array by assigning with .=, which fuses the right-hand side and writes element by element into the existing destination:

JULIA
x = rand(1000)
y = similar(x)                 # uninitialized array with the same shape

y .= 3 .* x .^ 2 .+ 1          # writes into y, allocates nothing new
@. y = 3x^2 + 1                # the same thing, one @. covers the "="

x .+= 1                        # x = x .+ 1 in place (note the .+=)
broadcast!(sqrt, y, x)         # function form: y .= sqrt.(x)

Without the dot, y = 3 .* x .^ 2 .+ 1 creates a fresh array and rebinds the name y; the old array is simply thrown away. This distinction matters a great deal for performance.

Shapes and expansion

Broadcasting also stretches a smaller array to match a bigger one. The rules are the same as NumPy's: compare the sizes dimension by dimension, and a dimension of size 1 (or a missing one) is repeated to match the other. A scalar counts as having every dimension of size 1.

JULIA
col = [1, 2, 3]            # 3-element Vector (length 3)
row = [10 20]              # 1x2 Matrix

col .+ row                 # a 3x2 matrix:
# [11 21
#  12 22
#  13 23]

[1 2 3] .* [10, 20]        # a 2x3 matrix: row of 3 times column of 2

Sizes that cannot be matched raise a DimensionMismatch. For instance, a vector of length 3 and another of length 2 are incompatible: [1,2,3] .+ [1,2] fails.

Sometimes you want a collection to be treated as one scalar thing, not iterated. Wrap it in Ref:

JULIA
allowed = [1, 3, 5, 7]

in.([1, 4, 5], allowed)           # DimensionMismatch: allowed is iterated too
in.([1, 4, 5], Ref(allowed))      # [true, false, true]
[1, 4, 5] .โˆˆ Ref(allowed)         # the same, with the infix operator

Without Ref, Julia tries to walk allowed in step with the first array. Their lengths (3 and 4) do not match, so the call fails. If the lengths had matched, it would silently pair the elements one by one, which is not what you meant. Strings, on the other hand, are treated as scalars by broadcasting, so "a" .* ["x", "y"] gives ["ax", "ay"].

How this differs from NumPy

NumPy "vectorizes" by shipping a list of precompiled array functions: np.sin, np.add and so on. Each call is a separate pass that creates its own temporary array, and a function you write in Python is not fast on arrays unless you rewrite it in terms of those array operations, or use np.vectorize, which is only a convenience wrapper around a slow Python loop.

Julia broadcasts any function, and the compiler combines the dots into one loop. Your own scalar function, written with plain if statements, runs inside that loop at compiled speed. So in Julia a loop written by hand and a dotted expression are about equally fast, and you choose by readability.

NumPy Julia
Elementwise on arrays Special functions (np.sin) Any function with a dot (sin.(x))
Chained operations One temporary per operation Fused into one loop
Your own function Rewrite or np.vectorize Dot it, no rewrite
Output reuse out= arguments, += .=, @., broadcast!
A plain loop Slow Fast

Stand-ins you can run

The shape rules are identical in NumPy, so the first example reproduces the col .+ row table above and shows an incompatible case. NumPy's * already means elementwise, so no dot is needed there.

Python Broadcasting shapes in NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Fusion is a compiler feature, so this stand-in is a model, not Julia. It evaluates 3x^2 + 2x - 1 the NumPy way (one new list per step) and the Julia way (one loop, one output) and counts how many arrays are created.

Python A model of fused versus unfused loops

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the result of x .^ 2 when x = [1, 2, 3]?

    The dot applies the power to each element. Without the dot, x ^ 2 on a vector is an error.
  2. 2. What does @. do in front of an expression?

    @. y = 3x^2 + 1 is shorthand for dotting every operation, including the assignment, which gives one fused loop that writes into y.
  3. 3. What shape does [1, 2, 3] .+ [10 20] have?

    The column of length 3 is stretched across the 2 columns of the 1x2 row, giving a 3x2 matrix.
  4. 4. How is Julia broadcasting different from NumPy vectorization?

    Julia does not need special array versions of functions. The compiler compiles your scalar code into the fused loop.

Summary

  • f.(x) and x .+ y apply a function or operator to every element; the dot is how you ask for it.
  • Any function, including your own, can be broadcast without writing an array version.
  • A chain of dotted operations is fused into a single loop with no temporary arrays; @. dots a whole expression for you.
  • y .= expr, x .+= 1 and broadcast! write into existing arrays and avoid allocation.
  • Shapes are expanded dimension by dimension; use Ref to stop a collection from being iterated.
  • The result is typically as fast as a hand-written loop, unlike plain Python or NumPy code that makes a temporary per step.

Next: group fixed values with Tuples and Named Tuples.