TExcelTAD
Home › Linear Algebra & Matrix Theory › The Determinant of a Triangular Matrix

The Determinant of a Triangular Matrix

For one family of matrices the determinant is effortless: if a matrix is triangular, its determinant is the product of its diagonal entries. This fact is the reason row reduction computes determinants quickly, and it is why machine-learning models that need determinants are often designed with triangular matrices.

Triangular matrices

A square matrix is

  • upper triangular if every entry below the main diagonal is 0 (aij=0a_{ij} = 0 for i>ji > j),
  • lower triangular if every entry above the diagonal is 0 (aij=0a_{ij} = 0 for i<ji < j),
  • diagonal if it is both.

Row echelon forms of square matrices are upper triangular (Gaussian Elimination).

The theorem

Theorem. If AA is an n×nn \times n triangular matrix, then

det⁡A=a11⋅a22⋅…⋅ann\det A = a_{11} \cdot a_{22} \cdot \ldots \cdot a_{nn}

Proof for lower triangular AA, by induction on nn. For n=1n = 1 it is clear. For n>1n > 1, expand along the first row (Minors and Cofactors): its only possibly non-zero entry is a11a_{11}, so det⁡A=a11⋅C11=a11⋅M11\det A = a_{11} \cdot C_{11} = a_{11} \cdot M_{11}. The minor M11M_{11} is the determinant of the matrix left after deleting row 1 and column 1, which is again lower triangular with diagonal a22,…,anna_{22}, \ldots, a_{nn}. By induction M11=a22⋯annM_{11} = a_{22} \cdots a_{nn}. For upper triangular matrices expand along the first column instead (or use det⁡AT=det⁡A\det A^T = \det A).

det⁡[27−14035100−180004]=2⋅3⋅(−1)⋅4=−24(the entries above the diagonal do not matter)\det \begin{bmatrix} 2 & 7 & -1 & 4 \\ 0 & 3 & 5 & 1 \\ 0 & 0 & -1 & 8 \\ 0 & 0 & 0 & 4 \end{bmatrix} = 2 \cdot 3 \cdot (-1) \cdot 4 = -24 \qquad \text{(the entries above the diagonal do not matter)}

Corollaries:

  • det⁡I=1\det I = 1 and det⁡(diag⁡(d1,…,dn))=d1⋯dn\det(\operatorname{diag}(d_1, \ldots, d_n)) = d_1 \cdots d_n.
  • A triangular matrix is invertible exactly when no diagonal entry is 0.
python · try it yourself
from sympy import Matrix, symbols, factor

U = Matrix([[2, 7, -1, 4],
            [0, 3, 5, 1],
            [0, 0, -1, 8],
            [0, 0, 0, 4]])
print("det U:", U.det(), " product of diagonal:", U[0, 0] * U[1, 1] * U[2, 2] * U[3, 3])

a, b, c, x, y, z = symbols("a b c x y z")
L = Matrix([[a, 0, 0], [x, b, 0], [y, z, c]])
print("symbolic lower triangular:", L.det())
det U: -24  product of diagonal: -24
symbolic lower triangular: a*b*c

The symbolic result shows that x,y,zx, y, z (the entries below the diagonal) have no influence at all.

Determinants through triangular forms

Row-reduce AA to an upper triangular UU, keeping track of how each row operation changes the determinant (the next lesson, Determinants and Row Operations, has the rules), then read off det⁡U\det U from its diagonal. That costs about n3/3n^3/3 operations instead of n!n!.

Numerical libraries do exactly this with the LU factorisation PA=LUPA = LU (Elementary Matrices): LL has 1s on its diagonal (det⁡L=1\det L = 1), so det⁡A=±(product of U’s diagonal)\det A = \pm(\text{product of } U\text{'s diagonal}), the sign coming from the number of row swaps in PP.

python · try it yourself
import numpy as np
from scipy.linalg import lu

A = np.array([[4.0, 3.0, 2.0],
              [2.0, 1.0, 3.0],
              [3.0, 2.0, 1.0]])
P, L, U = lu(A)
sign = round(float(np.linalg.det(P)))         # +1 or -1: parity of the row swaps
print("diag(U):", np.round(np.diag(U), 4).tolist())
print("det via LU:", round(sign * float(np.prod(np.diag(U))), 6))
print("np.linalg.det:", round(float(np.linalg.det(A)), 6))
diag(U): [4.0, -0.5, -1.5]
det via LU: 3.0
np.linalg.det: 3.0

In software: log-determinants

Determinants of large matrices overflow or underflow floating point quickly: a 500×500500 \times 500 diagonal matrix with entries 0.1 has determinant 10−50010^{-500}, below the smallest double. Since det⁡=∏diagonal\det = \prod \text{diagonal} of a triangular factor, libraries return the log-determinant instead, log⁡∣det⁡A∣=∑log⁡∣uii∣\log\lvert\det A\rvert = \sum \log\lvert u_{ii}\rvert, which is a harmless sum:

python · try it yourself
import numpy as np

D = np.diag(np.full(500, 0.1))
print("det:", float(np.linalg.det(D)))                 # underflows to 0.0
sign, logdet = np.linalg.slogdet(D)
print("slogdet:", float(sign), round(float(logdet), 4))
print("log10|det| =", round(float(logdet / np.log(10)), 4))
det: 0.0
slogdet: 1.0 -1151.2925
log10|det| = -500.0

Where this matters:

  • Gaussian models: the log-likelihood of a multivariate normal contains log⁡det⁡Σ\log \det \Sigma, computed from the triangular Cholesky factor of the covariance matrix Σ=LLT\Sigma = LL^T as 2⋅∑log⁡Lii2 \cdot \sum \log L_{ii}.
  • Normalizing flows (generative models that transform simple noise into images or audio) need log⁡∣det⁡J∣\log\lvert\det J\rvert of each layer's Jacobian JJ. Designs such as RealNVP and autoregressive flows are built so that JJ is triangular, making the log-determinant a cheap sum over the diagonal instead of an O(n3)O(n^3) computation.

Exercises

Exercise 1: Read it off

Find det⁡[−200530190.5]\det \begin{bmatrix} -2 & 0 & 0 \\ 5 & 3 & 0 \\ 1 & 9 & 0.5 \end{bmatrix} and det⁡[144007002]\det \begin{bmatrix} 1 & 4 & 4 \\ 0 & 0 & 7 \\ 0 & 0 & 2 \end{bmatrix}.

Answer

−3 and 0.

Solution

The first is lower triangular: (−2)⋅3⋅0.5=−3(-2) \cdot 3 \cdot 0.5 = -3. The second is upper triangular with a 0 on the diagonal: 1⋅0⋅2=01 \cdot 0 \cdot 2 = 0, so it is singular.

Exercise 2: Invertibility

For which values of kk is [k120k−2300k+1]\begin{bmatrix} k & 1 & 2 \\ 0 & k - 2 & 3 \\ 0 & 0 & k + 1 \end{bmatrix} invertible?

Answer

For all kk except 0, 2 and −1-1.

Solution

det⁡=k(k−2)(k+1)\det = k(k - 2)(k + 1), which is zero exactly when k=0k = 0, k=2k = 2 or k=−1k = -1.

Exercise 3: Product of triangular matrices

Show that the product of two upper triangular matrices is upper triangular with diagonal entries aiibiia_{ii}b_{ii}, and deduce det⁡(AB)=det⁡A⋅det⁡B\det(AB) = \det A \cdot \det B for them. Check on an example.

Answer

(AB)ii=aiibii(AB)_{ii} = a_{ii}b_{ii} and entries below the diagonal vanish, so det⁡(AB)=∏aiibii=det⁡A⋅det⁡B\det(AB) = \prod a_{ii}b_{ii} = \det A \cdot \det B.

Solution

(AB)ij=∑kaikbkj(AB)_{ij} = \sum_k a_{ik}b_{kj}. A non-zero term needs aik≠0a_{ik} \ne 0 (so k≥ik \ge i) and bkj≠0b_{kj} \ne 0 (so k≤jk \le j). For i>ji > j no kk satisfies both, so (AB)ij=0(AB)_{ij} = 0. For i=ji = j the only kk is k=ik = i, giving aiibiia_{ii}b_{ii}.

python · try it yourself
from sympy import Matrix
A = Matrix([[2, 1, 5], [0, -1, 3], [0, 0, 4]])
B = Matrix([[3, 0, 1], [0, 2, 2], [0, 0, -1]])
print((A * B).tolist(), (A * B).det(), A.det() * B.det())
[[6, 2, -1], [0, -2, -5], [0, 0, -4]] 48 48

Exercise 4: Log-det of a covariance

For the covariance matrix Σ=[4223]\Sigma = \begin{bmatrix} 4 & 2 \\ 2 & 3 \end{bmatrix}, compute the Cholesky factor LL with np.linalg.cholesky and check that 2⋅∑log⁡Lii2 \cdot \sum \log L_{ii} equals log⁡det⁡Σ\log \det \Sigma.

Answer

det⁡Σ=8\det \Sigma = 8, so both give log⁡8≈2.0794\log 8 \approx 2.0794.

Solution

det⁡Σ=4⋅3−2⋅2=8\det \Sigma = 4 \cdot 3 - 2 \cdot 2 = 8. Since Σ=LLT\Sigma = LL^T, det⁡Σ=(det⁡L)2=(∏Lii)2\det \Sigma = (\det L)^2 = (\prod L_{ii})^2.

python · try it yourself
import numpy as np
S = np.array([[4.0, 2.0], [2.0, 3.0]])
L = np.linalg.cholesky(S)
print(np.round(L, 4).tolist())
print(round(float(2 * np.sum(np.log(np.diag(L)))), 4), round(float(np.log(np.linalg.det(S))), 4))
[[2.0, 0.0], [1.0, 1.4142]]
2.0794 2.0794

Test yourself

What is the determinant of an upper triangular matrix with diagonal 3, −2, 5?

  1. 6
  2. It depends on the entries above the diagonal
  3. 30
  4. −30

When is a triangular matrix invertible?

  1. Always
  2. When it is symmetric
  3. When none of its diagonal entries is zero
  4. When its entries above the diagonal are non-zero

Why do normalizing-flow models favour layers with triangular Jacobians?

  1. Triangular matrices are always orthogonal
  2. Their log-determinant is just a sum of logs of the diagonal, which is cheap
  3. They have no determinant
  4. They make training deterministic