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 prove(Eq): x = Symbol.x(real=True, random=True) Eq << apply(Unequal(P(x), 0)) Eq << GreaterThan(P(x), 0, plausible=True) Eq <<= Eq[-1] & Eq[0]
def test_core_relational(): x = Symbol("x") y = Symbol("y") 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 apply(given): assert given.is_Unequality A, B = given.args if B != S.EmptySet: assert A == S.EmptySet A = B return GreaterThan(abs(A), 1, given=given)
def prove(Eq): A = Symbol.A(dtype=dtype.integer) inequality = Unequality(A, S.EmptySet, evaluate=False) Eq << apply(inequality) Eq << inequality.abs() Eq << GreaterThan(*Eq[-1].args, plausible=True) Eq << Eq[-1].subs(inequality)
def as_two_terms(self): from sympy.sets.sets import Interval if not isinstance(self.set, Interval): return self res = [None] * 2 kwargs = self.clauses() kwargs['given'] = self if self.plausible else None kwargs['evaluate'] = False if self.set.left_open: res[0] = StrictGreaterThan(self.element, self.set.start, **kwargs) else: res[0] = GreaterThan(self.element, self.set.start, **kwargs) if self.set.right_open: res[1] = StrictLessThan(self.element, self.set.stop, **kwargs) else: res[1] = LessThan(self.element, self.set.stop, **kwargs) return res
def test_lt_gt(): S = sympify x, y = Symbol('x'), Symbol('y') assert (x >= y) == GreaterThan(x, y) assert (x >= 0) == GreaterThan(x, 0) assert (x <= y) == LessThan(x, y) assert (x <= 0) == LessThan(x, 0) assert (0 <= x) == GreaterThan(x, 0) assert (0 >= x) == LessThan(x, 0) assert (S(0) >= x) == GreaterThan(0, x) assert (S(0) <= x) == LessThan(0, x) assert (x > y) == StrictGreaterThan(x, y) assert (x > 0) == StrictGreaterThan(x, 0) assert (x < y) == StrictLessThan(x, y) assert (x < 0) == StrictLessThan(x, 0) assert (0 < x) == StrictGreaterThan(x, 0) assert (0 > x) == StrictLessThan(x, 0) assert (S(0) > x) == StrictGreaterThan(0, x) assert (S(0) < x) == StrictLessThan(0, x) e = x**2 + 4 * x + 1 assert (e >= 0) == GreaterThan(e, 0) assert (0 <= e) == GreaterThan(e, 0) assert (e > 0) == StrictGreaterThan(e, 0) assert (0 < e) == StrictGreaterThan(e, 0) assert (e <= 0) == LessThan(e, 0) assert (0 >= e) == LessThan(e, 0) assert (e < 0) == StrictLessThan(e, 0) assert (0 > e) == StrictLessThan(e, 0) assert (S(0) >= e) == GreaterThan(0, e) assert (S(0) <= e) == LessThan(0, e) assert (S(0) < e) == StrictLessThan(0, e) assert (S(0) > e) == StrictGreaterThan(0, e)
def test_new_relational(): x = Symbol('x') 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, 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 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 from sympy.core.random import randint for i in range(100): while 1: strtype, length = (chr, 65535) if randint(0, 1) else (chr, 255) relation_type = strtype(randint(0, length)) if randint(0, 1): relation_type += strtype(randint(0, length)) if relation_type not in ('==', 'eq', '!=', '<>', 'ne', '>=', 'ge', '<=', 'le', '>', 'gt', '<', 'lt', ':=', '+=', '-=', '*=', '/=', '%='): break 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', '<='))
def test_Idx_inequalities(): i14 = Idx("i14", (1, 4)) i79 = Idx("i79", (7, 9)) i46 = Idx("i46", (4, 6)) i35 = Idx("i35", (3, 5)) assert i14 <= 5 assert i14 < 5 assert not (i14 >= 5) assert not (i14 > 5) assert 5 >= i14 assert 5 > i14 assert not (5 <= i14) assert not (5 < i14) assert LessThan(i14, 5) assert StrictLessThan(i14, 5) assert not GreaterThan(i14, 5) assert not StrictGreaterThan(i14, 5) assert i14 <= 4 assert isinstance(i14 < 4, StrictLessThan) assert isinstance(i14 >= 4, GreaterThan) assert not (i14 > 4) assert isinstance(i14 <= 1, LessThan) assert not (i14 < 1) assert i14 >= 1 assert isinstance(i14 > 1, StrictGreaterThan) assert not (i14 <= 0) assert not (i14 < 0) assert i14 >= 0 assert i14 > 0 from sympy.abc import x assert isinstance(i14 < x, StrictLessThan) assert isinstance(i14 > x, StrictGreaterThan) assert isinstance(i14 <= x, LessThan) assert isinstance(i14 >= x, GreaterThan) assert i14 < i79 assert i14 <= i79 assert not (i14 > i79) assert not (i14 >= i79) assert i14 <= i46 assert isinstance(i14 < i46, StrictLessThan) assert isinstance(i14 >= i46, GreaterThan) assert not (i14 > i46) assert isinstance(i14 < i35, StrictLessThan) assert isinstance(i14 > i35, StrictGreaterThan) assert isinstance(i14 <= i35, LessThan) assert isinstance(i14 >= i35, GreaterThan) iNone1 = Idx("iNone1") iNone2 = Idx("iNone2") assert isinstance(iNone1 < iNone2, StrictLessThan) assert isinstance(iNone1 > iNone2, StrictGreaterThan) assert isinstance(iNone1 <= iNone2, LessThan) assert isinstance(iNone1 >= iNone2, GreaterThan)
def test_new_relational(): x = Symbol('x') assert Eq(x) == Relational(x, 0) # None ==> Equality assert Eq(x) == Relational(x, 0, '==') assert Eq(x) == Relational(x, 0, 'eq') assert Eq(x) == 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) != Relational(x, 1) # None ==> Equality assert Eq(x) != Relational(x, 1, '==') assert Eq(x) != Relational(x, 1, 'eq') assert Eq(x) != 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 from random import randint for i in range(100): while 1: if sys.version_info[0] >= 3: strtype, length = (chr, 65535) if randint(0, 1) else (chr, 255) else: strtype, length = (unichr, 65535) if randint(0, 1) else (chr, 255) relation_type = strtype( randint(0, length) ) if randint(0, 1): relation_type += strtype( randint(0, length) ) if relation_type not in ('==', 'eq', '!=', '<>', 'ne', '>=', 'ge', '<=', 'le', '>', 'gt', '<', 'lt'): break raises(ValueError, lambda: Relational(x, 1, relation_type))
(r"x!!!", _factorial(_factorial(_factorial(x)))), (r"5!7!", _Mul(_factorial(5), _factorial(7))), (r"\sqrt{x}", sqrt(x)), (r"\sqrt{x + b}", sqrt(_Add(x, b))), (r"\sqrt[3]{\sin x}", root(sin(x), 3)), (r"\sqrt[y]{\sin x}", root(sin(x), y)), (r"\sqrt[\theta]{\sin x}", root(sin(x), theta)), (r"\sqrt{\frac{12}{6}}", _Sqrt(_Mul(12, _Pow(6, -1)))), (r"\overline{z}", _Conjugate(z)), (r"\overline{\overline{z}}", _Conjugate(_Conjugate(z))), (r"\overline{x + y}", _Conjugate(_Add(x, y))), (r"\overline{x} + \overline{y}", _Conjugate(x) + _Conjugate(y)), (r"x < y", StrictLessThan(x, y)), (r"x \leq y", LessThan(x, y)), (r"x > y", StrictGreaterThan(x, y)), (r"x \geq y", GreaterThan(x, y)), (r"\mathit{x}", Symbol('x')), (r"\mathit{test}", Symbol('test')), (r"\mathit{TEST}", Symbol('TEST')), (r"\mathit{HELLO world}", Symbol('HELLO world')), (r"\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))), (r"\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))), (r"\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))), (r"\sum^3_{k = 1} c", Sum(c, (k, 1, 3))), (r"\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))), (r"\sum_{n = 0}^{\infty} \frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))), (r"\prod_{a = b}^{c} x", Product(x, (a, b, c))), (r"\prod_{a = b}^c x", Product(x, (a, b, c))), (r"\prod^{c}_{a = b} x", Product(x, (a, b, c))), (r"\prod^c_{a = b} x", Product(x, (a, b, c))),
def apply(given): assert given.is_Supset A, B = given.args return GreaterThan(abs(A), abs(B), given=given)
def apply(p, q): x = Symbol('x') y = Symbol('y') return S.true return GreaterThan(KL(p(x, y), q(x, y), (x,), (y,)), KL(p(x), q(x), (x,)))
def test_new_relational(): x = Symbol("x") 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, 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 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 from random import randint from sympy.core.compatibility import unichr for i in range(100): while 1: strtype, length = (unichr, 65535) if randint(0, 1) else (chr, 255) relation_type = strtype(randint(0, length)) if randint(0, 1): relation_type += strtype(randint(0, length)) if relation_type not in ( "==", "eq", "!=", "<>", "ne", ">=", "ge", "<=", "le", ">", "gt", "<", "lt", ":=", "+=", "-=", "*=", "/=", "%=", ): break 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", "<="))
def apply(A, B): return GreaterThan(abs(Union(A, B)), abs(A))