def test_polyprod(): pm = gf.gen_pow_matrix(37) p1 = np.array([11, 29, 26, 31, 3]) p2 = np.array([19, 25, 31, 3, 14, 29]) right_answer = np.array([28, 25, 29, 30, 27, 17, 14, 1, 27, 2]) assert_equal(right_answer, gf.polyprod(p1, p2, pm))
def do_polyprod_and_polydiv_test(self, p1, p2, pm): div = gf.polydiv(p1, p2, pm) mult = gf.polyprod(div[0], p2, pm) assert_array_equal( p1, gf.add( mult, np.concatenate( [np.zeros(len(mult) - len(div[1])).astype(int), div[1]])))
def _encode(self, u): assert self.n - (self.g.shape[0] - 1) == u.shape[0] x_power = np.zeros_like(self.g, dtype=np.int) x_power[0] = 1 x_power_u = gf.polyprod(x_power, u, self.pm) _, mod = gf.polydivmod(x_power_u, self.g, self.pm) result = gf.polyadd(x_power_u, mod) result = np.concatenate( [np.zeros([self.n - result.shape[0]], dtype=np.int), result]) return result
def encode(self, U): """Кодирование БЧХ""" fi = [] x = np.zeros(self.g.shape[0], dtype=int) x[0] = 1 for word in U: first = polyprod(x, word, self.pm) second = polydiv(first, self.g, self.pm) v = sump(first, second[1]) if v.size < self.pm.shape[0] - 1: v = np.concatenate((np.zeros(self.pm.shape[0] - 1 - v.shape[0], dtype=int), v)) fi.append(v) return np.asarray(fi)
def encode(self, U): n = self.pm.shape[0] m = self.g.shape[0] - 1 x = np.zeros(m + 1, dtype=np.int64) x[0] = 1 res = np.zeros((U.shape[0], U.shape[1] + m), dtype=np.int64) for i in range(U.shape[0]): u = U[i] code = gf.polyprod(x, u, self.pm) _, mod = gf.polydivmod(code, self.g, self.pm) code = gf.polyadd(code, mod) len_code = code.shape[0] res[i][-len_code:] = code return res
def encode(self, U): # res = x^m * u(x) - (x^m * u(x)) mod g(x) # |________| |________| # ^ ^ # | | # s + s mod g(x) message_number, k = U.shape m = self.g.size - 1 x_m = np.zeros(m + 1, dtype=int) x_m[0] = 1 res = np.zeros((message_number, k + m), dtype=int) for i in range(message_number): s = gf.polyprod(x_m, U[i, :], self.pm) mod = gf.polydiv(s, self.g, self.pm)[1] tmp = gf.polyadd(s, mod) res[i, -tmp.size:] = tmp.copy() return res
def encode(self, U): mesg_cnt, k = U.shape x_m = np.zeros(self.m + 1).astype('int') x_m[0] = 1 V = np.zeros((mesg_cnt, self.n)).astype('int') for i in range(mesg_cnt): u = U[i, :] x_m_prod_u = gf.polyprod(x_m, u, self.pm) div, mod = gf.polydivmod(x_m_prod_u, self.g, self.pm) v = gf.polyadd(x_m_prod_u, mod) ind = self.n - v.shape[0] V[i, ind:] = gf.polyadd(x_m_prod_u, mod) return V
def test_polyprod(self): assert_array_equal( gf.polyprod(np.array([1, 0b11]), np.array([1, 0b100]), gf.gen_pow_matrix(0b1011)), [1, 0b111, 0b111])