What is Cholesky decomposition?
A square matrix A is said to have Cholesky decomposition if it can be written as a product of a lower triangular matrix and its conjugate transpose.
If all the entries of A are real numbers, then the conjugate transpose of L is the same as the transpose of L. So, A can be decomposed into a lower triangular matrix and its transpose. Please note that all the entries of the principal diagonal of A should be positive real entries.
How to perform Cholesky decomposition using Python?
We can use the following Python code to perform Cholesky decomposition using NumPy.
import numpy from numpy.linalg import LinAlgError n = 3 A = numpy.array([[3, 0], [0, 7]]) try: L = numpy.linalg.cholesky(A) L_conj_trans = L.transpose().conj() print("Matrix A: \n", A) print("L: \n", L) print("Conjugate Transpose of L: \n", L_conj_trans) A_cal = L.dot(L_conj_trans) if numpy.allclose(A, A_cal): print("Cholesky decomposition is successful") else: print("A could not be decomposed using Cholesky factorization") except LinAlgError: print("Cholesky decomposition is unsuccessful")
Here, A is the following matrix:
We are using the numpy.linalg.cholesky() function for Cholesky decomposition. We get the lower triangular matrix L as the output. After that, we are calculating the conjugate transpose of L using the L.transpose.conj() function.
Please note that if A cannot be decomposed using Cholesky factorization, the program raises a LinAlgError.
After calculating L and conjugate transpose of L, we are performing a dot product of the two matrices using the dot() function.
A_cal = L.dot(L_conj_trans)
After that, we are using the numpy.allclose() function to check whether the dot product of L and L* is the same as the matrix A.
The output of the given program will be:
Matrix A: [[3 0] [0 7]] L: [[1.73205081 0. ] [0. 2.64575131]] Conjugate Transpose of L: [[1.73205081 0. ] [0. 2.64575131]] Cholesky decomposition is successful
0 Comments