AI Knowledge Tree

Quadratic forms & positive definiteness

Read a quadratic form through the spectrum of its matrix, and use eigenvalue signs to test positive (semi)definiteness.

Loss surfaces, covariance matrices, and curvature in optimization all wear the same algebraic dress: a quadratic form. Reading the signs of the eigenvalues tells you whether the bowl opens up everywhere, flattens out, or has saddle directions.

Definition

A quadratic form in x∈Rnx \in \mathbb{R}^n is a scalar function[1]

q(x)=x⊤Ax=∑i,jaijxixj,q(x) = x^\top A x = \sum_{i,j} a_{ij} x_i x_j,

where A∈Rn×nA \in \mathbb{R}^{n \times n}. Without loss of generality, AA can always be taken symmetric: x⊤Ax=x⊤A+A⊤2xx^\top A x = x^\top \frac{A + A^\top}{2} x, so replace AA by its symmetrization. The cross-term aijxixj+ajixjxi=(aij+aji)xixja_{ij} x_i x_j + a_{ji} x_j x_i = (a_{ij} + a_{ji}) x_i x_j shows that only the symmetric part matters.

For n=2n = 2:

q(x)=a11x12+2a12x1x2+a22x22.q(x) = a_{11}x_1^2 + 2a_{12}x_1 x_2 + a_{22}x_2^2.

The level sets q(x)=cq(x) = c are ellipses (when the form is definite) or hyperbolas (when it is indefinite).

Definiteness

A symmetric matrix AA is called:

TermConditionEigenvalues
Positive definite (PD)x⊤Ax>0x^\top A x > 0 for all x≠0x \neq 0All λi>0\lambda_i > 0
Positive semidefinite (PSD)x⊤Ax≥0x^\top A x \ge 0 for all xxAll λi≥0\lambda_i \ge 0
Negative definite (ND)x⊤Ax<0x^\top A x < 0 for all x≠0x \neq 0All λi<0\lambda_i < 0
IndefiniteBoth signs occurMixed signs

Proof that eigenvalue signs determine definiteness. By the spectral theorem A=QΛQ⊤A = Q\Lambda Q^\top. Substitute x=Qyx = Qy (bijective since QQ is invertible):

x⊤Ax=y⊤Q⊤AQy=y⊤Λy=∑iλiyi2.x^\top A x = y^\top Q^\top A Q y = y^\top \Lambda y = \sum_i \lambda_i y_i^2.

If all λi>0\lambda_i > 0, the sum is positive whenever y≠0y \neq 0, i.e. whenever x≠0x \neq 0. If some λi<0\lambda_i < 0, choose y=eiy = e_i (so x=Qei=qix = Qe_i = q_i) to get q(qi)=λi<0q(q_i) = \lambda_i < 0. The sign pattern of the eigenvalues is exactly the definiteness. ■\qquad\blacksquare

Contours of the quadratic form xᵀAx: the signs of the eigenvalues decide the shape: a bowl (positive definite), a saddle (indefinite), or a dome (negative definite).
Contours of the quadratic form xᵀAx: the signs of the eigenvalues decide the shape: a bowl (positive definite), a saddle (indefinite), or a dome (negative definite).

Covariance matrices are PSD

If X∈Rn×dX \in \mathbb{R}^{n \times d} is a data matrix (rows are observations), the sample covariance Σ=1nX⊤X\Sigma = \frac{1}{n} X^\top X (after centering) satisfies

v⊤Σv=1nv⊤X⊤Xv=1n∥Xv∥2≥0,v^\top \Sigma v = \frac{1}{n} v^\top X^\top X v = \frac{1}{n} \lVert Xv\rVert^2 \ge 0,

so Σ\Sigma is PSD. Its eigenvalues are the squared singular values of 1nX\frac{1}{\sqrt{n}} X, and they are nonnegative.

Sylvester's criterion (practical test)

For a symmetric AA, positive definiteness is equivalent to all leading principal minors being positive[2]. The kk-th leading principal minor is the determinant of the top-left k×kk \times k block of AA:

Δk=det⁡A1:k, 1:k>0for k=1,…,n.\Delta_k = \det A_{1:k,\, 1:k} > 0 \quad \text{for } k = 1, \dots, n.

This gives an O(n3)O(n^3) test (compute nn determinants, each O(k3)O(k^3), amortized via Cholesky: if Cholesky succeeds, the matrix is PD).

Check it on arrays

python · numpy
import numpy as np

# PD matrix: A = Q diag(3,1) Q^T, eigenvalues 3 and 1
theta = np.pi / 6
Q = np.array([[np.cos(theta), -np.sin(theta)],
              [np.sin(theta),  np.cos(theta)]])
A = Q @ np.diag([3.0, 1.0]) @ Q.T

# Visualize q(x) on a grid and confirm q > 0 everywhere
x1, x2 = np.meshgrid(np.linspace(-1, 1, 40), np.linspace(-1, 1, 40))
X = np.stack([x1.ravel(), x2.ravel()])
q_vals = np.einsum("ij,jk,ik->i", X.T, A, X.T)   # batched x^T A x
print("min quadratic form value:", q_vals.min())    # > 0 (PD)

# Indefinite matrix: eigenvalues 3 and -1
A_indef = Q @ np.diag([3.0, -1.0]) @ Q.T
q_indef = np.einsum("ij,jk,ik->i", X.T, A_indef, X.T)
print("indefinite: min =", q_indef.min(), "max =", q_indef.max())  # neg and pos

# Cholesky as PD test
try:
    np.linalg.cholesky(A)
    print("A is PD: Cholesky succeeded")
except np.linalg.LinAlgError:
    print("not PD")

try:
    np.linalg.cholesky(A_indef)
    print("A_indef is PD")
except np.linalg.LinAlgError:
    print("A_indef is not PD: Cholesky failed")

The PD matrix's quadratic form stays positive; the indefinite one has both signs. Cholesky succeeds only for PD.

Exercises

Exercise 1. Classify A=(2112)A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} by computing its eigenvalues, then confirm the classification with Sylvester's criterion.

Solution

Step 1. Solve det⁡(A−λI)=0\det(A - \lambda I) = 0. The characteristic polynomial is (2−λ)2−1=0(2 - \lambda)^2 - 1 = 0, so 2−λ=±12 - \lambda = \pm 1, giving λ=3\lambda = 3 and λ=1\lambda = 1. Both eigenvalues are positive, so AA is positive definite.

Step 2. Check the leading principal minors. The first is Δ1=a11=2>0\Delta_1 = a_{11} = 2 > 0; the second is Δ2=det⁡A=2⋅2−1⋅1=3>0\Delta_2 = \det A = 2\cdot 2 - 1\cdot 1 = 3 > 0. Both are positive, so Sylvester's criterion confirms positive definiteness.

The two tests agree: the eigenvalue signs and the positivity of the leading minors both certify a bowl that opens upward in every direction.

Exercise 2. Prove that if AA is positive definite then AA is invertible, and that A−1A^{-1} is also positive definite.

Solution

Step 1 (invertible). By the spectral theorem A=QΛQ⊤A = Q\Lambda Q^\top with all λi>0\lambda_i > 0. The determinant is det⁡A=∏iλi>0\det A = \prod_i \lambda_i > 0, so AA is invertible. (Equivalently, Ax=0Ax = 0 would give x⊤Ax=0x^\top A x = 0, forcing x=0x = 0 by definiteness, so the null space is trivial.)

Step 2 (inverse is PD). Invert the spectral form: A−1=QΛ−1Q⊤A^{-1} = Q\Lambda^{-1}Q^\top, where Λ−1\Lambda^{-1} has diagonal entries 1/λi1/\lambda_i. Since each λi>0\lambda_i > 0, each 1/λi>01/\lambda_i > 0, so A−1A^{-1} has all-positive eigenvalues and is positive definite.

A PD matrix and its inverse share eigenvectors and have reciprocal, still-positive, eigenvalues.

Exercise 3 (code). Generate a random symmetric matrix, shift it by μI\mu I until it becomes positive definite, and confirm that Cholesky succeeds exactly when the smallest eigenvalue turns positive.

Solution
python · numpy
import numpy as np

rng = np.random.default_rng(5)
M = rng.normal(size=(6, 6))
S = (M + M.T) / 2                    # symmetric, generally indefinite

eigs = np.linalg.eigvalsh(S)
lam_min = eigs.min()

def is_pd(A):
    try:
        np.linalg.cholesky(A)
        return True
    except np.linalg.LinAlgError:
        return False

# Shift just below and just above the threshold -lam_min.
I = np.eye(6)
below = S + (-lam_min - 0.01) * I    # smallest eigenvalue still negative
above = S + (-lam_min + 0.01) * I    # smallest eigenvalue now positive

assert not is_pd(below)
assert is_pd(above)
print("min eigenvalue of S:", round(float(lam_min), 4),
      "-> PD once shifted past", round(float(-lam_min), 4))

Cholesky fails while the shifted matrix still has a negative eigenvalue and succeeds the moment the shift makes every eigenvalue positive, which is the spectral characterization of positive definiteness at work.

The spectral decomposition of a rectangular matrix, where the same geometry extends to non-square transforms, is the SVD of the next lesson.


References

  1. [1]Horn, R. A., & Johnson, C. R. (2012). Matrix Analysis (2nd ed.). Cambridge University Press. Quadratic forms and the classification of definiteness by eigenvalue sign.↩
  2. [2]Golub, G. H., & Van Loan, C. F. (2013). Matrix Computations (4th ed.). Johns Hopkins University Press. Positive definiteness, Sylvester's criterion, and Cholesky as a test (doi:10.56021/9781421407944).↩