def least_squares_house(X, Y, n): array = [] for n in range(0, n + 1): tn = [] for i in X: tn.append(i**n) array.append(tn) A = np.array(array).T (Q, R) = house_qr(A.copy()) y = np.dot(Q.T, Y.copy()) sol = la.solve(R, y) residual = la.norm(np.dot(A, sol) - Y) / la.norm(Y) return sol, residual
def least_squares_house(X, Y, n): array = [] for n in range(0, n+1): tn = [] for i in X: tn.append(i**n) array.append(tn) A = np.array(array).T (Q,R) = house_qr(A.copy()) y = np.dot(Q.T,Y.copy()) sol = la.solve(R, y) residual = la.norm(np.dot(A,sol) - Y)/la.norm(Y) return sol, residual
def result(m,n): np.random.seed(0) A = np.random.randn(m, n) print "matrix shape: \t" print A.shape Q1, R1 = mgs_qr(np.copy(A)) error = la.norm(np.dot(Q1,R1)-A)/la.norm(A) print "mgs relative error: \t%g" % error Q2, R2 = house_qr(np.copy(A)) error = la.norm(np.dot(Q2,R2)-A)/la.norm(A) print "house relative error: \t%g" % error cond = la.cond(A) print "condition number of A: \t%g" % cond
def result(m, n): np.random.seed(0) A = np.random.randn(m, n) print "matrix shape: \t" print A.shape Q1, R1 = mgs_qr(np.copy(A)) error = la.norm(np.dot(Q1, R1) - A) / la.norm(A) print "mgs relative error: \t%g" % error Q2, R2 = house_qr(np.copy(A)) error = la.norm(np.dot(Q2, R2) - A) / la.norm(A) print "house relative error: \t%g" % error cond = la.cond(A) print "condition number of A: \t%g" % cond