Skip to content

Lesson 6 of 9 · 29m

Linear Algebra: Factorizations, BLAS, and the Cost of `inv`

Use Julia's LinearAlgebra standard library like a numerical analyst: solve systems with factorizations, exploit structure, respect BLAS threading, and treat sparse matrices differently.

The line that makes numerical analysts twitch is x = inv(A) * b. It is mathematically familiar and computationally suspect. You almost never need the inverse. You need the action of the inverse on a vector or matrix, and factorizations provide that faster, more stably, and with better error control. Julia's LinearAlgebra package makes the correct pattern easy once you stop transliterating matrix algebra notation.

Solve, do not invert

using LinearAlgebra

A = randn(1000, 1000)
b = randn(1000)
x = A \ b

\ chooses an appropriate factorization and solve path. For repeated solves with the same matrix, factor once:

F = lu(A)
x1 = F \ b
x2 = F \ randn(1000)

This is both faster and clearer about what is being reused. If the matrix changes every iteration, caching the factorization is not an optimization; it is a correctness bug.

Tell the library about structure

Dense generic algorithms ignore information you already know. cholesky(Symmetric(A' * A)) says far more to the library than a dense unknown matrix does. Use Symmetric, Hermitian, UpperTriangular, Diagonal, and sparse types when they describe the mathematics. Positive definite systems can use Cholesky; symmetric indefinite systems need different care; triangular systems should never be treated as generic dense solves.

Inspect residuals, not vibes

After solving, check the residual in the norm relevant to the model:

r = A * x - b
relative_residual = norm(r) / (norm(A) * norm(x) + norm(b))

A small residual does not prove a tiny forward error in an ill-conditioned problem, but a large residual means something is already wrong. Solver tolerances, convergence reports, and matrix condition numbers belong in serious diagnostics.

BLAS threading is a resource allocation decision

Dense operations call multithreaded BLAS by default in many environments; inspect and control that with BLAS.get_num_threads() and BLAS.set_num_threads(4). More BLAS threads are not always faster. If Julia itself parallelizes outer work with task threads, nested BLAS parallelism can oversubscribe the machine, so batch jobs should set thread counts explicitly.

JULIA_NUM_THREADS=8 OPENBLAS_NUM_THREADS=1 julia --project run.jl

That split often works well when Julia parallelizes independent simulations and each simulation calls BLAS internally. Measure; do not copy it as ritual.

Sparse matrices are a different discipline

SparseArrays matrices save memory and exploit zeros, but factorization can fill in dramatically. Row ordering, graph structure, and memory allocation matter; do not benchmark sparse methods by densifying the matrix. At scale, mul!(C, A, B) and reusable work buffers can also reduce allocation, but profile before obscuring numerical structure for speed.

Field notes

  • A \ b is not an inverse operation; it is a solve interface that can exploit matrix type.
  • Never use cholesky unless positive definiteness is actually established; use factorization failure as diagnostic information.
  • cond(A) estimates sensitivity, but the cost may be meaningful for very large matrices.
  • BLAS threads and Julia task threads are separate knobs; multiplying them together can oversubscribe CPUs.
  • In sparse workflows, memory fill-in often dominates asymptotic operation counts.

Was this lesson helpful?

Sign in to rate