def diameter(self): """Returns the distance between supremum and infimum of the interval Some examples: >>> rounding_mode_backup = rounding.get_mode() >>> IReal(-10, 1).diameter() 11.0 >>> IReal().diameter() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... >>> IReal("undefined").diameter() # doctest: +ELLIPSIS Traceback (most recent call last): ... UndefinedIntervalError:... >>> rounding_mode_backup == rounding.get_mode() True """ if self.empty: raise EmptyIntervalError() if self.undefined: raise UndefinedIntervalError() rounding_mode_backup = rounding.get_mode() rounding.set_mode(1) ret = self.sup - self.inf rounding.set_mode(rounding_mode_backup) return ret
def middle(self): """Returns the middle point of the interval Some examples: >>> rounding_mode_backup = rounding.get_mode() >>> IReal(-10, 5).middle() -2.5 >>> IReal().middle() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... >>> IReal("undefined").middle() # doctest: +ELLIPSIS Traceback (most recent call last): ... UndefinedIntervalError:... >>> rounding_mode_backup == rounding.get_mode() True """ if self.empty: raise EmptyIntervalError() if self.undefined: raise UndefinedIntervalError() rounding_mode_backup = rounding.get_mode() rounding.set_mode(1) ret = (self.inf + self.sup) / 2.0 rounding.set_mode(rounding_mode_backup) return ret
def __le__(self, other): """Less Than Or Equal relation order operator Some examples: >>> IReal(2, 3) <= IReal(2.5) False >>> IReal(2, 3) <= IReal(3.1) True >>> IReal(3) <= IReal(3) True >>> IReal("undefined") <= IReal(3.1) False >>> IReal(3) <= IReal() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError: this order relation can't be applied... >>> IReal() <= IReal() True """ if self.empty and other.empty: return True if self.empty or other.empty: raise EmptyIntervalError("this order relation can't be applied " "to an empty and a non-empty interval") if self.undefined or other.undefined: return False return self.inf <= other.inf and self.sup <= other.sup
def __mul__(self, other): """Multiplication operator Some examples: >>> rounding_mode_backup = rounding.get_mode() >>> IReal(0.25, 0.5) * IReal(2, 3) [0.5, 1.5] >>> IReal(-0.75, 0.75) * 2 [-1.5, 1.5] >>> x = IReal("0.1") * "0.1" >>> x.inf < x.sup and str(x.inf) == str(x.sup) True >>> IReal("undefined") * 2 undefined interval >>> IReal(2) * IReal() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... >>> rounding_mode_backup == rounding.get_mode() True """ if type(other) != type(self): other = IReal(other) if self.empty or other.empty: raise EmptyIntervalError() x1, y1, x2, y2 = self.inf, self.sup, other.inf, other.sup rounding_mode_backup = rounding.get_mode() rounding.set_mode(-1) inf = min(x1*x2, x1*y2, y1*x2, y1*y2) rounding.set_mode(1) sup = max(x1*x2, x1*y2, y1*x2, y1*y2) rounding.set_mode(rounding_mode_backup) return IReal(inf, sup)
def __sub__(self, other): """Binary minus operator Some examples: >>> rounding_mode_backup = rounding.get_mode() >>> IReal(0.25, 0.5) - IReal(2) [-1.75, -1.5] >>> IReal(-0.75, 0.75) - 2 [-2.75, -1.25] >>> x = IReal("0.1") - "0.1" >>> x.inf < x.sup and str(-x.inf) == str(x.sup) True >>> IReal("undefined") + 2 undefined interval >>> IReal(2) + IReal() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... >>> rounding_mode_backup == rounding.get_mode() True """ if type(other) != type(self): other = IReal(other) if self.empty or other.empty: raise EmptyIntervalError() rounding_mode_backup = rounding.get_mode() rounding.set_mode(-1) inf = self.inf - other.sup rounding.set_mode(1) sup = self.sup - other.inf rounding.set_mode(rounding_mode_backup) return IReal(inf, sup)
def __invert__(self): """Inversion operator Some examples: >>> rounding_mode_backup = rounding.get_mode() >>> ~IReal(0.25, 0.5) [2.0, 4.0] >>> x = ~IReal(0.1); x.inf < x.sup and str(x.inf) == str(x.sup) True >>> ~IReal("undefined") undefined interval >>> ~IReal(-2, 2) undefined interval >>> ~IReal() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... >>> rounding_mode_backup == rounding.get_mode() True """ if self.empty: raise EmptyIntervalError() if 0.0 in self: return IReal("undefined") rounding_mode_backup = rounding.get_mode() rounding.set_mode(-1) inf = 1.0 / self.sup rounding.set_mode(1) sup = 1.0 / self.inf rounding.set_mode(rounding_mode_backup) return IReal(inf, sup)
def __neg__(self): """Unary minus operator Some examples: >>> -IReal("-25/100", 0.5) [-0.5, 0.25] >>> -IReal("undefined") undefined interval >>> -IReal() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... """ if self.empty: raise EmptyIntervalError() return IReal(-self.sup, -self.inf)
def __pos__(self): """Unary plus operator Some examples: >>> +IReal("25/100", 0.5) [0.25, 0.5] >>> +IReal("undefined") undefined interval >>> +IReal() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... """ if self.empty: raise EmptyIntervalError() return self
def __lt__(self, other): """Less Than relation order operator Some examples: >>> IReal(2, 3) < IReal(2.5) False >>> IReal(2, 3) < IReal(3.1) True >>> IReal("undefined") < IReal(3.1) False >>> IReal(3) < IReal() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... """ if self.empty or other.empty: raise EmptyIntervalError() if self.undefined or other.undefined: return False return self.sup < other.inf
def distance(self, other): """Returns the Hausdorff distance of the interval Some examples: >>> rounding_mode_backup = rounding.get_mode() >>> IReal(-10, 5).distance(IReal(10)) 20.0 >>> IReal(-10, 5).distance(IReal(10, 50)) 45.0 >>> x = IReal(-10, 5); x.distance(x) 0.0 >>> IReal(-1).distance(IReal()) # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... >>> IReal().distance(IReal(-1)) # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... >>> IReal("undefined").distance(IReal(12)) # doctest: +ELLIPSIS Traceback (most recent call last): ... UndefinedIntervalError:... >>> IReal(10).distance(IReal("undefined")) # doctest: +ELLIPSIS Traceback (most recent call last): ... UndefinedIntervalError:... >>> rounding_mode_backup == rounding.get_mode() True """ if self.empty or other.empty: raise EmptyIntervalError() if self.undefined or other.undefined: raise UndefinedIntervalError() rounding_mode_backup = rounding.get_mode() rounding.set_mode(1) ret = max(abs(self.inf-other.inf), abs(self.sup-other.sup)) rounding.set_mode(rounding_mode_backup) return ret
def __div__(self, other): """Division operator Some examples: >>> rounding_mode_backup = rounding.get_mode() >>> IReal(0.25, 0.5) / IReal(2, 4) [0.0625, 0.25] >>> IReal(-0.75, 0.75) / 2 [-0.375, 0.375] >>> x = IReal("0.1") / "0.1" >>> x.inf < x.sup and str(x.inf) == str(x.sup) True >>> IReal(1) / IReal(-2, 2) undefined interval >>> IReal("undefined") / 2 undefined interval >>> IReal(2) / IReal() # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... >>> rounding_mode_backup == rounding.get_mode() True """ if type(other) != type(self): other = IReal(other) if self.empty or other.empty: raise EmptyIntervalError() if 0.0 in other: return IReal("undefined") x1, y1, x2, y2 = self.inf, self.sup, other.inf, other.sup rounding_mode_backup = rounding.get_mode() rounding.set_mode(-1) inf = min(x1/x2, x1/y2, y1/x2, y1/y2) rounding.set_mode(1) sup = max(x1/x2, x1/y2, y1/x2, y1/y2) rounding.set_mode(rounding_mode_backup) return IReal(inf, sup)
def __abs__(self): """Calculates the absolute value of the interval Some examples: >>> abs(IReal(-1, 1)) 1.0 >>> abs(IReal(0.25, 1)) 1.0 >>> abs(IReal()) # doctest: +ELLIPSIS Traceback (most recent call last): ... EmptyIntervalError:... >>> abs(IReal("undefined")) # doctest: +ELLIPSIS Traceback (most recent call last): ... UndefinedIntervalError:... """ if self.empty: raise EmptyIntervalError() if self.undefined: raise UndefinedIntervalError() return max(abs(self.inf), abs(self.sup))