Skip to content
fn

ldl_factor

→Tensor
ldl_factor(input: Tensor, hermitian: bool = False)

Implementing kernel

C++ engine symbols that back this Python API.

LDL factorization of a symmetric (or Hermitian) matrix.

For a real symmetric matrix AA (possibly indefinite), computes a Bunch-Kaufman block factorization

A = L D L⊤,A \,=\, L\,D\,L^\top,

where LL is unit-lower-triangular and DD is block-diagonal with 1×11 \times 1 or 2×22 \times 2 blocks. Unlike cholesky, this factorization exists for indefinite symmetric matrices (e.g., saddle-point systems).

Parameters

inputTensor
Symmetric / Hermitian matrix of shape (*, n, n).
hermitian(bool, keyword - only)= False
Whether a complex input is Hermitian rather than symmetric. A real input is both, so either value factors it the same. Default False, as the reference's.

Returns

Tensor

Packed factor of shape (*, n, n). The strict lower triangle holds LL; the diagonal holds DD's entries (2×22 \times 2 blocks are stored in the sub-diagonal). The strict upper triangle is zero.

Raises

LinAlgError
If a 1×11 \times 1 block of DD is exactly zero — the factorization completes, but DD is singular. ldl_factor_ex returns it with a non-zero info instead.

Notes

Backed by LAPACK sytrf. Cost is O(n3/3)O(n^3 / 3). Pair with ldl_solve for solving symmetric indefinite systems.

sytrf writes the factor into the lower triangle and leaves the upper one as it found it — the input's own entries. Those are cleared here, so LD holds the factorization and nothing else.

LD is differentiable in input, to any order, wherever the pivots do not change — an addition over the reference framework, which defines no derivative for this factorization and raises on backward. The gradient lands on input's lower triangle, the part sytrf reads, as cholesky's does.

Examples

>>> import lucid
>>> from lucid.linalg import ldl_factor
>>> A = lucid.tensor([[4.0, 1.0], [1.0, 3.0]])
>>> LD, piv = ldl_factor(A)
>>> LD
tensor([[4., 0.], [0.25, 2.75]])
>>> piv
tensor([1, 2], dtype=lucid.int32)

Used by 1