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_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
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_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_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
def test_rdivide_reciprocal(): assert 1 / X == Reciprocal(X) assert 1 / abs(X) == Reciprocal(abs(X)) assert 3 / abs(X) == Poly(Reciprocal(abs(X)), [0, 3]) assert 3 / abs(X) == Poly(Reciprocal(abs(X)), [0, 3]) assert 1 / (X**2 + 2) == Reciprocal(Poly(X, [2, 0, 1])) assert 2 / (X**2 + 2) == Poly(Reciprocal(Poly(X, [2, 0, 1])), [0, 2]) assert -2 / (X**2 + 2) == Poly(Reciprocal(Poly(X, [2, 0, 1])), [0, -2]) assert 1 + 3 * (1/(abs(X))) + 10 * (1/abs(X))**2 \ == Poly(Reciprocal(abs(X)), [1, 3, 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_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_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))
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
def test_parse_17(): # https://www.wolframalpha.com/input/?i=Expand%5B%2810%2F7+%2B+X%29+%28-1%2F%285+Sqrt%5B2%5D%29+%2B+X%29+%28-Sqrt%5B5%5D+%2B+X%29%5D for Z in [X, Log(X), Abs(1+X**2)]: expr = (Z - Rat(1, 10) * sympy.sqrt(2)) \ * (Z + Rat(10, 7)) \ * (Z - sympy.sqrt(5)) coeffs = [ sympy.sqrt(10)/7, 1/sympy.sqrt(10) - (10*sympy.sqrt(5))/7 - sympy.sqrt(2)/7, (-sympy.sqrt(5) - 1/(5 * sympy.sqrt(2))) + Rat(10)/7, 1, ] expr_prime = Poly(Z, coeffs) assert expr == expr_prime
def test_divide_multiplication(): assert (X**2 + 2) / 2 == Poly(X, [1, 0, Rat(1, 2)])
def test_negate_polynomial(): assert -(X**2 + 2) == Poly(X, [-2, 0, -1])
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
def test_parse_13(): # 2*sqrt(|x|**2) - 3 > 10 expr = 2*Sqrt(Abs(X)**2) - 3 expr_prime = Poly(Sqrt(Pow(Abs(Y), 2)), [-3, 2]) assert expr == expr_prime