TExcelTAD
Home › Linear Algebra & Matrix Theory › Application: Simplifying Quadratic Forms

Application: Simplifying Quadratic Forms

An equation like 5x2−4xy+8y2=365x^2 - 4xy + 8y^2 = 36 describes an ellipse, but the cross term −4xy-4xy 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 nn variables is a polynomial whose terms all have degree 2. Every one can be written q(x)=xTAxq(\mathbf{x}) = \mathbf{x}^TA\mathbf{x} with a symmetric AA: put the coefficient of xi2x_i^2 on the diagonal and split the coefficient of each cross term xixjx_ix_j equally between positions (i,j)(i, j) and (j,i)(j, i).

5x2−4xy+8y2=[xy][5−2−28][xy]5x^2 - 4xy + 8y^2 = \begin{bmatrix} x & y \end{bmatrix} \begin{bmatrix} 5 & -2 \\ -2 & 8 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}
x2+2y2+3z2+4xy−6yz  ⟶  A=[12022−30−33]x^2 + 2y^2 + 3z^2 + 4xy - 6yz \;\longrightarrow\; A = \begin{bmatrix} 1 & 2 & 0 \\ 2 & 2 & -3 \\ 0 & -3 & 3 \end{bmatrix}

The principal axes theorem

By the spectral theorem, A=QΛQTA = Q\Lambda Q^T with QQ orthogonal. Introduce new coordinates y=QTx\mathbf{y} = Q^T\mathbf{x} (so x=Qy\mathbf{x} = Q\mathbf{y}). Then

q=xTAx=yTQTAQy=yTΛy=λ1y12+λ2y22+⋯+λnyn2q = \mathbf{x}^TA\mathbf{x} = \mathbf{y}^TQ^TAQ\mathbf{y} = \mathbf{y}^T\Lambda\mathbf{y} = \lambda_1y_1^2 + \lambda_2y_2^2 + \cdots + \lambda_ny_n^2

No cross terms remain. The columns of QQ, the eigenvectors, are the principal axes. Because QQ is orthogonal, the change of variables is a rotation (choose the column signs so det⁡Q=+1\det Q = +1) and possibly a reflection, so lengths and shapes are unchanged: the curve is the same, just seen in better coordinates.

Worked example

Simplify 5x2−4xy+8y2=365x^2 - 4xy + 8y^2 = 36.

python · try it yourself
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 4X2+9Y2=364X^2 + 9Y^2 = 36, that is X29+Y24=1\frac{X^2}{9} + \frac{Y^2}{4} = 1: an ellipse with semi-axes 3 along (2,1)/5(2, 1)/\sqrt{5} and 2 along (1,−2)/5(1, -2)/\sqrt{5}. (SymPy returned the second eigenvector with the opposite sign; either sign is a valid unit eigenvector.)

The rule: for q(x)=cq(\mathbf{x}) = c with c>0c > 0 and eigenvalues λi>0\lambda_i > 0, the semi-axis along the ii-th eigenvector has length c/λi\sqrt{c/\lambda_i}. The largest eigenvalue gives the shortest axis.

python · try it yourself
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 q(x)=cq(\mathbf{x}) = c (for c>0c > 0), no algebra needed:

Eigenvalues (2D) Curve Eigenvalues (3D) Surface
both>0\text{both} > 0 ellipse all>0\text{all} > 0 ellipsoid
opposite signs hyperbola two>0\text{two} > 0, one<0\text{one} < 0 hyperboloid of one sheet
one 0, one>0\text{one} > 0 pair of parallel lines one>0\text{one} > 0, two<0\text{two} < 0 hyperboloid of two sheets
both<0\text{both} < 0 empty all<0\text{all} < 0 empty

If linear terms are present (like +6x+ 6x), 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 q(x)q(\mathbf{x}) when ∥x∥=1\lVert \mathbf{x} \rVert = 1? Since ∥y∥=∥x∥=1\lVert \mathbf{y} \rVert = \lVert \mathbf{x} \rVert = 1, q=∑λiyi2q = \sum \lambda_iy_i^2 is a weighted average of the eigenvalues. So

max⁡∥x∥=1xTAx=λmax⁡(at its eigenvector)min⁡∥x∥=1xTAx=λmin⁡\begin{aligned} \max_{\lVert \mathbf{x} \rVert = 1} \mathbf{x}^TA\mathbf{x} &= \lambda_{\max} && \text{(at its eigenvector)} \\ \min_{\lVert \mathbf{x} \rVert = 1} \mathbf{x}^TA\mathbf{x} &= \lambda_{\min} \end{aligned}

The ratio xTAx/xTx\mathbf{x}^TA\mathbf{x} / \mathbf{x}^T\mathbf{x} 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 2×22 \times 2 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 xTΣ−1x=c\mathbf{x}^T\Sigma^{-1}\mathbf{x} = c; uncertainty ellipses in plots are drawn exactly as above.
  • Optimization. Near a minimum, a loss looks like 12hTHh\frac{1}{2}\mathbf{h}^TH\mathbf{h}; the eigenvalues of the Hessian HH 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 q=3x2−2y2+z2+6xy−4xz+2yzq = 3x^2 - 2y^2 + z^2 + 6xy - 4xz + 2yz.

Answer

A=[33−23−21−211]A = \begin{bmatrix} 3 & 3 & -2 \\ 3 & -2 & 1 \\ -2 & 1 & 1 \end{bmatrix}.

Solution

Diagonal: 3, −2, 1. Half of each cross coefficient off the diagonal: xy→6/2=3xy \to 6/2 = 3, xz→−4/2=−2xz \to -4/2 = -2, yz→2/2=1yz \to 2/2 = 1.

Exercise 2: Classify a conic

What curve is x2+6xy+y2=8x^2 + 6xy + y^2 = 8?

Answer

A hyperbola: in principal axes it becomes 4X2−2Y2=84X^2 - 2Y^2 = 8.

Solution

A=[1331]A = \begin{bmatrix} 1 & 3 \\ 3 & 1 \end{bmatrix}, eigenvalues 1±3=41 \pm 3 = 4 and −2-2 (eigenvectors (1,1)/2(1, 1)/\sqrt{2} and (1,−1)/2(1, -1)/\sqrt{2}). Opposite signs give a hyperbola, X22−Y24=1\frac{X^2}{2} - \frac{Y^2}{4} = 1, opening along the line y=xy = x.

Exercise 3: Axes of an ellipse (code)

Find the semi-axes of 13x2+10xy+13y2=7213x^2 + 10xy + 13y^2 = 72 and their directions.

Answer

Semi-axis 3 along (1,−1)/2(1, -1)/\sqrt{2} and semi-axis 2 along (1,1)/2(1, 1)/\sqrt{2}.

Solution

A=[135513]A = \begin{bmatrix} 13 & 5 \\ 5 & 13 \end{bmatrix}, eigenvalues 8 (on (1,−1)(1, -1)) and 18 (on (1,1)(1, 1)). Semi-axes 72/8=3\sqrt{72/8} = 3 and 72/18=2\sqrt{72/18} = 2.

python · try it yourself
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 (−1,1)/2(-1, 1)/\sqrt{2}, the same axis as (1,−1)/2(1, -1)/\sqrt{2}.)

Exercise 4: Extremes on the circle

What are the maximum and minimum of 5x2−4xy+8y25x^2 - 4xy + 8y^2 on the unit circle x2+y2=1x^2 + y^2 = 1?

Answer

Maximum 9 at ±(1,−2)/5\pm(1, -2)/\sqrt{5}, minimum 4 at ±(2,1)/5\pm(2, 1)/\sqrt{5}.

Solution

From the worked example, the eigenvalues are 4 and 9 with those eigenvectors. Check: at (2,1)/5(2, 1)/\sqrt{5}, q=(5⋅4−4⋅2+8⋅1)/5=20/5=4q = (5 \cdot 4 - 4 \cdot 2 + 8 \cdot 1)/5 = 20/5 = 4.

Test yourself

The quadratic form 2x2+6xy+y22x^2 + 6xy + y^2 has symmetric matrix…

  1. [2601]\begin{bmatrix} 2 & 6 \\ 0 & 1 \end{bmatrix}
  2. [2331]\begin{bmatrix} 2 & 3 \\ 3 & 1 \end{bmatrix}
  3. [2661]\begin{bmatrix} 2 & 6 \\ 6 & 1 \end{bmatrix}
  4. [1332]\begin{bmatrix} 1 & 3 \\ 3 & 2 \end{bmatrix}

In principal axes, q(x)=36q(\mathbf{x}) = 36 becomes 4X2+9Y2=364X^2 + 9Y^2 = 36. The longer semi-axis has length…

  1. 2
  2. 4
  3. 3
  4. 9

A quadric xTAx=1\mathbf{x}^TA\mathbf{x} = 1 in 3D, where AA has eigenvalues 2, 1, −3, is…

  1. an ellipsoid
  2. a hyperboloid of one sheet
  3. a hyperboloid of two sheets
  4. empty