mathlinear algebra

eigenvectors

the directions a matrix only stretches, and by how much.

report a mistake
.md

a matrix moves every vector, and most of them change direction. an eigenvector doesn't. the matrix only stretches it, and the stretch is its eigenvalue. find the eigenvalues from det(A − λI) = 0, the directions from (A − λI)v = 0, and apply the matrix again and again to see the biggest one take over.

the equation
Av = λv A only stretches v, by λ. v is not the zero vector
finding λ
det(A − λI) = 0 for a 2×2, λ² − (trace)λ + det = 0
finding v
(A − λI)v = 0 one row of the equation is enough
trace and det
λ₁ + λ₂ and λ₁λ₂ the sum and the product of the eigenvalues
powers
Aⁿv = λⁿv the biggest |λ| takes over. that's power iteration
a rotation
no real eigenvectors every line turns off itself, unless it's a half turn
a shear
one direction λ repeated, and one line keeps its direction
reach for it
  • the long-run shares of a random walk, where each step is a matrix
  • the axes a symmetric matrix stretches along, and by how much
  • how fast a repeated step settles, by |λ₂/λ₁| a step
  • Aⁿ without n multiplications, through A = PDP⁻¹
on this page
  1. the idea
  2. explore
  3. try it
  4. the rules
  5. worked examples
  6. the key idea up close
  7. in code
  8. pitfalls
  9. practice
  10. sources

the idea

a matrix is a rule that moves every vector, as the matrices page shows. take

A=(2112)A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}

and feed it v=(1,0)v = (1, 0): out comes Av=(2,1)Av = (2, 1), a vector pointing somewhere else. feed it (0,1)(0, 1) and get (1,2)(1, 2). most vectors change direction when AA acts on them. a few don’t. A(1,1)=(3,3)A(1, 1) = (3, 3), which is 3⋅(1,1)3 \cdot (1, 1): the same direction, three times as long. and A(1,−1)=(1,−1)A(1, -1) = (1, -1), untouched. those two are the eigenvectors of AA, and the stretches, 3 and 1, are its eigenvalues. the movie sweeps a vector around the circle and watches for the moments when vv and AvAv line up.

−3−2−10123−3−2−10123Avv
v=(1,0)v = (1, 0) in cyan, and Av=(2,1)Av = (2, 1) in amber. the matrix turned it: AvAv points 26.6°26.6° away from vv. most vectors do this. the question is which ones don’t.

an animation on a plane from −3.5 to 3.5 both ways: a unit vector v in cyan sweeps around the unit circle while Av in amber sweeps with it, for the matrix with rows (2, 1) and (1, 2). at 0°, v = (1, 0) and Av = (2, 1), turned 26.6°. at 45° the two line up, Av = 3v, and a dashed eigenline is drawn through the origin labeled λ = 3. between the lines Av turns by as much as 30°. at 135°, v = (−1, 1)/√2 and Av = v: a second eigenline, λ = 1. the rest of the turn finds nothing new, and Av's amber trail is an ellipse whose axes sit on the two eigenlines, reaching 3 from the center one way and 1 the other.

the definition in one line: vv is an eigenvector of AA when Av=λvAv = \lambda v for some number λ\lambda, with v≠0v \ne 0. the eigenvalue λ\lambda says what happens along that line. λ>1\lambda > 1 stretches, 0<λ<10 < \lambda < 1 shrinks, λ<0\lambda < 0 flips the vector to the other side of the origin, and λ=0\lambda = 0 squashes it to the origin. the zero vector is left out because A0=λ0A0 = \lambda 0 holds for every λ\lambda, and a line needs a direction.

explore

set the matrix with the sliders, or pick one of the four presets, and drag the tip of vv. AvAv follows in amber, and the dashed lines are the eigenlines with their λ\lambda. put vv on one and the angle between vv and AvAv drops to 0°0°, or reaches 180°180° where λ\lambda is negative.

A =2112
−4−2024−4−3−2−101234λ = 3λ = 1Avv

drag the tip of v, or tab to it and use the arrow keys: 0.5 a step

v
(1, 0.5)
Av
(2.5, 2)
the angle from v to Av
12.1°
eigenvalues
3 and 1
eigen-directions
along (1, 1) and (1, −1)
trace and det
4 and 3

two eigenlines, perpendicular: along (1, 1) A stretches it by 3, and along (1, −1) it leaves it alone

an interactive plane from −4 to 4 both ways. buttons pick a preset matrix, diagonal, symmetric, shear or rotation, and four sliders set its entries a, b, c, d from −3 to 3 in steps of 0.5. a point drags the tip of v, in cyan, on a grid of halves inside ±2, or moves with the arrow keys; Av is drawn in amber, and the eigenlines dashed with their λ. a readout gives v, Av, the angle between them, the eigenvalues, the eigen-directions, and the trace and determinant. it opens on the symmetric matrix with rows (2, 1) and (1, 2) and v = (1, 0.5): Av = (2.5, 2), turned 12.1°, eigenvalues 3 and 1 along (1, 1) and (1, −1), trace 4 and det 3. the diagonal preset, rows (2, 0) and (0, 0.5), sends (1, 0.5) to (2, 0.25), turned 19.4°, with the axes as eigenlines. the shear, rows (1, 1) and (0, 1), sends it to (1.5, 0.5), turned 8.1°, with one eigenline along (1, 0) and λ = 1 twice. the rotation, rows (0, −1) and (1, 0), sends it to (−0.5, 1), turned 90°, and the readout says there are no real eigenvectors.

the eigenlines are where v and Av agree. a rotation has none, a shear has one, and the other two have a pair.

try it

A has rows (2, 1) and (1, 2). is v = (1, 1) an eigenvector of A?

A =2112

pick one.

0 / 0 right

a practice exercise with four kinds of question: whether a given vector is an eigenvector of a 2×2 matrix and with what λ, the eigenvalues of a small matrix, which direction power iteration ends up pointing along, and the trace or determinant from the eigenvalues. the matrix is shown as a grid, and once there is a pick an eigenvector question draws v and Av. it opens on whether (1, 1) is an eigenvector of the matrix with rows (2, 1) and (1, 2), which it is, with λ = 3, then on the eigenvalues of the matrix with rows (3, 1) and (0, 2), which are 3 and 2.

the rules

the definition

Av=λv,v≠0Av = \lambda v, \qquad v \ne 0

AA sends vv to a multiple of itself. the multiple is the eigenvalue. any nonzero multiple of vv is an eigenvector with the same λ\lambda, since A(cv)=c Av=λ (cv)A(cv) = c\,Av = \lambda\,(cv), so an eigenvector is a direction, a line through the origin, more than one vector. the page writes one representative, like (1,1)(1, 1), and means the whole line.

finding λ

det⁡(A−λI)=0λ2−(a+d) λ+(ad−bc)=0\begin{gathered} \det(A - \lambda I) = 0 \\[6pt] \lambda^2 - (a + d)\,\lambda + (ad - bc) = 0 \end{gathered}

move everything in Av=λvAv = \lambda v to one side: (A−λI)v=0(A - \lambda I)v = 0. that matrix sends a nonzero vector to zero, so it squashes a direction flat, so it has no inverse, so its determinant is 0. the key idea below walks through it. for a 2×22 \times 2 with entries a,b,c,da, b, c, d, the determinant is (a−λ)(d−λ)−bc(a - \lambda)(d - \lambda) - bc, which is the quadratic above: λ2\lambda^2 minus the trace times λ\lambda plus the determinant. its two roots are the eigenvalues, and the discriminant (a+d)2−4(ad−bc)(a + d)^2 - 4(ad - bc) decides how many are real: two when it’s positive, one repeated when it’s 0, none when it’s negative.

finding v

(A−λI) v=0(A - \lambda I)\,v = 0

with λ\lambda in hand, the directions come from the matrix A−λIA - \lambda I. its determinant is 0, so its two rows are multiples of each other, and either one that isn’t all zeros is enough. the row (p,q)(p, q) asks for px+qy=0px + qy = 0, and (q,−p)(q, -p) answers it. for the movie’s matrix at λ=3\lambda = 3, A−3IA - 3I has rows (−1,1)(-1, 1) and (1,−1)(1, -1): −x+y=0-x + y = 0, so v=(1,1)v = (1, 1).

trace and determinant

λ1+λ2=a+dλ1λ2=ad−bc\begin{gathered} \lambda_1 + \lambda_2 = a + d \\[4pt] \lambda_1 \lambda_2 = ad - bc \end{gathered}

multiply out (λ−λ1)(λ−λ2)(\lambda - \lambda_1)(\lambda - \lambda_2) and it’s λ2−(λ1+λ2)λ+λ1λ2\lambda^2 - (\lambda_1 + \lambda_2)\lambda + \lambda_1 \lambda_2, the same quadratic, since it has the same roots. match the coefficients: the trace is the sum of the eigenvalues and the determinant is their product, and both are a check on a hand computation. the movie’s matrix has trace 4 and determinant 3, and 3+1=43 + 1 = 4, 3⋅1=33 \cdot 1 = 3. the determinant as a product says something on its own: the matrix scales areas by λ1λ2\lambda_1 \lambda_2, which is what stretching by λ1\lambda_1 one way and λ2\lambda_2 the other does to a square.

the four cases

a diagonal matrix stretches the axes, so the axes are its eigenlines and the diagonal entries are its eigenvalues. a symmetric matrix, one with b=cb = c, has real eigenvalues always and perpendicular eigenlines: the discriminant is (a−d)2+4b2(a - d)^2 + 4b^2, which can’t be negative, and the key idea below does the perpendicular part. a shear, (x,y)↦(x+y,y)(x, y) \mapsto (x + y, y), slides every point sideways by its height, so the xx axis is the only line that stays put, and λ=1\lambda = 1 comes out twice with one direction to show for it. a rotation turns every direction off its line, so there are no real eigenvectors, unless it’s a half turn, which sends every vv to −v-v: λ=−1\lambda = -1 on every line.

diagonal2000.5
λ = 2λ = 0.5

the axes are the eigenlines, and the diagonal entries are the eigenvalues: 2 and 0.5

symmetric2112
λ = 3λ = 1

two perpendicular eigenlines, and the eigenvalues are real: 3 along (1, 1) and 1 along (1, −1)

shear1101
λ = 1

one eigenline, the x axis, with λ = 1 counted twice. every other direction leans over

rotation0−110
no eigenlines

no real eigenvectors: every direction turns a quarter turn, so no line lands on itself

four small panels, each with the unit circle in gray, where the matrix sends it as a dotted amber curve, the eigenlines dashed, and each unit eigenvector in cyan with where the matrix sends it in amber. diagonal, rows (2, 0) and (0, 0.5): the circle becomes an ellipse reaching 2 along the x axis and 0.5 along y, eigenlines along the axes with λ = 2 and λ = 0.5. symmetric, rows (2, 1) and (1, 2): an ellipse tilted 45°, eigenlines along (1, 1) with λ = 3 and (1, −1) with λ = 1. shear, rows (1, 1) and (0, 1): a leaning ellipse, one eigenline along (1, 0) with λ = 1 repeated. rotation, rows (0, −1) and (1, 0): the circle maps onto itself, and there are no eigenlines.

the dotted amber curve is where the matrix sends the unit circle. along an eigenline, a point of the circle lands on its own line, λ times as far out.

powers

Anv=λnvA^n v = \lambda^n v

if AA stretches vv by λ\lambda, applying it nn times stretches it by λn\lambda^n. that makes eigenvectors the way to understand a matrix applied over and over. write any start vv as a mix of the two eigenvectors, v=c1v1+c2v2v = c_1 v_1 + c_2 v_2, and then

Anv=c1λ1nv1+c2λ2nv2A^n v = c_1 \lambda_1^n v_1 + c_2 \lambda_2^n v_2

each part grows or shrinks on its own. if ∣λ1∣>∣λ2∣|\lambda_1| > |\lambda_2|, the first part wins: the ratio of the second to the first is (λ2/λ1)n(\lambda_2 / \lambda_1)^n times what it was, so it shrinks by ∣λ2/λ1∣|\lambda_2 / \lambda_1| every step, and AnvA^n v turns toward v1v_1. that’s power iteration: apply AA, scale the result back to length 1 so the numbers stay in range, and repeat. the angle left between the vector and the eigenline shrinks by about ∣λ2/λ1∣|\lambda_2 / \lambda_1| a step, and the rayleigh quotient v⋅Av/v⋅vv \cdot Av / v \cdot v closes in on λ1\lambda_1. a ratio near 0 settles in a few steps. a ratio near 1 takes many.

A =2112
−1.5−1−0.500.511.5−1.5−1−0.500.511.5λ = 3λ = 1v₀

drag the tip of v₀, or tab to it and use the arrow keys: 0.1 a step

v₀, scaled to length 1
(0.8944, 0.4472)
rayleigh quotient, the guess at λ
2.8000
angle to the dominant eigenline
18.435°
this angle over the last one
–
|λ₂/λ₁|
0.333

|λ₂/λ₁| = 1/3: the angle left shrinks to a third each step

an interactive unit circle from −1.5 to 1.5 both ways. buttons pick a matrix: λ = 3 and 1, rows (2, 1) and (1, 2); λ = 1.2 and 1, rows (1.1, 0.1) and (0.1, 1.1); λ = 1 and 0.4, the two-room walk with rows (0.9, 0.5) and (0.1, 0.5); and λ = 1 and −1, rows (1, 0) and (0, −1). a point drags the start vector v₀, in cyan, on a grid of tenths, or moves with the arrow keys; a slider or a step button applies the matrix up to 12 times, scaling to length 1 each time, and the vector after n steps is drawn in white with the earlier steps as gray dots on the circle. the eigenlines are dashed with their λ. a readout gives the vector, the rayleigh quotient, the angle to the dominant eigenline, that angle over the last one, and |λ₂/λ₁|. it opens on the first matrix with v₀ = (1, 0.5), 18.4° from the line y = x. one step turns it to (0.7809, 0.6247), 6.3° away, with the quotient 2.9756; then 2.1°, 0.7°, 0.2°, each about a third of the last, and after 12 steps the vector reads (0.7071, 0.7071) with λ = 3.0000. the slow matrix is still 2.1° away after 12 steps, closing by 0.83 a step. the walk settles on (0.9806, 0.1961), the line through (5, 1), with λ = 1 and the angle shrinking by 0.4 a step. the tie bounces between (0.894, 0.447) and (0.894, −0.447) forever.

drag v₀ onto the line y = −x for the first matrix: a start with nothing along the winning direction never turns toward it.

diagonalization

A=PDP−1,An=PDnP−1A = P D P^{-1}, \qquad A^n = P D^n P^{-1}

put the eigenvectors in the columns of PP and the eigenvalues on the diagonal of DD. then AP=PDAP = PD, since AA times each column is λ\lambda times that column, and when the eigenvectors span the plane PP has an inverse and A=PDP−1A = PDP^{-1}. the payoff is the powers: A2=PDP−1PDP−1=PD2P−1A^2 = PDP^{-1}PDP^{-1} = PD^2P^{-1}, the inner P−1PP^{-1}P cancels every time, and An=PDnP−1A^n = PD^nP^{-1} with DnD^n the diagonal matrix of λn\lambda^n. the worked example below gets A10A^{10} this way, from 3103^{10}, without multiplying AA by itself. it needs two independent eigenvectors: a shear has one, and can’t be diagonalized.

worked examples

a symmetric matrix by hand

AA with rows (2,1)(2, 1) and (1,2)(1, 2), the movie’s matrix. trace 4, determinant 3:

λ2−4λ+3=0(λ−3)(λ−1)=0\begin{gathered} \lambda^2 - 4\lambda + 3 = 0 \\[4pt] (\lambda - 3)(\lambda - 1) = 0 \end{gathered}

so λ=3\lambda = 3 and λ=1\lambda = 1. for λ=3\lambda = 3, A−3IA - 3I has rows (−1,1)(-1, 1) and (1,−1)(1, -1), both saying y=xy = x: v1=(1,1)v_1 = (1, 1). for λ=1\lambda = 1, A−IA - I has rows (1,1)(1, 1) and (1,1)(1, 1), saying y=−xy = -x: v2=(1,−1)v_2 = (1, -1). check: A(1,1)=(3,3)A(1, 1) = (3, 3) and A(1,−1)=(1,−1)A(1, -1) = (1, -1). the two directions are perpendicular, (1,1)⋅(1,−1)=0(1, 1) \cdot (1, -1) = 0, as they always are for a symmetric matrix.

one that isn’t symmetric

rows (4,1)(4, 1) and (2,3)(2, 3). trace 7, determinant 12−2=1012 - 2 = 10, so λ2−7λ+10=(λ−5)(λ−2)\lambda^2 - 7\lambda + 10 = (\lambda - 5)(\lambda - 2) and the eigenvalues are 5 and 2. for λ=5\lambda = 5, A−5IA - 5I has top row (−1,1)(-1, 1): v1=(1,1)v_1 = (1, 1). for λ=2\lambda = 2, A−2IA - 2I has top row (2,1)(2, 1): 2x+y=02x + y = 0, so v2=(1,−2)v_2 = (1, -2). check: A(1,−2)=(4−2,2−6)=(2,−4)=2 (1,−2)A(1, -2) = (4 - 2, 2 - 6) = (2, -4) = 2\,(1, -2). the two lines aren’t perpendicular this time, (1,1)⋅(1,−2)=−1(1, 1) \cdot (1, -2) = -1, and nothing says they should be.

a shear

rows (1,1)(1, 1) and (0,1)(0, 1). trace 2, determinant 1: λ2−2λ+1=(λ−1)2\lambda^2 - 2\lambda + 1 = (\lambda - 1)^2, so λ=1\lambda = 1, twice. A−IA - I has rows (0,1)(0, 1) and (0,0)(0, 0), saying only y=0y = 0: the one direction is (1,0)(1, 0). there’s no second one. the repeated root promised two eigenvalues and delivered one line, and that is what a shear looks like: the xx axis holds still and everything above it leans.

a rotation

rows (0,−1)(0, -1) and (1,0)(1, 0), a quarter turn counter-clockwise. trace 0, determinant 1: λ2+1=0\lambda^2 + 1 = 0, which has no real roots. no vector in the plane comes back on its own line after a quarter turn. the eigenvalues are ii and −i-i, complex numbers, which this page leaves out. among real vectors the answer is: none.

a two-room walk

a walker is in one of two rooms. each minute, one in room 1 stays with probability 0.9 and moves with 0.1; one in room 2 stays with 0.5 and moves with 0.5. write the shares in each room as a column (p1,p2)(p_1, p_2). one minute later the shares are P(p1,p2)P(p_1, p_2) with

P=(0.90.50.10.5)P = \begin{pmatrix} 0.9 & 0.5 \\ 0.1 & 0.5 \end{pmatrix}

trace 1.4, determinant 0.4: λ2−1.4λ+0.4=(λ−1)(λ−0.4)\lambda^2 - 1.4\lambda + 0.4 = (\lambda - 1)(\lambda - 0.4), so the eigenvalues are 1 and 0.4. the eigenvector for λ=1\lambda = 1 is the state nothing changes: P−IP - I has top row (−0.1,0.5)(-0.1, 0.5), so p1=5p2p_1 = 5 p_2, and with the shares adding to 1 that’s (5/6,1/6)(5/6, 1/6). start everyone in room 1, (1,0)(1, 0), and watch: (0.9,0.1)(0.9, 0.1), then (0.86,0.14)(0.86, 0.14), then (0.844,0.156)(0.844, 0.156). the gap to 5/65/6 goes 1/61/6, 1/151/15, 2/752/75, each exactly 0.4 times the one before, because the start is (5/6,1/6)(5/6, 1/6) plus a multiple of the other eigenvector (1,−1)(1, -1), and that part shrinks by λ2=0.4\lambda_2 = 0.4 every minute. the long-run share is the λ=1\lambda = 1 eigenvector, and ∣λ2∣|\lambda_2| says how fast it’s reached. the same idea runs a walk on any number of rooms.

A¹⁰ without ten multiplications

for the movie’s matrix, PP has columns (1,1)(1, 1) and (1,−1)(1, -1), DD has 3 and 1 on the diagonal, and P−1=12PP^{-1} = \tfrac{1}{2} P, since P2=2IP^2 = 2I. then

An=PDnP−1=12(3n+13n−13n−13n+1)\begin{aligned} A^n &= P D^n P^{-1} \\[4pt] &= \frac{1}{2} \begin{pmatrix} 3^n + 1 & 3^n - 1 \\ 3^n - 1 & 3^n + 1 \end{pmatrix} \end{aligned}

n=1n = 1 gives back rows (2,1)(2, 1) and (1,2)(1, 2). n=5n = 5 gives 122122 and 121121, and n=10n = 10 gives 29,52529{,}525 on the diagonal and 29,52429{,}524 off it. the two entries are within 1 of each other because the (1,−1)(1, -1) part never grows: A10A^{10} is 3103^{10} times the matrix that projects onto (1,1)(1, 1), off by 12\tfrac{1}{2} in each entry.

the key idea up close

why det(A − λI) = 0

start from the definition and move λv\lambda v across:

Av=λvAv−λIv=0(A−λI) v=0\begin{gathered} Av = \lambda v \\[4pt] Av - \lambda I v = 0 \\[4pt] (A - \lambda I)\,v = 0 \end{gathered}

the II is there because A−λA - \lambda means nothing: a matrix minus a number. λI\lambda I is the matrix that scales everything by λ\lambda. so M=A−λIM = A - \lambda I is a matrix that sends the nonzero vector vv to the origin. a matrix that does that can’t have an inverse: if M−1M^{-1} existed, then v=M−1Mv=M−10=0v = M^{-1} M v = M^{-1} 0 = 0, and vv isn’t 0. and a matrix with no inverse has determinant 0. the determinant is the factor by which the matrix scales area. MM squashes the whole line through vv onto one point, so a square with a side along vv becomes a segment, with area 0, and the factor is 0. the linear systems page has the same fact from the other side: a system Mv=0Mv = 0 with a nonzero answer is one whose matrix has determinant 0.

that turns a question about vectors into one about a number. for a 2×22 \times 2,

det⁡(a−λbcd−λ)=(a−λ)(d−λ)−bc=λ2−(a+d)λ+(ad−bc)\begin{gathered} \det \begin{pmatrix} a - \lambda & b \\ c & d - \lambda \end{pmatrix} \\[6pt] = (a - \lambda)(d - \lambda) - bc \\[4pt] = \lambda^2 - (a + d)\lambda + (ad - bc) \end{gathered}

a quadratic in λ\lambda, with the trace and the determinant as its coefficients. its discriminant is

(a+d)2−4(ad−bc)=(a−d)2+4bc\begin{gathered} (a + d)^2 - 4(ad - bc) \\[4pt] = (a - d)^2 + 4bc \end{gathered}

which says when the eigenvalues are real. with b=cb = c, the last term is 4b2≥04b^2 \ge 0 and the whole thing can’t be negative: every symmetric matrix has real eigenvalues. with bcbc negative enough it goes under 0 and there are none. the rotation has a=d=0a = d = 0 and bc=−1bc = -1, discriminant −4-4. the shear has a=d=1a = d = 1 and bc=0bc = 0, discriminant 0, the repeated root.

why a symmetric matrix’s eigenlines are perpendicular

let Av1=λ1v1Av_1 = \lambda_1 v_1 and Av2=λ2v2Av_2 = \lambda_2 v_2 with λ1≠λ2\lambda_1 \ne \lambda_2. for a symmetric AA, u⋅Aw=Au⋅wu \cdot Aw = Au \cdot w for any uu and ww: the dot product doesn’t care which side the matrix sits on. then

λ2 (v1⋅v2)=v1⋅Av2=Av1⋅v2=λ1 (v1⋅v2)\begin{aligned} \lambda_2\,(v_1 \cdot v_2) &= v_1 \cdot Av_2 \\[4pt] &= Av_1 \cdot v_2 \\[4pt] &= \lambda_1\,(v_1 \cdot v_2) \end{aligned}

so (λ1−λ2)(v1⋅v2)=0(\lambda_1 - \lambda_2)(v_1 \cdot v_2) = 0, and since the eigenvalues differ, v1⋅v2=0v_1 \cdot v_2 = 0: perpendicular. that’s why the symmetric cases on this page come out with their eigenlines at right angles, and why a symmetric matrix is a pure stretch along two perpendicular axes, the picture the movie draws.

in code

python/math/power_iteration.py runs power iteration on a 2×22 \times 2: apply the matrix, scale back to length 1, repeat, with the rayleigh quotient as the guess at λ\lambda and the angle to the eigenline as the error. the run starts the movie’s matrix from (1,0)(1, 0), 45°45° off the eigenline y=xy = x:

step  λ guess   angle to (1, 1)   ratio
   0   2.0000     45.000°
   1   2.8000     18.435°        0.410
   2   2.9756      6.340°        0.344
   3   2.9973      2.121°        0.335
   4   2.9997      0.707°        0.333
   5   3.0000      0.236°        0.333

each step cuts the angle to a third: ∣λ2/λ1∣=1/3|\lambda_2 / \lambda_1| = 1/3. the first ratio is 0.41 rather than 0.333 because the angle shrinks by exactly a third only once it’s small, where the angle and its tangent agree. the λ\lambda guess is good to four places by step 5. the second run is the tie, rows (1,0)(1, 0) and (0,−1)(0, -1), from (1,0.5)(1, 0.5): the vector goes (0.894,0.447)(0.894, 0.447), (0.894,−0.447)(0.894, -0.447), (0.894,0.447)(0.894, 0.447) and so on, forever, and the guess at λ\lambda sits at 0.6, which is neither eigenvalue. with ∣λ1∣=∣λ2∣|\lambda_1| = |\lambda_2| neither part dies away, so nothing settles.

the last lines are what happens without the scaling. A700(1,0)A^{700}(1, 0) in doubles is (∞,∞)(\infty, \infty): the entries grow like 3n3^n, 37003^{700} is about 1033410^{334}, and the largest double, python’s sys.float_info.max,1 is about 1.8×103081.8 \times 10^{308}, so by step 700 the numbers have overflowed. by step 600 they’re already 9.4×102859.4 \times 10^{285}. the other way round, (12I)1100(1,0)(\tfrac{1}{2} I)^{1100}(1, 0) comes out as (0,0)(0, 0): 2−10742^{-1074} is the smallest positive double, 5×10−3245 \times 10^{-324}, and one more halving is 0. the direction is what power iteration is after, and scaling to length 1 each step keeps the direction and throws the size away.

python/math/power_iteration.py
"""power iteration on a 2×2 matrix: apply the matrix to a vector again and again, scale the result back to length 1 each time, and it turns toward the eigenvector with the biggest |λ|, with the rayleigh quotient as the guess at λ. the run prints how the angle left shrinks by about |λ₂/λ₁| a step, what happens when the two biggest |λ| tie, and what the numbers do without the scaling."""

from __future__ import annotations

import math

Vec = tuple[float, float]
Mat = tuple[Vec, Vec]


def matvec(a: Mat, v: Vec) -> Vec:
    """Return Av for a 2×2 matrix given as its rows."""
    return (a[0][0] * v[0] + a[0][1] * v[1], a[1][0] * v[0] + a[1][1] * v[1])


def normalize(v: Vec) -> Vec:
    """Return v scaled to length 1. The zero vector has no direction, so it raises ValueError."""
    n = math.hypot(v[0], v[1])
    if n == 0:
        raise ValueError("the zero vector has no direction")
    return (v[0] / n, v[1] / n)


def rayleigh(a: Mat, v: Vec) -> float:
    """Return (v · Av) / (v · v): the stretch along v, which is λ exactly when v is an eigenvector."""
    av = matvec(a, v)
    return (v[0] * av[0] + v[1] * av[1]) / (v[0] * v[0] + v[1] * v[1])


def eigenvalues(a: Mat) -> tuple[float, float] | None:
    """Return the real eigenvalues, the bigger first, from λ² − (trace)λ + det = 0, or None when there are none."""
    trace = a[0][0] + a[1][1]
    det = a[0][0] * a[1][1] - a[0][1] * a[1][0]
    disc = trace * trace - 4 * det
    if disc < 0:
        return None
    root = math.sqrt(disc)
    return ((trace + root) / 2, (trace - root) / 2)


def angle_to_line(v: Vec, direction: Vec) -> float:
    """Return the angle in degrees, 0 to 90, between v and the line through the origin along direction."""
    u = normalize(v)
    d = normalize(direction)
    return math.degrees(math.asin(min(1.0, abs(u[0] * d[1] - u[1] * d[0]))))


def power_iteration(a: Mat, v0: Vec, steps: int) -> list[tuple[Vec, float]]:
    """Return (vₙ, rayleigh quotient) for n = 0 to steps, each vₙ scaled to length 1.

    vₙ₊₁ is A vₙ, normalized. Writing v₀ as c₁v₁ + c₂v₂ in the eigenvectors, Aⁿv₀ = c₁λ₁ⁿv₁ + c₂λ₂ⁿv₂, so
    the v₂ part shrinks against the v₁ part by |λ₂/λ₁| a step.
    """
    out: list[tuple[Vec, float]] = []
    v = normalize(v0)
    for _ in range(steps + 1):
        out.append((v, rayleigh(a, v)))
        v = normalize(matvec(a, v))
    return out


def convergence(a: Mat, v0: Vec, direction: Vec, steps: int) -> list[tuple[int, float, float, float | None]]:
    """Return (n, λ guess, angle to the eigenline, this angle over the last) for each step of power iteration."""
    rows: list[tuple[int, float, float, float | None]] = []
    prev: float | None = None
    for n, (v, lam) in enumerate(power_iteration(a, v0, steps)):
        angle = angle_to_line(v, direction)
        ratio = angle / prev if prev else None
        rows.append((n, lam, angle, ratio))
        prev = angle
    return rows


def unscaled(a: Mat, v0: Vec, steps: int) -> Vec:
    """Return Aⁿv₀ with no scaling, in doubles: the numbers grow like |λ₁|ⁿ or shrink like it, and a double runs out at about 1e308 and 5e-324."""
    v = (float(v0[0]), float(v0[1]))
    for _ in range(steps):
        v = matvec(a, v)
    return v


def main() -> None:
    sym: Mat = ((2, 1), (1, 2))
    print("A = [[2, 1], [1, 2]], eigenvalues", eigenvalues(sym), "from v0 = (1, 0)")
    print("step  λ guess   angle to (1, 1)   ratio")
    for n, lam, angle, ratio in convergence(sym, (1, 0), (1, 1), 8):
        print(f"{n:4d}  {lam:7.4f}  {angle:9.3f}°  {'' if ratio is None else f'{ratio:11.3f}'}")

    tie: Mat = ((1, 0), (0, -1))
    print("\nA = [[1, 0], [0, -1]], eigenvalues", eigenvalues(tie), "from v0 = (1, 0.5): |λ₁| = |λ₂|")
    for n, (v, lam) in enumerate(power_iteration(tie, (1, 0.5), 4)):
        print(f"{n:4d}  v = ({v[0]:.4f}, {v[1]:.4f})  λ guess {lam:.4f}")

    print("\nwithout scaling, A^700 (1, 0) =", unscaled(sym, (1, 0), 700))
    half: Mat = ((0.5, 0), (0, 0.5))
    print("and (I/2)^1100 (1, 0) =", unscaled(half, (1, 0), 1100))


if __name__ == "__main__":
    main()

pitfalls

practice

  1. AA has rows (1,2)(1, 2) and (2,1)(2, 1). is (1,2)(1, 2) an eigenvector? is (1,1)(1, 1)?

    answer

    A(1,2)=(1+4,2+2)=(5,4)A(1, 2) = (1 + 4, 2 + 2) = (5, 4), and (5,4)(5, 4) isn’t a multiple of (1,2)(1, 2): no. A(1,1)=(3,3)=3 (1,1)A(1, 1) = (3, 3) = 3\,(1, 1): yes, with λ=3\lambda = 3. the other eigenvector is (1,−1)(1, -1), with A(1,−1)=(−1,1)=−1⋅(1,−1)A(1, -1) = (-1, 1) = -1 \cdot (1, -1), so λ=−1\lambda = -1.

  2. find the eigenvalues and eigenvectors of the matrix with rows (3,1)(3, 1) and (0,2)(0, 2).

    answer

    trace 5, determinant 6: λ2−5λ+6=(λ−3)(λ−2)\lambda^2 - 5\lambda + 6 = (\lambda - 3)(\lambda - 2), so λ=3\lambda = 3 and 22. a triangular matrix has its eigenvalues on the diagonal, since the determinant of A−λIA - \lambda I is the product of the diagonal entries. for λ=3\lambda = 3, A−3IA - 3I has rows (0,1)(0, 1) and (0,−1)(0, -1), saying y=0y = 0: v1=(1,0)v_1 = (1, 0). for λ=2\lambda = 2, A−2IA - 2I has top row (1,1)(1, 1), saying y=−xy = -x: v2=(1,−1)v_2 = (1, -1). check: A(1,−1)=(3−1,−2)=(2,−2)=2 (1,−1)A(1, -1) = (3 - 1, -2) = (2, -2) = 2\,(1, -1).

  3. AA has rows (5,4)(5, 4) and (1,2)(1, 2). find its eigenvalues two ways: from the quadratic, and from the trace and determinant.

    answer

    trace 7, determinant 10−4=610 - 4 = 6: λ2−7λ+6=(λ−6)(λ−1)\lambda^2 - 7\lambda + 6 = (\lambda - 6)(\lambda - 1), so λ=6\lambda = 6 and 11. the check: 6+1=76 + 1 = 7, the trace, and 6⋅1=66 \cdot 1 = 6, the determinant. two numbers with sum 7 and product 6 are 6 and 1, with no quadratic needed. the eigenvectors are (4,1)(4, 1), since A(4,1)=(24,6)=6 (4,1)A(4, 1) = (24, 6) = 6\,(4, 1), and (1,−1)(1, -1), since A(1,−1)=(1,−1)A(1, -1) = (1, -1). they aren’t perpendicular, (4,1)⋅(1,−1)=3(4, 1) \cdot (1, -1) = 3, because AA isn’t symmetric.

  4. the quarter turn has rows (0,−1)(0, -1) and (1,0)(1, 0), and the shear has rows (1,1)(1, 1) and (0,1)(0, 1). say, from the discriminant and from the picture, why neither has two eigen-directions.

    answer

    the discriminant is (a−d)2+4bc(a - d)^2 + 4bc. for the turn, a=d=0a = d = 0 and bc=−1bc = -1: −4-4, under 0, so the quadratic λ2+1\lambda^2 + 1 has no real roots. in the picture, every direction turns by 90°90°, so none lands on its own line. for the shear, a=d=1a = d = 1 and bc=0bc = 0: discriminant 0, one root, λ=1\lambda = 1, counted twice. in the picture, the xx axis holds still and every other line leans over, so there’s one eigenline, not two. A−IA - I has rows (0,1)(0, 1) and (0,0)(0, 0), which only says y=0y = 0.

  5. rows (2,0)(2, 0) and (0,−3)(0, -3). start power iteration at (1,1)(1, 1). which line does it turn toward, what do the signs do, and how fast does it get there?

    answer

    the eigenvalues are 2 along the xx axis and −3-3 along the yy axis, and the size decides: ∣−3∣>2|{-3}| > 2, so the yy part wins. each step doubles the xx part and triples the yy part while flipping its sign, so the vector turns toward the yy axis while swinging from one side of it to the other: (1,1)(1, 1), then (2,−3)(2, -3), then (4,9)(4, 9), then (8,−27)(8, -27), scaled to length 1 each time. the angle to the yy axis goes 45°45°, 33.7°33.7°, 24.0°24.0°, 16.5°16.5°, 11.2°11.2°, 7.5°7.5°, each about 2/32/3 of the last, since ∣λ2/λ1∣=2/3|\lambda_2 / \lambda_1| = 2/3. the rayleigh quotient heads for −3-3.

  6. for the matrix with rows (2,1)(2, 1) and (1,2)(1, 2), write AnA^n in closed form and find A5A^5.

    answer

    A=PDP−1A = PDP^{-1} with PP‘s columns (1,1)(1, 1) and (1,−1)(1, -1), DD the diagonal of 3 and 1, and P−1=12PP^{-1} = \tfrac{1}{2}P. so An=PDnP−1=12P diag(3n,1) PA^n = PD^nP^{-1} = \tfrac{1}{2} P\,\mathrm{diag}(3^n, 1)\,P, which multiplies out to 12(3n+1)\tfrac{1}{2}(3^n + 1) on the diagonal and 12(3n−1)\tfrac{1}{2}(3^n - 1) off it. 35=2433^5 = 243, so A5A^5 has 122 on the diagonal and 121 off it: rows (122,121)(122, 121) and (121,122)(121, 122). check with n=1n = 1: 12(3+1)=2\tfrac{1}{2}(3 + 1) = 2 and 12(3−1)=1\tfrac{1}{2}(3 - 1) = 1, the matrix itself. and A5(1,0)=(122,121)A^5(1, 0) = (122, 121), the first column, is what five applications to (1,0)(1, 0) give directly.

sources

  1. python’s sys.float_info (opens in a new tab) docs: max is the largest positive finite float there is, c’s DBL_MAX. ↩