Example #1
0
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    sCols = mat2coldict(S)
    return coldict2mat({k: find_error(sCols[k]) for k in sCols})


## print(find_error_matrix(listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])))

## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."
P = bitutil.bits2mat(bitutil.str2bits(s))

##print(P)

n = bitutil.noise(P, 0.03)

##print(bitutil.bits2str(bitutil.mat2bits(n+P)))

## Task 7
C = G * P
bits_before = len(P.D[0]) * len(P.D[1])
bits_after = len(C.D[0]) * len(C.D[1])

##print(C)

## Ungraded Task
CTILDE = C + bitutil.noise(C, 0.02)

## Task 8
def correct(A):
Example #2
0
    Input: a matrix S whose columns are error syndromes
    Output: a matrix whose cth column is the error corresponding to the cth column of S.
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S) == Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 3): 0, (3, 0): 0, (2, 1): 0, (6, 2): 0, (5, 1): one, (0, 3): 0, (4, 0): 0, (1, 2): 0, (3, 3): 0, (6, 3): 0, (5, 0): 0, (2, 2): 0, (4, 1): 0, (1, 1): 0, (3, 2): one, (0, 0): 0, (6, 0): 0, (2, 3): 0, (4, 2): 0, (1, 0): 0, (5, 3): 0, (0, 1): 0, (6, 1): 0, (3, 1): 0, (2, 0): 0, (4, 3): one, (5, 2): 0, (0, 2): 0})
        True
    """
    return coldict2mat(
        dict((k, find_error(c)) for (k, c) in mat2coldict(S).items()))


## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))

EP = P + noise(P, 0.02)
s_break = bits2str(mat2bits(EP))
# print(s_break)

## Task 9
C = G * P
bits_before = len(str2bits(s))
bits_after = len(mat2bits(C))
# print(bits_before, bits_after)

## Ungraded Task
CTILDE = C + noise(C, 0.04)

# print(bits2str(mat2bits(R * CTILDE)))

Example #3
0
  return Vec(set(range(1,8)), {el1:1})



#if __name__ == '__main__':
  #print(one)
  
L = [[one,0, one,one],[one, one, 0,one],[0,0,0,one],[one, one,one,0],[0,0,one,0],[0,one,0,0],[one,0,0,0]]
p = listlist2mat([[one,0,0,one]]).transpose()
G = listlist2mat(L)


#print G
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it"
#print s
# L = bits2mat(str2bits(s))
# print one*one
#print len(str2bits(s))
p = bits2mat(str2bits(s))
p = noise(p,0.2)
#print p
G_p = matrix_matrix_mul(G,p)
Rl = [[0,0,0,0,0,0,one],[0,0,0,0,0,one,0],[0,0,0,0,one,0,0],[0,0,one,0,0,0,0]]
R = listlist2mat(Rl)
result = matrix_matrix_mul(R,G_p)
#print bits2str(mat2bits(result))
# error_syndrome = Vec({1, 2, 3}, {1: 0, 2: 1,3:1})
# print find_error(error_syndrome)
# L_v = {'a':Vec({'a','b'},{'a':0,'b':0})}
# print coldict2mat(L_v)
Example #4
0
    """
    return rowdict2mat(
        {c: find_error([S[r, c] for r in S.D[0]])
         for c in S.D[1]}).transpose()


## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))

## Task 9
C = G * P
bits_before = len(P.D[1]) * len(P.D[0])
bits_after = len(C.D[0]) * len(C.D[1])

## Ungraded Task
CTILDE = G * P + G * noise(P, 0.02)


## Task 10
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A) == Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
        True
    """
    return A + find_error_matrix(H * A)
Example #5
0
matrix_of_bits = bits2mat(bits)
print(matrix_of_bits)

bits_back = mat2bits(matrix_of_bits)
print(bits_back)

print(str_back)
print()
print(len(str_back))


# try the noise
P = bits2mat(str2bits(str))
C = G*P
f = 0.5
E = noise(C, f)
C_tilde = C + E
C_correct = correct(C_tilde)
codeword_decode = R*C_correct
Rcv_str = bits2str(mat2bits(R*C_tilde))
ham_correct_str = bits2str(mat2bits(codeword_decode))
print("[original string]:")
print(str)
print("Prob is ",f)
print("[rcved string]:")
print(Rcv_str.encode('utf-8'))
print("[hamming corrected string]:")
print(ham_correct_str.encode('utf-8'))


Example #6
0
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    return coldict2mat({i: find_error(he) for i, he in mat2coldict(S).items()})


## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."
P = bits2mat(str2bits(s))

## Task 7
C = G * P
bits_before = len(mat2bits(P))
bits_after = len(mat2bits(C))

## Ungraded Task
CTILDE = C + noise(C, 0.02)


## Task 8
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
    """
    return A + find_error_matrix(H * A)

Example #7
0
            if S.f[y,x] == one:
                pos += (2 ** (len(S.D[0])-y-1) )
        retMat.f[pos-1,x] = one
    return retMat

## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))

## Task 9
C = G*P
bits_before = 4*224
bits_after = 7*224


## Ungraded Task
CTILDE = noise(C, 0.02) + C

## Task 10
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A) == Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
        True
    """
    return find_error_matrix(H * A) + A

Example #8
0
s = ''.join([chr(i) for i in range(256)])
#print(bits2str(str2bits(s)) == s) #True

#Task 4.14.9
#print(mat2bits(bits2mat(str2bits(s))) == str2bits(s)) #True

#Task 4.14.10
neo_str = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
#neo_str = 'a'
P = bits2mat(str2bits(neo_str))

#Task 4.14.11
#print(bits2str(mat2bits(P + noise(P, 0.02))))

#Task 4.14.12
C = G * P
CTILDE = C + noise(C, 0.02)

#Task 4.14.13
#print(bits2str(mat2bits(G_R * CTILDE)))


#Task 4.14.14
def correct(A):
    return A + find_error_matrix(A)


#Task 4.14.15
#print(bits2str(mat2bits(G_R * correct(CTILDE))))
#Does not succeed in fixing all the corrupted characters, because sometimes there's more than one error per column.
Example #9
0
        e = find_error(S[i])  # S[i] returns i-th column of S
        for j in res.D[0]:
            res[j, i] = e[j]

    return res


## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))  # p[i] column is 4-bit nibble

## Ungraded task
# Use noise(A, s) to produce the transmitted message
# input: P - matrix being transmitted
print("Message w/o encoding and ecc:",
      bits2str(mat2bits(P + noise(P, 0.02))).encode("utf-8"))

## Task 9
C = G * P  # encode
bits_before = len(str2bits(s))  # bits before encoding
bits_after = bits_before // 4 * 7  # bits after encoding
print(bits_before, bits_after)

## Ungraded Task
CTILDE = C + noise(C, 0.02)  # encoded message received
print("Message with encoding, but w/o ecc:",
      bits2str(mat2bits(R * CTILDE)).encode("utf-8"))


## Task 10
def correct(A):
Example #10
0
    """
    #return coldict2mat([find_error(e) for e in mat2coldict(S).values()])
    return coldict2mat({k:find_error(e) for k,e in mat2coldict(S).items()})

## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))

## Task 9
C = G*P
bits_before = len(P.D[0])*len(P.D[1])
bits_after = len(C.D[0])*len(C.D[1])


## Ungraded Task
CTILDE = C + noise(C,.01)

## Task 10
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A) == Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
        True
    """
    return A + find_error_matrix(H*A)

##non_codeword = Vec({0,1,2,3,4,5,6}, {0: one, 1:0, 2:one, 3:one, 4:0, 5:one, 6:one})
##error_vector = find_error(H*non_codeword)
Example #11
0
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    return coldict2mat([find_error(mat2coldict(S)[k]) for k in mat2coldict(S)])

## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You\x19re the one that has to walk through it."
P = bits2mat(str2bits(s))

## Task 7
C = G*P
bits_before = len(P.D[1])*4
bits_after = len(C.D[1])*7


## Ungraded Task
CTILDE = C + noise(C, 0.2)

## Task 8
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
    """
    return A + find_error_matrix(H*A)
Example #12
0
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    return coldict2mat([find_error(mat2coldict(S)[c]) for c in S.D[1]])

## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."
P = bits2mat(str2bits(s))

## Task 7
C = G*P
bits_before = len(P.D[0])*len(P.D[1])
bits_after = len(G.D[0])*len(P.D[1])


## Ungraded Task
CTILDE = C + noise(Mat(C.D, {}), 0.02)

## Task 8
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
    """
    return find_error_matrix(H*A) + A
Example #13
0
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    cols={col:Vec(S.D[0], {row:S[row,col] for row in S.D[0]}) for col in S.D[1]}
    return coldict2mat({x:find_error(y) for (x,y) in cols.items()})



## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."

c=bitutil.str2bits(s)
#print(bitutil.bits2str(c))
P = bitutil.bits2mat(c,4,False)
E=noise(P,0.02)


## Task 7
C = G*P

bits_before = 896
bits_after = 1568


## Ungraded Task
CTILDE = C+(noise(C,0.02))


## Task 8
def correct(A):
Example #14
0
#assert s == bits2str(mat2bits(bits2mat(str2bits(s))))

## Task 4.14.11
#print(bits2str(mat2bits(P + noise(P, 0.02))))

## Task 9
C = G*P
bits_before = len(mat2bits(P))
bits_after = len(mat2bits(C))

#print("Before: {}; After: {}. Rate: {}\n".format(
#    bits_before, bits_after, float(bits_after) / bits_before))


## Ungraded Task
CTILDE = C + noise(C, 0.02)
#print(bits2str(mat2bits(R*CTILDE)))


## Task 10
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A) == Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
        True
    """
    return A + find_error_matrix(H*A)
Example #15
0
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    sCols = mat2coldict(S)
    return coldict2mat({k:find_error(sCols[k]) for k in sCols})

## print(find_error_matrix(listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])))

## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."
P = bitutil.bits2mat(bitutil.str2bits(s))

##print(P)

n = bitutil.noise(P, 0.03)

##print(bitutil.bits2str(bitutil.mat2bits(n+P)))

## Task 7
C = G*P
bits_before = len(P.D[0])*len(P.D[1])
bits_after = len(C.D[0])*len(C.D[1])

##print(C)

## Ungraded Task
CTILDE = C+bitutil.noise(C, 0.02)

## Task 8
def correct(A):
Example #16
0
def error_test(s, prob):
    msg = bits2mat(str2bits(s))
    cw = G*msg
    rcvd = cw + noise(cw, prob)
    msg_rcvd = R*correct(rcvd)
    return s == bits2str(mat2bits(msg_rcvd))
Example #17
0
    """
    Sdict= matutil.mat2coldict(S)
    coldict={i:find_error(Sdict[i]) for i in S.D[1]}
    return matutil.coldict2mat(coldict)

## Task 6
import bitutil
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."
P = bitutil.bits2mat(bitutil.str2bits(s))

## Task 7
C = G*P
bits_before = len(P.D[0])*len(P.D[1])
bits_after = len(C.D[0])*len(C.D[1])


## Ungraded Task
CTILDE = bitutil.noise(C, 0.02) + C

## Task 8
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
    """
    return A+find_error_matrix(H*A)
Example #18
0
    res = Mat(({0, 1, 2, 3, 4, 5, 6},S.D[1]), {})
    for i in res.D[1]:
        e = find_error(S[i])  # S[i] returns i-th column of S
        for j in res.D[0]:
            res[j,i] = e[j]

    return res

## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))  # p[i] column is 4-bit nibble

## Ungraded task
# Use noise(A, s) to produce the transmitted message
# input: P - matrix being transmitted
print("Message w/o encoding and ecc:", bits2str(mat2bits(P + noise(P, 0.02))).encode("utf-8"))

## Task 9
C = G*P  # encode
bits_before = len(str2bits(s))  # bits before encoding
bits_after = bits_before//4*7   # bits after encoding
print(bits_before, bits_after)

## Ungraded Task
CTILDE = C + noise(C, 0.02)  # encoded message received
print("Message with encoding, but w/o ecc:", bits2str(mat2bits(R*CTILDE)).encode("utf-8"))


## Task 10
def correct(A):
    """
# Task 4.14.7
def find_error_matrix(s):
    return coldict2mat({pos: find_error(vec) for pos, vec in mat2coldict(s).items()})

test_matrix = listlist2mat([[One(), 0], [One(), 0], [One(), One()]])

# Task 4.14.8
s = ''.join([chr(i) for i in range(256)])
assert bits2str(str2bits(s)) == s

# Task 4.14.9 & 4.14.10 encode message string to matrix of nibbles
msg = "I'm trying to free your mind, Neo."
P = bits2mat(str2bits(msg))

# Task 4.14.11 add noise to message
E = noise(P, 0.02)
EP = E + P
perturbed_msg = bits2str(mat2bits(EP))

# Task 4.14.12
C = G*P

# Task 4.14.13
CE = noise(C, 0.02)
CTILDE = C + CE
perturbed_msg2 = bits2str(mat2bits(R*CTILDE))

# Task 4.13.13
def correct(A):
    return R * (find_error_matrix(H*A) + A)