def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' rep = vec2rep(w, U_basis + V_basis) rep_u = Vec(set(range(len(U_basis))), {k: v for k, v in rep.f.items() if k < len(U_basis)}) rep_v = Vec(set(range(len(V_basis))), {(k - len(U_basis)): v for k, v in rep.f.items() if k - len(U_basis) >= 0}) u = coldict2mat(U_basis) * rep_u v = coldict2mat(V_basis) * rep_v return (u, v)
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' from hw4 import vec2rep U = coldict2mat(U_basis) V = coldict2mat(V_basis) sum = U_basis + V_basis sol_w = vec2rep(sum,w) lenU = len(U_basis) wu = list2vec ([ v for i, v in sol_w.f.items () if i < lenU ]) wv = list2vec ([ v for i, v in sol_w.f.items () if i >= lenU ]) u = U*wu v = V*wv return (u,v)
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L) == 1: return False tList = copy.deepcopy(L) v_i = tList.pop(i) x = solve(coldict2mat(tList),v_i) res = v_i - coldict2mat(tList) * x if res * res < pow(10,-14) : return True return False pass
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) >>> from vec import Vec >>> D={'a','b','c','d'} >>> L = [Vec(D, {'a':4,'b':3,'c':1,'d':2}), Vec(D, {'a':8,'b':9,'c':-5,'d':-5}), Vec(D, {'a':10,'b':1,'c':-1,'d':5})] >>> Qlist, Rlist = aug_orthonormalize(L) ''' Ql, Rl = aug_orthogonalize(L) l=len(Ql) Qlist=[] Am=[] for v in Ql: A=sqrt(sum([v[i]**2 for i in range(len(v.D))])) Am.append(A) Qlist.append(1/A*v) D=set(range(l)) AmV=[Vec(D,{i:Am[i]}) for i in range(l)] AmM=coldict2mat(AmV) Rlist=AmM*coldict2mat(Rl) return Qlist, Rlist
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> D={'a','b','c','d'} >>> L = [Vec(D, {'a':1,'b':-1}), Vec(D, {'c':-1,'b':1}), Vec(D, {'c':1,'d':-1}), Vec(D, {'a':-1,'d':1}), Vec(D, {'b':1, 'c':1, 'd':-1})] >>> is_superfluous(L,4) False >>> is_superfluous(L,3) True >>> is_superfluous(L,2) True >>> L == [Vec(D,{'a':1,'b':-1}),Vec(D,{'c':-1,'b':1}),Vec(D,{'c':1,'d':-1}),Vec(D, {'a':-1,'d':1}),Vec(D,{'b':1, 'c':1, 'd':-1})] True >>> is_superfluous([Vec({0,1}, {})], 0) True >>> is_superfluous([Vec({0,1}, {0:1})], 0) False ''' if len(L) == 1: return L[0] == -L[0] L = L.copy() b = L.pop(i) r = b - coldict2mat(L) * solve(coldict2mat(L), b) return r * r < 10e-14
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' b = L[i] if i == 0: A = coldict2mat(L[i+1:]) elif i == len(L)-1: A = coldict2mat(L[0:i]) else: A = coldict2mat(L[0:i-1]+L[i+1:]) u = solve(A, b) r = b - A * u return True if r*r < 1.0e-14 else False
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' UV = coldict2mat(U_basis + V_basis) W = solve(UV, w) U = coldict2mat(U_basis) V = coldict2mat(V_basis) Wu = Vec(set(range(len(U_basis))), {x: W[x] for x in range(len(U_basis))}) Wv = Vec(set(range(len(V_basis))), {x: W[len(U_basis) + x] for x in range(len(V_basis))}) u = U * Wu v = V * Wv return (u, v)
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' L_aux = list(L) vector = L_aux.pop(i) sol = solve(coldict2mat(L_aux), vector) residual = vector - coldict2mat(L_aux) * sol if residual * residual < 10**-14: return True else: return False
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' uv_basis = U_basis + V_basis #basis of space U+V x = vec2rep(uv_basis,w) #x is the coordination representation in space U+V : uv_basis*x = w #then split the coordinations : the first len(U_basis) are coordinates in U, the rest are coordinates in V # coordinates in U uc = list2vec([x[i] for i in range(len(U_basis))]) # coordinates in V uv = list2vec([x[i] for i in range(len(U_basis),len(uv_basis))]) u = coldict2mat(U_basis)*uc v = coldict2mat(V_basis)*uv return (u,v)
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' from hw4 import vec2rep U = coldict2mat(U_basis) V = coldict2mat(V_basis) sum = U_basis + V_basis sol_w = vec2rep(sum, w) lenU = len(U_basis) wu = list2vec([v for i, v in sol_w.f.items() if i < lenU]) wv = list2vec([v for i, v in sol_w.f.items() if i >= lenU]) u = U * wu v = V * wv return (u, v)
def QR_solve(A): col_labels = sorted(A.D[1], key=repr) Acolms = dict2list(mat2coldict(A), col_labels) (Qlist, Rlist) = aug_orthogonalize(Acolms) Q = coldict2mat(list2dict(Qlist, col_labels)) R = coldict2mat(list2dict(Rlist, col_labels)) return Q, R
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([ a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' b = L.pop(i) u = solve(coldict2mat(L),b) residual = b - coldict2mat(L)*u if residual * residual < 10e-14: return True else: return False
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' UV = coldict2mat(U_basis + V_basis) W = solve(UV, w) U = coldict2mat(U_basis) V = coldict2mat(V_basis) Wu = Vec(set(range(len(U_basis))),{x: W[x] for x in range(len(U_basis))}) Wv = Vec(set(range(len(V_basis))),{x: W[len(U_basis) + x] for x in range(len(V_basis))}) u = U * Wu v = V * Wv return (u,v)
def QR_factor(A): col_labels = sorted(A.D[1], key=repr) Acols = dict2list(mat2coldict(A),col_labels) Qlist, Rlist = aug_orthonormalize(Acols) #Now make Mats Q = coldict2mat(Qlist) R = coldict2mat(list2dict(Rlist, col_labels)) return Q,R
def QR_factor(A): col_labels = sorted(A.D[1], key=repr) Acols = dict2list(mat2coldict(A), col_labels) Qlist, Rlist = aug_orthonormalize(Acols) #Now make Mats Q = coldict2mat(Qlist) R = coldict2mat(list2dict(Rlist, col_labels)) return Q, R
def direct_sum_decompose(U, V, w): U_copy = U.copy() U_copy.extend(V) linear_comb = solve(coldict2mat(U_copy), w) u_linear_comb = list2vec([linear_comb[i] for i in range(len(U))]) v_linear_comb = list2vec( [linear_comb[i] for i in range(len(U), len(V) + len(U))]) return (coldict2mat(U) * u_linear_comb, coldict2mat(V) * v_linear_comb)
def direct_sum_decompose(U_basis, V_basis, w): UV = coldict2mat(U_basis+V_basis) U = coldict2mat(U_basis) V = coldict2mat(V_basis) W = solve(UV,w) Wu = list2vec([v for i, v in W.f.items() if i < len(U_basis)]) Wv = list2vec([v for i, v in W.f.items() if i >= len(U_basis)]) u = U * Wu v = V * Wv return (u,v)
def direct_sum_decompose(U_basis, V_basis, w): ''' Input: - U_basis: a list of Vecs forming a basis for a vector space U - V_basis: a list of Vecs forming a basis for a vector space V - w: a Vec in the direct sum of U and V Output: - a pair (u, v) such that u + v = w, u is in U, v is in V Example: >>> D = {0,1,2,3,4,5} >>> U_basis = [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> (u, v) = direct_sum_decompose(U_basis, V_basis, w) >>> (u + v - w).is_almost_zero() True >>> U_matrix = coldict2mat(U_basis) >>> V_matrix = coldict2mat(V_basis) >>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero() True >>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero() True >>> ww = Vec(D,{0: 2, 1: 5, 2: 51, 4: 1, 5: 7}) >>> (u, v) = direct_sum_decompose(U_basis, V_basis, ww) >>> (u + v - ww).is_almost_zero() True >>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero() True >>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero() True >>> U_basis == [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] True >>> V_basis == [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] True >>> w == Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) True ''' union_basis = U_basis.copy() union_basis.extend(V_basis) union_matrix = coldict2mat(union_basis) U_matrix = coldict2mat(U_basis) V_matrix = coldict2mat(V_basis) # calculate the w representation in u and v terms w_rep = solve(union_matrix, w) # extracting the u an v representation on U_basis and V_basis respectively u_rep = Vec(U_matrix.D[1], {idx:w_rep[idx] for idx in range(len(U_basis))}) v_rep = Vec(V_matrix.D[1], {idx:w_rep[idx + len(U_basis)] for idx in range(len(V_basis))}) # calculate u and v vectors u = U_matrix * u_rep v = V_matrix * v_rep return (u, v)
def direct_sum_decompose(U_basis, V_basis, w): ''' Input: - U_basis: a list of Vecs forming a basis for a vector space U - V_basis: a list of Vecs forming a basis for a vector space V - w: a Vec in the direct sum of U and V Output: - a pair (u, v) such that u + v = w, u is in U, v is in V Example: >>> D = {0,1,2,3,4,5} >>> U_basis = [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> (u, v) = direct_sum_decompose(U_basis, V_basis, w) >>> (u + v - w).is_almost_zero() True >>> U_matrix = coldict2mat(U_basis) >>> V_matrix = coldict2mat(V_basis) >>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero() True >>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero() True >>> ww = Vec(D,{0: 2, 1: 5, 2: 51, 4: 1, 5: 7}) >>> (u, v) = direct_sum_decompose(U_basis, V_basis, ww) >>> (u + v - ww).is_almost_zero() True >>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero() True >>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero() True >>> U_basis == [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] True >>> V_basis == [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] True >>> w == Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) True >>> ww = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 51, 4: 1, 5: 7}) >>> (u, v) = direct_sum_decompose(U_basis, V_basis, ww) >>> (u + v - ww).is_almost_zero() True ''' uvmat = coldict2mat(U_basis+V_basis) uv = solve(uvmat,w) U_matrix = coldict2mat(U_basis) V_matrix = coldict2mat(V_basis) u = U_matrix*Vec(U_matrix.D[1], {i:uv[i] for i in range(len(U_matrix.D[1]))}) v = V_matrix*Vec(V_matrix.D[1], {i:uv[i+len(U_matrix.D[1])] for i in range(len(V_matrix.D[1]))}) return (u,v)
def direct_sum_decompose(U,V,w): basis_vectors_W = get_basis(U) UV = coldict2mat(basis_vectors_W) W = solve(UV, w) print(W) basis_vectors_W = get_basis(V) UV = coldict2mat(basis_vectors_W) W = solve(UV, w) print(W) basis_vectors_W = get_basis(U+V) UV = coldict2mat(basis_vectors_W) W = solve(UV, w) print(W)
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) ''' Qlist = orthonormalize(L) Rlist = mat2coldict(transpose(coldict2mat(Qlist)) * coldict2mat(L)) newRlist = [Rlist[k] for k in Rlist] return Qlist,newRlist
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) ''' Qlist = orthonormalize(L) Rlist = mat2coldict(transpose(coldict2mat(Qlist)) * coldict2mat(L)) newRlist = [Rlist[k] for k in Rlist] return Qlist, newRlist
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) ''' V, S = aug_orthogonalize(L) Q = orthonormalize(L) R = mat2coldict(transpose(coldict2mat(V)) * coldict2mat(Q) * coldict2mat(S)) return (Q,[x for x in R.values()])
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) ''' V, S = aug_orthogonalize(L) Q = orthonormalize(L) R = mat2coldict( transpose(coldict2mat(V)) * coldict2mat(Q) * coldict2mat(S)) return (Q, [x for x in R.values()])
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) """ from orthogonalization import aug_orthogonalize from matutil import mat2coldict, coldict2mat Qlist = orthonormalize(L) Rlist = mat2coldict(coldict2mat(Qlist).transpose() * coldict2mat(L)) return Qlist, list(Rlist.values())
def direct_sum_decompose(U_basis, V_basis, w): ''' Input: - U_basis: a list of Vecs forming a basis for a vector space U - V_basis: a list of Vecs forming a basis for a vector space V - w: a Vec in the direct sum of U and V Output: - a pair (u, v) such that u + v = w, u is in U, v is in V Example: >>> D = {0,1,2,3,4,5} >>> U_basis = [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> (u, v) = direct_sum_decompose(U_basis, V_basis, w) >>> (u + v - w).is_almost_zero() True >>> U_matrix = coldict2mat(U_basis) >>> V_matrix = coldict2mat(V_basis) >>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero() True >>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero() True >>> ww = Vec(D,{0: 2, 1: 5, 2: 51, 4: 1, 5: 7}) >>> (u, v) = direct_sum_decompose(U_basis, V_basis, ww) >>> (u + v - ww).is_almost_zero() True >>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero() True >>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero() True >>> U_basis == [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] True >>> V_basis == [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] True >>> w == Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) True ''' M = coldict2mat(U_basis+V_basis) solution = solve(M, w) u, v = [], [] U_matrix, V_matrix = coldict2mat(U_basis), coldict2mat(V_basis) for i in range(len(U_basis) + len(V_basis)): if i < len(U_basis): u.append(solution.f[i]) else: v.append(solution.f[i]) return U_matrix*list2vec(u), V_matrix*list2vec(v)
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' coldict_i = {k: L[k] for k in range(len(L)) if k != i} mat_i = coldict2mat(coldict_i) v = solve(mat_i, L[i]) res = mat_i * v - L[i] res_2 = res * res ##print(res_2) return res_2 < 1e-14
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' coldict_i = {k:L[k] for k in range(len(L)) if k!=i } mat_i = coldict2mat(coldict_i) v = solve(mat_i, L[i]) res = mat_i * v - L[i] res_2 = res*res ##print(res_2) return res_2 < 1e-14
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L) == 1: return False copyL = L[:] b = copyL[i] del copyL[i] matrix = coldict2mat(copyL) u = solve(matrix, b) residual = b - matrix * u return (residual * residual) < 10e-14
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' assert i in range(len(L)) A = coldict2mat({c:L[c] for c in range(len(L)) if c!=i}) residual = L[i] - A*solve(A, L[i]) if residual * residual < 1e-14: return True return False
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' assert(len(L) > 0) assert(i < len(L)) if len(L) == 1: return True mtx = coldict2mat([ L[j] for j in range(len(L)) if j != i ]) u = solve(mtx,L[i]) residual = mtx * u - L[i] return residual * residual < 10e-14
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' combined = U_basis + V_basis separation_boundary = len(U_basis) soln = solve(coldict2mat(combined), w) u = Vec(w.D, {}) v = Vec(w.D, {}) for key, val in soln.f.items(): if key < separation_boundary: u += val * U_basis[key] else: v += val * combined[key] return (u, v)
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> D={'a','b','c','d'} >>> L = [Vec(D, {'a':1,'b':-1}), Vec(D, {'c':-1,'b':1}), Vec(D, {'c':1,'d':-1}), Vec(D, {'a':-1,'d':1}), Vec(D, {'b':1, 'c':1, 'd':-1})] >>> is_superfluous(L,4) False >>> is_superfluous(L,3) True >>> is_superfluous(L,2) True >>> L == [Vec(D,{'a':1,'b':-1}),Vec(D,{'c':-1,'b':1}),Vec(D,{'c':1,'d':-1}),Vec(D, {'a':-1,'d':1}),Vec(D,{'b':1, 'c':1, 'd':-1})] True >>> is_superfluous([Vec({0,1}, {})], 0) True >>> is_superfluous([Vec({0,1}, {0:1})], 0) False ''' assert i <= len(L) if len(L) == 1: return False or len(L[i].f) == 0 A = coldict2mat([L[j] for j in range(len(L)) if j != i]) u = solve(A, L[i]) res = L[i] - A * u return res.is_almost_zero()
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L) > 1: A = coldict2mat(L[:i] + L[i + 1:]) v = solve(A, L[i]) r = L[i] - A * v if r * r < 10 ** -14: return True return False
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L)<=1: return False A = coldict2mat([L[index] for index in range(len(L)) if index != i]) b = solve(A,L[i]) residue = L[i] - A*b return residue*residue < 1e-14
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L) == 1: return False Li = L.pop(i) mL = coldict2mat(L) sol = solve(mL,Li) res = Li - mL * sol if abs(res*res) < 10**-14: return True else: return False
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L) <= 1: return False # remove L[i] without changing the list (i.e. w/o using L.pop()) b = L[i] # RHS of A*u = b A = coldict2mat(L[:i] + L[i+1:]) # coefficient matrix L w/o L[i] u = solve(A, b) e = b - A*u return e*e < 1e-14
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] # print (str(A.D) + str(A.f)) # print (str(B.D) + str(B.f)) # print('\n') colsB = utils.mat2coldict(B) return utils.coldict2mat({k:A*colsB[k] for k in colsB})
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' testMat = coldict2mat(U_basis + V_basis) coef = solve(testMat, w) Nu = len(U_basis) Nv = len(V_basis) retU = Vec(U_basis[0].D, {}) retV = Vec(V_basis[0].D, {}) for i in range(Nu): retU += coef[i] * U_basis[i] for i in range(Nv): retV += coef[i + Nu] * V_basis[i] return (retU, retV)
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L) == 1: return False elif i >= len(L): return False else: u = L[i] z = Vec(L[0].D, {}) A = coldict2mat(L[:i] + [z] + L[i + 1:]) b = solve(A, u) r = u - A * b return r * r < 1e-14
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] col_dict = mat2coldict(B) res = dict() for c in col_dict.keys(): res[c] = A * col_dict[c] return coldict2mat(res)
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 ''' I = {i: Vec(A.D[0], {i: 1}) for i in A.D[1]} rl = list() ll = list() cd = dict() rd = mat2rowdict(A) for k, i in rd.items(): ll.append(k) rl.append(rd[k]) for k in ll: cd[k] = triangular_solve(rl, ll, I[k]) return coldict2mat(cd)
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L) == 1: return False copyL = L[:] b = copyL[i] del copyL[i] matrix = coldict2mat(copyL) u = solve(matrix, b) residual = b - matrix*u return (residual * residual) < 10e-14
def is_superfluous(L, i): """ Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False """ if len(L) > 1: l = L.copy() b = l.pop(i) M = coldict2mat(l) solution = solve(M, b) residual = M * solution - b if residual * residual < 10e-14: return True return False
def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example: >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), {('y1',0):2, ('y2',0):4, ('y3',0):8, ('y1',1):10, ('y2',1):5, ('y3',1):5, ('y1',2):4, ('y2',2):25, ('y3',2):2, ('y1',3):5, ('y2',3):10, ('y3',3):4}) >>> print(Y_in) 0 1 2 3 ------------ y1 | 2 10 4 5 y2 | 4 5 25 10 y3 | 8 5 2 4 >>> print(mat_move2board(Y_in)) 0 1 2 3 ------------------ y1 | 0.25 2 2 1.25 y2 | 0.5 1 12.5 2.5 y3 | 1 1 1 1 ''' y_in = mat2coldict(Y) y_out = {x:move2board(y_in[x]) for x in y_in.keys()} return coldict2mat(y_out)
def exchange(S, A, z): ''' Input: - S: a list of vectors, as instances of your Vec class - A: a list of vectors, each of which are in S, with len(A) < len(S) - z: an instance of Vec such that A+[z] is linearly independent Output: a vector w in S but not in A such that Span S = Span ({z} U S - {w}) Example: >>> S = [list2vec(v) for v in [[0,0,5,3],[2,0,1,3],[0,0,1,0],[1,2,3,4]]] >>> A = [list2vec(v) for v in [[0,0,5,3],[2,0,1,3]]] >>> z = list2vec([0,2,1,1]) >>> exchange(S, A, z) == Vec({0, 1, 2, 3},{0: 0, 1: 0, 2: 1, 3: 0}) True ''' for i in range(len(S)): w = S[i] if w not in A: tmpL = list(S) tmpV = tmpL.pop(i) tmpL.insert(i,z) tmpMat = coldict2mat(tmpL) sol = solve(tmpMat,tmpV) residual = tmpV - tmpMat * sol if residual * residual < 1.0e-14: return w return None
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L) > 1: l = L.copy() b = l.pop(i) M = coldict2mat(l) solution = solve(M, b) residual = M * solution - b if residual * residual < 10e-14: return True return False
def direct_sum_decompose(U_basis, V_basis, w): ''' input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True ''' sum_base = U_basis + V_basis # find coefficients composed_result = solve(coldict2mat(sum_base),w) # calculate u u = Vec(U_basis[0].D,{}) for i in range(len(U_basis)): u += composed_result[i] * U_basis[i] # calculate v v = Vec(V_basis[0].D,{}) for i in range(len(U_basis), len(sum_base)): v += composed_result[i] * V_basis[i-len(U_basis)] return (u,v)
def is_superfluous(L, i): """ Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False """ if len(L) > 1: b = L.pop(i) A = coldict2mat(L) u = solve(A, b) residual = b - (A * u) condition = u != Vec(A.D[1], {}) and residual * residual < 10 ** -14 L.insert(i, b) else: return False return condition
def exchange(S, A, z): ''' Input: - S: a list of vectors, as instances of your Vec class - A: a list of vectors, each of which are in S, with len(A) < len(S) - z: an instance of Vec such that A+[z] is linearly independent Output: a vector w in S but not in A such that Span S = Span ({z} U S - {w}) Example: >>> S = [list2vec(v) for v in [[0,0,5,3],[2,0,1,3],[0,0,1,0],[1,2,3,4]]] >>> A = [list2vec(v) for v in [[0,0,5,3],[2,0,1,3]]] >>> z = list2vec([0,2,1,1]) >>> exchange(S, A, z) == Vec({0, 1, 2, 3},{0: 0, 1: 0, 2: 1, 3: 0}) True ''' for i in range(len(S)): w = S[i] if w not in A: tmpL = list(S) tmpV = tmpL.pop(i) tmpL.insert(i, z) tmpMat = coldict2mat(tmpL) sol = solve(tmpMat, tmpV) residual = tmpV - tmpMat * sol if residual * residual < 1.0e-14: return w return None
def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example: >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), {('y1',0):2, ('y2',0):4, ('y3',0):8, ('y1',1):10, ('y2',1):5, ('y3',1):5, ('y1',2):4, ('y2',2):25, ('y3',2):2, ('y1',3):5, ('y2',3):10, ('y3',3):4}) >>> print(Y_in) 0 1 2 3 ------------ y1 | 2 10 4 5 y2 | 4 5 25 10 y3 | 8 5 2 4 >>> print(mat_move2board(Y_in)) 0 1 2 3 ------------------ y1 | 0.25 2 2 1.25 y2 | 0.5 1 12.5 2.5 y3 | 1 1 1 1 ''' y_in = mat2coldict(Y) y_out = {x: move2board(y_in[x]) for x in y_in.keys()} return coldict2mat(y_out)
def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example: >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), ... {('y1',0):2, ('y2',0):4, ('y3',0):8, ... ('y1',1):10, ('y2',1):5, ('y3',1):5, ... ('y1',2):4, ('y2',2):25, ('y3',2):2, ... ('y1',3):5, ('y2',3):10, ('y3',3):4}) >>> print(Y_in) <BLANKLINE> 0 1 2 3 ------------ y1 | 2 10 4 5 y2 | 4 5 25 10 y3 | 8 5 2 4 <BLANKLINE> >>> print(mat_move2board(Y_in)) <BLANKLINE> 0 1 2 3 ------------------ y1 | 0.25 2 2 1.25 y2 | 0.5 1 12.5 2.5 y3 | 1 1 1 1 <BLANKLINE> ''' coldict = mat2coldict(Y) return coldict2mat({key:move2board(val) for key, val in coldict.items()})
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' L = L[:] v = L.pop(i) M = coldict2mat(L) u = solve(M, v) r = v - M * u return r * r < 10e-14
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' dist= len(L) if dist == 1: return False A= coldict2mat([L[j] for j in range(dist) if i != j]) b= L[i] u= solve(A,b) residual= b - A*u return (residual*residual) < 1E-14
def direct_sum_decompose(U_basis, V_basis, w): """ input: A list of Vecs, U_basis, containing a basis for a vector space, U. A list of Vecs, V_basis, containing a basis for a vector space, V. A Vec, w, that belongs to the direct sum of these spaces. output: A pair, (u, v), such that u+v=w and u is an element of U and v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})] >>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})] >>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}) >>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})) True """ testMat = coldict2mat(U_basis + V_basis) coef = solve(testMat, w) Nu = len(U_basis) Nv = len(V_basis) retU = Vec(U_basis[0].D, {}) retV = Vec(V_basis[0].D, {}) for i in range(Nu): retU += coef[i] * U_basis[i] for i in range(Nv): retV += coef[i + Nu] * V_basis[i] return (retU, retV)
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' assert i in range(len(L)) if len(L) > 1: colVecs = coldict2mat({ x : L[x] for x in range(len(L)) if x != i }) u = solve(colVecs, L[i]) residual = L[i] - colVecs * u else: residual = 1 return residual * residual < 10e-14
def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example: >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), ... {('y1',0):2, ('y2',0):4, ('y3',0):8, ... ('y1',1):10, ('y2',1):5, ('y3',1):5, ... ('y1',2):4, ('y2',2):25, ('y3',2):2, ... ('y1',3):5, ('y2',3):10, ('y3',3):4}) >>> print(Y_in) <BLANKLINE> 0 1 2 3 ------------ y1 | 2 10 4 5 y2 | 4 5 25 10 y3 | 8 5 2 4 <BLANKLINE> >>> print(mat_move2board(Y_in)) <BLANKLINE> 0 1 2 3 ------------------ y1 | 0.25 2 2 1.25 y2 | 0.5 1 12.5 2.5 y3 | 1 1 1 1 <BLANKLINE> ''' return coldict2mat({c: move2board(r) for c, r in mat2coldict(Y).items()})
def is_superfluous(L, i): ''' Input: - L: list of vectors as instances of Vec class - i: integer in range(len(L)) Output: True if the span of the vectors of L is the same as the span of the vectors of L, excluding L[i]. False otherwise. Examples: >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> is_superfluous(L, 3) True >>> is_superfluous([a0,a1,a2,a3], 3) True >>> is_superfluous([a0,a1,a2,a3], 0) True >>> is_superfluous([a0,a1,a2,a3], 1) False ''' if len(L) == 1: return False subL = [ L[x] for x in range(len(L)) if x != i] u = vec2rep(subL, L[i]) residual = L[i] - coldict2mat({i:subL[i] for i in range(len(subL)) })*u error = residual*residual return True if error < 10e-14 else False