TExcelTAD
Home › Math for Machine Learning › Determinant and Trace

Determinant and Trace

Two numbers summarize a square matrix surprisingly well. The determinant says how the matrix scales volume (and whether it is invertible); the trace is the sum of the diagonal. Both equal simple functions of the eigenvalues, and both show up in ML formulas: the Gaussian density contains det⁡Σ\det\Sigma, and the total variance of a dataset is tr⁡(Σ)\operatorname{tr}(\Sigma).

The determinant

For 2×22 \times 2 and 3×33 \times 3 matrices:

det⁡[abcd]=ad−bc,det⁡(A)=∑j=1n(−1)1+ja1jdet⁡(A1,j) (expansion along row 1)\det\begin{bmatrix} a & b \\ c & d \end{bmatrix} = ad - bc, \qquad \det(A) = \sum_{j=1}^{n} (-1)^{1+j} a_{1j} \det(A_{1,j}) \ \text{(expansion along row 1)}

where A1,jA_{1,j} deletes row 1 and column jj (Minors and Cofactors). In practice, computers use elimination: the determinant of a triangular matrix is the product of its diagonal.

Geometric meaning. ∣det⁡A∣\lvert \det A \rvert is the factor by which AA scales areas (2D) or volumes; the sign says whether orientation flips. det⁡A=0\det A = 0 means AA squashes space into a lower dimension, so it is not invertible.

Rules worth memorizing: det⁡(AB)=det⁡Adet⁡B\det(AB) = \det A \det B, det⁡(AT)=det⁡A\det(A^T) = \det A, det⁡(A−1)=1/det⁡A\det(A^{-1}) = 1/\det A, det⁡(λA)=λndet⁡A\det(\lambda A) = \lambda^n\det A for A∈Rn×nA \in \mathbb{R}^{n \times n}.

python · try it yourself
import numpy as np

A = np.array([[2.0, 1.0], [1.0, 3.0]])
B = np.array([[1.0, 4.0], [0.0, -1.0]])
print("det A =", round(np.linalg.det(A), 10), " det B =", round(np.linalg.det(B), 10))
print("det(AB) =", round(np.linalg.det(A @ B), 10), " det(3A) =", round(np.linalg.det(3 * A), 10))
# area of the unit square's image = |det A|
square = np.array([[0, 1, 1, 0], [0, 0, 1, 1]], dtype=float)
x, y = A @ square
area = 0.5 * abs(np.dot(x, np.roll(y, -1)) - np.dot(y, np.roll(x, -1)))   # shoelace formula
print("area of the transformed square:", area)
det A = 5.0  det B = -1.0
det(AB) = -5.0  det(3A) = 45.0
area of the transformed square: 5.0

The trace

tr⁡(A)=∑i=1naii\operatorname{tr}(A) = \sum_{i=1}^{n} a_{ii}

The trace is linear, tr⁡(A+B)=tr⁡A+tr⁡B\operatorname{tr}(A + B) = \operatorname{tr}A + \operatorname{tr}B, and cyclic: tr⁡(ABC)=tr⁡(BCA)=tr⁡(CAB)\operatorname{tr}(ABC) = \operatorname{tr}(BCA) = \operatorname{tr}(CAB). The cyclic rule is a workhorse in ML derivations; for example a scalar xTAx\mathbf{x}^TA\mathbf{x} equals tr⁡(xTAx)=tr⁡(AxxT)\operatorname{tr}(\mathbf{x}^TA\mathbf{x}) = \operatorname{tr}(A\mathbf{x}\mathbf{x}^T), which turns a sum of quadratic forms into one trace of a matrix with the data's covariance.

Both from the eigenvalues

If AA has eigenvalues λ1,…,λn\lambda_1, \ldots, \lambda_n (counted with multiplicity), then

det⁡(A)=∏i=1nλi,tr⁡(A)=∑i=1nλi\det(A) = \prod_{i=1}^{n} \lambda_i, \qquad \operatorname{tr}(A) = \sum_{i=1}^{n} \lambda_i
python · try it yourself
import numpy as np

rng = np.random.default_rng(0)
A = rng.normal(size=(4, 4))
lam = np.linalg.eigvals(A)
print("det:", round(np.linalg.det(A), 6), " product of eigenvalues:", np.round(np.prod(lam).real, 6))
print("trace:", round(np.trace(A), 6), " sum of eigenvalues:", np.round(np.sum(lam).real, 6))
X = rng.normal(size=(100, 4))
S = np.cov(X.T, bias=True)
print("total variance = tr(S):", np.isclose(np.trace(S), X.var(axis=0).sum()))
det: -1.884763  product of eigenvalues: -1.884763
trace: -0.868217  sum of eigenvalues: -0.868217
total variance = tr(S): True

The eigenvalues of a real matrix can be complex (in conjugate pairs), but their product and sum are real.

The log-determinant

Densities like the Gaussian need log⁡det⁡Σ\log\det\Sigma. Computing det⁡\det first and then the log overflows or underflows for large matrices; np.linalg.slogdet returns the sign and the log of the absolute value directly, and for SPD matrices log⁡det⁡Σ=2∑ilog⁡Lii\log\det\Sigma = 2\sum_i \log L_{ii} from the Cholesky factor.

In software

  • np.linalg.det and slogdet factorize with LU; slogdet is the one to use inside log-likelihoods.
  • Normalizing flows track how a transformation changes volume through log⁡∣det⁡J∣\log\lvert\det J\rvert (Change of Variables).
  • np.trace and the cyclic trick appear in derivations of PCA and of matrix gradients (Gradients of Matrices).

Exercises

Exercise 1: A 3×33 \times 3 determinant

Compute det⁡[201130011]\det\begin{bmatrix} 2 & 0 & 1 \\ 1 & 3 & 0 \\ 0 & 1 & 1 \end{bmatrix}.

Answer
Solution

Expand along the first row:

2(3⋅1−0⋅1)−0+1(1⋅1−3⋅0)=6+1=72(3 \cdot 1 - 0 \cdot 1) - 0 + 1(1 \cdot 1 - 3 \cdot 0) = 6 + 1 = 7

Exercise 2: Eigenvalues from trace and determinant

A 2×22 \times 2 matrix has trace 5 and determinant 6. What are its eigenvalues?

Answer

2 and 3.

Solution

The eigenvalues solve λ2−(tr⁡A)λ+det⁡A=0\lambda^2 - (\operatorname{tr}A)\lambda + \det A = 0, that is λ2−5λ+6=(λ−2)(λ−3)=0\lambda^2 - 5\lambda + 6 = (\lambda - 2)(\lambda - 3) = 0.

Exercise 3: Cyclic trace (code)

For random A∈R2×3A \in \mathbb{R}^{2 \times 3}, B∈R3×4B \in \mathbb{R}^{3 \times 4}, C∈R4×2C \in \mathbb{R}^{4 \times 2}, check tr⁡(ABC)=tr⁡(CAB)\operatorname{tr}(ABC) = \operatorname{tr}(CAB), and note the matrices have different sizes.

Answer

Equal; ABCABC is 2×22 \times 2 but CABCAB is 4×44 \times 4.

Solution
python · try it yourself
import numpy as np
rng = np.random.default_rng(2)
A, B, C = rng.normal(size=(2, 3)), rng.normal(size=(3, 4)), rng.normal(size=(4, 2))
print((A @ B @ C).shape, (C @ A @ B).shape, np.isclose(np.trace(A @ B @ C), np.trace(C @ A @ B)))
(2, 2) (4, 4) True

Test yourself

det⁡A=0\det A = 0 means…

  1. AA is the zero matrix
  2. AA is not invertible
  3. AA is orthogonal
  4. all eigenvalues are 1

The trace of a matrix equals…

  1. the product of its eigenvalues
  2. the sum of its eigenvalues
  3. its largest eigenvalue
  4. its rank

For A∈R3×3A \in \mathbb{R}^{3 \times 3}, det⁡(2A)\det(2A) equals…

  1. 2det⁡A2\det A
  2. 6det⁡A6\det A
  3. 8det⁡A8\det A
  4. det⁡A\det A