Completed
Push — master ( bd259b...7b4bdc )
by Christophe
9s
created

ImpreciseFloat.isdisjoint()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
1
"""
2
This module one type useful in :class:`Context` values:
3
4
* :class:`ImpreciseFloat` for representing imprecise numbers as intervals.
5
"""
6
7
import math
8
9
10
class ImpreciseFloat(object):
11
    """
12
    The :class:`ImpreciseFloat` class ca be used to represent intervals on the real line.
13
    """
14
15
    __slots__ = ['_inf', '_sup']
16
17
    def __init__(self, **kwargs):
18
        """
19
        Initialise an :class:`ImpreciseFloat`.
20
21
        Keyword arguments
22
        -----------------
23
            inf : :class:`float`
24
                the lower limit of the interval
25
                (default to :attr:`-math.inf <python:math.inf>`)
26
            sup : :class:`float`
27
                the upper limit of the interval
28
                (default to :attr:`math.inf <python:math.inf>`)
29
30
        .. versionadded:: 0.0.2
31
        """
32
33
        if 'inf' in kwargs:
34
            self._inf = float(kwargs['inf'])
35
        else:
36
            self._inf = -math.inf
37
38
        if 'sup' in kwargs:
39
            self._sup = float(kwargs['sup'])
40
        else:
41
            self._sup = math.inf
42
43
        if self._inf > self._sup:
44
            self._inf = math.inf
45
            self._sup = -math.inf
46
47
    @property
48
    def inf(self) -> float:
49
        """
50
        Get the lower limit.
51
52
        Returns
53
        -------
54
            :class:`float`
55
                the lower limit
56
57
        .. versionadded:: 0.0.2
58
        """
59
        return self._inf
60
61
    @property
62
    def sup(self) -> float:
63
        """
64
        Get the upper limit.
65
66
        Returns
67
        -------
68
            :class:`float`
69
                the upper limit
70
71
        .. versionadded:: 0.0.2
72
        """
73
        return self._sup
74
75
    def __le__(self, other: 'ImpreciseFloat') -> bool:
76
        """
77
        Test if the imprecise float is included (or equal) to the other.
78
79
        Parameters
80
        ----------
81
            other : :class:`ImpreciseFloat`
82
                the other imprecise float
83
84
        Returns
85
        -------
86
            :class:`bool`
87
                True if the imprecise float is included in the other
88
89
        .. versionadded:: 0.0.2
90
        """
91
        return self._inf >= other.inf and self._sup <= other.sup
92
93
    def __lt__(self, other: 'ImpreciseFloat') -> bool:
94
        """
95
        Test if the imprecise float is strictly included to the other.
96
97
        Parameters
98
        ----------
99
            other : :class:`ImpreciseFloat`
100
                the other imprecise float
101
102
        Returns
103
        -------
104
            :class:`bool`
105
                True if the imprecise float is strictly included in the other
106
107
        .. versionadded:: 0.0.2
108
        """
109
        return self <= other and self != other
110
111
    def __eq__(self, other: 'ImpreciseFloat') -> bool:
112
        """
113
        Test if the imprecise float is equal to the other.
114
115
        Parameters
116
        ----------
117
            other : :class:`ImpreciseFloat`
118
                the other imprecise float
119
120
        Returns
121
        -------
122
            :class:`bool`
123
                True if the imprecise float is equal to the other
124
125
        .. versionadded:: 0.0.2
126
        """
127
        return self._inf == other.inf and self._sup == other.sup
128
129
    def __ne__(self, other: 'ImpreciseFloat') -> bool:
130
        """
131
        Test if the imprecise float is not equal to the other.
132
133
        Parameters
134
        ----------
135
            other : :class:`ImpreciseFloat`
136
                the other imprecise float
137
138
        Returns
139
        -------
140
            :class:`bool`
141
                True if the imprecise float is not equal to the other
142
143
        .. versionadded:: 0.0.2
144
        """
145
        return not self == other
146
147
    def __ge__(self, other: 'ImpreciseFloat') -> bool:
148
        """
149
        Test if the imprecise float includes or is equal to the other.
150
151
        Parameters
152
        ----------
153
            other : :class:`ImpreciseFloat`
154
                the other imprecise float
155
156
        Returns
157
        -------
158
            :class:`bool`
159
                True if the imprecise float includes or is equal to the other
160
161
        .. versionadded:: 0.0.2
162
        """
163
        return self._inf <= other.inf and self._sup >= other.sup
164
165
    def __gt__(self, other: 'ImpreciseFloat') -> bool:
166
        """
167
        Test if the imprecise float strictly includes the other.
168
169
        Parameters
170
        ----------
171
            other : :class:`ImpreciseFloat`
172
                the other imprecise float
173
174
        Returns
175
        -------
176
            :class:`bool`
177
                True if the imprecise float strictly includes the other
178
179
        .. versionadded:: 0.0.2
180
        """
181
        return self >= other and self != other
182
183
    def __and__(self, other: 'ImpreciseFloat') -> 'ImpreciseFloat':
184
        """
185
        Compute the intersection of the the imprecise float with the other.
186
187
        Parameters
188
        ----------
189
            other : :class:`ImpreciseFloat`
190
                the other imprecise float
191
192
        Returns
193
        -------
194
            :class:`ImpreciseFloat`
195
                the intersection of the the imprecise float with the other
196
197
        .. versionadded:: 0.0.2
198
        """
199
        return ImpreciseFloat(inf=max(self._inf, other.inf), sup=min(self._sup, other.sup))
200
201
    def __or__(self, other: 'ImpreciseFloat') -> 'ImpreciseFloat':
202
        """
203
        Compute the extension of the the imprecise float with the other.
204
205
        Parameters
206
        ----------
207
            other : :class:`ImpreciseFloat`
208
                the other imprecise float
209
210
        Returns
211
        -------
212
            :class:`ImpreciseFloat`
213
                the extension of the the imprecise float with the other
214
215
        .. versionadded:: 0.0.2
216
        """
217
        return ImpreciseFloat(inf=min(self._inf, other.inf), sup=max(self._sup, other.sup))
218
219
    def isdisjoint(self, other: 'ImpreciseFloat') -> bool:
220
        """
221
        Return True if the imprecise float has no elements in common with the other.
222
        Imprecise floats are disjoint if and only if their intersection is the empty
223
        imprecise float.
224
225
        Parameters
226
        ----------
227
            other : :class:`ImpreciseFloat`
228
                the other imprecise float
229
230
        Returns
231
        -------
232
            :class:`bool`
233
                True if the imprecise float is disjoint from the other
234
235
        .. versionadded:: 0.0.2
236
        """
237
        return self._sup < other.inf or self._inf > other.sup
238
239
    def issubset(self, other: 'ImpreciseFloat') -> bool:
240
        """
241
        Test if the imprecise float is included (or equal) to the other.
242
243
        Parameters
244
        ----------
245
            other : :class:`ImpreciseFloat`
246
247
        Returns
248
        -------
249
            :class:`bool`
250
                True if this imprecise float is included or equal to the other
251
252
        .. versionadded:: 0.0.2
253
        """
254
        return self <= other
255
256
    def issuperset(self, other: 'ImpreciseFloat') -> bool:
257
        """
258
        Test if the imprecise float includes (or is equal to) the other.
259
260
        Parameters
261
        ----------
262
            other : :class:`ImpreciseFloat`
263
264
        Returns
265
        -------
266
            :class:`bool`
267
                True if this imprecise float includes (or is equal to) the other
268
269
        .. versionadded:: 0.0.2
270
        """
271
        return self >= other
272
273
    def union(self, *others) -> 'ImpreciseFloat':
274
        """
275
        Compute the union between this imprecise float and the others.
276
277
        Parameters
278
        ----------
279
            *others
280
                Variable length argument list
281
282
        Returns
283
        -------
284
            :class:`ImpreciseFloat`
285
                the union between this imprecise float and the others
286
287
        .. versionadded:: 0.0.2
288
        """
289
        result = self
290
        for other in others:
291
            result = result | other
292
        return result
293
294
    def intersection(self, *others) -> 'ImpreciseFloat':
295
        """
296
        Compute the intersection between this imprecise float and the others.
297
298
        Parameters
299
        ----------
300
            *others
301
                Variable length argument list
302
303
        Returns
304
        -------
305
            :class:`ImpreciseFloat`
306
                the intersection between this imprecise float and the others
307
308
        .. versionadded:: 0.0.2
309
        """
310
        result = self
311
        for other in others:
312
            result = result & other
313
        return result
314
315
    def __repr__(self) -> str:
316
        """
317
        Convert this imprecise float to a representable string.
318
319
        Returns
320
        -------
321
            :class:`str`
322
                the user friendly representable string of this imprecise float
323
324
        .. versionadded:: 0.0.2
325
        """
326
        return '%s(inf=%s, sup=%s)' % (type(self).__name__, repr(self._inf), repr(self._sup))
327
328
    def __str__(self) -> str:
329
        """
330
        Convert this imprecise float to a representable string.
331
332
        Returns
333
        -------
334
            :class:`str`
335
                the user friendly representable string of this imprecise float
336
337
        .. versionadded:: 0.0.2
338
        """
339
        return '[%s:%s]' % (repr(self._inf), repr(self._sup))
340