import numpy as np
def qr(A):
n = A.shape[0]
Q = np.empty_like(A)
R = np.zeros((n,n))
for j in range(n):
Q[:,j] = A[:,j]
for i in range(j):
R[i,j] = Q[:,i].dot(A[:,j])
Q[:,j] -= R[i,j]*Q[:,i]
R[j,j] = np.linalg.norm(Q[:,j])
Q[:,j] /= R[j,j]
return Q,R
We generate a random matrix and test this.
n = 4
A = np.random.rand(n,n)
print('A =\n',A)
Q,R = qr(A)
print('Q =\n',Q)
print('R =\n',R)
print('A-QR =\n',A-Q.dot(R))
A = [[0.65740643 0.80644261 0.58714836 0.5815085 ] [0.05973184 0.15401564 0.97914101 0.03653573] [0.6902891 0.9879373 0.73121611 0.73777795] [0.24723405 0.59180597 0.40238936 0.10926068]] Q = [[ 0.66633695 -0.41784852 0.05290702 -0.61530361] [ 0.06054327 0.24979359 0.9661765 -0.02099151] [ 0.69966631 0.06432462 -0.04504459 0.71014111] [ 0.2505926 0.87113037 -0.24834412 -0.3415559 ]] R = [[ 0.9865976 1.38621575 1.0629621 0.93327088] [ 0. 0.28059011 0.39681286 -0.09121849] [ 0. 0. 0.84421894 0.00569869] [ 0. 0. 0. 0.1280366 ]] A-QR = [[0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] [0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] [0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] [0.00000000e+00 0.00000000e+00 5.55111512e-17 0.00000000e+00]]
def mqr(A):
n = A.shape[0]
Q = A.copy()
R = np.zeros((n,n))
for i in range(n):
R[i,i] = np.linalg.norm(Q[:,i])
Q[:,i] /= R[i,i]
for j in range(i+1,n):
R[i,j] = Q[:,i].dot(Q[:,j])
Q[:,j] -= R[i,j]*Q[:,i]
return Q,R
Q,R = mqr(A)
print('Q =\n',Q)
print('R =\n',R)
print('A-QR =\n',A-Q.dot(R))
Q = [[ 0.66633695 -0.41784852 0.05290702 -0.61530361] [ 0.06054327 0.24979359 0.9661765 -0.02099151] [ 0.69966631 0.06432462 -0.04504459 0.71014111] [ 0.2505926 0.87113037 -0.24834412 -0.3415559 ]] R = [[ 0.9865976 1.38621575 1.0629621 0.93327088] [ 0. 0.28059011 0.39681286 -0.09121849] [ 0. 0. 0.84421894 0.00569869] [ 0. 0. 0. 0.1280366 ]] A-QR = [[0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] [0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] [0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] [0.00000000e+00 0.00000000e+00 5.55111512e-17 0.00000000e+00]]