test_ideals.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """Test ideals.py code."""
  2. from sympy.polys import QQ, ilex
  3. from sympy.abc import x, y, z
  4. from sympy.testing.pytest import raises
  5. def test_ideal_operations():
  6. R = QQ.old_poly_ring(x, y)
  7. I = R.ideal(x)
  8. J = R.ideal(y)
  9. S = R.ideal(x*y)
  10. T = R.ideal(x, y)
  11. assert not (I == J)
  12. assert I == I
  13. assert I.union(J) == T
  14. assert I + J == T
  15. assert I + T == T
  16. assert not I.subset(T)
  17. assert T.subset(I)
  18. assert I.product(J) == S
  19. assert I*J == S
  20. assert x*J == S
  21. assert I*y == S
  22. assert R.convert(x)*J == S
  23. assert I*R.convert(y) == S
  24. assert not I.is_zero()
  25. assert not J.is_whole_ring()
  26. assert R.ideal(x**2 + 1, x).is_whole_ring()
  27. assert R.ideal() == R.ideal(0)
  28. assert R.ideal().is_zero()
  29. assert T.contains(x*y)
  30. assert T.subset([x, y])
  31. assert T.in_terms_of_generators(x) == [R(1), R(0)]
  32. assert T**0 == R.ideal(1)
  33. assert T**1 == T
  34. assert T**2 == R.ideal(x**2, y**2, x*y)
  35. assert I**5 == R.ideal(x**5)
  36. def test_exceptions():
  37. I = QQ.old_poly_ring(x).ideal(x)
  38. J = QQ.old_poly_ring(y).ideal(1)
  39. raises(ValueError, lambda: I.union(x))
  40. raises(ValueError, lambda: I + J)
  41. raises(ValueError, lambda: I * J)
  42. raises(ValueError, lambda: I.union(J))
  43. assert (I == J) is False
  44. assert I != J
  45. def test_nontriv_global():
  46. R = QQ.old_poly_ring(x, y, z)
  47. def contains(I, f):
  48. return R.ideal(*I).contains(f)
  49. assert contains([x, y], x)
  50. assert contains([x, y], x + y)
  51. assert not contains([x, y], 1)
  52. assert not contains([x, y], z)
  53. assert contains([x**2 + y, x**2 + x], x - y)
  54. assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
  55. assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**3)
  56. assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4)
  57. assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y**2)
  58. assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4 + y**3 + 2*z*y*x)
  59. assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y*z)
  60. assert contains([x, 1 + x + y, 5 - 7*y], 1)
  61. assert contains(
  62. [x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
  63. x**3)
  64. assert not contains(
  65. [x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
  66. x**2 + y**2)
  67. # compare local order
  68. assert not contains([x*(1 + x + y), y*(1 + z)], x)
  69. assert not contains([x*(1 + x + y), y*(1 + z)], x + y)
  70. def test_nontriv_local():
  71. R = QQ.old_poly_ring(x, y, z, order=ilex)
  72. def contains(I, f):
  73. return R.ideal(*I).contains(f)
  74. assert contains([x, y], x)
  75. assert contains([x, y], x + y)
  76. assert not contains([x, y], 1)
  77. assert not contains([x, y], z)
  78. assert contains([x**2 + y, x**2 + x], x - y)
  79. assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
  80. assert contains([x*(1 + x + y), y*(1 + z)], x)
  81. assert contains([x*(1 + x + y), y*(1 + z)], x + y)
  82. def test_intersection():
  83. R = QQ.old_poly_ring(x, y, z)
  84. # SCA, example 1.8.11
  85. assert R.ideal(x, y).intersect(R.ideal(y**2, z)) == R.ideal(y**2, y*z, x*z)
  86. assert R.ideal(x, y).intersect(R.ideal()).is_zero()
  87. R = QQ.old_poly_ring(x, y, z, order="ilex")
  88. assert R.ideal(x, y).intersect(R.ideal(y**2 + y**2*z, z + z*x**3*y)) == \
  89. R.ideal(y**2, y*z, x*z)
  90. def test_quotient():
  91. # SCA, example 1.8.13
  92. R = QQ.old_poly_ring(x, y, z)
  93. assert R.ideal(x, y).quotient(R.ideal(y**2, z)) == R.ideal(x, y)
  94. def test_reduction():
  95. from sympy.polys.distributedmodules import sdm_nf_buchberger_reduced
  96. R = QQ.old_poly_ring(x, y)
  97. I = R.ideal(x**5, y)
  98. e = R.convert(x**3 + y**2)
  99. assert I.reduce_element(e) == e
  100. assert I.reduce_element(e, NF=sdm_nf_buchberger_reduced) == R.convert(x**3)