🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.linalg.cholesky()

AI & DATA SCIENCE // np-linalg-cholesky

np.linalg.cholesky() decomposes a symmetric, positive-definite matrix A into a lower triangular matrix L such that A equals L multiplied by its own transpose.

Syntax

np.linalg.cholesky(A)

Deep Dive Course

The Cholesky decomposition only exists for matrices that are both symmetric and positive-definite, all eigenvalues strictly positive — a common category in practice, since covariance matrices in statistics and many matrices arising from least-squares or optimization problems satisfy exactly this property. Because it exploits that special structure, computing a Cholesky decomposition is roughly twice as fast as a general-purpose decomposition like LU, which is why it's the preferred method specifically when you know in advance that a matrix qualifies.

1Understanding np.linalg.cholesky()

The Cholesky decomposition only exists for matrices that are both symmetric and positive-definite, all eigenvalues strictly positive — a common category in practice, since covariance matrices in statistics and many matrices arising from least-squares or optimization problems satisfy exactly this property. Because it exploits that special structure, computing a Cholesky decomposition is roughly twice as fast as a general-purpose decomposition like LU, which is why it's the preferred method specifically when you know in advance that a matrix qualifies.

💡

np.linalg.cholesky() raises a LinAlgError if the matrix isn't positive-definite or isn't symmetric — this failure is itself sometimes used as a quick numerical test for positive-definiteness, since it's cheaper than explicitly computing all the eigenvalues.

editor.html
import numpy as np

A = np.array([[4, 2], [2, 3]])
L = np.linalg.cholesky(A)
print(L)
localhost:3000

2Practical Example

Here is a real-world application of np.linalg.cholesky() showing how it is used in production NumPy code.

editor.html
import numpy as np

A = np.array([[4, 2], [2, 3]])
L = np.linalg.cholesky(A)
reconstructed = L @ L.T
print(np.allclose(reconstructed, A))
localhost:3000

3Best Practices

Follow these guidelines when working with np.linalg.cholesky():

1. Use Cholesky decomposition specifically for known symmetric, positive-definite matrices, like covariance matrices, where it's faster than more general decompositions

2. Handle the LinAlgError from a failed Cholesky attempt as a signal the matrix isn't positive-definite, rather than assuming it will always succeed

3. Use the resulting lower-triangular matrix L for efficient random sampling from a multivariate normal distribution, one of its most common practical applications

⚠️

Tip: np.linalg.cholesky() raises a LinAlgError if the matrix isn't positive-definite or isn't symmetric — this failure is itself sometimes used as a quick numerical test for positive-definiteness, since it's cheaper than explicitly computing all the eigenvalues.

editor.html
import numpy as np

A = np.array([[4, 2], [2, 3]])
L = np.linalg.cholesky(A)
print(L)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

A = np.array([[4, 2], [2, 3]])
L = np.linalg.cholesky(A)
print(L)
Example 02Advanced Example
import numpy as np

A = np.array([[4, 2], [2, 3]])
L = np.linalg.cholesky(A)
reconstructed = L @ L.T
print(np.allclose(reconstructed, A))

Best Practices

  • Use Cholesky decomposition specifically for known symmetric, positive-definite matrices, like covariance matrices, where it's faster than more general decompositions
  • Handle the LinAlgError from a failed Cholesky attempt as a signal the matrix isn't positive-definite, rather than assuming it will always succeed
  • Use the resulting lower-triangular matrix L for efficient random sampling from a multivariate normal distribution, one of its most common practical applications

Interview Question

Why is Cholesky decomposition generally faster than a general-purpose decomposition like LU, and what's the trade-off?

Hint: Think about what extra structural assumption Cholesky decomposition relies on.

Cholesky decomposition specifically exploits the guaranteed symmetry and positive-definiteness of its input to skip roughly half of the computational work that a general LU decomposition would need to do for an arbitrary matrix without those guarantees — it only needs to compute and store a single triangular factor, rather than separate lower and upper factors and pivoting information. The trade-off is that it only works at all for matrices satisfying those specific properties; passing it a matrix that isn't symmetric and positive-definite raises an error, unlike LU decomposition, which handles a much broader class of square matrices.

Exercises

MediumPractice using np.linalg.cholesky() in a real scenario.
View Solution
import numpy as np

A = np.array([[4, 2], [2, 3]])
L = np.linalg.cholesky(A)
print(L)

Frequently Asked Questions

Why is Cholesky decomposition generally faster than a general-purpose decomposition like LU, and what's the trade-off?

Cholesky decomposition specifically exploits the guaranteed symmetry and positive-definiteness of its input to skip roughly half of the computational work that a general LU decomposition would need to do for an arbitrary matrix without those guarantees — it only needs to compute and store a single triangular factor, rather than separate lower and upper factors and pivoting information. The trade-off is that it only works at all for matrices satisfying those specific properties; passing it a matrix that isn't symmetric and positive-definite raises an error, unlike LU decomposition, which handles a much broader class of square matrices.

Related Functions

np-linalg-invnp-linalg-svdnp-random-normal