def test_longdivide(self): C = PolynomialModP([1, 0, 0, 0, 0, 0, 0, 1], 2) D = PolynomialModP([1, 0, 1, 1], 2) q, r = C.long_divide(D) self.assertEqual(q.coefs, [1, 0, 1, 1, 1]) self.assertEqual(q.p, 2) self.assertEqual(r.coefs, [0]) self.assertEqual(r.p, 2)
def __init__(self, f, p): if type(f) is list: f = PolynomialModP(f, p) # Polynomial gets mod p of field elif type(f) is PolynomialModP: f.p = p # Polynomial gets mod p of field else: raise ValueError("Argument f type " + str(type(f)) + " is not supported.") if type(p) is not int: raise ValueError("Argument p is not an integer.") if p < 2: raise ValueError("Argument p is too small, should be larger than 1.") if p > 100: raise ValueError("Argument p is too large, should be smaller than 100.") if not is_prime(p): raise ValueError("Argument p is not actually prime.") self.p = p if f.degree() > 1 and not self.isIrreducible(f) \ or f.degree() == 1 and f.coefs[1] == 0: raise ValueError("Argument f is not irreducible.") if f.degree() == 0: raise ValueError("Argument f can not be constant.") self.f = f
def test_gcd_so(self): A = PolynomialModP([1, 0, 1, 0, 1], 2) B = PolynomialModP([1, 1, 1, 1], 2) g, x, y = A.gcd(B) self.assertEqual(g, 1) self.assertEqual(x * A + y * B, g)
def test_1_8(self): A = PolynomialModP([1, -1], 7) B = PolynomialModP([1, 1, 1], 7) g, x, y = A.gcd(B) self.assertEqual(g, PolynomialModP([3], 7)) self.assertEqual(x * A + y * B, g)
def test_1_1c(self): A = PolynomialModP([1, -1, 1], 3) B = PolynomialModP([1, 0, 1, 2], 3) g, x, y = A.gcd(B) self.assertEqual(g, PolynomialModP([1, -2], 3)) self.assertEqual(x * A + y * B, g)
def test_1_1b(self): A = PolynomialModP([1, 0, 1], 2) B = PolynomialModP([1, 0, 0, 1], 2) g, x, y = A.gcd(B) self.assertEqual(g, PolynomialModP([1, 1], 2)) self.assertEqual(x * A + y * B, g)
def test___init__comblist(self): C = PolynomialModP([IntegerModP(3, 7), 2, IntegerModP(1, 11)], 5) C.coefs = [3, 2, 1] C.p = 5
def test___init__intlist(self): C = PolynomialModP([3, 2, 1], 5) C.coefs = [3, 2, 1] C.p = 5