Common Matrix-Calculus Identities (xᵀAx, tr, log det, inverse)
A short list of identities covers most of machine learning: quadratic forms, traces, log-determinants and inverses. Derive each from differentials and check it numerically. Then use them to fit Gaussian covariances, tune Gaussian-process kernels, and understand normalizing flows and Kalman filters.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
A cheat sheet for matrix derivatives
Just as you memorize 7×8=56 instead of counting every time, engineers keep a short list of matrix-derivative facts. With about six of them, you can derive the training rules for most machine-learning models. This page builds that cheat sheet, and the demo checks each entry with real numbers.
Two of the stars are the log-determinant and the matrix inverse, which are the matrix versions of lnx and 1/x.
Matrix-calculus explorer: formula vs finite differences
shapes: A: 2×2, x: 2
rule: ∇f = (A + Aᵀ) x (= 2Ax if A is symmetric) · the gradient has the same shape as the input
input (x or X) · 2×1
-1.100
1.600
closed-form derivative · 2×1
1.920
1.810
finite-difference check · 2×1
1.920
1.810
largest difference: 3.4e-10 ✔ the formula is right
Only the symmetric part of A matters (A2.4-2).
Why engineers care
When a robot learns how noisy its sensors are, it fits a covariance matrix to data. The formula that does this comes straight from two cheat-sheet identities: the derivative of log det and the derivative of the inverse.
📝 Quick check (Level 0)
0/3 answered
1. det of diag(2, 3)?
2. The matrix inverse is like the number operation…
3. Log det is the matrix cousin of…
Level 1: Beginner
★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.
The cheat sheet
Function
Derivative
Scalar analogue
a⊤x
a
(ax)′=a
x⊤Ax
(A+A⊤)x
(ax2)′=2ax
tr(AX)
A⊤
(ax)′=a
tr(X⊤AX)
(A+A⊤)X
(ax2)′=2ax
logdetX
X−⊤
(lnx)′=1/x
X−1
d(X−1)=−X−1dXX−1
(1/x)′=−1/x2
detX
det(X)X−⊤
Jacobi’s formula
Gradients have the same shape as the variable. The scalar analogues are a memory aid and a sanity check.
📝 Quick check (Level 1)
0/3 answered
1. ∂ log det X/∂X at X = diag(2, 4): the (2, 2) entry?
2. ∂tr(AX)/∂X = ?
3. The scalar analogue of d(X⁻¹) = −X⁻¹dX X⁻¹ is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.
Deriving the identities with differentials
Inverse. Differentiate XX−1=I: dXX−1+Xd(X−1)=0, so d(X−1)=−X−1dXX−1.
Determinant (Jacobi’s formula).ddetX=detX⋅tr(X−1dX), proved at Level 3. So dlogdetX=tr(X−1dX)=⟨X−⊤,dX⟩, which gives ∂logdetX/∂X=X−⊤.
Trace of an inverse.dtr(X−1)=−tr(X−1dXX−1)=−tr(X−2dX), so ∂/∂X=−(X−2)⊤.
Application: the Gaussian covariance MLE
For n zero-mean samples with sample covariance S, the log-likelihood is ℓ(Σ)=−2n(logdetΣ+tr(Σ−1S)). Using the log det and inverse identities:
∂Σ∂ℓ=−2n(Σ−1−Σ−1SΣ−1)=0⟹Σ=S
The maximum-likelihood covariance is the sample covariance. This is how robots fit their sensor-noise models.
🐍 Exercise 1: log det and inverse derivatives
Implement ∂ log det X/∂X = X⁻ᵀ and the directional derivative of the inverse, and check both against finite differences.
📝 Quick check (Level 2)
0/3 answered
1. The MLE of a Gaussian covariance is…
2. d(X⁻¹) at X = 2I with dX = I: the (1, 1) entry?
3. Jacobi: d det X = ?
Level 3: Advanced
★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.
Proof of Jacobi’s formula
det(X+dX)=detX⋅det(I+X−1dX). For small E=X−1dX, det(I+E)=∏(1+μi)=1+∑μi+O(∥E∥2)=1+trE+⋯, where μi are the eigenvalues of E. So ddetX=detXtr(X−1dX). ∎
Eigenvalue derivatives
For symmetric A with a simple eigenvalue λi and unit eigenvector qi: dλi=qi⊤dAqi, the first-order perturbation formula of A2.4-4. In particular, ∂λmax/∂A=q1q1⊤, used in spectral-norm regularization.
Concavity of log det
Along X+tD (symmetric D, PD X), dt2d2logdet=−tr((X−1D)2)≤0. log det is concave on positive-definite matrices. That is why maximum-likelihood covariance and precision estimation (including the graphical lasso) are well-behaved convex problems.
Exercise 3.1: Show that ∂ det X/∂X = det(X) X⁻ᵀ, and check it on diag(2, 3)
From Jacobi, ddetX=detX⟨X−⊤,dX⟩. For X=diag(2,3): 6⋅diag(1/2,1/3)=diag(3,2). Check: ∂(x11x22)/∂x11=x22=3. ✓
📝 Quick check (Level 3)
0/3 answered
1. log det on PD matrices is…
2. ∂λ_max/∂A (simple eigenvalue) = ?
3. det(I + E) ≈ ?
Level 4: Expert
★★★★★ Expert⏱ 45 minResearchers: original papers and open problems.
Tuning Gaussian processes by gradient
A Gaussian process’s evidence is logp(y)=−21y⊤K−1y−21logdetK−2nlog2π. With the cheat sheet, its gradient with respect to any kernel hyperparameter θ is
∂θ∂logp=21tr((αα⊤−K−1)∂θ∂K),α=K−1y
(Rasmussen and Williams, 2006). This is how GPs used for robot dynamics models and Bayesian optimization learn their length-scales.
🐍 Exercise 2 (research): learn a Gaussian process length-scale
Code the GP log marginal likelihood and its analytic gradient with respect to the RBF length-scale ℓ. Gradient-check it, then run gradient ascent to recover ℓ ≈ 0.5 from data generated with 0.5.
Where these identities appear
Normalizing flows and change of variables: log∣detJ∣ terms and their gradients (A3.2-5). Architectures make J triangular so that logdet is a cheap sum.
Kalman filters and SLAM: covariance updates P+=(I−KH)P and information-form updates rely on inverse and determinant identities. Derivatives of the log-likelihood with respect to noise parameters let robots learn their noise models (Barfoot, 2017).
Sparse precision estimation: the graphical lasso maximizes logdetΘ−tr(SΘ)−ρ∥Θ∥1 (Friedman, Hastie and Tibshirani, 2008). Concavity of log det makes it tractable.
Gaussian policies in RL: the gradient of logN(a;μ,Σ) with respect to Σ uses exactly these two identities.
References
C. G. J. Jacobi (1841). “De formatione et proprietatibus determinantium.” Journal für die reine und angewandte Mathematik, 22.
K. B. Petersen, M. S. Pedersen (2012). The Matrix Cookbook.
C. Rasmussen, C. Williams (2006). Gaussian Processes for Machine Learning. MIT Press. Chapter 5.
J. Friedman, T. Hastie, R. Tibshirani (2008). “Sparse inverse covariance estimation with the graphical lasso.” Biostatistics, 9(3).
T. Barfoot (2017). State Estimation for Robotics. Cambridge University Press.
J. Magnus, H. Neudecker (2019). Matrix Differential Calculus, 3rd ed. Wiley.
Each identity mirrors a scalar rule (1/x, lnx, ax2), which is a quick sanity check.
Practice
Show:
Level 0
Problem 1Level 0
Does a cheat sheet of derivative rules save time compared with re-deriving each time? (yes/no)
Yes.
Problem 2Level 0
detdiag(2,3)?
6.
Problem 3Level 0
The inverse of the number 2?
0.5.
Level 1
Problem 4Level 1
∇(x⊤Ax) with A=I at (1,2): the second component?
2x, so 4.
Problem 5Level 1
∂tr(AX)/∂X with A=(1324): the (2,1) entry?
(A⊤)21=A12=2.
Problem 6Level 1
∂logdetX/∂X at X=diag(2,4): the (2,2) entry?
0.25.
Problem 7Level 1
The scalar version: (1/x)′ at x=2?
−1/4.
Level 2
Problem 8Level 2
Jacobi: ddetX for X=diag(2,3) and dX=diag(1,0)?
6⋅tr(diag(1/2,0))=3.
Problem 9Level 2
∂detX/∂X at X=diag(2,3): the (2,2) entry?
6⋅31=2.
Problem 10Level 2
d(X−1) at X=2I with dX=I: the (1,1) entry?
−21⋅1⋅21=−0.25.
Problem 11Level 2
∂tr(X⊤AX)/∂X with A=I at X=I: the (1,1) entry?
2X, so 2.
Problem 12Level 2
The 1-D MLE variance (zero mean) for samples {1,−1}?
(1+1)/2=1.
Problem 13Level 2
∂logdetX/∂X at X=I3. Its trace?
It is I3, so 3.
Level 3
Problem 14Level 3
A=diag(3,1) and dA=(0.10.20.20). The change of the largest eigenvalue?
e1⊤dAe1=0.1.
Problem 15Level 3
Is log det concave on positive-definite matrices? (yes/no)
Yes.
Problem 16Level 3
dt2d2logdet(I2+tI2) at t=0?
−tr(I2)=−2.
Problem 17Level 3
Proved(X−1)=−X−1dXX−1.
Differentiate XX−1=I: dXX−1+Xd(X−1)=0. Multiply on the left by X−1. ∎
Level 4
Problem 18Level 4
∂θ∂logdetK=tr(K−1∂θK) with K=θI3 at θ=2?
tr(21I3)=1.5.
Problem 19Level 4
Is the Gaussian maximum-likelihood covariance (with known zero mean) the sample covariance? (yes/no)
Yes.
Problem 20Level 4
A flow layer has detJ=e0.7. What is its log∣detJ∣?
0.7.
Coding practice
🐍 Code 1 (Level 1): the cheat sheet
Return ∇(xᵀAx), ∂tr(AX)/∂X and ∂ log det X/∂X.
🐍 Code 2 (Level 2): Jacobi's formula
Compute d(det X) = det X · tr(X⁻¹dX), and check it on a random direction.
🐍 Code 3 (Level 2): the covariance MLE
Write ∂ℓ/∂Σ for a zero-mean Gaussian, and show it vanishes at Σ = S.
🐍 Code 4 (Level 3): eigenvalue derivatives
Compute dλᵢ = qᵢᵀ dA qᵢ for a symmetric matrix, and match finite differences.
🐍 Code 5 (Level 4): concavity of log det
Check log det((X + Y)/2) ≥ average log det on random SPD pairs, and that the second directional derivative is −tr((X⁻¹D)²) < 0.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Common Matrix-Calculus Identities (xᵀAx, tr, log det, inverse): topic test
10 questions, drawn at random from a bank of 16. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.