torch.Tensor.Tensor.slogdet
Computes the sign and log absolute value of the determinant in a numerically stable way.
Returns both the sign and logarithm of the absolute determinant separately, enabling
reconstruction of the full determinant as sign * exp(logabsdet). This provides
numerical stability and allows handling negative determinants (e.g., for orientation-reversing transforms).
More informative than logdet() because it preserves sign information, which is important
for determinants of transformation matrices (orientation preservation, parity).
Use Cases:
- Computing oriented volumes in geometric algorithms
- Checking whether a transformation preserves or reverses orientation
- Variational inference where sign matters (e.g., flow models)
- Higher-precision reconstruction: det = exp(log + sign adjustment)
- Sign values: 0 for singular (det=0), ±1 for non-singular matrices
- Log value: Always non-negative since it's log of absolute value
- Reconstruction: sign * exp(logabsdet) recovers the actual determinant
- Orientation: Sign encodes whether transformation preserves orientation
- Batch support: Works with batched matrices (..., n, n)
- Singular matrices: Returns sign=0 and logabsdet=-inf
- Precision loss: exp(logabsdet) may lose precision for very large/small values
- Orientation: Sign is only meaningful for invertible matrices
Returns
{ sign: Tensor; logabsdet: Tensor }– Object with two properties: - sign: Tensor of shape () with values in -1, 0, 1 indicating sign of det(A) - logabsdet: Tensor of shape () with ln(|det(A)|)Examples
// Basic sign + log determinant
const A = torch.tensor([[-2, 1], [1, -1]]);
const { sign, logabsdet } = A.slogdet();
sign; // 1 (positive determinant)
logabsdet; // log(|1|) = 0
const det = sign * Math.exp(logabsdet.item()); // Recover: 1
// Orientation-reversing transformation (negative determinant)
const reflect = torch.tensor([[1, 0], [0, -1]]); // y-reflection
const { sign: s, logabsdet: lad } = reflect.slogdet();
s; // -1 (reverses orientation)
// This shows the reflection is orientation-reversing
// Numerically stable for difficult matrices
const scale_factor = 1e10;
const scaled = torch.eye(3).mul(scale_factor);
const { sign: sig, logabsdet: lad } = scaled.slogdet();
// Reconstruction avoids overflow/underflow
const det_value = sig * Math.exp(lad.item()); // Safe to compute
// Orientation check in 3D
const basis = torch.tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]]);
const { sign: orientation } = basis.slogdet();
if (orientation > 0) {
console.log('Right-handed coordinate system');
} else {
console.log('Left-handed coordinate system');
}See Also
- PyTorch torch.slogdet() (or tensor.slogdet())
- det - Direct determinant (less stable for extreme values)
- logdet - Log of absolute determinant (loses sign information)
- inverse - Matrix inversion (non-singular matrices only)
- lu - LU decomposition (can extract sign from permutation)