infinity.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  2. ## ##
  3. # infinity.py
  4. # --------------------------------
  5. # Copyright (c) 2005
  6. # Jean-Sébastien BOLDUC
  7. # Hans Vangheluwe
  8. # McGill University (Montréal)
  9. # --------------------------------
  10. #
  11. # - Singleton class "Inf" and unique instance "INFINITY" ---
  12. # stands for infinity (to use in time advance function)
  13. #
  14. ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  15. ## ##
  16. ## INFINITY OBJECT --- ADDED 04/04/2005
  17. ## more comparison operators -- HV 12/11/2006
  18. ##
  19. ## mul and rmul added -- Eugene 14/11/2006
  20. ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  21. ## ##
  22. class Infinity:
  23. """Singleton class: the single instance "INFINITY" stands for infinity."""
  24. def __new__(cls):
  25. if not hasattr(cls, "instance"):
  26. cls.instance = super(Infinity, cls).__new__(cls)
  27. return cls.instance
  28. def __deepcopy__(self, memo):
  29. return self
  30. def __add__(self, other):
  31. """ INFINITY + x = INFINITY """
  32. return self
  33. def __sub__(self, other):
  34. """ INFINITY - x = INFINITY (if x != INF), or NaN (if x == INFINITY) """
  35. if other == self:
  36. raise ValueError("INFINITY - INFINITY gives NaN (not defined)")
  37. return self
  38. def __mul__(self, other):
  39. """ INFINITY * x = INFINITY """
  40. return self
  41. def __radd__(self, other):
  42. """ x + INFINITY = INFINITY """
  43. return self
  44. def __rsub__(self, other):
  45. """ x - INFINITY = -INFINITY (if x != INFINITY), or NaN (if x == INFINITY) """
  46. if other == self:
  47. raise ValueError("INFINITY - INFINITY gives NaN (not defined)")
  48. raise ValueError("x - INFINITY gives MINUS_INFINITY (not defined)")
  49. def __rmul__(self, other):
  50. """ x * INFINITY = INFINITY """
  51. return self
  52. def __abs__(self):
  53. """ abs(INFINITY) = INFINITY -- absolute value """
  54. return self
  55. # def __cmp__(self, other):
  56. # if other is self:
  57. # return 0
  58. # else:
  59. # return 1
  60. def __eq__(self, other):
  61. if other is self:
  62. return True
  63. else:
  64. return False
  65. def __ne__(self, other):
  66. if other is self:
  67. return False
  68. else:
  69. return True
  70. def __lt__(self, other):
  71. return False
  72. def __le__(self, other):
  73. if other is self:
  74. return True
  75. else:
  76. return False
  77. def __gt__(self, other):
  78. if other is self:
  79. return False
  80. else:
  81. return True
  82. def __ge__(self, other):
  83. return True
  84. def __str__(self):
  85. return "+INFINITY"
  86. # Instantiate singleton:
  87. INFINITY = Infinity()