예제 #1
0
def selectRandom():
    vecs =[(a0,b0)]
    for x in range(4):
        a = list2vec([randGF2() for x in range(6)])
        b = list2vec([randGF2() for x in range(6)])
        vecs.append((a,b))
    return vecs
예제 #2
0
def growSec():
	from independence import is_independent
	from itertools import combinations
	proven = [(a0,b0)]
	found = False
	while not found:
		t = [(list2vec([randGF2() for i in range(6)]),list2vec([randGF2() for i in range(6)])) for j in range(2)]
		vecs = proven + t
		if all(is_independent(list(sum(x,()))) for x in combinations(vecs,3)):
			found = True
			proven += t
	
	found1 = False
	while not found1:
		t1 = [(list2vec([randGF2() for i in range(6)]),list2vec([randGF2() for i in range(6)])) for j in range(2)]
		vecs1 = proven + t1
		if all(is_independent(list(sum(x,()))) for x in combinations(vecs1,3)): 
			found1 = True
			proven += t1
	
	return proven		
	
			
	
			
	
		
	
		
	
		
예제 #3
0
def selectRandom():
    vecs = [(a0, b0)]
    for x in range(4):
        a = list2vec([randGF2() for x in range(6)])
        b = list2vec([randGF2() for x in range(6)])
        vecs.append((a, b))
    return vecs
예제 #4
0
def growSec():
    from independence import is_independent
    from itertools import combinations
    proven = [(a0, b0)]
    found = False
    while not found:
        t = [(list2vec([randGF2() for i in range(6)]),
              list2vec([randGF2() for i in range(6)])) for j in range(2)]
        vecs = proven + t
        if all(
                is_independent(list(sum(x, ())))
                for x in combinations(vecs, 3)):
            found = True
            proven += t

    found1 = False
    while not found1:
        t1 = [(list2vec([randGF2() for i in range(6)]),
               list2vec([randGF2() for i in range(6)])) for j in range(2)]
        vecs1 = proven + t1
        if all(
                is_independent(list(sum(x, ())))
                for x in combinations(vecs1, 3)):
            found1 = True
            proven += t1

    return proven
예제 #5
0
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)
예제 #6
0
파일: hw5.py 프로젝트: fperezlo/matrix
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)
예제 #7
0
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
    '''
    #W_basis = U_basis + V_basis
    #rep_w = vec2rep(W_basis, w)

    #U = set(range(len(U_basis)))
    #rep_u = Vec(U,{u:rep_w[u] for u in U})
    #u = coldict2mat(U_basis)*rep_u

    #V = set(range(len(U_basis), len(rep_w.D)))
    #rep_v = Vec(V,{v:rep_w[v] for v in V})
    #v = coldict2mat(V_basis)*rep_v
    T  = U_basis + V_basis
    x  = vec2rep(T, w)
    rep= list(x.f.values())
    u1 = list2vec(rep[0:len(U_basis)])
    v1 = list2vec(rep[len(U_basis):len(T)])
    u  = rep2vec(u1,U_basis)
    v  = rep2vec(v1,V_basis)
    return (u,v)
예제 #8
0
def compare(sen1, sen2, votingDict):
    total = 0
    vector = vecutil.list2vec(votingDict.get(sen1))
    vector2 = vecutil.list2vec(votingDict.get(sen2))
    for k in range(len(vector.f)):
        total = total + vector.f.get(k) * vector2.f.get(k)
    return (total)
예제 #9
0
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)
예제 #10
0
def choose_secret_vector(s,t):
    uFound = False
    u = list2vec([0,0,0,0,0,0])
    while not uFound:
        u = list2vec([randGF2() for i in range(6)])
        if a0*u == s and b0*u == t:
            uFound = True
    return u
예제 #11
0
파일: secret2.py 프로젝트: vvw/CodingMatrix
def bin2vect(n):
    num = bin(n)
    bin_list = [ one if x == '1' else 0 for x in num[2:]]
    if len(bin_list) < 6:
        new_lst = [0] * (6 - len(bin_list))
        new_lst.extend(bin_list)
        return list2vec(new_lst)
    return list2vec(bin_list)
예제 #12
0
def choose_secret_vector(s, t):
    uFound = False
    u = list2vec([0, 0, 0, 0, 0, 0])
    while not uFound:
        u = list2vec([randGF2() for i in range(6)])
        if a0 * u == s and b0 * u == t:
            uFound = True
    return u
예제 #13
0
def choose_secret_vector(s,t):
    #GF2 field elements s and t
    # output: a random 6 vector u such that a*u = s and b*u = t
    u = list2vec([randGF2() for x in range(6)])
    while a0 * u != s or b0 * u != t:
        u = list2vec([randGF2() for x in range(6)])

    return u
예제 #14
0
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 find_error(e):
    """ Takes error syndrome and return error vector """
    v = gf2_to_decimal(e)
    err_vector = [0] * 7
    pos = dot(list2vec(v), list2vec([1, 2, 4]))
    if pos:
        pos -= 1  # array from 0
        err_vector[pos] = One()
    return list2vec(err_vector)
예제 #16
0
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)
예제 #17
0
def generate_independent_vectors(n):
    B = []
    while (len(B) < n * 2):
        b = list2vec([randGF2() for i in range(6)])
        while b == a0 or b == b0:
            b = list2vec([randGF2() for i in range(6)])
        B.append(b)
        if not is_independent(B):
            del B[len(B) - 1]
    return B
예제 #18
0
파일: matutil.py 프로젝트: grogs84/nwspa
def test_rowlist2echelon():
  v1 = list2vec([0,2,3,4,5])
  v2 = list2vec([0,0,0,0,5])
  v3 = list2vec([1,2,3,4,5])
  v4 = list2vec([0,0,0,4,5])

  rowlist = [v1,v2,v3,v4]

  A = rowlist2echelon(rowlist)
  print(A)
예제 #19
0
def choose_tokens(a, b):
    tokens = [a, b]
    for _ in range(1000000):
        token_A = list2vec([randGF2() for _ in range(len(a0.D))])
        token_B = list2vec([randGF2() for _ in range(len(a0.D))])
        if is_token_pair_suitable(tokens, [token_A, token_B]):
            tokens = tokens + [token_A, token_B]
        if len(tokens) == 10:
            return tokens
    raise Exception("Unable to pick tokens")
예제 #20
0
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
    '''

    UV_basis = U_basis + V_basis
    uv_w = basis.vec2rep(UV_basis,w)

    uv_values = list(uv_w.f.values())

    udims = len(U_basis)

    ux = list2vec(uv_values[:udims])
    vy = list2vec(uv_values[udims:])


    u = basis.rep2vec(ux, U_basis)
    v = basis.rep2vec(vy, V_basis)

    return (u,v)
예제 #21
0
def task2():

    row = [(a0, b0)] + [
        (list2vec([randGF2() for i in range(6)]), list2vec([randGF2() for i in range(6)])) for j in range(4)
    ]

    while not all(is_independent(list(sum(x, ()))) for x in combinations(row, 3)):
        row = [(a0, b0)] + [
            (list2vec([randGF2() for i in range(6)]), list2vec([randGF2() for i in range(6)])) for j in range(4)
        ]

    return row
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
    '''
    U_V_basis = list(U_basis)
    U_V_basis.extend(V_basis)
    w_coordinates = vec2rep(U_V_basis, w)
    w_u_coord = list()
    w_v_coord = list()
    for n in range(len(U_V_basis)):
        if n < len(U_basis):
            w_u_coord.append(w_coordinates[n])
        else:
            w_v_coord.append(w_coordinates[n])

    u = rep2vec(list2vec(w_u_coord), U_basis)
    v = rep2vec(list2vec(w_v_coord), V_basis)
    return (u, v)
def pick_known_vectors(a, b):
    all = [ (a,b),None,None,None,None ]
    while True:
        for i in range(1,5):
            all[i] = (list2vec([ randGF2() for i in range(6) ]),
                      list2vec([ randGF2() for i in range(6) ]))
        ok = True
        for x,y,z in combinations(range(5), 3):
            L = [ all[x][0], all[x][1], all[y][0], all[y][1], all[z][0], all[z][1] ]
            if not is_independent(L):
                ok = False
                break
        if ok:
            return all
예제 #24
0
def helper():
    L0 = []
    while True:
        a1 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
        b1 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
        a2 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
        b2 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
        if is_independent ([a0, b0, a1, b1, a2, b2]) == True:
            break
        L0 = [a0, b0, a1, b1, a2, b2]
    while True:
        a3 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
        b3 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
        L1 = [a0, b0, a1, b1, a3, b3]
        L2 = [a0, b0, a2, b2, a3, b3]
        L3 = [a1, b1, a2, b2, a3, b3]
        if is_independent (L1) == True and is_independent (L2) == True and is_independent (L3) == True:
                break           
    while True:
        a4 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
        b4 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
        L4 = [a0, b0, a1, b1, a4, b4]
        L5 = [a0, b0, a2, b2, a4, b4]
        L6 = [a1, b1, a2, b2, a4, b4]
        L7 = [a0, b0, a3, b3, a4, b4]
        L8 = [a1, b1, a3, b3, a4, b4]
        L9 = [a2, b2, a3, b3, a4, b4]
        if is_independent (L4) == True and is_independent (L5) == True and is_independent (L6) == True and is_independent (L7) == True and is_independent (L8) == True and is_independent (L9) == True:
            break
    return a0, b0, a1, b1, a2, b2, a3, b3, a4, b4
예제 #25
0
def random_select():
    while True:
     a1 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
     b1 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
     a2 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
     b2 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
     a3 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
     b3 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
     a4 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
     b4 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()])
     l1 = [a0,a1,a2,b0,b1,b2]
     l2 = [a0,a1,a3,b0,b1,b3]
     l3 = [a0,a1,a4,b0,b1,b4]
     l4 = [a0,a2,a3,b0,b2,b3]
     l5 = [a0,a2,a4,b0,b2,b4]
     l6 = [a0,a3,a4,b0,b3,b4]
     l7 = [a1,a2,a3,b1,b2,b3]
     l8 = [a1,a2,a4,b1,b2,b4]
     l9 = [a1,a3,a4,b1,b3,b4]
     l10 =[a2,a3,a4,b2,b3,b4]
     t1 = my_is_independent(l1);
     t2 = my_is_independent(l2);
     t3 = my_is_independent(l3);
     t4 = my_is_independent(l4);
     t5 = my_is_independent(l5);
     t6 = my_is_independent(l6);
     t7 = my_is_independent(l7);
     t8 = my_is_independent(l8);
     t9 = my_is_independent(l9);
     t10 = my_is_independent(l10);
     if t1 == True and t2 == True and t3 == True and t4 == True and t5 == True  and t6 == True and t7 == True and t8 ==True and t9 == True and t10 == True:
      return [a1,b1,a2,b2,a3,b3,a4,b4]
예제 #26
0
def task2():
    
    
    row = [(a0,b0)] + [
                       ( list2vec([randGF2() for i in range(6) ]),
                         list2vec([randGF2() for i in range(6) ])
                       ) for j in range(4) ]
    
    while not all(is_independent(list(sum(x,()))) for x in combinations(row,3)):
        row = [(a0,b0)] + [
                       ( list2vec([randGF2() for i in range(6) ]),
                         list2vec([randGF2() for i in range(6) ])
                       ) for j in range(4) ]
        
    return row
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
    """
    UV_basis = U_basis + V_basis
    UV_matrix = coldict2mat(UV_basis)
    U_matrix = coldict2mat(U_basis)
    V_matrix = coldict2mat(V_basis)
    x = solve(UV_matrix, w)
    list_x = list(x.f.values())
    # print(list_x)
    u = U_matrix * list2vec(list_x[0 : len(U_matrix.D[1])])
    v = V_matrix * list2vec(list_x[len(U_matrix.D[1]) :])
    return (u, v)
예제 #28
0
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
    '''
    UV_basis = U_basis + V_basis
    UV_matrix = coldict2mat(UV_basis)
    U_matrix = coldict2mat(U_basis)
    V_matrix = coldict2mat(V_basis)
    x = solve(UV_matrix, w) 
    list_x = list(x.f.values())
    #print(list_x)
    u = U_matrix * list2vec(list_x[0:len(U_matrix.D[1])])
    v = V_matrix * list2vec(list_x[len(U_matrix.D[1]):])
    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
    '''
    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)
예제 #30
0
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
    '''
    res = []
    #print(A)
    n = len(A.D[0])
    row_vecs = mat2rowdict(A)
    rowlist = list(row_vecs.values())
    #print(rowlist)
    
    for i in range(n):
        b = list2vec([0] * i + [1] + [0] * (n-i-1))
        x = triangular_solve(rowlist, list(A.D[0]), b)
        res.append(x)
    
    #print(coldict2mat(res))
    return coldict2mat(res)
def choose_secret_vector(s, t):
    while True:
        secret = [randGF2() for i in range(6)]
        secret = list2vec(secret)
        print(secret)
        if a0 * secret == s and b0 * secret == t:
            return secret
예제 #32
0
def other_vectors():

    while True:
        l_ = [a0, b0]
        for i in range(8):
            l_.append(
                list2vec([
                    randGF2(),
                    randGF2(),
                    randGF2(),
                    randGF2(),
                    randGF2(),
                    randGF2()
                ]))
        ranks = []
        for comb in list(itertools.combinations([0, 2, 4, 6, 8], 3)):

            l = [(l_[i], l_[i + 1]) for i in comb]
            the_three = [item for sublist in l for item in sublist]
            r = rank(the_three)
            if r < 6:
                continue
            else:
                ranks.append(r)

        if set(ranks) == {6}:
            print("yess")
            return l_

    return l_
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
    """
    res = []
    # print(A)
    n = len(A.D[0])
    row_vecs = mat2rowdict(A)
    rowlist = list(row_vecs.values())
    # print(rowlist)

    for i in range(n):
        b = list2vec([0] * i + [1] + [0] * (n - i - 1))
        x = triangular_solve(rowlist, list(A.D[0]), b)
        res.append(x)

    # print(coldict2mat(res))
    return coldict2mat(res)
예제 #34
0
def choose_secret_vector(s,t):
    '''(GF2, GF2) -> Vec of GF2
    Return random vector u of length 6 such that a0*u == s, b0*u == t.
    '''
    u = list2vec([randGF2() for i in range(6)])
    
    return u if (a0*u==s) and (b0*u==t) else choose_secret_vector(s,t)
def choose_secret_vector(s,t):
    while True:
        secret = [ randGF2() for i in range(6) ]
        secret = list2vec(secret)
        print(secret)
        if a0 * secret == s and b0 * secret == t:
            return secret
예제 #36
0
def choose_secret_vector(s, t):
    check_random = False
    while not (check_random):
        u = list2vec([randGF2() for i in range(6)])
        if a0 * u == s and b0 * u == t:
            check_random = True
    return u
예제 #37
0
def find_average_similarity(sen, sen_set, voting_dict):
    """
    Input: the name of a senator, a set of senator names, and a voting dictionary.
    Output: the average dot-product between sen and those in sen_set.
    Example:
        >>> vd = {'Klein': [1,1,1], 'Fox-Epstein': [1,-1,0], 'Ravella': [-1,0,0]}
        >>> find_average_similarity('Klein', {'Fox-Epstein','Ravella'}, vd)
        -0.5
    """
    from vecutil import list2vec
    _sum = 0
    vec0 = list2vec(voting_dict[sen])
    for sen_name in sen_set:
        vec1 = list2vec(voting_dict[sen_name])
        _sum += vec0 * vec1
    return _sum/len(sen_set)
예제 #38
0
def randGF2_6():
    return list2vec(
        [randGF2(),
         randGF2(),
         randGF2(),
         randGF2(),
         randGF2(),
         randGF2()])
def pick_known_vectors(a, b):
    all = [(a, b), None, None, None, None]
    while True:
        for i in range(1, 5):
            all[i] = (list2vec([randGF2() for i in range(6)]),
                      list2vec([randGF2() for i in range(6)]))
        ok = True
        for x, y, z in combinations(range(5), 3):
            L = [
                all[x][0], all[x][1], all[y][0], all[y][1], all[z][0],
                all[z][1]
            ]
            if not is_independent(L):
                ok = False
                break
        if ok:
            return all
예제 #40
0
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
    '''

    R = len(A.D[0])
    C = len(A.D[1])
    coldict = {}
    for i in range(C):
        b = [0] * R
        b[i] = 1
        s = triangular_solve(list(matutil.mat2rowdict(A).values()), list(A.D[0]), list2vec(b))
        coldict[i] = s

    return coldict2mat(coldict)



# import hw5
# import hw4
# from mat import Mat
# import vecutil
# import matutil
# from vec import Vec
# from GF2 import one
# L = [Vec({0, 1, 2},{0: 1, 1: 0, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 1})]
# hw5.my_is_independent(L)
# hw5.my_is_independent(L[:2])
# S = [vecutil.list2vec(v) for v in [[1,0,0],[0,1,0],[0,0,1]]]
# B = [vecutil.list2vec(v) for v in [[1,1,0],[0,1,1],[1,0,1]]]
# ff = hw5.morph(S, B)
# ff == [(Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 1, 1: 0, 2: 0})), (Vec({0, 1, 2},{0: 0, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0})), (Vec({0, 1, 2},{0: 1, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}))]
# 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})
# sb = hw5.subset_basis([a0,a1,a2,a3])
# sb == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})]
# 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})
# ss = hw5.direct_sum_decompose(U_basis, V_basis, w)
# ss == (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}))
# M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
# hw5.is_invertible(M)
# A = Mat(({0, 1}, {0, 1, 2}), {(0, 1): 2, (1, 2): 1, (0, 0): 1, (1, 0): 3, (0, 2): 3, (1, 1): 1})
# B = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
# C = Mat(({0, 1, 2}, {0, 1}), {(0, 1): 0, (2, 0): 2, (0, 0): 1, (1, 0): 0, (1, 1): 1, (2, 1): 1})
# D = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 5, (1, 2): 2, (0, 0): 1, (2, 0): 4, (1, 0): 2, (2, 2): 7, (0, 2): 8, (2, 1): 6, (1, 1): 5})
# E = Mat(({0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}), {(1, 2): 7, (3, 2): 7, (0, 0): 3, (3, 0): 1, (0, 4): 3, (1, 4): 2, (1, 3): 4, (2, 3): 0, (2, 1): 56, (2, 4): 5, (4, 2): 6, (1, 0): 2, (0, 3): 7, (4, 0): 2, (0, 1): 5, (3, 3): 4, (4, 1): 4, (3, 1): 23, (4, 4): 5, (0, 2): 7, (2, 0): 2, (4, 3): 8, (2, 2): 9, (3, 4): 2, (1, 1): 4})
# M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0})
# hw5.find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0})
# A = matutil.listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]])
# hw5.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})
예제 #41
0
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
    '''

    R = len(A.D[0])
    C = len(A.D[1])
    coldict = {}
    for i in range(C):
        b = [0] * R
        b[i] = 1
        s = triangular_solve(list(matutil.mat2rowdict(A).values()),
                             list(A.D[0]), list2vec(b))
        coldict[i] = s

    return coldict2mat(coldict)


# import hw5
# import hw4
# from mat import Mat
# import vecutil
# import matutil
# from vec import Vec
# from GF2 import one
# L = [Vec({0, 1, 2},{0: 1, 1: 0, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 1})]
# hw5.my_is_independent(L)
# hw5.my_is_independent(L[:2])
# S = [vecutil.list2vec(v) for v in [[1,0,0],[0,1,0],[0,0,1]]]
# B = [vecutil.list2vec(v) for v in [[1,1,0],[0,1,1],[1,0,1]]]
# ff = hw5.morph(S, B)
# ff == [(Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 1, 1: 0, 2: 0})), (Vec({0, 1, 2},{0: 0, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0})), (Vec({0, 1, 2},{0: 1, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}))]
# 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})
# sb = hw5.subset_basis([a0,a1,a2,a3])
# sb == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})]
# 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})
# ss = hw5.direct_sum_decompose(U_basis, V_basis, w)
# ss == (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}))
# M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
# hw5.is_invertible(M)
# A = Mat(({0, 1}, {0, 1, 2}), {(0, 1): 2, (1, 2): 1, (0, 0): 1, (1, 0): 3, (0, 2): 3, (1, 1): 1})
# B = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
# C = Mat(({0, 1, 2}, {0, 1}), {(0, 1): 0, (2, 0): 2, (0, 0): 1, (1, 0): 0, (1, 1): 1, (2, 1): 1})
# D = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 5, (1, 2): 2, (0, 0): 1, (2, 0): 4, (1, 0): 2, (2, 2): 7, (0, 2): 8, (2, 1): 6, (1, 1): 5})
# E = Mat(({0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}), {(1, 2): 7, (3, 2): 7, (0, 0): 3, (3, 0): 1, (0, 4): 3, (1, 4): 2, (1, 3): 4, (2, 3): 0, (2, 1): 56, (2, 4): 5, (4, 2): 6, (1, 0): 2, (0, 3): 7, (4, 0): 2, (0, 1): 5, (3, 3): 4, (4, 1): 4, (3, 1): 23, (4, 4): 5, (0, 2): 7, (2, 0): 2, (4, 3): 8, (2, 2): 9, (3, 4): 2, (1, 1): 4})
# M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0})
# hw5.find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0})
# A = matutil.listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]])
# hw5.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})
def choose_secret_vector(s,t):
    u = [0, 0, 0, 0, 0, 0]
    u[1] = randGF2()
    u[2] = randGF2()
    u[4] = randGF2()
    u[5] = randGF2()
    u[0] = t - u[1] - u[5]
    u[3] = s - t 
    return list2vec(u) 
예제 #43
0
def choose_secret_vector(s, t):
    u = [0, 0, 0, 0, 0, 0]
    u[1] = randGF2()
    u[2] = randGF2()
    u[4] = randGF2()
    u[5] = randGF2()
    u[0] = t - u[1] - u[5]
    u[3] = s - t
    return list2vec(u)
def choose_secret_vector(s,t):
    secret_list=[0 for x in range(0,6)]	    
    
    #print ("Init1")
    for i in range(0,6):
        secret_list[i]=randGF2();

    secret_vec=list2vec(secret_list)

    #print ("Init2")
    while (a0*secret_vec!=s or b0*secret_vec!=t): #Dont use AND because the while loop should go as long as even one of the conditions is true
        for i in range(0,6):
            secret_list[i]=randGF2()
        
        #print ("Iteration")
        secret_vec=list2vec(secret_list)

    return secret_vec  
예제 #45
0
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
    '''
    rep_w = vec2rep(U_basis + V_basis, w)
    rep_u = [rep_w[i] for i in range(len(U_basis))]
    rep_v = [rep_w[len(U_basis) + i] for i in range(len(V_basis))]
    u = coldict2mat(U_basis) * list2vec(rep_u)
    v = coldict2mat(V_basis) * list2vec(rep_v)
    return (u, v)
예제 #46
0
def matrix_inverse(M):
	L = []
	if is_invertible(M):
		print('yayyy')
		for e in M.D[0]:
			w = [0]*len(M.D[0])
			w[e] = one
			w = list2vec(w)
			L.append(solve(M,w))
	#print(L)
	return coldict2mat(L)
예제 #47
0
def golay23Encode(data):
    if len(data) != 12:
        return None

    # convert to a vector over GF(2)
    datavec = list2vec(bitArrayToGF2(data))
    codeword = datavec * golay23mat

    # convert codeword to bit array
    codebits = GF2toBitArray(vec2list(codeword))
    return data + codebits
예제 #48
0
def policy_compare(sen_a, sen_b, voting_dict):
    """
    Input: last names of sen_a and sen_b, and a voting dictionary mapping senator
           names to lists representing their voting records.
    Output: the dot-product (as a number) representing the degree of similarity
            between two senators' voting policies
    Example:
        >>> voting_dict = {'Fox-Epstein':[-1,-1,-1,1],'Ravella':[1,1,1,1]}
        >>> policy_compare('Fox-Epstein','Ravella', voting_dict)
        -2
    """
 #   from vec import Vec
 #   from dictutil import list2dict
    from vecutil import list2vec
    #list_a = voting_data[sen_a]
    #dict_a = list2dict(list_a)
    vec_a = list2vec(voting_dict[sen_a]) #Vec(set(dict_a.keys()),dict_a)
    #list_b = voting_data[sen_b]
    #dict_b = list2dict(list_b)
    vec_b = list2vec(voting_dict[sen_b]) #Vec(set(dict_b.keys()),dict_b)
    return vec_a*vec_b
예제 #49
0
def choose_secret_vector(s, t):

    while True:
        u = list2vec(
            [randGF2(),
             randGF2(),
             randGF2(),
             randGF2(),
             randGF2(),
             randGF2()])
        if (a0 * u == s) and (b0 * u == t):
            break
    return u
예제 #50
0
def linear_regression(data):
    datalist = read_vectors(data)
    x = list(datalist[0].D)[1]
    y = list(datalist[0].D)[0]
    x_domain = {1, x}
    x_rowlist = []
    y_list = []
    for v in datalist:
        x_rowlist.append(Vec(x_domain, {1: 1, x: v[x]}))
        y_list.append(v[y])
    y_vec = list2vec(y_list)
    minimize = QR_solve(rowdict2mat(x_rowlist), y_vec)
    return minimize[1], minimize[x]  # return b,a
예제 #51
0
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
    '''
    dsum_basis = U_basis + V_basis                              # Basis of the direct sum is the union of the bases of the sub spaces
    sol = vec2rep (dsum_basis, w)                               # get the linear solution that multiplies with dsum_basis that gives w
    com = list (sol.f.values())                                 # break the U and V parts of the solution
    uvals = list2vec (com[:len(U_basis)])                            
    vvals = list2vec (com[len(U_basis):])     
    u = rep2vec(uvals, U_basis)
    v = rep2vec(vvals, V_basis)
    return u, v                       
예제 #52
0
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
    '''
    coeffvec = vec2rep(U_basis+V_basis, w)
    u=list2vec([0,0,0,0,0,0])
    v=list2vec([0,0,0,0,0,0])

    for i in range(len(U_basis)):
        u=u+coeffvec[i]*U_basis[i]
    for j in range(len(V_basis)):
        v=v+coeffvec[j+len(U_basis)]*V_basis[j]
    return (u,v)
예제 #53
0
def getVecs():
  while(True):
    a1 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()])
    b1 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()])
    a2 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()])
    b2 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()])
    a3 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()])
    b3 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()])
    a4 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()])
    b4 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()])

    vecs = [a0,b0,a1,b1,a2,b2,a3,b3,a4,b4]
    result = [is_independent([vecs[x],vecs[x+1],vecs[y],vecs[y+1],vecs[z],vecs[z+1]]) 
              for x in range(0,10,2) for y in range(x+2,10,2) for z in range(y+2,10,2)]
  
    if all(result):
      return vecs
예제 #54
0
파일: hw5.py 프로젝트: johnmerm/matrix
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
    """
    cd = list()
    label_list = list(mat2rowdict(A).keys())
    rowlist = list(mat2rowdict(A).values())
    for j in label_list:
        c = triangular_solve(rowlist, label_list, list2vec([1 if i == j else 0 for i in label_list]))
        cd.append(c)

    return coldict2mat(cd)
예제 #55
0
def is_invertible(M): 
    '''
    input: A matrix, M
    outpit: A boolean indicating if M is invertible.
    
    >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
    >>> is_invertible(M)
    True
    '''
    nullspace = solve(M, list2vec([0]))
    ker_zero = nullspace == Vec(nullspace.D, {})

    im_f_eq_codomain = independence.rank(subset_basis(matutil.mat2coldict(M).values())) == len(M.D[1])

    # invertible if its one-to-one, onto, and square matrix
    return ker_zero and im_f_eq_codomain and len(matutil.mat2coldict(M)) == len(matutil.mat2rowdict(M))
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
예제 #58
0
def exchange(S, A, z):
    '''
    Input:
        - S: a set of Vecs (not necessarily linearly independent)
        - A: a set of Vecs, a proper subset of 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} | S - {w})
    Examples:
        >>> from vecutil import list2vec
        >>> from vec import Vec
        >>> 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})) or (exchange(S, A, z) == Vec({0, 1, 2, 3},{0: 1, 1: 2, 2: 3, 3: 4}))
        True
        >>> S == {list2vec(v) for v in [[0,0,5,3],[2,0,1,3],[0,0,1,0],[1,2,3,4]]}
        True
        >>> A == {list2vec(v) for v in [[0,0,5,3],[2,0,1,3]]}
        True
        >>> z == list2vec([0,2,1,1])
        True
        >>> from GF2 import one
        >>> S = {Vec({0,1,2,3,4}, {i:one, (i+1)%5:one}) for i in range(5)}
        >>> A = {list2vec([0,one,one,0,0]),list2vec([0,0,one,one,0])}
        >>> z = list2vec([0,0,one,0,one])
        >>> exchange(S, A, z) in {list2vec(v) for v in [[one, one,0,0,0],[one,0,0,0,one],[0,0,0,one,one]]}
        True
        >>> S = {list2vec(v) for v in [[one,0,one,0],[one,one,one,one],[one,one,0,0],[one,one,one,0]]}
        >>> A = {list2vec([one,one,one,0])}
        >>> z = list2vec([0,one,0,0])
        >>> exchange(S, A, z) == list2vec([one,0,one,0])
        True
        >>> S = {list2vec(v) for v in [[0, 0, 0, one], [0, one, one, one], [0, 0, one, one], [one, 0, 0, 0], [0, one, one, 0]]}
        >>> A = {list2vec(v) for v in [[0, one, one, one], [0, 0, 0, one]]}
        >>> z = list2vec([0, one, 0, one])
        >>> exchange(S, A, z) in [list2vec([0, 0, one, one]), list2vec([0, one, one, 0])]
        True
    '''
    
    U = S | {z}
    V = set([v for v in U if is_superfluous(U,v)])
    
    R = list(V - (A | {z}))
    return R[0] if R else list2vec([0 for v in z.D])
예제 #59
0
def find_matrix_inverse(A):
    '''
    input: An invertible matrix, A, over GF(2)
    output: Inverse of A

    >>> M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0})
    >>> find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0})
    True
    '''
    R = len(A.D[0])
    C = len(A.D[1])
    coldict = {}
    for i in range(C):
        b = [0] * R
        b[i] = one
        s = solve(A, list2vec(b))
        coldict[i] = s

    return coldict2mat(coldict)