torch.linalg.slogdet
function slogdet<S extends Shape, D extends DType, Dev extends DeviceType>(A: Tensor<S, D, Dev>): { sign: Tensor<DynamicShape, D, Dev>; logabsdet: Tensor<DynamicShape, D, Dev> }Computes the sign and log absolute value of the determinant.
Returns both the sign and natural logarithm of the absolute determinant separately, enabling numerically stable computation of det(A) = sign(A) × exp(log|det(A)|). Avoids overflow/underflow by returning log(|det|) instead of |det|. Essential for:
- Numerical stability: Computing determinants without overflow/underflow
- Log-likelihood: Gaussian distribution probabilities in log-space
- Matrix properties: Checking invertibility (det ≠ 0) and orientation (sign)
- Probabilistic models: Computing normalizing constants in Bayesian inference
- Manifold learning: Jacobian determinants in change-of-variables formulas
- Matrix computations: Separating magnitude and sign information
Uses LU decomposition: det(A) = sign(permutation) × ∏diag(U). The log operation is applied to absolute values to avoid numerical issues with very large or very small determinants.
- Sign interpretation: sign = 0 indicates singular matrix (det = 0)
- Numerical safety: log(|det|) is safe for |det| ∈ [1e-30, 1e30] (no over/underflow)
- Reconstruction: det(A) = sign × exp(logabsdet) avoids numerical issues
- GPU efficient: Computed via LU factorization on GPU (no CPU readback)
- Gradient flow: Both sign and logabsdet are differentiable (logabsdet gradient is continuous except at det=0)
- Singular matrices: sign = 0 for singular matrices; logabsdet = -∞
- Sign discontinuity: Gradient of sign is undefined at det = 0
- Batches: All matrices must be square; shapes must be (..., n, n)
Parameters
ATensor<S, D, Dev>- Square matrix of shape (n, n) or batches of shape (..., n, n)
Returns
{ sign: Tensor<DynamicShape, D, Dev>; logabsdet: Tensor<DynamicShape, D, Dev> }– Object with two tensors: - sign: Tensor containing -1, 0, or 1 (0 for singular matrices) - logabsdet: Natural log of absolute determinant (log(|det(A)|))Examples
// Simple 2x2 matrix
const A = torch.tensor([[3, 1], [1, 2]]);
const { sign, logabsdet } = torch.linalg.slogdet(A);
// sign: 1, logabsdet: ln(5) ≈ 1.609
// Reconstruct: det = 1 * exp(1.609) = 5
// Singular matrix (rank deficient)
const singular = torch.tensor([[1, 2], [2, 4]]);
const { sign: s, logabsdet: lad } = torch.linalg.slogdet(singular);
// sign: 0, logabsdet: -∞
// Gaussian log-likelihood computation
const cov = torch.tensor([[2, 0.5], [0.5, 1]]);
const x = torch.tensor([1, -1]);
const mean = torch.tensor([0, 0]);
const { logabsdet } = torch.linalg.slogdet(cov);
const diff = x.sub(mean);
const inv_cov = torch.linalg.inv(cov);
const quad_form = diff.matmul(inv_cov).matmul(diff);
const log_prob = -0.5 * (logabsdet + quad_form + 2 * Math.log(2 * Math.PI));
// Batched computation
const batch = torch.randn(32, 5, 5); // 32 matrices of size 5x5
const { sign: signs, logabsdet: logdets } = torch.linalg.slogdet(batch);
// signs shape: [32], logdets shape: [32]