
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - Matrix QU Decomposition
Matrix QU Decomposition is a factorization method that decomposes a matrix A into the product of two matrices namely, a unitary matrix Q and an upper triangular matrix U. This method is closely related to QR Decomposition but with some distinct differences.
When we give a matrix A of size m x n then the QU Decomposition factorizes it into two matrices as −
A=QU
Where −
- Q is an orthogonal or unitary matrix which means QTQ = I for real matrices or QHQ = I for complete matrices where QT denotes the transpose of Q and QH denotes conjugate transpose.
- U is an triangular matrix which means all the elements below the diagonal are zero. The matrix U has the same shape as A.
The QU decomposition is particularly useful when working with least squares problems or in cases where we need to solve linear systems efficiently. It can also be seen as an alternative to QR decomposition when working with different matrix properties.
Properties of QU Decomposition
The matrix Q in QU decomposition is orthogonal if A is a real matrix or unitary if A is a complex matrix. This ensures that multiplying by Q preserves the length of vectors i.e., it is a rotation or reflection.
The matrix U is upper triangular which means it has non-zero entries only on and above the diagonal.
Steps for QU Decomposition
While Scipy does not have a direct implementation of QU decomposition but we can achieve the equivalent decomposition by using QR decomposition scipy.linalg.qr(). The R matrix from QR decomposition corresponds to the U in QU decomposition and Q remains orthogonal or unitary.
Following are the steps to be followed to implement the QU Decomposition in scipy −
- Initialization: Start with the matrix A of size mn.
- Apply Orthogonalization: By using an orthogonalization method such as Gram-Schmidt which is used to generate the orthogonal matrix Q.
- Construct Upper Triangular Matrix: From the orthogonalized vectors we can form the upper triangular matrix U. This can be done by solving for the coefficients that ensure the matrix product QU matches the original matrix A.
- Matrix Factorization: The matrix A is now decomposed into Q and U whereA = QU.
Implementation of QU Decomposition in Scipy
As we know that the Scipy provides methods to perform matrix decompositions like QR decomposition but the QU decomposition is not directly available in Scipy. However since QR decomposition can be seen as a special case of QU decomposition when the matrix is square we can use the QR decomposition function scipy.linalg.qr() for a similar purpose and the results of QR decomposition will give us Q and R where Ris upper triangular.
Here is the example which we can use QR Decomposition in scipy −
import numpy as np from scipy.linalg import qr # Define a matrix A A = np.array([[1, 2], [3, 4], [5, 6]]) # Perform QR decomposition Q, R = qr(A) print("Matrix Q:") print(Q) print("Matrix R (equivalent to U in QU decomposition):") print(R)
Here is the output of the QU Decomposition computed using the scipy.linalg.qr() function −
Matrix Q: [[-0.16903085 0.89708523 0.40824829] [-0.50709255 0.27602622 -0.81649658] [-0.84515425 -0.34503278 0.40824829]] Matrix R (equivalent to U in QU decomposition): [[-5.91607978 -7.43735744] [ 0. 0.82807867] [ 0. 0. ]]
Here is an another example which computes the QU Decomposition for the square matrix of the shape 3 x 3 −
import numpy as np from scipy.linalg import qr # Define a 3x3 square matrix A = np.array([[2, 4, 1], [6, 5, 3], [1, 2, 7]]) # Perform QR decomposition Q, R = qr(A) # Interpret R as U in QU decomposition U = R print("Original Matrix A:") print(A) print("\nOrthogonal Matrix Q:") print(Q) print("\nUpper Triangular Matrix U:") print(U) print("\nReconstructed A (Q * U):") print(Q @ U)
Following is the output of the QU Decomposition computed for the square using the scipy.linalg.qr() function −
Original Matrix A: [[2 4 1] [6 5 3] [1 2 7]] Orthogonal Matrix Q: [[-3.12347524e-01 8.38116355e-01 -4.47213595e-01] [-9.37042571e-01 -3.49215148e-01 -1.11022302e-16] [-1.56173762e-01 4.19058177e-01 8.94427191e-01]] Upper Triangular Matrix U: [[-6.40312424 -6.24695048 -4.21669157] [ 0. 2.44450604 2.72387815] [ 0. 0. 5.81377674]] Reconstructed A (Q * U): [[2. 4. 1.] [6. 5. 3.] [1. 2. 7.]]
Applications of QU Decomposition
Following are the applications of the QU Decomposition −
- Solving Linear Systems: QU decomposition is often used in solving over-determined or under-determined systems of linear equations.
- Least Squares Problems: In optimization and regression problems the QU decomposition helps in finding the best fit line or hyperplane by solving the least squares problem.
- Eigenvalue Problems: It can be used to find eigenvalues and eigenvectors for certain types of matrices.