
- 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 - linalg.qr() Function
The scipy.linalg.qr() is a function which is used to compute the QR decomposition of a matrix A, where A is factorized into two matrices Q and R . Here Q is an orthogonal or unitary for complex matrices matrix and R is an upper triangular matrix. This decomposition is useful in solving linear systems,,least squares problems and eigenvalue computations.
This function allows different modes of decomposition including 'full' which returns Q and R of full size and 'economic' returns compact forms. It also handles both real and complex matrices efficiently.
Syntax
Following is the syntax of the function scipy.linalg.qr() to perform qr Decomposition in scipy −
scipy.linalg.qr(a, mode='full', pivoting=False, overwrite_a=False, check_finite=True)
Parameters
Below are the parameters of the scipy.linalg.qr() function −
- a (array-like, shape = (M, N)): The input matrix to be decomposed. It can be a rectangular matrix.
- mode (string, optional, default: 'full'): This parameter specifies the kind of decomposition to compute such as full, economic, r, raw.
- pivoting (bool, default: False): If True then performs QR decomposition with column pivoting which reorders the columns of A to maximize numerical stability. When enabled then it also returns a permutation array piv.
- overwrite_a (bool, default: False): If True then the function will modify the input matrix a in place. If False then a copy of a will be used by leaving a unchanged.
- check_finite (bool, default: True): If True then checks whether the input matrix contains only finite numbers i.e. no NaN or inf values. If False then disables this check to improve performance when the input is guaranteed to be finite.
Return Value
The scipy.linalg.qr() function performs QR decomposition of a matrix A by returning two components as follows −
- Q: An orthogonal or unitary matrix.
- R: An upper triangular matrix.
- piv: A permutation array that shows the column reordering applied during the decomposition.
Optionally when column pivoting is enabled i.e., pivoting=True it also returns,
The specific form of the output depends on the mode parameter which controls whether the full or reduced form of Q and R are returned.
Basic QR Decomposition
Following is the example which uses scipy.linalg.qr() function to perform basic qr Decomposition. This example shows how to perform a basic QR decomposition in full mode which returns the complete orthogonal matrix Q and the upper triangular matrix R −
import numpy as np from scipy.linalg import qr # Example: QR decomposition in 'full' mode A = np.array([[1, 2], [3, 4], [5, 6]]) Q, R = qr(A, mode='full') print("Q (Orthogonal matrix):") print(Q) print("\nR (Upper triangular matrix):") print(R)
Here is the output of the scipy.linalg.qr() function which computes Full mode QR decomposition −
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.]] PS D:\Tutorialspoint> python sample.py Q (Orthogonal matrix): [[-0.16903085 0.89708523 0.40824829] [-0.50709255 0.27602622 -0.81649658] [-0.84515425 -0.34503278 0.40824829]] R (Upper triangular matrix): [[-5.91607978 -7.43735744] [ 0. 0.82807867] [ 0. 0. ]]
QR Decomposition with Pivoting
In this example QR decomposition with column pivoting is performed which reorders the columns of A to improve numerical stability. The permutation array piv indicates the column reordering −
import numpy as np from scipy.linalg import qr A = np.array([[1, 2], [3, 4], [5, 6]]) # Example: QR decomposition with column pivoting Q, R, piv = qr(A, pivoting=True) print("Q (Orthogonal matrix):") print(Q) print("\nR (Upper triangular matrix):") print(R) print("\nPermutation indices (piv):") print(piv)
Below is the output of the scipy.linalg.qr() function which computes pivoting QR decomposition −
Q (Orthogonal matrix): [[-0.26726124 0.87287156 0.40824829] [-0.53452248 0.21821789 -0.81649658] [-0.80178373 -0.43643578 0.40824829]] R (Upper triangular matrix): [[-7.48331477 -5.87974732] [ 0. -0.65465367] [ 0. 0. ]] Permutation indices (piv): [1 0]
QR Decomposition with Economic Mode
This example uses the economic mode of the QR decomposition which returns reduced matrices Q and R with the shape Q being M x min(M, N) and R being min(M,N)N −
import numpy as np from scipy.linalg import qr A = np.array([[1, 2], [3, 4], [5, 6]]) # Example: QR decomposition in 'economic' mode (reduced matrices) Q, R = qr(A, mode='economic') print("Q (Reduced orthogonal matrix):") print(Q) print("\nR (Reduced upper triangular matrix):") print(R)
Following is the output of the scipy.linalg.qr() function which computes Economic QR decomposition −
Q (Orthogonal matrix): [[-0.26726124 0.87287156 0.40824829] [-0.53452248 0.21821789 -0.81649658] [-0.80178373 -0.43643578 0.40824829]] R (Upper triangular matrix): [[-7.48331477 -5.87974732] [ 0. -0.65465367] [ 0. 0. ]] Permutation indices (piv): [1 0] PS D:\Tutorialspoint> python sample.py Q (Reduced orthogonal matrix): [[-0.16903085 0.89708523] [-0.50709255 0.27602622] [-0.84515425 -0.34503278]] R (Reduced upper triangular matrix): [[-5.91607978 -7.43735744] [ 0. 0.82807867]]