Matrix Calculus
Matrix calculus extends derivatives to vectors, matrices, and tensor-valued computations. It is the notation behind gradients, Jacobians, neural network layers, and coordinate updates.
For a function , the Jacobian is:
See Jacobian and Hessian for the relationship between Jacobians, Hessians, curvature, and automatic differentiation products.
For a linear map:
the Jacobian with respect to is:
For a scalar loss and parameters , optimization uses:
Key Ideas
- Gradients are for scalar outputs; Jacobians are for vector outputs.
- Backpropagation efficiently computes vector-Jacobian products without materializing every Jacobian.
- Shape tracking is part of understanding matrix calculus in code.
- Coordinate-based models often need gradients with respect to both features and positions.
Shape Contract
Always record the derivative target, source, and shape:
| Expression | Shape | Meaning |
|---|---|---|
| input vector | ||
| vector output | ||
| local linear map from input changes to output changes | ||
| scalar | training loss or objective | |
| direction of input sensitivity | ||
| same as | parameter update signal |
For a composition:
the chain rule is:
The order of multiplication depends on the row/column convention, so shape checking is the safest way to read equations.
Common Layer Derivatives
For a batch linear layer:
with and :
and:
These equations are useful when debugging tensor shapes, custom layers, or paper notation.
Autograd Products
Most frameworks compute products, not full derivative matrices:
| Product | Notation | Common Use |
|---|---|---|
| VJP | reverse-mode backpropagation | |
| JVP | forward sensitivity, neural ODEs, implicit methods | |
| HVP | curvature diagnostics and second-order methods |
If a paper says it uses a Jacobian or Hessian, check whether it materializes the matrix or only uses a product estimator.
Coordinate Models
For coordinate outputs , derivatives can be taken with respect to:
| Source | Example |
|---|---|
| parameters | for training |
| coordinates | for forces or refinement |
| time | for ODE or flow models |
| noise | sensitivity of generated coordinates to latent variables |
The derivative should respect the same symmetry contract as the model output.
Constrained Variables
Some variables do not live in unconstrained Euclidean space. Examples include probability simplexes, unit vectors, rotation matrices, valid molecular graphs, and restrained coordinate sets.
For a feasible set :
an update may require projection:
or a reparameterization:
This distinction is important when a paper claims valid molecules, normalized probabilities, rotations, or geometry constraints.
Practical Checks
- What are the input and output shapes?
- Is the derivative a scalar, vector, matrix, Jacobian, or vector-Jacobian product?
- Are tensors treated as row vectors or column vectors in the notation?
- Does the implementation detach or stop gradients through part of the computation?
- Is the derivative with respect to inputs, parameters, coordinates, or time?
- Is the variable unconstrained, projected, or reparameterized?
- Does the paper need an exact derivative matrix, or only an autograd product?