def test_sf_factor(self): t = Var('t') x = Var('x') field = IF(2)[t] self.assertCountEqual([(t + 1 @ field, 2)], field.square_free_factorization(t**2 + 1), "easy test") f = (t + 1)**2 * t**3 self.assertCountEqual([(t @ field, 3), (t + 1 @ field, 2)], field.square_free_factorization(f), "easy test") field = IF(5)[t] g = t @ field h = (t + 3) @ field f = (g**3 * h**3) @ field self.assertCountEqual([(g * h @ field, 3)], field.square_free_factorization(f), "medium test") field = IF(7, x**3 + x + 1)[t] f1 = t @ field f2 = (t**2 + 2) @ field f3 = (t**3 + t * (x + 1) + (x**2)) @ field f = (f1**3 * f2**2 * f3**3) @ field self.assertCountEqual([(f2, 2), (f1 * f3, 3)], field.square_free_factorization(f), "hard test")
def test_dd_factor(self): t = Var('t') x = Var('x') field = IF(2)[t] self.assertCountEqual([(t + 1 @ field, 1)], field.distinct_degree_factorization(t + 1), "easy test") f = (t**3 + t + 1) @ field self.assertCountEqual([(f, 3)], field.distinct_degree_factorization(f), "easy test") g = (t + 1) @ field h = (f * g) @ field self.assertCountEqual([(f, 3), (g, 1)], field.distinct_degree_factorization(h), "easy test") h1 = (t**7 + t**6 + t**5 + t**4 + t**2 + t + 1) @ field # "Random" polynomial, but it's irreducible h2 = (t**7 + t**5 + t**4 + t**3 + t**2 + t + 1) @ field self.assertCountEqual([(f, 3), (g, 1), ((h1 * h2) @ field, 7)], field.distinct_degree_factorization( (f * g * h1 * h2) @ field), "medium test") field = IF(2, x**3 + x + 1)[t] f1 = t @ field # "Random" polynomials, but they are irreducible f2 = (t**3 + t**2 + (x**2 + x)) @ field f3 = (t**3 + t * (x + 1) + (x**2)) @ field f = (f1 * f2 * f3) @ field self.assertCountEqual([(f1, 1), ((f2 * f3) @ field, 3)], field.distinct_degree_factorization(f), "hard test")
def test_ed_factor(self): t = Var('t') x = Var('x') field = IF(2)[t] self.assertCountEqual([(t + 1) @ field], field.equal_degree_factorization(t + 1, 1), "easy test") f = (t**2 + 1) @ field self.assertCountEqual([(t + 1) @ field, (t + 1) @ field], field.equal_degree_factorization(f, 1), "easy test") g = (t**3 + t + 1) @ field h = (t**3 + t**2 + 1) @ field f = (g * h) @ field self.assertCountEqual([g, h], field.equal_degree_factorization(f, 3), "medium test") field = IF(7)[t] g = (t**3 + 5 * t + 5) @ field h = (t**3 + 4 * t**2 + 3) @ field f = (g * h) @ field self.assertCountEqual([g, h], field.equal_degree_factorization(f, 3), "medium test") field = IF(2, x**3 + x + 1)[t] f1 = (t**2 + t + x + 1) @ field f2 = (t**2 * x + t * (x + 1) + x) @ field f = (f1 * f2) @ field self.assertCountEqual([f1, f2], field.equal_degree_factorization(f, 2), "hard test")
def test_factor(self): for method in ['ts', 'bfa', 'cz']: t = Var('t') x = Var('x') if method != 'cz': field = IF(2)[t] f = (t + 1) @ field self.assertCountEqual([f], field.factor(f, method=method), "easy test " + method) g = (t + 1) @ field h = t @ field f = (g * h) @ field self.assertCountEqual([g, h], field.factor(f, method=method), "easy test " + method) f = (g**2) @ field self.assertCountEqual([g, g], field.factor(f, method=method), "easy test " + method) field = IF(5)[t] g = (2 * t + 1) @ field h = field.divmod(g, 2)[0] f = (g**2) @ field self.assertCountEqual([h, h, 4], field.factor(f, method=method), "medium test " + method) g = (t**3 + t + 1) @ field h = (t + 2) @ field f = (g**2 * h**2) @ field self.assertCountEqual([g, g, h, h], field.factor(f, method=method), "medium test " + method) if method != 'cz': field = IF(2, x**3 + x + 1)[t] f1 = (t + 1) @ field f2 = (t + x + 1) @ field f3 = (t**2 + t * x + x) @ field f = (f1**3 * f2**2 * f3) @ field self.assertCountEqual([f1, f1, f1, f2, f2, f3], field.factor(f, method=method), "hard test " + method) field = IF(3, x**2 + 1)[t] f1 = (t + 2) @ field f2 = (t**2 + t * (x + 1) + 2) @ field f = (f1**5 * f2**3) @ field self.assertCountEqual([f1, f1, f1, f1, f1, f2, f2, f2], field.factor(f, method=method), "hard test " + method)
def test_ideal(self): x, y = link('x', 'y') field = IF(7) set_ordering('dp') f1 = (x**3) @ field f2 = (x**2 * y + 6 * y**4) @ field self.assertTrue(in_ideal(f1, [f1, f2], field), "easy test") self.assertTrue(in_ideal(f2, [f1, f2], field), "easy test") self.assertTrue(in_ideal(3 * y**2 * f1 + x * f2, [f1, f2], field), "easy test") self.assertFalse(in_ideal(f1 + f2 + x, [f1, f2], field), "medium test") set_ordering('lp') self.assertTrue(in_ideal((y**7) @ field, [f1, f2], field), "medium test") self.assertTrue(in_ideal(f1 + f2 + y**7, [f1, f2], field), "medium test") x, y, z = link('x', 'y', 'z') f1 = (x * y - 1) @ field f2 = (x * z - 1) @ field self.assertFalse( in_ideal(2 * f1 + z * f2 + (x + y) * (y - z) + y**2, [f1, f2], field), "medium test")
def test_groebner(self): x, y = link('x', 'y') field = IF(7) set_ordering('dp') f1 = (x**3) @ field f2 = (x**2 * y + 6 * y**4) @ field self.assertCountEqual([f1, f2], groebner_basis([f1, f2], field), "easy test") set_ordering('lp') f1 = (x**3) @ field f2 = (x**2 * y + 6 * y**4) @ field f3 = (x * y**4) @ field f4 = (y**7) @ field self.assertCountEqual([f1, f2, f3, f4], groebner_basis([f1, f2], field), "medium test") x, y, z = link('x', 'y', 'z') f1 = (x * y - 1) @ field f2 = (x * z - 1) @ field f3 = (y - z) @ field self.assertCountEqual([f1, f2, f3], groebner_basis([f1, f2], field), "medium test") f1 = (z**2 - 2) @ field f2 = (y**2 + 2 * y - 1) @ field f3 = ((y + z + 1) * x + y * z + z + 2) @ field f4 = (x**2 + x + y - 1) @ field self.assertCountEqual([f1, f2, f3, f4], groebner_basis([f1, f2, f3, f4], field), "medium test")
def test_field(self): t = Var('t') field = IF(7)[t] p = t**2 + 2 * t + 1 q = t + 1 self.assertEqual(q, field.gcd(p, q), "easy test")
def test_logarithm(self): # g^x = h => x = log(g, h) field = IF(5) self.assertEqual(3, field.discrete_logarithm(2, 3), "easy test") field = IF(59) self.assertEqual(25, field.discrete_logarithm(2, 11), "medium test") field = IF(383) self.assertEqual(110, field.discrete_logarithm(2, 228), "medium test") x = Var('x') field = IF(2, x**2 + x + 1) self.assertEqual(2, field.discrete_logarithm(x, x + 1), "hard test") field = IF(3, x**2 + 1) self.assertEqual(7, field.discrete_logarithm(x + 1, x + 2), "hard test")
def test_inverse(self): field = IF(3) self.assertEqual(1, field.inverse(1), "easy test") self.assertEqual(2, field.inverse(2), "easy test") self.assertRaises((ZeroDivisionError, ValueError), field.inverse, 0) mods = [17, 19, 23, 37, 97, 773] for p in mods: field = IF(p) for _ in range(20): a = random.randrange(1, p) self.assertEqual(pow(a, -1, p), field.inverse(a), "random test") x = Var('x') field = IF(2, x**8 + x**4 + x**3 + x + 1) a = x**6 + x**4 + x + 1 self.assertEqual(x**7 + x**6 + x**3 + x, field.inverse(a), "poly test") self.assertRaises((ZeroDivisionError, ValueError), field.inverse, 0)
def test_irreducibility(self): t = Var('t') x = Var('x') field = IF(2)[t] self.assertTrue(field.is_irreducible(t + 1), "easy test True") self.assertTrue( field.is_irreducible(t**7 + t**6 + t**5 + t**4 + t**2 + t + 1), "easy test True") field = IF(3)[t] self.assertFalse(field.is_irreducible(t**3 + 1), "easy test False") # (t + 1) ** 3 field = IF(2, x**2 + x + 1)[t] self.assertTrue(field.is_irreducible(t**2 - t + x), "medium test True") self.assertFalse(field.is_irreducible(t**2 - (x + 1)), "medium test False") # (t - x) * (t + x) field = IF(3, x**2 - x - 1)[t] self.assertTrue(field.is_irreducible(t**2 - t * (x - 1) - 1), "medium test True")
def test_multidivision(self): x, = link('x') field = IF(2) f = x**2 + x + 1 fs = [x + 1] q, r = multidivision(f, fs, field) self.assertEqual(f @ field, (q * fs[0] + r) @ field, "easy test") x, y = link('x', 'y') f = x**3 * y**2 fs = [x**3, x**2 * y - y**4] q1, q2, r = multidivision(f, fs, field) self.assertEqual(f @ field, (q1 * fs[0] + q2 * fs[1] + r) @ field, "medium test") x, y, z = link('x', 'y', 'z') field = IF(17) f = x**2 * z + 2 * x**2 * y + 5 * z**2 * x + 3 * y**2 * z fs = [x**2, x * y**2, z - y] q1, q2, q3, r = multidivision(f, fs, field) self.assertEqual(f @ field, (q1 * fs[0] + q2 * fs[1] + q3 * fs[2] + r) @ field, "hard test")