-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensorCode.py
More file actions
61 lines (51 loc) · 1.92 KB
/
TensorCode.py
File metadata and controls
61 lines (51 loc) · 1.92 KB
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
import numpy as np
from collections import namedtuple
LinCode = namedtuple('LinCode', ['shape', 'codeword'])
class C0:
@staticmethod
def __new__(_, w):
shape = w.shape if isinstance(w, np.ndarray) else len(w)
cw = np.concatenate((w, [sum(w)%2]))
return LinCode(shape, cw)
def __eq__(self, other):
if isinstance(other, C0):
return np.array_equal(self.cw, other.cw)
raise NotImplementedError
@staticmethod
def test(codeword):
w=codeword.codeword[:codeword.shape]
return C0(w) == codeword
class TensorCode:
NUM_TO_CHECK = 3
def __init__(self, baseCode=C0):
self.code=baseCode
def __call__(self, w):
if isinstance(w, np.ndarray):
shape = w.shape
rows = np.apply_along_axis(lambda x: self.code(x).codeword, 1, w)
cols = np.apply_along_axis(lambda x: self.code(x).codeword, 0, rows)
return LinCode(shape, cols)
w = np.array(w)
dim = int(np.sqrt(w.shape[0]))
w = w.reshape((dim, dim))
return self(w)
def get_word(self, codeword: LinCode):
shape = codeword.shape
return codeword.codeword[:shape[0], : shape[1]]
def test(self, codeword: LinCode):
cols, rows = codeword.shape
w=codeword.codeword[:cols, :rows]
for i in range(self.NUM_TO_CHECK):
# choose random row
row_idx = np.random.randint(rows)
r_w = w[row_idx,:]
tmp_cw = self.code(r_w)
if not np.array_equal(tmp_cw.codeword, codeword.codeword[row_idx, :]):
return False
# choose random column
col_idx = np.random.randint(cols)
c_w = w[:, col_idx]
tmp_cw = self.code(c_w)
if not np.array_equal(tmp_cw.codeword, codeword.codeword[:, col_idx]):
return False
return True