Skip to content

Lesson 4 of 9 · 26m

Arrays, Broadcasting, and Memory Identity

Master Julia's column-major arrays, dot broadcasting, mutation semantics, views, and aliasing so numerical code does what the mathematics says.

Most numerical bugs I have reviewed were not exotic solver failures. They were memory-shape bugs: a copy where a view was expected, mutation through an alias, a row-major mental model applied to a column-major array, or broadcasting that silently expanded the wrong dimension. Julia is explicit about arrays, but explicit only helps if you learn the model.

Arrays are mutable references

Assignment does not copy data:

a = [1.0, 2.0, 3.0]
b = a
b[1] = 99.0
a[1]  # 99.0

b is another name for the same array. Use copy(a) for a shallow copy and deepcopy(a) only when nested mutable structure truly requires it. In scientific code, accidental aliasing corrupts simulation state in ways unit tests often miss because every intermediate step still looks plausible.

Julia is column-major

for j in axes(A, 2), i in axes(A, 1)
    s += A[i, j]  # row index varies fastest: contiguous memory
end

The inner loop walks i, the row index, because consecutive rows in one column are contiguous in memory. Reverse the loops on a large matrix and the same arithmetic can become memory-bound and much slower.

Broadcasting is shaped by dots

x = [1.0, 2.0, 3.0]
y = 2.0 .* x .+ 1.0

Every dot means "apply this operation elementwise." The expression can fuse into one loop instead of allocating a temporary for every operator. This is more explicit than Python's hidden vectorization and often easier to reason about once the syntax stops looking noisy.

Shapes expand by dimension-one rules: a length-3 vector can broadcast across the rows of a 3 × 2 matrix, but mismatched non-one dimensions error. That error is a favor; silent expansion is how incorrect science gets published.

Slicing copies, views share

A = randn(100, 100)
row_copy = A[1, :]
row_view = @view A[1, :]

row_copy allocates new memory. row_view aliases the original matrix. In a hot loop, prefer views when mutation semantics permit:

r = @view A[i, :]
r ./= sum(abs, r)  # in-place row normalization without copying

The trailing ! is a convention warning callers that the function mutates an argument. It is not enforced by the compiler, so take it seriously as a contract.

Views are not free abstractions when passed to code expecting contiguous memory. Some BLAS operations and external libraries perform better on contiguous arrays, so a deliberate copy before an expensive factorization can be the correct cost.

Field notes

  • Use axes(A, d) or eachindex(A) rather than hard-coding 1:size(A, d) when writing generic array code.
  • reshape does not transpose data; it reinterprets linear order. Use transpose, permutedims, or explicit indexing when changing mathematical orientation.
  • A SubArray view is still an array abstraction. Passing it across a foreign-function boundary may force copying anyway.

Was this lesson helpful?

Sign in to rate