Exemple #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)
Exemple #2
0
def test_nan_equality_exceptions():
    # See issue sympy/sympy#7774
    assert Equality(nan, nan) is S.false
    assert Unequality(nan, nan) is S.true

    # See issue sympy/sympy#7773
    A = (x, Integer(0), Rational(1, 3), pi, oo, -oo)
    assert Equality(nan, random.choice(A)) is S.false
    assert Equality(random.choice(A), nan) is S.false
    assert Unequality(nan, random.choice(A)) is S.true
    assert Unequality(random.choice(A), nan) is S.true
Exemple #3
0
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)
Exemple #4
0
    def __new__(cls, function, *symbols, **assumptions):
        # Any embedded piecewise functions need to be brought out to the
        # top level so that integration can go into piecewise mode at the
        # earliest possible moment.
        #
        # This constructor only differs from ExprWithLimits
        # in the application of the orientation variable.  Perhaps merge?
        function = sympify(function)
        if hasattr(function, 'func') and function.func is Equality:
            lhs = function.lhs
            rhs = function.rhs
            return Equality(cls(lhs, *symbols, **assumptions),
                            cls(rhs, *symbols, **assumptions))
        function = piecewise_fold(function)

        if function is S.NaN:
            return S.NaN

        if symbols:
            limits, orientation = _process_limits(*symbols)
        else:
            # symbol not provided -- we can still try to compute a general form
            free = function.free_symbols
            if len(free) != 1:
                raise ValueError(
                    " specify dummy variables for %s. If the integrand contains"
                    " more than one free symbol, an integration variable should"
                    " be supplied explicitly e.g., integrate(f(x, y), x)" %
                    function)
            limits, orientation = [Tuple(s) for s in free], 1

        # denest any nested calls
        while cls == type(function):
            limits = list(function.limits) + limits
            function = function.function

        obj = Expr.__new__(cls, **assumptions)
        arglist = [orientation * function]
        arglist.extend(limits)
        obj._args = tuple(arglist)

        return obj
Exemple #5
0
    def __new__(cls, function, *symbols, **assumptions):
        # Any embedded piecewise functions need to be brought out to the
        # top level so that integration can go into piecewise mode at the
        # earliest possible moment.
        function = sympify(function)
        if hasattr(function, 'func') and function.func is Equality:
            lhs = function.lhs
            rhs = function.rhs
            return Equality(cls(lhs, *symbols, **assumptions),
                            cls(rhs, *symbols, **assumptions))
        function = piecewise_fold(function)

        if function is S.NaN:
            return S.NaN

        if symbols:
            limits, orientation = _process_limits(*symbols)
        else:
            # symbol not provided -- we can still try to compute a general form
            free = function.free_symbols
            if len(free) != 1:
                raise ValueError("specify dummy variables for %s" % function)
            limits, orientation = [Tuple(s) for s in free], 1

        # denest any nested calls
        while cls == type(function):
            limits = list(function.limits) + limits
            function = function.function

        # Only limits with lower and upper bounds are supported; the indefinite form
        # is not supported
        if any(len(l) != 3 or None in l for l in limits):
            raise ValueError(
                'ExprWithLimits requires values for lower and upper bounds.')

        obj = Expr.__new__(cls, **assumptions)
        arglist = [function]
        arglist.extend(limits)
        obj._args = tuple(arglist)

        return obj
Exemple #6
0
def test_new_relational():
    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
    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))