def test_solver_23_reciprocal_lte(): for c in [1, 3]: # Positive # 1 / X < 10 solution = Interval.Ropen(-oo, 0) | Interval.Lopen(Rat(c, 10), oo) event = (c / Y) < 10 assert event.solve() == solution # 1 / X <= 10 solution = Interval.Ropen(-oo, 0) | Interval(Rat(c, 10), oo) event = (c / Y) <= 10 assert event.solve() == solution # 1 / X <= sqrt(2) solution = Interval.Ropen(-oo, 0) | Interval(c / sympy.sqrt(2), oo) event = (c / Y) <= sympy.sqrt(2) assert event.solve() == solution # Negative. # 1 / X < -10 solution = Interval.open(-Rat(c, 10), 0) event = (c / Y) < -10 assert event.solve() == solution # 1 / X <= -10 solution = Interval.Ropen(-Rat(c, 10), 0) event = (c / Y) <= -10 assert event.solve() == solution # 1 / X <= -sqrt(2) solution = Interval.Ropen(-c / sympy.sqrt(2), 0) event = (c / Y) <= -sympy.sqrt(2) assert event.solve() == solution
def test_solver_22(): # 2 < abs(X) < 5 event = (2 < abs(Y)) < 5 solution = Interval.open(2, 5) | Interval.open(-5, -2) assert event.solve() == solution # 2 <= abs(X) < 5 event = (2 <= abs(Y)) < 5 solution = Interval.Ropen(2, 5) | Interval.Lopen(-5, -2) assert event.solve() == solution # 2 < abs(X) <= 5 event = (2 < abs(Y)) <= 5 solution = Interval.Lopen(2, 5) | Interval.Ropen(-5, -2) assert event.solve() == solution # 2 <= abs(X) <= 5 event = (2 <= abs(Y)) <= 5 solution = Interval(2, 5) | Interval(-5, -2) assert event.solve() == solution # -2 < abs(X) < 5 event = (-2 < abs(Y)) < 5 solution = Interval.open(-5, 5) assert event.solve() == solution # # -2 <= abs(X) < 5 event = (-2 <= abs(Y)) < 5 solution = Interval.open(-5, 5) assert event.solve() == solution # -2 < abs(X) <= 5 event = (-2 < abs(Y)) <= 5 solution = Interval(-5, 5) assert event.solve() == solution # 2 <= abs(X) <= 5 event = (-2 <= abs(Y)) <= 5 solution = Interval(-5, 5) assert event.solve() == solution
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
def test_FiniteReal_and(): assert FR(1) & FR(2) is EmptySet assert FR(1, 2) & FR(2) == FR(2) assert FR(1, 2) & FN('2') is EmptySet assert FR(1, 2) & FN('2', b=True) is EmptySet assert FR(1, 2) & Interval(0, 1) == FR(1) assert FR(1, 2) & Interval.Ropen(0, 1) is EmptySet assert FR(0, 2) & Interval(0, 1) == FR(0) assert FR(0, 2) & Interval.Lopen(0, 1) is EmptySet assert FR(-1, 1) & Interval(-10, 10) == FR(-1, 1) assert FR(-1, 11) & Interval(-10, 10) == FR(-1)
def test_simple_parse_real(): assert isinstance(.3 * bernoulli(p=.1), DistributionMix) a = .3 * bernoulli(p=.1) | .5 * norm() | .2 * poisson(mu=7) spe = a(X) assert isinstance(spe, SumSPE) assert allclose(spe.weights, [log(.3), log(.5), log(.2)]) assert isinstance(spe.children[0], DiscreteLeaf) assert isinstance(spe.children[1], ContinuousLeaf) assert isinstance(spe.children[2], DiscreteLeaf) assert spe.children[0].support == Interval(0, 1) assert spe.children[1].support == Interval(-oo, oo) assert spe.children[2].support == Interval(0, oo)
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))
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
def test_solver_finite_symbolic(): # Transform can never be symbolic. event = Y << {'a', 'b'} assert event.solve() == FiniteNominal('a', 'b') # Complement the Identity. event = ~(Y << {'a', 'b'}) assert event.solve() == FiniteNominal('a', 'b', b=True) # Transform can never be symbolic. event = Y**2 << {'a', 'b'} assert event.solve() is EmptySet # Complement the Identity. event = ~(Y**2 << {'a', 'b'}) assert event.solve() == FiniteNominal(b=True) # Solve Identity mixed. event = Y << {9, 'a', '7'} assert event.solve() == Union( FiniteReal(9), FiniteNominal('a', '7')) # Solve Transform mixed. event = Y**2 << {9, 'a', 'b'} assert event.solve() == FiniteReal(-3, 3) # Solve a disjunction. event = (Y << {'a', 'b'}) | (Y << {'c'}) assert event.solve() == FiniteNominal('a', 'b', 'c') # Solve a conjunction with intersection. event = (Y << {'a', 'b'}) & (Y << {'b', 'c'}) assert event.solve() == FiniteNominal('b') # Solve a conjunction with no intersection. event = (Y << {'a', 'b'}) & (Y << {'c'}) assert event.solve() is EmptySet # Solve a disjunction with complement. event = (Y << {'a', 'b'}) & ~(Y << {'c'}) assert event.solve() == FiniteNominal('a', 'b') # Solve a disjunction with complement. event = (Y << {'a', 'b'}) | ~(Y << {'c'}) assert event.solve() == FiniteNominal('c', b=True) # Union of interval and symbolic. event = (Y**2 <= 9) | (Y << {'a'}) assert event.solve() == Interval(-3, 3) | FiniteNominal('a') # Union of interval and not symbolic. event = (Y**2 <= 9) | ~(Y << {'a'}) assert event.solve() == Interval(-3, 3) | FiniteNominal('a', b=True) # Intersection of interval and symbolic. event = (Y**2 <= 9) & (Y << {'a'}) assert event.solve() is EmptySet # Intersection of interval and not symbolic. event = (Y**2 <= 9) & ~(Y << {'a'}) assert event.solve() == EmptySet
def test_FiniteReal_or(): assert FR(1) | FR(2) == FR(1, 2) assert FR(1, 2) | FR(2) == FR(1, 2) assert FR(1, 2) | FN('2') == Union(FR(1, 2), FN('2')) assert FR(1, 2) | Interval(0, 1) == Union(Interval(0, 1), FR(2)) assert FR(1, 2) | Interval.Ropen(0, 1) == Union(Interval(0, 1), FR(2)) assert FR(0, 1, 2) | Interval.open(0, 1) == Union(Interval(0, 1), FR(2)) assert FR(0, 2) | Interval.Lopen(0, 1) == Union(Interval(0, 1), FR(2)) assert FR(0, 2) | Interval.Lopen(2.5, 10) == Union(Interval.Lopen(2.5, 10), FR(0, 2)) assert FR(-1, 1) | Interval(-10, 10) == Interval(-10, 10) assert FR(-1, 11) | Interval(-10, 10) == Union(Interval(-10, 10), FR(11))
def test_product_condition_or_probabilithy_zero(): X = Id('X') Y = Id('Y') spe = ProductSPE([X >> norm(loc=0, scale=1), Y >> gamma(a=1)]) # Condition on event which has probability zero. event = (X > 2) & (X < 2) with pytest.raises(ValueError): spe.condition(event) assert spe.logprob(event) == -float('inf') # Condition on event which has probability zero. event = (Y < 0) | (Y < -1) with pytest.raises(ValueError): spe.condition(event) assert spe.logprob(event) == -float('inf') # Condition on an event where one clause has probability # zero, yielding a single product. spe_condition = spe.condition((Y < 0) | ((Log(X) >= 0) & (1 <= Y))) assert isinstance(spe_condition, ProductSPE) assert spe_condition.children[0].symbol == X assert spe_condition.children[0].conditioned assert spe_condition.children[0].support == Interval(1, oo) assert spe_condition.children[1].symbol == Y assert spe_condition.children[1].conditioned assert spe_condition.children[0].support == Interval(1, oo) # We have (X < 2) & ~(1 < exp(|3X**2|) is empty. # Thus Y remains unconditioned, # and X is partitioned into (-oo, 0) U (0, oo) with equal weight. event = (Exp(abs(3 * X**2)) > 1) | ((Log(Y) < 0.5) & (X < 2)) spe_condition = spe.condition(event) # # The most concise representation of spe_condition is: # (Product (Sum [.5 .5] X|X<0 X|X>0) Y) assert isinstance(spe_condition, ProductSPE) assert isinstance(spe_condition.children[0], SumSPE) assert spe_condition.children[0].weights == (-log(2), -log(2)) assert spe_condition.children[0].children[0].conditioned assert spe_condition.children[0].children[1].conditioned assert spe_condition.children[0].children[0].support \ in [Interval.Ropen(-oo, 0), Interval.Lopen(0, oo)] assert spe_condition.children[0].children[1].support \ in [Interval.Ropen(-oo, 0), Interval.Lopen(0, oo)] assert spe_condition.children[0].children[0].support \ != spe_condition.children[0].children[1].support assert spe_condition.children[1].symbol == Y assert not spe_condition.children[1].conditioned
def test_solver_10(): # Sympy hangs on this test. # exp(sqrt(log(x))) > -5 solution = Interval(1, oo) event = Exp(Sqrt(Log(Y))) > -5 answer = event.solve() assert answer == solution
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))
def test_FiniteNominal_and(): assert FN('a', 'b') & EmptySet is EmptySet assert FN('a', 'b') & FN('c') is EmptySet assert FN('a', 'b', 'c') & FN('a') == FN('a') assert FN('a', 'b', 'c') & FN(b=True) == FN('a', 'b', 'c') assert FN('a', 'b', 'c') & FN('a') == FN('a') assert FN('a', 'b', 'c', b=True) & FN('a') is EmptySet assert FN('a', 'b', 'c', b=True) & FN('d', 'a', 'b') == FN('d') assert FN('a', 'b', 'c', b=True) & FN('d') == FN('d') assert FN('a', 'b', 'c') & FN('a', b=True) == FN('b', 'c') assert FN('a', 'b', 'c') & FN('d', 'a', 'b', b=True) == FN('c') assert FN('a', 'b', 'c') & FN('d', b=True) == FN('a', 'b', 'c') assert FN('a', 'b', 'c', b=True) & FN('d', b=True) == FN('a', 'b', 'c', 'd', b=True) assert FN('a', 'b', 'c', b=True) & FN('a', b=True) == FN('a', 'b', 'c', b=True) assert FN(b=True) & FN(b=True) == FN(b=True) # FiniteReal assert FN('a') & FR(1) is EmptySet assert FN('a', b=True) & FR(1) is EmptySet # Interval assert FN('a') & Interval(0, 1) is EmptySet
def test_solver_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 solution = Union( Interval( -1.777221448430427630375448631016427343692, 0.09418455242255462832154474245589911789464), Interval.Ropen( 1.683036896007873002053903888560528225797, 5.448658707897512189124586716091172798465)) expr = Log(Y**3 - 3*Y + 3) event = ((1 <= expr) & (expr < 5)) answer = event.solve() assert isinstance(answer, Union) assert len(answer.args) == 2 first = answer.args[0] if answer.args[0].a < 0 else answer.args[1] second = answer.args[0] if answer.args[0].a > 0 else answer.args[1] # Check first interval. assert not first.left_open assert not first.right_open assert allclose(float(first.a), float(solution.args[0].a)) assert allclose(float(first.b), float(solution.args[0].b)) # Check second interval. assert not second.left_open assert second.right_open assert allclose(float(second.a), float(solution.args[1].a)) assert allclose(float(second.b), float(solution.args[1].b))
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
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])
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)) ])
def test_solver_9_closed(): # 2(log(x))**3 - log(x) -5 >= 0 solution = Interval( sympy.exp(1/(6*(sympy.sqrt(2019)/36 + Rat(5,4))**(Rat(1, 3))) + (sympy.sqrt(2019)/36 + Rat(5,4))**(Rat(1,3))), oo) event = 2*(Log(Y))**3 - Log(Y) - 5 >= 0 answer = event.solve() assert answer == solution
def test_simple_parse_nominal(): assert isinstance(.7 * choice({'a': .1, 'b': .9}), DistributionMix) a = .3 * bernoulli(p=.1) | .7 * choice({'a': .1, 'b': .9}) spe = a(X) assert isinstance(spe, SumSPE) assert allclose(spe.weights, [log(.3), log(.7)]) assert isinstance(spe.children[0], DiscreteLeaf) assert isinstance(spe.children[1], NominalLeaf) assert spe.children[0].support == Interval(0, 1) assert spe.children[1].support == FiniteNominal('a', 'b')
def test_solver_18(): # 3*(x**(1/7))**4 - 3*(x**(1/7))**2 <= 9 solution = Interval(0, (Rat(1, 2) + sympy.sqrt(13)/2)**(Rat(7, 2))) Z = Y**(Rat(1, 7)) expr = 3*Z**4 - 3*Z**2 event = (expr <= 9) answer = event.solve() assert answer == solution interval = (~event).solve() assert interval == Interval.open(solution.right, oo)
def test_FiniteNominal_or(): # EmptySet assert FN('a', 'b') | EmptySet == FN('a', 'b') # Nominal assert FN('a', 'b') | FN('c') == FN('a', 'b', 'c') assert FN('a', 'b', 'c') | FN('a') == FN('a', 'b', 'c') assert FN('a', 'b', 'c') | FN(b=True) == FN(b=True) assert FN('a', 'b', 'c', b=True) | FN('a') == FN('b', 'c', b=True) assert FN('a', 'b', 'c', b=True) | FN('d', 'a', 'b') == FN('c', b=True) assert FN('a', 'b', 'c', b=True) | FN('d') == FN('a', 'b', 'c', b=True) assert FN('a', 'b', 'c') | FN('a', b=True) == FN(b=True) assert FN('a', 'b', 'c') | FN('d', 'a', 'b', b=True) == FN('d', b=True) assert FN('a', 'b', 'c') | FN('d', b=True) == FN('d', b=True) assert FN('a', 'b', 'c', b=True) | FN('d', b=True) == FN(b=True) assert FN('a', 'b', 'c', b=True) | FN('a', b=True) == FN('a', b=True) assert FN(b=True) | FN(b=True) == FN(b=True) # FiniteReal assert FN('a') | FR(1) == Union(FN('a'), FR(1)) assert FN('a', b=True) | FR(1) == Union(FR(1), FN('a', b=True)) # Interval assert FN('a') | Interval(0, 1) == Union(FN('a'), Interval(0, 1))
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
def test_solver_26_piecewise_one_expr_basic_event(): event = (Y**2)*(0 <= Y) < 2 assert event.solve() == Interval.Ropen(0, sympy.sqrt(2)) event = (0 <= Y)*(Y**2) < 2 assert event.solve() == Interval.Ropen(0, sympy.sqrt(2)) event = ((0 <= Y) < 5)*(Y < 1) << {1} assert event.solve() == Interval.Ropen(0, 1) event = ((0 <= Y) < 5)*(~(Y < 1)) << {1} assert event.solve() == Interval.Ropen(1, 5) event = 10*(0 <= Y) << {10} assert event.solve() == Interval(0, oo) event = 10*(0 <= Y) << {0} assert event.solve() == Interval.Ropen(-oo, 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)), ])])
def test_parse_rv_discrete(): for dist in [ rv_discrete(values=((1, 2, 10), (.3, .5, .2))), discrete({ 1: .3, 2: .5, 10: .2 }) ]: spe = dist(X) assert spe.support == Interval(1, 10) assert allclose(spe.prob(X << {1}), .3) assert allclose(spe.prob(X << {2}), .5) assert allclose(spe.prob(X << {10}), .2) assert allclose(spe.prob(X <= 10), 1) dist = uniformd(values=((1, 2, 10, 0))) spe = dist(X) assert spe.support == Interval(0, 10) assert allclose(spe.prob(X << {1}), .25) assert allclose(spe.prob(X << {2}), .25) assert allclose(spe.prob(X << {10}), .25) assert allclose(spe.prob(X << {0}), .25)
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])
def test_solver_19(): # 3*(x**(1/7))**4 - 3*(x**(1/7))**2 <= 9 # or || 3*(x**(1/7))**4 - 3*(x**(1/7))**2 > 11 solution = Union( Interval(0, (Rat(1, 2) + sympy.sqrt(13)/2)**(Rat(7, 2))), Interval.open((Rat(1,2) + sympy.sqrt(141)/6)**(Rat(7, 2)), oo)) Z = Y**(Rat(1, 7)) expr = 3*Z**4 - 3*Z**2 event = (expr <= 9) | (expr > 11) answer = event.solve() assert answer == solution interval = (~event).solve() assert interval == Interval.Lopen( solution.args[0].right, solution.args[1].left)
def test_Interval_in(): with pytest.raises(Exception): Interval(3, 1) assert 1 in Interval(0, 1) assert 1 not in Interval.Ropen(0, 1) assert 1 in Interval.Lopen(0, 1) assert 0 in Interval(0, 1) assert 0 not in Interval.Lopen(0, 1) assert 0 in Interval.Ropen(0, 1) assert inf not in Interval(-inf, inf) assert -inf not in Interval(-inf, 0) assert 10 in Interval(-inf, inf)
def test_union_intervals_finite(): assert union_intervals_finite([ Interval.open(0,1), Interval(2,3), Interval.Lopen(1,2) ], FR(1)) \ == [Interval.Lopen(0, 3)] assert union_intervals_finite([ Interval.open(0,1), Interval.open(2, 3), Interval.open(1,2) ], FR(1, 3)) \ == [Interval.open(0, 2), Interval.Lopen(2, 3)] assert union_intervals_finite([ Interval.open(0,1), Interval.open(1, 3), Interval.open(11,15) ], FR(1, -11, -19, 3)) \ == [Interval.Lopen(0, 3), Interval.open(11,15), FR(-11, -19)]
def test_solver_finite_non_injective(): sqrt2 = sympy.sqrt(2) # Abs. solution = FiniteReal(-10, -3, 3, 10) event = abs(Y) << {10, 3} assert event.solve() == solution # Abs(Poly). solution = FiniteReal(-5, -Rat(3,2), Rat(3,2), 5) event = abs(2*Y) << {10, 3} assert event.solve() == solution # Poly order 2. solution = FiniteReal(-sqrt2, sqrt2) event = (Y**2) << {2} assert event.solve() == solution # Poly order 3. solution = FiniteReal(1, 3) event = Y**3 << {1, 27} assert event.solve() == solution # Poly Abs. solution = FiniteReal(-3, -1, 1, 3) event = (abs(Y))**3 << {1, 27} assert event.solve() == solution # Abs Not. solution = Union( Interval.open(-oo, -1), Interval.open(-1, 1), Interval.open(1, oo)) event = ~(abs(Y) << {1}) assert event.solve() == solution # Abs in EmptySet. solution = EmptySet event = (abs(Y))**3 << set() assert event.solve() == solution # Abs Not in EmptySet (yields all reals). solution = Interval(-oo, oo) event = ~(((abs(Y))**3) << set()) assert event.solve() == solution # Log in Reals (yields positive reals). solution = Interval.open(0, oo) event = ~((Log(Y))**3 << set()) assert event.solve() == solution