def test_contains(): assert 6 in ConditionSet(x, x > 5, Interval(1, 7)) assert (8 in ConditionSet(x, y > 5, Interval(1, 7))) is False # `in` should give True or False; in this case there is not # enough information for that result raises(TypeError, lambda: 6 in ConditionSet(x, y > 5, Interval(1, 7))) # here, there is enough information but the comparison is # not defined raises(TypeError, lambda: 0 in ConditionSet(x, 1/x >= 0, S.Reals)) assert ConditionSet(x, y > 5, Interval(1, 7) ).contains(6) == (y > 5) assert ConditionSet(x, y > 5, Interval(1, 7) ).contains(8) is S.false assert ConditionSet(x, y > 5, Interval(1, 7) ).contains(w) == And(Contains(w, Interval(1, 7)), y > 5) # This returns an unevaluated Contains object # because 1/0 should not be defined for 1 and 0 in the context of # reals. assert ConditionSet(x, 1/x >= 0, S.Reals).contains(0) == \ Contains(0, ConditionSet(x, 1/x >= 0, S.Reals), evaluate=False) c = ConditionSet((x, y), x + y > 1, S.Integers**2) assert not c.contains(1) assert c.contains((2, 1)) assert not c.contains((0, 1)) c = ConditionSet((w, (x, y)), w + x + y > 1, S.Integers*S.Integers**2) assert not c.contains(1) assert not c.contains((1, 2)) assert not c.contains(((1, 2), 3)) assert not c.contains(((1, 2), (3, 4))) assert c.contains((1, (3, 4)))
def test_flatten(): """Tests whether there is basic denesting functionality""" inner = ConditionSet(x, sin(x) + x > 0) outer = ConditionSet(x, Contains(x, inner), S.Reals) assert outer == ConditionSet(x, sin(x) + x > 0, S.Reals) inner = ConditionSet(y, sin(y) + y > 0) outer = ConditionSet(x, Contains(y, inner), S.Reals) assert outer != ConditionSet(x, sin(x) + x > 0, S.Reals) inner = ConditionSet(x, sin(x) + x > 0).intersect(Interval(-1, 1)) outer = ConditionSet(x, Contains(x, inner), S.Reals) assert outer == ConditionSet(x, sin(x) + x > 0, Interval(-1, 1))
def test_failing_contains(): # XXX This may have to return unevaluated Contains object # because 1/0 should not be defined for 1 and 0 in the context of # reals, but there is a nonsensical evaluation to ComplexInfinity # and the comparison is giving an error. assert ConditionSet(x, 1/x >= 0, S.Reals).contains(0) == \ Contains(0, ConditionSet(x, 1/x >= 0, S.Reals), evaluate=False)
def test_contains(): assert 6 in ConditionSet(x, x > 5, Interval(1, 7)) assert (8 in ConditionSet(x, y > 5, Interval(1, 7))) is False # `in` should give True or False; in this case there is not # enough information for that result raises(TypeError, lambda: 6 in ConditionSet(x, y > 5, Interval(1, 7))) assert ConditionSet(x, y > 5, Interval(1, 7)).contains(6) == (y > 5) assert ConditionSet(x, y > 5, Interval(1, 7)).contains(8) is S.false assert ConditionSet(x, y > 5, Interval(1, 7)).contains(w) == And( Contains(w, Interval(1, 7)), y > 5)
def test_flatten(): """Tests whether there is basic denesting functionality""" inner = ConditionSet(x, sin(x) + x > 0) outer = ConditionSet(x, Contains(x, inner), S.Reals) assert outer == ConditionSet(x, sin(x) + x > 0, S.Reals) inner = ConditionSet(y, sin(y) + y > 0) outer = ConditionSet(x, Contains(y, inner), S.Reals) assert outer != ConditionSet(x, sin(x) + x > 0, S.Reals) inner = ConditionSet(x, sin(x) + x > 0).intersect(Interval(-1, 1)) outer = ConditionSet(x, Contains(x, inner), S.Reals) assert outer == ConditionSet(x, sin(x) + x > 0, Interval(-1, 1)) def test_duplicate(): from sympy.core.function import BadSignatureError # test coverage for line 95 in conditionset.py, check for duplicates in symbols dup = symbols('a,a') raises(BadSignatureError, lambda: ConditionSet(dup, x < 0))
def test_subs_CondSet(): s = FiniteSet(z, y) c = ConditionSet(x, x < 2, s) # you can only replace sym with a symbol that is not in # the free symbols assert c.subs(x, 1) == c assert c.subs(x, y) == ConditionSet(y, y < 2, s) # double subs needed to change dummy if the base set # also contains the dummy orig = ConditionSet(y, y < 2, s) base = orig.subs(y, w) and_dummy = base.subs(y, w) assert base == ConditionSet(y, y < 2, {w, z}) assert and_dummy == ConditionSet(w, w < 2, {w, z}) assert c.subs(x, w) == ConditionSet(w, w < 2, s) assert ConditionSet(x, x < y, s).subs(y, w) == ConditionSet(x, x < w, s.subs(y, w)) # if the user uses assumptions that cause the condition # to evaluate, that can't be helped from SymPy's end n = Symbol('n', negative=True) assert ConditionSet(n, 0 < n, S.Integers) is S.EmptySet p = Symbol('p', positive=True) assert ConditionSet(n, n < y, S.Integers).subs(n, x) == ConditionSet( x, x < y, S.Integers) nc = Symbol('nc', commutative=False) raises(ValueError, lambda: ConditionSet(x, x < p, S.Integers).subs(x, nc)) raises(ValueError, lambda: ConditionSet(x, x < p, S.Integers).subs(x, n)) raises(ValueError, lambda: ConditionSet(x + 1, x < 1, S.Integers)) raises(ValueError, lambda: ConditionSet(x + 1, x < 1, s)) assert ConditionSet(n, n < x, Interval(0, oo)).subs(x, p) == Interval(0, oo) assert ConditionSet(n, n < x, Interval(-oo, 0)).subs(x, p) == Interval(-oo, 0) assert ConditionSet(f(x), f(x) < 1, {w, z}).subs(f(x), y) == ConditionSet(y, y < 1, {w, z}) # issue 17341 k = Symbol('k') img1 = ImageSet(Lambda(k, 2 * k * pi + asin(y)), S.Integers) img2 = ImageSet(Lambda(k, 2 * k * pi + asin(S.One / 3)), S.Integers) assert ConditionSet(x, Contains(y, Interval(-1, 1)), img1).subs(y, S.One / 3).dummy_eq(img2)
def test_subs_CondSet(): s = FiniteSet(z, y) c = ConditionSet(x, x < 2, s) assert c.subs(x, y) == c assert c.subs(z, y) == ConditionSet(x, x < 2, FiniteSet(y)) assert c.xreplace({x: y}) == ConditionSet(y, y < 2, s) assert ConditionSet(x, x < y, s ).subs(y, w) == ConditionSet(x, x < w, s.subs(y, w)) # if the user uses assumptions that cause the condition # to evaluate, that can't be helped from SymPy's end n = Symbol('n', negative=True) assert ConditionSet(n, 0 < n, S.Integers) is S.EmptySet p = Symbol('p', positive=True) assert ConditionSet(n, n < y, S.Integers ).subs(n, x) == ConditionSet(n, n < y, S.Integers) raises(ValueError, lambda: ConditionSet( x + 1, x < 1, S.Integers)) assert ConditionSet( p, n < x, Interval(-5, 5)).subs(x, p) == Interval(-5, 5), ConditionSet( p, n < x, Interval(-5, 5)).subs(x, p) assert ConditionSet( n, n < x, Interval(-oo, 0)).subs(x, p ) == Interval(-oo, 0) assert ConditionSet(f(x), f(x) < 1, {w, z} ).subs(f(x), y) == ConditionSet(f(x), f(x) < 1, {w, z}) # issue 17341 k = Symbol('k') img1 = ImageSet(Lambda(k, 2*k*pi + asin(y)), S.Integers) img2 = ImageSet(Lambda(k, 2*k*pi + asin(S.One/3)), S.Integers) assert ConditionSet(x, Contains( y, Interval(-1,1)), img1).subs(y, S.One/3).dummy_eq(img2) assert (0, 1) in ConditionSet((x, y), x + y < 3, S.Integers**2) raises(TypeError, lambda: ConditionSet(n, n < -10, Interval(0, 10)))
def test_as_relational(): assert ConditionSet((x, y), x > 1, S.Integers**2).as_relational((x, y) ) == (x > 1) & Contains((x, y), S.Integers**2) assert ConditionSet(x, x > 1, S.Integers).as_relational(x ) == Contains(x, S.Integers) & (x > 1)