Calculating the QR decomposition of a matrix
Similar to the LU decomposition, QR decomposition is the decomposition of an original matrix into its constituent parts.
In this particular case, the matrix A = QR, where Q is an orthogonal matrix and R is an upper triangular matrix.
Before getting into further details, let us look into the properties of an orthogonal matrix:
- It is a square matrix
- Multiplying Q with its transpose results in an identity matrix
How to do it…
QR decomposition can be done by using the qr
function within scipy.linalg
.
In the following code, let us look into how QR decomposition works:
- Load the relevant packages:
import scipy.linalg as linalg import numpy as np
- Initialize a matrix:
A = np.array([ [2., 1., 1.], [1., 3., 2.], [1., 0., 0]])
- Extract the
Q
andR
matrices of the matrixA
:
Q, R = linalg.qr(A)
- Check the output values:
print(Q) [[-0.81649658 0.27602622 -0.50709255] [-0.40824829 -0.89708523 0.16903085] [-0.40824829 0.34503278 0.84515425]] print(R) [[-2.44948974 -2...