11"""
2- This module implements the interval arithmetics
2+ Interval arithmetics
33"""
44
5+ from typing import Union
56import numpy as np
67
8+ from ..core import NUMERICAL_TOLERANCE
9+
710__all__ = ["Interval" , "IntervalUnion" , "sqrt" ]
811
912
10- def sqrt (obj ):
13+ def sqrt (obj ) -> "Interval | IntervalUnion | float" :
1114 """
1215 Square root of an interval or interval union
1316
@@ -28,7 +31,7 @@ class Interval:
2831 The interval abstraction
2932 """
3033
31- def __init__ (self , lower_bound , upper_bound ) :
34+ def __init__ (self , lower_bound : float , upper_bound : float ) -> None :
3235 """
3336 Constructor of the interval
3437
@@ -56,7 +59,7 @@ def to_tuple(self) -> tuple:
5659 """
5760 return (self .lower_bound , self .upper_bound )
5861
59- def contains (self , value ) -> bool :
62+ def contains (self , value : float ) -> bool :
6063 """
6164 Check if the interval contains the value
6265
@@ -68,7 +71,7 @@ def contains(self, value) -> bool:
6871 """
6972 return bool (self .lower_bound <= value <= self .upper_bound )
7073
71- def intersection (self , other ) :
74+ def intersection (self , other : "Interval | IntervalUnion" ) -> "Interval" :
7275 """
7376 Returns the intersection of two intervals
7477
@@ -110,7 +113,7 @@ def integer(self) -> bool:
110113
111114 return bool (np .ceil (self .lower_bound ) == np .floor (self .upper_bound ))
112115
113- def shrink_to_integers (self ):
116+ def shrink_to_integers (self ) -> "Interval" :
114117 """
115118 Shrinks the interval to integers
116119
@@ -140,7 +143,7 @@ def is_empty(self) -> bool:
140143 """
141144 return bool (self .upper_bound < self .lower_bound )
142145
143- def __add__ (self , other ) :
146+ def __add__ (self , other : Union [ int , float , "Interval" ]) -> Union [ "Interval" , "IntervalUnion" ] :
144147 """
145148 The addition operator
146149
@@ -164,7 +167,7 @@ def __add__(self, other):
164167 upper_bound = self .upper_bound + other .upper_bound ,
165168 )
166169
167- def __radd__ (self , other ) :
170+ def __radd__ (self , other : Union [ int , float , "Interval" ]) -> Union [ "Interval" , "IntervalUnion" ] :
168171 """
169172 The right hand addition
170173
@@ -176,7 +179,7 @@ def __radd__(self, other):
176179 """
177180 return self + other
178181
179- def __sub__ (self , other ) :
182+ def __sub__ (self , other : Union [ int , float , "Interval" ]) -> Union [ "Interval" , "IntervalUnion" ] :
180183 """
181184 The subtraction operator
182185
@@ -200,7 +203,7 @@ def __sub__(self, other):
200203 upper_bound = self .upper_bound - other .lower_bound ,
201204 )
202205
203- def __rsub__ (self , other ) :
206+ def __rsub__ (self , other : Union [ int , float , "Interval" ]) -> Union [ "Interval" , "IntervalUnion" ] :
204207 """
205208 The right hand subtraction
206209
@@ -212,7 +215,7 @@ def __rsub__(self, other):
212215 """
213216 return (- 1 ) * self + other
214217
215- def __mul__ (self , other ) :
218+ def __mul__ (self , other : Union [ int , float , "Interval" ]) -> Union [ "Interval" , "IntervalUnion" ] :
216219 """
217220 The multiplication operator
218221
@@ -238,7 +241,7 @@ def __mul__(self, other):
238241
239242 return Interval (min (term0 , term1 , term2 , term3 ), max (term0 , term1 , term2 , term3 ))
240243
241- def __rmul__ (self , other ) :
244+ def __rmul__ (self , other : Union [ int , float , "Interval" ]) -> Union [ "Interval" , "IntervalUnion" ] :
242245 """
243246 The right hand multiplication operator
244247
@@ -250,7 +253,9 @@ def __rmul__(self, other):
250253 """
251254 return self .__mul__ (other )
252255
253- def __truediv__ (self , other ):
256+ def __truediv__ (
257+ self , other : Union [int , float , "Interval" ]
258+ ) -> Union ["Interval" , "IntervalUnion" ]:
254259 """
255260 The division operator
256261
@@ -287,7 +292,9 @@ def __truediv__(self, other):
287292
288293 return IntervalUnion ([self * res_0 , self * res_1 ])
289294
290- def __rtruediv__ (self , other ):
295+ def __rtruediv__ (
296+ self , other : Union [int , float , "Interval" ]
297+ ) -> Union ["Interval" , "IntervalUnion" ]:
291298 """
292299 The right hand division operator
293300
@@ -337,7 +344,7 @@ def __ne__(self, other) -> bool:
337344 """
338345 return not self .__eq__ (other )
339346
340- def __neg__ (self ):
347+ def __neg__ (self ) -> "Interval" :
341348 """
342349 The negation operator
343350
@@ -346,7 +353,7 @@ def __neg__(self):
346353 """
347354 return (- 1 ) * self
348355
349- def __pow__ (self , other ) :
356+ def __pow__ (self , other : Union [ int , float ]) -> "Interval" :
350357 """
351358 The power operation on the interval
352359
@@ -374,7 +381,7 @@ def __pow__(self, other):
374381
375382 return res
376383
377- def representing_int (self ):
384+ def representing_int (self ) -> Union [ int , None ] :
378385 """
379386 Returns a representative integer
380387
@@ -383,7 +390,7 @@ def representing_int(self):
383390 """
384391 shrunk = self .shrink_to_integers ()
385392 if not shrunk .is_empty ():
386- return shrunk .lower_bound
393+ return int ( shrunk .lower_bound )
387394 return None
388395
389396
@@ -392,7 +399,7 @@ class IntervalUnion:
392399 The interval union abstraction
393400 """
394401
395- def __init__ (self , intervals ):
402+ def __init__ (self , intervals : Union [ "Interval" , tuple , list ] ):
396403 """
397404 Constructor of the object
398405
@@ -425,7 +432,7 @@ def to_tuple(self) -> list:
425432 """
426433 return [interval .to_tuple () for interval in self .intervals ]
427434
428- def simplify (self ):
435+ def simplify (self ) -> None :
429436 """
430437 Simplify the union of intervals
431438
0 commit comments