test_conditionset.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. from sympy.core.expr import unchanged
  2. from sympy.sets import (ConditionSet, Intersection, FiniteSet,
  3. EmptySet, Union, Contains, ImageSet)
  4. from sympy.sets.sets import SetKind
  5. from sympy.core.function import (Function, Lambda)
  6. from sympy.core.mod import Mod
  7. from sympy.core.kind import NumberKind
  8. from sympy.core.numbers import (oo, pi)
  9. from sympy.core.relational import (Eq, Ne)
  10. from sympy.core.singleton import S
  11. from sympy.core.symbol import (Symbol, symbols)
  12. from sympy.functions.elementary.complexes import Abs
  13. from sympy.functions.elementary.trigonometric import (asin, sin)
  14. from sympy.logic.boolalg import And
  15. from sympy.matrices.dense import Matrix
  16. from sympy.matrices.expressions.matexpr import MatrixSymbol
  17. from sympy.sets.sets import Interval
  18. from sympy.testing.pytest import raises, warns_deprecated_sympy
  19. w = Symbol('w')
  20. x = Symbol('x')
  21. y = Symbol('y')
  22. z = Symbol('z')
  23. f = Function('f')
  24. def test_CondSet():
  25. sin_sols_principal = ConditionSet(x, Eq(sin(x), 0),
  26. Interval(0, 2*pi, False, True))
  27. assert pi in sin_sols_principal
  28. assert pi/2 not in sin_sols_principal
  29. assert 3*pi not in sin_sols_principal
  30. assert oo not in sin_sols_principal
  31. assert 5 in ConditionSet(x, x**2 > 4, S.Reals)
  32. assert 1 not in ConditionSet(x, x**2 > 4, S.Reals)
  33. # in this case, 0 is not part of the base set so
  34. # it can't be in any subset selected by the condition
  35. assert 0 not in ConditionSet(x, y > 5, Interval(1, 7))
  36. # since 'in' requires a true/false, the following raises
  37. # an error because the given value provides no information
  38. # for the condition to evaluate (since the condition does
  39. # not depend on the dummy symbol): the result is `y > 5`.
  40. # In this case, ConditionSet is just acting like
  41. # Piecewise((Interval(1, 7), y > 5), (S.EmptySet, True)).
  42. raises(TypeError, lambda: 6 in ConditionSet(x, y > 5,
  43. Interval(1, 7)))
  44. X = MatrixSymbol('X', 2, 2)
  45. matrix_set = ConditionSet(X, Eq(X*Matrix([[1, 1], [1, 1]]), X))
  46. Y = Matrix([[0, 0], [0, 0]])
  47. assert matrix_set.contains(Y).doit() is S.true
  48. Z = Matrix([[1, 2], [3, 4]])
  49. assert matrix_set.contains(Z).doit() is S.false
  50. assert isinstance(ConditionSet(x, x < 1, {x, y}).base_set,
  51. FiniteSet)
  52. raises(TypeError, lambda: ConditionSet(x, x + 1, {x, y}))
  53. raises(TypeError, lambda: ConditionSet(x, x, 1))
  54. I = S.Integers
  55. U = S.UniversalSet
  56. C = ConditionSet
  57. assert C(x, False, I) is S.EmptySet
  58. assert C(x, True, I) is I
  59. assert C(x, x < 1, C(x, x < 2, I)
  60. ) == C(x, (x < 1) & (x < 2), I)
  61. assert C(y, y < 1, C(x, y < 2, I)
  62. ) == C(x, (x < 1) & (y < 2), I), C(y, y < 1, C(x, y < 2, I))
  63. assert C(y, y < 1, C(x, x < 2, I)
  64. ) == C(y, (y < 1) & (y < 2), I)
  65. assert C(y, y < 1, C(x, y < x, I)
  66. ) == C(x, (x < 1) & (y < x), I)
  67. assert unchanged(C, y, x < 1, C(x, y < x, I))
  68. assert ConditionSet(x, x < 1).base_set is U
  69. # arg checking is not done at instantiation but this
  70. # will raise an error when containment is tested
  71. assert ConditionSet((x,), x < 1).base_set is U
  72. c = ConditionSet((x, y), x < y, I**2)
  73. assert (1, 2) in c
  74. assert (1, pi) not in c
  75. raises(TypeError, lambda: C(x, x > 1, C((x, y), x > 1, I**2)))
  76. # signature mismatch since only 3 args are accepted
  77. raises(TypeError, lambda: C((x, y), x + y < 2, U, U))
  78. def test_CondSet_intersect():
  79. input_conditionset = ConditionSet(x, x**2 > 4, Interval(1, 4, False,
  80. False))
  81. other_domain = Interval(0, 3, False, False)
  82. output_conditionset = ConditionSet(x, x**2 > 4, Interval(
  83. 1, 3, False, False))
  84. assert Intersection(input_conditionset, other_domain
  85. ) == output_conditionset
  86. def test_issue_9849():
  87. assert ConditionSet(x, Eq(x, x), S.Naturals
  88. ) is S.Naturals
  89. assert ConditionSet(x, Eq(Abs(sin(x)), -1), S.Naturals
  90. ) == S.EmptySet
  91. def test_simplified_FiniteSet_in_CondSet():
  92. assert ConditionSet(x, And(x < 1, x > -3), FiniteSet(0, 1, 2)
  93. ) == FiniteSet(0)
  94. assert ConditionSet(x, x < 0, FiniteSet(0, 1, 2)) == EmptySet
  95. assert ConditionSet(x, And(x < -3), EmptySet) == EmptySet
  96. y = Symbol('y')
  97. assert (ConditionSet(x, And(x > 0), FiniteSet(-1, 0, 1, y)) ==
  98. Union(FiniteSet(1), ConditionSet(x, And(x > 0), FiniteSet(y))))
  99. assert (ConditionSet(x, Eq(Mod(x, 3), 1), FiniteSet(1, 4, 2, y)) ==
  100. Union(FiniteSet(1, 4), ConditionSet(x, Eq(Mod(x, 3), 1),
  101. FiniteSet(y))))
  102. def test_free_symbols():
  103. assert ConditionSet(x, Eq(y, 0), FiniteSet(z)
  104. ).free_symbols == {y, z}
  105. assert ConditionSet(x, Eq(x, 0), FiniteSet(z)
  106. ).free_symbols == {z}
  107. assert ConditionSet(x, Eq(x, 0), FiniteSet(x, z)
  108. ).free_symbols == {x, z}
  109. assert ConditionSet(x, Eq(x, 0), ImageSet(Lambda(y, y**2),
  110. S.Integers)).free_symbols == set()
  111. def test_bound_symbols():
  112. assert ConditionSet(x, Eq(y, 0), FiniteSet(z)
  113. ).bound_symbols == [x]
  114. assert ConditionSet(x, Eq(x, 0), FiniteSet(x, y)
  115. ).bound_symbols == [x]
  116. assert ConditionSet(x, x < 10, ImageSet(Lambda(y, y**2), S.Integers)
  117. ).bound_symbols == [x]
  118. assert ConditionSet(x, x < 10, ConditionSet(y, y > 1, S.Integers)
  119. ).bound_symbols == [x]
  120. def test_as_dummy():
  121. _0, _1 = symbols('_0 _1')
  122. assert ConditionSet(x, x < 1, Interval(y, oo)
  123. ).as_dummy() == ConditionSet(_0, _0 < 1, Interval(y, oo))
  124. assert ConditionSet(x, x < 1, Interval(x, oo)
  125. ).as_dummy() == ConditionSet(_0, _0 < 1, Interval(x, oo))
  126. assert ConditionSet(x, x < 1, ImageSet(Lambda(y, y**2), S.Integers)
  127. ).as_dummy() == ConditionSet(
  128. _0, _0 < 1, ImageSet(Lambda(_0, _0**2), S.Integers))
  129. e = ConditionSet((x, y), x <= y, S.Reals**2)
  130. assert e.bound_symbols == [x, y]
  131. assert e.as_dummy() == ConditionSet((_0, _1), _0 <= _1, S.Reals**2)
  132. assert e.as_dummy() == ConditionSet((y, x), y <= x, S.Reals**2
  133. ).as_dummy()
  134. def test_subs_CondSet():
  135. s = FiniteSet(z, y)
  136. c = ConditionSet(x, x < 2, s)
  137. assert c.subs(x, y) == c
  138. assert c.subs(z, y) == ConditionSet(x, x < 2, FiniteSet(y))
  139. assert c.xreplace({x: y}) == ConditionSet(y, y < 2, s)
  140. assert ConditionSet(x, x < y, s
  141. ).subs(y, w) == ConditionSet(x, x < w, s.subs(y, w))
  142. # if the user uses assumptions that cause the condition
  143. # to evaluate, that can't be helped from SymPy's end
  144. n = Symbol('n', negative=True)
  145. assert ConditionSet(n, 0 < n, S.Integers) is S.EmptySet
  146. p = Symbol('p', positive=True)
  147. assert ConditionSet(n, n < y, S.Integers
  148. ).subs(n, x) == ConditionSet(n, n < y, S.Integers)
  149. raises(ValueError, lambda: ConditionSet(
  150. x + 1, x < 1, S.Integers))
  151. assert ConditionSet(
  152. p, n < x, Interval(-5, 5)).subs(x, p) == Interval(-5, 5), ConditionSet(
  153. p, n < x, Interval(-5, 5)).subs(x, p)
  154. assert ConditionSet(
  155. n, n < x, Interval(-oo, 0)).subs(x, p
  156. ) == Interval(-oo, 0)
  157. assert ConditionSet(f(x), f(x) < 1, {w, z}
  158. ).subs(f(x), y) == ConditionSet(f(x), f(x) < 1, {w, z})
  159. # issue 17341
  160. k = Symbol('k')
  161. img1 = ImageSet(Lambda(k, 2*k*pi + asin(y)), S.Integers)
  162. img2 = ImageSet(Lambda(k, 2*k*pi + asin(S.One/3)), S.Integers)
  163. assert ConditionSet(x, Contains(
  164. y, Interval(-1,1)), img1).subs(y, S.One/3).dummy_eq(img2)
  165. assert (0, 1) in ConditionSet((x, y), x + y < 3, S.Integers**2)
  166. raises(TypeError, lambda: ConditionSet(n, n < -10, Interval(0, 10)))
  167. def test_subs_CondSet_tebr():
  168. with warns_deprecated_sympy():
  169. assert ConditionSet((x, y), {x + 1, x + y}, S.Reals**2) == \
  170. ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Reals**2)
  171. def test_dummy_eq():
  172. C = ConditionSet
  173. I = S.Integers
  174. c = C(x, x < 1, I)
  175. assert c.dummy_eq(C(y, y < 1, I))
  176. assert c.dummy_eq(1) == False
  177. assert c.dummy_eq(C(x, x < 1, S.Reals)) == False
  178. c1 = ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Reals**2)
  179. c2 = ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Reals**2)
  180. c3 = ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Complexes**2)
  181. assert c1.dummy_eq(c2)
  182. assert c1.dummy_eq(c3) is False
  183. assert c.dummy_eq(c1) is False
  184. assert c1.dummy_eq(c) is False
  185. # issue 19496
  186. m = Symbol('m')
  187. n = Symbol('n')
  188. a = Symbol('a')
  189. d1 = ImageSet(Lambda(m, m*pi), S.Integers)
  190. d2 = ImageSet(Lambda(n, n*pi), S.Integers)
  191. c1 = ConditionSet(x, Ne(a, 0), d1)
  192. c2 = ConditionSet(x, Ne(a, 0), d2)
  193. assert c1.dummy_eq(c2)
  194. def test_contains():
  195. assert 6 in ConditionSet(x, x > 5, Interval(1, 7))
  196. assert (8 in ConditionSet(x, y > 5, Interval(1, 7))) is False
  197. # `in` should give True or False; in this case there is not
  198. # enough information for that result
  199. raises(TypeError,
  200. lambda: 6 in ConditionSet(x, y > 5, Interval(1, 7)))
  201. # here, there is enough information but the comparison is
  202. # not defined
  203. raises(TypeError, lambda: 0 in ConditionSet(x, 1/x >= 0, S.Reals))
  204. assert ConditionSet(x, y > 5, Interval(1, 7)
  205. ).contains(6) == (y > 5)
  206. assert ConditionSet(x, y > 5, Interval(1, 7)
  207. ).contains(8) is S.false
  208. assert ConditionSet(x, y > 5, Interval(1, 7)
  209. ).contains(w) == And(Contains(w, Interval(1, 7)), y > 5)
  210. # This returns an unevaluated Contains object
  211. # because 1/0 should not be defined for 1 and 0 in the context of
  212. # reals.
  213. assert ConditionSet(x, 1/x >= 0, S.Reals).contains(0) == \
  214. Contains(0, ConditionSet(x, 1/x >= 0, S.Reals), evaluate=False)
  215. c = ConditionSet((x, y), x + y > 1, S.Integers**2)
  216. assert not c.contains(1)
  217. assert c.contains((2, 1))
  218. assert not c.contains((0, 1))
  219. c = ConditionSet((w, (x, y)), w + x + y > 1, S.Integers*S.Integers**2)
  220. assert not c.contains(1)
  221. assert not c.contains((1, 2))
  222. assert not c.contains(((1, 2), 3))
  223. assert not c.contains(((1, 2), (3, 4)))
  224. assert c.contains((1, (3, 4)))
  225. def test_as_relational():
  226. assert ConditionSet((x, y), x > 1, S.Integers**2).as_relational((x, y)
  227. ) == (x > 1) & Contains((x, y), S.Integers**2)
  228. assert ConditionSet(x, x > 1, S.Integers).as_relational(x
  229. ) == Contains(x, S.Integers) & (x > 1)
  230. def test_flatten():
  231. """Tests whether there is basic denesting functionality"""
  232. inner = ConditionSet(x, sin(x) + x > 0)
  233. outer = ConditionSet(x, Contains(x, inner), S.Reals)
  234. assert outer == ConditionSet(x, sin(x) + x > 0, S.Reals)
  235. inner = ConditionSet(y, sin(y) + y > 0)
  236. outer = ConditionSet(x, Contains(y, inner), S.Reals)
  237. assert outer != ConditionSet(x, sin(x) + x > 0, S.Reals)
  238. inner = ConditionSet(x, sin(x) + x > 0).intersect(Interval(-1, 1))
  239. outer = ConditionSet(x, Contains(x, inner), S.Reals)
  240. assert outer == ConditionSet(x, sin(x) + x > 0, Interval(-1, 1))
  241. def test_duplicate():
  242. from sympy.core.function import BadSignatureError
  243. # test coverage for line 95 in conditionset.py, check for duplicates in symbols
  244. dup = symbols('a,a')
  245. raises(BadSignatureError, lambda: ConditionSet(dup, x < 0))
  246. def test_SetKind_ConditionSet():
  247. assert ConditionSet(x, Eq(sin(x), 0), Interval(0, 2*pi)).kind is SetKind(NumberKind)
  248. assert ConditionSet(x, x < 0).kind is SetKind(NumberKind)