def test_heugcd_univariate_integers(): R, x = ring("x", ZZ) f = x**4 + 8 * x**3 + 21 * x**2 + 22 * x + 8 g = x**3 + 6 * x**2 + 11 * x + 6 h = x**2 + 3 * x + 2 cff = x**2 + 5 * x + 4 cfg = x + 3 assert heugcd(f, g) == (h, cff, cfg) f = x**4 - 4 g = x**4 + 4 * x**2 + 4 h = x**2 + 2 cff = x**2 - 2 cfg = x**2 + 2 assert heugcd(f, g) == (h, cff, cfg) f = x**8 + x**6 - 3 * x**4 - 3 * x**3 + 8 * x**2 + 2 * x - 5 g = 3 * x**6 + 5 * x**4 - 4 * x**2 - 9 * x + 21 h = 1 cff = f cfg = g assert heugcd(f, g) == (h, cff, cfg) f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \ + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \ + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \ + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \ - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \ + 289127344604779611146960547954288113529690984687482920704*x**14 \ + 19007977035740498977629742919480623972236450681*x**7 \ + 311973482284542371301330321821976049 g = 365431878023781158602430064717380211405897160759702125019136*x**21 \ + 197599133478719444145775798221171663643171734081650688*x**14 \ - 9504116979659010018253915765478924103928886144*x**7 \ - 311973482284542371301330321821976049 # TODO: assert heugcd(f, f.diff(x))[0] == g f = 1317378933230047068160 * x + 2945748836994210856960 g = 120352542776360960 * x + 269116466014453760 h = 120352542776360960 * x + 269116466014453760 cff = 10946 cfg = 1 assert heugcd(f, g) == (h, cff, cfg) with using(heu_gcd_max=0): pytest.raises(HeuristicGCDFailed, lambda: heugcd(f, g))
def test_benchmark_cyclic_4(method): with config.using(groebner=method): R, a, b, c, d = ring('a b c d', ZZ, lex) I = [ a + b + c + d, a * b + a * d + b * c + b * d, a * b * c + a * b * d + a * c * d + b * c * d, a * b * c * d - 1 ] assert groebner(I, R) == [ 4 * a + 3 * d**9 - 4 * d**5 - 3 * d, 4 * b + 4 * c - 3 * d**9 + 4 * d**5 + 7 * d, 4 * c**2 + 3 * d**10 - 4 * d**6 - 3 * d**2, 4 * c * d**4 + 4 * c - d**9 + 4 * d**5 + 5 * d, d**12 - d**8 - d**4 + 1 ] R, a, b, c, d = ring('a b c d', ZZ, grlex) I = [i.set_ring(R) for i in I] assert groebner(I, R) == [ 3 * b * c - c**2 + d**6 - 3 * d**2, -b + 3 * c**2 * d**3 - c - d**5 - 4 * d, -b + 3 * c * d**4 + 2 * c + 2 * d**5 + 2 * d, c**4 + 2 * c**2 * d**2 - d**4 - 2, c**3 * d + c * d**3 + d**4 + 1, b * c**2 - c**3 - c**2 * d - 2 * c * d**2 - d**3, b**2 - c**2, b * d + c**2 + c * d + d**2, a + b + c + d ]
def test_heugcd_univariate_integers(): R, x = ring("x", ZZ) f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8 g = x**3 + 6*x**2 + 11*x + 6 h = x**2 + 3*x + 2 cff = x**2 + 5*x + 4 cfg = x + 3 assert heugcd(f, g) == (h, cff, cfg) f = x**4 - 4 g = x**4 + 4*x**2 + 4 h = x**2 + 2 cff = x**2 - 2 cfg = x**2 + 2 assert heugcd(f, g) == (h, cff, cfg) f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 h = 1 cff = f cfg = g assert heugcd(f, g) == (h, cff, cfg) f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \ + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \ + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \ + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \ - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \ + 289127344604779611146960547954288113529690984687482920704*x**14 \ + 19007977035740498977629742919480623972236450681*x**7 \ + 311973482284542371301330321821976049 g = 365431878023781158602430064717380211405897160759702125019136*x**21 \ + 197599133478719444145775798221171663643171734081650688*x**14 \ - 9504116979659010018253915765478924103928886144*x**7 \ - 311973482284542371301330321821976049 # TODO: assert heugcd(f, f.diff(x))[0] == g f = 1317378933230047068160*x + 2945748836994210856960 g = 120352542776360960*x + 269116466014453760 h = 120352542776360960*x + 269116466014453760 cff = 10946 cfg = 1 assert heugcd(f, g) == (h, cff, cfg) with using(heu_gcd_max=0): pytest.raises(HeuristicGCDFailed, lambda: heugcd(f, g))
def test_sympyissue_16620(): R, x = ring('x', FF(2)) f = x**17 + 1 g = (1, [(x + 1, 1), (x**8 + x**5 + x**4 + x**3 + 1, 1), (x**8 + x**7 + x**6 + x**4 + x**2 + x + 1, 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = x**31 + 1 g = (1, [(x + 1, 1), (x**5 + x**2 + 1, 1), (x**5 + x**3 + 1, 1), (x**5 + x**3 + x**2 + x + 1, 1), (x**5 + x**4 + x**2 + x + 1, 1), (x**5 + x**4 + x**3 + x + 1, 1), (x**5 + x**4 + x**3 + x**2 + 1, 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g
def test_PolyElement_gcd(): R, x, y = ring("x,y", QQ) f = x**2/2 + x + QQ(1, 2) g = x/2 + QQ(1, 2) assert f.gcd(g) == x + 1 with using(use_heu_gcd=False): assert f.gcd(g) == x + 1 with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): assert f.gcd(g) == x + 1 R, x, y = ring("x,y", QQ.algebraic_field(sqrt(2))) f, g = (x + sqrt(2)*y)**2, x + sqrt(2)*y assert f.gcd(g) == g with using(gcd_aa_method='modgcd'): assert f.gcd(g) == g
def test_modgcd_algebraic_field_random(): A = QQ.algebraic_field(sqrt(2), sqrt(3)) R, x, y, z = ring('x, y, z', A) with using(gcd_aa_method='modgcd'): h = x**2 * y**3 + 1111 * sqrt(6) / 12 * z a, b = 11 * y**3 + 2, (y + x - 1) * h assert (h * a).cofactors(h * b) == (h, a, b) a, b = 12 * y + 2 * x - 1, (y + x - 1) * h assert (h * a).cofactors(h * b) == (h, a, b)
def test_gf_irreducible_p(): assert gf_irred_p_ben_or([7], 11, ZZ) is True assert gf_irred_p_ben_or([7, 3], 11, ZZ) is True assert gf_irred_p_ben_or([7, 3, 1], 11, ZZ) is False assert gf_irred_p_rabin([7], 11, ZZ) is True assert gf_irred_p_rabin([7, 3], 11, ZZ) is True assert gf_irred_p_rabin([7, 3, 1], 11, ZZ) is False assert gf_irred_p_ben_or([2, 3, 4, 5, 6], 13, ZZ) is False assert gf_irred_p_ben_or([2, 3, 4, 5, 8], 13, ZZ) is True with config.using(gf_irred_method='ben-or'): assert gf_irreducible_p([7], 11, ZZ) is True assert gf_irreducible_p([7, 3], 11, ZZ) is True assert gf_irreducible_p([7, 3, 1], 11, ZZ) is False with config.using(gf_irred_method='rabin'): assert gf_irreducible_p([7], 11, ZZ) is True assert gf_irreducible_p([7, 3], 11, ZZ) is True assert gf_irreducible_p([7, 3, 1], 11, ZZ) is False with config.using(gf_irred_method='other'): pytest.raises(KeyError, lambda: gf_irreducible_p([7], 11, ZZ)) f = [1, 9, 9, 13, 16, 15, 6, 7, 7, 7, 10] g = [1, 7, 16, 7, 15, 13, 13, 11, 16, 10, 9] h = gf_mul(f, g, 17, ZZ) assert gf_irred_p_ben_or(f, 17, ZZ) is True assert gf_irred_p_ben_or(g, 17, ZZ) is True assert gf_irred_p_ben_or(h, 17, ZZ) is False assert gf_irred_p_rabin(f, 17, ZZ) is True assert gf_irred_p_rabin(g, 17, ZZ) is True assert gf_irred_p_rabin(h, 17, ZZ) is False
def test_modgcd_bivariate_integers(): R, x, y = ring('x,y', ZZ) with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): f, g = R.zero, R.zero assert f.cofactors(g) == (0, 0, 0) f, g = 2 * x, R(2) assert f.cofactors(g) == (2, x, 1) f, g = x + 2 * y, x + y assert f.cofactors(g) == (1, f, g) f, g = x**2 + 2 * x * y + y**2, x**3 + y**3 assert f.cofactors(g) == (x + y, x + y, x**2 - x * y + y**2) f, g = x * y**2 + 2 * x * y + x, x * y**3 + x assert f.cofactors(g) == (x * y + x, y + 1, y**2 - y + 1) f, g = x**2 * y**2 + x**2 * y + 1, x * y**2 + x * y + 1 assert f.cofactors(g) == (1, f, g) f = 2 * x * y**2 + 4 * x * y + 2 * x + y**2 + 2 * y + 1 g = 2 * x * y**3 + 2 * x + y**3 + 1 assert f.cofactors(g) == (2 * x * y + 2 * x + y + 1, y + 1, y**2 - y + 1) f, g = 2 * x**2 + 4 * x + 2, x + 1 assert f.cofactors(g) == (x + 1, 2 * x + 2, 1) f, g = x + 1, 2 * x**2 + 4 * x + 2 assert f.cofactors(g) == (x + 1, 1, 2 * x + 2) f = 2 * x**2 + 4 * x * y - 2 * x - 4 * y g = x**2 + x - 2 assert f.cofactors(g) == (x - 1, 2 * x + 4 * y, x + 2) f = 2 * x**2 + 2 * x * y - 3 * x - 3 * y g = 4 * x * y - 2 * x + 4 * y**2 - 2 * y assert f.cofactors(g) == (x + y, 2 * x - 3, 4 * y - 2)
def test_benchmark_minimal_polynomial(method): with config.using(groebner=method): R, x, y, z = ring('x y z', QQ, lex) F = [x**3 + x + 1, y**2 + y + 1, (x + y) * z - (x**2 + y)] G = [ x + 155 * z**5 / 2067 - 355 * z**4 / 689 + 6062 * z**3 / 2067 - 3687 * z**2 / 689 + 6878 * z / 2067 - QQ(25, 53), y + 4 * z**5 / 53 - 91 * z**4 / 159 + 523 * z**3 / 159 - 387 * z**2 / 53 + 1043 * z / 159 - QQ(308, 159), z**6 - 7 * z**5 + 41 * z**4 - 82 * z**3 + 89 * z**2 - 46 * z + 13 ] assert groebner(F, R) == G
def test_dup_gf_irreducible_p(): R, x = ring('x', FF(11)) f = R.to_dense(R(7)) g = R.to_dense(7 * x + 3) h = R.to_dense(7 * x**2 + 3 * x + 1) for method in ('ben-or', 'rabin'): with using(gf_irred_method=method): assert dup_gf_irreducible_p(f, R.domain) is True assert dup_gf_irreducible_p(g, R.domain) is True assert dup_gf_irreducible_p(h, R.domain) is False with using(gf_irred_method='other'): pytest.raises(KeyError, lambda: dup_gf_irreducible_p(f, R.domain)) R, x = ring('x', FF(13)) f = R.to_dense(2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 6) g = R.to_dense(2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 8) with using(gf_irred_method='ben-or'): assert dup_gf_irreducible_p(f, R.domain) is False assert dup_gf_irreducible_p(g, R.domain) is True R, x = ring('x', FF(17)) f = (x**10 + 9 * x**9 + 9 * x**8 + 13 * x**7 + 16 * x**6 + 15 * x**5 + 6 * x**4 + 7 * x**3 + 7 * x**2 + 7 * x + 10) g = (x**10 + 7 * x**9 + 16 * x**8 + 7 * x**7 + 15 * x**6 + 13 * x**5 + 13 * x**4 + 11 * x**3 + 16 * x**2 + 10 * x + 9) h = f * g f, g, h = map(R.to_dense, (f, g, h)) for method in ('ben-or', 'rabin'): with using(gf_irred_method=method): assert dup_gf_irreducible_p(f, R.domain) is True assert dup_gf_irreducible_p(g, R.domain) is True assert dup_gf_irreducible_p(h, R.domain) is False F9 = FF(3, [1, 2, 2]) R, x = ring('x', F9) f = R.to_dense(x**3 + F9(8) * x**2 + F9(8) * x + F9(4)) for method in ('ben-or', 'rabin'): with using(gf_irred_method=method): assert dup_gf_irreducible_p(f, R.domain) is False F27 = FF(3, [1, 2, 0, 1]) R, x = ring('x', F27) f = R.to_dense(x**3 + F27(8) * x**2 + F27(19) * x + F27(24)) for method in ('ben-or', 'rabin'): with using(gf_irred_method=method): assert dup_gf_irreducible_p(f, R.domain) is True
def test_benchmark_katsura_4(method): with config.using(groebner=method): R, x0, x1, x2, x3 = ring('x:4', ZZ, lex) I = [ x0 + 2 * x1 + 2 * x2 + 2 * x3 - 1, x0**2 + 2 * x1**2 + 2 * x2**2 + 2 * x3**2 - x0, 2 * x0 * x1 + 2 * x1 * x2 + 2 * x2 * x3 - x1, x1**2 + 2 * x0 * x2 + 2 * x1 * x3 - x2 ] assert groebner(I, R) == [ 5913075 * x0 - 159690237696 * x3**7 + 31246269696 * x3**6 + 27439610544 * x3**5 - 6475723368 * x3**4 - 838935856 * x3**3 + 275119624 * x3**2 + 4884038 * x3 - 5913075, 1971025 * x1 - 97197721632 * x3**7 + 73975630752 * x3**6 - 12121915032 * x3**5 - 2760941496 * x3**4 + 814792828 * x3**3 - 1678512 * x3**2 - 9158924 * x3, 5913075 * x2 + 371438283744 * x3**7 - 237550027104 * x3**6 + 22645939824 * x3**5 + 11520686172 * x3**4 - 2024910556 * x3**3 - 132524276 * x3**2 + 30947828 * x3, 128304 * x3**8 - 93312 * x3**7 + 15552 * x3**6 + 3144 * x3**5 - 1120 * x3**4 + 36 * x3**3 + 15 * x3**2 - x3, ] R, x0, x1, x2, x3 = ring('x:4', ZZ, grlex) I = [i.set_ring(R) for i in I] assert groebner(I, R) == [ 393 * x1 - 4662 * x2**2 + 4462 * x2 * x3 - 59 * x2 + 224532 * x3**4 - 91224 * x3**3 - 678 * x3**2 + 2046 * x3, -x1 + 196 * x2**3 - 21 * x2**2 + 60 * x2 * x3 - 18 * x2 - 168 * x3**3 + 83 * x3**2 - 9 * x3, -6 * x1 + 1134 * x2**2 * x3 - 189 * x2**2 - 466 * x2 * x3 + 32 * x2 - 630 * x3**3 + 57 * x3**2 + 51 * x3, 33 * x1 + 63 * x2**2 + 2268 * x2 * x3**2 - 188 * x2 * x3 + 34 * x2 + 2520 * x3**3 - 849 * x3**2 + 3 * x3, 7 * x1**2 - x1 - 7 * x2**2 - 24 * x2 * x3 + 3 * x2 - 15 * x3**2 + 5 * x3, 14 * x1 * x2 - x1 + 14 * x2**2 + 18 * x2 * x3 - 4 * x2 + 6 * x3**2 - 2 * x3, 14 * x1 * x3 - x1 + 7 * x2**2 + 32 * x2 * x3 - 4 * x2 + 27 * x3**2 - 9 * x3, x0 + 2 * x1 + 2 * x2 + 2 * x3 - 1, ]
def test_dmp_ext_factor(method): with using(aa_factor_method=method): R, x = ring('x', QQ.algebraic_field(I)) assert R(0).factor_list() == (0, []) assert (x + 1).factor_list() == (1, [(x + 1, 1)]) assert (2 * x + 2).factor_list() == (2, [(x + 1, 1)]) assert (7 * x**4 + 1).factor_list() == (7, [(x**4 + QQ(1, 7), 1)]) assert (x**4 + 1).factor_list() == (1, [(x**2 - I, 1), (x**2 + I, 1)]) assert (4 * x**2 + 9).factor_list() == (4, [(x - 3 * I / 2, 1), (x + 3 * I / 2, 1)]) assert (4 * x**4 + 8 * x**3 + 77 * x**2 + 18 * x + 153).factor_list() == (4, [(x - 3 * I / 2, 1), (x + 1 + 4 * I, 1), (x + 1 - 4 * I, 1), (x + 3 * I / 2, 1)]) assert (x**2 + 1).factor_list() == (1, [(x - I, 1), (x + I, 1)]) R, x = ring('x', QQ.algebraic_field(sqrt(2))) assert (x**4 + 1).factor_list() == (1, [(x**2 - sqrt(2) * x + 1, 1), (x**2 + sqrt(2) * x + 1, 1)]) f = x**2 + 2 * sqrt(2) * x + 2 assert f.factor_list() == (1, [(x + sqrt(2), 2)]) assert (f**3).factor_list() == (1, [(x + sqrt(2), 6)]) f *= 2 assert f.factor_list() == (2, [(x + sqrt(2), 2)]) assert (f**3).factor_list() == (8, [(x + sqrt(2), 6)]) R, x, y = ring('x y', QQ.algebraic_field(sqrt(2))) assert R(0).factor_list() == (0, []) assert (x + 1).factor_list() == (1, [(x + 1, 1)]) assert (2 * x + 2).factor_list() == (2, [(x + 1, 1)]) assert (x**2 - 2 * y**2).factor_list() == (1, [(x - sqrt(2) * y, 1), (x + sqrt(2) * y, 1)]) assert (2 * x**2 - 4 * y**2).factor_list() == (2, [(x - sqrt(2) * y, 1), (x + sqrt(2) * y, 1)])
def test_benchmark_katsura_3(method): with config.using(groebner=method): R, x0, x1, x2 = ring('x:3', ZZ, lex) I = [ x0 + 2 * x1 + 2 * x2 - 1, x0**2 + 2 * x1**2 + 2 * x2**2 - x0, 2 * x0 * x1 + 2 * x1 * x2 - x1 ] assert groebner(I, R) == [ -7 + 7 * x0 + 8 * x2 + 158 * x2**2 - 420 * x2**3, 7 * x1 + 3 * x2 - 79 * x2**2 + 210 * x2**3, x2 + x2**2 - 40 * x2**3 + 84 * x2**4, ] R, x0, x1, x2 = ring('x:3', ZZ, grlex) I = [i.set_ring(R) for i in I] assert groebner(I, R) == [ 7 * x1 + 3 * x2 - 79 * x2**2 + 210 * x2**3, -x1 + x2 - 3 * x2**2 + 5 * x1**2, -x1 - 4 * x2 + 10 * x1 * x2 + 12 * x2**2, -1 + x0 + 2 * x1 + 2 * x2, ]
def test_dmp_gcd(): R, x = ring("x", ZZ) f, g = 0, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (0, 0, 0) f, g = 2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 0) f, g = -2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 0) f, g = 0, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 0, -1) f, g = 0, 2*x + 4 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 0, 1) f, g = 2*x + 4, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 1, 0) f, g = 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 1) f, g = -2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 1) f, g = 2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, -1) f, g = -2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, -1) f, g = x**2 + 2*x + 1, 1 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1) f, g = x**2 + 2*x + 1, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2) f, g = 2*x**2 + 4*x + 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1) f, g = 2, 2*x**2 + 4*x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1) f, g = 2*x**2 + 4*x + 2, x + 1 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1) f, g = x + 1, 2*x**2 + 4*x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2) f, g = x - 31, x assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, f, g) f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8 g = x**3 + 6*x**2 + 11*x + 6 h = x**2 + 3*x + 2 cff = x**2 + 5*x + 4 cfg = x + 3 assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg) f = x**4 - 4 g = x**4 + 4*x**2 + 4 h = x**2 + 2 cff = x**2 - 2 cfg = x**2 + 2 assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg) f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 h = 1 cff = f cfg = g assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg) R, x = ring("x", QQ) f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 h = 1 cff = f cfg = g assert R.dmp_qq_heu_gcd(f, g) == (h, cff, cfg) assert R.dup_ff_prs_gcd(f, g) == (h, cff, cfg) assert R.dup_ff_prs_gcd(R.zero, R.zero) == ([], [], []) R, x = ring("x", ZZ) f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \ + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \ + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \ + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \ - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \ + 289127344604779611146960547954288113529690984687482920704*x**14 \ + 19007977035740498977629742919480623972236450681*x**7 \ + 311973482284542371301330321821976049 g = 365431878023781158602430064717380211405897160759702125019136*x**21 \ + 197599133478719444145775798221171663643171734081650688*x**14 \ - 9504116979659010018253915765478924103928886144*x**7 \ - 311973482284542371301330321821976049 assert R.dmp_zz_heu_gcd(f, R.dmp_diff_in(f, 1, 0))[0] == g assert R.dup_rr_prs_gcd(f, R.dmp_diff_in(f, 1, 0))[0] == g R, x = ring("x", QQ) f = x**2/2 + x + QQ(1, 2) g = x/2 + QQ(1, 2) h = x + 1 assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dup_ff_prs_gcd(f, g) == (h, g, QQ(1, 2)) R, x = ring("x", ZZ) f = 1317378933230047068160*x + 2945748836994210856960 g = 120352542776360960*x + 269116466014453760 h = 120352542776360960*x + 269116466014453760 cff = 10946 cfg = 1 assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) with using(heu_gcd_max=0): pytest.raises(HeuristicGCDFailed, lambda: R.dmp_zz_heu_gcd(f, g)) R, x = ring("x", CC) f, g = (x**2 - 1, x**3 - 3*x + 2) assert R.dmp_inner_gcd(f, g) == (1, f, g) R, x, y = ring("x,y", CC) f, g = (x**2 - y, x**3 - y*x + 2) assert R.dmp_inner_gcd(f, g) == (1, f, g) R, x, y = ring("x,y", ZZ) f, g = 0, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (0, 0, 0) f, g = 2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 0) f, g = -2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 0) f, g = 0, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 0, -1) f, g = 0, 2*x + 4 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 0, 1) f, g = 2*x + 4, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 1, 0) f, g = 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 1) f, g = -2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 1) f, g = 2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, -1) f, g = -2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, -1) f, g = x**2 + 2*x + 1, 1 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1) f, g = x**2 + 2*x + 1, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2) with using(use_simplify_gcd=0): assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2) f, g = 2*x**2 + 4*x + 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1) f, g = 2, 2*x**2 + 4*x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1) f, g = 2*x**2 + 4*x + 2, x + 1 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1) f, g = x + 1, 2*x**2 + 4*x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2) with using(heu_gcd_max=0): pytest.raises(HeuristicGCDFailed, lambda: R.dmp_zz_heu_gcd(f, g)) f = x**2 + 2*x*y + y**2 g = x**2 + x*y assert R.dmp_rr_prs_gcd(f, g) == (x + y, x + y, x) R, x, y, z, u = ring("x,y,z,u", ZZ) f, g = u**2 + 2*u + 1, 2*u + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (u + 1, u + 1, 2) f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1 h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1 assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) assert R.dmp_rr_prs_gcd(f, g) == (h, cff, cfg) assert R.dmp_zz_heu_gcd(g, f) == (h, cfg, cff) assert R.dmp_rr_prs_gcd(g, f) == (h, cfg, cff) R, x, y, z = ring("x,y,z", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(2, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g H, cff, cfg = R.dmp_rr_prs_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v = ring("x,y,z,u,v", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(4, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(6, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(8, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z = ring("x,y,z", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_2(2, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g H, cff, cfg = R.dmp_rr_prs_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(2, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g H, cff, cfg = R.dmp_rr_prs_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v = ring("x,y,z,u,v", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(4, ZZ)) H, cff, cfg = R.dmp_inner_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y = ring("x,y", QQ) f = x**2/2 + x + QQ(1, 2) g = x/2 + QQ(1, 2) h = x + 1 assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1, 2)) with using(use_simplify_gcd=0): assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dmp_ff_prs_gcd(R.zero, R.zero) == (0, 0, 0) assert R.dmp_qq_heu_gcd(R.zero, R.zero) == (0, 0, 0) assert R.dmp_ff_prs_gcd(R.zero, g) == (x + 1, R.zero, QQ(1, 2)) assert R.dmp_qq_heu_gcd(R.zero, g) == (x + 1, R.zero, QQ(1, 2)) R, x, y = ring("x,y", RR) f = 2.1*x*y**2 - 2.2*x*y + 2.1*x g = 1.0*x**3 assert R.dmp_ff_prs_gcd(f, g) == \ (1.0*x, 2.1*y**2 - 2.2*y + 2.1, 1.0*x**2) R, x, y = ring("x,y", ZZ) f = (-17434367009167300000000000000000000000000000000000000000000000000000000*x**4*y - 250501827896299135568887342575961783764139560000000000000000000000000000000000000000000*x**3*y - 2440935909299672540738135183426056447877858000000000000000000000000000000*x**3 - 1349729941723537919695626818065131519270095220127010623905326719279566297660000000000000000000000000000*x**2*y - 26304033868956978374552886858060487282904504027042515077682955951658838800000000000000000*x**2 - 3232215785736369696036755035364398565076440134133908303058376297547504030528179314849416971379040931276000000000000000*x*y - 94485916261760032526508027937078714464844205539023800247528621905831259414691631156161537919255129011800*x - 2902585888465621357542575571971656665554321652262249362701116665830760628936600958940851960635161420991047110815678789984677193092993*y - 113133324167442997472440652189550843502029192913459268196939183295294085146407870078840385860571627108778756267503630290) g = (10000000000000000000000000000*x**2 + 71841388839807267676152024786000000000000000*x + 129029628760809605749020969023932901278290735413660734705971) assert (R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (g, -1743436700916730000000000000000000000000000*x**2*y - 12525091394814956778444367128798089188206978000000000000000*x*y - 244093590929967254073813518342605644787785800*x - 22495499028725631994927113634418779135935898997901327211111875586270479483*y - 876801128965234839118530545935732755107147297241756982389990, 1)) R, x = ring("x", ZZ) f, g = x**2 - 1, x**2 - 3*x + 2 assert R.dmp_gcd(f, g) == x - 1 with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): R.dmp_gcd(f, g) == x - 1 R, x = ring("x", QQ) f, g = x**2/2 + x + QQ(1, 2), x/2 + QQ(1, 2) assert R.dmp_gcd(f, g) == x + 1 with using(use_heu_gcd=False): R.dmp_gcd(f, g) == x + 1 R, x, y = ring("x,y", QQ.algebraic_field(sqrt(2))) f, g = (x + sqrt(2)*y)**2, x + sqrt(2)*y assert R.dmp_gcd(f, g) == g with using(gcd_aa_method='modgcd'): assert R.dmp_gcd(f, g) == g
def test_dmp_subresultants(): R, x = ring("x", ZZ) assert R.dmp_resultant(0, 0) == 0 assert R.dmp_resultant(1, 0) == 0 assert R.dmp_resultant(0, 1) == 0 f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 a = 15*x**4 - 3*x**2 + 9 b = 65*x**2 + 125*x - 245 c = 9326*x - 12300 d = 260708 assert R.dmp_subresultants(f, g) == [f, g, a, b, c, d] assert R.dmp_resultant(f, g) == R.dmp_LC(d) f = x**2 - 2*x + 1 g = x**2 - 1 a = 2*x - 2 assert R.dmp_subresultants(f, g) == [f, g, a] assert R.dmp_resultant(f, g) == 0 f = x**2 + 1 g = x**2 - 1 a = -2 assert R.dmp_subresultants(f, g) == [f, g, a] assert R.dmp_resultant(f, g) == 4 assert R.dmp_resultant(f, g, includePRS=True) == (4, [x**2 + 1, x**2 - 1, -2]) f = x**2 - 1 g = x**3 - x**2 + 2 assert R.dmp_resultant(f, g) == 0 f = 3*x**3 - x g = 5*x**2 + 1 assert R.dmp_resultant(f, g) == 64 f = x**2 - 2*x + 7 g = x**3 - x + 5 assert R.dmp_resultant(f, g) == 265 f = x**3 - 6*x**2 + 11*x - 6 g = x**3 - 15*x**2 + 74*x - 120 assert R.dmp_resultant(f, g) == -8640 f = x**3 - 6*x**2 + 11*x - 6 g = x**3 - 10*x**2 + 29*x - 20 assert R.dmp_resultant(f, g) == 0 f = x**3 - 1 g = x**3 + 2*x**2 + 2*x - 1 assert R.dmp_resultant(f, g) == 16 f = x**8 - 2 g = x - 1 assert R.dmp_resultant(f, g) == -1 assert R.dup_inner_subresultants(0, 0) == ([], []) assert R.dup_inner_subresultants(0, 1) == ([1], [1]) R, x, y = ring("x,y", ZZ) assert R.dmp_resultant(0, 0) == 0 assert R.dmp_prs_resultant(0, 0)[0] == 0 assert R.dmp_zz_collins_resultant(0, 0) == 0 assert R.dmp_qq_collins_resultant(0, 0) == 0 assert R.dmp_resultant(1, 0) == 0 assert R.dmp_resultant(1, 0) == 0 assert R.dmp_resultant(1, 0) == 0 assert R.dmp_resultant(0, 1) == 0 assert R.dmp_prs_resultant(0, 1)[0] == 0 assert R.dmp_zz_collins_resultant(0, 1) == 0 assert R.dmp_qq_collins_resultant(0, 1) == 0 assert R.dmp_inner_subresultants(0, 0) == ([], []) assert R.dmp_inner_subresultants(0, 1) == ([R.one], [[1]]) f = 3*x**2*y - y**3 - 4 g = x**2 + x*y**3 - 9 a = 3*x*y**4 + y**3 - 27*y + 4 b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16 r = R.dmp_LC(b) Y = R.drop(x).y rr = (-3*Y**10 - 12*Y**7 + Y**6 - 54*Y**4 + 8*Y**3 + 729*Y**2 - 216*Y + 16, [3*x**2*y - y**3 - 4, x**2 + x*y**3 - 9, 3*x*y**4 + y**3 - 27*y + 4, -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16]) assert R.dmp_subresultants(f, g) == [f, g, a, b] assert R.dmp_resultant(f, g) == r assert R.dmp_resultant(f, g, includePRS=True) == rr assert R.dmp_prs_resultant(f, g)[0] == r assert R.dmp_zz_collins_resultant(f, g) == r assert R.dmp_qq_collins_resultant(f, g) == r f = -x**3 + 5 g = 3*x**2*y + x**2 a = 45*y**2 + 30*y + 5 b = 675*y**3 + 675*y**2 + 225*y + 25 r = R.dmp_LC(b) assert R.dmp_subresultants(f, g) == [f, g, a] assert R.dmp_resultant(f, g) == r assert R.dmp_prs_resultant(f, g)[0] == r assert R.dmp_zz_collins_resultant(f, g) == r assert R.dmp_qq_collins_resultant(f, g) == r R, x, y, z, u, v = ring("x,y,z,u,v", ZZ) f = 6*x**2 - 3*x*y - 2*x*z + y*z g = x**2 - x*u - x*v + u*v r = y**2*z**2 - 3*y**2*z*u - 3*y**2*z*v + 9*y**2*u*v - 2*y*z**2*u \ - 2*y*z**2*v + 6*y*z*u**2 + 12*y*z*u*v + 6*y*z*v**2 - 18*y*u**2*v \ - 18*y*u*v**2 + 4*z**2*u*v - 12*z*u**2*v - 12*z*u*v**2 + 36*u**2*v**2 assert R.dmp_zz_collins_resultant(f, g) == r.drop(x) R, x, y, z, u, v = ring("x,y,z,u,v", QQ) f = x**2 - x*y/2 - x*z/3 + y*z/6 g = x**2 - x*u - x*v + u*v r = y**2*z**2/36 - y**2*z*u/12 - y**2*z*v/12 + y**2*u*v/4 \ - y*z**2*u/18 - y*z**2*v/18 + y*z*u**2/6 + y*z*u*v/3 \ + y*z*v**2/6 - y*u**2*v/2 - y*u*v**2/2 + z**2*u*v/9 \ - z*u**2*v/3 - z*u*v**2/3 + u**2*v**2 assert R.dmp_qq_collins_resultant(f, g) == r.drop(x) Rt, t = ring("t", ZZ) Rx, x = ring("x", Rt) f = x**6 - 5*x**4 + 5*x**2 + 4 g = -6*t*x**5 + x**4 + 20*t*x**3 - 3*x**2 - 10*t*x + 6 assert Rx.dmp_resultant(f, g) == 2930944*t**6 + 2198208*t**4 + 549552*t**2 + 45796 assert Rx.dmp_prs_resultant(x - 1, x + 1) == (2, [x - 1, x + 1, 2]) R, x, y = ring("x,y", ZZ) f = x + y g = x**2 - x*y + 1 assert R.dmp_resultant(f, g) == (1 + 2*y**2).drop(x) g += 1 with using(use_collins_resultant=True): assert R.dmp_resultant(f, g) == (2 + 2*y**2).drop(x) R, x, y = ring("x,y", QQ) f = x + y g = x**2 - x*y + 1 with using(use_collins_resultant=True): assert R.dmp_resultant(f, g) == (1 + 2*y**2).drop(x) R, x, y = ring("x y", ZZ) f = x + y + 2 g = 2*x*y + x + 3 assert R.dmp_zz_collins_resultant(f, g) == (-2*y**2 - 5*y + 1).drop(x)
def test_modgcd_algebraic_field(): A = QQ.algebraic_field(sqrt(2)) R, x = ring('x', A) with using(gcd_aa_method='modgcd'): f, g = 2 * x, R(2) assert f.cofactors(g) == (1, f, g) f, g = 2 * x, R(sqrt(2)) assert f.cofactors(g) == (1, f, g) f, g = 2 * x + 2, 6 * x**2 - 6 assert f.cofactors(g) == (x + 1, 2, 6 * x - 6) R, x, y = ring('x, y', A) with using(gcd_aa_method='modgcd'): f, g = x + sqrt(2) * y, x + y assert f.cofactors(g) == (1, f, g) f, g = x * y + sqrt(2) * y**2, sqrt(2) * y assert f.cofactors(g) == (y, x + sqrt(2) * y, sqrt(2)) f, g = x**2 + 2 * sqrt(2) * x * y + 2 * y**2, x + sqrt(2) * y assert f.cofactors(g) == (g, g, 1) A = QQ.algebraic_field(sqrt(2), sqrt(3)) R, x, y, z = ring('x, y, z', A) with using(gcd_aa_method='modgcd'): h = x**2 * y**7 + sqrt(6) / 21 * z f, g = h * (27 * y**3 + 1), h * (y + x) assert f.cofactors(g) == (h, 27 * y**3 + 1, x + y) h = x**13 * y**3 + x**10 / 2 + 1 / sqrt(2) f, g = h * (x + 1), h * sqrt(2) / sqrt(3) assert f.cofactors(g) == (h, x + 1, sqrt(2) / sqrt(3)) h = x**4 * y**9 + sqrt(6) / 22 * z f, g = h * (21 * y**3 + 1), h * (y + x) assert f.cofactors(g) == (x**4 * y**9 + sqrt(6) / 22 * z, 21 * y**3 + 1, x + y) h = x**4 * y**3 + sqrt(6) / 22 * z f, g = h * (11 * y**3 + 1), h * (y + x) assert f.cofactors(g) == (x**4 * y**3 + sqrt(6) / 22 * z, 11 * y**3 + 1, x + y) A = QQ.algebraic_field(sqrt(2)**(-1) * sqrt(3)) R, x = ring('x', A) with using(gcd_aa_method='modgcd'): f, g = x + 1, x - 1 assert f.cofactors(g) == (1, f, g)
def test_modgcd_multivariate_integers(): R, x, y = ring('x,y', ZZ) with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): f, g = R.zero, R.zero assert f.cofactors(g) == (0, 0, 0) f, g = 2 * x**2 + 4 * x + 2, x + 1 assert f.cofactors(g) == (x + 1, 2 * x + 2, 1) f, g = x + 1, 2 * x**2 + 4 * x + 2 assert f.cofactors(g) == (x + 1, 1, 2 * x + 2) f = 2 * x**2 + 2 * x * y - 3 * x - 3 * y g = 4 * x * y - 2 * x + 4 * y**2 - 2 * y assert f.cofactors(g) == (x + y, 2 * x - 3, 4 * y - 2) f, g = x * y**2 + 2 * x * y + x, x * y**3 + x assert f.cofactors(g) == (x * y + x, y + 1, y**2 - y + 1) f, g = x**2 * y**2 + x**2 * y + 1, x * y**2 + x * y + 1 assert f.cofactors(g) == (1, f, g) f = x**4 + 8 * x**3 + 21 * x**2 + 22 * x + 8 g = x**3 + 6 * x**2 + 11 * x + 6 assert f.cofactors(g) == (x**2 + 3 * x + 2, x**2 + 5 * x + 4, x + 3) R, x, y, z, u = ring('x,y,z,u', ZZ) with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): f, g = x + y + z, -x - y - z - u assert f.cofactors(g) == (1, f, g) f, g = u**2 + 2 * u + 1, 2 * u + 2 assert f.cofactors(g) == (u + 1, u + 1, 2) f, g = z**2 * u**2 + 2 * z**2 * u + z**2 + z * u + z, u**2 + 2 * u + 1 h, cff, cfg = u + 1, z**2 * u + z**2 + z, u + 1 assert f.cofactors(g) == (h, cff, cfg) assert g.cofactors(f) == (h, cfg, cff) f, g, h = (1199999999999991 * x**17 - y, 2 * y - 19989798798 + x**211, 12 * x * y**7 + x**4 - 1) for i in range(10): assert (f * h).cofactors(g * h) == (h, f, g) R, x, y, z = ring('x,y,z', ZZ) with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): f, g = x - y * z, x - y * z assert f.cofactors(g) == (x - y * z, 1, 1) f, g, h = R.fateman_poly_F_1() H, cff, cfg = f.cofactors(g) assert H == h and H * cff == f and H * cfg == g f, g, h = R.fateman_poly_F_2() H, cff, cfg = f.cofactors(g) assert H == h and H * cff == f and H * cfg == g f, g, h = R.fateman_poly_F_3() H, cff, cfg = f.cofactors(g) assert H == h and H * cff == f and H * cfg == g R, x, y, z, u, v = ring('x,y,z,u,v', ZZ) with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): f, g, h = R.fateman_poly_F_1() H, cff, cfg = f.cofactors(g) assert H == h and H * cff == f and H * cfg == g R, x, y, z, u, v, a, b = ring('x,y,z,u,v,a,b', ZZ) with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): f, g, h = R.fateman_poly_F_1() H, cff, cfg = f.cofactors(g) assert H == h and H * cff == f and H * cfg == g R, x, y, z, u, v, a, b, c, d = ring('x,y,z,u,v,a,b,c,d', ZZ) with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): f, g, h = R.fateman_poly_F_1() H, cff, cfg = f.cofactors(g) assert H == h and H * cff == f and H * cfg == g R, x, y, z, t = ring('x,y,z,t', ZZ) with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): f, g, h = R.fateman_poly_F_3() H, cff, cfg = f.cofactors(g) assert H == h and H * cff == f and H * cfg == g
def test_benchmark_cyclic_4_f5b(): with config.using(groebner='f5b'): _do_test_benchmark_cyclic_4()
def test_groebner_buchberger(): with config.using(groebner='buchberger'): _do_test_groebner()
def test__zz_wang(): R, x, y, z = ring('x y z', ZZ) UV, _x = ring('x', ZZ) p = ZZ(nextprime(R._zz_mignotte_bound(w_1))) assert p == 6291469 t_1, k_1, e_1 = y, 1, ZZ(-14) t_2, k_2, e_2 = z, 2, ZZ(3) t_3, k_3, e_3 = y + z, 2, ZZ(-11) t_4, k_4, e_4 = y - z, 1, ZZ(-17) T = [t_1, t_2, t_3, t_4] K = [k_1, k_2, k_3, k_4] E = [e_1, e_2, e_3, e_4] T = list(zip([t.drop(x) for t in T], K)) A = [ZZ(-14), ZZ(3)] S = w_1.eval([(y, A[0]), (z, A[1])]) cs, s = S.primitive() assert cs == 1 and s == S == (1036728 * _x**6 + 915552 * _x**5 + 55748 * _x**4 + 105621 * _x**3 - 17304 * _x**2 - 26841 * _x - 644) assert R._zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17] assert s.is_squarefree and s.degree() == w_1.degree() _, H = UV._zz_factor_sqf(s) h_1 = 187 * _x**2 - 23 h_2 = 44 * _x**2 + 42 * _x + 1 h_3 = 126 * _x**2 - 9 * _x + 28 LC = [lc.drop(x) for lc in [y**2 - z**2, -4 * y - 4 * z, -y * z**2]] factors = R._zz_wang_hensel_lifting(w_1, H, LC, A, p) assert H == [h_1, h_2, h_3] assert R._zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC) assert functools.reduce(operator.mul, factors) == w_1 # coverage tests f = x**6 + 5 * x**4 * y - 5 * x**2 * y**2 - y**3 assert R._zz_wang(f, mod=4, seed=1) == [x**2 - y, x**4 + 6 * x**2 * y + y**2] # This tests a bug in the Wang algorithm that occured only with a very # specific set of random numbers; issue sympy/sympy#6355. random_sequence = [ -1, -1, 0, 0, 0, 0, -1, -1, 0, -1, 3, -1, 3, 3, 3, 3, -1, 3 ] R, x, y, z = ring('x y z', ZZ) f = 2 * x**2 + y * z - y - z**2 + z assert R._zz_wang(f, seed=random_sequence) == [f] with using(eez_restart_if_needed=False): pytest.raises(ExtraneousFactors, lambda: R._zz_wang(f, seed=random_sequence))
def test_PolyElement_is_irreducible(): R, x = ring('x', FF(5)) f = (x**10 + 4 * x**9 + 2 * x**8 + 2 * x**7 + 3 * x**6 + 2 * x**5 + 4 * x**4 + x**3 + 4 * x**2 + 4) g = 3 * x**2 + 2 * x + 4 for method in ('ben-or', 'rabin'): with using(gf_irred_method=method): assert f.is_irreducible is True assert g.is_irreducible is False R, x = ring('x', FF(11)) f = R(7) g = 7 * x + 3 h = 7 * x**2 + 3 * x + 1 for method in ('ben-or', 'rabin'): with using(gf_irred_method=method): assert f.is_irreducible is True assert g.is_irreducible is True assert h.is_irreducible is False with using(gf_irred_method='other'): pytest.raises(KeyError, lambda: f.is_irreducible) R, x = ring('x', FF(13)) f = 2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 6 g = 2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 8 with using(gf_irred_method='ben-or'): assert f.is_irreducible is False assert g.is_irreducible is True R, x = ring('x', FF(17)) f = (x**10 + 9 * x**9 + 9 * x**8 + 13 * x**7 + 16 * x**6 + 15 * x**5 + 6 * x**4 + 7 * x**3 + 7 * x**2 + 7 * x + 10) g = (x**10 + 7 * x**9 + 16 * x**8 + 7 * x**7 + 15 * x**6 + 13 * x**5 + 13 * x**4 + 11 * x**3 + 16 * x**2 + 10 * x + 9) h = f * g for method in ('ben-or', 'rabin'): with using(gf_irred_method=method): assert f.is_irreducible is True assert g.is_irreducible is True assert h.is_irreducible is False F9 = FF(3, [2, 2, 1]) R, x = ring('x', F9) f = x**3 + F9(8) * x**2 + F9(8) * x + F9(4) for method in ('ben-or', 'rabin'): with using(gf_irred_method=method): assert f.is_irreducible is False F27 = FF(3, [1, 0, 2, 1]) R, x = ring('x', F27) f = x**3 + F27(8) * x**2 + F27(19) * x + F27(24) for method in ('ben-or', 'rabin'): with using(gf_irred_method=method): assert f.is_irreducible is True R, x = ring('x', ZZ) assert x.is_irreducible is True assert (x**2 + x + 1).is_irreducible is True assert (x**2 + 2 * x + 1).is_irreducible is False assert (x**2 - 1).is_irreducible is False f = 3 * x**4 + 2 * x**3 + 6 * x**2 + 8 * x assert (f + 7).is_irreducible is True assert (f + 4).is_irreducible is True assert (f + 10).is_irreducible is True assert (f + 14).is_irreducible is True R, x, y = ring('x y', ZZ) assert R(2).is_irreducible is True assert (x**2 + x + 1).is_irreducible is True assert (x**2 + 2 * x + 1).is_irreducible is False assert ((x - 2 * y) * (x + y)).is_irreducible is False assert (x**2 + y**2).is_irreducible is True R, x, y, z = ring('x y z', QQ) assert (x**2 + x + 1).is_irreducible assert (x**2 + 2 * x + 1).is_irreducible is False
def test_factor_list(): R, x = ring('x', FF(2)) assert (x**2 + 1).factor_list() == (1, [(x + 1, 2)]) R, x = ring('x', ZZ) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) assert (x**2 + 2 * x + 1).factor_list() == (1, [(x + 1, 2)]) # issue sympy/sympy#8037 assert (6 * x**2 - 5 * x - 6).factor_list() == (1, [(2 * x - 3, 1), (3 * x + 2, 1)]) R, x = ring('x', QQ) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)]) R, x = ring('x', QQ.algebraic_field(I)) f = x**4 + 2 * x**2 assert f.factor_list() == (1, [(x, 2), (x**2 + 2, 1)]) R, x = ring('x', RR) assert (1.0 * x**2 + 2.0 * x + 1.0).factor_list() == (1.0, [(1.0 * x + 1.0, 2)]) assert (2.0 * x**2 + 4.0 * x + 2.0).factor_list() == (2.0, [(1.0 * x + 1.0, 2)]) f = 6.7225336055071 * x**2 - 10.6463972754741 * x - 0.33469524022264 assert f.factor_list() == (1.0, [(f, 1)]) # issue diofant/diofant#238 f = 0.1 * x**2 + 1.1 * x + 1.0 assert f.factor_list() == (10.0, [(0.1 * x + 0.1, 1), (0.1 * x + 1.0, 1)]) f = 0.25 + 1.0 * x + 1.0 * x**2 assert f.factor_list() == (4.0, [(0.25 + 0.5 * x, 2)]) Rt, t = ring('t', ZZ) R, x = ring('x', Rt) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) assert (4 * t * x**2 + 4 * t**2 * x).factor_list() == (4 * t, [(x, 1), (x + t, 1)]) Rt, t = ring('t', QQ) R, x = ring('x', Rt) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) assert (t * x**2 / 2 + t**2 * x / 2).factor_list() == (t / 2, [(x, 1), (x + t, 1)]) R, x = ring('x', EX) pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list()) R, x, y = ring('x y', FF(2)) pytest.raises(NotImplementedError, lambda: (x**2 + y**2).factor_list()) R, x, y = ring('x y', ZZ) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) assert (x**2 + 2 * x + 1).factor_list() == (1, [(x + 1, 2)]) assert (4 * x**2 * y + 4 * x * y**2).factor_list() == (4, [(y, 1), (x, 1), (x + y, 1)]) R, x, y = ring('x y', QQ) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)]) assert (x**2 * y / 2 + x * y**2 / 2).factor_list() == (QQ(1, 2), [(y, 1), (x, 1), (x + y, 1)]) R, x, y = ring('x y', QQ.algebraic_field(I)) f, r = x**2 + y**2, (1, [(x - I * y, 1), (x + I * y, 1)]) for method in ('trager', 'modular'): with using(aa_factor_method=method): assert f.factor_list() == r R, x, y = ring('x y', RR) f = 2.0 * x**2 - 8.0 * y**2 assert f.factor_list() == (2.0, [(1.0 * x - 2.0 * y, 1), (1.0 * x + 2.0 * y, 1)]) f = 6.7225336055071 * x**2 * y**2 - 10.6463972754741 * x * y - 0.33469524022264 assert f.factor_list() == (1.0, [(f, 1)]) Rt, t = ring('t', ZZ) R, x, y = ring('x y', Rt) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) assert (4 * t * x**2 + 4 * t**2 * x).factor_list() == (4 * t, [(x, 1), (x + t, 1)]) Rt, t = ring('t', QQ) R, x, y = ring('x y', Rt) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) assert (t * x**2 / 2 + t**2 * x / 2).factor_list() == (t / 2, [(x, 1), (x + t, 1)]) R, x, y = ring('x y', EX) pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list()) # issue diofant/diofant#238 R, x, y, z = ring('x y z', RR) f = x * y + x * z + 0.1 * y + 0.1 * z assert f.factor_list() == (10.0, [(x + 0.1, 1), (0.1 * y + 0.1 * z, 1)]) f = 0.25 * x**2 + 1.0 * x * y * z + 1.0 * y**2 * z**2 assert f.factor_list() == (4.0, [(0.25 * x + 0.5 * y * z, 2)]) R, *X = ring('x:200', ZZ) f, g = X[0]**2 + 2 * X[0] + 1, X[0] + 1 assert f.factor_list() == (1, [(g, 2)]) f, g = X[-1]**2 + 2 * X[-1] + 1, X[-1] + 1 assert f.factor_list() == (1, [(g, 2)])
def test_dup_zz_factor(): R, x = ring("x", ZZ) assert R.dup_zz_factor(0) == (0, []) assert R.dup_zz_factor(7) == (7, []) assert R.dup_zz_factor(-7) == (-7, []) assert R.dup_zz_factor_sqf(0) == (0, []) assert R.dup_zz_factor_sqf(7) == (7, []) assert R.dup_zz_factor_sqf(-7) == (-7, []) assert R.dup_zz_factor(2*x + 4) == (2, [(x + 2, 1)]) assert R.dup_zz_factor_sqf(2*x + 4) == (2, [x + 2]) f = x**4 + x + 1 for i in range(20): assert R.dup_zz_factor(f) == (1, [(f, 1)]) assert R.dup_zz_factor(x**2 + 2*x + 2) == (1, [(x**2 + 2*x + 2, 1)]) with config.using(use_irreducible_in_factor=True): assert R.dup_zz_factor(x**2 + 2*x + 2) == (1, [(x**2 + 2*x + 2, 1)]) assert R.dup_zz_factor(18*x**2 + 12*x + 2) == (2, [(3*x + 1, 2)]) with config.using(use_irreducible_in_factor=True): assert R.dup_zz_factor(18*x**2 + 12*x + 2) == (2, [(3*x + 1, 2)]) assert R.dup_zz_factor(-9*x**2 + 1) == \ (-1, [(3*x - 1, 1), (3*x + 1, 1)]) with config.using(use_irreducible_in_factor=True): assert R.dup_zz_factor_sqf(3*x**4 + 2*x**3 + 6*x**2 + 8*x + 10) == (1, [3*x**4 + 2*x**3 + 6*x**2 + 8*x + 10]) assert R.dup_zz_factor_sqf(-9*x**2 + 1) == (-1, [3*x - 1, 3*x + 1]) with config.using(use_irreducible_in_factor=True): assert R.dup_zz_factor_sqf(-9*x**2 + 1) == (-1, [3*x - 1, 3*x + 1]) with config.using(use_cyclotomic_factor=False): assert R.dup_zz_factor_sqf(-9*x**2 + 1) == (-1, [3*x - 1, 3*x + 1]) assert R.dup_zz_factor(x**3 - 6*x**2 + 11*x - 6) == \ (1, [(x - 3, 1), (x - 2, 1), (x - 1, 1)]) assert R.dup_zz_factor_sqf(x**3 - 6*x**2 + 11*x - 6) == \ (1, [x - 3, x - 2, x - 1]) assert R.dup_zz_factor(3*x**3 + 10*x**2 + 13*x + 10) == \ (1, [(x + 2, 1), (3*x**2 + 4*x + 5, 1)]) assert R.dup_zz_factor_sqf(3*x**3 + 10*x**2 + 13*x + 10) == \ (1, [x + 2, 3*x**2 + 4*x + 5]) assert R.dup_zz_factor(-x**6 + x**2) == \ (-1, [(x - 1, 1), (x + 1, 1), (x, 2), (x**2 + 1, 1)]) f = 1080*x**8 + 5184*x**7 + 2099*x**6 + 744*x**5 + 2736*x**4 - 648*x**3 + 129*x**2 - 324 assert R.dup_zz_factor(f) == \ (1, [(5*x**4 + 24*x**3 + 9*x**2 + 12, 1), (216*x**4 + 31*x**2 - 27, 1)]) f = -29802322387695312500000000000000000000*x**25 \ + 2980232238769531250000000000000000*x**20 \ + 1743435859680175781250000000000*x**15 \ + 114142894744873046875000000*x**10 \ - 210106372833251953125*x**5 \ + 95367431640625 assert R.dup_zz_factor(f) == \ (-95367431640625, [(5*x - 1, 1), (100*x**2 + 10*x - 1, 2), (625*x**4 + 125*x**3 + 25*x**2 + 5*x + 1, 1), (10000*x**4 - 3000*x**3 + 400*x**2 - 20*x + 1, 2), (10000*x**4 + 2000*x**3 + 400*x**2 + 30*x + 1, 2)]) f = x**10 - 1 config.setup('USE_CYCLOTOMIC_FACTOR', True) F_0 = R.dup_zz_factor(f) config.setup('USE_CYCLOTOMIC_FACTOR', False) F_1 = R.dup_zz_factor(f) assert F_0 == F_1 == \ (1, [(x - 1, 1), (x + 1, 1), (x**4 - x**3 + x**2 - x + 1, 1), (x**4 + x**3 + x**2 + x + 1, 1)]) config.setup('USE_CYCLOTOMIC_FACTOR') f = x**10 + 1 config.setup('USE_CYCLOTOMIC_FACTOR', True) F_0 = R.dup_zz_factor(f) config.setup('USE_CYCLOTOMIC_FACTOR', False) F_1 = R.dup_zz_factor(f) assert F_0 == F_1 == \ (1, [(x**2 + 1, 1), (x**8 - x**6 + x**4 - x**2 + 1, 1)]) config.setup('USE_CYCLOTOMIC_FACTOR')
def test_benchmark_minpoly_f5b(): with config.using(groebner='f5b'): _do_test_benchmark_minpoly()
def test_benchmark_minpoly_buchberger(): with config.using(groebner='buchberger'): _do_test_benchmark_minpoly()
def test_groebner_f5b(): with config.using(groebner='f5b'): _do_test_groebner()
def test_modgcd_univariate_integers(): R, x = ring('x', ZZ) with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): f, g = R.zero, R.zero assert f.cofactors(g) == (0, 0, 0) f, g = R.zero, x assert f.cofactors(g) == (x, 0, 1) assert g.cofactors(f) == (x, 1, 0) f, g = R.zero, -x assert f.cofactors(g) == (x, 0, -1) assert g.cofactors(f) == (x, -1, 0) f, g = 2 * x, R(2) assert f.cofactors(g) == (2, x, 1) f, g = 2 * x + 2, 6 * x**2 - 6 assert f.cofactors(g) == (2 * x + 2, 1, 3 * x - 3) f, g = [1000000000000 * x + 998549000000] * 2 assert f.cofactors(g) == (f, 1, 1) f, g = 999530000000 * x + 1000000000000, 999530000000 * x + 999999000000 assert f.cofactors(g) == (1000000, 999530 * x + 1000000, 999530 * x + 999999) f = x**4 + 8 * x**3 + 21 * x**2 + 22 * x + 8 g = x**3 + 6 * x**2 + 11 * x + 6 assert f.cofactors(g) == (x**2 + 3 * x + 2, x**2 + 5 * x + 4, x + 3) f = x**4 - 4 g = x**4 + 4 * x**2 + 4 assert f.cofactors(g) == (x**2 + 2, x**2 - 2, x**2 + 2) f = x**8 + x**6 - 3 * x**4 - 3 * x**3 + 8 * x**2 + 2 * x - 5 g = 3 * x**6 + 5 * x**4 - 4 * x**2 - 9 * x + 21 assert f.cofactors(g) == (1, f, g) f = ( -352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272 * x**49 + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952 * x**42 + 378182690892293941192071663536490788434899030680411695933646320291525827756032 * x**35 + 112806468807371824947796775491032386836656074179286744191026149539708928 * x**28 - 12278371209708240950316872681744825481125965781519138077173235712 * x**21 + 289127344604779611146960547954288113529690984687482920704 * x**14 + 19007977035740498977629742919480623972236450681 * x**7 + 311973482284542371301330321821976049) g = (365431878023781158602430064717380211405897160759702125019136 * x**21 + 197599133478719444145775798221171663643171734081650688 * x**14 - 9504116979659010018253915765478924103928886144 * x**7 - 311973482284542371301330321821976049) assert f.gcd(f.diff(x)) == g f = 1317378933230047068160 * x + 2945748836994210856960 g = 120352542776360960 * x + 269116466014453760 assert f.cofactors(g) == (120352542776360960 * x + 269116466014453760, 10946, 1)
def test_dup_zz_factor(): R, x = ring('x', ZZ) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) assert R(-7).factor_list() == (-7, []) assert R._zz_factor_sqf(R(0)) == (0, []) assert R._zz_factor_sqf(R(7)) == (7, []) assert R._zz_factor_sqf(R(-7)) == (-7, []) assert (2 * x + 4).factor_list() == (2, [(x + 2, 1)]) assert R._zz_factor_sqf(2 * x + 4) == (2, [x + 2]) f = x**4 + x + 1 for i in range(20): assert f.factor_list() == (1, [(f, 1)]) f = x**5 - x**3 - x**2 + 1 assert f.factor_list() == (1, [(x + 1, 1), (x - 1, 2), (x**2 + x + 1, 1)]) for test in (True, False): with using(use_irreducible_in_factor=test): assert (x**2 + 2 * x + 2).factor_list() == (1, [(x**2 + 2 * x + 2, 1)]) assert (18 * x**2 + 12 * x + 2).factor_list() == (2, [(3 * x + 1, 2)]) f = -9 * x**2 + 1 assert R._zz_factor_sqf(f) == (-1, [3 * x - 1, 3 * x + 1]) assert f.factor_list() == (-1, [(3 * x - 1, 1), (3 * x + 1, 1)]) assert R._zz_factor_sqf(3 * x**4 + 2 * x**3 + 6 * x**2 + 8 * x + 10) == (1, [ 3 * x**4 + 2 * x**3 + 6 * x**2 + 8 * x + 10 ]) with using(use_cyclotomic_factor=False): assert R._zz_factor_sqf(-9 * x**2 + 1) == (-1, [3 * x - 1, 3 * x + 1]) assert (x**3 - 6 * x**2 + 11 * x - 6).factor_list() == (1, [(x - 3, 1), (x - 2, 1), (x - 1, 1)]) assert R._zz_factor_sqf(x**3 - 6 * x**2 + 11 * x - 6) == (1, [x - 3, x - 2, x - 1]) assert (3 * x**3 + 10 * x**2 + 13 * x + 10).factor_list() == (1, [ (x + 2, 1), (3 * x**2 + 4 * x + 5, 1) ]) assert R._zz_factor_sqf(3 * x**3 + 10 * x**2 + 13 * x + 10) == (1, [x + 2, 3 * x**2 + 4 * x + 5]) assert (-x**6 + x**2).factor_list() == (-1, [(x, 2), (x - 1, 1), (x + 1, 1), (x**2 + 1, 1)]) f = (1080 * x**8 + 5184 * x**7 + 2099 * x**6 + 744 * x**5 + 2736 * x**4 - 648 * x**3 + 129 * x**2 - 324) assert f.factor_list() == (1, [(216 * x**4 + 31 * x**2 - 27, 1), (5 * x**4 + 24 * x**3 + 9 * x**2 + 12, 1)]) f = (-29802322387695312500000000000000000000 * x**25 + 2980232238769531250000000000000000 * x**20 + 1743435859680175781250000000000 * x**15 + 114142894744873046875000000 * x**10 - 210106372833251953125 * x**5 + +95367431640625) assert (f.factor_list() == (-95367431640625, [ (5 * x - 1, 1), (100 * x**2 + 10 * x - 1, 2), (625 * x**4 + 125 * x**3 + 25 * x**2 + 5 * x + 1, 1), (10000 * x**4 - 3000 * x**3 + 400 * x**2 - 20 * x + 1, 2), (10000 * x**4 + 2000 * x**3 + 400 * x**2 + 30 * x + 1, 2) ])) f = x**10 - 1 for test in (True, False): with using(use_cyclotomic_factor=test): f = x**10 - 1 assert f.factor_list() == (1, [(x - 1, 1), (x + 1, 1), (x**4 - x**3 + x**2 - x + 1, 1), (x**4 + x**3 + x**2 + x + 1, 1)]) f = x**10 + 1 assert f.factor_list() == (1, [(x**2 + 1, 1), (x**8 - x**6 + x**4 - x**2 + 1, 1)])
def test_benchmark_cyclic_4_buchberger(): with config.using(groebner='buchberger'): _do_test_benchmark_cyclic_4()
def test_benchmark_kastura_4_f5b(): with config.using(groebner='f5b'): _do_test_benchmark_katsura_4()
def test_dmp_gcd(): R, x, y = ring("x,y", ZZ) f, g = 0, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (0, 0, 0) f, g = 2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 0) f, g = -2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 0) f, g = 0, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 0, -1) f, g = 0, 2 * x + 4 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2 * x + 4, 0, 1) f, g = 2 * x + 4, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2 * x + 4, 1, 0) f, g = 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 1) f, g = -2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 1) f, g = 2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, -1) f, g = -2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, -1) f, g = x**2 + 2 * x + 1, 1 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd( f, g) == (1, x**2 + 2 * x + 1, 1) f, g = x**2 + 2 * x + 1, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd( f, g) == (1, x**2 + 2 * x + 1, 2) with using(use_simplify_gcd=0): assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd( f, g) == (1, x**2 + 2 * x + 1, 2) f, g = 2 * x**2 + 4 * x + 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd( f, g) == (2, x**2 + 2 * x + 1, 1) f, g = 2, 2 * x**2 + 4 * x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd( f, g) == (2, 1, x**2 + 2 * x + 1) f, g = 2 * x**2 + 4 * x + 2, x + 1 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd( f, g) == (x + 1, 2 * x + 2, 1) f, g = x + 1, 2 * x**2 + 4 * x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd( f, g) == (x + 1, 1, 2 * x + 2) with using(heu_gcd_max=0): pytest.raises(HeuristicGCDFailed, lambda: R.dmp_zz_heu_gcd(f, g)) R, x, y, z, u = ring("x,y,z,u", ZZ) f, g = u**2 + 2 * u + 1, 2 * u + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (u + 1, u + 1, 2) f, g = z**2 * u**2 + 2 * z**2 * u + z**2 + z * u + z, u**2 + 2 * u + 1 h, cff, cfg = u + 1, z**2 * u + z**2 + z, u + 1 assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) assert R.dmp_rr_prs_gcd(f, g) == (h, cff, cfg) assert R.dmp_zz_heu_gcd(g, f) == (h, cfg, cff) assert R.dmp_rr_prs_gcd(g, f) == (h, cfg, cff) R, x, y, z = ring("x,y,z", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(2, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g H, cff, cfg = R.dmp_rr_prs_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v = ring("x,y,z,u,v", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(4, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(6, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(8, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z = ring("x,y,z", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_2(2, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g H, cff, cfg = R.dmp_rr_prs_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(2, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g H, cff, cfg = R.dmp_rr_prs_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v = ring("x,y,z,u,v", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(4, ZZ)) H, cff, cfg = R.dmp_inner_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y = ring("x,y", QQ) f = x**2 / 2 + x + QQ(1, 2) g = x / 2 + QQ(1, 2) h = x + 1 assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1, 2)) with using(use_simplify_gcd=0): assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dmp_ff_prs_gcd(R.zero, R.zero) == (0, 0, 0) assert R.dmp_qq_heu_gcd(R.zero, R.zero) == (0, 0, 0) assert R.dmp_ff_prs_gcd(R.zero, g) == (x + 1, R.zero, QQ(1, 2)) assert R.dmp_qq_heu_gcd(R.zero, g) == (x + 1, R.zero, QQ(1, 2)) R, x, y = ring("x,y", RR) f = 2.1 * x * y**2 - 2.2 * x * y + 2.1 * x g = 1.0 * x**3 assert R.dmp_ff_prs_gcd(f, g) == \ (1.0*x, 2.1*y**2 - 2.2*y + 2.1, 1.0*x**2) R, x, y = ring("x,y", ZZ) f = ( -17434367009167300000000000000000000000000000000000000000000000000000000 * x**4 * y - 250501827896299135568887342575961783764139560000000000000000000000000000000000000000000 * x**3 * y - 2440935909299672540738135183426056447877858000000000000000000000000000000 * x**3 - 1349729941723537919695626818065131519270095220127010623905326719279566297660000000000000000000000000000 * x**2 * y - 26304033868956978374552886858060487282904504027042515077682955951658838800000000000000000 * x**2 - 3232215785736369696036755035364398565076440134133908303058376297547504030528179314849416971379040931276000000000000000 * x * y - 94485916261760032526508027937078714464844205539023800247528621905831259414691631156161537919255129011800 * x - 2902585888465621357542575571971656665554321652262249362701116665830760628936600958940851960635161420991047110815678789984677193092993 * y - 113133324167442997472440652189550843502029192913459268196939183295294085146407870078840385860571627108778756267503630290 ) g = (10000000000000000000000000000 * x**2 + 71841388839807267676152024786000000000000000 * x + 129029628760809605749020969023932901278290735413660734705971) assert (R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == ( g, -1743436700916730000000000000000000000000000 * x**2 * y - 12525091394814956778444367128798089188206978000000000000000 * x * y - 244093590929967254073813518342605644787785800 * x - 22495499028725631994927113634418779135935898997901327211111875586270479483 * y - 876801128965234839118530545935732755107147297241756982389990, 1))
def test_gf_factor(): R, x = ring('x', FF(2)) f = x**4 + x g = (1, [(x, 1), (x + 1, 1), (x**2 + x + 1, 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = x**18 + x**17 + x**16 + x**14 + x**12 + x**11 + x**8 + x**5 + x**3 + 1 g = (1, [(x + 1, 4), (x**4 + x**3 + 1, 1), (x**10 + x**8 + x**7 + x**5 + 1, 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = x**63 + 1 g = (1, [(x + 1, 1), (x**2 + x + 1, 1), (x**3 + x + 1, 1), (x**6 + x + 1, 1), (x**3 + x**2 + 1, 1), (x**6 + x**3 + 1, 1), (x**6 + x**5 + 1, 1), (x**6 + x**4 + x**2 + x + 1, 1), (x**6 + x**5 + x**2 + x + 1, 1), (x**6 + x**4 + x**3 + x + 1, 1), (x**6 + x**5 + x**4 + x + 1, 1), (x**6 + x**5 + x**3 + x**2 + 1, 1), (x**6 + x**5 + x**4 + x**2 + 1, 1)]) for method in ('zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = (x**28 + x**27 + x**26 + x**25 + x**24 + x**20 + x**19 + x**17 + x**16 + x**15 + x**14 + x**13 + x**12 + x**11 + x**9 + x**8 + x**5 + x**4 + x**2 + x) g = (1, [(x, 1), (x + 1, 2), (x**5 + x**4 + x**3 + x + 1, 1), (x**10 + x**9 + x**8 + x**7 + 1, 1), (x**10 + x**9 + x**8 + x**5 + x**4 + x**2 + 1, 1)]) for method in ('zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g R, x = ring('x', FF(3)) f = x**6 - x**5 + x**4 + x**3 - x g = (1, [(x, 1), (x + 1, 1), (x**2 + 1, 1), (x**2 + x + 2, 1)]) for method in ('zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = x**4 + x**3 + x + 2 g = (1, [(x**2 + 1, 1), (x**2 + x + 2, 1)]) for method in ('zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g R, x = ring('x', FF(11)) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert R(0).factor_list() == (0, []) assert R(1).factor_list() == (1, []) assert x.factor_list() == (1, [(x, 1)]) assert (x + 1).factor_list() == (1, [(x + 1, 1)]) assert (2 * x + 3).factor_list() == (2, [(x + 7, 1)]) assert (5 * x**3 + 2 * x**2 + 7 * x + 2).factor_list() == (5, [(x + 2, 1), (x + 8, 2)]) f = x**6 + 8 * x**5 + x**4 + 8 * x**3 + 10 * x**2 + 8 * x + 1 g = (1, [(x + 1, 1), (x**2 + 5 * x + 3, 1), (x**3 + 2 * x**2 + 3 * x + 4, 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = x**3 + 5 * x**2 + 8 * x + 4 g = (1, [(x + 1, 1), (x + 2, 2)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = x**9 + x**8 + 10 * x**7 + x**6 + 10 * x**4 + 10 * x**3 + 10 * x**2 g = (1, [(x, 2), (x**2 + 9 * x + 5, 1), (x**5 + 3 * x**4 + 8 * x**2 + 5 * x + 2, 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = x**32 + 1 g = (1, [(x**16 + 3 * x**8 + 10, 1), (x**16 + 8 * x**8 + 10, 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = 8 * x**32 + 5 g = (8, [(x + 3, 1), (x + 8, 1), (x**2 + 9, 1), (x**2 + 2 * x + 2, 1), (x**2 + 9 * x + 2, 1), (x**8 + x**4 + 6, 1), (x**8 + 10 * x**4 + 6, 1), (x**4 + 5 * x**2 + 7, 1), (x**4 + 6 * x**2 + 7, 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = 8 * x**63 + 5 g = (8, [(x + 7, 1), (x**6 + 9 * x**3 + 4, 1), (x**2 + 4 * x + 5, 1), (x**3 + 6 * x**2 + 8 * x + 2, 1), (x**3 + 9 * x**2 + 9 * x + 2, 1), (x**6 + 2 * x**5 + 6 * x**4 + 8 * x**2 + 4 * x + 4, 1), (x**6 + 2 * x**5 + 8 * x**3 + 4 * x**2 + 6 * x + 4, 1), (x**6 + 5 * x**5 + 6 * x**4 + 8 * x**2 + 6 * x + 4, 1), (x**6 + 2 * x**5 + 3 * x**4 + 8 * x**3 + 6 * x + 4, 1), (x**6 + 10 * x**5 + 4 * x**4 + 7 * x**3 + 10 * x**2 + 7 * x + 4, 1), (x**6 + 3 * x**5 + 3 * x**4 + x**3 + 6 * x**2 + 8 * x + 4, 1), (x**6 + 6 * x**5 + 2 * x**4 + 7 * x**3 + 9 * x**2 + 8 * x + 4, 1), (x**6 + 10 * x**5 + 10 * x**4 + x**3 + 4 * x**2 + 9 * x + 4, 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g f = x**15 - 1 g = (1, [(x + 2, 1), (x + 6, 1), (x + 7, 1), (x + 8, 1), (x + 10, 1), (x**2 + x + 1, 1), (x**2 + 5 * x + 3, 1), (x**2 + 9 * x + 4, 1), (x**2 + 4 * x + 5, 1), (x**2 + 3 * x + 9, 1)]) for method in ('zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g with using(gf_factor_method='other'): pytest.raises(KeyError, lambda: (x + 1).factor_list()) R, x = ring('x', FF(13)) f = x**8 + x**6 + 10 * x**4 + 10 * x**3 + 8 * x**2 + 2 * x + 8 g = (1, [(x + 3, 1), (x**3 + 8 * x**2 + 4 * x + 12, 1), (x**4 + 2 * x**3 + 3 * x**2 + 4 * x + 6, 1)]) with using(gf_factor_method='berlekamp'): assert f.factor_list() == g R, x = ring('x', FF(809)) f = (x**10 + 2 * x**9 + 5 * x**8 + 26 * x**7 + 677 * x**6 + 436 * x**5 + 791 * x**4 + 325 * x**3 + 456 * x**2 + 24 * x + 577) g = (1, [(x + 701, 1), (x**9 + 110 * x**8 + 559 * x**7 + 532 * x**6 + 694 * x**5 + 151 * x**4 + 110 * x**3 + 70 * x**2 + 735 * x + 122, 1)]) for method in ('zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi) R, x = ring('x', FF(nextprime(2**15 * pi))) f = x**15 + x + 1 g = (1, [ (x**2 + 22730 * x + 68144, 1), (x**4 + 81553 * x**3 + 77449 * x**2 + 86810 * x + 4724, 1), (x**4 + 86276 * x**3 + 56779 * x**2 + 14859 * x + 31575, 1), (x**5 + 15347 * x**4 + 95022 * x**3 + 84569 * x**2 + 94508 * x + 92335, 1) ]) for method in ('zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g # Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n # (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1 R, x = ring('x', FF(nextprime(2**4 * pi))) f = x**6 + 2 * x**5 + 5 * x**4 + 26 * x**3 + 41 * x**2 + 39 * x + 38 g = (1, [(x**2 + 44 * x + 26, 1), (x**4 + 11 * x**3 + 25 * x**2 + 18 * x + 30, 1)]) for method in ('zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g F8 = FF(2, [1, 1, 0, 1]) R, x = ring('x', F8) f = x**10 + x**9 + F8(2) * x**8 + F8(2) * x**7 + F8(5) * x**6 + F8( 3) * x**5 g = (F8(1), [(x, 5), (x + F8(3), 1), (x + F8(6), 1), (x**3 + F8(4) * x**2 + x + F8(3), 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g F9 = FF(3, [2, 2, 1]) R, x = ring('x', F9) f = x**5 + F9(2) * x**4 + F9(6) * x**3 + F9(8) * x**2 + F9(5) * x + F9(4) g = (1, [(x + F9(8), 1), (x**2 + 2 * x + F9(4), 1), (x**2 + F9(4) * x + F9(4), 1)]) for method in ('berlekamp', 'zassenhaus', 'shoup'): with using(gf_factor_method=method): assert f.factor_list() == g
def test_benchmark_czichowski_buchberger(): with config.using(groebner='buchberger'): _do_test_benchmark_czichowski()
def test_benchmark_kastura_4_buchberger(): with config.using(groebner='buchberger'): _do_test_benchmark_katsura_4()
def test_benchmark_czichowski_f5b(): with config.using(groebner='f5b'): _do_test_benchmark_czichowski()