test_polynomialring.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """Tests for the PolynomialRing classes. """
  2. from sympy.polys.domains import QQ, ZZ
  3. from sympy.polys.polyerrors import ExactQuotientFailed, CoercionFailed, NotReversible
  4. from sympy.abc import x, y
  5. from sympy.testing.pytest import raises
  6. def test_build_order():
  7. R = QQ.old_poly_ring(x, y, order=(("lex", x), ("ilex", y)))
  8. assert R.order((1, 5)) == ((1,), (-5,))
  9. def test_globalring():
  10. Qxy = QQ.old_frac_field(x, y)
  11. R = QQ.old_poly_ring(x, y)
  12. X = R.convert(x)
  13. Y = R.convert(y)
  14. assert x in R
  15. assert 1/x not in R
  16. assert 1/(1 + x) not in R
  17. assert Y in R
  18. assert X.ring == R
  19. assert X * (Y**2 + 1) == R.convert(x * (y**2 + 1))
  20. assert X * y == X * Y == R.convert(x * y) == x * Y
  21. assert X + y == X + Y == R.convert(x + y) == x + Y
  22. assert X - y == X - Y == R.convert(x - y) == x - Y
  23. assert X + 1 == R.convert(x + 1)
  24. raises(ExactQuotientFailed, lambda: X/Y)
  25. raises(ExactQuotientFailed, lambda: x/Y)
  26. raises(ExactQuotientFailed, lambda: X/y)
  27. assert X**2 / X == X
  28. assert R.from_GlobalPolynomialRing(ZZ.old_poly_ring(x, y).convert(x), ZZ.old_poly_ring(x, y)) == X
  29. assert R.from_FractionField(Qxy.convert(x), Qxy) == X
  30. assert R.from_FractionField(Qxy.convert(x)/y, Qxy) is None
  31. assert R._sdm_to_vector(R._vector_to_sdm([X, Y], R.order), 2) == [X, Y]
  32. def test_localring():
  33. Qxy = QQ.old_frac_field(x, y)
  34. R = QQ.old_poly_ring(x, y, order="ilex")
  35. X = R.convert(x)
  36. Y = R.convert(y)
  37. assert x in R
  38. assert 1/x not in R
  39. assert 1/(1 + x) in R
  40. assert Y in R
  41. assert X.ring == R
  42. assert X*(Y**2 + 1)/(1 + X) == R.convert(x*(y**2 + 1)/(1 + x))
  43. assert X*y == X*Y
  44. raises(ExactQuotientFailed, lambda: X/Y)
  45. raises(ExactQuotientFailed, lambda: x/Y)
  46. raises(ExactQuotientFailed, lambda: X/y)
  47. assert X + y == X + Y == R.convert(x + y) == x + Y
  48. assert X - y == X - Y == R.convert(x - y) == x - Y
  49. assert X + 1 == R.convert(x + 1)
  50. assert X**2 / X == X
  51. assert R.from_GlobalPolynomialRing(ZZ.old_poly_ring(x, y).convert(x), ZZ.old_poly_ring(x, y)) == X
  52. assert R.from_FractionField(Qxy.convert(x), Qxy) == X
  53. raises(CoercionFailed, lambda: R.from_FractionField(Qxy.convert(x)/y, Qxy))
  54. raises(ExactQuotientFailed, lambda: X/Y)
  55. raises(NotReversible, lambda: X.invert())
  56. assert R._sdm_to_vector(
  57. R._vector_to_sdm([X/(X + 1), Y/(1 + X*Y)], R.order), 2) == \
  58. [X*(1 + X*Y), Y*(1 + X)]
  59. def test_conversion():
  60. L = QQ.old_poly_ring(x, y, order="ilex")
  61. G = QQ.old_poly_ring(x, y)
  62. assert L.convert(x) == L.convert(G.convert(x), G)
  63. assert G.convert(x) == G.convert(L.convert(x), L)
  64. raises(CoercionFailed, lambda: G.convert(L.convert(1/(1 + x)), L))
  65. def test_units():
  66. R = QQ.old_poly_ring(x)
  67. assert R.is_unit(R.convert(1))
  68. assert R.is_unit(R.convert(2))
  69. assert not R.is_unit(R.convert(x))
  70. assert not R.is_unit(R.convert(1 + x))
  71. R = QQ.old_poly_ring(x, order='ilex')
  72. assert R.is_unit(R.convert(1))
  73. assert R.is_unit(R.convert(2))
  74. assert not R.is_unit(R.convert(x))
  75. assert R.is_unit(R.convert(1 + x))
  76. R = ZZ.old_poly_ring(x)
  77. assert R.is_unit(R.convert(1))
  78. assert not R.is_unit(R.convert(2))
  79. assert not R.is_unit(R.convert(x))
  80. assert not R.is_unit(R.convert(1 + x))