def create_generator_polynomial(num_correction_bytes): """ Description: Generates a static generator Polynomial for error correction, which is the product of (x-2^i) for all i in the set {0, 1, ..., num_correction_bytes - 1}. Inputs: num_correction_bytes -- desired number of error correction bytes. In the formula, this is represented as k. Returns: A generator Polynomial for generating Reed-Solomon encoding data. """ # Replace with your code for part 5.B starting_map = {} starting_map[0] = z256.power(2, 0) starting_map[1] = 1 generator_polynomial = Polynomial(starting_map) # The code below is to continue multiplying when k > 1 if num_correction_bytes > 1: for power in range(1, num_correction_bytes): new_map = {} new_map[0] = z256.sub(0, z256.power(2, power)) new_map[1] = 1 new_expression = Polynomial(new_map) generator_polynomial = generator_polynomial.multiply_by_polynomial(new_expression) return generator_polynomial
def subtract_term(self, coefficient, power): """ Returns a new Polynomial that is the difference of this polynomial and (coefficient) * x^(power) using Z_256 arithmetic to subtract coefficients, if necessary. """ new_polynomial = Polynomial(self.get_terms()) terms = new_polynomial.get_terms() # The following statements are used to subtract the coefficient of the # monomial from that of coresponding term in the polynomial if power in terms.keys(): terms[power] = z256.sub(terms[power], coefficient) else: terms[power] = 0 terms[power] = z256.sub(0, coefficient) return Polynomial(terms)
def subtract_term(self, coefficient, power): """ Returns a new Polynomial that is the difference of this polynomial and (coefficient) * x^(power) using Z_256 arithmetic to subtract coefficients, if necessary. """ # Replace with your code for part 3.B new_expression1 = Polynomial(self._terms) return new_expression1.add_term(z256.sub(0, coefficient), power)
def create_generator_polynomial(num_correction_bytes): """ Generates a static generator Polynomial for error correction, which is the product of (x-2^i) for all i in the set {0, 1, ..., num_correction_bytes - 1}. Inputs: - num_correction_bytes: desired number of error correction bytes. In the formula, this is represented as k. Returns: generator Polynomial for generating Reed-Solomon encoding data. """ # Replace with your code for part 5.B generator_poly = Polynomial({1: 1, 0: z256.sub(0, 1)}) for idx in range(1, num_correction_bytes): mult_gen = Polynomial({1: 1, 0: z256.sub(0, z256.power(2, idx))}) generator_poly = generator_poly.multiply_by_polynomial(mult_gen) return generator_poly
def subtract_term(self, coefficient, power): """ Subtract one term from this polynomial. inputs: - coefficient: a Z_256 number representing the coefficient of the term - power: an integer representing the power of the term Returns: a new Polynomial that is the difference of this polynomial and (coefficient) * x^(power) using Z_256 arithmetic to subtract coefficients, if necessary. """ # Replace with your code for part 3.B #using defaultdict to prevent key errors new_poly_dict = defaultdict(int) old_poly_dict = self.get_terms() for key, value in old_poly_dict.items(): new_poly_dict[key] = value new_poly_dict[power] = z256.sub(new_poly_dict[power], coefficient) subbed = Polynomial(dict(new_poly_dict)) return subbed