ZetCode

Julia Linear Algebra Tutorial

last modified November 18, 2026

Linear algebra is the branch of mathematics concerned with vectors, matrices and the systems of linear equations they represent. Julia ships the LinearAlgebra standard library, which provides matrix operations, solvers, decompositions and vector products out of the box. This tutorial introduces its most useful tools with small matrices whose printed results are easy to follow.

A matrix is a rectangular grid of numbers arranged in rows and columns; a vector is a one-dimensional matrix. The LinearAlgebra library is part of every Julia installation, so no package needs to be installed. All examples were verified with Julia 1.13.4.

Creating Matrices

Julia's array syntax builds a matrix with spaces between columns and semicolons between rows. The size function reports the number of rows and columns.

main.jl
using LinearAlgebra

A = [1 2; 3 4]
println(A)
println(size(A))

The expression [1 2; 3 4] produces a two-by-two matrix: the first row is 1 2 and the second row is 3 4. Because all entries are integers, the element type is Int64. The size function returns a tuple (rows, columns).

$ julia main.jl
[1 2; 3 4]
(2, 2)

Matrix Multiplication

The asterisk * performs matrix multiplication when both operands are matrices. The result has the row count of the left operand and the column count of the right operand.

main.jl
using LinearAlgebra

A = [1 2; 3 4]
B = [5 6; 7 8]
C = A * B
println(C)

Each entry of C is the sum of products of a row of A and a column of B. For example, the top-left entry is 1*5 + 2*7 = 19. Matrix multiplication is not interchangeable: A * B and B * A generally give different results. For element-by-element multiplication, use the broadcast operator .* instead.

$ julia main.jl
[19 22; 43 50]

Matrix-Vector Multiplication

The same * operator multiplies a matrix by a vector, producing a new vector. This is a compact way to apply a linear transformation.

main.jl
using LinearAlgebra

A = [1 2; 3 4]
v = [5, 6]
println(A * v)

The matrix has two columns and the vector has two entries, so their lengths match. The first result entry is 1*5 + 2*6 = 17 and the second is 3*5 + 4*6 = 39. A length mismatch raises a DimensionMismatch error.

$ julia main.jl
[17, 39]

Solving Linear Systems

A linear system has the form A * x = b, where A and b are known and x is unknown. The backslash operator \ solves such a system.

main.jl
using LinearAlgebra

A = [2 1; 1 3]
b = [5, 10]
x = A \ b
println(x)
println(A * x)

Julia returns [1.0, 3.0]: the solution is x = 1 and y = 3. Multiplying A by the solution reproduces b, which confirms the result. Julia converts the integer inputs to floating point because solving uses a factorization. The \ operator is preferred over computing inv(A) * b: it is faster and more numerically stable.

$ julia main.jl
[1.0, 3.0]
[5.0, 10.0]

Dot Product

The dot product of two vectors is the sum of the products of their corresponding entries. It is a single number that measures how much two vectors point in the same direction.

main.jl
using LinearAlgebra

u = [1, 2, 3]
v = [4, 5, 6]
println(dot(u, v))

The calculation is 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32. The dot function requires both arguments to have the same length. For complex vectors, dot conjugates the first argument.

$ julia main.jl
32

Vector Norms

A norm assigns a non-negative length to a vector. The default norm(v) is the Euclidean (or 2-) norm; passing a second argument selects other norms.

main.jl
using LinearAlgebra

v = [3.0, 4.0]
println(norm(v))
println(norm(v, 1))
println(norm(v, Inf))

The Euclidean norm is sqrt(3² + 4²) = 5.0. The 1-norm is the sum of absolute values, 3 + 4 = 7.0, and the infinity norm is the largest absolute entry, 4.0. Norms are useful for measuring the size of vectors and the error of approximations.

$ julia main.jl
5.0
7.0
4.0

Transpose and Adjoint

The transpose of a matrix flips it across its diagonal: rows become columns. Julia provides the transpose function and the postfix apostrophe operator ', which is the adjoint — a conjugate transpose.

main.jl
using LinearAlgebra

A = [1 2; 3 4]
println(A')
println(transpose(A))
v = [1, 2, 3]
println(collect(transpose(v)))
Z = [1+2im, 3-4im]
println(Z')

For a real matrix, A' and transpose(A) are identical. Transposing the vector v produces a lazy wrapper, so collect materializes it as an ordinary row vector. For the complex vector Z, the adjoint also changes the sign of the imaginary parts, giving 1 - 2im and 3 + 4im.

$ julia main.jl
[1 3; 2 4]
[1 3; 2 4]
[1 2 3]
Complex{Int64}[1 - 2im 3 + 4im]

Eigenvalues and Eigenvectors

An eigenvector of a matrix is a vector whose direction is unchanged when the matrix is applied to it; it is only scaled. The scale factor is the corresponding eigenvalue. The eigen function returns both.

main.jl
using LinearAlgebra

A = [2 1; 1 2]
vals, vecs = eigen(A)
println(vals)
println(vecs)

The eigenvalues are 1.0 and 3.0. The matrix vecs stores the eigenvectors as columns: the first column belongs to 1.0 and the second to 3.0. The entries look familiar because 1/sqrt(2) is approximately 0.7071067811865475. Eigenvalues are central to stability analysis and to many physical models.

$ julia main.jl
[1.0, 3.0]
[-0.7071067811865475 0.7071067811865475; 0.7071067811865475 0.7071067811865475]

Singular Value Decomposition

The singular value decomposition (SVD) factors any matrix A into U * diag(s) * V', where U and V have orthonormal columns and s holds the non-negative singular values, ordered from largest to smallest.

main.jl
using LinearAlgebra

A = [1.0 2.0; 3.0 4.0]
U, s, V = svd(A)
println(U)
println(s)
println(V)

The vector s contains 5.465 and 0.366, and multiplying U * Diagonal(s) * V' reconstructs A. The ratio between the largest and smallest singular value is the condition number: a very small last value would mean the matrix is close to being singular, that is, close to having no unique inverse. SVD is the basis of techniques such as principal component analysis and low-rank approximation.

$ julia main.jl
[-0.4045535848337569 -0.9145142956773046; -0.9145142956773046 0.4045535848337569]
[5.464985704219043, 0.3659661906262575]
[-0.5760484367663209 0.8174155604703632; -0.8174155604703632 -0.5760484367663209]

Matrix Factorizations

A factorization rewrites a matrix as a product of simpler matrices so that repeated calculations become cheaper. The Cholesky factorization applies to symmetric positive-definite matrices; LU works for general square matrices.

main.jl
using LinearAlgebra

A = [4 1; 1 3]
F = cholesky(A)
println(F.L)
println(F.U)

B = [2 1; 1 3]
L, U, p = lu(B)
println(L)
println(U)
println(p)

Cholesky produces a lower-triangular L and its transpose U, so that A = L * U. LU produces a lower-triangular L, an upper-triangular U and a permutation vector p recording row swaps. Once a factorization exists, solving many systems with the same matrix reuses it through F \ b instead of recomputing it each time.

$ julia main.jl
[2.0 0.0; 0.5 1.6583123951777]
[2.0 0.5; 0.0 1.6583123951777]
[1.0 0.0; 0.5 1.0]
[2.0 1.0; 0.0 2.5]
[1, 2]

Best Practices for Linear Algebra

A few habits make numerical code more reliable and faster.

Source

Julia LinearAlgebra documentation

Julia Arrays documentation

This tutorial introduced the LinearAlgebra standard library: creating matrices, multiplication, solving systems, dot products, norms, transposes, eigenvalues, singular value decomposition and factorizations. The underlying arrays are described in Julia arrays, and the functions used in the examples are introduced in Julia functions. To make numerical code fast, see Julia performance tips.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.