def find_error(error_syndrome): comparison = (error_syndrome[0], error_syndrome[1], error_syndrome[2]) try: column_map = { (zero, zero, one): list2vec([one, zero, zero, zero, zero, zero, zero]), (zero, one, zero): list2vec([zero, one, zero, zero, zero, zero, zero]), (zero, one, one): list2vec([zero, zero, one, zero, zero, zero, zero]), (one, zero, zero): list2vec([zero, zero, zero, one, zero, zero, zero]), (one, zero, one): list2vec([zero, zero, zero, zero, one, zero, zero]), (one, one, zero): list2vec([zero, zero, zero, zero, zero, one, zero]), (one, one, one): list2vec([zero, zero, zero, zero, zero, zero, one]) } return column_map[comparison] except KeyError: return list2vec([zero, zero, zero, zero, zero, zero, zero])
import cancer_data import vec from vector import vecutil from matrix import matutil import math test_ident = matutil.rowdict2mat({ 0: vecutil.list2vec([1, 0, 0]), 1: vecutil.list2vec([0, 1, 0]), 2: vecutil.list2vec([0, 0, 1]), }) test_1 = vecutil.list2vec([-1, -1, -1]) test_2 = vecutil.list2vec([1, 1, 1]) def read_training_data(): return cancer_data.read_training_data('inner_product/train.data') def signum(u): """ input: a Vec u output: the Vec v with the same domain as u such that +1 if u[d] >= 0 v[d]= { -1 if u[d] < 0 """ return vec.Vec(u.D, {d: 1 if u[d] >= 0 else -1 for d in u.D}) assert (signum(vec.Vec({'A', 'B'}, {
def find_error_matrix(S): return coldict2mat( {c: find_error(list2vec([S[r, c] for r in S.D[0]])) for c in S.D[1]})
from matrix.matutil import listlist2mat, coldict2mat from matrix.bitutil import mat2bits, bits2mat, str2bits, bits2str, noise from vector.vecutil import list2vec from GF2 import zero, one G = listlist2mat([ [one, zero, one, one], [one, one, zero, one], [zero, zero, zero, one], [one, one, one, zero], [zero, zero, one, zero], [zero, one, zero, zero], [one, zero, zero, zero], ]) message1 = list2vec([one, zero, zero, one]) encoded = G * message1 print("the encoding of the message [one, 0, 0, one] is ", encoded) def extract_original(encoded): decoded = [encoded[6], encoded[5], encoded[4], encoded[2]] return decoded print("\ndecoded %s" % extract_original(encoded)) # we know that the kernel is trivial because G's row space spans GF(1)^4, and therefore that # G represents a one-to-one function, and therefore that it is invertible
def get_column(m, colno): return G * list2vec([m[r, colno] for r in m.D[0]])
def adjust(v, multipliers): return vecutil.list2vec([v[d] * multipliers[d] for d in v.D])
('y2', 'x1'): -x1, ('y2', 'x2'): -x2, ('y2', 'x3'): -1 }) return [u, v] w = Vec(D, { ('y1', 'x1'): 1 }) l0, l1 = make_equations(358, 36, 0, 0) l4, l5 = make_equations(329, 597, 0, 1) l2, l3 = make_equations(592, 157, 0, 1) l6, l7 = make_equations(580, 483, 0, 1) L = matutil.rowdict2mat({ 0: l0, 1: l1, 2: l2, 3: l3, 4: l4, 5: l5, 6: l6, 7: l7, 8: w, }) b = vecutil.list2vec([0, 0, 0, 0, 0, 0, 0, 0, 1]) print(solver.solve(L, b))
def test_method_orthogonal(method): b = vecutil.list2vec([1, 1, 1]) v1 = vecutil.list2vec([0, 2, 2]) v2 = vecutil.list2vec([0, 1, -1]) vlist = [v1, v2] return method(b, vlist)
def orthonormalization(L): """ input: a list L of linearly independent Vecs output: a list L* of len(L) orthonormal Vecs such that, for i = 1, ..., len(L), the first i Vecs of L* and the first i vecs of L span the same space """ orthed_up = orthogonalize(L) norms = [vec_norm(v) for v in orthed_up] return [(1 / norms[i] * orthed_up[i]) for i in range(len(orthed_up))] orth_test_list = [ vecutil.list2vec([4, 3, 1, 2]), vecutil.list2vec([8, 9, -5, -5]), vecutil.list2vec([10, 1, -1, 5]) ] orth_test = orthonormalization(orth_test_list) orth_test_rounded = [(vecutil.list2vec([round(o[d], 2) for d in o.D])) for o in orth_test] assert (orth_test_rounded == [ Vec({0, 1, 2, 3}, { 0: 0.73, 1: 0.55, 2: 0.18, 3: 0.37 }), Vec({0, 1, 2, 3}, {
def test_method_non_orthogonal(method): first = vecutil.list2vec([1, 0]) second = vecutil.list2vec([math.sqrt(2) / 2, math.sqrt(2) / 2]) vlist = [first, second] b = vecutil.list2vec([1, 1]) return method(b, vlist)
# c) SAME DIMENSION SAME RANK from vec import Vec from dimension import independence from vector import vecutil from matrix import matutil # 6.7.6 def my_is_independent(L): n = len(L) rank = independence.rank(L) return n == rank a = [vecutil.list2vec([1, 2, 3]), vecutil.list2vec([2, 2, 2])] assert (my_is_independent(a) == True) assert (my_is_independent([ vecutil.list2vec(v) for v in [[2, 4, 0], [8, 16, 4], [0, 0, 7]] ]) == False) # 6.7.7 def my_rank(L): while not independence.is_independent(L): L.pop() return len(L) my_rank_test = [ vecutil.list2vec(v) for v in [[1, 2, 3], [4, 5, 6], [1.1, 1.1, 1.1]]
def row_rearrange_algorithm(rowlist): col_label_list = sorted(rowlist[0].D, key=hash) new_rowlist = [] rows_left = set(range(len(rowlist))) for c in col_label_list: rows_with_nonzero = [r for r in rows_left if rowlist[r][c] != 0] if rows_with_nonzero != []: pivot = rows_with_nonzero[0] new_rowlist.append(rowlist[pivot]) for r in rows_with_nonzero[1:]: multiplier = rowlist[r][c] / rowlist[pivot][c] rowlist[r] -= multiplier * rowlist[pivot] rows_left.remove(pivot) return new_rowlist def print_rowlist(rowlist): as_mat = matutil.rowdict2mat(rowlist) print("%s" % as_mat) test = [[0, 2, 3, 4, 5], [0, 0, 0, 3, 2], [1, 2, 3, 4, 5], [0, 0, 0, 6, 7], [0, 0, 0, 9, 9]] a = [vecutil.list2vec(v) for v in test] print_rowlist(row_rearrange_algorithm(a))
def create_random_vector(n): return list2vec([randGF2() for i in range(n)])
from GF2 import one from vector.vecutil import list2vec from dimension import independence from matrix import bitutil, matutil import random a0 = list2vec([one, one, 0, one, 0, one]) b0 = list2vec([one, one, 0, 0, 0, one]) def randGF2(): return random.randint(0, 1) * one def create_random_vector(n): return list2vec([randGF2() for i in range(n)]) def choose_secret_vector(s, t): """ input: GF(2) field elements s and t (i.e bits) output: a random 6-vector u such that a0 * u = s and b0 * u = t """ u = create_random_vector(6) while a0 * u != s and b0 * u != t: u = create_random_vector(6) return u s = 0 t = one u = choose_secret_vector(s, t) print(u) def generate_remaining_vectors():