def qr_fact_househ(matrixA): m = matrixA.shape[0] n = matrixA.shape[1] #get Householder vector for first column of A H = make_h(matrixA.T[0]) Q = H for i in range(n): result = np.dot(Q, matrixA) result = result.T col = result[i, i:] smaller_matrix = make_h(col) height_1 = len(col) I = np.eye(n) #reverse all the H and V entries in matrix reverse = np.flipud(np.fliplr(smaller_matrix)) #want to insert smaller_matrix into H2 (matrix of zeros) for r in range(reverse.shape[0]): for c in range(reverse.shape[1]): I[I.shape[0] - r - 1, I.shape[1] - c - 1] = reverse[r, c] Q = mult(I, Q) R = mult(Q, matrixA) errorMatrix = np.dot(Q, R) - A error = find_max(errorMatrix) x = solve_qr_b(Q, R, b) return Q.T, R, error, x
def qr_fact_givens(matrixA): Q = np.eye(matrixA.shape[0]) R = matrixA for y in range(matrixA.shape[1]): for num in range(matrixA.shape[0]): if y is num: pivot = (num, y) if y < num: identity = np.eye(matrixA.shape[0]) cosX, sinX = make_givens(R, pivot, (num, y)) identity[num][num] = cosX identity[y][y] = cosX identity[y][num] = sinX identity[num][y] = -sinX R = mult(identity, R) Q = Q.dot(identity.T) #error value of ||QR - A|| errorValue = 0 errorMatrix = mult(Q,R) - A for i in range(errorMatrix.shape[0]): for j in range(errorMatrix.shape[1]): if (errorValue < errorMatrix[i, j]): errorValue = errorMatrix[i, j] x = solve_qr_b(Q, R, b) return Q, R, errorValue, x