Contents · Matrix Calculus & Autodiff
Part 0 · Matrix Calculus
- Matrix Calculus: Gradients and Jacobians
Part 1 · Automatic Differentiation
Part 2 · Backpropagation
Part 3 · Differentiable Simulation
Part 0 · Matrix Calculus
Matrix Calculus: Gradients and Jacobians
Calculus on scalars asks how one output moves when one input moves. Vector calculus asks the same of many outputs and many inputs at once, and packs the answer into a matrix. That matrix, the Jacobian, is the derivative: it is the best linear map that copies the behavior of a nonlinear function near a point. Every dynamics linearization, policy gradient, and differentiable rollout is an instance of assembling and multiplying these matrices.
Gradients and Jacobians
For a scalar field the derivative is the gradient, the column of partials . For a vector field each output has its own gradient, and stacking them by row gives the Jacobian
Row is ; column is , the direction the whole output moves when input moves. Two conventions coexist. The numerator layout above sends outputs to rows, so . The denominator layout transposes this, putting inputs on rows, so a scalar gradient comes out as a column. The math is identical; only the shapes and the order of factors in the chain rule flip. Pick one and hold it fixed.1
The Jacobian is the best local linear map
Near a point the first-order Taylor expansion is
So is the linear map that best reproduces for small displacements: the derivative is not a number attached to a point, it is a matrix. Feed it the basis vector and you recover column , confirming that columns are the responses to unit input moves.
Because is a linear map, its geometry is the SVD story. Write . A small sphere of perturbations around is sent to an ellipsoid around whose semi-axes are : the singular values are the local stretch factors and is the local volume scale.1 The right singular vectors mark the input directions that stretch most and least; each maps to .
The chain rule is matrix multiplication
Compose . The Jacobian of the composition is the product of the Jacobians, in output-to-input order,
This one identity is the whole of backpropagation and of gradient flow through a simulator: a deep network or a -step rollout is a long composition, and its Jacobian is the product of the per-layer or per-step Jacobians.2 Forward mode multiplies this product right to left onto an input perturbation; reverse mode multiplies left to right onto an output cotangent, which is why reverse mode is cheap when there are few outputs, such as a scalar loss.
A worked 2×2 example
Take . Differentiating each component,
Verify column one against a central finite difference with . The column is , so perturb only at the point :
The finite difference reproduces the analytic column to the digits shown, with error . This is exactly the check the demo runs live for every point.
- det J
- 4.00
- σ₁
- 2.83
- σ₂
- 1.41
Where these Jacobians show up
Linearizing continuous dynamics gives the pair and ; LQR and iLQR consume exactly these Jacobians at each nominal point to build their local quadratic models. In reinforcement learning the policy gradient is a Jacobian of sampled returns with respect to parameters, and value-function fitting rides the Jacobian of the critic. Differentiable simulation multiplies per-step Jacobians through a rollout so a loss can be pushed back onto controls, masses, or contact parameters.2 In every case the object being assembled is the same matrix of partials, and the chain rule is the same matrix product.
Footnotes
-
Petersen & Pedersen, The Matrix Cookbook (2012). ↩ ↩2
-
Baydin et al., Automatic Differentiation in Machine Learning: a Survey (2018). ↩ ↩2