TExcelTAD
Home › Julia › Julia Linear Algebra

Julia Linear Algebra

Linear algebra is one of Julia's strongest areas. Matrix multiplication and the backslash solver are part of Base, and the standard library module LinearAlgebra adds determinants, inverses, norms, factorizations and eigenvalues, all backed by the battle-tested BLAS and LAPACK libraries.

julia · try it yourself
using LinearAlgebra

A = [2.0 1.0; 1.0 3.0]
v = [1.0, 2.0]

println(A * v)              # matrix-vector product
println(A * A)              # matrix-matrix product
println(A')                 # transpose (adjoint)
println(dot(v, v), " ", v ⋅ v)
println(norm(v))
[4.0, 7.0]
[5.0 5.0; 5.0 10.0]
[2.0 1.0; 1.0 3.0]
5.0 5.0
2.23606797749979

⋅ (typed \cdot) is the same as dot, and × (\times) is the cross product of 3-vectors.

Vectors

julia · try it yourself
using LinearAlgebra

a = [1, 0, 0]
b = [0, 1, 0]
println(cross(a, b), " ", a × b)
println(dot([1, 2, 3], [4, 5, 6]))
println(norm([3, 4]), " ", norm([3, 4], 1), " ", norm([3, 4], Inf))
println(round.(normalize([3.0, 4.0]), digits=4))
angle_deg = acosd(dot([1, 0], [1, 1]) / (norm([1, 0]) * norm([1, 1])))
println(round(angle_deg, digits=6))
[0, 0, 1] [0, 0, 1]
32
5.0 7.0 4.0
[0.6, 0.8]
45.0

Determinant, inverse, rank, trace

julia · try it yourself
using LinearAlgebra

M = [4 7; 2 6]
println(det(M))
println(round.(inv(M), digits=4))
println(rank(M), " ", tr(M))
println(round.(inv(M) * M, digits=10))

S = [1 2; 2 4]                 # second row is 2 × the first
println(rank(S), " ", det(S))
println(Matrix{Int}(I, 3, 3))   # identity matrix
10.0
[0.6 -0.7; -0.2 0.4]
2 10
[1.0 0.0; 0.0 1.0]
1 0.0
[1 0 0; 0 1 0; 0 0 1]

I is a special object representing an identity matrix of whatever size is needed: A + 2I adds 2 to the diagonal of A without building a full identity matrix.

Solving linear systems

To solve A x = b, use the backslash operator A \ b. It picks a suitable method (LU, Cholesky, QR, ...) based on the matrix's structure, and is faster and more accurate than computing inv(A) * b:

julia · try it yourself
using LinearAlgebra

# 2x +  y -  z =   8
# -3x - y + 2z = -11
# -2x + y + 2z =  -3
A = [2 1 -1; -3 -1 2; -2 1 2]
b = [8, -11, -3]
x = A \ b
println(round.(x, digits=10))
println(A * x ≈ b)

# Least squares: fit y = c1 + c2*t to noisy points
t = [0.0, 1.0, 2.0, 3.0]
y = [1.1, 2.9, 5.2, 6.8]
X = [ones(4) t]
c = X \ y
println(round.(c, digits=3))
[2.0, 3.0, -1.0]
true
[1.09, 1.94]

When A isn't square, \ returns the least-squares solution, which is how you fit a straight line: here y ≈ 1.09 + 1.94t.

Eigenvalues and factorizations

julia · try it yourself
using LinearAlgebra

S = [2.0 1.0; 1.0 2.0]
vals, vecs = eigen(S)
println(vals)
println(round.(abs.(vecs), digits=4))

F = lu([4.0 3.0; 6.0 3.0])
println(F.L)
println(F.U)

C = cholesky([4.0 2.0; 2.0 3.0])
println(round.(C.L, digits=4))
println(issymmetric(S), " ", isposdef(S))
[1.0, 3.0]
[0.7071 0.7071; 0.7071 0.7071]
[1.0 0.0; 0.6666666666666666 1.0]
[6.0 3.0; 0.0 1.0]
[2.0 0.0; 1.0 1.4142]
true true
Factorization Function Typical use
LU lu(A) general square systems
Cholesky cholesky(A) symmetric positive-definite systems
QR qr(A) least squares
Eigen eigen(A), eigvals(A) eigenvalues/vectors
SVD svd(A) rank, pseudo-inverse, compression

A factorization object can be reused to solve many systems with the same matrix: F = lu(A); x1 = F \ b1; x2 = F \ b2.

Special matrix types

LinearAlgebra provides structured matrix types that store only what's needed and use faster algorithms: Diagonal, Symmetric, UpperTriangular, LowerTriangular, Tridiagonal.

julia · try it yourself
using LinearAlgebra

D = Diagonal([1.0, 2.0, 4.0])
println(D * [1.0, 1.0, 1.0])
println(det(D), " ", inv(D).diag)
U = UpperTriangular([1 2 3; 4 5 6; 7 8 9])
println(Matrix(U))
[1.0, 2.0, 4.0]
8.0 [1.0, 0.5, 0.25]
[1 2 3; 0 5 6; 0 0 9]

Test yourself

What is the recommended way to solve A * x = b?

  1. x = inv(A) * b
  2. x = A \ b
  3. x = b / A
  4. x = solve(A, b)

What is det([1 2; 3 4])?

  1. 2.0
  2. -2.0
  3. 10.0
  4. 0.0

What does I represent in A + 3I?

  1. The imaginary unit
  2. An identity matrix of the right size
  3. An integer type
  4. The inverse of A