w3resource

Apply QR, LU, and Cholesky decompositions using SciPy on a matrix

NumPy: Integration with SciPy Exercise-13 with Solution

Write a NumPy program to create a matrix and apply various SciPy linear algebra decompositions (QR, LU, Cholesky).

Sample Solution:

Python Code:

import numpy as np  # Import NumPy library
from scipy.linalg import qr, lu, cholesky  # Import linear algebra decomposition functions from SciPy

# Create a matrix using NumPy
matrix = np.array([
    [4, 12, -16],
    [12, 37, -43],
    [-16, -43, 98]
])

# Apply QR decomposition
Q, R = qr(matrix)

# Apply LU decomposition
P, L, U = lu(matrix)

# Apply Cholesky decomposition
# Ensure the matrix is positive-definite for Cholesky decomposition
cholesky_matrix = np.array([
    [4, 12, -16],
    [12, 37, -43],
    [-16, -43, 98]
])
L_cholesky = cholesky(cholesky_matrix, lower=True)

# Print the original matrix and the decomposition results
print("Original Matrix:")
print(matrix)

print("\nQR Decomposition:")
print("Q matrix:")
print(Q)
print("R matrix:")
print(R)

print("\nLU Decomposition:")
print("P matrix:")
print(P)
print("L matrix:")
print(L)
print("U matrix:")
print(U)

print("\nCholesky Decomposition:")
print("L matrix:")
print(L_cholesky)

Output:

Original Matrix:
[[  4  12 -16]
 [ 12  37 -43]
 [-16 -43  98]]

QR Decomposition:
Q matrix:
[[-0.19611614 -0.16947544  0.96582428]
 [-0.58834841 -0.76762404 -0.25416428]
 [ 0.78446454 -0.61808689  0.05083286]]
R matrix:
[[-20.39607805 -57.85425987 105.31436457]
 [  0.          -3.8580585  -24.85307452]
 [  0.           0.           0.45749571]]

LU Decomposition:
P matrix:
[[0. 0. 1.]
 [0. 1. 0.]
 [1. 0. 0.]]
L matrix:
[[ 1.          0.          0.        ]
 [-0.75        1.          0.        ]
 [-0.25        0.26315789  1.        ]]
U matrix:
[[-16.         -43.          98.        ]
 [  0.           4.75        30.5       ]
 [  0.           0.           0.47368421]]

Cholesky Decomposition:
L matrix:
[[ 2.  0.  0.]
 [ 6.  1.  0.]
 [-8.  5.  3.]]

Explanation:

  • Import Libraries:
    • Import the NumPy library for creating and manipulating arrays.
    • Import the qr, lu, and "cholesky" functions from SciPy's linear algebra module for matrix decompositions.
  • Create a matrix:
    • Define a symmetric matrix as a NumPy array. Ensure the matrix is symmetric and positive-definite for Cholesky decomposition.
  • Apply QR Decomposition:
    • Use the qr function from SciPy to perform QR decomposition, which decomposes the matrix into an orthogonal matrix Q and an upper triangular matrix R.
  • Apply LU Decomposition:
    • Use the lu function from SciPy to perform LU decomposition, which decomposes the matrix into a permutation matrix P, a lower triangular matrix L, and an upper triangular matrix U.
  • Apply Cholesky Decomposition:
    • Use the "cholesky" function from SciPy to perform Cholesky decomposition, which decomposes the matrix into a lower triangular matrix L. Ensure the matrix is positive-definite.
    • Finally display the original matrix and the results of the QR, LU, and Cholesky decompositions to verify the operations.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Compute various distance metrics using NumPy and SciPy.
Next: Perform optimization using SciPy's optimize module to find function minimum.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.