Exemple #1
0
def GF2_span(D, L):
    '''
    >>> from GF2 import one
    >>> D = {'a', 'b', 'c'}
    >>> L = [Vec(D, {'a': one, 'c': one}), Vec(D, {'b': one})]
    >>> len(GF2_span(D, L))
    4
    >>> Vec(D, {}) in GF2_span(D, L)
    True
    >>> Vec(D, {'b': one}) in GF2_span(D, L)
    True
    >>> Vec(D, {'a':one, 'c':one}) in GF2_span(D, L)
    True
    >>> Vec(D, {x:one for x in D}) in GF2_span(D, L)
    True
    '''
    l = []
    multiplied =[]
    for e in L:
        a= scalar_mul(e, 0)
        multiplied.append(a)
        b = scalar_mul(e, 1)
        multiplied.append(b)
    for i in multiplied:
        for j in multiplied:
            final = add(i,j)
            if final not in l:
                l.append(final)
    return l
Exemple #2
0
def GF2_span(D, L):
    '''
    from GF2 import one
    D = {'a', 'b', 'c'}
    L = [Vec(D, {'a': one, 'c': one}), Vec(D, {'b': one})]
    len(GF2_span(D, L))
    4
    Vec(D, {}) in GF2_span(D, L)
    True
    Vec(D, {'b': one}) in GF2_span(D, L)
    True
    Vec(D, {'a':one, 'c':one}) in GF2_span(D, L)
    True
    Vec(D, {x:one for x in D}) in GF2_span(D, L)
    True
    '''
    from GF2 import one
    from itertools import product

    import vec

    prods = [xx for xx in product([0,one], repeat=len(L))]
    final_vecs = []
    for prod in prods:
        vecs = []
        for ii, jj in enumerate(prod):
            vecs.append(vec.scalar_mul(L[ii],jj))
        final_vecs.append(vec_sum(vecs, D))

    return final_vecs
Exemple #3
0
def scale_vecs(vecdict):
    '''
    >>> v1 = Vec({1,2,3}, {2: 9})
    >>> v2 = Vec({1,2,4}, {1: 1, 2: 2, 4: 8})
    >>> scale_vecs({3: v1, 5: v2}) == [Vec({1,2,3},{2: 3.0}), Vec({1,2,4},{1: 0.2, 2: 0.4, 4: 1.6})]
    True
    '''
    return [scalar_mul(vecdict[k], 1.0/k) for k in vecdict]
def scale_vecs(vecdict):
    '''
    >>> v1 = Vec({1,2,4}, {2: 9})
    >>> v2 = Vec({1,2,4}, {1: 1, 2: 2, 4: 8})
    >>> result = scale_vecs({3: v1, 5: v2})
    >>> len(result)
    2
    >>> [v in [Vec({1,2,4},{2: 3.0}), Vec({1,2,4},{1: 0.2, 2: 0.4, 4: 1.6})] for v in result]
    [True, True]
    '''
    return [ scalar_mul(v,1/k)  for (k, v) in vecdict.items()]
Exemple #5
0
def scale_vecs(vecdict):
    '''
    >>> v1 = Vec({1,2,4}, {2: 9})
    >>> v2 = Vec({1,2,4}, {1: 1, 2: 2, 4: 8})
    >>> result = scale_vecs({3: v1, 5: v2})
    >>> len(result)
    2
    >>> [v in [Vec({1,2,4},{2: 3.0}), Vec({1,2,4},{1: 0.2, 2: 0.4, 4: 1.6})] for v in result]
    [True, True]
    '''
    return [scalar_mul(vecdict[i], 1. / i) for i in vecdict.keys()]
Exemple #6
0
def scale_vecs(vecdict):
    '''
    >>> v1 = Vec({1,2,3}, {2: 9})
    >>> v2 = Vec({1,2,4}, {1: 1, 2: 2, 4: 8})
    >>> scale_vecs({3: v1, 5: v2}) == [Vec({1,2,3},{2: 3.0}), Vec({1,2,4},{1: 0.2, 2: 0.4, 4: 1.6})]
    True
    '''
##    l=[]
##    for (key, value) in vecdict.items():
##        x = scalar_mul(value, 1/key)
##        l.append(x)
#    return l

    return [scalar_mul(value, 1/key)  for (key,value) in vecdict.items()]
def gradient_descent_step(A, b, w, sigma):
    '''
    Input:
        - A: feature Mat
        - b: diagnoses Vec
        - w: hypothesis Vec
        - sigma: step size
    Output:
        - The vector w' resulting from 1 iteration of gradient descent
          starting from w and moving sigma.
    '''
    hypothesis_vec = find_grad(A,b,w)
    x = scalar_mul(hypothesis_vec,sigma)
    next_w = w - x
    return next_w
Exemple #8
0
def scale_vecs(vecdict):
    '''
    >>> v1 = Vec({1,2,4}, {2: 9})
    >>> v2 = Vec({1,2,4}, {1: 1, 2: 2, 4: 8})
    >>> result = scale_vecs({3: v1, 5: v2})
    >>> len(result)
    2
    >>> [v in [Vec({1,2,4},{2: 3.0}), Vec({1,2,4},{1: 0.2, 2: 0.4, 4: 1.6})] for v in result]
    [True, True]
    '''
    retveclist = list()
    from vec import scalar_mul
    for k in vecdict.keys():
        v = vecdict[k]
        retveclist.append(scalar_mul(v, (1/k)))
        #domain = v.D
        #for d in domain:
        #    if d in v.f and k != 0:
        #        v.f[d] /= k
        #retveclist.append(v)
    return retveclist
Exemple #9
0
def scale_vecs(vecdict):
    return [scalar_mul(vecdict[k], 1.0/k) for k in vecdict]
Exemple #10
0
from vec import Vec
from vec import scalar_mul
from GF2 import one
v1 = Vec([0, 1, 2, 3, 4],{0:one, 1:one, 3:one, 4:one})
v2 = Vec([0, 1, 2, 3, 4],{2: one})
v3 = Vec([0, 1, 2, 3, 4],{2:one, 3:one, 4:one})
v4 = Vec([0, 1, 2, 3, 4],{0: one, 2:one, 3:one, 4:one})
v5 = Vec([0, 1, 2, 3, 4],{0: one, 1:one, 2:one, 3:one, 4:one})

for x1 in (0, one):
    for x2 in (0, one):
        for x3 in (0, one):
            for x4 in (0, one):
                for x5 in (0, one):
                    if (scalar_mul(v1,x1)+scalar_mul(v2,x2)+scalar_mul(v3,x3)+scalar_mul(v4,x4)+scalar_mul(v5,x5)) == Vec([0, 1, 2, 3, 4],{}):
                        print(x1, ", ", x2, ", ", x3, ", ", x4, ", ", x5)
Exemple #11
0
from vec import Vec
from vec import scalar_mul
from GF2 import one
v1 = Vec([0, 1, 2, 3, 4, 5, 6, 7],{0: one, 1: one})
v2 = Vec([0, 1, 2, 3, 4, 5, 6, 7],{1: one, 2: one})
v3 = Vec([0, 1, 2, 3, 4, 5, 6, 7],{0: one, 3: one})
v4 = Vec([0, 1, 2, 3, 4, 5, 6, 7],{1: one, 4: one})
v5 = Vec([0, 1, 2, 3, 4, 5, 6, 7],{2: one, 4: one})
v6 = Vec([0, 1, 2, 3, 4, 5, 6, 7],{3: one, 4: one})
v7 = Vec([0, 1, 2, 3, 4, 5, 6, 7],{5: one, 7: one})
v8 = Vec([0, 1, 2, 3, 4, 5, 6, 7],{6: one, 7: one})

for x1 in (0, one):
    for x2 in (0, one):
        for x3 in (0, one):
            for x4 in (0, one):
                 for x5 in (0, one):
                     for x6 in (0, one):
                         for x7 in (0, one):
                             for x8 in (0, one):
                                 if (scalar_mul(v1,x1)+scalar_mul(v2,x2)+scalar_mul(v3,x3)+scalar_mul(v4,x4)+scalar_mul(v5,x5)+scalar_mul(v6,x6)+scalar_mul(v7,x7)+scalar_mul(v8,x8)) == Vec([0, 1, 2, 3, 4, 5, 6, 7],{1: one, 3: one}):
                                     print(x1, ", ", x2, ", ", x3, ", ", x4, ", ", x5, ", ", x6, ", ", x7, ", ", x8)
Exemple #12
0
def rep2vec(u, veclist):
    return sum([scalar_mul(veclist[i], u[i]) for i in range(len(veclist))])
Exemple #13
0
def lin_comb(vlist, clist):
    return sum([scalar_mul(v, a) for (v, a) in zip(vlist, clist)])