def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' rl=mat2rowdict(A) l=len(A.D[0]) m={} for i in range(l) : b = l * [0] b[i] = 1 m[i] = triangular_solve_n(rl, b) return coldict2mat(m)
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' B = {} rows = mat2rowdict(A) for i in A.D[1]: v = Vec(A.D[0], {i:1}) u = triangular_solve_n (rows, v) B[i] = u return coldict2mat(B)
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' A_as_list = list(mat2rowdict(A).values()) col_vectors = [] for i in range(len(list(mat2coldict(A).values()))): v = Vec(A.D[0],{}) v[i] = 1 col_vectors.append(triangular_solve_n(A_as_list,v)) return coldict2mat(col_vectors) print(A_as_list)
def find_triangular_matrix_inverse(A): ''' Supporting GF2 is not required. Input: - A: an upper triangular Mat with nonzero diagonal elements Output: - Mat that is the inverse of A Example: >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' rows = mat2rowdict(A) cols = mat2coldict(identity(A.D[0], 1)) B = coldict2mat({x:triangular_solve_n(rows, cols[x]) for x in cols}) return B
def find_triangular_matrix_inverse(A): ''' Supporting GF2 is not required. Input: - A: an upper triangular Mat with nonzero diagonal elements Output: - Mat that is the inverse of A Example: >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' rows = mat2rowdict(A) cols = mat2coldict(identity(A.D[0], 1)) B = coldict2mat({x: triangular_solve_n(rows, cols[x]) for x in cols}) return B
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 ''' Q,R = factor(A) return triangular_solve_n(mat2rowdict(R),Q.transpose()*b)
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' from matutil import identity,mat2coldict,mat2rowdict from triangular import triangular_solve_n identity_matrix = identity(A.D[0],one) identity_dict = mat2coldict(identity_matrix) rowsdict = mat2rowdict(A) rowslist = [ rowsdict[i] for i in rowsdict.keys() ] return coldict2mat([ triangular_solve_n(rowslist,identity_dict[i]) for i in identity_dict.keys() ])
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 ''' import QR Q, R = QR.factor(A) c = transpose(Q) * b x = triangular_solve_n(mat2rowdict(R), c) return x
def find_triangular_matrix_inverse(A): ''' Supporting GF2 is not required. Input: - A: an upper triangular Mat with nonzero diagonal elements Output: - Mat that is the inverse of A Example: >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' rows = len(A.D[0]) I=[[1 if i == j else 0 for j in range(rows)] for i in range(rows)] B = list(mu.mat2rowdict(A).values()) C =[tr.triangular_solve_n(B, i) for i in I] D = mu.coldict2mat(C) return D
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' from matutil import identity, mat2coldict, mat2rowdict from triangular import triangular_solve_n identity_matrix = identity(A.D[0], one) identity_dict = mat2coldict(identity_matrix) rowsdict = mat2rowdict(A) rowslist = [rowsdict[i] for i in rowsdict.keys()] return coldict2mat([ triangular_solve_n(rowslist, identity_dict[i]) for i in identity_dict.keys() ])
def solve8(A, Q, R, b): c = transpose(Q) * b x = triangular_solve_n(mat2rowdict(R), c) return x
## Problem 7 # Write your solution for this problem in orthonormalization.py. ## Problem 8 # Please give each solution as a Vec from triangular import triangular_solve_n least_squares_A1 = listlist2mat([[8, 1], [6, 2], [0, 6]]) least_squares_Q1 = listlist2mat([[.8,-0.099],[.6, 0.132],[0,0.986]]) least_squares_R1 = listlist2mat([[10,2],[0,6.08]]) least_squares_b1 = list2vec([10, 8, 6]) x_hat_1 = triangular_solve_n([list2vec(v) for v in [[10,2],[0,6.08]]], least_squares_b1*least_squares_Q1) least_squares_A2 = listlist2mat([[3, 1], [4, 1], [5, 1]]) least_squares_Q2 = listlist2mat([[.424, .808],[.566, .115],[.707, -.577]]) least_squares_R2 = listlist2mat([[7.07, 1.7],[0,.346]]) least_squares_b2 = list2vec([10,13,15]) x_hat_2 = triangular_solve_n([list2vec(v) for v in [[7.07, 1.7],[0,.346]]], least_squares_b2*least_squares_Q2) ## Problem 9 def QR_solve(A, b): ''' Input:
## Problem 7 # Write your solution for this problem in orthonormalization.py. ## Problem 8 # Please give each solution as a Vec least_squares_A1 = listlist2mat([[8, 1], [6, 2], [0, 6]]) least_squares_Q1 = listlist2mat([[.8,-0.099],[.6, 0.132],[0,0.986]]) least_squares_R1 = listlist2mat([[10,2],[0,6.08]]) least_squares_b1 = list2vec([10, 8, 6]) x_hat_1 = triangular_solve_n(mat2rowdict(least_squares_R1),least_squares_Q1.transpose()*least_squares_b1) least_squares_A2 = listlist2mat([[3, 1], [4, 1], [5, 1]]) least_squares_Q2 = listlist2mat([[.424, .808],[.566, .115],[.707, -.577]]) least_squares_R2 = listlist2mat([[7.07, 1.7],[0,.346]]) least_squares_b2 = list2vec([10,13,15]) x_hat_2 = triangular_solve_n(mat2rowdict(least_squares_R2),least_squares_Q2.transpose()*least_squares_b2) ## Problem 9 def QR_solve(A, b): ''' Input:
from vec import Vec import triangular def list2vec(L): return Vec(set(range(len(L))), {x: y for x, y in enumerate(L)}) print(list2vec([3, 2, 1])) rowlist = [list2vec([2, 3, -4]), list2vec([0, 1, 2]), list2vec([0, 0, 5])] b = [10, 3, 15] x = triangular.triangular_solve_n(rowlist, b) print(x)
## Problem 6 # Write your solution for this problem in orthonormalization.py. ## Problem 7 # Write your solution for this problem in orthonormalization.py. ## Problem 8 # Please give each solution as a Vec from triangular import triangular_solve_n least_squares_A1 = listlist2mat([[8, 1], [6, 2], [0, 6]]) least_squares_Q1 = listlist2mat([[.8, -0.099], [.6, 0.132], [0, 0.986]]) least_squares_R1 = listlist2mat([[10, 2], [0, 6.08]]) least_squares_b1 = list2vec([10, 8, 6]) x_hat_1 = triangular_solve_n([list2vec(v) for v in [[10, 2], [0, 6.08]]], least_squares_b1 * least_squares_Q1) least_squares_A2 = listlist2mat([[3, 1], [4, 1], [5, 1]]) least_squares_Q2 = listlist2mat([[.424, .808], [.566, .115], [.707, -.577]]) least_squares_R2 = listlist2mat([[7.07, 1.7], [0, .346]]) least_squares_b2 = list2vec([10, 13, 15]) x_hat_2 = triangular_solve_n([list2vec(v) for v in [[7.07, 1.7], [0, .346]]], least_squares_b2 * least_squares_Q2) ## Problem 9 def QR_solve(A, b): ''' Input: - A: a Mat
def solve8(A,Q,R,b): c = transpose(Q) * b x = triangular_solve_n(mat2rowdict(R), c) return x