fn

cholesky

Tensor
cholesky(x: Tensor, upper: bool = False)
source

Cholesky decomposition of a symmetric positive-definite matrix.

For a real symmetric positive-definite (SPD) matrix AA returns the unique lower-triangular factor LL with positive diagonal such that

A=LL(or A=UU if upper=True).A = L L^\top \qquad \text{(or } A = U^\top U \text{ if } \texttt{upper=True}\text{)}.

The Cholesky factor is the standard tool for solving SPD linear systems, sampling from a multivariate Gaussian, and computing matrix square roots. It is roughly twice as fast as LU and avoids any pivoting.

Parameters

xTensor
Symmetric positive-definite matrix of shape (*, n, n).
upperbool= False
If True return the upper-triangular factor UU such that A=UUA = U^\top U. Default False returns the lower-triangular factor LL.

Returns

Tensor

Triangular Cholesky factor of shape (*, n, n).

Notes

Algorithm: LAPACK potrf via Apple Accelerate on CPU, MLX on GPU. Cost is O(n3/3)O(n^3 / 3), half the work of LU.

Backward is implemented in Python via Murray's (2016) formula

LA=sym ⁣(LΦ(LG)L1),\frac{\partial L}{\partial A} \,=\, \mathrm{sym}\!\big(L^{-\top}\,\Phi(L^\top G)\,L^{-1}\big),

where Φ\Phi zeros the strict upper triangle and halves the diagonal. Two triangular solves implement the inversion implicitly.

Examples

>>> import lucid
>>> from lucid.linalg import cholesky
>>> A = lucid.tensor([[4.0, 2.0], [2.0, 3.0]])  # SPD
>>> L = cholesky(A)
>>> L
Tensor([[2.0000, 0.0000],
        [1.0000, 1.4142]])
>>> L @ L.T
Tensor([[4.0000, 2.0000],
        [2.0000, 3.0000]])