Home / COMP2870 / Linear algebra
Linear Algebra
COMP2870 · weeks 4–7
Follows the 2026/27 notes by T. Ranner. Same examples, animated.
The plan
| When | Topic | Notes | Worksheet |
|---|---|---|---|
| Wk 4 Mon | Intro: matrices, vectors | Ch 1 | 1.4 Simple systems of linear equations |
| Wk 4 Wed | Systems, inverse, determinant (floating point = Lab 1, Ch 2) | Ch 2–3 | |
| Wk 5 Mon | Direct solvers: Gaussian elimination, LU, pivoting | Ch 4 | 1.5 Solving systems of linear equations |
| Wk 5 Wed | Iterative solvers: Jacobi, Gauss–Seidel | Ch 5 | |
| Wk 6 Mon | Complex numbers | Ch 6 | 1.6 Complex numbers and eigenvalues |
| Wk 6 Wed | Eigenvalues and eigenvectors | Ch 7 | |
| Wk 7 Mon | Practical eigenvalues: QR algorithm | Ch 8 |
Worksheet column is my best guess from the titles. Week 10 has a project using all of this (PCA).
1. A x = b: find x. Small or dense: Gaussian elimination (exact steps, O(n³)). Huge and sparse: Jacobi / Gauss–Seidel (guess, improve, repeat).
2. A x = λ x: find eigenvalues λ. 2×2 by hand: solve det(A − λI) = 0. Big: QR algorithm (keep swapping A for a similar matrix until it's triangular).
1. Multiplying matrices
Matrix = a grid of numbers. m rows, n columns: "m × n". Square if m = n. Vector = a matrix with one column.
Definition 1.1. A matrix is a rectangular array of numbers called entries or elements of the matrix. A matrix with m rows and n columns is called an m × n matrix or m-by-n matrix. We may additionally say that the matrix is of order m × n. If m = n, then we say that the matrix is square.
Definition 1.2. A column vector, often just called a vector, is a matrix with a single column. A matrix with a single row is a row vector. The entries of a vector are called components. A vector with n rows is called an n-vector.
To multiply, each row of A meets each column of B: multiply the matching pairs, add them up. Watch it happen:
Animation style ported from matrixmultiplication.xyz by André Staltz (MIT licence).
- (m × n) times (n × p) gives m × p. The inner numbers must match.
- AB ≠ BA in general. Order matters.
- A x (matrix times vector) is the same thing with p = 1. That's how a system of equations is written as A x = b.
2. Floating point Lab 1 · Ch 2
Computers store numbers with a fixed number of digits, so most numbers get rounded. Rounding errors are why pivoting (section 7) exists.
A number is stored as ± 0.b₁b₂…bt × βe. β = base, t = digits kept, e = exponent with L ≤ e ≤ U. "Normalised" means b₁ ≠ 0. eps = gap between 1 and the next number up = β1−t.
x = ±0.b₁b₂b₃…bt−1bt × βe. (β, t, L, U) fully defines a finite precision number system. … eps = β1−t = 2u. … Machine precision epsilon is the smallest positive number eps such that 1 + eps > 1.
Absolute error = |fl(x) − x|. Relative error = |fl(x) − x| / |x|, and it's always below u = ½β1−t. Order matters: (x + y) + z can differ from x + (y + z). Add the small numbers first.
Try it: in your browser, 0.1 + 0.2 = .
3. Systems A x = b, and the inverse Ch 3
n equations, n unknowns. A holds the coefficients, b is the right-hand side (given), x is what you find.
For a square A, these all mean the same thing: A x = b has exactly one solution for every b; A has an inverse; A is non-singular; det A ≠ 0.
The following statements are equivalent: The linear system Ax = b has a unique solution, for any right-hand side vector b. There exists a matrix (let's call it A−1 the inverse of A) such that A−1A = In, and we say that the matrix A is invertible. The matrix A is non-singular.
The 2×2 inverse
Big catch: don't solve A x = b by computing A−1. It's slower and less accurate than elimination (the notes time it: np.linalg.solve always wins).
Special matrices
- Lower triangular: zeros above the diagonal (aij = 0 for j > i). Upper triangular: zeros below. det = product of the diagonal.
- Sparse: mostly zeros, about αn non-zeros with α ≪ n (from graphs: entry ij ≠ 0 only if i and j are connected). Iterative methods love these.
4. The determinant
det A = the signed area (2D) or volume (3D) of the shape made by A's columns. Zero area means the columns are squashed flat, so det A = 0 ⇔ A is not invertible.
Definition 3.1. If n = 2, det A = a₁₁a₂₂ − a₂₁a₁₂. If n = 3, det A = a₁₁(a₂₂a₃₃ − a₂₃a₃₂) − a₁₂(a₂₁a₃₃ − a₂₃a₃₁) + a₁₃(a₂₁a₃₂ − a₂₂a₃₁). For general n, the determinant can be found by, for example, Laplace expansions det A = Σj=1n (−1)j+1 a1,j M1,j, where a1,j is the entry of the first row and jth column of A and M1,j is the determinant of the submatrix obtained by removing the first row and the jth column from A.
Rules: det(AB) = det A det B · det(cA) = cn det A · det(A−1) = 1 / det A.
5. Gaussian elimination Ch 4
The school method for simultaneous equations, written so a computer can follow it without thinking. Three moves are allowed. None of them change the solution:
These are the elementary row operations (EROs). Gaussian elimination uses them to make A upper triangular, then backward substitution finishes the job.
We can swap any two rows of the matrix (and corresponding right-hand side entries). We can multiply any row of the matrix (and corresponding right-hand side entry) by a scalar. We can replace row q by row q + k× row p.
The algorithm
- Use row 1 to put zeros under the diagonal in column 1.Subtract multiples of row 1 from the rows below it to eliminate (make zero) non-zero entries in column 1.
- Use the new row 2 to put zeros under the diagonal in column 2.Subtract multiples of the new row 2 from the rows below it to eliminate non-zero entries in column 2.
- Keep going until row n − 1. Now it's upper triangular: solve bottom-up.Repeat for row 3, 4, …, n − 1. After row n − 1, all entries below the diagonal have been eliminated, so A is now upper triangular and the resulting system can be solved by backward substitution.
Backward substitution (upper triangular, bottom-up) and forward substitution (lower triangular, top-down): each row has one new unknown; everything else is already known.
xi = (1/aii)(bi − Σj=1i−1 aijxj) for each row i = 1, 2, …, n in turn [forward]. Each calculation only involves values xj that have already been computed (j < i). The matrix A must have non-zero diagonal entries.
6. Cost, and LU factorisation
- Elimination costs O(n³). Back substitution costs only O(n²).
- Same A, many different b's? Don't redo the O(n³) part. Do it once and save it. That's LU.
A = L U. U = what elimination turns A into. L = 1s on the diagonal, the multipliers underneath. Then A x = b becomes: solve L z = b (forward), then U x = z (backward). Each is O(n²).
L is assumed to have 1's on the diagonal, to ensure that the factorisation is unique. The process involves division by the diagonal entries u₁₁, u₂₂, etc., so they must be non-zero. … the matrix U is the same as the fully eliminated upper triangular form produced by Gaussian elimination; L contains the multipliers that were used at each stage to eliminate the rows.
7. Pivoting
Two reasons to swap rows: the pivot is 0 (you can't divide), or the pivot is tiny (dividing by it blows rounding errors up).
- Before clearing column j, look at the diagonal entry and everything under it.Before eliminating entries in column j: find the entry in column j, below the diagonal, of maximum magnitude;
- If a lower one is bigger (ignoring sign), swap its row up.if that entry is larger in magnitude than the diagonal entry then swap its row with row j.
- Then clear the column as usual.Then eliminate column j as before.
Why it works: the pivot is the biggest, so every multiplier is between −1 and 1. Errors can't get amplified. (For LU with pivoting: every |lij| ≤ 1.)
See the tiny-pivot problem for real (Example 4.7)
System: εx₁ + x₂ = 2 + ε, x₁ + x₂ = 3. The answer is always (1, 2). Without a swap you divide by ε:
8. Iterative methods: Jacobi and Gauss–Seidel Ch 5
Elimination is O(n³) work and O(n²) storage. For millions of unknowns that's too much. Instead: guess, improve, repeat until it's good enough.
Every method here has the form new x = old x + P (b − A x). The bit in brackets is the residual: how far A x is from b. Pick P cheap to apply, and close enough to A−1 that it converges fast.
F(x(k)) = x(k) + P(b − Ax(k)) for some matrix P. We call r(k) = b − Ax(k) the residual. … Jacobi method where we take P = D−1 where D is the diagonal of A. … [Gauss–Seidel] we use P = (D + L)−1.
Exam question, from scratch
Exercise 5.1: "Take one iteration of (a) Jacobi iteration and (b) Gauss–Seidel iteration to approximate the solution of the following system using the initial guess x(0) = (0, 1, 1)T." The top half shows which numbers you're using. The bottom half is what you write. Step through it, then cover the screen and do it yourself on paper.
| Jacobi | Gauss–Seidel | |
|---|---|---|
| P | D−1 (just the diagonal) | (D + L)−1 (diagonal + below) |
| Uses | only old values x(k) | new values as soon as they're computed |
| Speed | slower | generally faster |
| Cost per step | O(n²) full matrix, O(n) sparse | |
Gauss–Seidel, component by component: xi(k+1) = xi(k) + (1/Aii)(bi − Σj<i Aijxj(k+1) − Σj≥i Aijxj(k)). Jacobi is the same with every x(k) on the right.
When to stop
- A maximum number of iterations (always, so it can't loop forever).
- Change is small: ‖x(k+1) − x(k)‖ < tol.
- Residual is small: ‖b − A x(k)‖ < tol.
‖y‖ = √(y₁² + … + yn²), the normal length. Residual ≠ error. The error x − x(k) needs the true answer, which you don't have. The residual you can always compute. They're linked: r(k) = A(x − x(k)).
The residual r(k) is different to the error x − x(k). The error cannot be computed in general since it requires knowledge of the exact solution x. The residual can be computed and is therefore often used to monitor convergence.
Sparse storage (COO): three arrays, the non-zero values, their row numbers, their column numbers. That's 3αn numbers, O(n), instead of n².
9. Complex numbers Ch 6
x² + 1 = 0 has no real solution. Invent i with i² = −1, and suddenly every polynomial has roots. That matters because eigenvalues are polynomial roots (rotations have complex ones).
z = a + bi: a is the real part, b is the imaginary part. Think of it as the point (a, b).
Definition 6.1. Any number that can be written as z = a + bi, with a, b real numbers and i the imaginary unit, is called a complex number. In this format, we call a the real part of z and b the imaginary part of z.
| add | (a + bi) + (c + di) = (a + c) + (b + d)i |
| multiply | (a + bi)(c + di) = (ac − bd) + (ad + bc)i (expand, then i² = −1) |
| conjugate | z̄ = a − bi |
| modulus | |z| = √(a² + b²) = √(z z̄) |
| divide | multiply top and bottom by the conjugate of the bottom |
| polar | z = R(cos θ + i sin θ) = R eiθ, R = |z|, θ = arg z |
| powers | zp = Rp eipθ · Euler: eiπ = −1 |
Fundamental Theorem of Algebra: every polynomial (degree ≥ 1) has a complex root. Not true if you only allow real numbers.
Theorem 6.1. For any complex numbers a₀, …, an with an ≠ 0, there is at least one complex number z which satisfies: anzn + ⋯ + a₁z + a₀ = 0.
Complex vectors and matrices
Hermitian product ⟨a, b⟩ = Σ ai b̄i: the dot product, but conjugate the second vector. Then ⟨z, z⟩ = Σ|zi|², so ‖z‖ = √⟨z, z⟩ is a real length.
Conjugate transpose AH: transpose and conjugate every entry. If AH = A, A is Hermitian (the complex version of symmetric).
Definition 6.3. A complex matrix is a matrix whose entries are complex numbers. A complex vector is a vector whose entries are complex numbers. Definition 6.4. For a complex n-matrix A, we write AH for the conjugate transpose (also called the Hermitian transpose) given by (AH)ij = Āji = ĀT. If AH = A, then we say that A is Hermitian.
10. Eigenvalues and eigenvectors Ch 7
Most vectors get turned when you multiply by A. An eigenvector doesn't turn: A just stretches it (or flips it, or squashes it). The stretch factor is the eigenvalue λ.
Definition 7.1. We say that any non-zero vector x is an eigenvector of A if Ax is parallel to x. Here by parallel, we mean that there exists a number λ (which can be positive, negative or zero, real or complex) such that Ax = λx. We call the associated number λ an eigenvalue of A.
Finding them by hand
- Move everything to one side: (A − λI) x = 0.(A − λIn)x = 0.
- A non-zero x can only get sent to 0 if A − λI is singular, i.e. its det is 0.det(A − λIn) = 0. … We call (7.2) the characteristic equation or eigenvalue equation.
- That's a polynomial in λ (degree n). Solve it: those are the eigenvalues.We will see that (7.2) gives us a degree n polynomial equation in λ.
- For each λ, find a non-zero x with (A − λI)x = 0. In this module you can spot it by eye.All the examples of finding eigenvectors we will see, you will be able to solve simply by inspection.
Example 7.3: A = (3 1; 1 3). det(A − λI) = (3 − λ)² − 1 = λ² − 6λ + 8 = (λ − 4)(λ − 2). So λ = 4 with x = (1, 1), and λ = 2 with x = (−1, 1). Pick that preset above and watch.
- Sum of eigenvalues = trace (sum of the diagonal). Product = det A.
- 0 is an eigenvalue ⇔ A is singular.
- Triangular matrix: the eigenvalues are just the diagonal entries.
- Any multiple of an eigenvector is an eigenvector too, so normalise to get a unique answer.
- Watch out: rotations give complex λ (Example 7.4); (3 1; 0 3) has only one eigenvector direction (Example 7.5).
Theorem 7.1: every n × n matrix has n complex eigenvalues (some may repeat). Reason: det(A − λI) is a degree-n polynomial, and the Fundamental Theorem of Algebra gives it n roots. But there's no formula for the roots once the degree is 5 or more (Abel–Ruffini), so big matrices need an algorithm: QR.
Similar matrices
A and B are similar if B = P−1AP for some invertible P. Think: the same transformation, described in different coordinates. So they have the same eigenvalues.
Definition 7.2. We say that two matrices A and B are similar if there exists an invertible n × n matrix P such that PB = AP.
Theorem 7.2. If A and B are similar matrices then A and B have the same eigenvalues.
Plan: take any eigenvector of B and show that P times it is an eigenvector of A, with the same λ.
- Start with an eigenvector x of B, eigenvalue λ. Set y = P x.Let x be an eigenvector of B with associated eigenvalue λ and y = Px.
- A y = A P x, just by what y is.Ay = APx (definition of y)
- AP = PB, because they're similar.= PBx (since A and B are similar)
- B x = λx, because x is an eigenvector of B.= Pλx (since x is an eigenvector of B)
- Pull the number λ out front.= λ(Px) (rearranging) = λy.
- So λ is an eigenvalue of A too. For the other direction, run the same argument with A and B swapped and P−1 instead of P.This shows that any eigenvalue of B is an eigenvalue of A. … To show any eigenvalue of A is an eigenvalue of B, we repeat the calculation with A and B swapped and replace P by P−1.
The converse is false: same eigenvalues doesn't mean similar. (3 1; 0 3) and (3 0; 0 3) both have λ = 3, 3, but they're not similar (Example 7.8).
Symmetric matrices (real, AT = A): all eigenvalues are real, and eigenvectors for different eigenvalues are at right angles (orthogonal: ⟨a, b⟩ = 0). The proof is "not essential" per the notes.
Theorem 7.3. Let A be a symmetric matrix (AT = A) with real entries. Then A has n real eigenvalues (zero imaginary part) and any eigenvectors with distinct eigenvalues are orthogonal.
11. QR: eigenvalues for big matrices Ch 8
Orthogonal matrix Q: columns are unit length and at right angles to each other. Then QTQ = I, so Q−1 = QT (a free inverse). det Q = ±1. Rotations are orthogonal. They don't add rounding error.
A (real-valued) matrix Q is orthogonal if QTQ = In.
The key move: remove the shadow
Given a unit vector q, the part of a pointing along q is (a · q) q. Subtract it, and what's left (u) is at right angles to q.
Gram–Schmidt: A = Q R
For each column a(j): subtract its shadow on every earlier q, then scale to length 1.
u(j) = a(j) − Σi=1j−1 (a(j) · q(i)) q(i), q(j) = u(j) / ‖u(j)‖. Rij = a(j) · q(i) if i < j; ‖u(j)‖ if i = j; 0 if i > j.
The QR algorithm
- Start with A(0) = A.First, we set A(0) = A, then we successively perform for k = 0, 1, 2, …:
- Split it: A(k) = Q R.Compute the QR factorisation of A(k) into an orthogonal part and upper triangular part A(k) = Q(k)R(k);
- Put it back the other way: A(k+1) = R Q.Update the matrix A(k+1) by recombining Q and R in the reverse order: A(k+1) = R(k)Q(k).
Why the eigenvalues don't change: R = QTA, so RQ = QTAQ = Q−1AQ. That's similar to A. Quick check: the trace (sum of eigenvalues) stays the same every step.
12. The five algorithms you must state and apply
From the notes: "we expect that you can state and apply for the following algorithms in an exam setting". Write each one from memory in the box, then open the answer and compare.
1. Gaussian elimination (without pivoting) → animation
answer
For each column i = 1, …, n − 1: for each row j below it, (row j) ← (row j) − (aji/aii) (row i), applied to b too. Result: upper triangular. Then backward substitution: xn first, xi = (bi − Σj>i aijxj) / aii.2. Gaussian elimination with pivoting → animation
answer
Same, but before eliminating column j: find the entry of maximum magnitude in column j on or below the diagonal; if it's larger than the diagonal entry, swap its row with row j (and swap b). Then eliminate. Multipliers are then all ≤ 1 in size.3. Jacobi iteration → animation
answer
x(k+1) = x(k) + D−1(b − Ax(k)), D = diagonal of A. Componentwise: xi(k+1) = xi(k) + (1/Aii)(bi − Σj Aijxj(k)). Equivalently x(k+1) = D−1(b − (L + U)x(k)). Stop on max iterations, small change, or small residual.4. Gauss–Seidel iteration → animation
answer
x(k+1) = x(k) + (D + L)−1(b − Ax(k)). Componentwise: xi(k+1) = xi(k) + (1/Aii)(bi − Σj<i Aijxj(k+1) − Σj≥i Aijxj(k)): use each new value as soon as it's computed.5. QR factorisation through Gram–Schmidt → animation
answer
For j = 1, …, n: u(j) = a(j) − Σi<j (a(j)·q(i)) q(i); q(j) = u(j)/‖u(j)‖. Q has columns q(j); Rij = a(j)·q(i) (i < j), Rjj = ‖u(j)‖, 0 below. Then A = QR. (QR algorithm: A(k) = Q(k)R(k), A(k+1) = R(k)Q(k).)13. Definitions to know by heart
| Term | Plain | Notes wording |
|---|---|---|
| square matrix | same number of rows and columns | If m = n, then we say that the matrix is square. |
| invertible | there's a matrix that undoes it | There exists a matrix A−1 such that A−1A = In. |
| lower triangular | zeros above the diagonal | Every entry above the leading diagonal is zero: aij = 0 for j > i. |
| sparse | mostly zeros | A sparse n × n has αn nonzero entries, where α ≪ n. |
| residual | how wrong A x is | r(k) = b − Ax(k) |
| Euclidean norm | length | ‖y‖ = √(Σi=1n yi²) |
| eps | gap from 1 to the next float | eps = β1−t = 2u |
| complex number | a + bi | Definition 6.1 (above) |
| polar form | distance + angle | Definition 6.2. The polar form of z is R(cos θ + i sin θ). We call R the modulus of z and θ is an argument of z. |
| Hermitian | equals its conjugate transpose | If AH = A, then we say that A is Hermitian. |
| eigenvector / eigenvalue | A x = λx, x ≠ 0 | Definition 7.1 (above) |
| similar | B = P−1AP | Definition 7.2: PB = AP, P invertible |
| orthogonal vectors | at right angles | Definition 7.3. We say two vectors a and b are orthogonal if ⟨a, b⟩ = 0. |
| orthogonal matrix | columns unit length and at right angles | QTQ = In |