add.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from sympy.core.numbers import oo, Infinity, NegativeInfinity
  2. from sympy.core.singleton import S
  3. from sympy.core import Basic, Expr
  4. from sympy.multipledispatch import Dispatcher
  5. from sympy.sets import Interval, FiniteSet
  6. # XXX: The functions in this module are clearly not tested and are broken in a
  7. # number of ways.
  8. _set_add = Dispatcher('_set_add')
  9. _set_sub = Dispatcher('_set_sub')
  10. @_set_add.register(Basic, Basic)
  11. def _(x, y):
  12. return None
  13. @_set_add.register(Expr, Expr)
  14. def _(x, y):
  15. return x+y
  16. @_set_add.register(Interval, Interval)
  17. def _(x, y):
  18. """
  19. Additions in interval arithmetic
  20. https://en.wikipedia.org/wiki/Interval_arithmetic
  21. """
  22. return Interval(x.start + y.start, x.end + y.end,
  23. x.left_open or y.left_open, x.right_open or y.right_open)
  24. @_set_add.register(Interval, Infinity)
  25. def _(x, y):
  26. if x.start is S.NegativeInfinity:
  27. return Interval(-oo, oo)
  28. return FiniteSet({S.Infinity})
  29. @_set_add.register(Interval, NegativeInfinity)
  30. def _(x, y):
  31. if x.end is S.Infinity:
  32. return Interval(-oo, oo)
  33. return FiniteSet({S.NegativeInfinity})
  34. @_set_sub.register(Basic, Basic)
  35. def _(x, y):
  36. return None
  37. @_set_sub.register(Expr, Expr)
  38. def _(x, y):
  39. return x-y
  40. @_set_sub.register(Interval, Interval)
  41. def _(x, y):
  42. """
  43. Subtractions in interval arithmetic
  44. https://en.wikipedia.org/wiki/Interval_arithmetic
  45. """
  46. return Interval(x.start - y.end, x.end - y.start,
  47. x.left_open or y.right_open, x.right_open or y.left_open)
  48. @_set_sub.register(Interval, Infinity)
  49. def _(x, y):
  50. if x.start is S.NegativeInfinity:
  51. return Interval(-oo, oo)
  52. return FiniteSet(-oo)
  53. @_set_sub.register(Interval, NegativeInfinity)
  54. def _(x, y):
  55. if x.start is S.NegativeInfinity:
  56. return Interval(-oo, oo)
  57. return FiniteSet(-oo)