← Module 05 / Calculus & optimization

Lesson 05.5 · §4.5

Why does a quadratic gradient contain a transpose?

Derive linear and quadratic gradients, including nonsymmetric matrices.

In this lesson 4 ideas
  1. Differentiate every occurrence of a coordinate
  2. Derive the rules from entries
  3. Transfer to fitting
  4. Explore the relationship

Source scope: §4.5, PDF pages 28. Numerical examples, experiments, added derivations, and practice are teaching supplements unless identified as source results.

Differentiate every occurrence of a coordinate

Take A=[1234]A=\begin{bmatrix}1&2\\3&4\end{bmatrix}. The quadratic expression is q(x)=x12+5x1x2+4x22q(x)=x_1^2+5x_1x_2+4x_2^2, so

∇q(x)=[2x1+5x25x1+8x2]=(A+AT)x.\nabla q(x)=\begin{bmatrix}2x_1+5x_2\\5x_1+8x_2\end{bmatrix}=(A+A^T)x.

Writing 2Ax2Ax would give the wrong answer because AA is not symmetric. Each coordinate appears on both sides of AA in xTAxx^TAx.

The source gives two rules, for constant a∈Rna\in\mathbb R^n, constant square A∈Rn×nA\in\mathbb R^{n\times n}, and x∈Rnx\in\mathbb R^n:

∇x(aTx)=a,∇x(xTAx)=(A+AT)x.\nabla_x(a^Tx)=a,\qquad \nabla_x(x^TAx)=(A+A^T)x.

If A=ATA=A^T, the second becomes 2Ax2Ax. The square shape is necessary for the scalar expression xTAxx^TAx to be defined.

Derive the rules from entries

The source states these formulas; this derivation is a teaching supplement. For the linear rule, differentiate aTx=∑iaixia^Tx=\sum_i a_ix_i with respect to xkx_k to get aka_k.

For the quadratic rule, start from Module 4’s identity:

∂∂xk∑i,jAijxixj=∑jAkjxj+∑iAikxi=[Ax]k+[ATx]k.\frac{\partial}{\partial x_k}\sum_{i,j}A_{ij}x_ix_j =\sum_j A_{kj}x_j+\sum_i A_{ik}x_i =[Ax]_k+[A^Tx]_k.

One sum differentiates the left occurrence, the other the right. The diagonal term contributes twice, as it should for Akkxk2A_{kk}x_k^2. Assemble the coordinates to get (A+AT)x(A+A^T)x.

At x=(1,−1)x=(1,-1) the running example gives (−3,−3)T(-3,-3)^T. Check the first coordinate independently by the centered difference [q(x+ϵe1)−q(x−ϵe1)]/(2ϵ)[q(x+\epsilon e_1)-q(x-\epsilon e_1)]/(2\epsilon). With ϵ=0.001\epsilon=0.001, it equals −3-3 up to numerical rounding. A finite-difference check is evidence, not a proof, and tiny steps can suffer cancellation in floating-point arithmetic.

Transfer to fitting

Teaching supplement. For L(x)=∥Bx−b∥22L(x)=\|Bx-b\|_2^2, expand to xTBTBx−2bTBx+bTbx^TB^TBx-2b^TBx+b^Tb. The constant has zero derivative and BTBB^TB is symmetric, yielding

∇L(x)=2BT(Bx−b).\nabla L(x)=2B^T(Bx-b).

Here BB may be rectangular: the square matrix in the quadratic rule is BTBB^TB. If BB is m×nm\times n, the gradient has nn entries, matching the parameter vector.

Explore the relationship

Explore it yourself

Check a gradient independently

Does 2Ax still work when A is not symmetric?

Preparing interactive controls…

A
12
34

q(x) = 0; (A + Aᵀ)x = (-3, -3).

Centered finite difference (ε = 0.001) = (-3, -3); error norm = 0.

2Ax = (-2, -2). The transpose term accounts for the second occurrence of each coordinate.

q = x₁² + 5x₁x₂ + 4x₂². Differentiate each term and compare the resulting two entries with the matrix formula. The numerical check evaluates q at four neighboring points; it does not reuse the analytic gradient.