test_polynomialring.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 * (Y**2 + 1) == R.convert(x * (y**2 + 1))
  19. assert X + 1 == R.convert(x + 1)
  20. raises(ExactQuotientFailed, lambda: X/Y)
  21. raises(TypeError, lambda: x/Y)
  22. raises(TypeError, lambda: X/y)
  23. assert X**2 / X == X
  24. assert R.from_GlobalPolynomialRing(ZZ.old_poly_ring(x, y).convert(x), ZZ.old_poly_ring(x, y)) == X
  25. assert R.from_FractionField(Qxy.convert(x), Qxy) == X
  26. assert R.from_FractionField(Qxy.convert(x/y), Qxy) is None
  27. assert R._sdm_to_vector(R._vector_to_sdm([X, Y], R.order), 2) == [X, Y]
  28. def test_localring():
  29. Qxy = QQ.old_frac_field(x, y)
  30. R = QQ.old_poly_ring(x, y, order="ilex")
  31. X = R.convert(x)
  32. Y = R.convert(y)
  33. assert x in R
  34. assert 1/x not in R
  35. assert 1/(1 + x) in R
  36. assert Y in R
  37. assert X*(Y**2 + 1)/(1 + X) == R.convert(x*(y**2 + 1)/(1 + x))
  38. raises(TypeError, lambda: x/Y)
  39. raises(TypeError, lambda: X/y)
  40. assert X + 1 == R.convert(x + 1)
  41. assert X**2 / X == X
  42. assert R.from_GlobalPolynomialRing(ZZ.old_poly_ring(x, y).convert(x), ZZ.old_poly_ring(x, y)) == X
  43. assert R.from_FractionField(Qxy.convert(x), Qxy) == X
  44. raises(CoercionFailed, lambda: R.from_FractionField(Qxy.convert(x/y), Qxy))
  45. raises(ExactQuotientFailed, lambda: R.exquo(X, Y))
  46. raises(NotReversible, lambda: R.revert(X))
  47. assert R._sdm_to_vector(
  48. R._vector_to_sdm([X/(X + 1), Y/(1 + X*Y)], R.order), 2) == \
  49. [X*(1 + X*Y), Y*(1 + X)]
  50. def test_conversion():
  51. L = QQ.old_poly_ring(x, y, order="ilex")
  52. G = QQ.old_poly_ring(x, y)
  53. assert L.convert(x) == L.convert(G.convert(x), G)
  54. assert G.convert(x) == G.convert(L.convert(x), L)
  55. raises(CoercionFailed, lambda: G.convert(L.convert(1/(1 + x)), L))
  56. def test_units():
  57. R = QQ.old_poly_ring(x)
  58. assert R.is_unit(R.convert(1))
  59. assert R.is_unit(R.convert(2))
  60. assert not R.is_unit(R.convert(x))
  61. assert not R.is_unit(R.convert(1 + x))
  62. R = QQ.old_poly_ring(x, order='ilex')
  63. assert R.is_unit(R.convert(1))
  64. assert R.is_unit(R.convert(2))
  65. assert not R.is_unit(R.convert(x))
  66. assert R.is_unit(R.convert(1 + x))
  67. R = ZZ.old_poly_ring(x)
  68. assert R.is_unit(R.convert(1))
  69. assert not R.is_unit(R.convert(2))
  70. assert not R.is_unit(R.convert(x))
  71. assert not R.is_unit(R.convert(1 + x))