torch.linalg.cholesky
function cholesky<S extends Shape, D extends DType, Dev extends DeviceType>(A: Tensor<S, D, Dev>): Tensor<S, D, Dev>function cholesky<S extends Shape, D extends DType, Dev extends DeviceType>(A: Tensor<S, D, Dev>, options: CholeskyOptions): Tensor<S, D, Dev>Cholesky decomposition: factorizes a symmetric positive-definite matrix into triangular form.
Computes A = LL^T (lower triangular) or A = U^T U (upper triangular) where L/U are triangular matrices. Essential for:
- Solving linear systems (more efficient than LU when symmetry is available)
- Computing matrix determinant and log-determinant
- Sampling from multivariate Gaussian (covariance matrix factorization)
- Likelihood computation in probabilistic models
- Simulating correlated random variables
- Verifying positive-definiteness (fails if matrix is not positive-definite)
The Cholesky decomposition is faster and more stable than LU for symmetric positive-definite matrices. It requires O(n³/3) operations (about half of LU). The input matrix must be:
- Square (n × n)
- Symmetric (A = A^T)
- Positive-definite (all eigenvalues > 0)
When to use Cholesky:
- Solving Ax = b where A is symmetric positive-definite (twice faster than LU)
- Sampling from multivariate Gaussian N(μ, Σ): x = μ + L @ ε where Σ = LL^T
- Computing log determinant: log(det(A)) = 2 × sum(log(diag(L)))
- Kalman filtering and other probabilistic inference
- Convex optimization (interior point methods)
- Computing eigenvalues of symmetric matrices via LLT
Compared to alternatives:
- vs LU: Cholesky is 2x faster for symmetric positive-definite matrices
- vs eigendecomposition: Cholesky is faster but doesn't give eigenvalues
- vs SVD: Cholesky is faster but SVD handles rank-deficient matrices
- Numerical stability: Excellent for well-conditioned symmetric matrices
Input requirements:
- Input must be symmetric: A = A^T (only lower or upper triangle used based on UPLO)
- Input must be positive-definite (all eigenvalues > 0)
- Decomposition fails (numerical error) if matrix is not positive-definite
- Symmetric input required: Input must be symmetric (A = A^T)
- Positive-definite required: All eigenvalues must be strictly positive
- Fails gracefully: Decomposition fails if matrix is not positive-definite
- Very fast: About 2x faster than LU decomposition for same problem size
- Memory efficient: Uses in-place computation internally
- Numerical stability: Excellent for well-conditioned matrices
- GPU accelerated: Efficient GPU implementation available
- Lower triangle used: For input matrix, only lower (or upper based on UPLO) triangle is used
- Positive-definiteness required: Matrix must have all eigenvalues 0
- Symmetry required: Matrix must be exactly symmetric (within numerical precision)
- No scaling: Input should be well-scaled to avoid numerical issues
- Square matrices only: Input dimensions must be [..., n, n]
Parameters
ATensor<S, D, Dev>- Symmetric positive-definite square matrix (n × n) or batch (..., n, n)
Returns
Tensor<S, D, Dev>– L or U: lower or upper triangular factorExamples
// Basic Cholesky decomposition
const A = torch.tensor([[4.0, 2.0], [2.0, 3.0]]); // Symmetric positive-definite
const L = torch.linalg.cholesky(A); // Lower triangular
// Verify: L @ L^T ≈ A// Solve system Ax = b using Cholesky (more efficient than solve for symmetric matrices)
const A = torch.tensor([[4.0, 2.0], [2.0, 3.0]]);
const b = torch.tensor([1.0, 2.0]);
const L = torch.linalg.cholesky(A);
// Solve Ly = b, then L^T x = y
const y = torch.linalg.solve_triangular(L, b, false); // false = lower triangular
const x = torch.linalg.solve_triangular(L.T, y, true); // true = upper triangular
// Faster than general solve for symmetric positive-definite matrices// Generate correlated random variables from multivariate Gaussian
const mean = torch.zeros(3);
const cov = torch.tensor([
[1.0, 0.5, 0.2],
[0.5, 2.0, 0.3],
[0.2, 0.3, 1.5]
]); // Covariance matrix (symmetric positive-definite)
const L = torch.linalg.cholesky(cov); // Cholesky factor
const z = torch.randn(3); // Standard normal
const x = mean.add(L.matmul(z)); // Correlated sample from N(mean, cov)// Compute log determinant efficiently
const A = torch.tensor([[4.0, 2.0], [2.0, 3.0]]);
const L = torch.linalg.cholesky(A);
const logDet = 2 * L.diagonal(-1, -2).log().sum(); // log(det(A)) = 2 * sum(log(diag(L)))
// Avoids numerical underflow/overflow from direct determinant computation// Batched Cholesky decomposition
const A_batch = torch.randn(32, 10, 10);
// Make each matrix symmetric positive-definite
const A_sym = A_batch.add(A_batch.transpose(-2, -1)).div(2).add(torch.eye(10).mul(0.1));
const L_batch = torch.linalg.cholesky(A_sym);
// L_batch shape: [32, 10, 10] - Cholesky factor for each matrix// Upper triangular Cholesky (for algorithms expecting upper triangular)
const A = torch.tensor([[4.0, 2.0], [2.0, 3.0]]);
const U = torch.linalg.cholesky(A, { upper: true }); // U^T U = A
// U is upper triangular; equivalent to L.T from lower triangular caseSee Also
- PyTorch torch.linalg.cholesky()
- solve - Solve linear systems (uses Cholesky for symmetric matrices)
- lu_factor - General LU decomposition (slower but works for non-symmetric)
- eigh - Eigendecomposition for symmetric matrices (also gives eigenvalues)
- svd - Singular value decomposition (more general, handles rank-deficient)
- matrix_rank - Compute matrix rank (Cholesky can detect positive-definiteness)