def orthonormal_projection_orthogonal(W, b): ''' Input: - W: Mat whose rows are orthonormal - b: Vec whose labels are equal to W's column labels Output: - The projection of b orthogonal to W's row space. Example: >>> W = Mat(({0, 1}, {0, 1, 2}), {(0, 1): 0, (1, 2): 0, (0, 0): 1, (1, 0): 0, (0, 2): 0, (1, 1): 1}) >>> b = Vec({0, 1, 2},{0: 3, 1: 1, 2: 4}) >>> orthonormal_projection_orthogonal(W, b) == Vec({0, 1, 2},{0: 0, 1: 0, 2: 4}) True >>> W1 = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 1, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): -1, (2, 2): -1, (0, 2): 0, (2, 1): 0, (1, 1): 0}) >>> b1 = Vec({0, 1, 2},{0: 7, 1: 1, 2: 9}) >>> print(test_format(orthonormal_projection_orthogonal(W1, b1))) Vec({0.000000, 1.000000, 2.000000}, {}) >>> W2 = Mat(({0, 1}, {0, 1, 2}), {(0, 1): 1, (1, 2): 0.6, (0, 0): 0, (1, 0): -0.8, (0, 2): 0, (1, 1): 0}) >>> b2 = Vec({0, 1, 2},{0: 1, 1: 2, 2: 3}) >>> print(test_format(orthonormal_projection_orthogonal(W2, b2))) Vec({0.000000, 1.000000, 2.000000}, {0.000000: 1.800000, 2.000000: 2.400000}) >>> W3 = Mat(({'a','b'}, {'A','B','C','D'}), {('a','A'):1/2, ('a','B'):1/2, ('a','C'):1/2, ('a','D'):1/2,('b','A'):1/2,('b','B'):-1/2, ('b','C'):1/2, ('b','D'):-1/2}) >>> b3 = Vec({'A','B','C','D'},{'A': 8, 'B': 2, 'C': 4, 'D':1}) >>> print(test_format(orthonormal_projection_orthogonal(W3, b3))) Vec({'A', 'B', 'C', 'D'}, {'A': 2.000000, 'B': 0.500000, 'C': -2.000000, 'D': -0.500000}) ''' list2 = [] list1 = [] x = mat2rowdict(W) for i in W.D[0]: list1.append(x[i]) for i in list1: result = project_along(b,i,eps=1e-20) list2.append(result) return b - sum(list2)
def QR_solve(A, b): ''' Input: - A: a Mat - b: a Vec Output: - vector x that minimizes norm(b - A*x) Example: >>> domain = ({'a','b','c'},{'A','B'}) >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2}) >>> Q, R = QR.factor(A) >>> b = Vec(domain[0], {'a': 1, 'b': -1}) >>> x = QR_solve(A, b) >>> result = A.transpose()*(b-A*x) >>> result * result < 1E-10 True I used the following as args to triangular_solve() and the grader was happy: rowlist = mat2rowdict(R) (mentioned in the pdf) label_list = sorted(A.D[1], key=repr) (mentioned in the pdf) b = c vector b = Vec(domain[0], {'a': 1, 'b': -1})(mentioned above and on slide 1 of orthogonalization 8) ''' Q, R = QR.factor(A) rowlist = mat2rowdict(R) label_list = sorted(A.D[1], key = repr) return solve(A,b)
def aug_orthonormalize(L): ''' Input: - L: a list of Vecs Output: - A pair Qlist, Rlist such that: * coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist) * Qlist = orthonormalize(L) ''' vstarlist, tcols = aug_orthogonalize(L) sigmas = [sqrt(v*v) for v in vstarlist] qlist = orthonormalize(vstarlist) T = coldict2mat(tcols) trows = mat2rowdict(T) rrows = [adjust(trows[k], sigmas[k]) for k in trows] rdict = mat2coldict(rowdict2mat(rrows)) rlist = [rdict[k] for k in rdict] return qlist, rlist
def QR_solve(A, b): ''' Input: - A: a Mat - b: a Vec Output: - vector x that minimizes norm(b - A*x) Example: >>> domain = ({'a','b','c'},{'A','B'}) >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2}) >>> Q, R = factor(A) >>> b = Vec(domain[0], {'a': 1, 'b': -1}) >>> x = QR_solve(A, b) >>> result = A.transpose()*(b-A*x) >>> result * result < 1E-10 True ''' Q, R = factor(A) c = Q.transpose()*b Rdict = mat2rowdict(R) Rrows = [Rdict[key] for key in Rdict] labels = sorted(A.D[1], key=repr) return triangular_solve(Rrows, labels, c)