m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
11 KiB

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