def test_solve_univariate_inequality():
    assert isolve(x**2 >= 4, x, relational=False) == Union(Interval(-oo, -2),
        Interval(2, oo))
    assert isolve(x**2 >= 4, x) == Or(And(Le(2, x), Lt(x, oo)), And(Le(x, -2),
        Lt(-oo, x)))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x, relational=False) == \
        Union(Interval(1, 2), Interval(3, oo))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x) == \
        Or(And(Le(1, x), Le(x, 2)), And(Le(3, x), Lt(x, oo)))
    assert isolve((x - 1)*(x - 2)*(x - 4) < 0, x, domain = FiniteSet(0, 3)) == \
        Or(Eq(x, 0), Eq(x, 3))
    # issue 2785:
    assert isolve(x**3 - 2*x - 1 > 0, x, relational=False) == \
        Union(Interval(-1, -sqrt(5)/2 + S.Half, True, True),
              Interval(S.Half + sqrt(5)/2, oo, True, True))
    # issue 2794:
    assert isolve(x**3 - x**2 + x - 1 > 0, x, relational=False) == \
        Interval(1, oo, True)
    #issue 13105
    assert isolve((x + I)*(x + 2*I) < 0, x) == Eq(x, 0)
    assert isolve(((x - 1)*(x - 2) + I)*((x - 1)*(x - 2) + 2*I) < 0, x) == Or(Eq(x, 1), Eq(x, 2))
    assert isolve((((x - 1)*(x - 2) + I)*((x - 1)*(x - 2) + 2*I))/(x - 2) > 0, x) == Eq(x, 1)
    raises (ValueError, lambda: isolve((x**2 - 3*x*I + 2)/x < 0, x))

    # numerical testing in valid() is needed
    assert isolve(x**7 - x - 2 > 0, x) == \
        And(rootof(x**7 - x - 2, 0) < x, x < oo)

    # handle numerator and denominator; although these would be handled as
    # rational inequalities, these test confirm that the right thing is done
    # when the domain is EX (e.g. when 2 is replaced with sqrt(2))
    assert isolve(1/(x - 2) > 0, x) == And(S(2) < x, x < oo)
    den = ((x - 1)*(x - 2)).expand()
    assert isolve((x - 1)/den <= 0, x) == \
        Or(And(-oo < x, x < 1), And(S.One < x, x < 2))

    n = Dummy('n')
    raises(NotImplementedError, lambda: isolve(Abs(x) <= n, x, relational=False))
    c1 = Dummy("c1", positive=True)
    raises(NotImplementedError, lambda: isolve(n/c1 < 0, c1))
    n = Dummy('n', negative=True)
    assert isolve(n/c1 > -2, c1) == (-n/2 < c1)
    assert isolve(n/c1 < 0, c1) == True
    assert isolve(n/c1 > 0, c1) == False

    zero = cos(1)**2 + sin(1)**2 - 1
    raises(NotImplementedError, lambda: isolve(x**2 < zero, x))
    raises(NotImplementedError, lambda: isolve(
        x**2 < zero*I, x))
    raises(NotImplementedError, lambda: isolve(1/(x - y) < 2, x))
    raises(NotImplementedError, lambda: isolve(1/(x - y) < 0, x))
    raises(TypeError, lambda: isolve(x - I < 0, x))

    zero = x**2 + x - x*(x + 1)
    assert isolve(zero < 0, x, relational=False) is S.EmptySet
    assert isolve(zero <= 0, x, relational=False) is S.Reals

    # make sure iter_solutions gets a default value
    raises(NotImplementedError, lambda: isolve(
        Eq(cos(x)**2 + sin(x)**2, 1), x))
Example #2
0
def make_cond(rel, d, direction, iteration):
    """
    Create a symbolic condition which, once resolved at runtime, returns True
    if `iteration` is within the Dimension `d`'s min/max bounds, False otherwise.
    """
    if rel is None:
        if direction is Forward:
            cond = Le(iteration, d.symbolic_max)
        else:
            cond = Ge(iteration, d.symbolic_min)
    else:
        # Only case we know how to deal with, today, is the one induced
        # by a ConditionalDimension with structured condition (e.g. via `factor`)
        if not (rel.is_Equality and rel.rhs == 0 and isinstance(rel.lhs, Mod)):
            raise InvalidOperator("Unable to understand data streaming pattern")
        _, v = rel.lhs.args

        if direction is Forward:
            # The LHS rounds `s` up to the nearest multiple of `v`
            cond = Le(Mul(((iteration + v - 1) / v), v, evaluate=False), d.symbolic_max)
        else:
            # The LHS rounds `s` down to the nearest multiple of `v`
            cond = Ge(Mul((iteration / v), v, evaluate=False), d.symbolic_min)

    if cond is true:
        return None
    else:
        return cond
Example #3
0
def test_solve_univariate_inequality():
    assert isolve(x**2 >= 4,
                  x, relational=False) == Union(Interval(-oo, -2),
                                                Interval(2, oo))
    assert isolve(x**2 >= 4, x) == Or(And(Le(2, x), Lt(x, oo)),
                                      And(Le(x, -2), Lt(-oo, x)))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x, relational=False) == \
        Union(Interval(1, 2), Interval(3, oo))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x) == \
        Or(And(Le(1, x), Le(x, 2)), And(Le(3, x), Lt(x, oo)))
    # issue 2785:
    assert isolve(x**3 - 2*x - 1 > 0, x, relational=False) == \
        Union(Interval(-1, -sqrt(5)/2 + S(1)/2, True, True),
              Interval(S(1)/2 + sqrt(5)/2, oo, True, True))
    # issue 2794:
    assert isolve(x**3 - x**2 + x - 1 > 0, x, relational=False) == \
        Interval(1, oo, True)

    # XXX should be limited in domain, e.g. between 0 and 2*pi
    assert isolve(sin(x) < S.Half, x) == \
        Or(And(-oo < x, x < pi/6), And(5*pi/6 < x, x < oo))
    assert isolve(sin(x) > S.Half, x) == And(pi / 6 < x, x < 5 * pi / 6)

    # numerical testing in valid() is needed
    assert isolve(x**7 - x - 2 > 0, x) == \
        And(RootOf(x**7 - x - 2, 0) < x, x < oo)

    # handle numerator and denominator; although these would be handled as
    # rational inequalities, these test confirm that the right thing is done
    # when the domain is EX (e.g. when 2 is replaced with sqrt(2))
    assert isolve(1 / (x - 2) > 0, x) == And(S(2) < x, x < oo)
    den = ((x - 1) * (x - 2)).expand()
    assert isolve((x - 1)/den <= 0, x) == \
        Or(And(-oo < x, x < 1), And(S(1) < x, x < 2))
Example #4
0
def test_solve_univariate_inequality():
    assert isolve(x**2 >= 4, x, relational=False) == Union(Interval(-oo, -2),
        Interval(2, oo))
    assert isolve(x**2 >= 4, x) == Or(And(Le(2, x), Lt(x, oo)), And(Le(x, -2),
        Lt(-oo, x)))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x, relational=False) == \
        Union(Interval(1, 2), Interval(3, oo))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x) == \
        Or(And(Le(1, x), Le(x, 2)), And(Le(3, x), Lt(x, oo)))
    # issue 2785:
    assert isolve(x**3 - 2*x - 1 > 0, x, relational=False) == \
        Union(Interval(-1, -sqrt(5)/2 + S(1)/2, True, True),
              Interval(S(1)/2 + sqrt(5)/2, oo, True, True))
    # issue 2794:
    assert isolve(x**3 - x**2 + x - 1 > 0, x, relational=False) == \
        Interval(1, oo, True)

    # numerical testing in valid() is needed
    assert isolve(x**7 - x - 2 > 0, x) == \
        And(rootof(x**7 - x - 2, 0) < x, x < oo)

    # handle numerator and denominator; although these would be handled as
    # rational inequalities, these test confirm that the right thing is done
    # when the domain is EX (e.g. when 2 is replaced with sqrt(2))
    assert isolve(1/(x - 2) > 0, x) == And(S(2) < x, x < oo)
    den = ((x - 1)*(x - 2)).expand()
    assert isolve((x - 1)/den <= 0, x) == \
        Or(And(-oo < x, x < 1), And(S(1) < x, x < 2))

    n = Dummy('n')
    raises(NotImplementedError, lambda: isolve(Abs(x) <= n, x, relational=False))
Example #5
0
def test_reduce_inequalities_multivariate():
    x = Symbol('x')
    y = Symbol('y')

    assert reduce_inequalities([Ge(x**2, 1), Ge(y**2, 1)]) == \
        And(Eq(im(x), 0), Eq(im(y), 0), Or(And(Le(1, re(x)), Lt(re(x), oo)),
                                           And(Le(re(x), -1), Lt(-oo, re(x)))),
            Or(And(Le(1, re(y)), Lt(re(y), oo)), And(Le(re(y), -1), Lt(-oo, re(y)))))
Example #6
0
def test_reduce_abs_inequalities():
    x = Symbol('x', real=True)

    assert reduce_inequalities(abs(x - 5) < 3) == And(Lt(2, x), Lt(x, 8))
    assert reduce_inequalities(abs(2 * x + 3) >= 8) == Or(
        And(Le(S(5) / 2, x), Lt(x, oo)), And(Le(x, -S(11) / 2), Lt(-oo, x)))
    assert reduce_inequalities(abs(x - 4) + abs(3 * x - 5) < 7) == And(
        Lt(S(1) / 2, x), Lt(x, 4))
    assert reduce_inequalities(abs(x - 4) + abs(3*abs(x) - 5) < 7) == \
        Or(And(S(-2) < x, x < -1), And(S(1)/2 < x, x < 4))

    x = Symbol('x')
    raises(NotImplementedError, lambda: reduce_inequalities(abs(x - 5) < 3))
def test_Infinity_inequations():
    assert oo > pi
    assert not (oo < pi)
    assert exp(-3) < oo

    assert Float('+inf') > pi
    assert not (Float('+inf') < pi)
    assert exp(-3) < Float('+inf')

    raises(TypeError, lambda: oo < I)
    raises(TypeError, lambda: oo <= I)
    raises(TypeError, lambda: oo > I)
    raises(TypeError, lambda: oo >= I)
    raises(TypeError, lambda: -oo < I)
    raises(TypeError, lambda: -oo <= I)
    raises(TypeError, lambda: -oo > I)
    raises(TypeError, lambda: -oo >= I)

    raises(TypeError, lambda: I < oo)
    raises(TypeError, lambda: I <= oo)
    raises(TypeError, lambda: I > oo)
    raises(TypeError, lambda: I >= oo)
    raises(TypeError, lambda: I < -oo)
    raises(TypeError, lambda: I <= -oo)
    raises(TypeError, lambda: I > -oo)
    raises(TypeError, lambda: I >= -oo)

    assert oo > -oo and oo >= -oo
    assert (oo < -oo) == False and (oo <= -oo) == False
    assert -oo < oo and -oo <= oo
    assert (-oo > oo) == False and (-oo >= oo) == False

    assert (oo < oo) == False  # issue 7775
    assert (oo > oo) == False
    assert (-oo > -oo) == False and (-oo < -oo) == False
    assert oo >= oo and oo <= oo and -oo >= -oo and -oo <= -oo
    assert (-oo < -Float('inf')) == False
    assert (oo > Float('inf')) == False
    assert -oo >= -Float('inf')
    assert oo <= Float('inf')

    x = Symbol('x')
    b = Symbol('b', finite=True, real=True)
    assert (x < oo) == Lt(x, oo)  # issue 7775
    assert b < oo and b > -oo and b <= oo and b >= -oo
    assert oo > b and oo >= b and (oo < b) == False and (oo <= b) == False
    assert (-oo > b) == False and (-oo >= b) == False and -oo < b and -oo <= b
    assert (oo < x) == Lt(oo, x) and (oo > x) == Gt(oo, x)
    assert (oo <= x) == Le(oo, x) and (oo >= x) == Ge(oo, x)
    assert (-oo < x) == Lt(-oo, x) and (-oo > x) == Gt(-oo, x)
    assert (-oo <= x) == Le(-oo, x) and (-oo >= x) == Ge(-oo, x)
Example #8
0
def test_reduce_abs_inequalities():
    real = Q.real(x)

    assert reduce_inequalities(abs(x - 5) < 3,
                               assume=real) == And(Lt(2, x), Lt(x, 8))
    assert reduce_inequalities(abs(2 * x + 3) >= 8, assume=real) == Or(
        And(Le(S(5) / 2, x), Lt(x, oo)), And(Le(x, -S(11) / 2), Lt(-oo, x)))
    assert reduce_inequalities(abs(x - 4) + abs(3 * x - 5) < 7,
                               assume=real) == And(Lt(S(1) / 2, x), Lt(x, 4))
    assert reduce_inequalities(abs(x - 4) + abs(3 * abs(x) - 5) < 7,
                               assume=real) == Or(And(S(-2) < x, x < -1),
                                                  And(S(1) / 2 < x, x < 4))

    raises(NotImplementedError, lambda: reduce_inequalities(abs(x - 5) < 3))
Example #9
0
def test_reduce_abs_inequalities():
    e = abs(x - 5) < 3
    ans = And(Lt(2, x), Lt(x, 8))
    assert reduce_inequalities(e) == ans
    assert reduce_inequalities(e, x) == ans
    assert reduce_inequalities(abs(x - 5)) == Eq(x, 5)
    assert reduce_inequalities(abs(2 * x + 3) >= 8) == Or(
        And(Le(S(5) / 2, x), Lt(x, oo)), And(Le(x, -S(11) / 2), Lt(-oo, x)))
    assert reduce_inequalities(abs(x - 4) + abs(3 * x - 5) < 7) == And(
        Lt(S(1) / 2, x), Lt(x, 4))
    assert reduce_inequalities(abs(x - 4) + abs(3*abs(x) - 5) < 7) == \
        Or(And(S(-2) < x, x < -1), And(S(1)/2 < x, x < 4))

    nr = Symbol('nr', real=False)
    raises(TypeError, lambda: reduce_inequalities(abs(nr - 5) < 3))
Example #10
0
 def pmf(self, x):
     x = sympify(x)
     if not (x.is_number or x.is_Symbol or isinstance(x, RandomSymbol)):
         raise ValueError("'x' expected as an argument of type 'number' or 'Symbol' or , "
                     "'RandomSymbol' not %s" % (type(x)))
     cond = Ge(x, 1) & Le(x, self.sides) & Contains(x, S.Integers)
     return Piecewise((S.One/self.sides, cond), (S.Zero, True))
Example #11
0
 def check(k, delta, c):
     _value_check(k.is_integer and k.is_positive,
                  "'k' must be a positive integer")
     _value_check(
         Gt(delta, 0) and Le(delta, 1),
         "'delta' must be a real number in the interval (0,1)")
     _value_check(c.is_positive, "'c' must be a positive real number.")
Example #12
0
def test_Sum_doit():
    assert Sum(n * Integral(a**2), (n, 0, 2)).doit() == a**3
    assert Sum(n*Integral(a**2), (n, 0, 2)).doit(deep=False) == \
        3*Integral(a**2)
    assert summation(n * Integral(a**2), (n, 0, 2)) == 3 * Integral(a**2)

    # test nested sum evaluation
    s = Sum(Sum(Sum(2, (z, 1, n + 1)), (y, x + 1, n)), (x, 1, n))
    assert 0 == (s.doit() - n * (n + 1) * (n - 1)).factor()

    assert Sum(Sum(KroneckerDelta(m, n), (m, 1, 3)), (n, 1, 3)).doit() == 3
    assert Sum(Sum(KroneckerDelta(k, m), (m, 1, 3)), (n, 1, 3)).doit() == \
        3*Piecewise((1, And(S(1) <= k, k <= 3)), (0, True))
    assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, 3)).doit() == \
        f(1) + f(2) + f(3)
    assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, oo)).doit() == \
        Sum(Piecewise((f(n), And(Le(0, n), n < oo)), (0, True)), (n, 1, oo))
    l = Symbol('l', integer=True, positive=True)
    assert Sum(f(l)*Sum(KroneckerDelta(m, l), (m, 0, oo)), (l, 1, oo)).doit() == \
        Sum(f(l), (l, 1, oo))

    # issue 2597
    nmax = symbols('N', integer=True, positive=True)
    pw = Piecewise((1, And(S(1) <= n, n <= nmax)), (0, True))
    assert Sum(pw, (n, 1, nmax)).doit() == Sum(pw, (n, 1, nmax))
Example #13
0
def test_relational_noncommutative():
    from sympy import Lt, Gt, Le, Ge
    a, b = symbols('a b', commutative=False)
    assert (a < b) == Lt(a, b)
    assert (a <= b) == Le(a, b)
    assert (a > b) == Gt(a, b)
    assert (a >= b) == Ge(a, b)
Example #14
0
def test_issue_21869():
    x = Symbol('x', real=True)
    y = Symbol('y', real=True)
    expr = And(Eq(x**2, 4), Le(x, y))
    assert expr.simplify() == expr

    expr = And(Eq(x**2, 4), Eq(x, 2))
    assert expr.simplify() == Eq(x, 2)

    expr = And(Eq(x**3, x**2), Eq(x, 1))
    assert expr.simplify() == Eq(x, 1)

    expr = And(Eq(sin(x), x**2), Eq(x, 0))
    assert expr.simplify() == Eq(x, 0)

    expr = And(Eq(x**3, x**2), Eq(x, 2))
    assert expr.simplify() == S.false

    expr = And(Eq(y, x**2), Eq(x, 1))
    assert expr.simplify() == And(Eq(y, 1), Eq(x, 1))

    expr = And(Eq(y**2, 1), Eq(y, x**2), Eq(x, 1))
    assert expr.simplify() == And(Eq(y, 1), Eq(x, 1))

    expr = And(Eq(y**2, 4), Eq(y, 2 * x**2), Eq(x, 1))
    assert expr.simplify() == And(Eq(y, 2), Eq(x, 1))

    expr = And(Eq(y**2, 4), Eq(y, x**2), Eq(x, 1))
    assert expr.simplify() == S.false
Example #15
0
    def _visit_ComparisonNode(self, stmt):

        first = self._visit(stmt.first)
        second = self._visit(stmt.second)
        op = stmt.value.first
        if op == '==':
            return Eq(first, second, evaluate=False)
        elif op == '!=':

            return Ne(first, second, evaluate=False)
        elif op == '<':

            return Lt(first, second, evaluate=False)
        elif op == '>':

            return Gt(first, second, evaluate=False)
        elif op == '<=':

            return Le(first, second, evaluate=False)
        elif op == '>=':

            return Ge(first, second, evaluate=False)
        elif op == 'is':

            return Is(first, second)
        else:
            msg = 'unknown/unavailable binary operator {node}'
            msg = msg.format(node=type(op))
            raise PyccelSyntaxError(msg)
Example #16
0
def test_relational_noncommutative():
    from sympy import Lt, Gt, Le, Ge
    A, B = symbols('A,B', commutative=False)
    assert (A < B) == Lt(A, B)
    assert (A <= B) == Le(A, B)
    assert (A > B) == Gt(A, B)
    assert (A >= B) == Ge(A, B)
Example #17
0
def test_python_relational():
    assert python(Eq(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x == y"
    assert python(Le(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x <= y"
    assert python(Gt(x, y)) == "y = Symbol('y')\nx = Symbol('x')\ne = y < x"
    assert python(Ne(x/(y+1), y**2)) in [
            "x = Symbol('x')\ny = Symbol('y')\ne = x/(1 + y) != y**2",
            "x = Symbol('x')\ny = Symbol('y')\ne = x/(y + 1) != y**2"]
Example #18
0
def test_pretty_relational():
    assert pretty(Eq(x, y)) == 'x = y'
    assert pretty(Le(x, y)) == 'x <= y'
    assert pretty(Gt(x, y)) == 'y < x'
    assert pretty(Ne(x / (y + 1), y**2)) in [
        '  x       2\n----- != y \n1 + y      ',
        '  x       2\n----- != y \ny + 1      '
    ]
Example #19
0
def test_reduce_abs_inequalities():
    e = abs(x - 5) < 3
    ans = And(Lt(2, x), Lt(x, 8))
    assert reduce_inequalities(e) == ans
    assert reduce_inequalities(e, x) == ans
    assert reduce_inequalities(abs(x - 5)) == Eq(x, 5)
    assert reduce_inequalities(abs(2 * x + 3) >= 8) == Or(
        And(Le(Rational(5, 2), x), Lt(x, oo)),
        And(Le(x, Rational(-11, 2)), Lt(-oo, x)))
    assert reduce_inequalities(abs(x - 4) + abs(3 * x - 5) < 7) == And(
        Lt(S.Half, x), Lt(x, 4))
    assert reduce_inequalities(abs(x - 4) + abs(3*abs(x) - 5) < 7) == \
        Or(And(S(-2) < x, x < -1), And(S.Half < x, x < 4))

    nr = Symbol('nr', extended_real=False)
    raises(TypeError, lambda: reduce_inequalities(abs(nr - 5) < 3))
    assert reduce_inequalities(x < 3, symbols=[x, nr]) == And(-oo < x, x < 3)
Example #20
0
 def pmf(self, x):
     n, p = self.n, self.p
     x = sympify(x)
     if not (x.is_number or x.is_Symbol or isinstance(x, RandomSymbol)):
         raise ValueError("'x' expected as an argument of type 'number' or 'Symbol' or , "
                     "'RandomSymbol' not %s" % (type(x)))
     cond = Ge(x, 0) & Le(x, n) & Contains(x, S.Integers)
     return Piecewise((binomial(n, x) * p**x * (1 - p)**(n - x), cond), (S.Zero, True))
Example #21
0
def test_ccode_Relational():
    from sympy import Eq, Ne, Le, Lt, Gt, Ge
    assert ccode(Eq(x, y)) == "x == y"
    assert ccode(Ne(x, y)) == "x != y"
    assert ccode(Le(x, y)) == "x <= y"
    assert ccode(Lt(x, y)) == "x < y"
    assert ccode(Gt(x, y)) == "x > y"
    assert ccode(Ge(x, y)) == "x >= y"
Example #22
0
def test_reduce_poly_inequalities_real_relational():
    global_assumptions.add(Q.real(x))
    global_assumptions.add(Q.real(y))

    assert reduce_poly_inequalities([[Eq(x**2, 0)]], x, relational=True) == Eq(x, 0)
    assert reduce_poly_inequalities([[Le(x**2, 0)]], x, relational=True) == Eq(x, 0)
    assert reduce_poly_inequalities([[Lt(x**2, 0)]], x, relational=True) == False
    assert reduce_poly_inequalities([[Ge(x**2, 0)]], x, relational=True) == True
    assert reduce_poly_inequalities([[Gt(x**2, 0)]], x, relational=True) == Or(Lt(x, 0), Lt(0, x))
    assert reduce_poly_inequalities([[Ne(x**2, 0)]], x, relational=True) == Or(Lt(x, 0), Lt(0, x))

    assert reduce_poly_inequalities([[Eq(x**2, 1)]], x, relational=True) == Or(Eq(x, -1), Eq(x, 1))
    assert reduce_poly_inequalities([[Le(x**2, 1)]], x, relational=True) == And(Le(-1, x), Le(x, 1))
    assert reduce_poly_inequalities([[Lt(x**2, 1)]], x, relational=True) == And(Lt(-1, x), Lt(x, 1))
    assert reduce_poly_inequalities([[Ge(x**2, 1)]], x, relational=True) == Or(Le(x, -1), Le(1, x))
    assert reduce_poly_inequalities([[Gt(x**2, 1)]], x, relational=True) == Or(Lt(x, -1), Lt(1, x))
    assert reduce_poly_inequalities([[Ne(x**2, 1)]], x, relational=True) == Or(Lt(x, -1), And(Lt(-1, x), Lt(x, 1)), Lt(1, x))

    assert reduce_poly_inequalities([[Eq(x**2, 1.0)]], x, relational=True).evalf() == Or(Eq(x, -1.0), Eq(x, 1.0)).evalf()
    assert reduce_poly_inequalities([[Le(x**2, 1.0)]], x, relational=True) == And(Le(-1.0, x), Le(x, 1.0))
    assert reduce_poly_inequalities([[Lt(x**2, 1.0)]], x, relational=True) == And(Lt(-1.0, x), Lt(x, 1.0))
    assert reduce_poly_inequalities([[Ge(x**2, 1.0)]], x, relational=True) == Or(Le(x, -1.0), Le(1.0, x))
    assert reduce_poly_inequalities([[Gt(x**2, 1.0)]], x, relational=True) == Or(Lt(x, -1.0), Lt(1.0, x))
    assert reduce_poly_inequalities([[Ne(x**2, 1.0)]], x, relational=True) == Or(Lt(x, -1.0), And(Lt(-1.0, x), Lt(x, 1.0)), Lt(1.0, x))

    global_assumptions.remove(Q.real(x))
    global_assumptions.remove(Q.real(y))
def test_python_relational():
    assert python(Eq(x, y)) == "e = Eq(x, y)"
    assert python(Ge(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x >= y"
    assert python(Le(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x <= y"
    assert python(Gt(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x > y"
    assert python(Lt(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x < y"
    assert python(
        Ne(x / (y + 1),
           y**2)) in ["e = Ne(x/(1 + y), y**2)", "e = Ne(x/(y + 1), y**2)"]
Example #24
0
def test_solve_univariate_inequality():
    x = Symbol('x', real=True)
    assert isolve(x**2 >= 4,
                  x, relational=False) == Union(Interval(-oo, -2),
                                                Interval(2, oo))
    assert isolve(x**2 >= 4, x) == Or(And(Le(2, x), Lt(x, oo)),
                                      And(Le(x, -2), Lt(-oo, x)))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x, relational=False) == \
        Union(Interval(1, 2), Interval(3, oo))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x) == \
        Or(And(Le(1, x), Le(x, 2)), And(Le(3, x), Lt(x, oo)))
    # issue 2785:
    assert isolve(x**3 - 2*x - 1 > 0, x, relational=False) == \
        Union(Interval(-1, -sqrt(5)/2 + S(1)/2, True, True),
              Interval(S(1)/2 + sqrt(5)/2, oo, True, True))
    # issue 2794:
    assert isolve(x**3 - x**2 + x - 1 > 0, x, relational=False) == \
        Interval(1, oo, True)
Example #25
0
def test_reduce_abs_inequalities():
    real = Q.real(x)

    assert reduce_inequalities(abs(x - 5) < 3, assume=real) == And(Gt(x, 2), Lt(x, 8))
    assert reduce_inequalities(abs(2*x + 3) >= 8, assume=real) == Or(Le(x, -S(11)/2), Ge(x, S(5)/2))
    assert reduce_inequalities(abs(x - 4) + abs(3*x - 5) < 7, assume=real) == And(Gt(x, S(1)/2), Lt(x, 4))
    assert reduce_inequalities(abs(x - 4) + abs(3*abs(x) - 5) < 7, assume=real) == Or(And(-2 < x, x < -1), And(S(1)/2 < x, x < 4))

    raises(NotImplementedError, "reduce_inequalities(abs(x - 5) < 3)")
Example #26
0
    def pmf(self, x):
        x = sympify(x)
        if not (x.is_number or x.is_Symbol or is_random(x)):
            raise ValueError(
                "'x' expected as an argument of type 'number' or 'Symbol' or , "
                "'RandomSymbol' not %s" % (type(x)))

        cond1 = Eq(x, 1) & x.is_integer
        cond2 = Ge(x, 1) & Le(x, self.k) & x.is_integer
        rho = Piecewise((Rational(1, self.k), cond1),
                        (Rational(1, x * (x - 1)), cond2), (S.Zero, True))

        cond1 = Ge(x, 1) & Le(x, round(self.k / self.R) - 1)
        cond2 = Eq(x, round(self.k / self.R))
        tau = Piecewise((self.R / (self.k * x), cond1),
                        (self.R * log(self.R / self.delta) / self.k, cond2),
                        (S.Zero, True))

        return (rho + tau) / self.Z
Example #27
0
 def pmf(self, x):
     x = sympify(x)
     if not (x.is_number or x.is_Symbol or is_random(x)):
         raise ValueError(
             "'x' expected as an argument of type 'number' or 'Symbol' or , "
             "'RandomSymbol' not %s" % (type(x)))
     cond1 = Eq(x, 1) & x.is_integer
     cond2 = Ge(x, 1) & Le(x, self.k) & x.is_integer
     return Piecewise((1 / self.k, cond1), (1 / (x * (x - 1)), cond2),
                      (S.Zero, True))
Example #28
0
def test_Interval_as_relational():
    x = Symbol('x')

    assert Interval(-1, 2, False, False).as_relational(x) == \
        And(Le(-1, x), Le(x, 2))
    assert Interval(-1, 2, True, False).as_relational(x) == \
        And(Lt(-1, x), Le(x, 2))
    assert Interval(-1, 2, False, True).as_relational(x) == \
        And(Le(-1, x), Lt(x, 2))
    assert Interval(-1, 2, True, True).as_relational(x) == \
        And(Lt(-1, x), Lt(x, 2))

    assert Interval(-oo, 2, right_open=False).as_relational(x) == Le(x, 2)
    assert Interval(-oo, 2, right_open=True).as_relational(x) == Lt(x, 2)

    assert Interval(-2, oo, left_open=False).as_relational(x) == Ge(x, -2)
    assert Interval(-2, oo, left_open=True).as_relational(x) == Gt(x, -2)

    assert Interval(-oo, oo).as_relational(x) is True
Example #29
0
  def init():
    # FIXME: Are (+-)oo correctly handled?
    Expr.__add__ = \
        lambda s, e: Expr(apply_and_simplify(s.expr, e.expr, operator.add))
    Expr.__sub__ = \
        lambda s, e: Expr(apply_and_simplify(s.expr, e.expr, operator.sub))
    Expr.__mul__ = \
        lambda s, e: Expr(apply_and_simplify(s.expr, e.expr, operator.mul, \
            invert_on_negative=True))
    Expr.__div__ = \
        lambda s, e: Expr(apply_and_simplify(s.expr, e.expr, operator.div, \
            invert_on_negative=True))
    Expr.__pow__ = lambda s, e: Expr(s.expr ** e.expr)
    Expr.__neg__ = lambda s: Expr(-s.expr)

    Expr.__eq__ = lambda s, e: Expr(Eq(s.expr, e.expr))
    Expr.__ne__ = lambda s, e: Expr(Ne(s.expr, e.expr))
    Expr.__lt__ = lambda s, e: Expr(Lt(s.expr, e.expr))
    Expr.__le__ = lambda s, e: Expr(Le(s.expr, e.expr))
    Expr.__gt__ = lambda s, e: Expr(Gt(s.expr, e.expr))
    Expr.__ge__ = lambda s, e: Expr(Ge(s.expr, e.expr))

    Expr.__and__    = lambda s, e: Expr(And(s.expr, e.expr))
    Expr.__or__     = lambda s, e: Expr(Or(s.expr,  e.expr))
    Expr.__invert__ = lambda s:    Expr(Not(s.expr))

    Expr.is_eq       = lambda s, e: s.expr == e.expr
    Expr.is_ne       = lambda s, e: s.expr != e.expr
    Expr.is_empty    = lambda s: s.is_eq(Expr.empty)

    Expr.is_inf       = lambda s: s.expr == S.Infinity or s.expr == -S.Infinity
    Expr.is_plus_inf  = lambda s: s.expr == S.Infinity
    Expr.is_minus_inf = lambda s: s.expr == -S.Infinity

    Expr.is_constant = lambda s: isinstance(s.expr, Integer)
    Expr.is_integer  = lambda s: isinstance(s.expr, Integer)
    Expr.is_rational = lambda s: isinstance(s.expr, Rational)
    Expr.is_symbol   = lambda s: isinstance(s.expr, Symbol)

    Expr.is_min = lambda s: isinstance(s.expr, Min)
    Expr.is_max = lambda s: isinstance(s.expr, Max)
    Expr.is_add = lambda s: isinstance(s.expr, Add)
    Expr.is_mul = lambda s: isinstance(s.expr, Mul)
    Expr.is_pow = lambda s: isinstance(s.expr, Pow)

    Expr.get_integer = lambda s: s.expr.p
    Expr.get_numer   = lambda s: s.expr.p
    Expr.get_denom   = lambda s: s.expr.q

    Expr.get_name = lambda s: s.expr.name

    Expr.compare = lambda s, e: s.compare(e)

    # Empty. When min/max is invalid.
    Expr.empty = Expr("EMPTY")
Example #30
0
def test_Interval_as_relational():
    x = Symbol('x')

    assert Interval(-1, 2, False, False).as_relational(x) == \
        And(Le(-1, x), Le(x, 2))
    assert Interval(-1, 2, True, False).as_relational(x) == \
        And(Lt(-1, x), Le(x, 2))
    assert Interval(-1, 2, False, True).as_relational(x) == \
        And(Le(-1, x), Lt(x, 2))
    assert Interval(-1, 2, True, True).as_relational(x) == \
        And(Lt(-1, x), Lt(x, 2))

    assert Interval(-oo, 2, right_open=False).as_relational(x) == And(
        Lt(-oo, x), Le(x, 2))
    assert Interval(-oo, 2, right_open=True).as_relational(x) == And(
        Lt(-oo, x), Lt(x, 2))

    assert Interval(-2, oo, left_open=False).as_relational(x) == And(
        Le(-2, x), Lt(x, oo))
    assert Interval(-2, oo, left_open=True).as_relational(x) == And(
        Lt(-2, x), Lt(x, oo))

    assert Interval(-oo, oo).as_relational(x) == And(Lt(-oo, x), Lt(x, oo))
    x = Symbol('x', real=True)
    y = Symbol('y', real=True)
    assert Interval(x, y).as_relational(x) == (x <= y)
    assert Interval(y, x).as_relational(x) == (y <= x)