Ejemplo n.º 1
0
def test_parse_2_open():
    # log(x) < 2 & (x < exp(2))
    expr = (Log(X) > 2) & (X < sympy.exp(2))
    event = EventAnd([
        EventInterval(Log(Y), Interval.open(2, oo)),
        EventInterval(Y, Interval.open(-oo, sympy.exp(2)))
    ])
    assert expr == event
Ejemplo n.º 2
0
def test_parse_2_closed():
    # (log(x) <= 2) & (x >= exp(2))
    expr = (Log(X) >= 2) & (X <= sympy.exp(2))
    event = EventAnd([
        EventInterval(Log(Y), Interval(2, oo)),
        EventInterval(Y, Interval(-oo, sympy.exp(2)))
    ])
    assert expr == event
Ejemplo n.º 3
0
def test_parse_5_lopen():
    # (2*x + 10 < 4) & (x + 10 >= 3)
    expr = ((2*X + 10) <= 4) & (X + 10 > 3)
    event = EventAnd([
        EventInterval(Poly(Y, [10, 2]), Interval(-oo, 4)),
        EventInterval(Poly(Y, [10, 1]), Interval.open(3, oo)),
    ])
    assert expr == event
Ejemplo n.º 4
0
def test_parse_27_piecewise_many():
    assert (Y < 0)*(Y**2) + (0 <= Y)*Y**((1, 2)) == Piecewise(
        [
            Poly(Y, [0, 0, 1]),
            Radical(Y, 2)],
        [
            EventInterval(Y, Interval.open(-oo, 0)),
            EventInterval(Y, Interval(0, oo))
        ])
Ejemplo n.º 5
0
def test_parse_6():
    # (x**2 - 2*x) > 10
    expr = (X**2 - 2*X) > 10
    event = EventInterval(Poly(Y, [0, -2, 1]), Interval.open(10, oo))
    assert expr == event

    # (exp(x)**2 - 2*exp(x)) > 10
    expr = (Exp(X)**2 - 2*Exp(X)) > 10
    event = EventInterval(Poly(Exp(X), [0, -2, 1]), Interval.open(10, oo))
    assert expr == event
Ejemplo n.º 6
0
def test_parse_26_piecewise_one_expr_compound_event():
    assert (Y**2)*((Y < 0) | (0 < Y)) == Piecewise(
        [Poly(Y, [0, 0, 1])],
        [EventOr([
            EventInterval(Y, Interval.open(-oo, 0)),
            EventInterval(Y, Interval.open(0, oo)),
            ])])

    assert (Y**2)*(~((3 < Y) <= 4)) == Piecewise(
        [Poly(Y, [0, 0, 1])],
        [EventOr([
            EventInterval(Y, Interval(-oo, 3)),
            EventInterval(Y, Interval.open(4, oo)),
            ])])
Ejemplo n.º 7
0
def test_parse_18():
    # 3*(x**(1/7))**4 - 3*(x**(1/7))**2 <= 9
    Z = X**Rat(1, 7)
    expr = 3*Z**4 - 3*Z**2
    expr_prime = Poly(Radical(Y, 7), [0, 0, -3, 0, 3])
    assert expr == expr_prime

    event = EventInterval(expr_prime, Interval(-oo, 9))
    assert (expr <= 9) == event

    event_not = EventInterval(expr_prime, Interval.open(9, oo))
    assert ~(expr <= 9) == event_not

    expr = (3*Abs(Z))**4 - (3*Abs(Z))**2
    expr_prime = Poly(Poly(Abs(Z), [0, 3]), [0, 0, -1, 0, 3])
Ejemplo n.º 8
0
def test_event_containment_real():
    assert (X << Interval(0, 10)) == EventInterval(X, Interval(0, 10))
    for values in [FiniteReal(0, 10), [0, 10], {0, 10}]:
        assert (X << values) == EventFiniteReal(X, FiniteReal(0, 10))
    # with pytest.raises(ValueError):
    #     X << {1, None}
    assert X << {1, 2} == EventFiniteReal(X, {1, 2})
    assert ~(X << {1, 2}) == EventOr([
        EventInterval(X, Interval.Ropen(-oo, 1)),
        EventInterval(X, Interval.open(1, 2)),
        EventInterval(X, Interval.Lopen(2, oo)),
    ])
    # https://github.com/probcomp/sum-product-dsl/issues/22
    # and of EventBasic does not yet perform simplifications.
    assert ~(~(X << {1, 2})) == \
        ((1 <= X) & ((X <= 1) | (2 <= X)) & (X <= 2))
Ejemplo n.º 9
0
def test_parse_16():
    # (x**(1/7))**4 < 9
    for expr in [((X**Rat(1,7)))**4, (X**(1,7))**4]:
        expr_prime = Pow(Radical(Y, 7), 4)
        assert expr == expr_prime

    event = EventInterval(expr, Interval.open(-oo, 9))
    assert (expr < 9) == event
Ejemplo n.º 10
0
def test_parse_12():
    # 2*sqrt(|x|) - 3 > 10
    expr = 2*Sqrt(Abs(X)) - 3
    expr_prime = Poly(Sqrt(Abs(Y)), [-3, 2])
    assert expr == expr_prime

    event = EventInterval(expr, Interval.open(10, oo))
    assert (expr > 10) == event
Ejemplo n.º 11
0
def test_parse_15():
    # ((x**4)**(1/7)) < 9
    expr = ((X**4))**Rat(1, 7)
    expr_prime = Radical(Pow(Y, 4), 7)
    assert expr == expr_prime

    event = EventInterval(expr_prime, Interval.open(-oo, 9))
    assert (expr < 9) == event
Ejemplo n.º 12
0
def test_parse_21__ci_():
    # 1 <= log(x**3 - 3*x + 3) < 5
    # Can only be solved by numerical approximation of roots.
    # https://www.wolframalpha.com/input/?i=1+%3C%3D+log%28x**3+-+3x+%2B+3%29+%3C+5
    expr = Log(X**3 - 3*X + 3)
    expr_prime = Log(Poly(Y, [3, -3, 0, 1]))
    assert expr == expr_prime
    assert ((1 <= expr) & (expr < 5)) \
        == EventInterval(expr, Interval.Ropen(1, 5))
Ejemplo n.º 13
0
def test_parse_9_open():
    # 2(log(x))**3 - log(x) -5 > 0
    expr = 2*(Log(X))**3 - Log(X) - 5
    expr_prime = Poly(Log(Y), [-5, -1, 0, 2])
    assert expr == expr_prime

    event = EventInterval(expr, Interval.open(0, oo))
    assert (expr > 0) == event

    # Cannot add polynomials with different subexpressions.
    with pytest.raises(ValueError):
        (2*Log(X))**3 - Log(X) - 5
Ejemplo n.º 14
0
def test_parse_19():
    # 3*(x**(1/7))**4 - 3*(x**(1/7))**2 <= 9
    #   or || 3*(x**(1/7))**4 - 3*(x**(1/7))**2 > 11
    Z = X**Rat(1, 7)
    expr = 3*Z**4 - 3*Z**2

    event = (expr <= 9) | (expr > 11)
    event_prime = EventOr([
        EventInterval(expr, Interval(-oo, 9)),
        EventInterval(expr, Interval.open(11, oo)),
    ])
    assert event == event_prime

    event = ((expr <= 9) | (expr > 11)) & (~(expr < 10))
    event_prime = EventAnd([
        EventOr([
            EventInterval(expr, Interval(-oo, 9)),
            EventInterval(expr, Interval.open(11, oo))]),
        EventInterval(expr, Interval(10, oo))
    ])
    assert event == event_prime
Ejemplo n.º 15
0
def test_event_containment_union():
    assert (X << (Interval(0, 1) | Interval(2, 3))) \
        == (((0 <= X) <= 1) | ((2 <= X) <= 3))
    assert (X << (FiniteReal(0, 1) | Interval(2, 3))) \
        == ((X << {0, 1}) | ((2 <= X) <= 3))
    assert (X << FiniteNominal('a', b=True)) \
        == EventFiniteNominal(X, FiniteNominal('a', b=True))
    assert X << EmptySet == EventFiniteReal(X, EmptySet)
    # Ordering is not guaranteed.
    a = X << (Interval(0,1) | (FiniteReal(1.5) | FiniteNominal('a')))
    assert len(a.subexprs) == 3
    assert EventInterval(X, Interval(0,1)) in a.subexprs
    assert EventFiniteReal(X, FiniteReal(1.5)) in a.subexprs
    assert EventFiniteNominal(X, FiniteNominal('a')) in a.subexprs
Ejemplo n.º 16
0
def test_event_inequality_parse():
    assert (5 < (X < 10)) \
        == ((5 < X) < 10) \
        == EventInterval(X, Interval.open(5, 10))
    assert (5 <= (X < 10)) \
        == ((5 <= X) < 10) \
        == EventInterval(X, Interval.Ropen(5, 10))
    assert (5 < (X <= 10)) \
        == ((5 < X) <= 10) \
        == EventInterval(X, Interval.Lopen(5, 10))
    assert (5 <= (X <= 10)) \
        == ((5 <= X) <= 10) \
        == EventInterval(X, Interval(5, 10))
    # GOTCHA: This expression is syntactically equivalent to
    # (5 < X) and (X < 10)
    # Since and short circuits and 5 < X is not False,
    # return value of expression is X < 10
    assert (5 < X < 10) == (X < 10)

    # Yields a finite set .
    assert ((5 < X) < 5) == EventFiniteReal(X, EmptySet)
    assert ((5 < X) <= 5) == EventFiniteReal(X, EmptySet)
    assert ((5 <= X) < 5) == EventFiniteReal(X, EmptySet)
    assert ((5 <= X) <= 5) == EventFiniteReal(X, FiniteReal(5))

    # Negated single interval.
    assert ~(5 < X) == (X <= 5)
    assert ~(5 <= X) == (X < 5)
    assert ~(X < 5) == (5 <= X)
    assert ~(X <= 5) == (5 < X)

    # Negated union of two intervals.
    assert ~(5 < (X < 10)) \
        == ~((5 < X) < 10) \
        == (X <= 5) | (10 <= X)
    assert ~(5 <= (X < 10)) \
        == ~((5 <= X) < 10) \
        == (X < 5) | (10 <= X)
    assert ~(5 < (X <= 10)) \
        == ~((5 < X) <= 10) \
        == (X <= 5) | (10 < X)
    assert ~(5 <= (X <= 10)) \
        == ~((5 <= X) <= 10) \
        == (X < 5) | (10 < X)
    assert ~((10 < X) < 5) \
        == ~(10 < (X < 5)) \
        == EventInterval(X, Interval(-oo, oo))

    # A complicated negated union.
    assert ((~(X < 5)) < 10) \
        == ((5 <= X) < 10) \
        == EventInterval(X, Interval.Ropen(5, 10))
Ejemplo n.º 17
0
def test_parse_26_piecewise_one_expr_basic_event():
    assert (Y**2)*(0 <= Y) == Piecewise(
        [Poly(Y, [0, 0, 1])],
        [EventInterval(Y, Interval(0, oo))])
    assert (0 <= Y)*(Y**2) == Piecewise(
        [Poly(Y, [0, 0, 1])],
        [EventInterval(Y, Interval(0, oo))])
    assert ((0 <= Y) < 5)*(Y < 1) == Piecewise(
        [EventInterval(Y, Interval.Ropen(-oo, 1))],
        [EventInterval(Y, Interval.Ropen(0, 5))],
    )
    assert ((0 <= Y) < 5)*(~(Y < 1)) == Piecewise(
        [EventInterval(Y, Interval(1, oo))],
        [EventInterval(Y, Interval.Ropen(0, 5))],
    )
    assert 10*(0 <= Y) == Poly(
        EventInterval(Y, Interval(0, oo)),
        [0, 10])
Ejemplo n.º 18
0
def test_parse_1_open():
    # log(x) > 2
    expr = Log(X) > 2
    event = EventInterval(Log(Y), Interval(2, oo, left_open=True))
    assert expr == event
Ejemplo n.º 19
0
def test_parse_4():
    # (x >= 0) & (x <= 0)
    expr = (X >= 0) | (X <= 0)
    event = EventInterval(X, Interval(-oo, oo))
    assert expr == event
Ejemplo n.º 20
0
def test_parse_10():
    # exp(sqrt(log(x))) > -5
    expr = Exp(Sqrt(Log(Y)))
    event = EventInterval(expr, Interval.open(-5, oo))
    assert (expr > -5) == event
Ejemplo n.º 21
0
def test_parse_20():
    # log(x**2 - 3) < 5
    expr = Log(X**2 - 3)
    expr_prime = Log(Poly(Y, [-3, 0, 1]))
    event = EventInterval(expr_prime, Interval.open(-oo, 5))
    assert (expr < 5) == event
Ejemplo n.º 22
0
def test_parse_14():
    # x**2 > 10
    expr = X**2 > 10
    event = EventInterval(Pow(Y, 2), Interval.open(10, oo))
    assert expr == event
Ejemplo n.º 23
0
def test_parse_1_closed():
    # log(x) >= 2
    expr = Log(X) >= 2
    event = EventInterval(Log(Y), Interval(2, oo))
    assert expr == event