Search

Search papers and lessons

Quadratic Form

Level 2 · 9 min read

A quadratic form is a function of a vector that squares and multiplies its coordinates, written compactly as x transpose times a matrix times x, and it sets the shape of bowls and ellipses.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

TL;DR

A quadratic form takes a vector and returns a single number built only from squares and products of its coordinates. Two coordinates x1x_1 and x2x_2 give expressions such as 2x12+2x1x2+3x222x_1^2 + 2x_1x_2 + 3x_2^2. Written as x⊤A x\mathbf{x}^\top A\,\mathbf{x}, the whole expression is controlled by one matrix AA, and that matrix tells you the shape of the function.

The shape is the useful part. When all the values are positive, the graph is a bowl, and its level sets are ellipses.

Where you'll see it: in the squared length of an error vector in least squares, in the curvature of a loss surface at its minimum, and in the energy of a physical system near equilibrium.

Intuition

Squares and cross terms

Start with the simplest case. The function f(x)=x2f(x) = x^2 takes a number and squares it. It is a parabola, a bowl with its bottom at zero.

Now take two coordinates. The function f(x1,x2)=x12+x22f(x_1, x_2) = x_1^2 + x_2^2 is a bowl in three dimensions, round like a cereal bowl, because the two coordinates contribute in the same way. Change the coefficients to f=4x12+x22f = 4x_1^2 + x_2^2 and the bowl stretches: it is steeper in the x1x_1 direction. Add a cross term x1x2x_1 x_2 and the bowl tilts, so its axes are no longer lined up with the coordinate lines.

A quadratic form is any combination of these pieces: squares of each coordinate, and products of pairs of coordinates, with coefficients. Nothing linear is allowed, and there is no constant.

−2−1.5−1−0.500.511.5202468distance from the origin along an axisvalue of the form
  • x₁² + x₂², along either axis
  • 4x₁² + x₂², along the x₁ axis
Slicing a bowl along an axis gives a parabola. Making the coefficient 4 times larger makes the bowl 4 times steeper in that direction.

Why a matrix

Writing out every coefficient gets messy as the number of coordinates grows. A matrix holds all of them in one place. The diagonal entries carry the squares, and the off-diagonal entries carry the cross terms. Then one short expression, x⊤Ax\mathbf{x}^\top A \mathbf{x}, describes the whole function, and the rules of matrix algebra can be used on it.

The math

Definition

Definition: Quadratic form

For a Vector x=(x1,…,xn)⊤\mathbf{x} = (x_1, \dots, x_n)^\top in Rn\mathbb{R}^n and a square matrix AA with entries AijA_{ij}, the quadratic form is

q(x)=x⊤A x=∑i=1n∑j=1nAij xi xj.q(\mathbf{x}) = \mathbf{x}^\top A\, \mathbf{x} = \sum_{i=1}^{n} \sum_{j=1}^{n} A_{ij}\, x_i\, x_j.

Here x⊤\mathbf{x}^\top is the row version of x\mathbf{x} (the transpose), so the product x⊤Ax\mathbf{x}^\top A \mathbf{x} is a number, a 1×11 \times 1 matrix.

Read the double sum in words. Multiply every entry AijA_{ij} by the product xixjx_i x_j, then add everything up. A diagonal term Aiixi2A_{ii} x_i^2 is a square. An off-diagonal pair AijxixjA_{ij} x_i x_j and AjixjxiA_{ji} x_j x_i together give a cross term.

Only the symmetric part matters

Two different matrices can give the same quadratic form. Since xixj=xjxix_i x_j = x_j x_i, the entries AijA_{ij} and AjiA_{ji} only ever appear added together. So you can replace AA by its symmetric version

S=12(A+A⊤),S = \tfrac{1}{2}\left(A + A^\top\right),

which has the same quadratic form. A matrix equal to its own transpose is a Symmetric Matrix. From now on we assume AA is symmetric, because that is the case where the shape results hold.

Expanding a small example

Take A=(2113)A = \begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix}, which is symmetric. Then

x⊤Ax=2x12+x1x2+x2x1+3x22=2x12+2x1x2+3x22.\mathbf{x}^\top A \mathbf{x} = 2x_1^2 + x_1x_2 + x_2x_1 + 3x_2^2 = 2x_1^2 + 2x_1x_2 + 3x_2^2.

The off-diagonal 1s combine into a cross term with coefficient 2.

Gradient

The rate of change of qq in each direction is collected in the Gradient (the vector of partial derivatives, which points uphill). For a symmetric AA,

∇q(x)=2Ax.\nabla q(\mathbf{x}) = 2A\mathbf{x}.

Check it on the example: ∂q/∂x1=4x1+2x2\partial q/\partial x_1 = 4x_1 + 2x_2 and ∂q/∂x2=2x1+6x2\partial q/\partial x_2 = 2x_1 + 6x_2, and these two numbers are the entries of 2Ax2A\mathbf{x}. This is why quadratic forms are so common in optimization. The gradient is linear in x\mathbf{x}, and it is zero exactly where Ax=0A \mathbf{x} = \mathbf{0}.

Definiteness: the shape

A symmetric matrix AA is classified by the sign of qq over all nonzero vectors:

  • Positive definite: x⊤Ax>0\mathbf{x}^\top A\mathbf{x} \gt 0 for every x≠0\mathbf{x} \neq \mathbf{0}. The graph is a bowl with its only minimum at zero. Level sets {x:q(x)=c}\{\mathbf{x} : q(\mathbf{x}) = c\} are ellipses.
  • Positive semidefinite: x⊤Ax≥0\mathbf{x}^\top A\mathbf{x} \ge 0 for all x\mathbf{x}, with equality for some nonzero vectors. The bowl has a flat valley.
  • Indefinite: qq takes both signs. The graph is a saddle.

A symmetric matrix is positive definite (see Positive Definite Matrix) exactly when all of its eigenvalues are positive. The eigenvalues also give the axes of the ellipses: long axes go with small eigenvalues. For a loss function LL, the matrix of second derivatives (the Hessian) plays the role of AA near a minimum: LL looks like a quadratic form there, which is the curvature mentioned in the TL;DR.

Worked examples

Example
1. A two-variable form, by hand

Let A=(2113)A = \begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix} and x=(1,2)⊤\mathbf{x} = (1, 2)^\top.

  1. First compute AxA\mathbf{x}: the first entry is 2×1+1×2=42 \times 1 + 1 \times 2 = 4, the second is 1×1+3×2=71 \times 1 + 3 \times 2 = 7. So Ax=(4,7)⊤A\mathbf{x} = (4, 7)^\top.
  2. Then take the Dot Product with x\mathbf{x}: 1×4+2×7=181 \times 4 + 2 \times 7 = 18.
  3. Check with the expanded form 2x12+2x1x2+3x222x_1^2 + 2x_1x_2 + 3x_2^2: 2×1+2×1×2+3×4=2+4+12=182 \times 1 + 2 \times 1 \times 2 + 3 \times 4 = 2 + 4 + 12 = 18.

Interpretation: both routes agree. The matrix route is shorter, and it generalises to any number of coordinates.

Example
2. The standard case: a diagonal matrix and its ellipses

Let A=(2008)A = \begin{pmatrix} 2 & 0 \\ 0 & 8 \end{pmatrix}, so q(x)=2x12+8x22q(\mathbf{x}) = 2x_1^2 + 8x_2^2. With no cross term, the axes line up with the coordinates.

  1. Evaluate at x=(1,1)\mathbf{x} = (1, 1): q=2+8=10q = 2 + 8 = 10.
  2. Find the level set q=1q = 1. Solve 2x12+8x22=12x_1^2 + 8x_2^2 = 1. This is an ellipse with x1x_1 reaching ±1/2≈±0.707\pm 1/\sqrt{2} \approx \pm 0.707 and x2x_2 reaching ±1/8≈±0.354\pm 1/\sqrt{8} \approx \pm 0.354.
  3. Check one point on the ellipse: x1=1/2x_1 = 1/\sqrt{2}, x2=0x_2 = 0 gives 2×12=12 \times \tfrac{1}{2} = 1.
  4. Both diagonal entries are positive, so q>0q \gt 0 for every nonzero vector, and AA is positive definite.

Interpretation: the entry 8 is four times the entry 2, so the bowl is four times steeper in the x2x_2 direction. Its ellipses are narrower in x2x_2. The diagonal entries are the eigenvalues here, and the level set's axes are 1/λ1/\sqrt{\lambda} for each eigenvalue λ\lambda.

Example
3. A realistic case: the squared error of a fit

Least squares (Linear Regression) fits a line or plane to data. Suppose the data matrix XX has one row per example, here rows (1,0)(1, 0) and (1,1)(1, 1), so X=(1011)X = \begin{pmatrix} 1 & 0 \\ 1 & 1 \end{pmatrix}. For a weight vector w\mathbf{w}, the predictions are XwX\mathbf{w}, and the squared length of the predictions (the sum of their squares) is ∥Xw∥2\|X\mathbf{w}\|^2. The squared error of a fit has the same shape, with the targets subtracted first.

  1. Build M=X⊤XM = X^\top X. Each entry is a dot product of two columns of XX. The columns are (1,1)(1, 1) and (0,1)(0, 1). Column 1 with itself gives 1+1=21 + 1 = 2; column 1 with column 2 gives 0+1=10 + 1 = 1 (twice, by symmetry); column 2 with itself gives 0+1=10 + 1 = 1. So M=(2111)M = \begin{pmatrix} 2 & 1 \\ 1 & 1 \end{pmatrix}.
  2. Then ∥Xw∥2=w⊤X⊤Xw=w⊤Mw\|X\mathbf{w}\|^2 = \mathbf{w}^\top X^\top X \mathbf{w} = \mathbf{w}^\top M \mathbf{w}, which is a quadratic form.
  3. Test w=(1,−1)\mathbf{w} = (1, -1): Xw=(1×1+0×(−1),1×1+1×(−1))=(1,0)X\mathbf{w} = (1 \times 1 + 0 \times (-1), 1 \times 1 + 1 \times (-1)) = (1, 0), so the squared length is 11.
  4. Through the form: 2×1+2×1×(−1)+1×1=2−2+1=12 \times 1 + 2 \times 1 \times (-1) + 1 \times 1 = 2 - 2 + 1 = 1. The two routes agree.
  5. The form is never negative, because it is a squared length. Its values are zero only when Xw=0X\mathbf{w} = \mathbf{0}, and here that happens only at w=0\mathbf{w} = \mathbf{0}, so the matrix is positive definite.

Interpretation: the squared size of a linear model's output, as a function of the weights, is a bowl, and X⊤XX^\top X sets how steep it is in each direction. The same matrix controls the bowl of the squared error, whose bottom is the least-squares answer.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Matrix is not symmetricEigenvalue and ellipse rules do not apply directlyReplace AA by 12(A+A⊤)\tfrac{1}{2}(A + A^\top) first
Zero eigenvalue or zero directionThe bowl has a flat valley, so the minimum is not uniqueAdd a small multiple of the identity (regularisation)
Negative eigenvalueThe form is a saddle, not a bowl; it has no minimum and Gradient Descent can run off along the negative directionCheck definiteness before relying on a minimum
Large dimensionStoring and inverting AA becomes expensiveUse factorisations or iterative solvers

History

Quadratic forms are among the oldest objects in algebra. Their study goes back to the classification of conic sections and then to the theory of binary forms in the nineteenth century, where mathematicians asked which integers a form can represent. The modern matrix view grew out of that work. The link between definiteness and eigenvalues, the basis of the shape results above, was worked out as linear algebra matured in the nineteenth and early twentieth centuries. Quadratic forms caught on in statistics and optimization because squared errors are quadratic forms.

Recap

A quadratic form x⊤Ax\mathbf{x}^\top A \mathbf{x} collects squares and cross terms into one matrix. Only the symmetric part of AA matters. Its gradient is 2Ax2A\mathbf{x}. Its sign pattern decides the shape: positive definite gives a bowl and ellipses, indefinite gives a saddle. This shape controls how easy an optimization problem is, which is why the Gradient and definiteness checks keep reappearing in later lessons.

Check yourself

QuestionRecall: what is the difference between a quadratic form and a linear function of a vector?Show answer

A linear function a⊤x\mathbf{a}^\top \mathbf{x} has only first powers of the coordinates. A quadratic form x⊤Ax\mathbf{x}^\top A \mathbf{x} has products of pairs of coordinates, so it includes squares and cross terms. That extra degree is what makes its graph a bowl or a saddle rather than a flat plane.

QuestionComputation: for A=(1225)A = \begin{pmatrix} 1 & 2 \\ 2 & 5 \end{pmatrix} and x=(3,−1)⊤\mathbf{x} = (3, -1)^\top, what is x⊤Ax\mathbf{x}^\top A \mathbf{x}?Show answer

First Ax=(1×3+2×(−1),2×3+5×(−1))=(1,1)A\mathbf{x} = (1 \times 3 + 2 \times (-1), 2 \times 3 + 5 \times (-1)) = (1, 1). Then x⊤(1,1)⊤=3×1+(−1)×1=2\mathbf{x}^\top (1, 1)^\top = 3 \times 1 + (-1) \times 1 = 2. Check: 1⋅9+4⋅3⋅(−1)+5⋅1=9−12+5=21 \cdot 9 + 4 \cdot 3 \cdot (-1) + 5 \cdot 1 = 9 - 12 + 5 = 2.

QuestionWhy: why does replacing AA by its symmetric part leave the quadratic form unchanged?Show answer

Because xixj=xjxix_i x_j = x_j x_i, the entries AijA_{ij} and AjiA_{ji} always multiply the same product and only their sum matters. The symmetric part 12(A+A⊤)\tfrac{1}{2}(A + A^\top) has the same sum for each pair, so it produces the same value for every vector.

QuestionApplied: a loss function is L(w)=w⊤MwL(\mathbf{w}) = \mathbf{w}^\top M \mathbf{w} with MM having eigenvalues 4 and −1-1. Is the origin a minimum, and why does it matter for gradient descent?Show answer

No. A negative eigenvalue means LL takes negative values along one direction, so the form is indefinite and the origin is a saddle point, not a minimum. Along the eigenvector for −1-1, LL decreases without limit, so gradient descent started anywhere with a component along that direction runs away instead of settling. A minimum is only guaranteed when all the eigenvalues are positive.

Sources