torch.linalg.cholesky¶
-
torch.linalg.
cholesky
(A, *, out=None) → Tensor¶ Computes the Cholesky decomposition of a complex Hermitian or real symmetric positive-definite matrix.
Letting be or , the Cholesky decomposition of a complex Hermitian or real symmetric positive-definite matrix is defined as
where is a lower triangular matrix and is the conjugate transpose when is complex, and the transpose when is real-valued.
Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if
A
is a batch of matrices then the output has the same batch dimensions.Note
When inputs are on a CUDA device, this function synchronizes that device with the CPU.
See also
torch.linalg.cholesky_ex()
for a version of this operation that skips the (slow) error checking by default and instead returns the debug information. This makes it a faster way to check if a matrix is positive-definite.torch.linalg.eigh()
for a different decomposition of a Hermitian matrix. The eigenvalue decomposition gives more information about the matrix but it slower to compute than the Cholesky decomposition.- Parameters
A (Tensor) – tensor of shape (*, n, n) where * is zero or more batch dimensions consisting of symmetric or Hermitian positive-definite matrices.
- Keyword Arguments
out (Tensor, optional) – output tensor. Ignored if None. Default: None.
- Raises
RuntimeError – if the
A
matrix or any matrix in a batchedA
is not Hermitian (resp. symmetric) positive-definite. IfA
is a batch of matrices, the error message will include the batch index of the first matrix that fails to meet this condition.
Examples:
>>> a = torch.randn(2, 2, dtype=torch.complex128) >>> a = a @ a.t().conj() + torch.eye(2) # creates a Hermitian positive-definite matrix >>> l = torch.linalg.cholesky(a) >>> a tensor([[2.5266+0.0000j, 1.9586-2.0626j], [1.9586+2.0626j, 9.4160+0.0000j]], dtype=torch.complex128) >>> l tensor([[1.5895+0.0000j, 0.0000+0.0000j], [1.2322+1.2976j, 2.4928+0.0000j]], dtype=torch.complex128) >>> l @ l.t().conj() tensor([[2.5266+0.0000j, 1.9586-2.0626j], [1.9586+2.0626j, 9.4160+0.0000j]], dtype=torch.complex128) >>> a = torch.randn(3, 2, 2, dtype=torch.float64) >>> a = a @ a.transpose(-2, -1) + torch.eye(2).squeeze(0) # symmetric positive definite matrices >>> l = torch.linalg.cholesky(a) >>> a tensor([[[ 1.1629, 2.0237], [ 2.0237, 6.6593]], [[ 0.4187, 0.1830], [ 0.1830, 0.1018]], [[ 1.9348, -2.5744], [-2.5744, 4.6386]]], dtype=torch.float64) >>> l tensor([[[ 1.0784, 0.0000], [ 1.8766, 1.7713]], [[ 0.6471, 0.0000], [ 0.2829, 0.1477]], [[ 1.3910, 0.0000], [-1.8509, 1.1014]]], dtype=torch.float64) >>> torch.allclose(l @ l.transpose(-2, -1), a) True