Application: Simplifying Quadratic Forms
An equation like describes an ellipse, but the cross term hides its shape. Rotating to the eigenvectors of a symmetric matrix removes every cross term at once, and the eigenvalues read off the axes.
Quadratic forms as matrices
A quadratic form in variables is a polynomial whose terms all have degree 2. Every one can be written with a symmetric : put the coefficient of on the diagonal and split the coefficient of each cross term equally between positions and .
The principal axes theorem
By the spectral theorem, with orthogonal. Introduce new coordinates (so ). Then
No cross terms remain. The columns of , the eigenvectors, are the principal axes. Because is orthogonal, the change of variables is a rotation (choose the column signs so ) and possibly a reflection, so lengths and shapes are unchanged: the curve is the same, just seen in better coordinates.
Worked example
Simplify .
from sympy import Matrix, symbols, expand, simplify, sqrt
x, y, X, Y = symbols("x y X Y")
A = Matrix([[5, -2], [-2, 8]])
for lam, mult, vecs in A.eigenvects():
v = vecs[0]
print("eigenvalue", lam, "unit eigenvector", list(v / v.norm()))
Q = Matrix([[2, 1], [1, -2]]) / sqrt(5) # columns: eigenvectors for 4 and 9
print("Q^T A Q =", simplify(Q.T * A * Q).tolist())
xy = Q * Matrix([X, Y]) # substitute x = Q [X, Y]
q = expand((xy.T * A * xy)[0])
print("in new coordinates:", q, "= 36")eigenvalue 4 unit eigenvector [2*sqrt(5)/5, sqrt(5)/5]
eigenvalue 9 unit eigenvector [-sqrt(5)/5, 2*sqrt(5)/5]
Q^T A Q = [[4, 0], [0, 9]]
in new coordinates: 4*X**2 + 9*Y**2 = 36
So , that is : an ellipse with semi-axes 3 along and 2 along . (SymPy returned the second eigenvector with the opposite sign; either sign is a valid unit eigenvector.)
The rule: for with and eigenvalues , the semi-axis along the -th eigenvector has length . The largest eigenvalue gives the shortest axis.
import numpy as np
import matplotlib.pyplot as plt
A = np.array([[5.0, -2.0], [-2.0, 8.0]])
w, Q = np.linalg.eigh(A)
t = np.linspace(0, 2 * np.pi, 400)
pts = Q @ np.vstack([np.sqrt(36 / w[0]) * np.cos(t), np.sqrt(36 / w[1]) * np.sin(t)])
q = np.einsum("ij,ik,kj->j", pts, A, pts) # q(x) at every point
print("eigenvalues:", w, " semi-axes:", np.sqrt(36 / w))
print("q on the curve: min", round(float(q.min()), 6), "max", round(float(q.max()), 6))
plt.figure(figsize=(5, 5))
plt.plot(pts[0], pts[1])
for i in range(2):
a = np.sqrt(36 / w[i]) * Q[:, i]
plt.plot([-a[0], a[0]], [-a[1], a[1]], "--", label=f"axis λ={w[i]:.0f}")
plt.gca().set_aspect("equal"); plt.legend(); plt.title("5x² − 4xy + 8y² = 36")eigenvalues: [4. 9.] semi-axes: [3. 2.]
q on the curve: min 36.0 max 36.0
Classifying conics and quadrics
The signs of the eigenvalues decide the shape of (for ), no algebra needed:
| Eigenvalues (2D) | Curve | Eigenvalues (3D) | Surface |
|---|---|---|---|
| ellipse | ellipsoid | ||
| opposite signs | hyperbola | , | hyperboloid of one sheet |
| one 0, | pair of parallel lines | , | hyperboloid of two sheets |
| empty | empty |
If linear terms are present (like ), first diagonalize the quadratic part, then complete the square in the new coordinates to find the centre. See Conic Sections and Quadric Surfaces.
Optimization on the unit sphere
The same diagonal form answers: what are the largest and smallest values of when ? Since , is a weighted average of the eigenvalues. So
The ratio is the Rayleigh quotient. This fact is the core of PCA: the direction of maximum variance is the top eigenvector of the covariance matrix.
In software
- Collision and physics. Ellipsoid bounding volumes and inertia ellipsoids are quadratic forms; diagonalizing gives their axes for drawing and testing.
- Computer vision. The Harris corner detector looks at the eigenvalues of a structure tensor: two large ones mean a corner, one large one an edge.
- ML and statistics. The contour lines of a Gaussian density are the ellipses ; uncertainty ellipses in plots are drawn exactly as above.
- Optimization. Near a minimum, a loss looks like ; the eigenvalues of the Hessian tell you how stretched the valley is, which controls how slowly gradient descent converges.
Exercises
Exercise 1: Write the matrix
Find the symmetric matrix of .
Answer
.
Solution
Diagonal: 3, −2, 1. Half of each cross coefficient off the diagonal: , , .
Exercise 2: Classify a conic
What curve is ?
Answer
A hyperbola: in principal axes it becomes .
Solution
, eigenvalues and (eigenvectors and ). Opposite signs give a hyperbola, , opening along the line .
Exercise 3: Axes of an ellipse (code)
Find the semi-axes of and their directions.
Answer
Semi-axis 3 along and semi-axis 2 along .
Solution
, eigenvalues 8 (on ) and 18 (on ). Semi-axes and .
import numpy as np
w, Q = np.linalg.eigh(np.array([[13.0, 5.0], [5.0, 13.0]]))
print(w, np.sqrt(72 / w), np.round(Q, 4).tolist())[ 8. 18.] [3. 2.] [[-0.7071, 0.7071], [0.7071, 0.7071]]
(NumPy's first eigenvector is , the same axis as .)
Exercise 4: Extremes on the circle
What are the maximum and minimum of on the unit circle ?
Answer
Maximum 9 at , minimum 4 at .
Solution
From the worked example, the eigenvalues are 4 and 9 with those eigenvectors. Check: at , .
Test yourself
The quadratic form has symmetric matrix…
In principal axes, becomes . The longer semi-axis has length…
- 2
- 4
- 3
- 9
A quadric in 3D, where has eigenvalues 2, 1, −3, is…
- an ellipsoid
- a hyperboloid of one sheet
- a hyperboloid of two sheets
- empty