import numpy as np
import numpy.linalg as la
A = np.random.randn(20, 5)
Q, R = la.qr(A)
print(Q.shape)
print(R.shape)
la.norm(Q.dot(R) - A)
Now try adding "complete"
as an argument to qr()
.
Let's make our own 'complete' QR.
m, n = A.shape
Qnew = np.zeros((m,m))
Qnew[:m,:n] = Q
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
Rnew = np.zeros((m,n))
Rnew[:n,:n] = R
la.norm(
Qnew.T.dot(Qnew) - np.eye(m))
la.norm(
Qnew.dot(Rnew) - A)