Numpy.linalg.cholesky() method “returns the Cholesky decomposition.” L * L.H, of the square matrix a, where L is lower-triangular and .H is the conjugate transpose operator. An a must be Hermitian (symmetric if real-valued) and positive-definite.
The Cholesky decomposition is a factorization of a positive-definite matrix into the product of a lower triangular matrix and its transpose.
Syntax
numpy.linalg.cholesky(arr)
Parameters
arr: The np.cholesky() function takes only one parameter: the given Hermitian (symmetric if all elements are real), a positive-definite input matrix.
Return Value
The cholesky() function returns the upper or lower-triangular Cholesky factor of a. Returns a matrix object if a is a matrix object. If decomposition fails, the given matrix is not a positive-definite; this function returns a LinAlgError.
Example 1: How to Use numpy.linalg.cholesky() method
# Finding Cholesky value
import numpy as np
# Declaring the first array
arr1 = np.array([[2.+0.j, -0.-3.j], [0.+3.j, 5.+0.j]])
print("Original array is :\n", arr1)
# Calculating and printing cholesky value
L = np.linalg.cholesky(arr1)
print("Cholesky value is:\n", L)
# Verifying the output
v_val = np.dot(L, L.T.conj())
print("Verified value is:\n", v_val)
Output
Original array is :
[[ 2.+0.j -0.-3.j]
[ 0.+3.j 5.+0.j]]
Cholesky value is:
[[1.41421356+0.j 0. +0.j ]
[0. +2.12132034j 0.70710678+0.j ]]
Verified value is:
[[2.+0.j 0.-3.j]
[0.+3.j 5.+0.j]]
Example 2: Calculate the Cholesky value when the input is an array or matrix
# Finding Cholesky value when input is an array_like or matrix :
import numpy as np
# Declaring the first array
arr1 = np.array([[2, (-0-3j)], [3j, 5]])
print("Original array is :\n", arr1)
# Calculating and printing Cholesky value
L = np.linalg.cholesky(arr1)
print("Cholesky value1 is:\n", L)
M = np.linalg.cholesky(np.matrix(arr1))
print("Cholesky value2 is:\n", M)
# Verifying the output
v_val1 = np.dot(L, L.T.conj())
print("Verified value1 is:\n", v_val1)
v_val2 = np.dot(M, M.T.conj())
print("Verified value2 is:\n", v_val1)
Output
Original array is :
[[2.+0.j 0.-3.j]
[0.+3.j 5.+0.j]]
Cholesky value1 is:
[[1.41421356+0.j 0. +0.j ]
[0. +2.12132034j 0.70710678+0.j ]]
Cholesky value2 is:
[[1.41421356+0.j 0. +0.j ]
[0. +2.12132034j 0.70710678+0.j ]]
Verified value1 is:
[[2.+0.j 0.-3.j]
[0.+3.j 5.+0.j]]
Verified value2 is:
[[2.+0.j 0.-3.j]
[0.+3.j 5.+0.j]]
That’s it.
Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog. He is also expert in JavaScript and Python development.