A13 · Lesson 31 · The QR algorithm
How every eigenvalue routine actually works
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 operations.
- The stems are 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 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.
Predict
A 2 × 2 symmetric matrix has eigenvalues 3 and 1. By what factor does each unshifted QR step shrink its off-diagonal entry?
About , the ratio of the eigenvalues. Starting from the off-diagonal goes 1, 0.6, 0.22, 0.074, 0.025, while the diagonal approaches 3 and 1.
Name it
The iteration
Each is similar to , so the eigenvalues stay the same. For eigenvalues of distinct sizes, 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 per step: the QR algorithm is power iteration on all the eigenvectors at once.
What makes it practical
- Reduce first. A one-time similarity transformation (Householder reflections, about operations) makes A upper Hessenberg, zero below the first subdiagonal, or tridiagonal if A is symmetric. QR steps keep that shape and cost only , or for tridiagonal.
- Shift. Factor instead and add back. With σ near an eigenvalue the last subdiagonal entry collapses in two or three steps (Wilkinson's shift gives cubic convergence for symmetric matrices).
- 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 operations; eigenvectors too, about .
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 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.
Worked example
Do two unshifted QR steps on .
Show the solution
- One step on a symmetric 2 × 2. For the new entries are with the trace unchanged.
- First step. From (2, 1, 2) it gives (2.8, 0.6, 1.2).
- 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.
Worked example
A series RLC circuit (10 Ω, 1 mH, 1 µF) has state matrix in . Find its poles.
Show the solution
- The characteristic equation.
- Its roots.
- Read them. That's a ring at 4.97 kHz decaying with a 0.2 ms time constant.
- Larger models. For a 2 × 2 the formula is fine; for a 200-state model,
numpy.linalg.eigvals(A)runs QR.
Answer s⁻¹: a 4.97 kHz ring that decays with a 0.2 ms time constant.
Worked example
Find the roots of as the eigenvalues of its companion matrix.
Show the solution
- The companion matrix. For it is
- Here.
- Its eigenvalues. Its characteristic polynomial is the original cubic, so its eigenvalues, 1, 2 and 3, are the roots.
- The usual direction, backward. Software avoids characteristic polynomials when finding eigenvalues, but finds polynomial roots by computing eigenvalues.
np.rootsdoes exactly this, with the QR algorithm on the companion matrix.
Answer the roots are 1, 2 and 3.
Check
One unshifted QR step on : what's the new off-diagonal entry?
Check
What's the larger eigenvalue of , the one QR converges to in the top corner?
Check
A matrix's eigenvalues include 4 and the next smaller, 1. By what factor does unshifted QR shrink the subdiagonal entry between them each step?
Check
About how long do all the eigenvalues of a dense 2000 × 2000 matrix take at 20 GFLOP/s (about operations)?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
A series RLC circuit has R = 10 Ω, L = 1 mH, C = 0.1 µF. What's the real part of its poles, the eigenvalues of its state matrix?
Check
Why doesn't eigenvalue software find the roots of the characteristic polynomial?
Check
Unshifted QR runs on a matrix whose two largest eigenvalues are 5 and 4.
(a)
By what factor has the off-diagonal entry between them shrunk after 10 steps?
(b)
How many steps until it's below of its start?
Check
The matrix .
Step 1
What is its trace?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is its determinant?
Step 3
What is its larger eigenvalue?
Explain why
Why does the QR iteration keep the eigenvalues unchanged while driving the matrix toward triangular form?
Each step factors and forms . Since , the new matrix is : 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
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.