def test_FiniteSet(): x = Symbol("x") A = FiniteSet(1, 2, 3) B = FiniteSet(3, 4, 5) AorB = Union(A, B) AandB = A.intersection(B) assert AandB == FiniteSet(3) assert FiniteSet(EmptySet()) != EmptySet() assert FiniteSet(FiniteSet(1, 2, 3)) != FiniteSet(1, 2, 3)
def test_sets(): x = Integer(2) y = Integer(3) x1 = sympy.Integer(2) y1 = sympy.Integer(3) assert Interval(x, y) == Interval(x1, y1) assert Interval(x1, y) == Interval(x1, y1) assert Interval(x, y)._sympy_() == sympy.Interval(x1, y1) assert sympify(sympy.Interval(x1, y1)) == Interval(x, y) assert sympify(sympy.EmptySet()) == EmptySet() assert sympy.EmptySet() == EmptySet()._sympy_() assert FiniteSet(x, y) == FiniteSet(x1, y1) assert FiniteSet(x1, y) == FiniteSet(x1, y1) assert FiniteSet(x, y)._sympy_() == sympy.FiniteSet(x1, y1) assert sympify(sympy.FiniteSet(x1, y1)) == FiniteSet(x, y) x = Interval(1, 2) y = Interval(2, 3) x1 = sympy.Interval(1, 2) y1 = sympy.Interval(2, 3) assert Union(x, y) == Union(x1, y1) assert Union(x1, y) == Union(x1, y1) assert Union(x, y)._sympy_() == sympy.Union(x1, y1) assert sympify(sympy.Union(x1, y1)) == Union(x, y) assert Complement(x, y) == Complement(x1, y1) assert Complement(x1, y) == Complement(x1, y1) assert Complement(x, y)._sympy_() == sympy.Complement(x1, y1) assert sympify(sympy.Complement(x1, y1)) == Complement(x, y)
def _print_Piecewise(self, expr): # Filter out default zeros since they are implicit in a TDB filtered_args = [(x, cond) for x, cond in zip(*[iter(expr.args)] * 2) if not ((cond == S.true) and (x == S.Zero))] exprs = [self._stringify_expr(x) for x, cond in filtered_args] # Only a small subset of piecewise functions can be represented # Need to verify that each cond's highlim equals the next cond's lowlim # to_interval() is used because symengine does not implement as_set() intervals = [to_interval(cond) for x, cond in filtered_args] intersected_intervals = UniversalSet() for i in intervals: intersected_intervals = intersected_intervals.intersection(i) if (len(intervals) > 1) and (intersected_intervals != EmptySet): raise ValueError( 'Overlapping intervals cannot be represented: {}'.format( intervals)) continuous_interval = Interval(intervals[0].args[0], intervals[-1].args[1], False, True) # XXX: Wait, should this be the intersection or the union of continuous_interval? if Union(*intervals).union(continuous_interval) != continuous_interval: raise ValueError('Piecewise intervals must be continuous') if not all([cond.free_symbols == {v.T} for x, cond in filtered_args]): raise ValueError( 'Only temperature-dependent piecewise conditions are supported' ) # Sort expressions based on intervals sortindices = [ i[0] for i in sorted(enumerate(intervals), key=lambda x: x[1].args[0]) ] exprs = [exprs[idx] for idx in sortindices] # Infinity is implicit in TDB, so we shouldn't print it; ',' means use default value as_str = lambda x: ',' if (x == S.Infinity) or (x == S.NegativeInfinity ) else str(x) if len(exprs) > 1: result = '{1} {0}; {2} Y'.format(exprs[0], as_str(intervals[0].args[0]), as_str(intervals[0].args[1])) result += 'Y'.join([ ' {0}; {1} '.format(expr, as_str(i.args[1])) for i, expr in zip(intervals[1:], exprs[1:]) ]) result += 'N' else: result = '{0} {1}; {2} N'.format(as_str(intervals[0].args[0]), exprs[0], as_str(intervals[0].args[1])) return result
def to_interval(relational): if isinstance(relational, And): result = UniversalSet() for i in relational.args: result = result.intersection(to_interval(i)) return result elif isinstance(relational, Or): return Union(*[to_interval(i) for i in relational.args]) elif isinstance(relational, Not): return Complement(*[to_interval(i) for i in relational.args]) if relational == S.true: return Interval(S.NegativeInfinity, S.Infinity, left_open=True, right_open=True) if len(relational.free_symbols) != 1: raise ValueError('Relational must only have one free symbol') if len(relational.args) != 2: raise ValueError('Relational must only have two arguments') free_symbol = list(relational.free_symbols)[0] lhs = relational.args[0] rhs = relational.args[1] if isinstance(relational, LessThan): if rhs == free_symbol: return Interval(lhs, S.Infinity, left_open=False, right_open=True) else: return Interval(S.NegativeInfinity, rhs, left_open=True, right_open=False) elif isinstance(relational, StrictLessThan): if rhs == free_symbol: return Interval(lhs, S.Infinity, left_open=True, right_open=False) else: return Interval(S.NegativeInfinity, rhs, left_open=False, right_open=True) else: raise ValueError('Unsupported Relational: {}'.format( relational.__class__.__name__))
def test_sets(): x = Integer(2) y = Integer(3) x1 = sympy.Integer(2) y1 = sympy.Integer(3) assert Interval(x, y) == Interval(x1, y1) assert Interval(x1, y) == Interval(x1, y1) assert Interval(x, y)._sympy_() == sympy.Interval(x1, y1) assert sympify(sympy.Interval(x1, y1)) == Interval(x, y) assert sympify(sympy.S.EmptySet) == EmptySet() assert sympy.S.EmptySet == EmptySet()._sympy_() assert sympify(sympy.S.UniversalSet) == UniversalSet() assert sympy.S.UniversalSet == UniversalSet()._sympy_() assert sympify(sympy.S.Reals) == Reals() assert sympy.S.Reals == Reals()._sympy_() assert sympify(sympy.S.Rationals) == Rationals() assert sympy.S.Rationals == Rationals()._sympy_() assert sympify(sympy.S.Integers) == Integers() assert sympy.S.Integers == Integers()._sympy_() assert FiniteSet(x, y) == FiniteSet(x1, y1) assert FiniteSet(x1, y) == FiniteSet(x1, y1) assert FiniteSet(x, y)._sympy_() == sympy.FiniteSet(x1, y1) assert sympify(sympy.FiniteSet(x1, y1)) == FiniteSet(x, y) x = Interval(1, 2) y = Interval(2, 3) x1 = sympy.Interval(1, 2) y1 = sympy.Interval(2, 3) assert Union(x, y) == Union(x1, y1) assert Union(x1, y) == Union(x1, y1) assert Union(x, y)._sympy_() == sympy.Union(x1, y1) assert sympify(sympy.Union(x1, y1)) == Union(x, y) assert Complement(x, y) == Complement(x1, y1) assert Complement(x1, y) == Complement(x1, y1) assert Complement(x, y)._sympy_() == sympy.Complement(x1, y1) assert sympify(sympy.Complement(x1, y1)) == Complement(x, y)
def test_Complement(): assert Complement(Interval(1, 3), Interval(1, 2)) == Interval(2, 3, True) assert Complement(FiniteSet(1, 3, 4), FiniteSet(3, 4)) == FiniteSet(1) assert Complement(Union(Interval(0, 2), FiniteSet(2, 3, 4)), Interval(1, 3)) == \ Union(Interval(0, 1, False, True), FiniteSet(4))
def test_Union(): assert Union(Interval(1, 2), Interval(2, 3)) == Interval(1, 3) assert Union(Interval(1, 2), Interval(2, 3, True)) == Interval(1, 3) assert Union(Interval(1, 3), Interval(2, 4)) == Interval(1, 4) assert Union(Interval(1, 2), Interval(1, 3)) == Interval(1, 3) assert Union(Interval(1, 3), Interval(1, 2)) == Interval(1, 3) assert Union(Interval(1, 3, False, True), Interval(1, 2)) == \ Interval(1, 3, False, True) assert Union(Interval(1, 3), Interval(1, 2, False, True)) == Interval(1, 3) assert Union(Interval(1, 2, True), Interval(1, 3)) == Interval(1, 3) assert Union(Interval(1, 2, True), Interval(1, 3, True)) == \ Interval(1, 3, True) assert Union(Interval(1, 2, True), Interval(1, 3, True, True)) == \ Interval(1, 3, True, True) assert Union(Interval(1, 2, True, True), Interval(1, 3, True)) == \ Interval(1, 3, True) assert Union(Interval(1, 3), Interval(2, 3)) == Interval(1, 3) assert Union(Interval(1, 3, False, True), Interval(2, 3)) == \ Interval(1, 3) assert Union(Interval(1, 2, False, True), Interval(2, 3, True)) != \ Interval(1, 3) assert Union(Interval(1, 2), EmptySet()) == Interval(1, 2) assert Union(EmptySet()) == EmptySet()