"Complete" QR vs Reduced QR

In [37]:
import numpy as np
import numpy.linalg as la
In [38]:
A = np.random.randn(20, 5)
In [39]:
Q, R = la.qr(A)
In [40]:
print(Q.shape)
print(R.shape)
(20, 5)
(5, 5)

In [41]:
la.norm(Q.dot(R) - A)
Out[41]:
3.580159108734146e-15

Now try adding "complete" as an argument to qr().


Let's make our own 'complete' QR.

In [42]:
m, n = A.shape

Qnew = np.zeros((m,m))
Qnew[:m,:n] = Q
In [43]:
for i in range(n, m):
    q = np.random.randn(m)
    for j in range(i):
        prev_q = Qnew[:, j]
        q = q - prev_q.dot(q)*prev_q
    q = q/la.norm(q)
    Qnew[:, i] = q
In [44]:
Rnew = np.zeros((m,n))
Rnew[:n,:n] = R
In [45]:
la.norm(
    Qnew.T.dot(Qnew) - np.eye(m))
Out[45]:
9.2925073774828479e-15
In [46]:
la.norm(
    Qnew.dot(Rnew) - A)
Out[46]:
3.580159108734146e-15