Spaces:
Sleeping
Sleeping
File size: 4,225 Bytes
6a86ad5 |
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 |
from sympy.core.singleton import S
from sympy.core.sympify import sympify
from sympy.functions.elementary.miscellaneous import Min, Max
from sympy.sets.sets import (EmptySet, FiniteSet, Intersection,
Interval, ProductSet, Set, Union, UniversalSet)
from sympy.sets.fancysets import (ComplexRegion, Naturals, Naturals0,
Integers, Rationals, Reals)
from sympy.multipledispatch import Dispatcher
union_sets = Dispatcher('union_sets')
@union_sets.register(Naturals0, Naturals)
def _(a, b):
return a
@union_sets.register(Rationals, Naturals)
def _(a, b):
return a
@union_sets.register(Rationals, Naturals0)
def _(a, b):
return a
@union_sets.register(Reals, Naturals)
def _(a, b):
return a
@union_sets.register(Reals, Naturals0)
def _(a, b):
return a
@union_sets.register(Reals, Rationals)
def _(a, b):
return a
@union_sets.register(Integers, Set)
def _(a, b):
intersect = Intersection(a, b)
if intersect == a:
return b
elif intersect == b:
return a
@union_sets.register(ComplexRegion, Set)
def _(a, b):
if b.is_subset(S.Reals):
# treat a subset of reals as a complex region
b = ComplexRegion.from_real(b)
if b.is_ComplexRegion:
# a in rectangular form
if (not a.polar) and (not b.polar):
return ComplexRegion(Union(a.sets, b.sets))
# a in polar form
elif a.polar and b.polar:
return ComplexRegion(Union(a.sets, b.sets), polar=True)
return None
@union_sets.register(EmptySet, Set)
def _(a, b):
return b
@union_sets.register(UniversalSet, Set)
def _(a, b):
return a
@union_sets.register(ProductSet, ProductSet)
def _(a, b):
if b.is_subset(a):
return a
if len(b.sets) != len(a.sets):
return None
if len(a.sets) == 2:
a1, a2 = a.sets
b1, b2 = b.sets
if a1 == b1:
return a1 * Union(a2, b2)
if a2 == b2:
return Union(a1, b1) * a2
return None
@union_sets.register(ProductSet, Set)
def _(a, b):
if b.is_subset(a):
return a
return None
@union_sets.register(Interval, Interval)
def _(a, b):
if a._is_comparable(b):
# Non-overlapping intervals
end = Min(a.end, b.end)
start = Max(a.start, b.start)
if (end < start or
(end == start and (end not in a and end not in b))):
return None
else:
start = Min(a.start, b.start)
end = Max(a.end, b.end)
left_open = ((a.start != start or a.left_open) and
(b.start != start or b.left_open))
right_open = ((a.end != end or a.right_open) and
(b.end != end or b.right_open))
return Interval(start, end, left_open, right_open)
@union_sets.register(Interval, UniversalSet)
def _(a, b):
return S.UniversalSet
@union_sets.register(Interval, Set)
def _(a, b):
# If I have open end points and these endpoints are contained in b
# But only in case, when endpoints are finite. Because
# interval does not contain oo or -oo.
open_left_in_b_and_finite = (a.left_open and
sympify(b.contains(a.start)) is S.true and
a.start.is_finite)
open_right_in_b_and_finite = (a.right_open and
sympify(b.contains(a.end)) is S.true and
a.end.is_finite)
if open_left_in_b_and_finite or open_right_in_b_and_finite:
# Fill in my end points and return
open_left = a.left_open and a.start not in b
open_right = a.right_open and a.end not in b
new_a = Interval(a.start, a.end, open_left, open_right)
return {new_a, b}
return None
@union_sets.register(FiniteSet, FiniteSet)
def _(a, b):
return FiniteSet(*(a._elements | b._elements))
@union_sets.register(FiniteSet, Set)
def _(a, b):
# If `b` set contains one of my elements, remove it from `a`
if any(b.contains(x) == True for x in a):
return {
FiniteSet(*[x for x in a if b.contains(x) != True]), b}
return None
@union_sets.register(Set, Set)
def _(a, b):
return None
|