Skip to content
fn

slogdet

→Tensor
slogdet(A: Tensor)

Sign and natural log of the absolute determinant.

Returns the pair (sign, log⁡∣det⁡A∣)(\mathrm{sign},\, \log|\det A|) such that

det⁡(A) = sign⋅exp⁡(log⁡∣det⁡A∣).\det(A) \,=\, \mathrm{sign} \cdot \exp(\log|\det A|).

Stays correct where det⁡A\det A itself leaves the floating-point range — large or small covariance matrices in log-likelihood calculations, or any matrix whose determinant would overflow to ∞\infty or underflow to 00.

Parameters

Square matrix of shape (*, n, n).

Returns

Tensor

±1\pm 1 (or 00 for singular matrices), shape (*,).

Notes

Computed from the LU factorisation PA=LUPA = LU as log⁡∣det⁡A∣=∑ilog⁡∣Uii∣\log|\det A| = \sum_i \log|U_{ii}|, with the sign the product of the signs of UiiU_{ii} and of the permutation PP — the determinant itself is never formed, so nothing here can overflow. Cost is O(n3)O(n^3). A zero pivot makes the matrix singular: the sign is the signed zero det⁡A\det A would be, and logabsdet is −∞-\infty.

The gradient of logabsdet is A−⊤A^{-\top}, solved from the LU factors of A⊤A^\top the value was read from rather than formed as cof(A)/det⁡(A)\mathrm{cof}(A)/\det(A), so it stays finite wherever the matrix is invertible — and a matrix the factorisation found a nonzero last pivot for, however small, has a (large) gradient instead of an error. At a singular matrix it is cof(A)/det⁡(A)\mathrm{cof}(A)/\det(A) — ±∞\pm\infty along the adjugate of a matrix of rank n−1n-1.

Examples

>>> import lucid
>>> from lucid.linalg import slogdet
>>> sign, logabs = slogdet(lucid.tensor([[1.0, 2.0], [3.0, 4.0]]))
>>> sign, logabs
(tensor(-1.), tensor(0.6931))

Used by 1