Coverage for /usr/lib/python3/dist-packages/sympy/sets/setexpr.py: 65%
63 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1from sympy.core import Expr
2from sympy.core.decorators import call_highest_priority, _sympifyit
3from .fancysets import ImageSet
4from .sets import set_add, set_sub, set_mul, set_div, set_pow, set_function
7class SetExpr(Expr):
8 """An expression that can take on values of a set.
10 Examples
11 ========
13 >>> from sympy import Interval, FiniteSet
14 >>> from sympy.sets.setexpr import SetExpr
16 >>> a = SetExpr(Interval(0, 5))
17 >>> b = SetExpr(FiniteSet(1, 10))
18 >>> (a + b).set
19 Union(Interval(1, 6), Interval(10, 15))
20 >>> (2*a + b).set
21 Interval(1, 20)
22 """
23 _op_priority = 11.0
25 def __new__(cls, setarg):
26 return Expr.__new__(cls, setarg)
28 set = property(lambda self: self.args[0])
30 def _latex(self, printer):
31 return r"SetExpr\left({}\right)".format(printer._print(self.set))
33 @_sympifyit('other', NotImplemented)
34 @call_highest_priority('__radd__')
35 def __add__(self, other):
36 return _setexpr_apply_operation(set_add, self, other)
38 @_sympifyit('other', NotImplemented)
39 @call_highest_priority('__add__')
40 def __radd__(self, other):
41 return _setexpr_apply_operation(set_add, other, self)
43 @_sympifyit('other', NotImplemented)
44 @call_highest_priority('__rmul__')
45 def __mul__(self, other):
46 return _setexpr_apply_operation(set_mul, self, other)
48 @_sympifyit('other', NotImplemented)
49 @call_highest_priority('__mul__')
50 def __rmul__(self, other):
51 return _setexpr_apply_operation(set_mul, other, self)
53 @_sympifyit('other', NotImplemented)
54 @call_highest_priority('__rsub__')
55 def __sub__(self, other):
56 return _setexpr_apply_operation(set_sub, self, other)
58 @_sympifyit('other', NotImplemented)
59 @call_highest_priority('__sub__')
60 def __rsub__(self, other):
61 return _setexpr_apply_operation(set_sub, other, self)
63 @_sympifyit('other', NotImplemented)
64 @call_highest_priority('__rpow__')
65 def __pow__(self, other):
66 return _setexpr_apply_operation(set_pow, self, other)
68 @_sympifyit('other', NotImplemented)
69 @call_highest_priority('__pow__')
70 def __rpow__(self, other):
71 return _setexpr_apply_operation(set_pow, other, self)
73 @_sympifyit('other', NotImplemented)
74 @call_highest_priority('__rtruediv__')
75 def __truediv__(self, other):
76 return _setexpr_apply_operation(set_div, self, other)
78 @_sympifyit('other', NotImplemented)
79 @call_highest_priority('__truediv__')
80 def __rtruediv__(self, other):
81 return _setexpr_apply_operation(set_div, other, self)
83 def _eval_func(self, func):
84 # TODO: this could be implemented straight into `imageset`:
85 res = set_function(func, self.set)
86 if res is None:
87 return SetExpr(ImageSet(func, self.set))
88 return SetExpr(res)
91def _setexpr_apply_operation(op, x, y):
92 if isinstance(x, SetExpr):
93 x = x.set
94 if isinstance(y, SetExpr):
95 y = y.set
96 out = op(x, y)
97 return SetExpr(out)