test_heuristicgcd.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from sympy.polys.rings import ring
  2. from sympy.polys.domains import ZZ
  3. from sympy.polys.heuristicgcd import heugcd
  4. def test_heugcd_univariate_integers():
  5. R, x = ring("x", ZZ)
  6. f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
  7. g = x**3 + 6*x**2 + 11*x + 6
  8. h = x**2 + 3*x + 2
  9. cff = x**2 + 5*x + 4
  10. cfg = x + 3
  11. assert heugcd(f, g) == (h, cff, cfg)
  12. f = x**4 - 4
  13. g = x**4 + 4*x**2 + 4
  14. h = x**2 + 2
  15. cff = x**2 - 2
  16. cfg = x**2 + 2
  17. assert heugcd(f, g) == (h, cff, cfg)
  18. f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
  19. g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
  20. h = 1
  21. cff = f
  22. cfg = g
  23. assert heugcd(f, g) == (h, cff, cfg)
  24. f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
  25. + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
  26. + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
  27. + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
  28. - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
  29. + 289127344604779611146960547954288113529690984687482920704*x**14 \
  30. + 19007977035740498977629742919480623972236450681*x**7 \
  31. + 311973482284542371301330321821976049
  32. g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
  33. + 197599133478719444145775798221171663643171734081650688*x**14 \
  34. - 9504116979659010018253915765478924103928886144*x**7 \
  35. - 311973482284542371301330321821976049
  36. # TODO: assert heugcd(f, f.diff(x))[0] == g
  37. f = 1317378933230047068160*x + 2945748836994210856960
  38. g = 120352542776360960*x + 269116466014453760
  39. h = 120352542776360960*x + 269116466014453760
  40. cff = 10946
  41. cfg = 1
  42. assert heugcd(f, g) == (h, cff, cfg)
  43. def test_heugcd_multivariate_integers():
  44. R, x, y = ring("x,y", ZZ)
  45. f, g = 2*x**2 + 4*x + 2, x + 1
  46. assert heugcd(f, g) == (x + 1, 2*x + 2, 1)
  47. f, g = x + 1, 2*x**2 + 4*x + 2
  48. assert heugcd(f, g) == (x + 1, 1, 2*x + 2)
  49. R, x, y, z, u = ring("x,y,z,u", ZZ)
  50. f, g = u**2 + 2*u + 1, 2*u + 2
  51. assert heugcd(f, g) == (u + 1, u + 1, 2)
  52. f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
  53. h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1
  54. assert heugcd(f, g) == (h, cff, cfg)
  55. assert heugcd(g, f) == (h, cfg, cff)
  56. R, x, y, z = ring("x,y,z", ZZ)
  57. f, g, h = R.fateman_poly_F_1()
  58. H, cff, cfg = heugcd(f, g)
  59. assert H == h and H*cff == f and H*cfg == g
  60. R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
  61. f, g, h = R.fateman_poly_F_1()
  62. H, cff, cfg = heugcd(f, g)
  63. assert H == h and H*cff == f and H*cfg == g
  64. R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)
  65. f, g, h = R.fateman_poly_F_1()
  66. H, cff, cfg = heugcd(f, g)
  67. assert H == h and H*cff == f and H*cfg == g
  68. R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)
  69. f, g, h = R.fateman_poly_F_1()
  70. H, cff, cfg = heugcd(f, g)
  71. assert H == h and H*cff == f and H*cfg == g
  72. R, x, y, z = ring("x,y,z", ZZ)
  73. f, g, h = R.fateman_poly_F_2()
  74. H, cff, cfg = heugcd(f, g)
  75. assert H == h and H*cff == f and H*cfg == g
  76. f, g, h = R.fateman_poly_F_3()
  77. H, cff, cfg = heugcd(f, g)
  78. assert H == h and H*cff == f and H*cfg == g
  79. R, x, y, z, t = ring("x,y,z,t", ZZ)
  80. f, g, h = R.fateman_poly_F_3()
  81. H, cff, cfg = heugcd(f, g)
  82. assert H == h and H*cff == f and H*cfg == g
  83. def test_issue_10996():
  84. R, x, y, z = ring("x,y,z", ZZ)
  85. f = 12*x**6*y**7*z**3 - 3*x**4*y**9*z**3 + 12*x**3*y**5*z**4
  86. g = -48*x**7*y**8*z**3 + 12*x**5*y**10*z**3 - 48*x**5*y**7*z**2 + \
  87. 36*x**4*y**7*z - 48*x**4*y**6*z**4 + 12*x**3*y**9*z**2 - 48*x**3*y**4 \
  88. - 9*x**2*y**9*z - 48*x**2*y**5*z**3 + 12*x*y**6 + 36*x*y**5*z**2 - 48*y**2*z
  89. H, cff, cfg = heugcd(f, g)
  90. assert H == 12*x**3*y**4 - 3*x*y**6 + 12*y**2*z
  91. assert H*cff == f and H*cfg == g