equality.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. """
  2. Module for mathematical equality [1] and inequalities [2].
  3. The purpose of this module is to provide the instances which represent the
  4. binary predicates in order to combine the relationals into logical inference
  5. system. Objects such as ``Q.eq``, ``Q.lt`` should remain internal to
  6. assumptions module, and user must use the classes such as :obj:`~.Eq()`,
  7. :obj:`~.Lt()` instead to construct the relational expressions.
  8. References
  9. ==========
  10. .. [1] https://en.wikipedia.org/wiki/Equality_(mathematics)
  11. .. [2] https://en.wikipedia.org/wiki/Inequality_(mathematics)
  12. """
  13. from sympy.assumptions import Q
  14. from sympy.core.relational import is_eq, is_neq, is_gt, is_ge, is_lt, is_le
  15. from .binrel import BinaryRelation
  16. __all__ = ['EqualityPredicate', 'UnequalityPredicate', 'StrictGreaterThanPredicate',
  17. 'GreaterThanPredicate', 'StrictLessThanPredicate', 'LessThanPredicate']
  18. class EqualityPredicate(BinaryRelation):
  19. """
  20. Binary predicate for $=$.
  21. The purpose of this class is to provide the instance which represent
  22. the equality predicate in order to allow the logical inference.
  23. This class must remain internal to assumptions module and user must
  24. use :obj:`~.Eq()` instead to construct the equality expression.
  25. Evaluating this predicate to ``True`` or ``False`` is done by
  26. :func:`~.core.relational.is_eq()`
  27. Examples
  28. ========
  29. >>> from sympy import ask, Q
  30. >>> Q.eq(0, 0)
  31. Q.eq(0, 0)
  32. >>> ask(_)
  33. True
  34. See Also
  35. ========
  36. sympy.core.relational.Eq
  37. """
  38. is_reflexive = True
  39. is_symmetric = True
  40. name = 'eq'
  41. handler = None # Do not allow dispatching by this predicate
  42. @property
  43. def negated(self):
  44. return Q.ne
  45. def eval(self, args, assumptions=True):
  46. if assumptions == True:
  47. # default assumptions for is_eq is None
  48. assumptions = None
  49. return is_eq(*args, assumptions)
  50. class UnequalityPredicate(BinaryRelation):
  51. r"""
  52. Binary predicate for $\neq$.
  53. The purpose of this class is to provide the instance which represent
  54. the inequation predicate in order to allow the logical inference.
  55. This class must remain internal to assumptions module and user must
  56. use :obj:`~.Ne()` instead to construct the inequation expression.
  57. Evaluating this predicate to ``True`` or ``False`` is done by
  58. :func:`~.core.relational.is_neq()`
  59. Examples
  60. ========
  61. >>> from sympy import ask, Q
  62. >>> Q.ne(0, 0)
  63. Q.ne(0, 0)
  64. >>> ask(_)
  65. False
  66. See Also
  67. ========
  68. sympy.core.relational.Ne
  69. """
  70. is_reflexive = False
  71. is_symmetric = True
  72. name = 'ne'
  73. handler = None
  74. @property
  75. def negated(self):
  76. return Q.eq
  77. def eval(self, args, assumptions=True):
  78. if assumptions == True:
  79. # default assumptions for is_neq is None
  80. assumptions = None
  81. return is_neq(*args, assumptions)
  82. class StrictGreaterThanPredicate(BinaryRelation):
  83. """
  84. Binary predicate for $>$.
  85. The purpose of this class is to provide the instance which represent
  86. the ">" predicate in order to allow the logical inference.
  87. This class must remain internal to assumptions module and user must
  88. use :obj:`~.Gt()` instead to construct the equality expression.
  89. Evaluating this predicate to ``True`` or ``False`` is done by
  90. :func:`~.core.relational.is_gt()`
  91. Examples
  92. ========
  93. >>> from sympy import ask, Q
  94. >>> Q.gt(0, 0)
  95. Q.gt(0, 0)
  96. >>> ask(_)
  97. False
  98. See Also
  99. ========
  100. sympy.core.relational.Gt
  101. """
  102. is_reflexive = False
  103. is_symmetric = False
  104. name = 'gt'
  105. handler = None
  106. @property
  107. def reversed(self):
  108. return Q.lt
  109. @property
  110. def negated(self):
  111. return Q.le
  112. def eval(self, args, assumptions=True):
  113. if assumptions == True:
  114. # default assumptions for is_gt is None
  115. assumptions = None
  116. return is_gt(*args, assumptions)
  117. class GreaterThanPredicate(BinaryRelation):
  118. """
  119. Binary predicate for $>=$.
  120. The purpose of this class is to provide the instance which represent
  121. the ">=" predicate in order to allow the logical inference.
  122. This class must remain internal to assumptions module and user must
  123. use :obj:`~.Ge()` instead to construct the equality expression.
  124. Evaluating this predicate to ``True`` or ``False`` is done by
  125. :func:`~.core.relational.is_ge()`
  126. Examples
  127. ========
  128. >>> from sympy import ask, Q
  129. >>> Q.ge(0, 0)
  130. Q.ge(0, 0)
  131. >>> ask(_)
  132. True
  133. See Also
  134. ========
  135. sympy.core.relational.Ge
  136. """
  137. is_reflexive = True
  138. is_symmetric = False
  139. name = 'ge'
  140. handler = None
  141. @property
  142. def reversed(self):
  143. return Q.le
  144. @property
  145. def negated(self):
  146. return Q.lt
  147. def eval(self, args, assumptions=True):
  148. if assumptions == True:
  149. # default assumptions for is_ge is None
  150. assumptions = None
  151. return is_ge(*args, assumptions)
  152. class StrictLessThanPredicate(BinaryRelation):
  153. """
  154. Binary predicate for $<$.
  155. The purpose of this class is to provide the instance which represent
  156. the "<" predicate in order to allow the logical inference.
  157. This class must remain internal to assumptions module and user must
  158. use :obj:`~.Lt()` instead to construct the equality expression.
  159. Evaluating this predicate to ``True`` or ``False`` is done by
  160. :func:`~.core.relational.is_lt()`
  161. Examples
  162. ========
  163. >>> from sympy import ask, Q
  164. >>> Q.lt(0, 0)
  165. Q.lt(0, 0)
  166. >>> ask(_)
  167. False
  168. See Also
  169. ========
  170. sympy.core.relational.Lt
  171. """
  172. is_reflexive = False
  173. is_symmetric = False
  174. name = 'lt'
  175. handler = None
  176. @property
  177. def reversed(self):
  178. return Q.gt
  179. @property
  180. def negated(self):
  181. return Q.ge
  182. def eval(self, args, assumptions=True):
  183. if assumptions == True:
  184. # default assumptions for is_lt is None
  185. assumptions = None
  186. return is_lt(*args, assumptions)
  187. class LessThanPredicate(BinaryRelation):
  188. """
  189. Binary predicate for $<=$.
  190. The purpose of this class is to provide the instance which represent
  191. the "<=" predicate in order to allow the logical inference.
  192. This class must remain internal to assumptions module and user must
  193. use :obj:`~.Le()` instead to construct the equality expression.
  194. Evaluating this predicate to ``True`` or ``False`` is done by
  195. :func:`~.core.relational.is_le()`
  196. Examples
  197. ========
  198. >>> from sympy import ask, Q
  199. >>> Q.le(0, 0)
  200. Q.le(0, 0)
  201. >>> ask(_)
  202. True
  203. See Also
  204. ========
  205. sympy.core.relational.Le
  206. """
  207. is_reflexive = True
  208. is_symmetric = False
  209. name = 'le'
  210. handler = None
  211. @property
  212. def reversed(self):
  213. return Q.ge
  214. @property
  215. def negated(self):
  216. return Q.gt
  217. def eval(self, args, assumptions=True):
  218. if assumptions == True:
  219. # default assumptions for is_le is None
  220. assumptions = None
  221. return is_le(*args, assumptions)