TExcelTAD
Home › Julia › Julia Broadcasting

Julia Broadcasting

Broadcasting applies a function or operator element by element across arrays. In Julia you request it with a dot: f.(x) calls f on every element of x, and x .+ y adds element-wise. Any function — including your own — can be broadcast this way.

julia · try it yourself
v = [1, 2, 3, 4]
println(v .* 10)
println(v .^ 2)
println(sqrt.(v))
println(v .> 2)

double(x) = 2x
println(double.(v))
println(uppercase.(["a", "b"]))
[10, 20, 30, 40]
[1, 4, 9, 16]
[1.0, 1.4142135623730951, 1.7320508075688772, 2.0]
Bool[0, 0, 1, 1]
[2, 4, 6, 8]
["A", "B"]

Why do we need a dot?

Many operators have a mathematical meaning on whole arrays. A * B is matrix multiplication and v^2 would mean "v times v" in the linear-algebra sense, which isn't defined for vectors. The dot makes element-wise intent explicit:

julia · try it yourself
A = [1 2; 3 4]
println(A * A)       # matrix product
println(A .* A)      # element-wise product
println(A .+ 1)      # add 1 to each element
try
    [1, 2] * [3, 4]
catch e
    println(typeof(e))
end
println([1, 2] .* [3, 4])
[7 10; 15 22]
[1 4; 9 16]
[2 3; 4 5]
MethodError
[3, 8]

Shapes that broadcast

Broadcasting doesn't require equal sizes. A scalar is reused for every element, and a dimension of length 1 is "stretched" to match the other array. A column vector combined with a row vector produces a full matrix:

julia · try it yourself
col = [1, 2, 3]          # 3-element column
row = [10 20]            # 1×2 row
println(col .+ row)
println(size(col .* row))

prices = [2.0, 5.0]
discounts = [0.0 0.1 0.25]
println(prices .* (1 .- discounts))
try
    [1, 2, 3] .+ [1, 2]
catch e
    println(typeof(e))
end
[11 21; 12 22; 13 23]
(3, 2)
[2.0 1.8 1.5; 5.0 4.5 3.75]
DimensionMismatch

Sizes must match or be 1 in each dimension; a 3-vector and a 2-vector can't broadcast together.

Dot fusion and @.

Several dotted operations in one expression are fused into a single loop, without creating temporary arrays. The @. macro adds dots to every call and operator in an expression:

julia · try it yourself
x = [0.0, 0.5, 1.0]
y1 = 3 .* x .^ 2 .+ 2 .* x .+ 1
y2 = @. 3x^2 + 2x + 1
println(y1)
println(y1 == y2)

out = similar(x)
out .= x .* 2          # .= writes into the existing array
println(out)
@. out = sin(x)^2 + cos(x)^2
println(out)
[1.0, 2.75, 6.0]
true
[0.0, 1.0, 2.0]
[1.0, 1.0, 1.0]

.= is in-place assignment: it fills out instead of creating a new array, which is important in performance-critical loops.

Protecting arguments from broadcasting

Sometimes one argument should be treated as a single object, not iterated. Wrap it in Ref(...) or a one-element tuple:

julia · try it yourself
allowed = ["red", "green"]
colors = ["red", "blue", "green"]
println(in.(colors, Ref(allowed)))
println(colors .∈ (allowed,))

words = ["cat", "horse"]
println(occursin.("o", words))
println(string.("item-", 1:3))
Bool[1, 0, 1]
Bool[1, 0, 1]
Bool[0, 1]
["item-1", "item-2", "item-3"]

Without Ref, in.(colors, allowed) would try to pair colors[i] with allowed[i] and fail because the lengths differ. Strings are already treated as scalars by broadcasting, which is why occursin.("o", words) works directly.

Test yourself

What is [1, 2, 3] .+ 10?

  1. [11, 12, 13]
  2. [1, 2, 3, 10]
  3. an error
  4. 16

What does @. a * b + c expand to?

  1. a * b + c
  2. a .* b .+ c
  3. @a * @b + @c
  4. map(a * b + c)

What is size([1, 2] .* [1 2 3])?

  1. (2,)
  2. (3,)
  3. (2, 3)
  4. a DimensionMismatch