def aug_orthonormalize(L): Lstar, sigmadict = aug_orthogonalize(L) norms = [find_norm(l) for l in Lstar] print(Lstar, 'l', norms, 'norms') R = [adjust(sigma, norms) for sigma in sigmadict] Q = [(1 / norm) * l for l, norm in zip(Lstar, norms)] return Q, R
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) ''' A,B = aug_orthogonalize(L) # multipliers = Vec(L[0].D,{}) D = set(range(len(L))) multipliers = Vec(D,{}) # normalize for i in range(len(A)): norm = sqrt(A[i] * A[i]) for key in A[i].D: A[i][key] = A[i][key] / norm multipliers[i] = norm # multiply with the latter matrix for vector in B: for i in vector.D: vector[i] = multipliers[i] * vector[i] return A,B # test #L = [list2vec(v) for v in [[4,3,1,2],[8,9,-5,-5],[10,1,-1,5]]] #expectedQ = [list2vec(x) for x in [[0.73, 0.55, 0.18, 0.37], [0.19, 0.40, -0.57, -0.69], [0.53, -0.65, -0.51, 0.18]]] #actualQ, actualR = aug_orthonormalize(L) #print(str(L)) #print(str(coldict2mat(actualQ)*coldict2mat(actualR)))
def aug_orthonormalize(L): def adjust(v,mul): return Vec(v.D,{i:mul[i]*v[i] for i in v.D}) assert isinstance(L,list) Qstar,Rstar = aug_orthogonalize(L) mul = [sqrt(vstar*vstar) for vstar in Qstar] Qlist = [Qstar[i]/mul[i] for i in range(len(mul))] Rlist = [adjust(r,mul) for r in Rstar] return Qlist,Rlist
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, sigmalist = aug_orthogonalize(L) normlist = [sqrt(vec * vec) for vec in vstarlist] return orthonormalize(vstarlist), [adjust(sigmavec, normlist) for sigmavec in sigmalist]
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) >>> from matutil import coldict2mat >>> print(coldict2mat(Qlist)) <BLANKLINE> 0 1 2 --------------------- a | 0.73 0.187 0.528 b | 0.548 0.403 -0.653 c | 0.183 -0.566 -0.512 d | 0.365 -0.695 0.181 <BLANKLINE> >>> print(coldict2mat(Rlist)) <BLANKLINE> 0 1 2 ------------------ 0 | 5.48 8.03 9.49 1 | 0 11.4 -0.636 2 | 0 0 6.04 <BLANKLINE> >>> print(coldict2mat(Qlist)*coldict2mat(Rlist)) <BLANKLINE> 0 1 2 --------- a | 4 8 10 b | 3 9 1 c | 1 -5 -1 d | 2 -5 5 <BLANKLINE> ''' vstarlist, r_vecs = orthogonalization.aug_orthogonalize( L) # get Rlist vecs Qlist = [normalize(v) for v in vstarlist] # get Qmatrix # transform r_vecs to Rlist for i in range(len(r_vecs)): for j in r_vecs[i].D: r_vecs[i][j] = r_vecs[i][j] * math.sqrt( vstarlist[j] * vstarlist[j]) return Qlist, r_vecs
def aug_orthonormalize(L): ''' Input: - L: a list of Vecs Output: - A pair Qlist, Rlist of lists 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) >>> from matutil import coldict2mat >>> print(coldict2mat(Qlist)) <BLANKLINE> 0 1 2 --------------------- a | 0.73 0.187 0.528 b | 0.548 0.403 -0.653 c | 0.183 -0.566 -0.512 d | 0.365 -0.695 0.181 <BLANKLINE> >>> print(coldict2mat(Rlist)) <BLANKLINE> 0 1 2 ------------------ 0 | 5.48 8.03 9.49 1 | 0 11.4 -0.636 2 | 0 0 6.04 <BLANKLINE> >>> print(coldict2mat(Qlist)*coldict2mat(Rlist)) <BLANKLINE> 0 1 2 --------- a | 4 8 10 b | 3 9 1 c | 1 -5 -1 d | 2 -5 5 <BLANKLINE> ''' def to_R(vecs, sigma): r_rows = mat2rowdict(rowdict2mat(sigma).transpose()) r_rows_scaled = [sqrt(v*v)*r_rows[i] for i, v in enumerate(vecs)] r_cols = mat2coldict(rowdict2mat(r_rows_scaled)) return [r_cols[i] for i in sorted(r_cols.keys())] v, sigma = aug_orthogonalize(L) r = to_R(v, sigma) return (orthonormalize(v), r)
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) ''' Q, R = aug_orthogonalize(L) Qlist = orthonormalize(Q) multipliers = [sqrt(square_norm(q)) for q in Q] Rlist = [adjust(r, multipliers) for r in R] return (Qlist, Rlist)
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) ''' Q, R = aug_orthogonalize(L) Qlist = orthonormalize(L) Rlist = [ adjust(r, [ math.sqrt(x*x) for x in Q ]) for r in R ] #Rlist = [ math.sqrt(x*x) for x in Q ] return Qlist, Rlist
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) >>> from matutil import coldict2mat >>> print(coldict2mat(Qlist)) <BLANKLINE> 0 1 2 --------------------- a | 0.73 0.187 0.528 b | 0.548 0.403 -0.653 c | 0.183 -0.566 -0.512 d | 0.365 -0.695 0.181 <BLANKLINE> >>> print(coldict2mat(Rlist)) <BLANKLINE> 0 1 2 ------------------ 0 | 5.48 8.03 9.49 1 | 0 11.4 -0.636 2 | 0 0 6.04 <BLANKLINE> >>> print(coldict2mat(Qlist)*coldict2mat(Rlist)) <BLANKLINE> 0 1 2 --------- a | 4 8 10 b | 3 9 1 c | 1 -5 -1 d | 2 -5 5 <BLANKLINE> """ vstarlist, r_vecs = orthogonalization.aug_orthogonalize(L) # get Rlist vecs Qlist = [normalize(v) for v in vstarlist] # get Qmatrix # transform r_vecs to Rlist for i in range(len(r_vecs)): for j in r_vecs[i].D: r_vecs[i][j] = r_vecs[i][j] * math.sqrt(vstarlist[j] * vstarlist[j]) return Qlist, r_vecs
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, sigmalist = aug_orthogonalize(L) normlist = [sqrt(vec * vec) for vec in vstarlist] return orthonormalize(vstarlist), [ adjust(sigmavec, normlist) for sigmavec in sigmalist ]
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) ''' orthoL, retR = aug_orthogonalize(L) normL = [sqrt(li * li) for li in orthoL] orthoL = [li / ni for li, ni in zip(orthoL, normL)] for i in range(len(normL)): for iVec in retR: iVec[i] = normL[i] * iVec[i] return (orthoL, retR)
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) ''' orthoL,retR = aug_orthogonalize(L) normL = [sqrt(li*li) for li in orthoL] orthoL = [li/ni for li,ni in zip(orthoL, normL)] for i in range(len(normL)): for iVec in retR: iVec[i] = normL[i]*iVec[i] return (orthoL, retR)
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, sigmavecs = aug_orthogonalize(L) multipliers = [math.sqrt(v*v) for v in Qlist] Rlist = [] for vec in sigmavecs: Rlist.append(list2vec([a*b for a,b in zip(vec2list(vec), multipliers)])) Qlist = orthonormalize(Qlist) return (Qlist, Rlist)
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) ''' def adjust(v,multipliers): return Vec(v.D,{i: v.f[i]*multipliers[i] if i in v.f else 0 for i in v.D}) q,r = aug_orthogonalize(L) Qlist = orthonormalize(q) Rlist = [ adjust(v,[ sqrt(v*v) for v in orthogonalize(L) ]) for v in r ] return Qlist,Rlist
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) >>> from matutil import coldict2mat >>> print(coldict2mat(Qlist)) <BLANKLINE> 0 1 2 --------------------- a | 0.73 0.187 0.528 b | 0.548 0.403 -0.653 c | 0.183 -0.566 -0.512 d | 0.365 -0.695 0.181 <BLANKLINE> >>> print(coldict2mat(Rlist)) <BLANKLINE> 0 1 2 ------------------ 0 | 5.48 8.03 9.49 1 | 0 11.4 -0.636 2 | 0 0 6.04 <BLANKLINE> >>> print(coldict2mat(Qlist)*coldict2mat(Rlist)) <BLANKLINE> 0 1 2 --------- a | 4 8 10 b | 3 9 1 c | 1 -5 -1 d | 2 -5 5 <BLANKLINE> ''' (a, b) = aug_orthogonalize(L) norms = [sqrt(v * v) for v in a] Qlist = [a[i] / norms[i] for i in range(len(a))] scaler = coldict2mat([Vec(b[i].D, {i: norms[i]}) for i in range(len(b))]) Rlist = [scaler * v for v in b] return (Qlist, Rlist)
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) >>> from matutil import coldict2mat >>> print(coldict2mat(Qlist)) <BLANKLINE> 0 1 2 --------------------- a | 0.73 0.187 0.528 b | 0.548 0.403 -0.653 c | 0.183 -0.566 -0.512 d | 0.365 -0.695 0.181 <BLANKLINE> >>> print(coldict2mat(Rlist)) <BLANKLINE> 0 1 2 ------------------ 0 | 5.48 8.03 9.49 1 | 0 11.4 -0.636 2 | 0 0 6.04 <BLANKLINE> >>> print(coldict2mat(Qlist)*coldict2mat(Rlist)) <BLANKLINE> 0 1 2 --------- a | 4 8 10 b | 3 9 1 c | 1 -5 -1 d | 2 -5 5 <BLANKLINE> ''' Qlist, Rlist = aug_orthogonalize(L) norms = [sqrt(x * x) for x in Qlist] Q = [Qlist[x] / norms[x] for x in range(len(norms))] ##Use the norms to scale the vectors in Rlist R = [list2vec([norms[x] * t[x] for x in range(len(norms))]) for t in Rlist] return Q, R
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) >>> from matutil import coldict2mat >>> print(coldict2mat(Qlist)) <BLANKLINE> 0 1 2 --------------------- a | 0.73 0.187 0.528 b | 0.548 0.403 -0.653 c | 0.183 -0.566 -0.512 d | 0.365 -0.695 0.181 <BLANKLINE> >>> print(coldict2mat(Rlist)) <BLANKLINE> 0 1 2 ------------------ 0 | 5.48 8.03 9.49 1 | 0 11.4 -0.636 2 | 0 0 6.04 <BLANKLINE> >>> print(coldict2mat(Qlist)*coldict2mat(Rlist)) <BLANKLINE> 0 1 2 --------- a | 4 8 10 b | 3 9 1 c | 1 -5 -1 d | 2 -5 5 <BLANKLINE> ''' Qlist, Rlist = aug_orthogonalize(L) norms = [sqrt(x*x) for x in Qlist] Q = [Qlist[x] / norms[x] for x in range(len(norms))] ##Use the norms to scale the vectors in Rlist R = [list2vec([norms[x]*t[x] for x in range(len(norms))]) for t in Rlist] return Q,R
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 q, r = aug_orthogonalize(L) qList = orthonormalize(q) mults = [(v*v)**(1/2) for v in q] for vector in r: for i in range(len(vector.D)): vector[i] = vector[i] * mults[i] return qList, r
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) ''' Q, R = aug_orthogonalize(L) Q_mat = matutil.coldict2mat(Q) Q_norm_mat = matutil.coldict2mat(orthonormalize(Q)) R_mat = matutil.coldict2mat(R) R_norm_mat = (Q_mat.transpose() * Q_norm_mat) * R_mat return mat2list(Q_norm_mat), mat2list(R_norm_mat)
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 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 math import sqrt (Qlist, Rlist) = aug_orthogonalize(L) # Rlist rows to be scaled up by norm of Qlist vectors Rmult = [sqrt(v * v) for v in Qlist] Qmult = [1 / x for x in Rmult] return ([x * v for (x, v) in zip(Qmult, Qlist)], [adjust(Rmult, w) for w in Rlist])
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 math import sqrt (Qlist, Rlist) = aug_orthogonalize(L) # Rlist rows to be scaled up by norm of Qlist vectors Rmult = [sqrt(v*v) for v in Qlist] Qmult = [1/x for x in Rmult] return ([x*v for (x,v) in zip(Qmult, Qlist)], [adjust(Rmult, w) for w in Rlist])
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) ''' L1, L2 = aug_orthogonalize(L) L3 = [] for i in L1: temp = 0 for j in i.D: temp += i[j] ** 2 L3.append(temp) L3 = [sqrt(i) for i in L3] Qlist = orthonormalize(L) Rlist = [adjust(L2[i], L3) for i in range(len(L2))] return Qlist, Rlist
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 = list() Rlist = list() normlist = list() vstarlist, sigma_vecs = aug_orthogonalize(L) for vstar,sigma_vec in zip(vstarlist,sigma_vecs): q = normalize(vstar) normlist.append(sqrt(vstar*vstar)) Qlist.append(q) Rlist.append(sigma_vec) for r in range(len(Rlist)): for k in Rlist[r].D: Rlist[r][k] = normlist[k] * Rlist[r][k] return (Qlist, Rlist)