forked from diofant/diofant
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sets.py
1938 lines (1463 loc) · 53.1 KB
/
sets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Generic set theory interfaces."""
import itertools
import typing
from ..core import Basic, Eq, Expr, Mul, S, nan, oo, zoo
from ..core.compatibility import iterable
from ..core.decorators import _sympifyit
from ..core.evalf import EvalfMixin
from ..core.evaluate import global_evaluate
from ..core.singleton import Singleton
from ..core.sympify import sympify
from ..logic import And, Not, Or, false, true
from ..utilities import ordered, subsets
from .contains import Contains
class Set(Basic):
"""
The base class for any kind of set.
This is not meant to be used directly as a container of items. It does not
behave like the builtin ``set``; see :class:`FiniteSet` for that.
Real intervals are represented by the :class:`Interval` class and unions of
sets by the :class:`Union` class. The empty set is represented by the
:class:`EmptySet` class and available as a singleton as ``S.EmptySet``.
"""
is_number = False
is_iterable = False
is_interval = False
is_FiniteSet = False
is_Interval = False
is_ProductSet = False
is_Union = False
is_Intersection: typing.Optional[bool] = None
is_EmptySet: typing.Optional[bool] = None
is_UniversalSet: typing.Optional[bool] = None
is_Complement: typing.Optional[bool] = None
is_SymmetricDifference: typing.Optional[bool] = None
@staticmethod
def _infimum_key(expr):
"""Return infimum (if possible) else oo."""
try:
infimum = expr.inf
assert infimum.is_comparable
except (NotImplementedError,
AttributeError, AssertionError, ValueError):
infimum = oo
return infimum
def union(self, other):
"""
Returns the union of 'self' and 'other'.
Examples
========
As a shortcut it is possible to use the '+' operator:
>>> Interval(0, 1).union(Interval(2, 3))
[0, 1] U [2, 3]
>>> Interval(0, 1) + Interval(2, 3)
[0, 1] U [2, 3]
>>> Interval(1, 2, True, True) + FiniteSet(2, 3)
(1, 2] U {3}
Similarly it is possible to use the '-' operator for set differences:
>>> Interval(0, 2) - Interval(0, 1)
(1, 2]
>>> Interval(1, 3) - FiniteSet(2)
[1, 2) U (2, 3]
"""
return Union(self, other)
def intersection(self, other):
"""
Returns the intersection of 'self' and 'other'.
>>> Interval(1, 3).intersection(Interval(1, 2))
[1, 2]
"""
return Intersection(self, other)
def _intersection(self, other):
"""
This function should only be used internally
self._intersection(other) returns a new, intersected set if self knows how
to intersect itself with other, otherwise it returns ``None``
When making a new set class you can be assured that other will not
be a :class:`Union`, :class:`FiniteSet`, or :class:`EmptySet`
Used within the :class:`Intersection` class
"""
return
def is_disjoint(self, other):
"""
Returns True if 'self' and 'other' are disjoint
Examples
========
>>> Interval(0, 2).is_disjoint(Interval(1, 2))
False
>>> Interval(0, 2).is_disjoint(Interval(3, 4))
True
References
==========
* https://en.wikipedia.org/wiki/Disjoint_sets
"""
return self.intersection(other) == S.EmptySet
def isdisjoint(self, other):
"""Alias for :meth:`is_disjoint()`."""
return self.is_disjoint(other)
def _union(self, other):
"""
This function should only be used internally
self._union(other) returns a new, joined set if self knows how
to join itself with other, otherwise it returns ``None``.
It may also return a python set of Diofant Sets if they are somehow
simpler. If it does this it must be idempotent i.e. the sets returned
must return ``None`` with _union'ed with each other
Used within the :class:`Union` class
"""
return
def complement(self, universe):
r"""
The complement of 'self' w.r.t the given the universe.
Examples
========
>>> Interval(0, 1).complement(S.Reals)
(-oo, 0) U (1, oo)
>>> Interval(0, 1).complement(S.UniversalSet)
UniversalSet() \ [0, 1]
"""
return Complement(universe, self)
def _complement(self, other):
# this behaves as other - self
if other.is_subset(self):
return S.EmptySet
elif isinstance(other, ProductSet):
# For each set consider it or it's complement
# We need at least one of the sets to be complemented
# Consider all 2^n combinations.
# We can conveniently represent these options easily using a
# ProductSet
# XXX: this doesn't work if the dimentions of the sets isn't same.
# A - B is essentially same as A if B has a different
# dimensionality than A
switch_sets = ProductSet(FiniteSet(o, o - s) for s, o in
zip(self.sets, other.sets))
product_sets = (ProductSet(*set) for set in switch_sets)
# Union of all combinations but this one
return Union(p for p in product_sets if p != other)
elif isinstance(other, Interval):
if isinstance(self, (FiniteSet, Interval)):
return Intersection(other, self.complement(S.Reals))
elif isinstance(other, Union):
return Union(o - self for o in other.args)
elif isinstance(other, Complement):
return Complement(other.args[0], Union(other.args[1], self), evaluate=False)
elif isinstance(other, FiniteSet):
unks = FiniteSet(*[el for el in other
if self.contains(el) not in [true, false]])
other = FiniteSet(*[el for el in other
if self.contains(el) != true])
ret = FiniteSet(*[el for el in other if self.contains(el) == false])
if unks:
ret |= Complement(FiniteSet(*unks), self, evaluate=False)
return ret
def symmetric_difference(self, other):
"""
Returns symmetric difference of ``self`` and ``other``.
Examples
========
>>> Interval(1, 3).symmetric_difference(Reals)
(-oo, 1) U (3, oo)
References
==========
* https://en.wikipedia.org/wiki/Symmetric_difference
"""
return SymmetricDifference(self, other)
def _symmetric_difference(self, other):
return Union(Complement(self, other), Complement(other, self))
@property
def inf(self):
"""
The infimum of 'self'
Examples
========
>>> Interval(0, 1).inf
0
>>> Union(Interval(0, 1), Interval(2, 3)).inf
0
"""
raise NotImplementedError(f'({self}).inf')
@property
def sup(self):
"""
The supremum of 'self'
Examples
========
>>> Interval(0, 1).sup
1
>>> Union(Interval(0, 1), Interval(2, 3)).sup
3
"""
raise NotImplementedError(f'({self}).sup')
@_sympifyit('other', false)
def contains(self, other):
"""
Returns True if 'other' is contained in 'self' as an element.
As a shortcut it is possible to use the 'in' operator:
Examples
========
>>> Interval(0, 1).contains(0.5)
true
>>> 0.5 in Interval(0, 1)
True
"""
ret = self._contains(other)
if ret is None:
ret = Contains(other, self, evaluate=False)
return ret
def _contains(self, other):
raise NotImplementedError(f'({self})._contains({other})')
def is_subset(self, other):
"""
Returns True if 'self' is a subset of 'other'.
Examples
========
>>> Interval(0, 0.5).is_subset(Interval(0, 1))
True
>>> Interval(0, 1).is_subset(Interval(0, 1, left_open=True))
False
"""
if isinstance(other, Set):
return self.intersection(other) == self
else:
raise ValueError(f"Unknown argument '{other}'")
def issubset(self, other):
"""Alias for :meth:`is_subset()`."""
return self.is_subset(other)
def is_proper_subset(self, other):
"""
Returns True if 'self' is a proper subset of 'other'.
Examples
========
>>> Interval(0, 0.5).is_proper_subset(Interval(0, 1))
True
>>> Interval(0, 1).is_proper_subset(Interval(0, 1))
False
"""
if isinstance(other, Set):
return self != other and self.is_subset(other)
else:
raise ValueError(f"Unknown argument '{other}'")
def is_superset(self, other):
"""
Returns True if 'self' is a superset of 'other'.
Examples
========
>>> Interval(0, 0.5).is_superset(Interval(0, 1))
False
>>> Interval(0, 1).is_superset(Interval(0, 1, left_open=True))
True
"""
if isinstance(other, Set):
return other.is_subset(self)
else:
raise ValueError(f"Unknown argument '{other}'")
def issuperset(self, other):
"""Alias for :meth:`is_superset()`."""
return self.is_superset(other)
def is_proper_superset(self, other):
"""
Returns True if 'self' is a proper superset of 'other'.
Examples
========
>>> Interval(0, 1).is_proper_superset(Interval(0, 0.5))
True
>>> Interval(0, 1).is_proper_superset(Interval(0, 1))
False
"""
if isinstance(other, Set):
return self != other and self.is_superset(other)
else:
raise ValueError(f"Unknown argument '{other}'")
def _eval_powerset(self):
raise NotImplementedError(f'Power set not defined for: {self.func}')
def powerset(self):
"""
Find the Power set of 'self'.
Examples
========
>>> A = EmptySet()
>>> A.powerset()
{EmptySet()}
>>> A = FiniteSet(1, 2)
>>> a, b, c = FiniteSet(1), FiniteSet(2), FiniteSet(1, 2)
>>> A.powerset() == FiniteSet(a, b, c, EmptySet())
True
References
==========
* https://en.wikipedia.org/wiki/Power_set
"""
return self._eval_powerset()
@property
def measure(self):
"""
The (Lebesgue) measure of 'self'
Examples
========
>>> Interval(0, 1).measure
1
>>> Union(Interval(0, 1), Interval(2, 3)).measure
2
"""
raise NotImplementedError(f'({self}).measure')
@property
def boundary(self):
"""
The boundary or frontier of a set
A point x is on the boundary of a set S if
1. x is in the closure of S.
I.e. Every neighborhood of x contains a point in S.
2. x is not in the interior of S.
I.e. There does not exist an open set centered on x contained
entirely within S.
There are the points on the outer rim of S. If S is open then these
points need not actually be contained within S.
For example, the boundary of an interval is its start and end points.
This is true regardless of whether or not the interval is open.
Examples
========
>>> Interval(0, 1).boundary
{0, 1}
>>> Interval(0, 1, True, False).boundary
{0, 1}
"""
raise NotImplementedError(f'({self}).boundary')
@property
def is_open(self):
"""
Test if a set is open.
A set is open if it has an empty intersection with its boundary.
Examples
========
>>> S.Reals.is_open
True
See Also
========
boundary
"""
return not Intersection(self, self.boundary)
@property
def is_closed(self):
"""
Test if a set is closed.
Examples
========
>>> Interval(0, 1).is_closed
True
"""
return self.boundary.is_subset(self)
@property
def closure(self):
"""
Return the closure of a set.
Examples
========
>>> Interval(0, 1, right_open=True).closure
[0, 1]
"""
return self + self.boundary
@property
def interior(self):
"""
Return the interior of a set.
The interior of a set consists all points of a set that do not
belong to its boundary.
Examples
========
>>> Interval(0, 1).interior
(0, 1)
>>> Interval(0, 1).boundary.interior
EmptySet()
"""
return self - self.boundary
def _eval_imageset(self, f):
from .fancysets import ImageSet
return ImageSet(f, self)
def __add__(self, other):
return self.union(other)
def __or__(self, other):
return self.union(other)
def __and__(self, other):
return self.intersection(other)
def __mul__(self, other):
return ProductSet(self, other)
def __xor__(self, other):
return SymmetricDifference(self, other)
def __pow__(self, exp):
if not sympify(exp).is_Integer or exp < 0:
raise ValueError(f'{exp}: Exponent must be a positive Integer')
return ProductSet([self]*exp)
def __sub__(self, other):
return Complement(self, other)
def __contains__(self, other):
symb = self.contains(other)
if symb not in (true, false):
raise TypeError(f'contains did not evaluate to a bool: {symb!r}')
return bool(symb)
class ProductSet(Set):
"""
Represents a Cartesian Product of Sets.
Returns a Cartesian product given several sets as either an iterable
or individual arguments.
Can use '*' operator on any sets for convenient shorthand.
Examples
========
>>> I = Interval(0, 5)
>>> S = FiniteSet(1, 2, 3)
>>> ProductSet(I, S)
[0, 5] x {1, 2, 3}
>>> (2, 2) in ProductSet(I, S)
True
>>> Interval(0, 1) * Interval(0, 1) # The unit square
[0, 1] x [0, 1]
>>> H, T = Symbol('H'), Symbol('T')
>>> coin = FiniteSet(H, T)
>>> set(coin**2)
{(H, H), (H, T), (T, H), (T, T)}
Notes
=====
- Passes most operations down to the argument sets
- Flattens Products of ProductSets
References
==========
* https://en.wikipedia.org/wiki/Cartesian_product
"""
is_ProductSet = True
def __new__(cls, *sets, **assumptions):
def flatten(arg):
if isinstance(arg, Set):
if arg.is_ProductSet:
return sum(map(flatten, arg.args), [])
else:
return [arg]
elif iterable(arg):
return sum(map(flatten, arg), [])
raise TypeError('Input must be Sets or iterables of Sets')
sets = flatten(list(sets))
if EmptySet() in sets or len(sets) == 0:
return EmptySet()
if len(sets) == 1:
return sets[0]
return Basic.__new__(cls, *sets, **assumptions)
def _eval_Eq(self, other):
if not other.is_ProductSet:
return
if len(self.args) != len(other.args):
return false
return And(*(Eq(x, y) for x, y in zip(self.args, other.args)))
def _contains(self, other):
"""
'in' operator for ProductSets
Examples
========
>>> (2, 3) in Interval(0, 5) * Interval(0, 5)
True
>>> (10, 10) in Interval(0, 5) * Interval(0, 5)
False
Passes operation on to constituent sets
"""
try:
if len(other) != len(self.args):
return false
except TypeError: # maybe element isn't an iterable
return false
return And(*[s.contains(i) for s, i in zip(self.sets, other)])
def _intersection(self, other):
"""
This function should only be used internally
See Set._intersection for docstring
"""
if not other.is_ProductSet:
return
if len(other.args) != len(self.args):
return S.EmptySet
return ProductSet(a.intersection(b) for a, b in zip(self.sets, other.sets))
def _union(self, other):
if not other.is_ProductSet:
return
if len(other.args) != len(self.args):
return
if self.args[0] == other.args[0]:
return self.args[0] * Union(ProductSet(self.args[1:]),
ProductSet(other.args[1:]))
if self.args[-1] == other.args[-1]:
return Union(ProductSet(self.args[:-1]),
ProductSet(other.args[:-1])) * self.args[-1]
@property
def sets(self):
return self.args
@property
def boundary(self):
return Union(ProductSet(b + b.boundary if i != j else b.boundary
for j, b in enumerate(self.sets))
for i, a in enumerate(self.sets))
@property
def is_iterable(self):
return all(set.is_iterable for set in self.sets)
def __iter__(self):
from ..utilities.iterables import cantor_product
if self.is_iterable:
return cantor_product(*self.sets)
else:
raise TypeError('Not all constituent sets are iterable')
@property
def measure(self):
measure = 1
for set in self.sets:
measure *= set.measure
return measure
def __len__(self):
return int(Mul(*[len(s) for s in self.args]))
class Interval(Set, EvalfMixin):
"""
Represents a real interval as a Set.
Returns an interval with end points "start" and "end".
For left_open=True (default left_open is False) the interval
will be open on the left. Similarly, for right_open=True the interval
will be open on the right.
Examples
========
>>> Interval(0, 1)
[0, 1]
>>> Interval(0, 1, False, True)
[0, 1)
>>> Interval.Ropen(0, 1)
[0, 1)
>>> Interval.Lopen(0, 1)
(0, 1]
>>> Interval.open(0, 1)
(0, 1)
>>> a = Symbol('a', real=True)
>>> Interval(0, a)
[0, a]
Notes
=====
- Only real end points are supported
- Interval(a, b) with a > b will return the empty set
- Use the evalf() method to turn an Interval into an mpmath
'mpi' interval instance
References
==========
* https://en.wikipedia.org/wiki/Interval_%28mathematics%29
"""
is_Interval = True
def __new__(cls, start=-oo, end=oo, left_open=False, right_open=False):
start = sympify(start, strict=True)
end = sympify(end, strict=True)
left_open = sympify(left_open, strict=True)
right_open = sympify(right_open, strict=True)
if not all(isinstance(a, (type(true), type(false)))
for a in [left_open, right_open]):
raise NotImplementedError(
'left_open and right_open can have only true/false values, '
f'got {left_open} and {right_open}')
if not all(i.is_extended_real is not False for i in (start, end)):
raise ValueError('Non-real intervals are not supported')
if (end - start).is_negative:
return S.EmptySet
is_open = left_open or right_open
if end == start and is_open:
return S.EmptySet
if end == start and not is_open:
return FiniteSet(end)
return Basic.__new__(cls, start, end, left_open, right_open)
@property
def start(self):
"""
The left end point of 'self'.
This property takes the same value as the 'inf' property.
Examples
========
>>> Interval(0, 1).start
0
"""
return self.args[0]
inf = left = start
@classmethod
def open(cls, a, b):
"""Return an interval including neither boundary."""
return cls(a, b, True, True)
@classmethod
def Lopen(cls, a, b):
"""Return an interval not including the left boundary."""
return cls(a, b, True, False)
@classmethod
def Ropen(cls, a, b):
"""Return an interval not including the right boundary."""
return cls(a, b, False, True)
@property
def end(self):
"""
The right end point of 'self'.
This property takes the same value as the 'sup' property.
Examples
========
>>> Interval(0, 1).end
1
"""
return self.args[1]
sup = right = end
@property
def left_open(self):
"""
True if 'self' is left-open.
Examples
========
>>> Interval(0, 1, left_open=True).left_open
true
>>> Interval(0, 1, left_open=False).left_open
false
"""
return self.args[2]
@property
def right_open(self):
"""
True if 'self' is right-open.
Examples
========
>>> Interval(0, 1, right_open=True).right_open
true
>>> Interval(0, 1, right_open=False).right_open
false
"""
return self.args[3]
def _intersection(self, other):
"""
This function should only be used internally
See Set._intersection for docstring
"""
# We only know how to intersect with other intervals
if not other.is_Interval:
return
# handle unbounded self
if Eq(self, S.Reals) == true and all((abs(_) < oo) is true or
abs(_) == oo
for _ in other.boundary):
if other.is_left_unbounded and not other.left_open:
other = Interval(other.start, other.end, True, other.right_open)
if other.is_right_unbounded and not other.right_open:
other = Interval(other.start, other.end, other.left_open, True)
return other
elif Eq(self, S.ExtendedReals) == true:
return other
# We can't intersect [0,3] with [x,6] -- we don't know if x>0 or x<0
if not self._is_comparable(other):
return
empty = False
if self.start <= other.end and other.start <= self.end:
# Get topology right.
if self.start < other.start:
start = other.start
left_open = other.left_open
elif self.start > other.start:
start = self.start
left_open = self.left_open
else:
start = self.start
left_open = self.left_open or other.left_open
if self.end < other.end:
end = self.end
right_open = self.right_open
elif self.end > other.end:
end = other.end
right_open = other.right_open
else:
end = self.end
right_open = self.right_open or other.right_open
if end - start == 0 and (left_open or right_open):
empty = True
else:
empty = True
if empty:
return S.EmptySet
return Interval(start, end, left_open, right_open)
def _complement(self, other):
if other in (S.Reals, S.ExtendedReals):
a = Interval(-oo, self.start,
other.left_open, not self.left_open)
b = Interval(self.end, oo, not self.right_open, other.right_open)
return Union(a, b)
return Set._complement(self, other)
def _union(self, other):
"""
This function should only be used internally
See Set._union for docstring
"""
if other.is_UniversalSet:
return S.UniversalSet
if other.is_Interval and self._is_comparable(other):
from ..functions import Max, Min
# Non-overlapping intervals
end = Min(self.end, other.end)
start = Max(self.start, other.start)
if (end < start or
(end == start and (end not in self and end not in other))):
return
else:
start = Min(self.start, other.start)
end = Max(self.end, other.end)
left_open = ((self.start != start or self.left_open) and
(other.start != start or other.left_open))
right_open = ((self.end != end or self.right_open) and
(other.end != end or other.right_open))
return Interval(start, end, left_open, right_open)
# If I have open end points and these endpoints are contained in other
if ((self.left_open and other.contains(self.start) is true) or
(self.right_open and other.contains(self.end) is true)):
# Fill in my end points and return
open_left = self.left_open and self.start not in other
open_right = self.right_open and self.end not in other
new_self = Interval(self.start, self.end, open_left, open_right)
return {new_self, other}
@property
def boundary(self):
return FiniteSet(*(p for p, c in [(self.start, not self.left_open),
(self.end, not self.right_open)]
if abs(p) != oo or c))
def _contains(self, other):
if not isinstance(other, Expr) or other in (nan, zoo):
return false
if other.is_extended_real is False:
return false
if self.left_open:
expr = other > self.start
else:
expr = other >= self.start
if other == self.start:
return true
if self.right_open:
expr = And(expr, other < self.end)
else:
expr = And(expr, other <= self.end)
if other == self.end:
return true
return sympify(expr, strict=True)
def _eval_imageset(self, f):
from ..calculus.singularities import singularities
from ..core import Lambda, diff
from ..functions import Max, Min
from ..series import limit
from ..solvers import solve
# TODO: handle functions with infinitely many solutions (eg, sin, tan)
# TODO: handle multivariate functions
expr = f.expr
if len(expr.free_symbols) > 1 or len(f.variables) != 1:
return
var = f.variables[0]
if expr.is_Piecewise:
result = S.EmptySet
domain_set = self
for (p_expr, p_cond) in expr.args:
if p_cond == true:
intrvl = domain_set