A1331 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 10n3 operations.

  • The stems are log10⁡ 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 |r| 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?

Name it

The iteration

A0=A,Ak=QkRk,Ak+1=RkQk=QkTAkQk

Each Ak+1 is similar to Ak, so the eigenvalues stay the same. For eigenvalues of distinct sizes, Ak 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 |λi+1/λi| 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 103n3 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(n2), or O(n) for tridiagonal.
  2. Shift. Factor Ak−σkI instead and add σ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 10n3 operations; eigenvectors too, about 25n3.

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 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.

Worked example

Do two unshifted QR steps on (2112).

Show the solution
  1. One step on a symmetric 2 × 2. For (abbd) the new entries are a′=a+b2(a+d)a2+b2b′=b(ad−b2)a2+b2 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.

Worked example

A series RLC circuit (10 Ω, 1 mH, 1 µF) has state matrix A=(01/C−1/L−R/L) in (vC,i). Find its poles.

Show the solution
  1. The characteristic equation. λ2+RLλ+1LC=0
  2. Its roots. λ=−5000±j31225s−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 λ=−5000±j31225 s⁻¹: a 4.97 kHz ring that decays with a 0.2 ms time constant.

Worked example

Find the roots of x3−6x2+11x−6 as the eigenvalues of its companion matrix.

Show the solution
  1. The companion matrix. For x3+a2x2+a1x+a0 it is (−a2−a1−a0100010)
  2. Here. (6−116100010)
  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

One unshifted QR step on (3112): what's the new off-diagonal entry?

Check

What's the larger eigenvalue of (4115), 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 10n3 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 10−12 of its start?

Check

The matrix (abcd).

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°.

Explain why

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

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.