Пример #1
0
def test_reduce_rational_inequalities_real_relational():
    assert reduce_rational_inequalities([], x) == False
    assert reduce_rational_inequalities(
        [[(x**2 + 3*x + 2)/(x**2 - 16) >= 0]], x, relational=False) == \
        Union(Interval.open(-oo, -4), Interval(-2, -1), Interval.open(4, oo))

    assert reduce_rational_inequalities(
        [[((-2*x - 10)*(3 - x))/((x**2 + 5)*(x - 2)**2) < 0]], x,
        relational=False) == \
        Union(Interval.open(-5, 2), Interval.open(2, 3))

    assert reduce_rational_inequalities([[(x + 1)/(x - 5) <= 0]], x,
        relational=False) == \
        Interval.Ropen(-1, 5)

    assert reduce_rational_inequalities([[(x**2 + 4*x + 3)/(x - 1) > 0]], x,
        relational=False) == \
        Union(Interval.open(-3, -1), Interval.open(1, oo))

    assert reduce_rational_inequalities([[(x**2 - 16)/(x - 1)**2 < 0]], x,
        relational=False) == \
        Union(Interval.open(-4, 1), Interval.open(1, 4))

    assert reduce_rational_inequalities([[(3*x + 1)/(x + 4) >= 1]], x,
        relational=False) == \
        Union(Interval.open(-oo, -4), Interval.Ropen(S(3)/2, oo))

    assert reduce_rational_inequalities([[(x - 8)/x <= 3 - x]], x,
        relational=False) == \
        Union(Interval.Lopen(-oo, -2), Interval.Lopen(0, 4))

    # issue sympy/sympy#10237
    assert reduce_rational_inequalities([[x < oo, x >= 0, -oo < x]],
                                        x,
                                        relational=False) == Interval(0, oo)
Пример #2
0
def test_reduce_rational_inequalities_real_relational():
    assert reduce_rational_inequalities([], x) == False
    assert reduce_rational_inequalities(
        [[(x**2 + 3*x + 2)/(x**2 - 16) >= 0]], x, relational=False) == \
        Union(Interval.open(-oo, -4), Interval(-2, -1), Interval.open(4, oo))

    assert reduce_rational_inequalities(
        [[((-2*x - 10)*(3 - x))/((x**2 + 5)*(x - 2)**2) < 0]], x,
        relational=False) == \
        Union(Interval.open(-5, 2), Interval.open(2, 3))

    assert reduce_rational_inequalities([[(x + 1)/(x - 5) <= 0]], x,
        relational=False) == \
        Interval.Ropen(-1, 5)

    assert reduce_rational_inequalities([[(x**2 + 4*x + 3)/(x - 1) > 0]], x,
        relational=False) == \
        Union(Interval.open(-3, -1), Interval.open(1, oo))

    assert reduce_rational_inequalities([[(x**2 - 16)/(x - 1)**2 < 0]], x,
        relational=False) == \
        Union(Interval.open(-4, 1), Interval.open(1, 4))

    assert reduce_rational_inequalities([[(3*x + 1)/(x + 4) >= 1]], x,
        relational=False) == \
        Union(Interval.open(-oo, -4), Interval.Ropen(S(3)/2, oo))

    assert reduce_rational_inequalities([[(x - 8)/x <= 3 - x]], x,
        relational=False) == \
        Union(Interval.Lopen(-oo, -2), Interval.Lopen(0, 4))

    # issue sympy/sympy#10237
    assert reduce_rational_inequalities(
        [[x < oo, x >= 0, -oo < x]], x, relational=False) == Interval(0, oo)
Пример #3
0
def test_reduce_rational_inequalities_real_relational():
    def OpenInterval(a, b):
        return Interval(a, b, True, True)

    def LeftOpenInterval(a, b):
        return Interval(a, b, True, False)

    def RightOpenInterval(a, b):
        return Interval(a, b, False, True)

    x = Symbol('x', real=True)

    assert reduce_rational_inequalities([[(x**2 + 3*x + 2)/(x**2 - 16) >= 0]], x, relational=False) == \
        Union(OpenInterval(-oo, -4), Interval(-2, -1), OpenInterval(4, oo))

    assert reduce_rational_inequalities([[((-2*x - 10)*(3 - x))/((x**2 + 5)*(x - 2)**2) < 0]], x, relational=False) == \
        Union(OpenInterval(-5, 2), OpenInterval(2, 3))

    assert reduce_rational_inequalities([[(x + 1)/(x - 5) <= 0]], x, relational=False) == \
        RightOpenInterval(-1, 5)

    assert reduce_rational_inequalities([[(x**2 + 4*x + 3)/(x - 1) > 0]], x, relational=False) == \
        Union(OpenInterval(-3, -1), OpenInterval(1, oo))

    assert reduce_rational_inequalities([[(x**2 - 16)/(x - 1)**2 < 0]], x, relational=False) == \
        Union(OpenInterval(-4, 1), OpenInterval(1, 4))

    assert reduce_rational_inequalities([[(3*x + 1)/(x + 4) >= 1]], x, relational=False) == \
        Union(OpenInterval(-oo, -4), RightOpenInterval(S(3)/2, oo))

    assert reduce_rational_inequalities([[(x - 8)/x <= 3 - x]], x, relational=False) == \
        Union(LeftOpenInterval(-oo, -2), LeftOpenInterval(0, 4))
Пример #4
0
def test_reduce_rational_inequalities_real_relational():
    def OpenInterval(a, b):
        return Interval(a, b, True, True)
    def LeftOpenInterval(a, b):
        return Interval(a, b, True, False)
    def RightOpenInterval(a, b):
        return Interval(a, b, False, True)

    with assuming(Q.real(x)):
        assert reduce_rational_inequalities([[(x**2 + 3*x + 2)/(x**2 - 16) >= 0]], x, relational=False) == \
            Union(OpenInterval(-oo, -4), Interval(-2, -1), OpenInterval(4, oo))

        assert reduce_rational_inequalities([[((-2*x - 10)*(3 - x))/((x**2 + 5)*(x - 2)**2) < 0]], x, relational=False) == \
            Union(OpenInterval(-5, 2), OpenInterval(2, 3))

        assert reduce_rational_inequalities([[(x + 1)/(x - 5) <= 0]], x, assume=Q.real(x), relational=False) == \
            RightOpenInterval(-1, 5)

        assert reduce_rational_inequalities([[(x**2 + 4*x + 3)/(x - 1) > 0]], x, assume=Q.real(x), relational=False) == \
            Union(OpenInterval(-3, -1), OpenInterval(1, oo))

        assert reduce_rational_inequalities([[(x**2 - 16)/(x - 1)**2 < 0]], x, assume=Q.real(x), relational=False) == \
            Union(OpenInterval(-4, 1), OpenInterval(1, 4))

        assert reduce_rational_inequalities([[(3*x + 1)/(x + 4) >= 1]], x, assume=Q.real(x), relational=False) == \
            Union(OpenInterval(-oo, -4), RightOpenInterval(S(3)/2, oo))

        assert reduce_rational_inequalities([[(x - 8)/x <= 3 - x]], x, assume=Q.real(x), relational=False) == \
            Union(LeftOpenInterval(-oo, -2), LeftOpenInterval(0, 4))
Пример #5
0
def _has_conflict(rd1, rd2, offset):
    """
    :param rd1: rectangular domain from Stencil1 (1D) <- (low, high, stride) (written domain)
    :param rd2: rectangular domain from Stencil2 (1D) <- (low, high, stride) (read domain)
    :param offset_vector: Offset from the center (relative to rd2), 1D projections
    :return: Whether OV from rectangular domain 2 reads from rectangular domain 1
    """

    n1 = sympy.Symbol("n1")
    n2 = sympy.Symbol("n2")

    # Diophantine equations:
    # offset(iterationspace1) + n1 * stride(iteration_space1) <- write vectors
    diophantine_eq1 = rd1[0] + n1 * rd1[2]
    # offset(iterationspace2) + n2 * stride(iteration_space2) + offset_vector  <- Read vectors
    diophantine_eq2 = rd2[0] + n2 * rd2[2] + offset
    # Since sympy solves eq = 0, we want dio_eq1 - dio_eq2 = 0.
    eqn = diophantine_eq1 - diophantine_eq2
    parameter = sympy.Symbol("t", integer=True)
    sat_param = sympy.Symbol("t_0", integer=True)
    if not eqn.free_symbols:
        return eqn == 0
    solutions = dio.diophantine(eqn, parameter)  # default parameter is "t"
    # print("Sols:", solutions)
    for sol in solutions:
        if len(eqn.free_symbols) != 2:
            if n1 in eqn.free_symbols:
                parametric_n1 = sol[0]
                parametric_n2 = 0
            else:
                parametric_n1 = 0
                parametric_n2 = sol[0]
        else:
            (parametric_n1, parametric_n2) = sol
        # Solutions is a set of tuples, each containing either a number or parametric expression
        # which give conditions on satisfiability.

        # are these satisfiable on the original bounds?
        # substitute the parametric forms in
        substituted_1 = diophantine_eq1.subs({n1: parametric_n1})
        substituted_2 = (
            rd2[0] + parametric_n2 * rd2[2]
        )  # we ditch the offset because it's irrelevant since the bounds are based on the center of the stencil

        # print(substituted_1, "\t", substituted_2)
        # print(rd1[0], rd1[1], rd2[0], rd2[1])
        # now do they satisfy the bounds?
        satisfactory_interval = ineq.reduce_rational_inequalities(
            [[rd1[0] <= substituted_1, rd1[1] > substituted_1, rd2[0] <= substituted_2, rd2[1] > substituted_2]],
            sat_param,
            relational=False,
        )
        # print(satisfactory_interval)

        if _contains_integer(satisfactory_interval):
            return True
    return False
Пример #6
0
def crecimiento(f):
    #crecimiento, decrecimiento y puntos singulares
    crec = reduce_rational_inequalities([[f.diff() > 0]], x, relational=False)
    decrec = reduce_rational_inequalities([[f.diff() < 0]],
                                          x,
                                          relational=False)
    sing = []
    for t in solve(f.diff()):
        if f.diff().diff().subs(x, t) < 0:
            tipo = 'máx'
        elif f.diff().diff().subs(x, t) > 0:
            tipo = 'mín'
        else:
            tipo = 'inflex'
        sing.append([t, tipo])

    sol = [crec, decrec, sing]

    return sol
Пример #7
0
def test_reduce_poly_inequalities_complex_relational():
    assert reduce_rational_inequalities(
        [[Eq(x**2, 0)]], x, relational=True) == Eq(x, 0)
    assert reduce_rational_inequalities(
        [[Le(x**2, 0)]], x, relational=True) == Eq(x, 0)
    assert reduce_rational_inequalities(
        [[Lt(x**2, 0)]], x, relational=True) == False
    assert reduce_rational_inequalities(
        [[Ge(x**2, 0)]], x, relational=True) == And(Lt(-oo, x), Lt(x, oo))
    assert reduce_rational_inequalities(
        [[Gt(x**2, 0)]], x, relational=True) == \
        And(Gt(x, -oo), Lt(x, oo), Ne(x, 0))
    assert reduce_rational_inequalities(
        [[Ne(x**2, 0)]], x, relational=True) == \
        And(Gt(x, -oo), Lt(x, oo), Ne(x, 0))

    for one in (S(1), S(1.0)):
        inf = one*oo
        assert reduce_rational_inequalities(
            [[Eq(x**2, one)]], x, relational=True) == \
            Or(Eq(x, -one), Eq(x, one))
        assert reduce_rational_inequalities(
            [[Le(x**2, one)]], x, relational=True) == \
            And(And(Le(-one, x), Le(x, one)))
        assert reduce_rational_inequalities(
            [[Lt(x**2, one)]], x, relational=True) == \
            And(And(Lt(-one, x), Lt(x, one)))
        assert reduce_rational_inequalities(
            [[Ge(x**2, one)]], x, relational=True) == \
            And(Or(And(Le(one, x), Lt(x, inf)), And(Le(x, -one), Lt(-inf, x))))
        assert reduce_rational_inequalities(
            [[Gt(x**2, one)]], x, relational=True) == \
            And(Or(And(Lt(-inf, x), Lt(x, -one)), And(Lt(one, x), Lt(x, inf))))
        assert reduce_rational_inequalities(
            [[Ne(x**2, one)]], x, relational=True) == \
            Or(And(Lt(-inf, x), Lt(x, -one)),
               And(Lt(-one, x), Lt(x, one)),
               And(Lt(one, x), Lt(x, inf)))
Пример #8
0
def _reduce_inequalities(conditions, var, **kwargs):
    try:
        return reduce_rational_inequalities(conditions, var, **kwargs)
    except PolynomialError:
        raise ValueError("Reduction of condition failed %s\n" % conditions[0])
Пример #9
0
def _reduce_inequalities(conditions, var, **kwargs):
    try:
        return reduce_rational_inequalities(conditions, var, **kwargs)
    except PolynomialError:
        raise ValueError("Reduction of condition failed %s\n" % conditions[0])
Пример #10
0
def test_reduce_poly_inequalities_real_relational():
    with assuming(Q.real(x), Q.real(y)):
        assert reduce_rational_inequalities(
            [[Eq(x**2, 0)]], x, relational=True) == Eq(x, 0)
        assert reduce_rational_inequalities(
            [[Le(x**2, 0)]], x, relational=True) == Eq(x, 0)
        assert reduce_rational_inequalities(
            [[Lt(x**2, 0)]], x, relational=True) is False
        assert reduce_rational_inequalities(
            [[Ge(x**2, 0)]], x, relational=True) is True
        assert reduce_rational_inequalities(
            [[Gt(x**2, 0)]], x, relational=True) == Or(Lt(x, 0), Gt(x, 0))
        assert reduce_rational_inequalities(
            [[Ne(x**2, 0)]], x, relational=True) == Or(Lt(x, 0), Gt(x, 0))

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

        assert reduce_rational_inequalities(
            [[Le(x**2, 1.0)]], x, relational=True) == And(Le(-1.0, x), Le(x, 1.0))
        assert reduce_rational_inequalities(
            [[Lt(x**2, 1.0)]], x, relational=True) == And(Lt(-1.0, x), Lt(x, 1.0))
        assert reduce_rational_inequalities(
            [[Ge(x**2, 1.0)]], x, relational=True) == Or(Le(x, -1.0), Ge(x, 1.0))
        assert reduce_rational_inequalities(
            [[Gt(x**2, 1.0)]], x, relational=True) == Or(Lt(x, -1.0), Gt(x, 1.0))
        assert reduce_rational_inequalities([[Ne(x**2, 1.0)]], x, relational=True) == \
            Or(Lt(x, -1.0), And(Lt(-1.0, x), Lt(x, 1.0)), Gt(x, 1.0))
Пример #11
0
def test_reduce_poly_inequalities_real_relational():
    with assuming(Q.real(x), Q.real(y)):
        assert reduce_rational_inequalities(
            [[Eq(x**2, 0)]], x, relational=True) == Eq(x, 0)
        assert reduce_rational_inequalities(
            [[Le(x**2, 0)]], x, relational=True) == Eq(x, 0)
        assert reduce_rational_inequalities(
            [[Lt(x**2, 0)]], x, relational=True) == False
        assert reduce_rational_inequalities(
            [[Ge(x**2, 0)]], x, relational=True) == And(Lt(-oo, x), Lt(x, oo))
        assert reduce_rational_inequalities(
            [[Gt(x**2, 0)]], x, relational=True) == Or(And(Lt(-oo, x), Lt(x, 0)), And(Lt(0, x), Lt(x, oo)))
        assert reduce_rational_inequalities(
            [[Ne(x**2, 0)]], x, relational=True) == Or(And(Lt(-oo, x), Lt(x, 0)), And(Lt(0, x), Lt(x, oo)))

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

        assert reduce_rational_inequalities(
            [[Le(x**2, 1.0)]], x, relational=True) == And(Le(-1.0, x), Le(x, 1.0))
        assert reduce_rational_inequalities(
            [[Lt(x**2, 1.0)]], x, relational=True) == And(Lt(-1.0, x), Lt(x, 1.0))
        assert reduce_rational_inequalities(
            [[Ge(x**2, 1.0)]], x, relational=True) == Or(And(Lt(Float('-inf'), x), Le(x, -1.0)),
                                                         And(Le(1.0, x), Lt(x, Float('+inf'))))
        assert reduce_rational_inequalities(
            [[Gt(x**2, 1.0)]], x, relational=True) == Or(And(Lt(Float('-inf'), x), Lt(x, -1.0)),
                                                         And(Lt(1.0, x), Lt(x, Float('+inf'))))
        assert reduce_rational_inequalities([[Ne(x**2, 1.0)]], x, relational=True) == \
            Or(And(Lt(-1.0, x), Lt(x, 1.0)), And(Lt(Float('-inf'), x), Lt(x, -1.0)),
               And(Lt(1.0, x), Lt(x, Float('+inf'))))
Пример #12
0
def test_reduce_poly_inequalities_complex_relational():
    x = Symbol('x')
    cond = Eq(im(x), 0)

    assert reduce_rational_inequalities([[Eq(x**2, 0)]], x,
                                        relational=True) == And(
                                            Eq(re(x), 0), cond)
    assert reduce_rational_inequalities([[Le(x**2, 0)]], x,
                                        relational=True) == And(
                                            Eq(re(x), 0), cond)
    assert reduce_rational_inequalities([[Lt(x**2, 0)]], x,
                                        relational=True) == False
    assert reduce_rational_inequalities(
        [[Ge(x**2, 0)]], x, relational=True) == And(cond, Lt(-oo, re(x)),
                                                    Lt(re(x), oo))
    assert reduce_rational_inequalities([[Gt(x**2, 0)]], x, relational=True) == \
        And(cond, Or(And(Lt(-oo, re(x)), Lt(re(x), 0)), And(Lt(0, re(x)), Lt(re(x), oo))))
    assert reduce_rational_inequalities([[Ne(x**2, 0)]], x, relational=True) == \
        And(cond, Or(And(Lt(-oo, re(x)), Lt(re(x), 0)), And(Lt(0, re(x)), Lt(re(x), oo))))

    assert reduce_rational_inequalities([[Eq(x**2, 1)]], x, relational=True) == \
        And(Or(Eq(re(x), -1), Eq(re(x), 1)), cond)
    assert reduce_rational_inequalities([[Le(x**2, 1)]], x, relational=True) == \
        And(And(Le(-1, re(x)), Le(re(x), 1)), cond)
    assert reduce_rational_inequalities([[Lt(x**2, 1)]], x, relational=True) == \
        And(And(Lt(-1, re(x)), Lt(re(x), 1)), cond)
    assert reduce_rational_inequalities([[Ge(x**2, 1)]], x, relational=True) == \
        And(cond, Or(And(Le(1, re(x)), Lt(re(x), oo)), And(Le(re(x), -1), Lt(-oo, re(x)))))
    assert reduce_rational_inequalities([[Gt(x**2, 1)]], x, relational=True) == \
        And(cond, Or(And(Lt(-oo, re(x)), Lt(re(x), -1)), And(Lt(1, re(x)), Lt(re(x), oo))))
    assert reduce_rational_inequalities([[Ne(x**2, 1)]], x, relational=True) == \
        And(cond, Or(And(Lt(-oo, re(x)), Lt(re(x), -1)),
                     And(Lt(-1, re(x)), Lt(re(x), 1)), And(Lt(1, re(x)), Lt(re(x), oo))))

    assert reduce_rational_inequalities([[Le(x**2, 1.0)]], x, relational=True) == \
        And(And(Le(-1.0, re(x)), Le(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Lt(x**2, 1.0)]], x, relational=True) == \
        And(And(Lt(-1.0, re(x)), Lt(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Ge(x**2, 1.0)]], x, relational=True) == \
        And(cond, Or(And(Le(1.0, re(x)), re(x) < Float('+inf')),
                     And(Le(re(x), -1.0), Float('-inf') < re(x))))
    assert reduce_rational_inequalities([[Gt(x**2, 1.0)]], x, relational=True) == \
        And(cond, Or(And(Float('-inf') < re(x), re(x) < -1.0),
                     And(Lt(1.0, re(x)), re(x) < Float('+inf'))))
    assert reduce_rational_inequalities([[Ne(x**2, 1.0)]], x, relational=True) == \
        And(cond, Or(And(Float('-inf') < re(x), Lt(re(x), -1.0)),
                     And(Lt(-1.0, re(x)), re(x) < 1.0), And(Lt(1.0, re(x)), re(x) < Float('+inf'))))
Пример #13
0
def test_reduce_poly_inequalities_real_interval():
    assert reduce_rational_inequalities(
        [[Eq(x**2, 0)]], x, relational=False) == FiniteSet(0)
    assert reduce_rational_inequalities(
        [[Le(x**2, 0)]], x, relational=False) == FiniteSet(0)
    assert reduce_rational_inequalities(
        [[Lt(x**2, 0)]], x, relational=False) == S.EmptySet
    assert reduce_rational_inequalities(
        [[Ge(x**2, 0)]], x, relational=False) == \
        S.Reals if x.is_real else Interval(-oo, oo)
    assert reduce_rational_inequalities(
        [[Gt(x**2, 0)]], x, relational=False) == \
        FiniteSet(0).complement(S.Reals)
    assert reduce_rational_inequalities(
        [[Ne(x**2, 0)]], x, relational=False) == \
        FiniteSet(0).complement(S.Reals)

    assert reduce_rational_inequalities(
        [[Eq(x**2, 1)]], x, relational=False) == FiniteSet(-1, 1)
    assert reduce_rational_inequalities(
        [[Le(x**2, 1)]], x, relational=False) == Interval(-1, 1)
    assert reduce_rational_inequalities(
        [[Lt(x**2, 1)]], x, relational=False) == Interval(-1, 1, True, True)
    assert reduce_rational_inequalities(
        [[Ge(x**2, 1)]], x, relational=False) == \
        Union(Interval(-oo, -1), Interval(1, oo))
    assert reduce_rational_inequalities(
        [[Gt(x**2, 1)]], x, relational=False) == \
        Interval(-1, 1).complement(S.Reals)
    assert reduce_rational_inequalities(
        [[Ne(x**2, 1)]], x, relational=False) == \
        FiniteSet(-1, 1).complement(S.Reals)
    assert reduce_rational_inequalities([[Eq(
        x**2, 1.0)]], x, relational=False) == FiniteSet(-1.0, 1.0).evalf()
    assert reduce_rational_inequalities(
        [[Le(x**2, 1.0)]], x, relational=False) == Interval(-1.0, 1.0)
    assert reduce_rational_inequalities([[Lt(
        x**2, 1.0)]], x, relational=False) == Interval(-1.0, 1.0, True, True)
    assert reduce_rational_inequalities(
        [[Ge(x**2, 1.0)]], x, relational=False) == \
        Union(Interval(-inf, -1.0), Interval(1.0, inf))
    assert reduce_rational_inequalities(
        [[Gt(x**2, 1.0)]], x, relational=False) == \
        Union(Interval(-inf, -1.0, right_open=True),
        Interval(1.0, inf, left_open=True))
    assert reduce_rational_inequalities([[Ne(
        x**2, 1.0)]], x, relational=False) == \
        FiniteSet(-1.0, 1.0).complement(S.Reals)

    s = sqrt(2)

    assert reduce_rational_inequalities([[Lt(
        x**2 - 1, 0), Gt(x**2 - 1, 0)]], x, relational=False) == S.EmptySet
    assert reduce_rational_inequalities([[Le(x**2 - 1, 0), Ge(
        x**2 - 1, 0)]], x, relational=False) == FiniteSet(-1, 1)
    assert reduce_rational_inequalities(
        [[Le(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x, relational=False
        ) == Union(Interval(-s, -1, False, False), Interval(1, s, False, False))
    assert reduce_rational_inequalities(
        [[Le(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x, relational=False
        ) == Union(Interval(-s, -1, False, True), Interval(1, s, True, False))
    assert reduce_rational_inequalities(
        [[Lt(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x, relational=False
        ) == Union(Interval(-s, -1, True, False), Interval(1, s, False, True))
    assert reduce_rational_inequalities(
        [[Lt(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x, relational=False
        ) == Union(Interval(-s, -1, True, True), Interval(1, s, True, True))
    assert reduce_rational_inequalities(
        [[Lt(x**2 - 2, 0), Ne(x**2 - 1, 0)]], x, relational=False
        ) == Union(Interval(-s, -1, True, True), Interval(-1, 1, True, True),
        Interval(1, s, True, True))
Пример #14
0
def test_reduce_poly_inequalities_real_relational():
    x = Symbol('x', real=True)
    y = Symbol('y', real=True)

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

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

    assert reduce_rational_inequalities([[Le(x**2, 1.0)]], x,
                                        relational=True) == And(
                                            Le(-1.0, x), Le(x, 1.0))
    assert reduce_rational_inequalities([[Lt(x**2, 1.0)]], x,
                                        relational=True) == And(
                                            Lt(-1.0, x), Lt(x, 1.0))
    assert reduce_rational_inequalities([[Ge(x**2, 1.0)]], x,
                                        relational=True) == Or(
                                            And(Lt(Float('-inf'), x),
                                                Le(x, -1.0)),
                                            And(Le(1.0, x),
                                                Lt(x, Float('+inf'))))
    assert reduce_rational_inequalities([[Gt(x**2, 1.0)]], x,
                                        relational=True) == Or(
                                            And(Lt(Float('-inf'), x),
                                                Lt(x, -1.0)),
                                            And(Lt(1.0, x),
                                                Lt(x, Float('+inf'))))
    assert reduce_rational_inequalities([[Ne(x**2, 1.0)]], x, relational=True) == \
            Or(And(Lt(-1.0, x), Lt(x, 1.0)), And(Lt(Float('-inf'), x), Lt(x, -1.0)),
               And(Lt(1.0, x), Lt(x, Float('+inf'))))
Пример #15
0
def simplify_and(
        x: sympy.Basic,
        gen: typing.Optional[sympy.Symbol] = None,
        extra_conditions: typing.Optional[sympy.Basic] = True) -> sympy.Basic:
    """
  Some rules, because SymPy currently does not automatically simplify them...
  """
    assert isinstance(x, sympy.Basic), "type x: %r" % type(x)
    from sympy.solvers.inequalities import reduce_rational_inequalities
    from sympy.core.relational import Relational

    syms = []
    if gen is not None:
        syms.append(gen)

    w1 = sympy.Wild("w1")
    w2 = sympy.Wild("w2")
    for sub_expr in x.find(sympy.Eq(w1, w2)):
        m = sub_expr.match(sympy.Eq(w1, w2))
        ws_ = m[w1], m[w2]
        for w_ in ws_:
            if isinstance(w_, sympy.Symbol) and w_ not in syms:
                syms.append(w_)
    for w_ in x.free_symbols:
        if w_ not in syms:
            syms.append(w_)

    if len(syms) >= 1:
        _c = syms[0]
        if len(syms) >= 2:
            n = syms[1]
        else:
            n = sympy.Wild("n")
    else:
        return x

    x = x.replace(((_c - 2 * n >= -1) & (_c - 2 * n <= -1)),
                  sympy.Eq(_c, 2 * n - 1))  # probably not needed anymore...
    apply_rules = True
    while apply_rules:
        apply_rules = False
        for and_expr in x.find(sympy.And):
            assert isinstance(and_expr, sympy.And)

            and_expr_ = reduce_rational_inequalities([and_expr.args], _c)
            # print(and_expr, "->", and_expr_)
            if and_expr_ != and_expr:
                x = x.replace(and_expr, and_expr_)
                and_expr = and_expr_
                if and_expr == sympy.sympify(False):
                    continue
                if isinstance(and_expr, sympy.Rel):
                    continue
                assert isinstance(and_expr, sympy.And)

            and_expr_args = list(and_expr.args)
            # for i, part in enumerate(and_expr_args):
            #  and_expr_args[i] = part.simplify()
            if all([
                    isinstance(part, Relational) and _c in part.free_symbols
                    for part in and_expr_args
            ]):
                # No equality, as that should have been resolved above.
                rel_ops = ["==", ">=", "<="]
                if not (_c.is_Integer or _c.assumptions0["integer"]):
                    rel_ops.extend(["<", ">"])
                rhs_by_c = {op: [] for op in rel_ops}
                for part in and_expr_args:
                    assert isinstance(part, Relational)
                    part = _solve_inequality(part, _c)
                    assert isinstance(part, Relational)
                    assert part.lhs == _c
                    rel_op, rhs = part.rel_op, part.rhs
                    if _c.is_Integer or _c.assumptions0["integer"]:
                        if rel_op == "<":
                            rhs = rhs - 1
                            rel_op = "<="
                        elif rel_op == ">":
                            rhs = rhs + 1
                            rel_op = ">="
                    assert rel_op in rhs_by_c, "x: %r, _c: %r, and expr: %r, part %r" % (
                        x, _c, and_expr, part)
                    other_rhs = rhs_by_c[rel_op]
                    assert isinstance(other_rhs, list)
                    need_to_add = True
                    for rhs_ in other_rhs:
                        cmp = Relational.ValidRelationOperator[rel_op](rhs,
                                                                       rhs_)
                        if simplify_and(
                                sympy.And(sympy.Not(cmp),
                                          extra_conditions)) == sympy.sympify(
                                              False):  # checks True...
                            other_rhs.remove(rhs_)
                            break
                        elif simplify_and(sympy.And(
                                cmp,
                                extra_conditions)) == sympy.sympify(False):
                            need_to_add = False
                            break
                        # else:
                        #  raise NotImplementedError("cannot compare %r in %r; extra cond %r" % (cmp, and_expr, extra_conditions))
                    if need_to_add:
                        other_rhs.append(rhs)
                if rhs_by_c[">="] and rhs_by_c["<="]:
                    all_false = False
                    for lhs in rhs_by_c[">="]:
                        for rhs in rhs_by_c["<="]:
                            if sympy.Lt(lhs, rhs) == sympy.sympify(False):
                                all_false = True
                            if sympy.Eq(lhs, rhs) == sympy.sympify(True):
                                rhs_by_c["=="].append(lhs)
                    if all_false:
                        x = x.replace(and_expr, False)
                        continue
                if rhs_by_c["=="]:
                    all_false = False
                    while len(rhs_by_c["=="]) >= 2:
                        lhs, rhs = rhs_by_c["=="][:2]
                        if sympy.Eq(lhs, rhs) == sympy.sympify(False):
                            all_false = True
                            break
                        elif sympy.Eq(lhs, rhs) == sympy.sympify(True):
                            rhs_by_c["=="].pop(1)
                        else:
                            raise NotImplementedError(
                                "cannot cmp %r == %r. rhs_by_c %r" %
                                (lhs, rhs, rhs_by_c))
                    if all_false:
                        x = x.replace(and_expr, False)
                        continue
                    new_parts = [sympy.Eq(_c, rhs_by_c["=="][0])]
                    for op in rel_ops:
                        for part in rhs_by_c[op]:
                            new_parts.append(
                                Relational.ValidRelationOperator[op](
                                    rhs_by_c["=="][0], part).simplify())
                else:  # no "=="
                    new_parts = []
                    for op in rel_ops:
                        for part in rhs_by_c[op]:
                            new_parts.append(
                                Relational.ValidRelationOperator[op](_c, part))
                    assert new_parts
                and_expr_ = sympy.And(*new_parts)
                # print(and_expr, "--->", and_expr_)
                x = x.replace(and_expr, and_expr_)
                and_expr = and_expr_

            # Probably all the remaining hard-coded rules are not needed anymore with the more generic code above...
            if sympy.Eq(_c, 2 * n) in and_expr.args:
                if (_c - 2 * n <= -1) in and_expr.args:
                    x = x.replace(and_expr, False)
                    continue
                if sympy.Eq(_c - 2 * n, -1) in and_expr.args:
                    x = x.replace(and_expr, False)
                    continue
                if (_c - n <= -1) in and_expr.args:
                    x = x.replace(and_expr, False)
                    continue
            if (_c >= n) in and_expr.args and (_c - n <= -1) in and_expr.args:
                x = x.replace(and_expr, False)
                continue
            if sympy.Eq(_c - 2 * n, -1) in and_expr.args:  # assume n>=1
                if (_c >= n) in and_expr.args:
                    x = x.replace(
                        and_expr,
                        sympy.And(
                            *
                            [arg for arg in and_expr.args
                             if arg != (_c >= n)]))
                    apply_rules = True
                    break
                if (_c - n >= -1) in and_expr.args:
                    x = x.replace(
                        and_expr,
                        sympy.And(*[
                            arg for arg in and_expr.args
                            if arg != (_c - n >= -1)
                        ]))
                    apply_rules = True
                    break
            if (_c >= n) in and_expr.args:
                if (_c - n >= -1) in and_expr.args:
                    x = x.replace(
                        and_expr,
                        sympy.And(*[
                            arg for arg in and_expr.args
                            if arg != (_c - n >= -1)
                        ]))
                    apply_rules = True
                    break
            if (_c - n >= -1) in and_expr.args and (_c - n <=
                                                    -1) in and_expr.args:
                args = list(and_expr.args)
                args.remove((_c - n >= -1))
                args.remove((_c - n <= -1))
                args.append(sympy.Eq(_c - n, -1))
                if (_c - 2 * n <= -1) in args:
                    args.remove((_c - 2 * n <= -1))
                x = x.replace(and_expr, sympy.And(*args))
                apply_rules = True
                break
    return x
Пример #16
0
def test_reduce_poly_inequalities_complex_relational():
    cond = Eq(im(x), 0)

    assert reduce_rational_inequalities([[Eq(x**2, 0)]], x,
                                        relational=True) == And(
                                            Eq(re(x), 0), cond)
    assert reduce_rational_inequalities([[Le(x**2, 0)]], x,
                                        relational=True) == And(
                                            Eq(re(x), 0), cond)
    assert reduce_rational_inequalities([[Lt(x**2, 0)]], x,
                                        relational=True) is False
    assert reduce_rational_inequalities([[Ge(x**2, 0)]], x,
                                        relational=True) == cond
    assert reduce_rational_inequalities([[Gt(x**2, 0)]], x,
                                        relational=True) == And(
                                            Or(Lt(re(x), 0), Lt(0, re(x))),
                                            cond)
    assert reduce_rational_inequalities([[Ne(x**2, 0)]], x,
                                        relational=True) == And(
                                            Or(Lt(re(x), 0), Lt(0, re(x))),
                                            cond)

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

    assert reduce_rational_inequalities([[Le(x**2, 1.0)]], x,
                                        relational=True) == And(
                                            And(Le(-1.0, re(x)),
                                                Le(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Lt(x**2, 1.0)]], x,
                                        relational=True) == And(
                                            And(Lt(-1.0, re(x)),
                                                Lt(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Ge(x**2, 1.0)]], x,
                                        relational=True) == And(
                                            Or(Le(re(x), -1.0),
                                               Le(1.0, re(x))), cond)
    assert reduce_rational_inequalities([[Gt(x**2, 1.0)]], x,
                                        relational=True) == And(
                                            Or(Lt(re(x), -1.0),
                                               Lt(1.0, re(x))), cond)
    assert reduce_rational_inequalities([[Ne(x**2, 1.0)]], x,
                                        relational=True) == And(
                                            Or(
                                                Lt(re(x), -1.0),
                                                And(Lt(-1.0, re(x)),
                                                    Lt(re(x), 1.0)),
                                                Lt(1.0, re(x))), cond)
Пример #17
0
def test_reduce_poly_inequalities_real_interval():
    with assuming(Q.real(x), Q.real(y)):
        assert reduce_rational_inequalities([[Eq(x**2, 0)]],
                                            x,
                                            relational=False) == FiniteSet(0)
        assert reduce_rational_inequalities([[Le(x**2, 0)]],
                                            x,
                                            relational=False) == FiniteSet(0)
        assert reduce_rational_inequalities([[Lt(x**2, 0)]],
                                            x,
                                            relational=False) == S.EmptySet
        assert reduce_rational_inequalities([[Ge(x**2, 0)]],
                                            x,
                                            relational=False) == Interval(
                                                -oo, oo)
        assert reduce_rational_inequalities(
            [[Gt(x**2, 0)]], x, relational=False) == FiniteSet(0).complement
        assert reduce_rational_inequalities(
            [[Ne(x**2, 0)]], x, relational=False) == FiniteSet(0).complement

        assert reduce_rational_inequalities([[Eq(x**2, 1)]],
                                            x,
                                            relational=False) == FiniteSet(
                                                -1, 1)
        assert reduce_rational_inequalities([[Le(x**2, 1)]],
                                            x,
                                            relational=False) == Interval(
                                                -1, 1)
        assert reduce_rational_inequalities([[Lt(x**2, 1)]],
                                            x,
                                            relational=False) == Interval(
                                                -1, 1, True, True)
        assert reduce_rational_inequalities([[Ge(x**2, 1)]],
                                            x,
                                            relational=False) == Union(
                                                Interval(-oo, -1),
                                                Interval(1, oo))
        assert reduce_rational_inequalities([[Gt(x**2, 1)]],
                                            x,
                                            relational=False) == Interval(
                                                -1, 1).complement
        assert reduce_rational_inequalities([[Ne(x**2, 1)]],
                                            x,
                                            relational=False) == FiniteSet(
                                                -1, 1).complement
        assert reduce_rational_inequalities([[Eq(x**2, 1.0)]],
                                            x,
                                            relational=False) == FiniteSet(
                                                -1.0, 1.0).evalf()
        assert reduce_rational_inequalities([[Le(x**2, 1.0)]],
                                            x,
                                            relational=False) == Interval(
                                                -1.0, 1.0)
        assert reduce_rational_inequalities([[Lt(x**2, 1.0)]],
                                            x,
                                            relational=False) == Interval(
                                                -1.0, 1.0, True, True)
        assert reduce_rational_inequalities([[Ge(x**2, 1.0)]],
                                            x,
                                            relational=False) == Union(
                                                Interval(-inf, -1.0),
                                                Interval(1.0, inf))
        assert reduce_rational_inequalities([[Gt(x**2, 1.0)]],
                                            x,
                                            relational=False) == Union(
                                                Interval(-inf,
                                                         -1.0,
                                                         right_open=True),
                                                Interval(1.0,
                                                         inf,
                                                         left_open=True))
        assert reduce_rational_inequalities([[Ne(x**2, 1.0)]],
                                            x,
                                            relational=False) == FiniteSet(
                                                -1.0, 1.0).complement

        s = sqrt(2)

        assert reduce_rational_inequalities(
            [[Lt(x**2 - 1, 0), Gt(x**2 - 1, 0)]], x,
            relational=False) == S.EmptySet
        assert reduce_rational_inequalities(
            [[Le(x**2 - 1, 0), Ge(x**2 - 1, 0)]], x,
            relational=False) == FiniteSet(-1, 1)
        assert reduce_rational_inequalities(
            [[Le(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, False, False),
                                       Interval(1, s, False, False))
        assert reduce_rational_inequalities(
            [[Le(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, False, True),
                                       Interval(1, s, True, False))
        assert reduce_rational_inequalities(
            [[Lt(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, True, False),
                                       Interval(1, s, False, True))
        assert reduce_rational_inequalities(
            [[Lt(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, True, True),
                                       Interval(1, s, True, True))
        assert reduce_rational_inequalities(
            [[Lt(x**2 - 2, 0), Ne(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, True, True),
                                       Interval(-1, 1, True, True),
                                       Interval(1, s, True, True))
Пример #18
0
def test_reduce_poly_inequalities_real_relational():
    with assuming(Q.real(x), Q.real(y)):
        assert reduce_rational_inequalities([[Eq(x**2, 0)]],
                                            x,
                                            relational=True) == Eq(x, 0)
        assert reduce_rational_inequalities([[Le(x**2, 0)]],
                                            x,
                                            relational=True) == Eq(x, 0)
        assert reduce_rational_inequalities(
            [[Lt(x**2, 0)]], x, relational=True) is False
        assert reduce_rational_inequalities(
            [[Ge(x**2, 0)]], x, relational=True) is True
        assert reduce_rational_inequalities([[Gt(x**2, 0)]],
                                            x,
                                            relational=True) == Or(
                                                Lt(x, 0), Lt(0, x))
        assert reduce_rational_inequalities([[Ne(x**2, 0)]],
                                            x,
                                            relational=True) == Or(
                                                Lt(x, 0), Lt(0, x))

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

        assert reduce_rational_inequalities([[Le(x**2, 1.0)]],
                                            x,
                                            relational=True) == And(
                                                Le(-1.0, x), Le(x, 1.0))
        assert reduce_rational_inequalities([[Lt(x**2, 1.0)]],
                                            x,
                                            relational=True) == And(
                                                Lt(-1.0, x), Lt(x, 1.0))
        assert reduce_rational_inequalities([[Ge(x**2, 1.0)]],
                                            x,
                                            relational=True) == Or(
                                                Le(x, -1.0), Le(1.0, x))
        assert reduce_rational_inequalities([[Gt(x**2, 1.0)]],
                                            x,
                                            relational=True) == Or(
                                                Lt(x, -1.0), Lt(1.0, x))
        assert reduce_rational_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))
Пример #19
0
def test_reduce_poly_inequalities_complex_relational():
    assert reduce_rational_inequalities([[Eq(x**2, 0)]], x,
                                        relational=True) == Eq(x, 0)
    assert reduce_rational_inequalities([[Le(x**2, 0)]], x,
                                        relational=True) == Eq(x, 0)
    assert reduce_rational_inequalities([[Lt(x**2, 0)]], x,
                                        relational=True) == False
    assert reduce_rational_inequalities([[Ge(x**2, 0)]], x,
                                        relational=True) == And(
                                            Lt(-oo, x), Lt(x, oo))
    assert reduce_rational_inequalities(
        [[Gt(x**2, 0)]], x, relational=True) == \
        And(Gt(x, -oo), Lt(x, oo), Ne(x, 0))
    assert reduce_rational_inequalities(
        [[Ne(x**2, 0)]], x, relational=True) == \
        And(Gt(x, -oo), Lt(x, oo), Ne(x, 0))

    for one in (S(1), S(1.0)):
        inf = one * oo
        assert reduce_rational_inequalities(
            [[Eq(x**2, one)]], x, relational=True) == \
            Or(Eq(x, -one), Eq(x, one))
        assert reduce_rational_inequalities(
            [[Le(x**2, one)]], x, relational=True) == \
            And(And(Le(-one, x), Le(x, one)))
        assert reduce_rational_inequalities(
            [[Lt(x**2, one)]], x, relational=True) == \
            And(And(Lt(-one, x), Lt(x, one)))
        assert reduce_rational_inequalities(
            [[Ge(x**2, one)]], x, relational=True) == \
            And(Or(And(Le(one, x), Lt(x, inf)), And(Le(x, -one), Lt(-inf, x))))
        assert reduce_rational_inequalities(
            [[Gt(x**2, one)]], x, relational=True) == \
            And(Or(And(Lt(-inf, x), Lt(x, -one)), And(Lt(one, x), Lt(x, inf))))
        assert reduce_rational_inequalities(
            [[Ne(x**2, one)]], x, relational=True) == \
            Or(And(Lt(-inf, x), Lt(x, -one)),
               And(Lt(-one, x), Lt(x, one)),
               And(Lt(one, x), Lt(x, inf)))
Пример #20
0
def test_reduce_poly_inequalities_complex_relational():
    cond = Eq(im(x), 0)

    assert reduce_rational_inequalities(
        [[Eq(x**2, 0)]], x, relational=True) == And(Eq(re(x), 0), cond)
    assert reduce_rational_inequalities(
        [[Le(x**2, 0)]], x, relational=True) == And(Eq(re(x), 0), cond)
    assert reduce_rational_inequalities(
        [[Lt(x**2, 0)]], x, relational=True) == False
    assert reduce_rational_inequalities(
        [[Ge(x**2, 0)]], x, relational=True) == cond
    assert reduce_rational_inequalities([[Gt(x**2, 0)]], x, relational=True) == \
        And(Or(Lt(re(x), 0), Gt(re(x), 0)), cond)
    assert reduce_rational_inequalities([[Ne(x**2, 0)]], x, relational=True) == \
        And(Or(Lt(re(x), 0), Gt(re(x), 0)), cond)

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

    assert reduce_rational_inequalities([[Le(x**2, 1.0)]], x, relational=True) == \
        And(And(Le(-1.0, re(x)), Le(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Lt(x**2, 1.0)]], x, relational=True) == \
        And(And(Lt(-1.0, re(x)), Lt(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Ge(x**2, 1.0)]], x, relational=True) == \
        And(Or(Le(re(x), -1.0), Ge(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Gt(x**2, 1.0)]], x, relational=True) == \
        And(Or(Lt(re(x), -1.0), Gt(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Ne(x**2, 1.0)]], x, relational=True) == \
        And(Or(Lt(re(x), -1.0), And(Lt(-1.0, re(x)), Lt(re(x), 1.0)), Gt(re(x), 1.0)), cond)
Пример #21
0
def test_reduce_poly_inequalities_real_interval():
    assert reduce_rational_inequalities([[Eq(x**2, 0)]], x,
                                        relational=False) == FiniteSet(0)
    assert reduce_rational_inequalities([[Le(x**2, 0)]], x,
                                        relational=False) == FiniteSet(0)
    assert reduce_rational_inequalities([[Lt(x**2, 0)]], x,
                                        relational=False) == S.EmptySet
    assert reduce_rational_inequalities(
        [[Ge(x**2, 0)]], x, relational=False) == \
        S.Reals if x.is_real else Interval(-oo, oo)
    assert reduce_rational_inequalities(
        [[Gt(x**2, 0)]], x, relational=False) == \
        FiniteSet(0).complement(S.Reals)
    assert reduce_rational_inequalities(
        [[Ne(x**2, 0)]], x, relational=False) == \
        FiniteSet(0).complement(S.Reals)

    assert reduce_rational_inequalities([[Eq(x**2, 1)]], x,
                                        relational=False) == FiniteSet(-1, 1)
    assert reduce_rational_inequalities([[Le(x**2, 1)]], x,
                                        relational=False) == Interval(-1, 1)
    assert reduce_rational_inequalities([[Lt(x**2, 1)]], x,
                                        relational=False) == Interval(
                                            -1, 1, True, True)
    assert reduce_rational_inequalities(
        [[Ge(x**2, 1)]], x, relational=False) == \
        Union(Interval(-oo, -1), Interval(1, oo))
    assert reduce_rational_inequalities(
        [[Gt(x**2, 1)]], x, relational=False) == \
        Interval(-1, 1).complement(S.Reals)
    assert reduce_rational_inequalities(
        [[Ne(x**2, 1)]], x, relational=False) == \
        FiniteSet(-1, 1).complement(S.Reals)
    assert reduce_rational_inequalities([[Eq(x**2, 1.0)]], x,
                                        relational=False) == FiniteSet(
                                            -1.0, 1.0).evalf()
    assert reduce_rational_inequalities([[Le(x**2, 1.0)]], x,
                                        relational=False) == Interval(
                                            -1.0, 1.0)
    assert reduce_rational_inequalities([[Lt(x**2, 1.0)]], x,
                                        relational=False) == Interval(
                                            -1.0, 1.0, True, True)
    assert reduce_rational_inequalities(
        [[Ge(x**2, 1.0)]], x, relational=False) == \
        Union(Interval(-inf, -1.0), Interval(1.0, inf))
    assert reduce_rational_inequalities(
        [[Gt(x**2, 1.0)]], x, relational=False) == \
        Union(Interval(-inf, -1.0, right_open=True),
        Interval(1.0, inf, left_open=True))
    assert reduce_rational_inequalities([[Ne(
        x**2, 1.0)]], x, relational=False) == \
        FiniteSet(-1.0, 1.0).complement(S.Reals)

    s = sqrt(2)

    assert reduce_rational_inequalities(
        [[Lt(x**2 - 1, 0), Gt(x**2 - 1, 0)]], x,
        relational=False) == S.EmptySet
    assert reduce_rational_inequalities(
        [[Le(x**2 - 1, 0), Ge(x**2 - 1, 0)]], x,
        relational=False) == FiniteSet(-1, 1)
    assert reduce_rational_inequalities(
        [[Le(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, False, False),
                                   Interval(1, s, False, False))
    assert reduce_rational_inequalities(
        [[Le(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, False, True),
                                   Interval(1, s, True, False))
    assert reduce_rational_inequalities(
        [[Lt(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, True, False),
                                   Interval(1, s, False, True))
    assert reduce_rational_inequalities(
        [[Lt(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, True, True),
                                   Interval(1, s, True, True))
    assert reduce_rational_inequalities(
        [[Lt(x**2 - 2, 0), Ne(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, True, True),
                                   Interval(-1, 1, True, True),
                                   Interval(1, s, True, True))

    assert reduce_rational_inequalities([[Lt(x**2, -1.)]], x) is S.false
Пример #22
0
def test_reduce_poly_inequalities_complex_relational():
    cond = Eq(im(x), 0)

    assert reduce_rational_inequalities(
        [[Eq(x**2, 0)]], x, relational=True) == And(Eq(re(x), 0), cond)
    assert reduce_rational_inequalities(
        [[Le(x**2, 0)]], x, relational=True) == And(Eq(re(x), 0), cond)
    assert reduce_rational_inequalities(
        [[Lt(x**2, 0)]], x, relational=True) == False
    assert reduce_rational_inequalities(
        [[Ge(x**2, 0)]], x, relational=True) == And(cond, Lt(-oo, re(x)), Lt(re(x), oo))
    assert reduce_rational_inequalities([[Gt(x**2, 0)]], x, relational=True) == \
        And(cond, Or(And(Lt(-oo, re(x)), Lt(re(x), 0)), And(Lt(0, re(x)), Lt(re(x), oo))))
    assert reduce_rational_inequalities([[Ne(x**2, 0)]], x, relational=True) == \
        And(cond, Or(And(Lt(-oo, re(x)), Lt(re(x), 0)), And(Lt(0, re(x)), Lt(re(x), oo))))

    assert reduce_rational_inequalities([[Eq(x**2, 1)]], x, relational=True) == \
        And(Or(Eq(re(x), -1), Eq(re(x), 1)), cond)
    assert reduce_rational_inequalities([[Le(x**2, 1)]], x, relational=True) == \
        And(And(Le(-1, re(x)), Le(re(x), 1)), cond)
    assert reduce_rational_inequalities([[Lt(x**2, 1)]], x, relational=True) == \
        And(And(Lt(-1, re(x)), Lt(re(x), 1)), cond)
    assert reduce_rational_inequalities([[Ge(x**2, 1)]], x, relational=True) == \
        And(cond, Or(And(Le(1, re(x)), Lt(re(x), oo)), And(Le(re(x), -1), Lt(-oo, re(x)))))
    assert reduce_rational_inequalities([[Gt(x**2, 1)]], x, relational=True) == \
        And(cond, Or(And(Lt(-oo, re(x)), Lt(re(x), -1)), And(Lt(1, re(x)), Lt(re(x), oo))))
    assert reduce_rational_inequalities([[Ne(x**2, 1)]], x, relational=True) == \
        And(cond, Or(And(Lt(-oo, re(x)), Lt(re(x), -1)),
                     And(Lt(-1, re(x)), Lt(re(x), 1)), And(Lt(1, re(x)), Lt(re(x), oo))))

    assert reduce_rational_inequalities([[Le(x**2, 1.0)]], x, relational=True) == \
        And(And(Le(-1.0, re(x)), Le(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Lt(x**2, 1.0)]], x, relational=True) == \
        And(And(Lt(-1.0, re(x)), Lt(re(x), 1.0)), cond)
    assert reduce_rational_inequalities([[Ge(x**2, 1.0)]], x, relational=True) == \
        And(cond, Or(And(Le(1.0, re(x)), re(x) < Float('+inf')),
                     And(Le(re(x), -1.0), Float('-inf') < re(x))))
    assert reduce_rational_inequalities([[Gt(x**2, 1.0)]], x, relational=True) == \
        And(cond, Or(And(Float('-inf') < re(x), re(x) < -1.0),
                     And(Lt(1.0, re(x)), re(x) < Float('+inf'))))
    assert reduce_rational_inequalities([[Ne(x**2, 1.0)]], x, relational=True) == \
        And(cond, Or(And(Float('-inf') < re(x), Lt(re(x), -1.0)),
                     And(Lt(-1.0, re(x)), re(x) < 1.0), And(Lt(1.0, re(x)), re(x) < Float('+inf'))))