infinity.py 3.0 KB

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