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 ( for ),
- lower triangular if every entry above the diagonal is 0 ( for ),
- diagonal if it is both.
Row echelon forms of square matrices are upper triangular (Gaussian Elimination).
The theorem
Theorem. If is an triangular matrix, then
Proof for lower triangular , by induction on . For it is clear. For , expand along the first row (Minors and Cofactors): its only possibly non-zero entry is , so . The minor is the determinant of the matrix left after deleting row 1 and column 1, which is again lower triangular with diagonal . By induction . For upper triangular matrices expand along the first column instead (or use ).
Corollaries:
- and .
- A triangular matrix is invertible exactly when no diagonal entry is 0.
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 (the entries below the diagonal) have no influence at all.
Determinants through triangular forms
Row-reduce to an upper triangular , keeping track of how each row operation changes the determinant (the next lesson, Determinants and Row Operations, has the rules), then read off from its diagonal. That costs about operations instead of .
Numerical libraries do exactly this with the LU factorisation (Elementary Matrices): has 1s on its diagonal (), so , the sign coming from the number of row swaps in .
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 diagonal matrix with entries 0.1 has determinant , below the smallest double. Since of a triangular factor, libraries return the log-determinant instead, , which is a harmless sum:
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 , computed from the triangular Cholesky factor of the covariance matrix as .
- Normalizing flows (generative models that transform simple noise into images or audio) need of each layer's Jacobian . Designs such as RealNVP and autoregressive flows are built so that is triangular, making the log-determinant a cheap sum over the diagonal instead of an computation.
Exercises
Exercise 1: Read it off
Find and .
Answer
−3 and 0.
Solution
The first is lower triangular: . The second is upper triangular with a 0 on the diagonal: , so it is singular.
Exercise 2: Invertibility
For which values of is invertible?
Answer
For all except 0, 2 and .
Solution
, which is zero exactly when , or .
Exercise 3: Product of triangular matrices
Show that the product of two upper triangular matrices is upper triangular with diagonal entries , and deduce for them. Check on an example.
Answer
and entries below the diagonal vanish, so .
Solution
. A non-zero term needs (so ) and (so ). For no satisfies both, so . For the only is , giving .
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 , compute the Cholesky factor with np.linalg.cholesky and check that equals .
Answer
, so both give .
Solution
. Since , .
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?
- 6
- It depends on the entries above the diagonal
- 30
- −30
When is a triangular matrix invertible?
- Always
- When it is symmetric
- When none of its diagonal entries is zero
- When its entries above the diagonal are non-zero
Why do normalizing-flow models favour layers with triangular Jacobians?
- Triangular matrices are always orthogonal
- Their log-determinant is just a sum of logs of the diagonal, which is cheap
- They have no determinant
- They make training deterministic