AppDividend
Latest Code Tutorials

Numpy linalg cholesky: How to Use np cholesky() in Python

0

Python numpy.linalg.cholesky() is used to get Cholesky decomposition value. Let’s understand what Cholesky decomposition is. If we have L * L.H, of a square matrix a, where L is the lower triangle and .H is the conjugate transpose operator (which is the ordinary transpose value), must be Hermitian (symmetric if real-value) and clearly defined. Only L is returned.

Cholesky decomposition is sometimes used to get the fast result of 

Ax = b, where A is both Hermitian/symmetric and positive-definite).

First, we solve for y in 

Ly = b

and then for in

L.Hx = y

Syntax

numpy.linalg.cholesky(arr)

Parameters

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 error.

Programming Example

Finding Cholesky value

# 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]]

Explanation

In this example, we have first made a numpy array, which is printed later. Then we called numpy.linalg.cholesky() value and printed it. As discussed above, we have then verified the Cholesky property that L*L.H=Array. 

So, we can see that the verified value is the same as our original numpy array.

Finding Cholesky value when input is an array_like or matrix

You can find 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]]

Explanation

In this example, we have first made an array that is printed later. 

Then we have calculated the Cholesky value when the given array is an array-like object and a matrix.
As discussed above, we have then verified the Cholesky property that L*L.H=Array. 

So, we can see that the verified value is the same as our original numpy array. But here, the first ans1 is of the type array object, and arr2 is of type matrix object, which was returned.

That is it for the numpy linalg cholesky() function in Python.

See also

Numpy kron()

Numpy matrix_power()

Numpy outer()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.