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.
  • Optionally when column pivoting is enabled i.e., pivoting=True it also returns,

  • piv: A permutation array that shows the column reordering applied during the decomposition.

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]]
scipy_linalg.htm
Advertisements