Skip to content
AgileLogics
7 min read

Linear Algebra

Prerequisites: Matrices and multidimensional arrays

The LinearAlgebra standard library provides fast, high-quality linear algebra (backed by BLAS and LAPACK). Matrix multiplication is plain *:

using LinearAlgebra

A = [2.0 1.0; 1.0 3.0]
B = [1.0 0.0; 2.0 1.0]
display(A * B)
display(A')
println(det(A), " ", tr(A), " ", rank(A))
display(inv(A))
display(A * inv(A) ≈ I)
2×2 Matrix{Float64}:
 4.0  1.0
 7.0  3.0
2×2 adjoint(::Matrix{Float64}) with eltype Float64:
 2.0  1.0
 1.0  3.0
5.0 5.0 2
2×2 Matrix{Float64}:
  0.6  -0.2
 -0.2   0.4
true
  • A' is the adjoint (conjugate transpose); for real matrices it's the transpose.
  • I is the identity matrix of whatever size is needed.
  • det, tr (trace), rank, inv do what you expect.

Solving linear systems

To solve A x = b, use the backslash operator — it picks a suitable factorization and is faster and more accurate than inv(A) * b:

using LinearAlgebra

# 2x + y = 5,  x + 3y = 10
A = [2.0 1.0; 1.0 3.0]
b = [5.0, 10.0]
x = A \ b
println(x)
println(A * x ≈ b)
[1.0, 3.0]
true

Vectors

using LinearAlgebra

u = [1.0, 2.0, 2.0]
v = [2.0, 0.0, 1.0]
println(dot(u, v), " ", u ⋅ v)
println(cross(u, v))
println(norm(u), " ", normalize(u))
println(u' * v)
display(u * v')
4.0 4.0
[2.0, 3.0, -4.0]
3.0 [0.3333333333333333, 0.6666666666666666, 0.6666666666666666]
4.0
3×3 Matrix{Float64}:
 2.0  0.0  1.0
 4.0  0.0  2.0
 4.0  0.0  2.0

u' * v is a row times a column — a number (the dot product). u * v' is a column times a row — the outer product matrix.

Eigenvalues and factorizations

using LinearAlgebra

S = [2.0 1.0; 1.0 2.0]
e = eigen(S)
println(e.values)
display(e.vectors)

F = lu([4.0 3.0; 6.0 3.0])
display(F.L)
display(F.U)
Q, R = qr([1.0 2.0; 3.0 4.0])
println(round.(Q * R, digits = 10))
[1.0, 3.0]
2×2 Matrix{Float64}:
 -0.707107  0.707107
  0.707107  0.707107
2×2 Matrix{Float64}:
 1.0       0.0
 0.666667  1.0
2×2 Matrix{Float64}:
 6.0  3.0
 0.0  1.0
[1.0 2.0; 3.0 4.0]

eigen, svd, lu, qr, and cholesky return factorization objects. Reuse a factorization to solve many systems with the same matrix: F = lu(A); F \ b1; F \ b2.

Least squares

For more equations than unknowns, \ finds the best fit. Fit a line y = a + b x:

using LinearAlgebra

x = [0.0, 1.0, 2.0, 3.0, 4.0]
y = [1.1, 2.9, 5.2, 7.1, 8.8]
X = [ones(length(x)) x]
a, b = X \ y
println("intercept = ", round(a, digits = 3), ", slope = ", round(b, digits = 3))
intercept = 1.1, slope = 1.96

Special matrix types

Diagonal, Symmetric, UpperTriangular, Tridiagonal, and sparse matrices (SparseArrays standard library) store only what they need and dispatch to specialized algorithms — multiple dispatch at work again.