infinity.py 2.6 KB

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