from orthogonalization import orthogonalize
from orthogonalization import aug_orthogonalize
from math import sqrt
from vec import Vec

def normalize(v): return 1/sqrt(v*v) * v

def orthonormalize(L):
    '''
    Input: a list L of linearly independent Vecs
    Output: A list T of orthonormal Vecs such that for all i in
    [1, len(L)], Span L[:i] == Span T[:i]
    '''
    return [normalize(v) for v in orthogonalize(L)]

def adjust(v, multipliers):
    return Vec(v.D, {i:multipliers[i]*v[i] for i in v.D})

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, sigmas = aug_orthogonalize(L)
    Qlist = [normalize(v) for v in vstarlist]
    Rlist = [adjust(s, [sqrt(vstarlist[i]*vstarlist[i])
                        for i in range(len(s.D))]) for s in sigmas]
    return Qlist, Rlist
