예제 #1
0
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)
예제 #2
0
def test_Declaration():
    u = symbols('u', real=True)
    vu = Variable(u, type=Type.from_expr(u))
    assert Declaration(vu).variable.type == real
    vn = Variable(n, type=Type.from_expr(n))
    assert Declaration(vn).variable.type == integer

    lt = StrictLessThan(vu, vn)
    assert isinstance(lt, StrictLessThan)

    vuc = Variable(u, Type.from_expr(u), value=3.0, attrs={value_const})
    assert value_const in vuc.attrs
    assert pointer_const not in vuc.attrs
    decl = Declaration(vuc)
    assert decl.variable == vuc
    assert isinstance(decl.variable.value, Float)
    assert decl.variable.value == 3.0
    assert decl.func(*decl.args) == decl
    assert vuc.as_Declaration() == decl
    assert vuc.as_Declaration(value=None, attrs=None) == Declaration(vu)

    vy = Variable(y, type=integer, value=3)
    decl2 = Declaration(vy)
    assert decl2.variable == vy
    assert decl2.variable.value == Integer(3)

    vi = Variable(i, type=Type.from_expr(i), value=3.0)
    decl3 = Declaration(vi)
    assert decl3.variable.type == integer
    assert decl3.variable.value == 3.0

    raises(ValueError, lambda: Declaration(vi, 42))
예제 #3
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)
예제 #4
0
    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
예제 #5
0
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)
예제 #6
0
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', '<='))
예제 #7
0
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)
예제 #8
0
def prove(Eq):
    k = Symbol.k(integer=True, positive=True)
    n = Symbol.n(integer=True, positive=True)
    Eq << apply(n, k)

    s2_quote = Symbol.s_quote_2(definition=Eq[0].rhs.limits[0][1])
    Eq << s2_quote.this.definition

    Eq.s2_definition = Eq[0].subs(Eq[-1].reversed)

    s1_quote = Eq[2].lhs

    Eq << s1_quote.assertion()

    i = Eq[1].lhs.indices[0]
    x_slice = Eq[-1].limits[0][0]
    x = x_slice.base

    Eq.x_abs_positive_s1, Eq.x_abs_sum_s1, Eq.x_union_s1 = Eq[-1].split()

    j = Symbol.j(domain=Interval(0, k, integer=True))

    x_quote = Eq[1].lhs.base

    Eq.x_quote_set_in_s2 = Subset(image_set(UNION[i:0:k](x_quote[i].set),
                                            x_slice, s1_quote),
                                  Eq[0].lhs,
                                  plausible=True)

    Eq << Eq.x_quote_set_in_s2.definition

    Eq << Eq[-1].subs(Eq.s2_definition)

    Eq << Eq[-1].definition.definition
    Eq << Eq[-1].this.function.args[0].simplify()

    Eq << Eq[1].union_comprehension((i, 0, k))

    x_quote_union = Eq[-1].subs(Eq.x_union_s1)
    Eq << x_quote_union

    Eq << Eq[1].abs()
    x_quote_abs = Eq[-1]

    Eq << Eq[-1].sum((i, 0, k))

    Eq << sets.imply.less_than.union.apply(*Eq[-1].rhs.args[1].arg.args)

    Eq << Eq[-2].subs(Eq[-1])

    Eq << Eq[-1].subs(Eq.x_abs_sum_s1)

    Eq << x_quote_union.abs()
    x_quote_union_abs = Eq[-1]

    u = Eq[-1].lhs.arg
    Eq << sets.imply.less_than.union_comprehension.apply(u.function, *u.limits)

    Eq << Eq[-2].subs(Eq[-1])

    Eq << Eq[-4].subs(Eq[-1])
    SqueezeTheorem = Eq[-1]

    Eq << x_quote_abs.as_Or()

    Eq << Eq[-1].subs(i, j)

    Eq << Eq[-2].forall((i, Unequality(i, j)))

    Eq << sets.imply.greater_than.apply(*Eq[-2].rhs.arg.args[::-1])

    Eq << Eq[-1].subs(Eq.x_abs_positive_s1.limits_subs(i, j))

    Eq << Eq[-4].subs(Eq[-1])

    Eq << Eq[-4].subs(Eq.x_abs_positive_s1)

    Eq << (Eq[-1] & Eq[-2])

    Eq << (x_quote_union & SqueezeTheorem & Eq[-1])

    Eq.x_quote_definition = Eq[1].reference((i, 0, k))

    Eq.subset_A = Subset(Eq[4].lhs, Eq[4].rhs, plausible=True)
    Eq.supset_A = Supset(Eq[4].lhs, Eq[3].lhs, plausible=True)

    Eq << Eq.supset_A.subs(Eq[3])

    Eq << Eq[-1].definition.definition

    Eq << Eq[-1].split()

    notContains = Eq[-1]
    Eq << ~Eq[-1]

    Eq << Eq[-1].definition

    Eq << Eq.x_quote_definition[j]

    Eq << Eq[-1].intersect(Eq[-2].reversed)

    Eq << sets.imply.equality.inclusion_exclusion_principle.apply(
        *Eq[-1].lhs.args)

    Eq << Eq[-1].subs(Eq[-2])

    Eq.set_size_inequality = Eq[-1].subs(
        StrictLessThan(Eq[-1].function.rhs,
                       Eq[-1].function.rhs + 1,
                       plausible=True))

    Eq << x_quote_union.this.function.lhs.bisect({i, j})

    Eq << sets.imply.less_than.union.apply(*Eq[-1].lhs.args)

    Eq << sets.imply.less_than.union_comprehension.apply(
        *Eq[-2].lhs.args[0].args)

    Eq << Eq[-2].subs(Eq[-1]) + Eq.set_size_inequality

    Eq << Eq[-1].this().function.rhs.args[-1].simplify()

    Eq << Eq[-1].this().function.rhs.args[0].arg.simplify()

    Eq << Eq[-1].subs(x_quote_union_abs)

    Eq << Eq[-1].subs(SqueezeTheorem)

    Eq << Eq.subset_A.subs(Eq[3])

    Eq << Eq[-1].definition.definition

    s2_hat_n = Symbol("\hat{s}_{2, n}", definition=Eq[-1].limits[0][1])

    Eq << s2_hat_n.this.definition

    Eq.s2_hat_n_assertion = Eq[-2].this.limits[0].subs(Eq[-1].reversed)

    Eq << Eq[-1].this.rhs.as_image_set()

    s2_quote_n = Symbol("s'_{2, n}", definition=Eq[-1].rhs.limits[0][1])

    assert s2_quote_n in s2_quote
    assert Supset(s2_quote, s2_quote_n)

    Eq << s2_quote_n.this.definition

    Eq << Eq[-2].subs(Eq[-1].reversed)

    Eq.s2_hat_n_hypothesis = Eq.s2_hat_n_assertion.this.limits[0].subs(Eq[-1])

    Eq << s2_quote_n.assertion()

    Eq.n_not_in_x, Eq.x_abs_positive_s2_n, Eq.x_abs_sum_s2_n, Eq.x_union_s2_n = Eq[
        -1].split()

    Eq << Eq.n_not_in_x.definition

    Eq.x_j_inequality = Eq[-1].limits_subs(i, j)

    Eq << Eq.x_union_s2_n.func(Contains(n, Eq.x_union_s2_n.lhs),
                               *Eq.x_union_s2_n.limits,
                               plausible=True)

    Eq << Eq[-1].subs(Eq.x_union_s2_n)

    Eq << Eq[-1].definition

    x_hat = Symbol(r"\hat{x}",
                   shape=(oo, ),
                   dtype=dtype.integer,
                   definition=LAMBDA[i](Piecewise(
                       (x_slice[i] - {n}, Equality(i, j)),
                       (x_slice[i], True))))

    Eq.x_hat_definition = x_hat.equality_defined()

    Eq << Eq[-1].this.function.limits_subs(i, j)

    Eq.x_j_subset = Eq[-1].apply(sets.contains.imply.subset, simplify=False)

    Eq << Eq.x_j_subset.apply(sets.inequality.subset.imply.inequality,
                              Eq.x_j_inequality,
                              simplify=False)

    Eq.x_j_abs_positive = Eq[-1].apply(
        sets.inequality.imply.strict_greater_than)

    Eq.x_hat_abs = Eq.x_hat_definition.abs()

    Eq << Eq.x_hat_abs.as_Or()
    Eq << Eq[-1].subs(i, j)
    Eq << Eq[-2].forall((i, Unequality(i, j)))

    Eq << Eq[-1].subs(Eq.x_abs_positive_s2_n)  # -1

    Eq << Eq[-3].subs(Eq.x_j_abs_positive)

    Eq.x_hat_abs_positive = Eq[-1] & Eq[-2]

    Eq.x_hat_union = Eq.x_hat_definition.union_comprehension((i, 0, k))
    Eq.x_union_complement = Eq.x_union_s2_n - {n}

    Eq << Eq.x_union_s2_n.abs().subs(Eq.x_abs_sum_s2_n.reversed).apply(
        sets.equality.imply.forall_equality.nonoverlapping)

    Eq << Eq[-1].limits_subs(Eq[-1].variables[1], j).limits_subs(
        Eq[-1].variable, i)

    Eq.x_complement_n = Eq[-1].apply(sets.equality.subset.imply.equality,
                                     Eq.x_j_subset)

    Eq << Eq.x_complement_n.this.function.function.union_comprehension(
        *Eq.x_complement_n.function.function.limits)

    Eq << Eq.x_hat_union.subs(Eq[-1].reversed)

    Eq.x_hat_union = Eq[-1].subs(Eq.x_union_complement)

    Eq << Eq.x_hat_abs.sum((i, 0, k)).subs(Eq.x_abs_sum_s2_n)

    Eq << Eq.x_j_subset.apply(sets.subset.imply.equality.complement)

    Eq << Eq[-2].subs(Eq[-1])

    Eq << (Eq[-1] & Eq.x_hat_abs_positive & Eq.x_hat_union)

    function = Contains(x_hat[:k + 1], s1_quote)
    function = Eq[-1].function.func(function, *Eq[-1].function.limits)

    Eq.x_hat_in_s1 = Eq[-1].func(function, *Eq[-1].limits, plausible=True)

    Eq << Eq.x_hat_in_s1.definition

    Eq << Eq.x_hat_definition.as_Or()
    Eq << Eq[-1].subs(i, j)
    Eq << Eq[-2].forall((i, Unequality(i, j)))

    Eq <<= Eq[-1] & Eq.x_complement_n.reversed

    Eq << (Eq[-1] & Eq[-3])

    Eq << Eq[-1].this.function.function.reference(
        *Eq[-1].function.function.limits)

    Eq << Eq.x_hat_in_s1.subs(Eq[-1])

    Eq << Eq.s2_hat_n_hypothesis.strip().strip()

    Eq << Eq[-1].subs(Eq.x_quote_definition)

    Eq.equation = Eq[-1] - {n}

    Eq << Eq.x_union_s1.intersect({n})

    Eq.nonoverlapping_s1_quote = Eq[-1].apply(
        sets.equality.imply.equality.given.emptyset.intersect)

    Eq.xi_complement_n = Eq.nonoverlapping_s1_quote.apply(
        sets.equality.imply.equality.given.emptyset.complement, reverse=True)

    Eq << Eq.equation.subs(Eq.xi_complement_n)

    a = Eq[-1].variable
    b = Symbol.b(**a.dtype.dict)

    Eq << Eq[-1].limits_subs(a, b)

    Eq << Eq[-1].this.function.subs(x[:k + 1], a)

    Eq << Eq[-1].limits_subs(b, x[:k + 1])

    Eq << Eq[-1].this.function.function.reference((i, 0, k))

    Eq.supset_A = sets.supset.imply.supset.apply(Eq.supset_A, (j, ),
                                                 simplify=False)

    Eq << Eq.supset_A.subs(Eq.subset_A)
예제 #9
0
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))
예제 #10
0
 (r"\theta!", _factorial(theta)),
 (r"(x + 1)!", _factorial(_Add(x, 1))),
 (r"(x!)!", _factorial(_factorial(x))),
 (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))),
예제 #11
0
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", "<="))