mathlinear algebra
eigenvectors
the directions a matrix only stretches, and by how much.
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
- 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
the idea
a matrix is a rule that moves every vector, as the matrices page shows. take
and feed it : out comes , a vector pointing somewhere else. feed it and get . most vectors change direction when acts on them. a few don’t. , which is : the same direction, three times as long. and , untouched. those two are the eigenvectors of , and the stretches, 3 and 1, are its eigenvalues. the movie sweeps a vector around the circle and watches for the moments when and line up.
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: is an eigenvector of when for some number , with . the eigenvalue says what happens along that line. stretches, shrinks, flips the vector to the other side of the origin, and squashes it to the origin. the zero vector is left out because holds for every , 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 . follows in amber, and the dashed lines are the eigenlines with their . put on one and the angle between and drops to , or reaches where is negative.
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.
try it
A has rows (2, 1) and (1, 2). is v = (1, 1) an eigenvector of A?
pick one.
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
sends to a multiple of itself. the multiple is the eigenvalue. any nonzero multiple of is an eigenvector with the same , since , so an eigenvector is a direction, a line through the origin, more than one vector. the page writes one representative, like , and means the whole line.
finding λ
move everything in to one side: . 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 with entries , the determinant is , which is the quadratic above: minus the trace times plus the determinant. its two roots are the eigenvalues, and the discriminant decides how many are real: two when it’s positive, one repeated when it’s 0, none when it’s negative.
finding v
with in hand, the directions come from the matrix . 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 asks for , and answers it. for the movie’s matrix at , has rows and : , so .
trace and determinant
multiply out and it’s , 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 , . the determinant as a product says something on its own: the matrix scales areas by , which is what stretching by one way and 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 , has real eigenvalues always and perpendicular eigenlines: the discriminant is , which can’t be negative, and the key idea below does the perpendicular part. a shear, , slides every point sideways by its height, so the axis is the only line that stays put, and 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 to : on every line.
the axes are the eigenlines, and the diagonal entries are the eigenvalues: 2 and 0.5
two perpendicular eigenlines, and the eigenvalues are real: 3 along (1, 1) and 1 along (1, −1)
one eigenline, the x axis, with λ = 1 counted twice. every other direction leans over
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.
powers
if stretches by , applying it times stretches it by . that makes eigenvectors the way to understand a matrix applied over and over. write any start as a mix of the two eigenvectors, , and then
each part grows or shrinks on its own. if , the first part wins: the ratio of the second to the first is times what it was, so it shrinks by every step, and turns toward . that’s power iteration: apply , 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 a step, and the rayleigh quotient closes in on . a ratio near 0 settles in a few steps. a ratio near 1 takes many.
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.
diagonalization
put the eigenvectors in the columns of and the eigenvalues on the diagonal of . then , since times each column is times that column, and when the eigenvectors span the plane has an inverse and . the payoff is the powers: , the inner cancels every time, and with the diagonal matrix of . the worked example below gets this way, from , without multiplying by itself. it needs two independent eigenvectors: a shear has one, and can’t be diagonalized.
worked examples
a symmetric matrix by hand
with rows and , the movie’s matrix. trace 4, determinant 3:
so and . for , has rows and , both saying : . for , has rows and , saying : . check: and . the two directions are perpendicular, , as they always are for a symmetric matrix.
one that isn’t symmetric
rows and . trace 7, determinant , so and the eigenvalues are 5 and 2. for , has top row : . for , has top row : , so . check: . the two lines aren’t perpendicular this time, , and nothing says they should be.
a shear
rows and . trace 2, determinant 1: , so , twice. has rows and , saying only : the one direction is . there’s no second one. the repeated root promised two eigenvalues and delivered one line, and that is what a shear looks like: the axis holds still and everything above it leans.
a rotation
rows and , a quarter turn counter-clockwise. trace 0, determinant 1: , which has no real roots. no vector in the plane comes back on its own line after a quarter turn. the eigenvalues are and , 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 . one minute later the shares are with
trace 1.4, determinant 0.4: , so the eigenvalues are 1 and 0.4. the eigenvector for is the state nothing changes: has top row , so , and with the shares adding to 1 that’s . start everyone in room 1, , and watch: , then , then . the gap to goes , , , each exactly 0.4 times the one before, because the start is plus a multiple of the other eigenvector , and that part shrinks by every minute. the long-run share is the eigenvector, and 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, has columns and , has 3 and 1 on the diagonal, and , since . then
gives back rows and . gives and , and gives on the diagonal and off it. the two entries are within 1 of each other because the part never grows: is times the matrix that projects onto , off by in each entry.
the key idea up close
why det(A − λI) = 0
start from the definition and move across:
the is there because means nothing: a matrix minus a number. is the matrix that scales everything by . so is a matrix that sends the nonzero vector to the origin. a matrix that does that can’t have an inverse: if existed, then , and isn’t 0. and a matrix with no inverse has determinant 0. the determinant is the factor by which the matrix scales area. squashes the whole line through onto one point, so a square with a side along 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 with a nonzero answer is one whose matrix has determinant 0.
that turns a question about vectors into one about a number. for a ,
a quadratic in , with the trace and the determinant as its coefficients. its discriminant is
which says when the eigenvalues are real. with , the last term is and the whole thing can’t be negative: every symmetric matrix has real eigenvalues. with negative enough it goes under 0 and there are none. the rotation has and , discriminant . the shear has and , discriminant 0, the repeated root.
why a symmetric matrix’s eigenlines are perpendicular
let and with . for a symmetric , for any and : the dot product doesn’t care which side the matrix sits on. then
so , and since the eigenvalues differ, : 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 : apply the matrix, scale back to length 1, repeat, with the rayleigh quotient as the guess at and the angle to the eigenline as the error. the run starts the movie’s matrix from , off the eigenline :
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: . 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 guess is good to four places by step 5. the second run is the tie, rows and , from : the vector goes , , and so on, forever, and the guess at sits at 0.6, which is neither eigenvalue. with neither part dies away, so nothing settles.
the last lines are what happens without the scaling. in doubles is : the entries grow like , is about , and the largest double, python’s sys.float_info.max,1 is about , so by step 700 the numbers have overflowed. by step 600 they’re already . the other way round, comes out as : is the smallest positive double, , 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.
"""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
- an eigenvector isn’t one vector. , and are all eigenvectors of the movie’s matrix with , and so is every other nonzero multiple. the page writes one and means the line. two answers that differ by a factor are the same answer
- a matrix can have no real eigenvectors. a quarter turn has . it isn’t a mistake in the algebra: no direction in the plane survives the turn, and the discriminant going negative is how the quadratic says so
- a repeated eigenvalue can come with one direction. the shear’s is a double root, and has rows and , which pin down one line. don’t expect two eigenvectors from two roots when the roots coincide, and don’t try to diagonalize it
- power iteration stalls. when the two biggest are equal, the vector bounces and never settles: rows and from flip between and . and a start with nothing along the dominant direction never turns toward it: under the movie’s matrix stays on the line forever. a random start has some of every direction, which is why it almost always works
- without scaling, the numbers run away. overflows to for the movie’s matrix, and underflows to 0. scale to length 1 each step
- is allowed. isn’t. rows and send to the origin: an eigenvector with eigenvalue 0, and the sign that the determinant is 0. the zero vector, on the other hand, satisfies for every and says nothing
practice
-
has rows and . is an eigenvector? is ?
answer
, and isn’t a multiple of : no. : yes, with . the other eigenvector is , with , so .
-
find the eigenvalues and eigenvectors of the matrix with rows and .
answer
trace 5, determinant 6: , so and . a triangular matrix has its eigenvalues on the diagonal, since the determinant of is the product of the diagonal entries. for , has rows and , saying : . for , has top row , saying : . check: .
-
has rows and . find its eigenvalues two ways: from the quadratic, and from the trace and determinant.
answer
trace 7, determinant : , so and . the check: , the trace, and , the determinant. two numbers with sum 7 and product 6 are 6 and 1, with no quadratic needed. the eigenvectors are , since , and , since . they aren’t perpendicular, , because isn’t symmetric.
-
the quarter turn has rows and , and the shear has rows and . say, from the discriminant and from the picture, why neither has two eigen-directions.
answer
the discriminant is . for the turn, and : , under 0, so the quadratic has no real roots. in the picture, every direction turns by , so none lands on its own line. for the shear, and : discriminant 0, one root, , counted twice. in the picture, the axis holds still and every other line leans over, so there’s one eigenline, not two. has rows and , which only says .
-
rows and . start power iteration at . 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 axis and along the axis, and the size decides: , so the part wins. each step doubles the part and triples the part while flipping its sign, so the vector turns toward the axis while swinging from one side of it to the other: , then , then , then , scaled to length 1 each time. the angle to the axis goes , , , , , , each about of the last, since . the rayleigh quotient heads for .
-
for the matrix with rows and , write in closed form and find .
answer
with ‘s columns and , the diagonal of 3 and 1, and . so , which multiplies out to on the diagonal and off it. , so has 122 on the diagonal and 121 off it: rows and . check with : and , the matrix itself. and , the first column, is what five applications to give directly.
sources
-
python’s sys.float_info (opens in a new tab) docs:
maxis the largest positive finite float there is, c’sDBL_MAX. ↩