QR Decomposition

$$A = Q*R$$

While the LU decomposition is limited with square matrices, $QR$ decomposition does not have this limitation. In the QR decomposition, we factor a matrix to return a matrix $Q$ and $R$ where:

1. $Q$ is an othorgonal matrix:

$$Q*Q^T = I $$ and $$ Q=Q^{-1}$$

2. $R$ is Upper triangular matrix.

Implementation in Numpy

Performing QR decomposition in numpy can be done using the $qr()$ method which takes in a matrix.

import numpy as np

A = np.array(np.linspace(1, 12, 12)).reshape(4,3)
A
array([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.], [10., 11., 12.]])
Q,R = np.linalg.qr(A)
Q
array([[-0.07761505, -0.83305216, 0.53358462], [-0.31046021, -0.45123659, -0.8036038 ], [-0.54330537, -0.06942101, 0.00645373], [-0.77615053, 0.31239456, 0.26356544]])
R
array([[-1.28840987e+01, -1.45916299e+01, -1.62991610e+01], [ 0.00000000e+00, -1.04131520e+00, -2.08263040e+00], [ 0.00000000e+00, 0.00000000e+00, -3.39618744e-15]])

Using the dot product to combine the factors, we get:

Q.dot(R)
array([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.], [10., 11., 12.]])