def test_benchmark_cyclic_4(method): with 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_benchmark_katsura_4(method): with 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_sympyissue_23174(): _, x = ring('x', FF(2)) f = (x**16 + x**15 + x**14 + x**13 + x**12 + x**11 + x**10 + x**9 + x**8 + x**7 + x**6 + x**5 + x**4 + x**3 + x**2 + x + 1) r = (1, [(x**8 + x**5 + x**4 + x**3 + 1, 1), (x**8 + x**7 + x**6 + x**4 + x**2 + x + 1, 1)]) with using(gf_factor_method='zassenhaus'): assert f.factor_list() == r
def test_benchmark_minimal_polynomial(method): with 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_sympyissue_16620(): _, 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_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 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', FF(5)) f = 3 * x**2 + 2 * x + 4 g = 2 * x**2 + 2 * x + 3 assert f.cofactors(g) == (x + 3, 3 * x + 3, 2 * x + 1) R, x = ring('x', FF(11)) assert R(0).cofactors(R(0)) == (0, 0, 0) assert R(2).cofactors(R(0)) == (1, 2, 0) assert R(0).cofactors(R(2)) == (1, 0, 2) assert R(2).cofactors(R(2)) == (1, 2, 2) assert R(0).cofactors(x) == (x, 0, 1) assert x.cofactors(R(0)) == (x, 1, 0) assert (3 * x).cofactors(3 * x) == (x, 3, 3) assert (x**2 + 8 * x + 7).cofactors(x**3 + 7 * x**2 + x + 7) == (x + 7, x + 1, x**2 + 1) R, x = ring('x', ZZ) for test in (True, False): for method in ('prs', 'modgcd'): with using(use_heu_gcd=test, fallback_gcd_zz_method=method): assert R(0).cofactors(R(0)) == (0, 0, 0) assert R(0).cofactors(x) == (x, 0, 1) assert x.cofactors(R(0)) == (x, 1, 0) assert R(0).cofactors(-x) == (x, 0, -1) assert (-x).cofactors(R(0)) == (x, -1, 0) assert (2 * x).cofactors(R(2)) == (2, x, 1) assert R(2).cofactors(R(0)) == (2, 1, 0) assert R(-2).cofactors(R(0)) == (2, -1, 0) assert R(0).cofactors(R(-2)) == (2, 0, -1) assert R(0).cofactors(2 * x + 4) == (2 * x + 4, 0, 1) assert (2 * x + 4).cofactors(R(0)) == (2 * x + 4, 1, 0) assert R(2).cofactors(R(2)) == (2, 1, 1) assert R(-2).cofactors(R(2)) == (2, -1, 1) assert R(2).cofactors(R(-2)) == (2, 1, -1) assert R(-2).cofactors(R(-2)) == (2, -1, -1) assert (x**2 + 2 * x + 1).cofactors(R(1)) == (1, x**2 + 2 * x + 1, 1) assert (x**2 + 2 * x + 1).cofactors(R(2)) == (1, x**2 + 2 * x + 1, 2) assert (2 * x**2 + 4 * x + 2).cofactors(R(2)) == (2, x**2 + 2 * x + 1, 1) assert R(2).cofactors(2 * x**2 + 4 * x + 2) == (2, 1, x**2 + 2 * x + 1) assert (2 * x**2 + 4 * x + 2).cofactors(x + 1) == (x + 1, 2 * x + 2, 1) assert (x + 1).cofactors(2 * x**2 + 4 * x + 2) == (x + 1, 1, 2 * x + 2) assert (x - 31).cofactors(x) == (1, x - 31, x) 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**2 - 1 g = x**2 - 3 * x + 2 assert f.cofactors(g) == (x - 1, x + 1, x - 2) 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) h = ( 365431878023781158602430064717380211405897160759702125019136 * x**21 + 197599133478719444145775798221171663643171734081650688 * x**14 - 9504116979659010018253915765478924103928886144 * x**7 - 311973482284542371301330321821976049) cff = (-964661685087874498642420170752 * x**28 + 649736296036977287118848 * x**21 + 658473216967637120 * x**14 - 30463679113 * x**7 - 1) cfg = (-47268422569305850433478588366848 * x**27 + 30940259392972115602096128 * x**20 + 18261628279718027904 * x**13 - 426497272383 * x**6) assert f.cofactors(f.diff()) == (h, cff, cfg) f = 1317378933230047068160 * x + 2945748836994210856960 g = 120352542776360960 * x + 269116466014453760 H, cff, cfg = 120352542776360960 * x + 269116466014453760, 10946, 1 assert f.cofactors(g) == (H, cff, cfg) with using(heu_gcd_max=0): assert f.cofactors(g) == (H, cff, cfg) R, x = ring('x', QQ) for test in (True, False): with using(use_heu_gcd=test, fallback_gcd_zz_method='prs'): 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) assert R(0).cofactors(R(0)) == (0, 0, 0) f, g = x**2 / 2 + x + QQ(1, 2), x / 2 + QQ(1, 2) assert f.cofactors(g) == (x + 1, g, QQ(1, 2)) f, g = x**2 - 1, x**2 - 3 * x + 2 assert f.cofactors(g) == (x - 1, x + 1, x - 2) R, x = ring('x', QQ.algebraic_field(sqrt(2))) for method in ('modgcd', 'prs'): with using(gcd_aa_method=method): 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 = ring('x', QQ.algebraic_field(sqrt(2)**(-1) * sqrt(3))) for method in ('modgcd', 'prs'): with using(gcd_aa_method=method): f, g = x + 1, x - 1 assert f.cofactors(g) == (1, f, g) R, x = ring('x', CC) f, g = x**2 - 1, x**3 - 3 * x + 2 assert f.cofactors(g) == (x - 1, x + 1, x**2 + x - 2) R, x, y = ring('x y', ZZ) for test in (True, False): for method in ('prs', 'modgcd'): with using(use_heu_gcd=test, fallback_gcd_zz_method=method): assert R(0).cofactors(R(0)) == (0, 0, 0) assert R(2).cofactors(R(0)) == (2, 1, 0) assert R(-2).cofactors(R(0)) == (2, -1, 0) assert R(0).cofactors(R(-2)) == (2, 0, -1) assert R(0).cofactors(2 * x + 4) == (2 * x + 4, 0, 1) assert (2 * x).cofactors(R(2)) == (2, x, 1) assert (2 * x + 4).cofactors(R(0)) == (2 * x + 4, 1, 0) assert R(2).cofactors(R(2)) == (2, 1, 1) assert R(-2).cofactors(R(2)) == (2, -1, 1) assert R(2).cofactors(R(-2)) == (2, 1, -1) assert R(-2).cofactors(R(-2)) == (2, -1, -1) assert (x**2 + 2 * x + 1).cofactors(R(1)) == (1, x**2 + 2 * x + 1, 1) assert (x**2 + 2 * x + 1).cofactors(R(2)) == (1, x**2 + 2 * x + 1, 2) assert (2 * x**2 + 4 * x + 2).cofactors(R(2)) == (2, x**2 + 2 * x + 1, 1) assert R(2).cofactors(2 * x**2 + 4 * x + 2) == (2, 1, x**2 + 2 * x + 1) f, g = 2 * x**2 + 4 * x + 2, x + 1 assert f.cofactors(g) == (g, 2 * x + 2, 1) assert g.cofactors(f) == (g, 1, 2 * x + 2) with using(heu_gcd_max=0): assert f.cofactors(g) == (g, 2 * x + 2, 1) 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, g = x + 2 * y, x + y assert f.cofactors(g) == (1, f, g) f, g = x**2 + 2 * x * y + y**2, x**2 + x * y assert f.cofactors(g) == (x + y, x + y, x) 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 = 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) 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 ) h = ( -1743436700916730000000000000000000000000000 * x**2 * y - 12525091394814956778444367128798089188206978000000000000000 * x * y - 244093590929967254073813518342605644787785800 * x - 22495499028725631994927113634418779135935898997901327211111875586270479483 * y - 876801128965234839118530545935732755107147297241756982389990 ) assert f.cofactors(g) == (g, h, 1) R, x, y = ring('x y', QQ) for test in (True, False): with using(use_heu_gcd=test, fallback_gcd_zz_method='prs'): f, g = x**2 / 2 + x + QQ(1, 2), x / 2 + QQ(1, 2) assert f.cofactors(g) == (x + 1, g, QQ(1, 2)) assert g.cofactors(f) == (x + 1, QQ(1, 2), g) assert f.gcd(g) == x + 1 with using(fallback_gcd_zz_method='modgcd'): assert f.gcd(g) == x + 1 assert R(0).cofactors(R(0)) == (0, 0, 0) assert R(0).cofactors(g) == (x + 1, 0, QQ(1, 2)) f, g = x**2 / 4 + x * y + y**2, x**2 / 2 + x * y assert f.cofactors(g) == (x + 2 * y, x / 4 + y / 2, x / 2) f, g = x**2 / 2 + x * y + y**2 / 2, x**2 + x * y assert f.cofactors(g) == (x + y, x / 2 + y / 2, x) R, x, y = ring('x y', QQ.algebraic_field(sqrt(2))) for method in ('modgcd', 'prs'): with using(gcd_aa_method=method): f, g = (x + sqrt(2) * y)**2, x + sqrt(2) * y assert f.cofactors(g) == (g, g, 1) 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) R, x, y = ring('x y', RR) for test in (True, False): with using(use_heu_gcd=test, fallback_gcd_zz_method='prs'): f, g = 2.1 * x * y**2 - 2.2 * x * y + 2.1 * x, 1.0 * x**3 h = 1.0 * x assert f.cofactors(g) == (h, 2.1 * y**2 - 2.2 * y + 2.1, 1.0 * x**2) f, g = 2.1 * x * y**2 - 2.1 * x * y + 2.1 * x, 2.1 * x**3 assert f.cofactors(g) == (h, f // h, g // h) assert g.cofactors(f) == (h, g // h, f // h) R, x, y = ring('x y', CC) f, g = x**2 - y, x**3 - y * x + 2 assert f.cofactors(g) == (1, f, g) R, x, y, z = ring('x y z', ZZ) for test in (True, False): for method in ('prs', 'modgcd'): with using(use_heu_gcd=test, fallback_gcd_zz_method=method): 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 = ring('x y z', QQ.algebraic_field(sqrt(2), sqrt(3))) 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) 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) R, x, y, z = ring('x y z', QQ.algebraic_field(I)) for method in ('prs', 'modgcd'): with using(gcd_aa_method=method): f, g = R.one, I * z assert f.cofactors(g) == (1, f, g) assert g.cofactors(f) == (1, g, f) R, x, y, z, u = ring('x y z u', ZZ) for test in (True, False): for method in ('prs', 'modgcd'): with using(use_heu_gcd=test, fallback_gcd_zz_method=method): 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 = x + y + z, -x - y - z - u assert f.cofactors(g) == (1, f, 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 f, g, h = (1199999999999991 * x**17 - y, 2 * y - 19989798798 + x**211, 12 * x * y**7 + x**4 - 1) for _ in range(10): assert (f * h).cofactors(g * h) == (h, f, g) R, x, y, z, u, _ = ring('x y z u v', ZZ) for test in (True, False): with using(use_heu_gcd=test, 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 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, _, a, b = ring('x y z u v a b', ZZ) for test in (True, False): with using(use_heu_gcd=test, 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, _, a, b, *_ = ring('x y z u v a b c d', ZZ) for test in (True, False): with using(use_heu_gcd=test, 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 F, x = field('x', QQ) R, _ = ring('t', F) assert R(x).gcd(R(0)) == 1
def test_PolyElement_subresultants(): R, x = ring('x', ZZ) for check in (True, False): with using(use_collins_resultant=check): assert R(0).resultant(R(0)) == 0 assert R(0).resultant(R(0), includePRS=True) == (0, []) assert R(1).resultant(R(0)) == 0 assert R(1).subresultants(R(0)) == [1] assert R(0).resultant(R(1)) == 0 assert R(0).resultant(R(1), includePRS=True) == (0, [1]) 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 = R(260708) assert f.subresultants(g) == [f, g, a, b, c, d] assert f.resultant(g) == d.drop(x) f = x**2 - 2 * x + 1 g = x**2 - 1 a = 2 * x - 2 assert f.subresultants(g) == [f, g, a] assert f.resultant(g) == 0 f = x**2 + 1 g = x**2 - 1 a = -2 assert f.subresultants(g) == [f, g, a] assert f.resultant(g) == 4 assert f.resultant(g, includePRS=True) == (4, [x**2 + 1, x**2 - 1, -2]) f = x**2 - 1 g = x**3 - x**2 + 2 assert f.resultant(g) == 0 f = 3 * x**3 - x g = 5 * x**2 + 1 assert f.resultant(g) == 64 f = x**2 - 2 * x + 7 g = x**3 - x + 5 assert f.resultant(g) == 265 f = x**3 - 6 * x**2 + 11 * x - 6 g = x**3 - 15 * x**2 + 74 * x - 120 assert f.resultant(g) == -8640 f = x**3 - 6 * x**2 + 11 * x - 6 g = x**3 - 10 * x**2 + 29 * x - 20 assert f.resultant(g) == 0 f = x**3 - 1 g = x**3 + 2 * x**2 + 2 * x - 1 assert f.resultant(g) == 16 f = x**8 - 2 g = x - 1 assert f.resultant(g) == -1 # issue sympy/sympy#10666 f = x**3 - 7 * x + 7 g = x assert f.resultant(g) == -g.resultant(f) == -7 Rt, t = ring('t', ZZ) _, 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 f.resultant( g) == 2930944 * t**6 + 2198208 * t**4 + 549552 * t**2 + 45796 assert (x - 1).resultant(x + 1, includePRS=True) == (2, [x - 1, x + 1, 2]) R, x, y = ring('x y', ZZ) for check in (True, False): with using(use_collins_resultant=check): assert R(0).resultant(R(0)) == 0 assert R(0).resultant(R(0), includePRS=True) == (0, []) assert R(0).resultant(R(1)) == 0 assert R(1).resultant(R(0)) == 0 assert R(1).subresultants(R(0)) == [1] assert R(0).resultant(R(1), includePRS=True) == (0, [1]) f = x + y + 2 g = 2 * x * y + x + 3 assert f.resultant(g) == (-2 * y**2 - 5 * y + 1).drop(x) 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 = b.drop(x) rr = (r, [ 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 f.subresultants(g) == [f, g, a, b] assert f.resultant(g) == r assert f.resultant(g, includePRS=True) == rr 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 = b.drop(x) assert f.subresultants(g) == [f, g, a] assert f.resultant(g) == r assert f.resultant(g, includePRS=True)[0] == r f = x + y g = x**2 - x * y + 1 assert f.resultant(g) == (1 + 2 * y**2).drop(x) g += 1 assert f.resultant(g) == (2 + 2 * y**2).drop(x) R, x, y = ring('x y', QQ) for check in (True, False): with using(use_collins_resultant=check): assert R(0).resultant(R(0)) == 0 assert R(0).resultant(R(1)) == 0 f = x + y g = x**2 - x * y + 1 assert f.resultant(g) == (1 + 2 * y**2).drop(x) f = x / 2 + y + QQ(2, 3) g = 2 * x * y + x + 3 assert f.resultant(g) == (-2 * y**2 - 7 * y / 3 + QQ(5, 6)).drop(x) f = 3 * x**2 * y - y**3 - 4 g = x**2 + x * y**3 - 9 assert f.resultant(g) == (-3 * y**10 - 12 * y**7 + y**6 - 54 * y**4 + 8 * y**3 + 729 * y**2 - 216 * y + 16).drop(x) f = -x**3 + 5 g = 3 * x**2 * y + x**2 assert f.resultant(g) == (675 * y**3 + 675 * y**2 + 225 * y + 25).drop(x) R, x, y, z, u, v = ring('x y z u v', ZZ) for check in (True, False): with using(use_collins_resultant=check): 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 f.resultant(g) == r.drop(x) R, x, y, z, u, v = ring('x y z u v', QQ) for check in (True, False): with using(use_collins_resultant=check): 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 f.resultant(g) == r.drop(x)
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, _ = ring('x y z', QQ) assert (x**2 + x + 1).is_irreducible assert (x**2 + 2 * x + 1).is_irreducible is False
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, (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 F4 = FF(2, [1, 1, 1]) R, x = ring('x', F4) f = x**3 + F4(3) * x + F4(2) g = (1, [(x + 1, 1), (x**2 + x + F4(2), 1)]) for method in ('berlekamp', '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 = (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 f = x**4 + F8(5) * x**2 + 1 g = (1, [(x + F8(4), 2), (x + F8(7), 2)]) 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_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, R(EX(sin(1))).factor_list) R, x, y = ring('x y', FF(2)) pytest.raises(NotImplementedError, (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__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_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 _ 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_czichowski(method): # This is very slow (> 2 minutes on 3.4 GHz) without GMPY with using(groebner=method): R, x, t = ring('x t', ZZ, lex) I = [9*x**8 + 36*x**7 - 32*x**6 - 252*x**5 - 78*x**4 + 468*x**3 + 288*x**2 - 108*x + 9, (-72 - 72*t)*x**7 + (-256 - 252*t)*x**6 + (192 + 192*t)*x**5 + (1280 + 1260*t)*x**4 + (312 + 312*t)*x**3 + (-404*t)*x**2 + (-576 - 576*t)*x + 96 + 108*t] assert groebner(I, R) == [ 3725588592068034903797967297424801242396746870413359539263038139343329273586196480000*x - 160420835591776763325581422211936558925462474417709511019228211783493866564923546661604487873*t**7 - 1406108495478033395547109582678806497509499966197028487131115097902188374051595011248311352864*t**6 - 5241326875850889518164640374668786338033653548841427557880599579174438246266263602956254030352*t**5 - 10758917262823299139373269714910672770004760114329943852726887632013485035262879510837043892416*t**4 - 13119383576444715672578819534846747735372132018341964647712009275306635391456880068261130581248*t**3 - 9491412317016197146080450036267011389660653495578680036574753839055748080962214787557853941760*t**2 - 3767520915562795326943800040277726397326609797172964377014046018280260848046603967211258368000*t - 632314652371226552085897259159210286886724229880266931574701654721512325555116066073245696000, 610733380717522355121*t**8 + 6243748742141230639968*t**7 + 27761407182086143225024*t**6 + 70066148869420956398592*t**5 + 109701225644313784229376*t**4 + 109009005495588442152960*t**3 + 67072101084384786432000*t**2 + 23339979742629593088000*t + 3513592776846090240000, ] R, x, t = ring('x t', ZZ, grlex) I = [i.set_ring(R) for i in I] assert groebner(I, R) == [ 16996618586000601590732959134095643086442*t**3*x - 32936701459297092865176560282688198064839*t**3 + 78592411049800639484139414821529525782364*t**2*x - 120753953358671750165454009478961405619916*t**2 + 120988399875140799712152158915653654637280*t*x - 144576390266626470824138354942076045758736*t + 60017634054270480831259316163620768960*x**2 + 61976058033571109604821862786675242894400*x - 56266268491293858791834120380427754600960, 576689018321912327136790519059646508441672750656050290242749*t**4 + 2326673103677477425562248201573604572527893938459296513327336*t**3 + 110743790416688497407826310048520299245819959064297990236000*t**2*x + 3308669114229100853338245486174247752683277925010505284338016*t**2 + 323150205645687941261103426627818874426097912639158572428800*t*x + 1914335199925152083917206349978534224695445819017286960055680*t + 861662882561803377986838989464278045397192862768588480000*x**2 + 235296483281783440197069672204341465480107019878814196672000*x + 361850798943225141738895123621685122544503614946436727532800, -117584925286448670474763406733005510014188341867*t**3 + 68566565876066068463853874568722190223721653044*t**2*x - 435970731348366266878180788833437896139920683940*t**2 + 196297602447033751918195568051376792491869233408*t*x - 525011527660010557871349062870980202067479780112*t + 517905853447200553360289634770487684447317120*x**3 + 569119014870778921949288951688799397569321920*x**2 + 138877356748142786670127389526667463202210102080*x - 205109210539096046121625447192779783475018619520, -3725142681462373002731339445216700112264527*t**3 + 583711207282060457652784180668273817487940*t**2*x - 12381382393074485225164741437227437062814908*t**2 + 151081054097783125250959636747516827435040*t*x**2 + 1814103857455163948531448580501928933873280*t*x - 13353115629395094645843682074271212731433648*t + 236415091385250007660606958022544983766080*x**2 + 1390443278862804663728298060085399578417600*x - 4716885828494075789338754454248931750698880, ]
def test_groebner(method): with using(groebner=method): R, x, y = ring('x y', QQ, lex) f = x**2 + 2*x*y**2 g = x*y + 2*y**3 - 1 assert not is_groebner([f, g]) ans = [x, y**3 - QQ(1, 2)] assert groebner([f, g], R) == ans assert is_groebner(ans) assert is_minimal(ans, R) assert groebner([x, x**2], R) == [x] assert groebner([x**2, x], R) == [x] R, x, y = ring('x y', ZZ) f = x**2*y + y**6 + 1 g = x**2 - 2*x*y assert not is_groebner([f, g]) ans = [2*x - y**10 - 4*y**7 - y**4 - 4*y, y**12 + 4*y**9 + 2*y**6 + 4*y**3 + 1] assert groebner([f, g], R) == ans assert is_groebner(ans) assert is_minimal(ans, R) is False R, y, x = ring('y x', QQ, lex) f = 2*x**2*y + y**2 g = 2*x**3 + x*y - 1 assert groebner([f, g], R) == [y, x**3 - QQ(1, 2)] R, x, y, z = ring('x y z', QQ, lex) f = x - z**2 g = y - z**3 assert groebner([f, g], R) == [f, g] R, x, y = ring('x y', QQ, grlex) f = x**3 - 2*x*y g = x**2*y + x - 2*y**2 assert groebner([f, g], R) == [x**2, x*y, -x/2 + y**2] R, x, y, z = ring('x y z', QQ, lex) f = -x**2 + y g = -x**3 + z assert groebner([f, g], R) == [x**2 - y, x*y - z, x*z - y**2, y**3 - z**2] R, x, y, z = ring('x y z', QQ, grlex) f = -x**2 + y g = -x**3 + z assert groebner([f, g], R) == [y**3 - z**2, x**2 - y, x*y - z, x*z - y**2] R, x, y, z = ring('x y z', QQ, lex) f = -x**2 + z g = -x**3 + y assert groebner([f, g], R) == [x**2 - z, x*y - z**2, x*z - y, y**2 - z**3] R, x, y, z = ring('x y z', QQ, grlex) f = -x**2 + z g = -x**3 + y assert groebner([f, g], R) == [-y**2 + z**3, x**2 - z, x*y - z**2, x*z - y] R, x, y, z = ring('x y z', QQ, lex) f = x - y**2 g = -y**3 + z assert groebner([f, g], R) == [x - y**2, y**3 - z] R, x, y, z = ring('x y z', QQ, grlex) f = x - y**2 g = -y**3 + z assert groebner([f, g], R) == [x**2 - y*z, x*y - z, -x + y**2] R, x, y, z = ring('x y z', QQ, lex) f = x - z**2 g = y - z**3 assert groebner([f, g], R) == [x - z**2, y - z**3] R, x, y, z = ring('x y z', QQ, grlex) f = x - z**2 g = y - z**3 assert groebner([f, g], R) == [x**2 - y*z, x*z - y, -x + z**2] R, x, y, z = ring('x y z', QQ, lex) f = -y**2 + z g = x - y**3 assert groebner([f, g], R) == [x - y*z, y**2 - z] R, x, y, z = ring('x y z', QQ, grlex) f = -y**2 + z g = x - y**3 assert groebner([f, g], R) == [-x**2 + z**3, x*y - z**2, y**2 - z, -x + y*z] R, x, y, z = ring('x y z', QQ, lex) f = y - z**2 g = x - z**3 assert groebner([f, g], R) == [x - z**3, y - z**2] R, x, y, z = ring('x y z', QQ, grlex) f = y - z**2 g = x - z**3 assert groebner([f, g], R) == [-x**2 + y**3, x*z - y**2, -x + y*z, -y + z**2] R, x, y, z = ring('x y z', QQ, lex) f = 4*x**2*y**2 + 4*x*y + 1 g = x**2 + y**2 - 1 assert groebner([f, g], R) == [ x - 4*y**7 + 8*y**5 - 7*y**3 + 3*y, y**8 - 2*y**6 + 3*y**4/2 - y**2/2 + QQ(1, 16), ] b = [y**2 + x*y + x**2, y + x, y, x**2, x] assert is_groebner(b) assert not is_minimal(b, R)