When a matrix multiplies a vector, it normally changes both its length and its direction. But almost every matrix has a few privileged directions along which the vector does not turn: it is only stretched, shrunk or flipped. Those directions are the eigenvectors, and the stretch factors are the eigenvalues. Knowing them is like having an "X-ray" of the matrix: they reveal its natural axes, make its powers easy to compute and, in 3D graphics, help us find the axis of a rotation or the dominant orientation of a point cloud. With this lesson we wrap up the fundamentals of linear algebra.

Geometric intuition: directions that do not turn

In "Stellar Lighthouse", when the ship engages its boost, the engine deforms the ship's outline in the top-down view by multiplying each point (measured from the center of the ship) by the matrix:

\[ A = \begin{pmatrix} 3 & 1 \ 1 & 3 \end{pmatrix} \]

Let's see what happens to a few vectors:

Vector \(\mathbf{v}\) \(A,\mathbf{v}\) What happened?
\((1, 0)\) \((3, 1)\) Its direction changed
\((0, 1)\) \((1, 3)\) Its direction changed
\((1, 1)\) \((4, 4) = 4,(1, 1)\) Same direction, 4 times longer
\((-1, 1)\) \((-2, 2) = 2,(-1, 1)\) Same direction, 2 times longer

The last two are special: the matrix does not turn them. And it is no coincidence that they are \(\mathbf{u}_1 = (1, 1)\) and \(\mathbf{u}_2 = (-1, 1)\), the top-down ship axes we have been using since the first lesson: the boost effect is designed to stretch the ship 4 times along its axis and 2 times across it.

That is the whole idea: \(A\) looks like a complicated matrix when seen from the world axes, but seen from "its own" directions it is simply a stretch by 4 and another by 2.

Definition: (A,\mathbf{v} = \lambda,\mathbf{v})

Let \(A\) be a square matrix. A nonzero vector \(\mathbf{v}\) is an eigenvector (or characteristic vector) of \(A\) if there is a number \(\lambda\) such that:

\[ A,\mathbf{v} = \lambda,\mathbf{v} \]

The number \(\lambda\) (lambda) is the eigenvalue (or characteristic value) associated with \(\mathbf{v}\).

Eigenvalue Effect on its eigenvector
\(\lambda > 1\) Stretches it
\(\lambda = 1\) Leaves it untouched
\(0 < \lambda < 1\) Shrinks it
\(\lambda = 0\) Flattens it to the zero vector (the matrix is singular)
\(\lambda < 0\) Flips it to point the opposite way (and scales it by \(\lvert \lambda \rvert\))

Two important remarks:

  • The zero vector is excluded by definition: \(A,\mathbf{0} = \lambda,\mathbf{0}\) always holds and tells us nothing. The eigenvalue, on the other hand, can be 0.
  • An eigenvector represents a direction, not one particular arrow. If \(\mathbf{v}\) is an eigenvector, so is any nonzero multiple of it, with the same eigenvalue: \(A,(k\mathbf{v}) = k,A,\mathbf{v} = \lambda,(k\mathbf{v})\). That is why \((1, 1)\), \((2, 2)\) and \((-5, -5)\) are "the same" eigenvector.

The characteristic polynomial

How do we find them without trial and error? We rewrite the definition, moving everything to one side (the trick is to write \(\lambda,\mathbf{v}\) as \(\lambda I,\mathbf{v}\) so that we can factor):

\[ A,\mathbf{v} = \lambda,\mathbf{v} ;\Longrightarrow; A,\mathbf{v} - \lambda I,\mathbf{v} = \mathbf{0} ;\Longrightarrow; (A - \lambda I),\mathbf{v} = \mathbf{0} \]

This is a homogeneous system, and we are looking for a solution \(\mathbf{v}\) other than the zero one. From what we learned about systems, that is possible only if the matrix \(A - \lambda I\) is singular, that is:

\[ \det(A - \lambda I) = 0 \]

This is the characteristic equation. Expanding the determinant gives a polynomial in \(\lambda\) of degree \(n\)—the characteristic polynomial—whose roots are the eigenvalues. The full procedure has two phases:

  1. Eigenvalues: solve \(\det(A - \lambda I) = 0\).
  2. Eigenvectors: for each eigenvalue \(\lambda\), solve the system \((A - \lambda I),\mathbf{v} = \mathbf{0}\).

\(A - \lambda I\) is nothing more than \(A\) with \(\lambda\) subtracted from each diagonal entry.

Working a 2×2 case by hand

Let's apply it to the boost matrix.

Phase 1: eigenvalues.

\[ \det \begin{pmatrix} 3 - \lambda & 1 \ 1 & 3 - \lambda \end{pmatrix} = (3 - \lambda)^2 - 1 = \lambda^2 - 6\lambda + 8 = 0 \]

With the quadratic formula:

\[ \lambda = \frac{6 \pm \sqrt{36 - 32}}{2} = \frac{6 \pm 2}{2} \quad\Longrightarrow\quad \lambda_1 = 4, \qquad \lambda_2 = 2 \]

Phase 2: eigenvectors.

For \(\lambda_1 = 4\):

\[ (A - 4I),\mathbf{v} = \begin{pmatrix} -1 & 1 \ 1 & -1 \end{pmatrix} \begin{pmatrix} x \ y \end{pmatrix} = \begin{pmatrix} 0 \ 0 \end{pmatrix} \quad\Longrightarrow\quad -x + y = 0 \quad\Longrightarrow\quad y = x \]

The two equations say the same thing—they have to: we chose \(\lambda\) precisely so that the system would have infinitely many solutions. Any vector with \(y = x\) will do; we take \(\mathbf{v}_1 = (1, 1)\).

For \(\lambda_2 = 2\):

\[ (A - 2I),\mathbf{v} = \begin{pmatrix} 1 & 1 \ 1 & 1 \end{pmatrix} \begin{pmatrix} x \ y \end{pmatrix} = \begin{pmatrix} 0 \ 0 \end{pmatrix} \quad\Longrightarrow\quad x + y = 0 \quad\Longrightarrow\quad y = -x \]

We take \(\mathbf{v}_2 = (-1, 1)\).

Shortcut and checks for 2×2 matrices

For every 2×2 matrix the characteristic polynomial is:

\[ \lambda^2 - \text{tr}(A),\lambda + \det(A) = 0 \]

where the trace \(\text{tr}(A)\) is the sum of the diagonal entries. Here \(\text{tr}(A) = 6\) and \(\det(A) = 8\). This yields two checks that are valid for any size:

  • the sum of the eigenvalues is the trace: \(4 + 2 = 6\) ✓
  • the product of the eigenvalues is the determinant: \(4 \cdot 2 = 8\) ✓

The second one makes geometric sense: if the matrix stretches by 4 in one direction and by 2 in another, areas are multiplied by 8.

import numpy as np

def eigenvalues_2x2(m):
    """Real eigenvalues of a 2x2 matrix, from largest to smallest.
    Returns an empty tuple if there are no real eigenvalues."""
    trace = m[0, 0] + m[1, 1]
    det = m[0, 0] * m[1, 1] - m[0, 1] * m[1, 0]
    discriminant = trace ** 2 - 4.0 * det
    if discriminant < 0:
        return ()
    root = np.sqrt(discriminant)
    return (float((trace + root) / 2.0), float((trace - root) / 2.0))

a = np.array([[3.0, 1.0],
              [1.0, 3.0]])
print(eigenvalues_2x2(a))
print(a @ np.array([1.0, 1.0]), a @ np.array([-1.0, 1.0]))
(4.0, 2.0)
[4. 4.] [-2.  2.]

A 3×3 example

The procedure is the same; the only difference is that the polynomial has degree 3. Consider:

\[ T = \begin{pmatrix} 2 & 0 & 0 \ 0 & 3 & 4 \ 0 & 4 & 9 \end{pmatrix} \]

Eigenvalues. We expand \(\det(T - \lambda I)\) along the first row, which has two zeros:

\[ \det \begin{pmatrix} 2 - \lambda & 0 & 0 \ 0 & 3 - \lambda & 4 \ 0 & 4 & 9 - \lambda \end{pmatrix} = (2 - \lambda),\big[(3 - \lambda)(9 - \lambda) - 16\big] = (2 - \lambda),(\lambda^2 - 12\lambda + 11) \]

The second factor vanishes for \(\lambda = \frac{12 \pm \sqrt{144 - 44}}{2} = \frac{12 \pm 10}{2}\), that is, 11 and 1. The eigenvalues are \(\lambda = 2\), \(\lambda = 1\) and \(\lambda = 11\).

Check: sum \(2 + 1 + 11 = 14 = \text{tr}(T) = 2 + 3 + 9\) ✓; product \(2 \cdot 1 \cdot 11 = 22 = \det(T) = 2,(27 - 16)\) ✓.

Eigenvectors. We solve \((T - \lambda I),\mathbf{v} = \mathbf{0}\) for each one:

\(\lambda\) Equations of \((T - \lambda I),\mathbf{v} = \mathbf{0}\) Eigenvector
\(2\) \(y + 4z = 0\) and \(4y + 7z = 0\), so \(y = z = 0\); \(x\) free \((1, 0, 0)\)
\(1\) \(x = 0\); \(2y + 4z = 0\), so \(y = -2z\) \((0, -2, 1)\)
\(11\) \(-9x = 0\); \(-8y + 4z = 0\), so \(z = 2y\) \((0, 1, 2)\)

Let's check the last one: \(T,(0, 1, 2) = (0,\ 3 + 8,\ 4 + 18) = (0, 11, 22) = 11,(0, 1, 2)\) ✓.

In practice, polynomials of degree 3 or higher are rarely solved by hand: that is what NumPy's numerical methods are for. What matters is understanding what is being computed.

Eigenspaces

For an eigenvalue \(\lambda\), the set of all its eigenvectors, together with the zero vector, is the solution set of the homogeneous system \((A - \lambda I),\mathbf{v} = \mathbf{0}\), and it is a subspace: the eigenspace of \(\lambda\).

  • Usually it is a line through the origin (dimension 1), as in all the examples above.
  • It can be larger. The diagonal matrix \(\begin{pmatrix} 2 & 0 & 0 \ 0 & 2 & 0 \ 0 & 0 & 5 \end{pmatrix}\) stretches the whole XY plane by 2: the eigenspace of \(\lambda = 2\) is a plane (dimension 2), and the eigenvalue 2 is a double root of the characteristic polynomial.
  • At the extreme, for \(3I\) all of \(\mathbb{R}^3\) is the eigenspace of \(\lambda = 3\): any nonzero vector is an eigenvector.

A very useful fact: eigenvectors associated with distinct eigenvalues are always linearly independent. That is why an \(n \times n\) matrix with \(n\) distinct real eigenvalues automatically provides a basis made up of eigenvectors.

We are not always that lucky. The matrix \(\begin{pmatrix} 1 & 1 \ 0 & 1 \end{pmatrix}\) (a slant, or shear) has the eigenvalue 1 repeated, but its eigenspace is only the line of the X axis: it does not have enough eigenvectors to form a basis.

Diagonalization and matrix powers

The idea

If an \(n \times n\) matrix \(A\) has \(n\) linearly independent eigenvectors, we place them as the columns of a matrix \(P\) and put the eigenvalues, in the same order, in a diagonal matrix \(D\). Then:

\[ A = P,D,P^{-1} \]

We say that \(A\) is diagonalizable. The formula comes from writing the \(n\) equalities \(A,\mathbf{v}_i = \lambda_i \mathbf{v}_i\) all at once as \(A,P = P,D\) and multiplying by \(P^{-1}\) on the right.

Read from right to left, which is how it acts on a vector, the formula tells a story in three steps:

  1. \(P^{-1}\): expresses the vector in the basis of eigenvectors (a change of coordinates, as in the previous lessons).
  2. \(D\): in that basis, the matrix merely scales each coordinate by its eigenvalue.
  3. \(P\): returns to the original coordinates.

Example

For the boost matrix, \(P\) is precisely the matrix \(U\) of the ship basis, whose inverse we computed in the previous lesson:

\[ \underbrace{\begin{pmatrix} 3 & 1 \ 1 & 3 \end{pmatrix}}_{A}

\underbrace{\begin{pmatrix} 1 & -1 \ 1 & 1 \end{pmatrix}}{P} \underbrace{\begin{pmatrix} 4 & 0 \ 0 & 2 \end{pmatrix}}{D} \underbrace{\frac{1}{2}\begin{pmatrix} 1 & 1 \ -1 & 1 \end{pmatrix}}_{P^{-1}} \]

Check: \(P,D = \begin{pmatrix} 4 & -2 \ 4 & 2 \end{pmatrix}\), and \(\frac{1}{2} \begin{pmatrix} 4 & -2 \ 4 & 2 \end{pmatrix} \begin{pmatrix} 1 & 1 \ -1 & 1 \end{pmatrix} = \frac{1}{2} \begin{pmatrix} 6 & 2 \ 2 & 6 \end{pmatrix} = A\) ✓.

Powers

Applying the same matrix many times (an effect that repeats frame after frame, for example) requires computing \(A^k\). With diagonalization, the intermediate factors \(P^{-1} P\) cancel out:

\[ A^2 = (P D P^{-1})(P D P^{-1}) = P,D^2 P^{-1} \qquad\text{and in general}\qquad A^k = P,D^k P^{-1} \]

And raising a diagonal matrix to a power is trivial: you raise each diagonal entry to that power.

Example. \(A^5\), with \(4^5 = 1024\) and \(2^5 = 32\):

\[ A^5 = \begin{pmatrix} 1 & -1 \ 1 & 1 \end{pmatrix} \begin{pmatrix} 1024 & 0 \ 0 & 32 \end{pmatrix} \frac{1}{2}\begin{pmatrix} 1 & 1 \ -1 & 1 \end{pmatrix} = \frac{1}{2} \begin{pmatrix} 1024 + 32 & 1024 - 32 \ 1024 - 32 & 1024 + 32 \end{pmatrix} = \begin{pmatrix} 528 & 496 \ 496 & 528 \end{pmatrix} \]

This also explains the long-term behavior: as \(A\) is repeated, the direction of the largest eigenvalue dominates. If every eigenvalue satisfies \(\lvert \lambda \rvert < 1\), the powers tend to the zero matrix; if any satisfies \(\lvert \lambda \rvert > 1\), they grow without bound.

def diagonalize(m):
    """Return (p, d) with m = p @ d @ inv(p).
    Columns of p: eigenvectors; diagonal of d: eigenvalues."""
    values, p = np.linalg.eig(m)
    return p, np.diag(values)

def matrix_power(m, k):
    """Compute m^k by diagonalization (m must be diagonalizable)."""
    p, d = diagonalize(m)
    return p @ np.diag(np.diag(d) ** k) @ np.linalg.inv(p)

p, d = diagonalize(a)
print(np.allclose(p @ d @ np.linalg.inv(p), a))
print(np.round(matrix_power(a, 5), 6))
True
[[528. 496.]
 [496. 528.]]

np.diag does two jobs: given a vector it builds a diagonal matrix, and given a matrix it extracts its diagonal. That is why np.diag(np.diag(d) ** k) means "take the diagonal, raise it to the power k and build the matrix again".

Symmetric matrices

Symmetric matrices (\(A^T = A\)), which we met in the previous lesson, are the most favorable case. The so-called spectral theorem guarantees that every real symmetric matrix:

  • has all real eigenvalues;
  • has eigenvectors that are mutually perpendicular when they correspond to distinct eigenvalues;
  • is always diagonalizable, and with an orthogonal matrix at that.

If we normalize the eigenvectors to length 1 and place them as the columns of \(Q\), this matrix is orthogonal, so \(Q^{-1} = Q^T\) and the diagonalization does not require computing any inverse:

\[ A = Q,D,Q^T \]

Both of our examples are symmetric, and the promise holds:

  • In \(A\): \((1, 1)\) and \((-1, 1)\) are perpendicular, because \(1 \cdot (-1) + 1 \cdot 1 = 0\).
  • In \(T\): \((0, -2, 1)\) and \((0, 1, 2)\) give \(0 - 2 + 2 = 0\), and both are perpendicular to \((1, 0, 0)\).

Geometrically, a symmetric matrix is always a stretch along perpendicular axes: no direction gets "slanted". For these matrices NumPy offers np.linalg.eigh, which is faster and more accurate:

t = np.array([[2.0, 0.0, 0.0],
              [0.0, 3.0, 4.0],
              [0.0, 4.0, 9.0]])
values, q = np.linalg.eigh(t)
print(values)
print(np.round(q, 4))
print(np.allclose(q.T @ q, np.eye(3)), np.allclose(q @ np.diag(values) @ q.T, t))
[ 1.  2. 11.]
[[ 0.      1.      0.    ]
 [-0.8944  0.      0.4472]
 [ 0.4472  0.      0.8944]]
True True

Each column of q is an eigenvector of length 1: the first, \((0, -0.8944, 0.4472)\), is \((0, -2, 1)\) divided by its length \(\sqrt{5}\). The sign of each column is arbitrary (it may vary from one installation to another), because \(\mathbf{v}\) and \(-\mathbf{v}\) represent the same direction.

When there are no real eigenvalues: rotations

Not every matrix has invariant directions. Take the matrix that rotates every vector in the plane a quarter turn counterclockwise:

\[ G = \begin{pmatrix} 0 & -1 \ 1 & 0 \end{pmatrix} \qquad \det(G - \lambda I) = \lambda^2 + 1 = 0 \]

This equation has no real solutions. And that makes sense: a 90° rotation changes the direction of every vector; none can stay aligned with itself. In general, a rotation in 2D (other than those of 0° and 180°) has no real eigenvalues. The solutions are complex numbers, and NumPy returns them as such, but for us the message is geometric: there are no invariant directions.

In 3D the situation changes in a very interesting way. A polynomial of degree 3 always has at least one real root, and for a rotation that root is \(\lambda = 1\): there is one direction that the rotation leaves untouched, and it is its axis of rotation. Let's check this with the ship's orientation matrix \(N\), which we saw was orthogonal with determinant \(+1\):

ship_orientation = np.array([[0.6, 0.0, 0.8],
                             [0.0, 1.0, 0.0],
                             [-0.8, 0.0, 0.6]])
values, vectors = np.linalg.eig(ship_orientation)
print(np.round(values, 4))
print(np.round(vectors[:, 2].real, 4))     # eigenvector of eigenvalue 1
[0.6+0.8j 0.6-0.8j 1. +0.j ]
[0. 1. 0.]

Two eigenvalues are complex (the "part that rotates") and the third is 1, with eigenvector \((0, 1, 0)\): the ship's orientation is obtained by rotating about the Y axis, the vertical one. Indeed, \(N,(0, 1, 0) = (0, 1, 0)\). This idea—the axis of a rotation is its eigenvector with eigenvalue 1—will come back when we study the axis-angle representation in Module 4.

Application in graphics: principal axes of a point cloud

An engine often needs to wrap an object in a simple box to speed up computations such as collisions. If the object is rotated, a box aligned with the world axes wastes a lot of space; an oriented box, aligned with the object itself, is better. But how do we work out the "natural" orientation of a bunch of vertices? With eigenvectors.

The covariance matrix

Given a cloud of \(n\) points:

  1. Compute its center (the mean of the points).
  2. Subtract the center from each point, obtaining the displacements \(\mathbf{d}_i\).
  3. Build the covariance matrix, which summarizes how the points are spread out:

\[ C = \frac{1}{n} \sum_{i=1}^{n} \mathbf{d}_i,\mathbf{d}_i^T \]

Each term \(\mathbf{d}_i,\mathbf{d}_i^T\) is a column × row product that gives a square matrix. The diagonal of \(C\) holds the spread along each axis (the variance), and the off-diagonal entries hold how much two coordinates tend to vary together. \(C\) is always symmetric, so it has perpendicular eigenvectors:

  • the eigenvectors of \(C\) are the principal axes of the cloud;
  • each eigenvalue is the variance along its axis: the largest one marks the direction in which the cloud is most elongated.

Top-down example by hand

Four vertices of the ship's outline, measured from its center: \((2, 2)\), \((-2, -2)\), \((1, -1)\) and \((-1, 1)\). Their mean is \((0, 0)\), so they are already centered.

  • Sum of \(x^2\): \(4 + 4 + 1 + 1 = 10\), and the same for \(z^2\).
  • Sum of \(x,z\): \(4 + 4 - 1 - 1 = 6\).

\[ C = \frac{1}{4} \begin{pmatrix} 10 & 6 \ 6 & 10 \end{pmatrix} = \begin{pmatrix} 2.5 & 1.5 \ 1.5 & 2.5 \end{pmatrix} \]

Characteristic polynomial: \(\lambda^2 - 5\lambda + (6.25 - 2.25) = \lambda^2 - 5\lambda + 4 = 0\), with roots \(\lambda_1 = 4\) and \(\lambda_2 = 1\). Eigenvectors: for \(\lambda = 4\), \(-1.5x + 1.5z = 0\), that is, \((1, 1)\); for \(\lambda = 1\), \(1.5x + 1.5z = 0\), that is, \((-1, 1)\).

The analysis has "discovered", from the points alone, that the ship is elongated in the direction \((1, 1)\) and that its transverse axis is \((-1, 1)\): the ship's axes.

In mini3d, in 3D

def covariance_matrix(points):
    """Covariance matrix of a point cloud (one point per row)."""
    points = np.asarray(points, dtype=float)
    centered = points - points.mean(axis=0)
    return centered.T @ centered / len(points)

def principal_axes(points):
    """Return (center, variances, axes) of a point cloud.
    The columns of 'axes' are the principal axes, from largest to smallest variance."""
    points = np.asarray(points, dtype=float)
    center = points.mean(axis=0)
    variances, axes = np.linalg.eigh(covariance_matrix(points))
    order = np.argsort(variances)[::-1]          # from largest to smallest
    return center, variances[order], axes[:, order]

# Six vertices of the ship's hull, in world coordinates
hull = np.array([[10.4, 3.0, 2.8], [5.6, 3.0, 9.2],
                 [8.0, 4.0, 6.0], [8.0, 2.0, 6.0],
                 [9.6, 3.0, 7.2], [6.4, 3.0, 4.8]])

center, variances, axes = principal_axes(hull)
print(np.round(center, 4))
print(np.round(variances, 4))
print(np.round(axes, 4) + 0.0)     # the + 0.0 keeps "-0." from being shown
[8. 3. 6.]
[5.3333 1.3333 0.3333]
[[-0.6 -0.8  0. ]
 [ 0.   0.   1. ]
 [ 0.8 -0.6  0. ]]

How to read it:

  • points.mean(axis=0) averages by columns: it is the center of the cloud, \((8, 3, 6)\), the ship's position.
  • centered.T @ centered computes the sum of all the products \(\mathbf{d}_i,\mathbf{d}_i^T\) in one go, because each point is a row.
  • eigh returns the eigenvalues from smallest to largest; np.argsort(...)[::-1] reorders them from largest to smallest and axes[:, order] reorders the columns to match.
  • The longest axis is \((-0.6, 0, 0.8)\), which is \(-\mathbf{n}_1\): the same line as the ship's first axis (remember that the sign is arbitrary). The second, \((-0.8, 0, -0.6)\), is \(-\mathbf{n}_3\), and the third is \(\mathbf{n}_2 = (0, 1, 0)\).

Knowing nothing about the ship's orientation, the analysis has recovered its three axes from the vertices. With the center, the axes and the extent of the points along each axis, the oriented box can be built; boxes will be back in the lesson "Intersections: Rays, Spheres, Triangles and Boxes".

Note: in statistics it is common to divide by \(n - 1\) instead of \(n\). The principal axes are the same; only the scale of the eigenvalues changes.

Eigenvalues in NumPy: eig and eigh

np.linalg.eig(m) np.linalg.eigh(m)
Accepted matrices Any square matrix Symmetric only
Eigenvalues May be complex; no guaranteed order Always real; sorted from smallest to largest
Eigenvectors Columns of length 1; not necessarily perpendicular Orthonormal columns (they form an orthogonal matrix)
Accuracy and speed Good Better

Both return two results: an array with the eigenvalues and a matrix whose columns are the eigenvectors, in the same order.

values, vectors = np.linalg.eig(a)
print(values)
print(np.round(vectors, 4))

v = vectors[:, 0]                         # first eigenvector: a COLUMN
print(np.allclose(a @ v, values[0] * v))
[4. 2.]
[[ 0.7071 -0.7071]
 [ 0.7071  0.7071]]
True

NumPy returns \((0.7071, 0.7071)\) instead of \((1, 1)\): it is the same direction, scaled so that its length is 1.

Common Mistakes and Tips

  • Reading the eigenvectors by rows. In NumPy they are the columns: vectors[:, i], not vectors[i].
  • Expecting a "unique" eigenvector. Any nonzero multiple will do. If your result by hand is \((1, 1)\) and NumPy's is \((0.7071, 0.7071)\) or \((-0.7071, -0.7071)\), that is not an error.
  • Accepting the zero vector as an eigenvector. When solving \((A - \lambda I),\mathbf{v} = \mathbf{0}\) you must keep a nonzero solution.
  • Getting a system with a unique solution when looking for the eigenvector. If \((A - \lambda I),\mathbf{v} = \mathbf{0}\) only admits \(\mathbf{v} = \mathbf{0}\), the eigenvalue has been computed incorrectly.
  • Subtracting \(\lambda\) from every entry. In \(A - \lambda I\) it is subtracted only on the diagonal.
  • Mixing up the order of \(P\) and \(D\). Column \(i\) of \(P\) must correspond to entry \(i\) of the diagonal of \(D\).
  • Using eigh with a non-symmetric matrix. It gives no warning: it uses only half of the matrix and returns a wrong result. Check first with is_symmetric.
  • Being scared off by complex numbers. If eig returns values with j, the matrix includes a rotation. For symmetric matrices this never happens.
  • Tip: always check with the trace and the determinant (sum and product of the eigenvalues) and, for each pair, that \(A,\mathbf{v} = \lambda,\mathbf{v}\).

Exercises

Exercise 1. The ship's shield deforms its surroundings in the top-down view according to the matrix \(Z = \begin{pmatrix} 4 & 1 \ 2 & 3 \end{pmatrix}\).

a) Compute its eigenvalues and eigenvectors by hand and verify the result with the trace and the determinant. b) Are its eigenvectors perpendicular? Why does that not contradict what we saw about symmetric matrices?

Exercise 2. A trail effect applies the matrix \(W = \begin{pmatrix} 2 & 1 \ 1 & 2 \end{pmatrix}\) on every frame. Diagonalize it and compute by hand \(W^4\), the accumulated effect after 4 frames. Check it with matrix_power.

Exercise 3. In the top-down view, the radar detects four fragments of a debris field at \((14, 6)\), \((6, 2)\), \((9, 6)\) and \((11, 2)\).

a) Compute the center and the covariance matrix. b) Find the principal axes. In which direction is the field elongated? c) Check it with principal_axes.

Solutions

Solution 1.

a) \(\text{tr}(Z) = 7\), \(\det(Z) = 12 - 2 = 10\). Polynomial: \(\lambda^2 - 7\lambda + 10 = 0\), with roots \(\lambda = \frac{7 \pm \sqrt{49 - 40}}{2} = \frac{7 \pm 3}{2}\): \(\lambda_1 = 5\) and \(\lambda_2 = 2\).

  • For \(\lambda = 5\): \((Z - 5I) = \begin{pmatrix} -1 & 1 \ 2 & -2 \end{pmatrix}\), so \(-x + y = 0\) and \(\mathbf{v}_1 = (1, 1)\). Check: \(Z,(1, 1) = (5, 5)\) ✓.
  • For \(\lambda = 2\): \((Z - 2I) = \begin{pmatrix} 2 & 1 \ 2 & 1 \end{pmatrix}\), so \(2x + y = 0\) and \(\mathbf{v}_2 = (1, -2)\). Check: \(Z,(1, -2) = (4 - 2,\ 2 - 6) = (2, -4) = 2,(1, -2)\) ✓.

Sum: \(5 + 2 = 7\) ✓. Product: \(5 \cdot 2 = 10\) ✓.

b) \(1 \cdot 1 + 1 \cdot (-2) = -1 \ne 0\): they are not perpendicular. There is no contradiction, because \(Z\) is not symmetric; perpendicularity is guaranteed only for symmetric matrices.

Solution 2. \(\text{tr}(W) = 4\), \(\det(W) = 3\): \(\lambda^2 - 4\lambda + 3 = 0\), with roots \(\lambda_1 = 3\) and \(\lambda_2 = 1\).

  • \(\lambda = 3\): \(-x + y = 0\), eigenvector \((1, 1)\).
  • \(\lambda = 1\): \(x + y = 0\), eigenvector \((-1, 1)\).

It is the same \(P\) as in the lesson's example, with \(D = \begin{pmatrix} 3 & 0 \ 0 & 1 \end{pmatrix}\). Since \(3^4 = 81\) and \(1^4 = 1\):

\[ W^4 = \begin{pmatrix} 1 & -1 \ 1 & 1 \end{pmatrix} \begin{pmatrix} 81 & 0 \ 0 & 1 \end{pmatrix} \frac{1}{2}\begin{pmatrix} 1 & 1 \ -1 & 1 \end{pmatrix} = \frac{1}{2} \begin{pmatrix} 81 + 1 & 81 - 1 \ 81 - 1 & 81 + 1 \end{pmatrix} = \begin{pmatrix} 41 & 40 \ 40 & 41 \end{pmatrix} \]

w = np.array([[2.0, 1.0], [1.0, 2.0]])
print(np.round(matrix_power(w, 4), 6))
[[41. 40.]
 [40. 41.]]

After 4 frames the trail has been stretched 81 times along \((1, 1)\) and not at all along \((-1, 1)\): the direction of the largest eigenvalue dominates.

Solution 3.

a) Center: \(\left(\frac{14 + 6 + 9 + 11}{4},\ \frac{6 + 2 + 6 + 2}{4}\right) = (10, 4)\). Displacements from the center: \((4, 2)\), \((-4, -2)\), \((-1, 2)\) and \((1, -2)\).

  • Sum of \(x^2\): \(16 + 16 + 1 + 1 = 34\).
  • Sum of \(z^2\): \(4 + 4 + 4 + 4 = 16\).
  • Sum of \(x,z\): \(8 + 8 - 2 - 2 = 12\).

\[ C = \frac{1}{4} \begin{pmatrix} 34 & 12 \ 12 & 16 \end{pmatrix} = \begin{pmatrix} 8.5 & 3 \ 3 & 4 \end{pmatrix} \]

b) \(\text{tr}(C) = 12.5\), \(\det(C) = 34 - 9 = 25\): \(\lambda^2 - 12.5\lambda + 25 = 0\), with roots \(\lambda = \frac{12.5 \pm \sqrt{156.25 - 100}}{2} = \frac{12.5 \pm 7.5}{2}\): \(\lambda_1 = 10\) and \(\lambda_2 = 2.5\).

  • \(\lambda = 10\): \(-1.5x + 3z = 0\), so \(x = 2z\): axis \((2, 1)\).
  • \(\lambda = 2.5\): \(6x + 3z = 0\), so \(z = -2x\): axis \((-1, 2)\).

The field is elongated in the direction \((2, 1)\), where the variance (10) is four times larger than in the perpendicular direction (2.5). The axes are perpendicular, \(2 \cdot (-1) + 1 \cdot 2 = 0\), as befits a symmetric matrix.

c)

debris = [[14, 6], [6, 2], [9, 6], [11, 2]]
center, variances, axes = principal_axes(debris)
print(center)
print(variances)
print(np.round(axes, 4))
[10.  4.]
[10.   2.5]
[[-0.8944  0.4472]
 [-0.4472 -0.8944]]

The first column, \((-0.8944, -0.4472)\), is \(-(2, 1)/\sqrt{5}\): the same line as \((2, 1)\), with length 1 and the opposite sign. Typical mistake: dismissing an axis as wrong because its sign is the opposite of the one computed by hand.

Conclusion

An eigenvector of a matrix is a direction that the matrix does not turn, and its eigenvalue is the factor by which the matrix stretches it. They are found by solving the characteristic equation \(\det(A - \lambda I) = 0\) and then the system \((A - \lambda I),\mathbf{v} = \mathbf{0}\). When there are enough eigenvectors, the matrix can be diagonalized, \(A = P,D,P^{-1}\), which reduces it to simple scalings in the right basis and simplifies its powers. Symmetric matrices always have real eigenvalues and perpendicular axes—hence their use for finding the principal axes of a point cloud—whereas rotations have no invariant directions in 2D and keep only their axis in 3D. mini3d gains eigenvalues_2x2, diagonalize, matrix_power, covariance_matrix and principal_axes.

This lesson brings Module 1 to a close. You now have the whole language of linear algebra at your disposal: vectors and bases, matrices and determinants, systems of equations, inverses, orthogonal matrices and eigenvalues. So far we have treated matrices mostly as objects to compute with; in Module 2, "Linear Transformations", we will look at them as what they really are in a 3D engine: functions that move and deform space. We will start with the lesson "Definition and Properties", where we will pin down what it means for a transformation to be linear and why every linear transformation can be represented by a matrix.

© Copyright 2026. All rights reserved