Skip to main content

Mathematical Infrastructure

Matrix Calculus

The differentiation identities you actually use in ML: derivatives of traces, log-determinants, and quadratic forms with respect to matrices and vectors.

CoreTier 1StableSupporting~20 min

Learning position

Place this page in a reading path.

mathematical-infrastructure | layer 1 | tier 1. This page has 2 direct prerequisites and 4 published dependents.

What next

Feedforward Networks and Backpropagation

This is the first curated or graph-derived continuation from the current page.

Evidence badge

Source-grounded page

This page has no public Lean mapping yet. Use the evidence page to inspect how claim status labels work.

Show the backing system

Why This Matters

Chain rule for matrix calculus: Jacobians compose by multiplication

f∂z/∂x(m × n)g∂y/∂z(p × m)xℝⁿz = f(x)ℝᵐy = g(z)ℝᵖChain Rule∂y/∂x = (∂y/∂z)(∂z/∂x)(p × n) = (p × m) · (m × n)∂y/∂z : p × m·∂z/∂x : m × n=∂y/∂x : p × nInner dimensions (m) cancel; result is p × n

Every time you derive a gradient for a machine learning model — whether it is linear regression, a Gaussian model, or a neural network — you are doing matrix calculus. Most ML papers skip the derivation steps, writing "taking the derivative and setting to zero" as if the reader knows how to differentiate log⁡det⁡(X)\log \det(X) with respect to XX.

This topic gives you the identities that make those derivations mechanical. Once you know a handful of rules, you can derive gradients for any model whose loss involves traces, determinants, and quadratic forms.

Mental Model

Matrix calculus extends scalar calculus to matrices the same way vector calculus extends it to vectors. The key insight: every matrix expression is really a scalar function of many variables (the matrix entries). We package the partial derivatives into matrices of the same shape.

The derivative of a scalar with respect to a matrix is a matrix. The derivative of a scalar with respect to a vector is a vector. These are the gradients you need for optimization.

Formal Setup and Notation

Let f:Rm×n→Rf: \mathbb{R}^{m \times n} \to \mathbb{R} be a scalar-valued function of a matrix XX. The derivative of ff with respect to XX is the matrix of partial derivatives:

∂f∂X∈Rm×n,[∂f∂X]ij=∂f∂Xij\frac{\partial f}{\partial X} \in \mathbb{R}^{m \times n}, \quad \left[\frac{\partial f}{\partial X}\right]_{ij} = \frac{\partial f}{\partial X_{ij}}

Definition

Denominator Layout Convention

In the denominator layout (also called Jacobian formulation), the derivative ∂f∂X\frac{\partial f}{\partial X} has the same shape as XTX^T. For a scalar ff and column vector x∈Rnx \in \mathbb{R}^n, ∂f∂x\frac{\partial f}{\partial x} is a row vector. Many statistics textbooks use this convention.

Definition

Numerator Layout Convention

In the numerator layout (also called gradient formulation), the derivative ∂f∂X\frac{\partial f}{\partial X} has the same shape as XX. For a scalar ff and column vector x∈Rnx \in \mathbb{R}^n, ∂f∂x\frac{\partial f}{\partial x} is a column vector. Most ML and optimization textbooks use this convention because the gradient points in the direction of steepest ascent.

This topic uses numerator layout throughout.

Core Identities

These are the identities you need to know. In all cases, AA is a constant matrix of appropriate dimensions, xx is a vector, and XX is a matrix.

Vector identities:

∂∂x(aTx)=a\frac{\partial}{\partial x}(a^T x) = a

∂∂x(xTAx)=(A+AT)x\frac{\partial}{\partial x}(x^T A x) = (A + A^T)x

If AA is symmetric, this simplifies to 2Ax2Ax.

∂∂x∥x∥2=2x\frac{\partial}{\partial x}\|x\|^2 = 2x

Trace identities:

∂∂Xtr(AX)=AT\frac{\partial}{\partial X}\mathrm{tr}(AX) = A^T

∂∂Xtr(XTA)=A\frac{\partial}{\partial X}\mathrm{tr}(X^T A) = A

∂∂Xtr(XTAX)=(A+AT)X\frac{\partial}{\partial X}\mathrm{tr}(X^T A X) = (A + A^T)X

∂∂Xtr(AXB)=ATBT\frac{\partial}{\partial X}\mathrm{tr}(AXB) = A^T B^T

Log-determinant (valid where det⁡X>0\det X > 0, in particular on the cone of symmetric positive-definite matrices):

∂∂Xlog⁡det⁡(X)=X−T\frac{\partial}{\partial X}\log \det(X) = X^{-T}

If XX is symmetric and positive-definite (so log⁡det⁡X\log \det X is real and differentiable), this is X−1X^{-1}.

Inverse:

∂∂Xtr(AX−1)=−(X−1AX−1)T=−X−TATX−T\frac{\partial}{\partial X}\mathrm{tr}(AX^{-1}) = -(X^{-1}AX^{-1})^T = -X^{-T}A^TX^{-T}

The Chain Rule for Matrix Expressions

Definition

Matrix Chain Rule via Differentials

To differentiate complex matrix expressions, use differentials. The differential of ff is:

df=tr((∂f∂X)TdX)df = \mathrm{tr}\left(\left(\frac{\partial f}{\partial X}\right)^T dX\right)

The procedure: (1) compute the differential dfdf using rules like d(AB)=(dA)B+A(dB)d(AB) = (dA)B + A(dB), d(A−1)=−A−1(dA)A−1d(A^{-1}) = -A^{-1}(dA)A^{-1}, and d(log⁡det⁡A)=tr(A−1dA)d(\log\det A) = \mathrm{tr}(A^{-1} dA); (2) rearrange into the form tr((⋅)TdX)\mathrm{tr}((\cdot)^T dX); (3) read off the derivative.

Main Theorems

Proposition

Chain Rule for Matrix-to-Scalar Functions

Statement

If f:Rm×n→Rf: \mathbb{R}^{m \times n} \to \mathbb{R} is a scalar function and X=X(θ)X = X(\theta) depends on parameters θ∈Rp\theta \in \mathbb{R}^p, then:

∂f∂θk=tr((∂f∂X)T∂X∂θk)\frac{\partial f}{\partial \theta_k} = \mathrm{tr}\left(\left(\frac{\partial f}{\partial X}\right)^T \frac{\partial X}{\partial \theta_k}\right)

For the common case where f(X)=f(Aθ)f(X) = f(A\theta) with AA constant and x=Aθx = A\theta:

∂f∂θ=AT∂f∂x\frac{\partial f}{\partial \theta} = A^T \frac{\partial f}{\partial x}

Intuition

The chain rule in matrix calculus works the same as in scalar calculus: the derivative of the composition is the product of derivatives. The trace appears because we need to contract a matrix of derivatives (the Jacobian) down to a scalar. The key is keeping track of transposes.

Proof Sketch

Write the differential: df=tr((∂f∂X)TdX)df = \mathrm{tr}((\frac{\partial f}{\partial X})^T dX). If X=X(θ)X = X(\theta), then dX=∑k∂X∂θkdθkdX = \sum_k \frac{\partial X}{\partial \theta_k} d\theta_k. Substitute and use linearity of trace: df=∑ktr((∂f∂X)T∂X∂θk)dθkdf = \sum_k \mathrm{tr}((\frac{\partial f}{\partial X})^T \frac{\partial X}{\partial \theta_k}) d\theta_k. Read off ∂f∂θk\frac{\partial f}{\partial \theta_k} as the coefficient of dθkd\theta_k.

Why It Matters

This is how you derive gradients for any model. Want the gradient of the log-likelihood of a Gaussian with respect to the mean? Chain rule through the quadratic form. With respect to the covariance? Chain rule through the log-determinant and the inverse.

Failure Mode

The most common error is getting transposes wrong. In numerator layout, ∂∂x(aTx)=a\frac{\partial}{\partial x}(a^Tx) = a (a column vector), but in denominator layout it is aTa^T (a row vector). Always state your convention and check dimensions.

Canonical Examples

Example

Maximum likelihood for multivariate Gaussian

The log-likelihood of μ\mu and Σ\Sigma given data {xi}i=1n\{x_i\}_{i=1}^n is ℓ=−n2log⁡det⁡Σ−12∑i(xi−μ)TΣ−1(xi−μ)+const\ell = -\frac{n}{2}\log\det\Sigma - \frac{1}{2}\sum_i (x_i - \mu)^T \Sigma^{-1}(x_i - \mu) + \text{const}. Taking ∂ℓ∂μ\frac{\partial \ell}{\partial \mu}: the derivative of the quadratic form gives Σ−1∑i(xi−μ)\Sigma^{-1}\sum_i(x_i - \mu). Setting to zero gives μ^=xˉ\hat{\mu} = \bar{x}. Taking ∂ℓ∂Σ\frac{\partial \ell}{\partial \Sigma}: use ∂∂Σlog⁡det⁡Σ=Σ−1\frac{\partial}{\partial \Sigma}\log\det\Sigma = \Sigma^{-1} and ∂∂Σtr(Σ−1A)=−Σ−1AΣ−1\frac{\partial}{\partial \Sigma}\mathrm{tr}(\Sigma^{-1}A) = -\Sigma^{-1}A\Sigma^{-1}. Setting to zero gives Σ^=1n∑i(xi−xˉ)(xi−xˉ)T\hat{\Sigma} = \frac{1}{n}\sum_i(x_i-\bar{x})(x_i-\bar{x})^T.

Example

Linear regression gradient

For f(β)=∥y−Xβ∥2=(y−Xβ)T(y−Xβ)f(\beta) = \|y - X\beta\|^2 = (y - X\beta)^T(y-X\beta), expand to yTy−2βTXTy+βTXTXβy^Ty - 2\beta^TX^Ty + \beta^TX^TX\beta. Then ∂f∂β=−2XTy+2XTXβ\frac{\partial f}{\partial \beta} = -2X^Ty + 2X^TX\beta. Setting to zero gives the normal equations XTXβ=XTyX^TX\beta = X^Ty.

Common Confusions

Watch Out

Layout conventions cause most errors

Different textbooks use different layout conventions, and mixing them produces wrong transposes. Pick one convention (numerator layout for ML) and stick with it. When reading papers, check which convention they use by verifying a simple case like ∂∂x(aTx)\frac{\partial}{\partial x}(a^Tx).

Watch Out

The derivative of a trace is not a trace

∂∂Xtr(AX)=AT\frac{\partial}{\partial X}\mathrm{tr}(AX) = A^T is a matrix, not a scalar. The trace of a matrix is a scalar, but its derivative with respect to a matrix is a matrix. This is the gradient: it tells you how much the trace changes when you perturb each entry of XX.

Summary

  • ∂∂x(xTAx)=(A+AT)x\frac{\partial}{\partial x}(x^TAx) = (A+A^T)x; for symmetric AA this is 2Ax2Ax
  • ∂∂Xtr(AX)=AT\frac{\partial}{\partial X}\mathrm{tr}(AX) = A^T
  • ∂∂Xlog⁡det⁡(X)=X−T\frac{\partial}{\partial X}\log\det(X) = X^{-T}
  • Use differentials for complex expressions: compute dfdf, rearrange into tr((⋅)TdX)\mathrm{tr}((\cdot)^T dX), read off the derivative
  • Always check dimensions and state your layout convention
  • Most ML uses numerator layout (gradient has same shape as variable)

Exercises

ExerciseCore

Problem

Derive ∂∂x(xTAx)\frac{\partial}{\partial x}(x^TAx) where AA is a constant square matrix and xx is a vector.

ExerciseAdvanced

Problem

Derive ∂∂Σlog⁡det⁡(Σ)\frac{\partial}{\partial \Sigma}\log\det(\Sigma) using differentials. Assume Σ\Sigma is symmetric positive definite.

ExerciseResearch

Problem

Derive the derivative ∂∂Xtr(XTAX)\frac{\partial}{\partial X}\mathrm{tr}(X^TAX) using the differential method, where AA is a constant matrix and XX is an n×pn \times p matrix.

References

Canonical:

  • Petersen & Pedersen, The Matrix Cookbook (2012), https://www.math.uwaterloo.ca/~hwolkowi/matrixcookbook.pdf
  • Magnus & Neudecker, Matrix Differential Calculus with Applications in Statistics and Econometrics (2019), Chapters 1-5
  • Boyd & Vandenberghe, Convex Optimization (2004), Section A.4 (matrix calculus identities)

Current:

  • Parr & Howard, "The Matrix Calculus You Need for Deep Learning" (2018), arXiv:1802.01528
  • Goodfellow, Bengio, Courville, Deep Learning (2016), Section 4.3 (gradient-based optimization)
  • Deisenroth, Faisal, Ong, Mathematics for Machine Learning (2020), Chapter 5.3 (gradients of matrix and vector expressions)

Next Topics

The natural next steps from matrix calculus:

Last reviewed: July 3, 2026

Cite this page

Sneiderman, Robby. "Matrix Calculus." TheoremPath, reviewed 2026-07-03. https://theorempath.com/topics/matrix-calculus

Canonical URL
https://theorempath.com/topics/matrix-calculus
Author
Robby Sneiderman, TheoremPath
Last reviewed
2026-07-03
What this is
A reference page on TheoremPath. Written and maintained by the named author. Not peer reviewed and not refereed by any venue. Each claim below carries its own verification status.
Terms
All rights reserved. Non-commercial quotation with attribution permitted.

Citing one claim

One statement on this page is separately addressable. Each carries an identifier of the form claim:<topic>::<statement> printed beside it, and resolves to that identifier's anchor on this URL. Quote the statement, its assumptions, and its failure modes together.

Recorded verification on this page: 1 informal_only.

  • This page is a secondary source. Where a claim cites literature, the result is due to that source and not to this site.
  • 1 of 1 claims on this page name no source. Their attribution to the literature is not recorded here, whatever their verification status says.
  • 1 of 1 carry neither a source nor a proof check. Their provenance is the author's reading of the literature; check them against a primary source before relying on them.
  • Statements are given in LaTeX exactly as authored. The rendered page passes the same LaTeX through KaTeX, so text scraped from the HTML repeats expressions and drops radicals; quote from statementTex.

/claims.json lists 1,178 of the 1,231 statements this site records, with the anchor and the recorded verification of each. What earns a place in it: a theorem, lemma, corollary, or proposition block on a published topic page. Definitions are not listed, and neither are the 27 statements on comparison pages. A missing entry means the rule above did not select the statement, not that this site is silent on the result.

Canonical graph

Required before and derived from this topic

These links come from prerequisite edges in the curriculum graph. Editorial suggestions are shown here only when the target page also cites this page as a prerequisite.

Required prerequisites

2

Derived topics

4