def test_Not(): assert Not(Equality(x, y)) == Unequality(x, y) assert Not(Unequality(x, y)) == Equality(x, y) assert Not(StrictGreaterThan(x, y)) == LessThan(x, y) assert Not(StrictLessThan(x, y)) == GreaterThan(x, y) assert Not(GreaterThan(x, y)) == StrictLessThan(x, y) assert Not(LessThan(x, y)) == StrictGreaterThan(x, y)
def test_nan_equality_exceptions(): # See issue sympy/sympy#7774 assert Equality(nan, nan) is false assert Unequality(nan, nan) is true # See issue sympy/sympy#7773 A = (x, Integer(0), Rational(1, 3), pi, oo, -oo) assert Equality(nan, random.choice(A)) is false assert Equality(random.choice(A), nan) is false assert Unequality(nan, random.choice(A)) is true assert Unequality(random.choice(A), nan) is true
def test_core_relational(): for c in (Equality, Equality(x, y), GreaterThan, GreaterThan(x, y), LessThan, LessThan(x, y), Relational, Relational(x, y), StrictGreaterThan, StrictGreaterThan(x, y), StrictLessThan, StrictLessThan(x, y), Unequality, Unequality(x, y)): check(c)
def test_Not(): pytest.raises(TypeError, lambda: Not(True, False)) assert Not(True) is false assert Not(False) is true assert Not(0) is true assert Not(1) is false assert Not(2) is false assert Not(Unequality(a, b)) == Equality(a, b)
def test_Equality(): assert Equality(IM, IM) is true assert Unequality(IM, IM) is false assert Equality(IM, IM.subs({1: 2})) is false assert Unequality(IM, IM.subs({1: 2})) is true assert Equality(IM, 2) is false assert Unequality(IM, 2) is true M = ImmutableMatrix([x, y]) assert Equality(M, IM) is false assert Unequality(M, IM) is true assert Equality(M, M.subs({x: 2})).subs({x: 2}) is true assert Unequality(M, M.subs({x: 2})).subs({x: 2}) is false assert Equality(M, M.subs({x: 2})).subs({x: 3}) is false assert Unequality(M, M.subs({x: 2})).subs({x: 3}) is true
def test_Equality(): assert Equality(IM, IM) is S.true assert Unequality(IM, IM) is S.false assert Equality(IM, IM.subs(1, 2)) is S.false assert Unequality(IM, IM.subs(1, 2)) is S.true assert Equality(IM, 2) is S.false assert Unequality(IM, 2) is S.true M = ImmutableMatrix([x, y]) assert Equality(M, IM) is S.false assert Unequality(M, IM) is S.true assert Equality(M, M.subs(x, 2)).subs(x, 2) is S.true assert Unequality(M, M.subs(x, 2)).subs(x, 2) is S.false assert Equality(M, M.subs(x, 2)).subs(x, 3) is S.false assert Unequality(M, M.subs(x, 2)).subs(x, 3) is S.true
def test_relational(): p = NumPyPrinter() e = Equality(x, 1) assert p.doprint(e) == 'equal(x, 1)' e = Unequality(x, 1) assert p.doprint(e) == 'not_equal(x, 1)' e = (x < 1) assert p.doprint(e) == 'less(x, 1)' e = (x <= 1) assert p.doprint(e) == 'less_equal(x, 1)' e = (x > 1) assert p.doprint(e) == 'greater(x, 1)' e = (x >= 1) assert p.doprint(e) == 'greater_equal(x, 1)'
def eval(cls, arg): from diofant import (Equality, GreaterThan, LessThan, StrictGreaterThan, StrictLessThan, Unequality) if isinstance(arg, Number) or arg in (True, False): return false if arg else true if arg.is_Not: return arg.args[0] # Simplify Relational objects. if isinstance(arg, Equality): return Unequality(*arg.args) if isinstance(arg, Unequality): return Equality(*arg.args) if isinstance(arg, StrictLessThan): return GreaterThan(*arg.args) if isinstance(arg, StrictGreaterThan): return LessThan(*arg.args) if isinstance(arg, LessThan): return StrictGreaterThan(*arg.args) if isinstance(arg, GreaterThan): return StrictLessThan(*arg.args)
def test_new_relational(): assert Eq(x, 0) == Relational(x, 0) # None ==> Equality assert Eq(x, 0) == Relational(x, 0, '==') assert Eq(x, 0) == Relational(x, 0, 'eq') assert Eq(x, 0) == Equality(x, 0) assert Eq(x, -1) == Relational(x, -1) # None ==> Equality assert Eq(x, -1) == Relational(x, -1, '==') assert Eq(x, -1) == Relational(x, -1, 'eq') assert Eq(x, -1) == Equality(x, -1) assert Eq(x, 0) != Relational(x, 1) # None ==> Equality assert Eq(x, 0) != Relational(x, 1, '==') assert Eq(x, 0) != Relational(x, 1, 'eq') assert Eq(x, 0) != Equality(x, 1) assert Eq(x, -1) != Relational(x, 1) # None ==> Equality assert Eq(x, -1) != Relational(x, 1, '==') assert Eq(x, -1) != Relational(x, 1, 'eq') assert Eq(x, -1) != Equality(x, 1) assert Ne(x, 0) == Relational(x, 0, '!=') assert Ne(x, 0) == Relational(x, 0, '<>') assert Ne(x, 0) == Relational(x, 0, 'ne') assert Ne(x, 0) == Unequality(x, 0) assert Ne(x, 0) != Relational(x, 1, '!=') assert Ne(x, 0) != Relational(x, 1, '<>') assert Ne(x, 0) != Relational(x, 1, 'ne') assert Ne(x, 0) != Unequality(x, 1) assert Ge(x, 0) == Relational(x, 0, '>=') assert Ge(x, 0) == Relational(x, 0, 'ge') assert Ge(x, 0) == GreaterThan(x, 0) assert Ge(x, 1) != Relational(x, 0, '>=') assert Ge(x, 1) != Relational(x, 0, 'ge') assert Ge(x, 1) != GreaterThan(x, 0) assert (x >= 1) == Relational(x, 1, '>=') assert (x >= 1) == Relational(x, 1, 'ge') assert (x >= 1) == GreaterThan(x, 1) assert (x >= 0) != Relational(x, 1, '>=') assert (x >= 0) != Relational(x, 1, 'ge') assert (x >= 0) != GreaterThan(x, 1) assert Le(x, 0) == Relational(x, 0, '<=') assert Le(x, 0) == Relational(x, 0, 'le') assert Le(x, 0) == LessThan(x, 0) assert Le(x, 1) != Relational(x, 0, '<=') assert Le(x, 1) != Relational(x, 0, 'le') assert Le(x, 1) != LessThan(x, 0) assert (x <= 1) == Relational(x, 1, '<=') assert (x <= 1) == Relational(x, 1, 'le') assert (x <= 1) == LessThan(x, 1) assert (x <= 0) != Relational(x, 1, '<=') assert (x <= 0) != Relational(x, 1, 'le') assert (x <= 0) != LessThan(x, 1) assert Gt(x, 0) == Relational(x, 0, '>') assert Gt(x, 0) == Relational(x, 0, 'gt') assert Gt(x, 0) == StrictGreaterThan(x, 0) assert Gt(x, 1) != Relational(x, 0, '>') assert Gt(x, 1) != Relational(x, 0, 'gt') assert Gt(x, 1) != StrictGreaterThan(x, 0) assert (x > 1) == Relational(x, 1, '>') assert (x > 1) == Relational(x, 1, 'gt') assert (x > 1) == StrictGreaterThan(x, 1) assert (x > 0) != Relational(x, 1, '>') assert (x > 0) != Relational(x, 1, 'gt') assert (x > 0) != StrictGreaterThan(x, 1) assert Lt(x, 0) == Relational(x, 0, '<') assert Lt(x, 0) == Relational(x, 0, 'lt') assert Lt(x, 0) == StrictLessThan(x, 0) assert Lt(x, 1) != Relational(x, 0, '<') assert Lt(x, 1) != Relational(x, 0, 'lt') assert Lt(x, 1) != StrictLessThan(x, 0) assert (x < 1) == Relational(x, 1, '<') assert (x < 1) == Relational(x, 1, 'lt') assert (x < 1) == StrictLessThan(x, 1) assert (x < 0) != Relational(x, 1, '<') assert (x < 0) != Relational(x, 1, 'lt') assert (x < 0) != StrictLessThan(x, 1) # finally, some fuzz testing for i in range(100): while 1: strtype, length = (chr, 65535) if random.randint(0, 1) else (chr, 255) relation_type = strtype(random.randint(0, length)) if random.randint(0, 1): relation_type += strtype(random.randint(0, length)) if relation_type not in ('==', 'eq', '!=', '<>', 'ne', '>=', 'ge', '<=', 'le', '>', 'gt', '<', 'lt', ':='): break pytest.raises(ValueError, lambda: Relational(x, 1, relation_type)) assert all(Relational(x, 0, op).rel_op == '==' for op in ('eq', '==')) assert all( Relational(x, 0, op).rel_op == '!=' for op in ('ne', '<>', '!=')) assert all(Relational(x, 0, op).rel_op == '>' for op in ('gt', '>')) assert all(Relational(x, 0, op).rel_op == '<' for op in ('lt', '<')) assert all(Relational(x, 0, op).rel_op == '>=' for op in ('ge', '>=')) assert all(Relational(x, 0, op).rel_op == '<=' for op in ('le', '<='))