---
title: The QR algorithm
heading: How every eigenvalue routine actually works
eyebrow: A13 · Lesson 31 · The QR algorithm
xp: 40
---

::: hook
When you call `numpy.linalg.eig` to get a state-space system's poles or a circuit's natural frequencies, it doesn't find the roots of the characteristic polynomial. That would be badly conditioned (lesson 3's Wilkinson polynomial). It runs the **QR algorithm**: factor the matrix as QR, multiply the factors in the other order, and repeat. Each step is a similarity transformation, so the eigenvalues never change, while the matrix slowly becomes triangular with the eigenvalues on its diagonal. With two refinements, a cheap reduction first and clever shifts, it finds all n eigenvalues in about $10n^3$ operations.
:::

::: play
- The stems are $\log_{10}$ of the off-diagonal entry of a 2 × 2 symmetric matrix after k unshifted QR steps. The eigenvalues are 1 and r; θ sets the starting off-diagonal.
- The entry falls by $\lvert r\rvert$ each step, the ratio of the eigenvalues, exactly like power iteration (and for the same reason). With r = 0.3 it reaches rounding level in about 30 steps; with r = 0.9 it takes hundreds.
- Real implementations use shifts, which make the ratio nearly zero: two or three steps per eigenvalue.
:::
:::

```sim plot
view: graph
x: k = 0..30
y: digits = -17..0
slider: r = -0.95..0.95 step 0.05 = 0.5
slider: t = 5..85 step 1 = 40 °
plot: log(abs((1 - r)*r^k*tan(t*pi/180)/(1 + (r^k*tan(t*pi/180))^2)) + 1e-17) | stems | label: log₁₀ |off-diagonal|
readout: shrink factor per step = abs(r)
readout: steps to rounding level, about = 16/log(1/abs(r))
```

::: predict
## A 2 × 2 symmetric matrix has eigenvalues 3 and 1. By what factor does each unshifted QR step shrink its off-diagonal entry?

- [ ] 1/9
- [x] About 1/3
- [ ] 1/2
- [ ] It doesn't shrink

::: reveal
About $\frac13$, the ratio of the eigenvalues. Starting from $\begin{pmatrix}2 & 1\\ 1 & 2\end{pmatrix}$ the off-diagonal goes 1, 0.6, 0.22, 0.074, 0.025, while the diagonal approaches 3 and 1.
:::
:::

::: nameit
## The iteration

$$A_0 = A, \qquad A_k = Q_kR_k, \qquad A_{k+1} = R_kQ_k = Q_k^TA_kQ_k$$

Each $A_{k+1}$ is similar to $A_k$, so the eigenvalues stay the same. For eigenvalues of distinct sizes, $A_k$ tends to upper triangular (the **Schur form**), with the eigenvalues on the diagonal in decreasing size; for a symmetric matrix it tends to diagonal. The entry below the diagonal in position (i + 1, i) shrinks by $\lvert\lambda_{i+1}/\lambda_i\rvert$ per step: the QR algorithm is power iteration on all the eigenvectors at once.

## What makes it practical

1. **Reduce first.** A one-time similarity transformation (Householder reflections, about $\frac{10}{3}n^3$ operations) makes A upper Hessenberg, zero below the first subdiagonal, or tridiagonal if A is symmetric. QR steps keep that shape and cost only $O(n^2)$, or $O(n)$ for tridiagonal.
2. **Shift.** Factor $A_k - \sigma_kI$ instead and add $\sigma_kI$ back. With σ near an eigenvalue the last subdiagonal entry collapses in two or three steps (Wilkinson's shift gives cubic convergence for symmetric matrices).
3. **Deflate.** When a subdiagonal entry is negligible, split the problem and continue on the smaller pieces.

All eigenvalues of a dense n × n matrix cost about $10n^3$ operations; eigenvectors too, about $25n^3$.

## Don't use the characteristic polynomial

Polynomial roots can be extremely sensitive to the coefficients, which are themselves rounded. Going the other way is fine: `numpy.roots` finds a polynomial's roots as the eigenvalues of its companion matrix, by the QR algorithm.

## In systems

The poles of a state-space system $\dot x = Ax$ are the eigenvalues of A. A network's natural frequencies, a structure's vibration modes and a Markov chain's settling rates are all eigenvalue problems solved this way.
:::

::: example
## Do two unshifted QR steps on $\begin{pmatrix}2 & 1\\ 1 & 2\end{pmatrix}$.

::: solution
1. **One step on a symmetric 2 × 2.** For $\begin{pmatrix}a & b\\ b & d\end{pmatrix}$ the new entries are
   $$\begin{aligned} a' &= a + \frac{b^2(a + d)}{a^2 + b^2} \\ b' &= \frac{b(ad - b^2)}{a^2 + b^2} \end{aligned}$$
   with the trace unchanged.
2. **First step.** From (2, 1, 2) it gives (2.8, 0.6, 1.2).
3. **Second step.** It gives (2.9756, 0.2195, 1.0244), heading for 3 and 1.

**Answer:** (2.8, 0.6, 1.2) and then (2.9756, 0.2195, 1.0244), with the diagonal heading for the eigenvalues 3 and 1.
:::
:::

::: example
## A series RLC circuit (10 Ω, 1 mH, 1 µF) has state matrix $A = \begin{pmatrix}0 & 1/C\\ -1/L & -R/L\end{pmatrix}$ in $(v_C, i)$. Find its poles.

::: solution
1. **The characteristic equation.**
   $$\lambda^2 + \frac RL\lambda + \frac{1}{LC} = 0$$
2. **Its roots.**
   $$\lambda = -5000 \pm j31\,225\ \text{s}^{-1}$$
3. **Read them.** That's a ring at 4.97 kHz decaying with a 0.2 ms time constant.
4. **Larger models.** For a 2 × 2 the formula is fine; for a 200-state model, `numpy.linalg.eigvals(A)` runs QR.

**Answer:** $\lambda = -5000 \pm j31\,225$ s⁻¹: a 4.97 kHz ring that decays with a 0.2 ms time constant.
:::
:::

::: example
## Find the roots of $x^3 - 6x^2 + 11x - 6$ as the eigenvalues of its companion matrix.

::: solution
1. **The companion matrix.** For $x^3 + a_2x^2 + a_1x + a_0$ it is
   $$\begin{pmatrix}-a_2 & -a_1 & -a_0\\ 1 & 0 & 0\\ 0 & 1 & 0\end{pmatrix}$$
2. **Here.**
   $$\begin{pmatrix}6 & -11 & 6\\ 1 & 0 & 0\\ 0 & 1 & 0\end{pmatrix}$$
3. **Its eigenvalues.** Its characteristic polynomial is the original cubic, so its eigenvalues, 1, 2 and 3, are the roots.
4. **The usual direction, backward.** Software avoids characteristic polynomials when finding eigenvalues, but finds polynomial roots by computing eigenvalues. `np.roots` does exactly this, with the QR algorithm on the companion matrix.

**Answer:** the roots are 1, 2 and 3.
:::
:::

::: check
given: a = 2 | 3 | 5; b = 1 | 2; d = 1 | 2
answer: b*(a*d - b^2)/(a^2 + b^2)
tolerance: 0.1%

## One unshifted QR step on $\begin{pmatrix}\given{a} & \given{b}\\ \given{b} & \given{d}\end{pmatrix}$: what's the new off-diagonal entry?

::: right
Right. $\frac{b(ad - b^2)}{a^2 + b^2}$ = {answer}.
:::

::: misses
- [b] One step changes it; it doesn't stay put.
- [any] Not yet. With $c = a/\rho$, $s = b/\rho$, $\rho = \sqrt{a^2 + b^2}$: $R_{22} = (ad - b^2)/\rho$ and the new entry is $R_{22}s$.
:::
:::

::: check
given: a = 2 | 4 | 6; b = 1 | 3; d = 1 | 5
answer: (a + d + sqrt((a - d)^2 + 4*b^2))/2
tolerance: 0.1%

## What's the larger eigenvalue of $\begin{pmatrix}\given{a} & \given{b}\\ \given{b} & \given{d}\end{pmatrix}$, the one QR converges to in the top corner?

::: right
Right. $\frac{a + d + \sqrt{(a - d)^2 + 4b^2}}{2}$ = {answer}.
:::

::: misses
- [a] The diagonal entries aren't the eigenvalues until the off-diagonal is gone.
- [any] Not yet. From the trace and determinant: $\lambda = \frac{T \pm\sqrt{T^2 - 4D}}{2}$.
:::
:::

::: check
given: l1 = 4 | 10; l2 = 1 | 2 | 3
answer: abs(l2/l1)
tolerance: 0.5%

## A matrix's eigenvalues include {l1} and the next smaller, {l2}. By what factor does unshifted QR shrink the subdiagonal entry between them each step?

::: right
Right. $\lvert\lambda_{i+1}/\lambda_i\rvert$ = {answer}.
:::

::: misses
- [abs(l1/l2)] Invert it.
- [any] Not yet. The ratio of the smaller to the larger.
:::
:::

::: check
given: n = 500 | 2000 | 10000; g = 20 | 100
answer: 10*n^3/(g*1e9)
unit: s
tolerance: 1%

## About how long do all the eigenvalues of a dense {n} × {n} matrix take at {g} GFLOP/s (about $10n^3$ operations)?

::: right
Right. {answer} s.
:::

::: misses
- [2*n^3/3/(g*1e9)] That's one LU factorization; eigenvalues cost about 15 times more.
- [any] Not yet. $10n^3$ divided by the rate.
:::
:::

::: check
given: R = 10 | 50; L = 1 | 10; C = 1 | 0.1
answer: -R/(2*L*1e-3)
unit: s^-1
tolerance: 0.5%

## A series RLC circuit has R = {R} Ω, L = {L} mH, C = {C} µF. What's the real part of its poles, the eigenvalues of its state matrix?

::: right
Right. $-\frac{R}{2L}$ = {answer} s⁻¹ (when underdamped).
:::

::: misses
- [-R/(L*1e-3)] The poles' real part is half of $-R/L$.
- [any] Not yet. $\lambda^2 + \frac RL\lambda + \frac{1}{LC} = 0$.
:::
:::

::: check
kind: choice

## Why doesn't eigenvalue software find the roots of the characteristic polynomial?

- [ ] Polynomials can't be stored
- [x] The roots can be extremely sensitive to the coefficients, which are rounded; working on the matrix directly is stable
- [ ] It's too slow for 2 × 2 matrices
- [ ] Characteristic polynomials don't have real coefficients

::: misses
- [1] They can; the problem is accuracy.
- [3] Speed isn't the issue.
- [4] For a real matrix they do.
:::
:::

::: check
kind: parts
given: l1 = 5 | 10; l2 = 4 | 6

## Unshifted QR runs on a matrix whose two largest eigenvalues are {l1} and {l2}.

::: part
answer: (l2/l1)^10
tolerance: 1%

### By what factor has the off-diagonal entry between them shrunk after 10 steps?

::: misses
- [l2/l1] That's per step.
- [any] $(\lambda_2/\lambda_1)^{10}$.
:::
:::

::: part
answer: ceil(ln(1e-12)/ln(l2/l1))
tolerance: 0

### How many steps until it's below $10^{-12}$ of its start?

::: misses
- [any] Solve $(\lambda_2/\lambda_1)^k \le 10^{-12}$.
:::
:::
:::

::: check
kind: derivation
vars: a = 1..4, b = 0.5..2, c = 0.5..2, d = 1..4

## The matrix $\begin{pmatrix}a & b\\ c & d\end{pmatrix}$.

::: step
answer: a + d

### What is its trace?
:::

::: step
answer: a*d - b*c

### What is its determinant?
:::

::: step
answer: (a + d)/2 + sqrt((a - d)^2/4 + b*c)

### What is its larger eigenvalue?
:::

::: right
Right. For 2 × 2 matrices the quadratic is fine; for larger ones, software uses QR instead.
:::
:::

::: explain
## Why does the QR iteration keep the eigenvalues unchanged while driving the matrix toward triangular form?

::: model
Each step factors $A_k = Q_kR_k$ and forms $A_{k+1} = R_kQ_k$. Since $R_k = Q_k^TA_k$, the new matrix is $Q_k^TA_kQ_k$: a similarity transformation by an orthogonal matrix, which never changes eigenvalues. At the same time, the accumulated product of the Q's is what you'd get from running power iteration on a whole set of vectors and keeping them orthogonal. Its first column swings toward the dominant eigenvector, the second toward the next, and so on, at rates set by the ratios of the eigenvalues. In that basis the matrix becomes triangular, with the entries below the diagonal shrinking by those same ratios, until the eigenvalues can be read off the diagonal.
:::
:::

::: practice

::: check
given: a = 4 | 1; b = 1 | 2; d = 2 | 3
answer: b*(a*d - b^2)/(a^2 + b^2)
tolerance: 0.1%

## New off-diagonal after one QR step on ({a}, {b}, {d})?

::: misses
- [any] $\frac{b(ad - b^2)}{a^2 + b^2}$.
:::
:::

::: check
given: a = 3 | 7; b = 2 | 1; d = 0 | 4
answer: (a + d + sqrt((a - d)^2 + 4*b^2))/2
tolerance: 0.1%

## Larger eigenvalue of ({a}, {b}; {b}, {d})?

::: misses
- [any] $\frac{a + d + \sqrt{(a - d)^2 + 4b^2}}{2}$.
:::
:::

::: check
given: n = 800 | 5000; g = 50 | 500
answer: 10*n^3/(g*1e9)
unit: s
tolerance: 1%

## Time for all eigenvalues, n = {n}, {g} GFLOP/s?

::: misses
- [any] $10n^3$ over the rate.
:::
:::
:::
