0
0
SciPydata~5 mins

Cholesky decomposition in SciPy

Choose your learning style9 modes available
Introduction

Cholesky decomposition helps us break a special kind of matrix into simpler parts. This makes solving math problems faster and easier.

When you want to solve systems of linear equations quickly.
When you need to find the square root of a matrix that is symmetric and positive definite.
When working with covariance matrices in statistics or machine learning.
When optimizing algorithms that involve matrix calculations.
When you want to speed up matrix inversion for certain matrices.
Syntax
SciPy
from scipy.linalg import cholesky
L = cholesky(A, lower=True)

A must be a symmetric, positive definite matrix.

The lower=True option returns the lower-triangular matrix L such that A = L @ L.T.

Examples
Decompose a 2x2 matrix and get the lower-triangular matrix.
SciPy
from scipy.linalg import cholesky
import numpy as np
A = np.array([[4, 2], [2, 3]])
L = cholesky(A, lower=True)
print(L)
Decompose a 3x3 matrix and print the result.
SciPy
from scipy.linalg import cholesky
import numpy as np
A = np.array([[25, 15, -5], [15, 18, 0], [-5, 0, 11]])
L = cholesky(A, lower=True)
print(L)
Sample Program

This program shows how to use Cholesky decomposition on a 2x2 matrix. It prints the original matrix, the lower-triangular matrix, and then reconstructs the original matrix by multiplying L and its transpose.

SciPy
from scipy.linalg import cholesky
import numpy as np

# Define a symmetric positive definite matrix
A = np.array([[6, 3], [3, 2]])

# Perform Cholesky decomposition
L = cholesky(A, lower=True)

print("Matrix A:")
print(A)
print("\nLower-triangular matrix L from Cholesky decomposition:")
print(L)

# Verify that A = L @ L.T
reconstructed = L @ L.T
print("\nReconstructed matrix from L @ L.T:")
print(reconstructed)
OutputSuccess
Important Notes

Cholesky decomposition only works if the matrix is symmetric and positive definite.

If the matrix is not positive definite, the function will raise an error.

Using lower=True returns the lower-triangular matrix; lower=False returns the upper-triangular matrix.

Summary

Cholesky decomposition breaks a matrix into a product of a lower-triangular matrix and its transpose.

It is useful for solving equations and working with special matrices efficiently.

Use scipy.linalg.cholesky with a symmetric, positive definite matrix to get the decomposition.