def decode_linear_code(c, g, syndrome_table): ''' function to decode codeword encoded with linear_code Parameters ---------- c : list or np.ndarray code_vector. g : numpy.Polynominal generator polynominal, used to create code_vector. syndrome_table : list of error_vectors if codeword contains an error, syndrome_table is used to correct wrong bit. Returns ------- numpy.ndarray data_vector. ''' q, r = divmod(Poly(c), g) q = np.r_[q.coef % 2, np.zeros(len(c) - len(q) - len(g) + 1)] r = np.r_[r.coef % 2, np.zeros(len(g) - len(r))] syndrome_index = np.sum([int(a * 2**i) for i, a in enumerate(r)]) while syndrome_index > 0: c = c ^ syndrome_table[syndrome_index] q, r = divmod(Poly(c), g) q = np.r_[q.coef % 2, np.zeros(len(c) - len(q) - len(g) + 1)] r = np.r_[r.coef % 2, np.zeros(len(g) - len(r))] syndrome_index = np.sum([int(a * 2**i) for i, a in enumerate(r)]) return np.array(q, dtype=int)
def move(self, x_new, y_new, z_new): """ move the crankshaft into the commanded node orientation :param x_new: float, new x coordinate of the crankshaft node :param y_new: float, new y coordinate of the crankshaft node :param z_new: float, new z coordinate of the crankshaft node :return: """ self._node['x'] = x_new self._node['y'] = y_new self._node['z'] = z_new node_local = self._node_loc_local( ) # local x, y and z for the platform x = node_local['x'] y = node_local['y'] z = node_local['z'] k_sq = self._crank.length**2 - self._link.length**2 + x**2 + y**2 + z**2 a = 1 + (x / z)**2 # x^2 term b = -(k_sq * x) / (z**2) # x term c = (k_sq / (2 * z))**2 - self._crank.length**2 # constant term c_local_x = Poly([c, b, a]).roots()[1] # ax^2 + bx + c = 0 if np.iscomplex(c_local_x): print("You cannot complete this move!") self.incompatible = True else: self.incompatible = False c_local_z = k_sq / (2 * z) - (c_local_x * x / z) self._crank.move({'x': c_local_x, 'z': c_local_z}) self._connector = self._con_loc_global() return
def create_syndrome_table(n, g): ''' create syndrome table for correcting errors in linear code Parameters ---------- n : int len of linear-code code_vector. g : numpy.Polinominal generator polynominal used by linear-code-encoder. Returns ------- list of error_vectors, one per syndrome. get the corresponding error_vector by using the value represented by your syndrome as index of this list. ''' zeros = np.zeros(n, dtype=int) syndrome_table = [0 for i in range(n + 1)] syndrome_table[0] = zeros #when syndrome = 0, no bit-error to correct for i in range(n): faulty_vector = zeros.copy() faulty_vector[i] = 1 q, r = divmod(Poly(faulty_vector), g) r = np.r_[r.coef % 2, np.zeros(len(g) - len(r))] index = np.sum([int(a * 2**i) for i, a in enumerate(r)]) syndrome_table[index] = faulty_vector return np.array(syndrome_table)
def __read_file(self, bhm_out_name): """Read the file and fill relevant object variables""" with open(bhm_out_name, "r") as bhm_out: # skip initial comment lines while True: line = bhm_out.readline() if line[0] != '#': break order, n_pieces = np.fromstring(line, sep=" ") self._order = int(order) n_pieces = int(n_pieces) line = bhm_out.readline() self._knots = np.fromstring(line, sep=" ") if self._knots.size != n_pieces + 1: raise ValueError("Incorrect number of knots %d" % self._knots.size) self._spline_pieces = [] self._errorbar_pieces = [] for i in range(0, n_pieces): header = bhm_out.readline() # discarded line = bhm_out.readline() coeffs = np.fromstring(line, sep=" ") if coeffs.size != self._order + 1: raise ValueError( "Spline piece #%d: incorrect number of coefficients %d" % (i, coeffs.size)) self._spline_pieces.append(Poly(coeffs)) line = bhm_out.readline() # self._bar_coeff[i,:] = np.fromstring(line, sep=" ") bar_coeffs = np.fromstring(line, sep=" ") if bar_coeffs.size != 2 * self._order + 1: raise ValueError( "Spline piece #%d: incorrect number of error bar coefficients %d" % (i, bar_coeffs.size)) self._errorbar_pieces.append(Poly(bar_coeffs)) return
def findCurve(self): #self.evaluating = True gradient = self.getGradient() C0 = self.getC0() self._dynamic_ax.plot(gradient.get_xdata(), gradient.get_ydata()) roots = self.getRoots(gradient) self._dynamic_ax.scatter(roots[0, :], roots[1, :]) curveDegree = len(roots[0, :]) + 1 start_Coefficients = np.ones(curveDegree) start_Coefficients[0] = C0 polynom = Poly(start_Coefficients) prediction = polynom(self.dataPoints.get_xdata()) print(gradient.get_ydata()) print(self.dataPoints.get_ydata()) #print(C0) self._dynamic_ax.plot(gradient.get_xdata(), prediction) self.dynamic_canvas.draw()
def __init__(self, coef, omega): self.h = Poly(coef) self.omega = omega
char=np.sum([b*2**i for i, b in enumerate(blocks[i])]) + \ np.sum([b*2**(i+4) for i, b in enumerate(blocks[i+1])]) string += chr(char) return string if __name__ == '__main__': #shared attributes: n = 7 k = 4 t = 1 #private key(s): g = Poly( [1, 1, 0, 1] ) #generator polynom for 7/4 linear code (from table, 1.0 + 1.0·x¹ + 0.0·x² + 1.0·x³) P_M = gen_perm_M(n) #create permutation matrix G_M = create_linear_code_matrix(n, k, g) #linear code generator matrix S_M, S_inv = create_rand_bin_M(k, True) #random binary matrix and its inverse P_M_inv = P_M.T #inverse permutation matrix syndrome_table = create_syndrome_table(n, g) #part of linear-code decoder linear_code_decoder = lambda c: decode_linear_code(c, g, syndrome_table) #public key: pub_key = (P_M @ G_M @ S_M) % 2 msg_tx = 'Hello World?'