contains.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from sympy.core import S
  2. from sympy.core.relational import Eq, Ne
  3. from sympy.logic.boolalg import BooleanFunction
  4. from sympy.utilities.misc import func_name
  5. from .sets import Set
  6. class Contains(BooleanFunction):
  7. """
  8. Asserts that x is an element of the set S.
  9. Examples
  10. ========
  11. >>> from sympy import Symbol, Integer, S, Contains
  12. >>> Contains(Integer(2), S.Integers)
  13. True
  14. >>> Contains(Integer(-2), S.Naturals)
  15. False
  16. >>> i = Symbol('i', integer=True)
  17. >>> Contains(i, S.Naturals)
  18. Contains(i, Naturals)
  19. References
  20. ==========
  21. .. [1] https://en.wikipedia.org/wiki/Element_%28mathematics%29
  22. """
  23. @classmethod
  24. def eval(cls, x, s):
  25. if not isinstance(s, Set):
  26. raise TypeError('expecting Set, not %s' % func_name(s))
  27. ret = s.contains(x)
  28. if not isinstance(ret, Contains) and (
  29. ret in (S.true, S.false) or isinstance(ret, Set)):
  30. return ret
  31. @property
  32. def binary_symbols(self):
  33. return set().union(*[i.binary_symbols
  34. for i in self.args[1].args
  35. if i.is_Boolean or i.is_Symbol or
  36. isinstance(i, (Eq, Ne))])
  37. def as_set(self):
  38. raise NotImplementedError()