-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
88 lines (75 loc) · 3.98 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import numpy as np
ys = {
"Mike" : np.array([1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0]),
"Mikhail": np.array([0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1]),
"Dmitrii": np.array([1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1]),
"Simon" : np.array([0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0]),
"Andrew" : np.array([1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0]),
"Vlud" : np.array([0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1]),
"Azamath": np.array([0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0])
}
# initial vector
y = ys["Mikhail"]
# initial matrix
G = np.array([
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1]
])
# transponded initial matrix for column operations
G_t = np.transpose(G)
# column permutations (letters replaced with respective numbers)
permutations = np.array([
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
[0, 1, 2, 12, 13, 14, 3, 4, 5, 15, 16, 17, 18, 19, 20, 6, 7, 8, 21, 22, 23, 9, 10, 11],
[0, 1, 2, 18, 19, 20, 12, 13, 14, 6, 7, 8, 21, 22, 23, 3, 4, 5, 9, 10, 11, 15, 16, 17],
[0, 1, 2, 21, 22, 23, 18, 19, 20, 3, 4, 5, 9, 10, 11, 12, 13, 14, 15, 16, 17, 6, 7, 8],
[0, 1, 2, 9, 10, 11, 21, 22, 23, 12, 13, 14, 15, 16, 17, 18, 19, 20, 6, 7, 8, 3, 4, 5],
[0, 1, 2, 15, 16, 17, 9, 10, 11, 18, 19, 20, 6, 7, 8, 21, 22, 23, 3, 4, 5, 12, 13, 14],
[0, 1, 2, 6, 7, 8, 15, 16, 17, 21, 22, 23, 3, 4, 5, 9, 10, 11, 12, 13, 14, 18, 19, 20],
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[18, 19, 20, 6, 7, 8, 21, 22, 23, 9, 10, 11, 0, 1, 2, 12, 13, 14, 3, 4, 5, 15, 16, 17],
[21, 22, 23, 3, 4, 5, 9, 10, 11, 15, 16, 17, 0, 1, 2, 18, 19, 20, 12, 13, 14, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15, 16, 17, 6, 7, 8, 0, 1, 2, 21, 22, 23, 18, 19, 20, 3, 4, 5],
[15, 16, 17, 18, 19, 20, 6, 7, 8, 3, 4, 5, 0, 1, 2, 9, 10, 11, 21, 22, 23, 12, 13, 14],
[6, 7, 8, 21, 22, 23, 3, 4, 5, 12, 13, 14, 0, 1, 2, 15, 16, 17, 9, 10, 11, 18, 19, 20],
[3, 4, 5, 9, 10, 11, 12, 13, 14, 18, 19, 20, 0, 1, 2, 6, 7, 8, 15, 16, 17, 21, 22, 23]
])
# for name, y in ys.items():
# print(">>>>> ", name)
for i in range(len(permutations)):
print('Iteration: ', i) # number of current permutation
G_reversed = np.empty([len(G_t), len(G)], dtype = int)
for j in range(len(G_t)):
G_reversed[permutations[i][j]] = G_t[j]
G_reversed = np.transpose(G_reversed)
print('Matrix G:')
print(G_reversed)
a_i = np.empty(len(G), dtype = int) # information vector
for j in range(len(G)):
a_i[j] = y[permutations[i][j]]
print('a: ', a_i)
c_i = a_i @ G_reversed # encoded word
for j in range(len(c_i)):
c_i = c_i % 2
print('c: ', c_i)
print("y: ", y)
error_i = (c_i + y) % 2 # error vector
print('e: ', error_i)
print('e columns: ', end='')
for i in range(len(error_i)):
if (error_i[i] == 1):
print(i if i < 10 else chr(ord('a') + i - 10), end='')
print()
weight_error_i = sum(error_i) # weight of error vector
print('w(e): ', weight_error_i)
print()