Esempio n. 1
0
def test_piecewise():

    # Test canonicalization
    assert Piecewise((x, x < 1), (0, True)) == Piecewise((x, x < 1), (0, True))
    assert Piecewise((x, x < 1), (0, True), (1, True)) == \
        Piecewise((x, x < 1), (0, True))
    assert Piecewise((x, x < 1), (0, False), (-1, 1 > 2)) == \
        Piecewise((x, x < 1))
    assert Piecewise((x, x < 1), (0, x < 1), (0, True)) == \
        Piecewise((x, x < 1), (0, True))
    assert Piecewise((x, x < 1), (0, x < 2), (0, True)) == \
        Piecewise((x, x < 1), (0, True))
    assert Piecewise((x, x < 1), (x, x < 2), (0, True)) == \
        Piecewise((x, Or(x < 1, x < 2)), (0, True))
    assert Piecewise((x, x < 1), (x, x < 2), (x, True)) == x
    assert Piecewise((x, True)) == x
    # False condition is never retained
    assert Piecewise((x, False)) == Piecewise(
        (x, False), evaluate=False) == Piecewise()
    raises(TypeError, lambda: Piecewise(x))
    assert Piecewise((x, 1)) == x  # 1 and 0 are accepted as True/False
    raises(TypeError, lambda: Piecewise((x, 2)))
    raises(TypeError, lambda: Piecewise((x, x**2)))
    raises(TypeError, lambda: Piecewise(([1], True)))
    assert Piecewise(((1, 2), True)) == Tuple(1, 2)
    cond = (Piecewise((1, x < 0), (2, True)) < y)
    assert Piecewise((1, cond)) == Piecewise((1, ITE(x < 0, y > 1, y > 2)))

    assert Piecewise((1, x > 0), (2, And(x <= 0, x > -1))) == Piecewise(
        (1, x > 0), (2, x > -1))

    # Test subs
    p = Piecewise((-1, x < -1), (x**2, x < 0), (log(x), x >= 0))
    p_x2 = Piecewise((-1, x**2 < -1), (x**4, x**2 < 0), (log(x**2), x**2 >= 0))
    assert p.subs(x, x**2) == p_x2
    assert p.subs(x, -5) == -1
    assert p.subs(x, -1) == 1
    assert p.subs(x, 1) == log(1)

    # More subs tests
    p2 = Piecewise((1, x < pi), (-1, x < 2 * pi), (0, x > 2 * pi))
    p3 = Piecewise((1, Eq(x, 0)), (1 / x, True))
    p4 = Piecewise((1, Eq(x, 0)), (2, 1 / x > 2))
    assert p2.subs(x, 2) == 1
    assert p2.subs(x, 4) == -1
    assert p2.subs(x, 10) == 0
    assert p3.subs(x, 0.0) == 1
    assert p4.subs(x, 0.0) == 1

    f, g, h = symbols('f,g,h', cls=Function)
    pf = Piecewise((f(x), x < -1), (f(x) + h(x) + 2, x <= 1))
    pg = Piecewise((g(x), x < -1), (g(x) + h(x) + 2, x <= 1))
    assert pg.subs(g, f) == pf

    assert Piecewise((1, Eq(x, 0)), (0, True)).subs(x, 0) == 1
    assert Piecewise((1, Eq(x, 0)), (0, True)).subs(x, 1) == 0
    assert Piecewise((1, Eq(x, y)), (0, True)).subs(x, y) == 1
    assert Piecewise((1, Eq(x, z)), (0, True)).subs(x, z) == 1
    assert Piecewise((1, Eq(exp(x), cos(z))), (0, True)).subs(x, z) == \
        Piecewise((1, Eq(exp(z), cos(z))), (0, True))

    p5 = Piecewise((0, Eq(cos(x) + y, 0)), (1, True))
    assert p5.subs(y, 0) == Piecewise((0, Eq(cos(x), 0)), (1, True))

    assert Piecewise((-1, y < 1), (0, x < 0), (1, Eq(x, 0)),
                     (2, True)).subs(x, 1) == Piecewise((-1, y < 1), (2, True))
    assert Piecewise((1, Eq(x**2, -1)), (2, x < 0)).subs(x, I) == 1

    # Test evalf
    assert p.evalf() == p
    assert p.evalf(subs={x: -2}) == -1
    assert p.evalf(subs={x: -1}) == 1
    assert p.evalf(subs={x: 1}) == log(1)

    # Test doit
    f_int = Piecewise((Integral(x, (x, 0, 1)), x < 1))
    assert f_int.doit() == Piecewise((1 / 2, x < 1))

    # Test differentiation
    f = x
    fp = x * p
    dp = Piecewise((0, x < -1), (2 * x, x < 0), (1 / x, x >= 0))
    fp_dx = x * dp + p
    assert diff(p, x) == dp
    assert diff(f * p, x) == fp_dx

    # Test simple arithmetic
    assert x * p == fp
    assert x * p + p == p + x * p
    assert p + f == f + p
    assert p + dp == dp + p
    assert p - dp == -(dp - p)

    # Test power
    dp2 = Piecewise((0, x < -1), (4 * x**2, x < 0), (1 / x**2, x >= 0))
    assert dp**2 == dp2

    # Test _eval_interval
    f1 = x * y + 2
    f2 = x * y**2 + 3
    peval = Piecewise((f1, x < 0), (f2, x > 0))
    peval_interval = f1.subs(x, 0) - f1.subs(x, -1) + f2.subs(x, 1) - f2.subs(
        x, 0)
    assert peval._eval_interval(x, 0, 0) == 0
    assert peval._eval_interval(x, -1, 1) == peval_interval
    peval2 = Piecewise((f1, x < 0), (f2, True))
    assert peval2._eval_interval(x, 0, 0) == 0
    assert peval2._eval_interval(x, 1, -1) == -peval_interval
    assert peval2._eval_interval(x, -1, -2) == f1.subs(x, -2) - f1.subs(x, -1)
    assert peval2._eval_interval(x, -1, 1) == peval_interval
    assert peval2._eval_interval(x, None, 0) == peval2.subs(x, 0)
    assert peval2._eval_interval(x, -1, None) == -peval2.subs(x, -1)

    # Test integration
    assert p.integrate() == Piecewise((-x, x < -1), (x**3 / 3 + 4 / 3, x < 0),
                                      (x * log(x) - x + 4 / 3, True))
    p = Piecewise((x, x < 1), (x**2, -1 <= x), (x, 3 < x))
    assert integrate(p, (x, -2, 2)) == 5 / 6.0
    assert integrate(p, (x, 2, -2)) == -5 / 6.0
    p = Piecewise((0, x < 0), (1, x < 1), (0, x < 2), (1, x < 3), (0, True))
    assert integrate(p, (x, -oo, oo)) == 2
    p = Piecewise((x, x < -10), (x**2, x <= -1), (x, 1 < x))
    assert integrate(p, (x, -2, 2)) == Undefined

    # Test commutativity
    assert isinstance(p, Piecewise) and p.is_commutative is True
Esempio n. 2
0
def test_solve_ics():
    # Basic tests that things work from dsolve.
    assert dsolve(f(x).diff(x) - 1/f(x), f(x), ics={f(1): 2}) == \
        Eq(f(x), sqrt(2 * x + 2))
    assert dsolve(f(x).diff(x) - f(x), f(x), ics={f(0): 1}) == Eq(f(x), exp(x))
    assert dsolve(f(x).diff(x) - f(x), f(x), ics={f(x).diff(x).subs(x, 0):
                                                  1}) == Eq(f(x), exp(x))
    assert dsolve(f(x).diff(x, x) + f(x),
                  f(x),
                  ics={
                      f(0): 1,
                      f(x).diff(x).subs(x, 0): 1
                  }) == Eq(f(x),
                           sin(x) + cos(x))
    assert dsolve([f(x).diff(x) - f(x) + g(x),
                   g(x).diff(x) - g(x) - f(x)], [f(x), g(x)],
                  ics={
                      f(0): 1,
                      g(0): 0
                  }) == [Eq(f(x),
                            exp(x) * cos(x)),
                         Eq(g(x),
                            exp(x) * sin(x))]

    # Test cases where dsolve returns two solutions.
    eq = (x**2 * f(x)**2 - x).diff(x)
    assert dsolve(eq, f(x), ics={f(1): 0}) == [
                                     Eq(f(x), -sqrt(x - 1) / x),
                                     Eq(f(x),
                                        sqrt(x - 1) / x)
                                 ]
    assert dsolve(eq, f(x), ics={f(x).diff(x).subs(x, 1): 0}) == [
                                     Eq(f(x), -sqrt(x - S.Half) / x),
                                     Eq(f(x),
                                        sqrt(x - S.Half) / x)
                                 ]

    eq = cos(f(x)) - (x * sin(f(x)) - f(x)**2) * f(x).diff(x)
    assert dsolve(eq, f(x), ics={f(0): 1}, hint='1st_exact',
                  simplify=False) == Eq(x * cos(f(x)) + f(x)**3 / 3,
                                        Rational(1, 3))
    assert dsolve(eq, f(x), ics={f(0): 1}, hint='1st_exact',
                  simplify=True) == Eq(x * cos(f(x)) + f(x)**3 / 3,
                                       Rational(1, 3))

    assert solve_ics([Eq(f(x), C1 * exp(x))], [f(x)], [C1], {f(0): 1}) == {
                                                                 C1: 1
                                                             }
    assert solve_ics([Eq(f(x),
                         C1 * sin(x) + C2 * cos(x))], [f(x)], [C1, C2], {
                             f(0): 1,
                             f(pi / 2): 1
                         }) == {
                             C1: 1,
                             C2: 1
                         }

    assert solve_ics([Eq(f(x),
                         C1 * sin(x) + C2 * cos(x))], [f(x)], [C1, C2], {
                             f(0): 1,
                             f(x).diff(x).subs(x, 0): 1
                         }) == {
                             C1: 1,
                             C2: 1
                         }

    assert solve_ics([Eq(f(x), C1*sin(x) + C2*cos(x))], [f(x)], [C1, C2], {f(0): 1}) == \
        {C2: 1}

    # Some more complicated tests Refer to PR #16098

    assert set(dsolve(f(x).diff(x)*(f(x).diff(x, 2)-x), ics={f(0):0, f(x).diff(x).subs(x, 1):0})) == \
        {Eq(f(x), 0), Eq(f(x), x ** 3 / 6 - x / 2)}
    assert set(dsolve(f(x).diff(x)*(f(x).diff(x, 2)-x), ics={f(0):0})) == \
        {Eq(f(x), 0), Eq(f(x), C2*x + x**3/6)}

    K, r, f0 = symbols('K r f0')
    sol = Eq(
        f(x),
        K * f0 * exp(r * x) / ((-K + f0) * (f0 * exp(r * x) / (-K + f0) - 1)))
    assert (dsolve(Eq(f(x).diff(x),
                      r * f(x) * (1 - f(x) / K)),
                   f(x),
                   ics={f(0): f0})) == sol

    #Order dependent issues Refer to PR #16098
    assert set(dsolve(f(x).diff(x)*(f(x).diff(x, 2)-x), ics={f(x).diff(x).subs(x,0):0, f(0):0})) == \
        {Eq(f(x), 0), Eq(f(x), x ** 3 / 6)}
    assert set(dsolve(f(x).diff(x)*(f(x).diff(x, 2)-x), ics={f(0):0, f(x).diff(x).subs(x,0):0})) == \
        {Eq(f(x), 0), Eq(f(x), x ** 3 / 6)}

    # XXX: Ought to be ValueError
    raises(
        ValueError,
        lambda: solve_ics([Eq(f(x),
                              C1 * sin(x) + C2 * cos(x))], [f(x)], [C1, C2], {
                                  f(0): 1,
                                  f(pi): 1
                              }))

    # Degenerate case. f'(0) is identically 0.
    raises(
        ValueError, lambda: solve_ics([Eq(f(x), sqrt(C1 - x**2))], [f(x)],
                                      [C1], {f(x).diff(x).subs(x, 0): 0}))

    EI, q, L = symbols('EI q L')

    # eq = Eq(EI*diff(f(x), x, 4), q)
    sols = [
        Eq(f(x), C1 + C2 * x + C3 * x**2 + C4 * x**3 + q * x**4 / (24 * EI))
    ]
    funcs = [f(x)]
    constants = [C1, C2, C3, C4]
    # Test both cases, Derivative (the default from f(x).diff(x).subs(x, L)),
    # and Subs
    ics1 = {
        f(0): 0,
        f(x).diff(x).subs(x, 0): 0,
        f(L).diff(L, 2): 0,
        f(L).diff(L, 3): 0
    }
    ics2 = {
        f(0): 0,
        f(x).diff(x).subs(x, 0): 0,
        Subs(f(x).diff(x, 2), x, L): 0,
        Subs(f(x).diff(x, 3), x, L): 0
    }

    solved_constants1 = solve_ics(sols, funcs, constants, ics1)
    solved_constants2 = solve_ics(sols, funcs, constants, ics2)
    assert solved_constants1 == solved_constants2 == {
        C1: 0,
        C2: 0,
        C3: L**2 * q / (4 * EI),
        C4: -L * q / (6 * EI)
    }
Esempio n. 3
0
def test_equals():
    w, x, y, z = symbols('w:z')
    f = Function('f')
    assert Eq(x, 1).equals(Eq(x * (y + 1) - x * y - x + 1, x))
    assert Eq(x, y).equals(x < y, True) == False
    assert Eq(x, f(1)).equals(Eq(x, f(2)), True) == f(1) - f(2)
    assert Eq(f(1), y).equals(Eq(f(2), y), True) == f(1) - f(2)
    assert Eq(x, f(1)).equals(Eq(f(2), x), True) == f(1) - f(2)
    assert Eq(f(1), x).equals(Eq(x, f(2)), True) == f(1) - f(2)
    assert Eq(w, x).equals(Eq(y, z), True) == False
    assert Eq(f(1), f(2)).equals(Eq(f(3), f(4)), True) == f(1) - f(3)
    assert (x < y).equals(y > x, True) == True
    assert (x < y).equals(y >= x, True) == False
    assert (x < y).equals(z < y, True) == False
    assert (x < y).equals(x < z, True) == False
    assert (x < f(1)).equals(x < f(2), True) == f(1) - f(2)
    assert (f(1) < x).equals(f(2) < x, True) == f(1) - f(2)
def test_evalf_relational():
    assert Eq(x / 5, y / 10).evalf() == Eq(0.2 * x, 0.1 * y)
Esempio n. 5
0
def test_classify_ode():
    assert classify_ode(f(x).diff(x, 2), f(x)) == \
        (
        'nth_algebraic',
        'nth_linear_constant_coeff_homogeneous',
        'nth_linear_euler_eq_homogeneous',
        'Liouville',
        '2nd_power_series_ordinary',
        'nth_algebraic_Integral',
        'Liouville_Integral',
        )
    assert classify_ode(f(x),
                        f(x)) == ('nth_algebraic', 'nth_algebraic_Integral')
    assert classify_ode(
        Eq(f(x).diff(x), 0),
        f(x)) == ('nth_algebraic', 'separable', '1st_exact', '1st_linear',
                  'Bernoulli', '1st_homogeneous_coeff_best',
                  '1st_homogeneous_coeff_subs_indep_div_dep',
                  '1st_homogeneous_coeff_subs_dep_div_indep',
                  '1st_power_series', 'lie_group',
                  'nth_linear_constant_coeff_homogeneous',
                  'nth_linear_euler_eq_homogeneous', 'nth_algebraic_Integral',
                  'separable_Integral', '1st_exact_Integral',
                  '1st_linear_Integral', 'Bernoulli_Integral',
                  '1st_homogeneous_coeff_subs_indep_div_dep_Integral',
                  '1st_homogeneous_coeff_subs_dep_div_indep_Integral')
    assert classify_ode(
        f(x).diff(x)**2,
        f(x)) == ('factorable', 'nth_algebraic', 'separable', '1st_exact',
                  '1st_linear', 'Bernoulli', '1st_homogeneous_coeff_best',
                  '1st_homogeneous_coeff_subs_indep_div_dep',
                  '1st_homogeneous_coeff_subs_dep_div_indep',
                  '1st_power_series', 'lie_group',
                  'nth_linear_euler_eq_homogeneous', 'nth_algebraic_Integral',
                  'separable_Integral', '1st_exact_Integral',
                  '1st_linear_Integral', 'Bernoulli_Integral',
                  '1st_homogeneous_coeff_subs_indep_div_dep_Integral',
                  '1st_homogeneous_coeff_subs_dep_div_indep_Integral')
    # issue 4749: f(x) should be cleared from highest derivative before classifying
    a = classify_ode(Eq(f(x).diff(x) + f(x), x), f(x))
    b = classify_ode(f(x).diff(x) * f(x) + f(x) * f(x) - x * f(x), f(x))
    c = classify_ode(f(x).diff(x) / f(x) + f(x) / f(x) - x / f(x), f(x))
    assert a == ('1st_exact', '1st_linear', 'Bernoulli', 'almost_linear',
                 '1st_power_series', "lie_group",
                 'nth_linear_constant_coeff_undetermined_coefficients',
                 'nth_linear_constant_coeff_variation_of_parameters',
                 '1st_exact_Integral', '1st_linear_Integral',
                 'Bernoulli_Integral', 'almost_linear_Integral',
                 'nth_linear_constant_coeff_variation_of_parameters_Integral')
    assert b == ('factorable', '1st_linear', 'Bernoulli', '1st_power_series',
                 'lie_group',
                 'nth_linear_constant_coeff_undetermined_coefficients',
                 'nth_linear_constant_coeff_variation_of_parameters',
                 '1st_linear_Integral', 'Bernoulli_Integral',
                 'nth_linear_constant_coeff_variation_of_parameters_Integral')
    assert c == ('1st_linear', 'Bernoulli', '1st_power_series', 'lie_group',
                 'nth_linear_constant_coeff_undetermined_coefficients',
                 'nth_linear_constant_coeff_variation_of_parameters',
                 '1st_linear_Integral', 'Bernoulli_Integral',
                 'nth_linear_constant_coeff_variation_of_parameters_Integral')

    assert classify_ode(
        2 * x * f(x) * f(x).diff(x) + (1 + x) * f(x)**2 - exp(x),
        f(x)) == ('1st_exact', 'Bernoulli', 'almost_linear', 'lie_group',
                  '1st_exact_Integral', 'Bernoulli_Integral',
                  'almost_linear_Integral')
    assert 'Riccati_special_minus2' in \
        classify_ode(2*f(x).diff(x) + f(x)**2 - f(x)/x + 3*x**(-2), f(x))
    raises(ValueError,
           lambda: classify_ode(x + f(x, y).diff(x).diff(y), f(x, y)))
    # issue 5176
    k = Symbol('k')
    assert classify_ode(f(x).diff(x)/(k*f(x) + k*x*f(x)) + 2*f(x)/(k*f(x) +
        k*x*f(x)) + x*f(x).diff(x)/(k*f(x) + k*x*f(x)) + z, f(x)) == \
        ('separable', '1st_exact', '1st_linear', 'Bernoulli',
        '1st_power_series', 'lie_group', 'separable_Integral', '1st_exact_Integral',
        '1st_linear_Integral', 'Bernoulli_Integral')
    # preprocessing
    ans = (
        'nth_algebraic', 'separable', '1st_exact', '1st_linear', 'Bernoulli',
        '1st_homogeneous_coeff_best',
        '1st_homogeneous_coeff_subs_indep_div_dep',
        '1st_homogeneous_coeff_subs_dep_div_indep', '1st_power_series',
        'lie_group', 'nth_linear_constant_coeff_undetermined_coefficients',
        'nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients',
        'nth_linear_constant_coeff_variation_of_parameters',
        'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters',
        'nth_algebraic_Integral', 'separable_Integral', '1st_exact_Integral',
        '1st_linear_Integral', 'Bernoulli_Integral',
        '1st_homogeneous_coeff_subs_indep_div_dep_Integral',
        '1st_homogeneous_coeff_subs_dep_div_indep_Integral',
        'nth_linear_constant_coeff_variation_of_parameters_Integral',
        'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters_Integral')
    #     w/o f(x) given
    assert classify_ode(diff(f(x) + x, x) + diff(f(x), x)) == ans
    #     w/ f(x) and prep=True
    assert classify_ode(diff(f(x) + x, x) + diff(f(x), x), f(x),
                        prep=True) == ans

    assert classify_ode(Eq(2*x**3*f(x).diff(x), 0), f(x)) == \
        ('factorable', 'nth_algebraic', 'separable', '1st_exact',
         '1st_linear', 'Bernoulli', '1st_power_series',
         'lie_group', 'nth_linear_euler_eq_homogeneous',
         'nth_algebraic_Integral', 'separable_Integral', '1st_exact_Integral',
         '1st_linear_Integral', 'Bernoulli_Integral')


    assert classify_ode(Eq(2*f(x)**3*f(x).diff(x), 0), f(x)) == \
        ('factorable', 'nth_algebraic', 'separable', '1st_exact', '1st_linear',
         'Bernoulli', '1st_power_series', 'lie_group', 'nth_algebraic_Integral',
         'separable_Integral', '1st_exact_Integral', '1st_linear_Integral',
         'Bernoulli_Integral')
    # test issue 13864
    assert classify_ode(Eq(diff(f(x), x) - f(x)**x, 0), f(x)) == \
        ('1st_power_series', 'lie_group')
    assert isinstance(classify_ode(Eq(f(x), 5), f(x), dict=True), dict)

    #This is for new behavior of classify_ode when called internally with default, It should
    # return the first hint which matches therefore, 'ordered_hints' key will not be there.
    assert sorted(classify_ode(Eq(f(x).diff(x), 0), f(x), dict=True).keys()) == \
        ['default', 'nth_linear_constant_coeff_homogeneous', 'order']
    a = classify_ode(2 * x * f(x) * f(x).diff(x) + (1 + x) * f(x)**2 - exp(x),
                     f(x),
                     dict=True,
                     hint='Bernoulli')
    assert sorted(a.keys()) == [
        'Bernoulli', 'Bernoulli_Integral', 'default', 'order', 'ordered_hints'
    ]
Esempio n. 6
0
 def assertExpressionEqual(self, lhs: Expression, rhs: Expression):
     self.assertTrue(
         bool(Eq(lhs.sympified_expression, rhs.sympified_expression)),
         '{} and {} are not equal'.format(lhs, rhs))
Esempio n. 7
0
def test_conjugate_priors():
    mu = Normal('mu', 2, 3)
    x = Normal('x', mu, 1)
    assert isinstance(simplify(density(mu, Eq(x, y), evaluate=False)(z)), Mul)
Esempio n. 8
0
def test_issue_11045():
    assert integrate(1 / (x * sqrt(x**2 - 1)), (x, 1, 2)) == pi / 3

    # handle And with Or arguments
    assert Piecewise((1, And(Or(x < 1, x > 3), x < 2)), (0, True)).integrate(
        (x, 0, 3)) == 1

    # hidden false
    assert Piecewise((1, x > 1), (2, x > x + 1), (3, True)).integrate(
        (x, 0, 3)) == 5
    # targetcond is Eq
    assert Piecewise((1, x > 1), (2, Eq(1, x)), (3, True)).integrate(
        (x, 0, 4)) == 6
    # And has Relational needing to be solved
    assert Piecewise((1, And(2 * x > x + 1, x < 2)), (0, True)).integrate(
        (x, 0, 3)) == 1
    # Or has Relational needing to be solved
    assert Piecewise((1, Or(2 * x > x + 2, x < 1)), (0, True)).integrate(
        (x, 0, 3)) == 2
    # ignore hidden false (handled in canonicalization)
    assert Piecewise((1, x > 1), (2, x > x + 1), (3, True)).integrate(
        (x, 0, 3)) == 5
    # watch for hidden True Piecewise
    assert Piecewise((2, Eq(1 - x, x * (1 / x - 1))), (0, True)).integrate(
        (x, 0, 3)) == 6

    # overlapping conditions of targetcond are recognized and ignored;
    # the condition x > 3 will be pre-empted by the first condition
    assert Piecewise((1, Or(x < 1, x > 2)), (2, x > 3), (3, True)).integrate(
        (x, 0, 4)) == 6

    # convert Ne to Or
    assert Piecewise((1, Ne(x, 0)), (2, True)).integrate((x, -1, 1)) == 2

    # no default but well defined
    assert Piecewise((x, (x > 1) & (x < 3)), (1, (x < 4))).integrate(
        (x, 1, 4)) == 5

    p = Piecewise((x, (x > 1) & (x < 3)), (1, (x < 4)))
    nan = Undefined
    i = p.integrate((x, 1, y))
    assert i == Piecewise(
        (y - 1, y < 1),
        (Min(3, y)**2 / 2 - Min(3, y) + Min(4, y) - 1 / 2, y <= Min(4, y)),
        (nan, True))
    assert p.integrate((x, 1, -1)) == i.subs(y, -1)
    assert p.integrate((x, 1, 4)) == 5
    assert p.integrate((x, 1, 5)) == nan

    # handle Not
    p = Piecewise((1, x > 1), (2, Not(And(x > 1, x < 3))), (3, True))
    assert p.integrate((x, 0, 3)) == 4

    # handle updating of int_expr when there is overlap
    p = Piecewise((1, And(5 > x, x > 1)), (2, Or(x < 3, x > 7)), (4, x < 8))
    assert p.integrate((x, 0, 10)) == 20

    # And with Eq arg handling
    assert Piecewise((1, x < 1), (2, And(Eq(x, 3), x > 1))).integrate(
        (x, 0, 3)) == S.NaN
    assert Piecewise((1, x < 1), (2, And(Eq(x, 3), x > 1)),
                     (3, True)).integrate((x, 0, 3)) == 7
    assert Piecewise((1, x < 0), (2, And(Eq(x, 3), x < 1)),
                     (3, True)).integrate((x, -1, 1)) == 4
    # middle condition doesn't matter: it's a zero width interval
    assert Piecewise((1, x < 1), (2, Eq(x, 3) & (y < x)), (3, True)).integrate(
        (x, 0, 3)) == 7
Esempio n. 9
0
# Program 01a: A program that solves a simple ODE.
# See Example 10.

from sympy import dsolve, Eq, symbols, Function

t = symbols('t')
x = symbols('x', cls=Function)

deqn1 = Eq(x(t).diff(t), 1 - x(t))
sol1 = dsolve(deqn1, x(t))

print(sol1)
Esempio n. 10
0
def test_piecewise_fold_piecewise_in_cond_2():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (1 / p1, True))
    p3 = Piecewise((0, (x >= 0) | Eq(cos(x), 0)), (1 / cos(x), x < 0),
                   (zoo, True))  # redundant b/c all x are already covered
    assert (piecewise_fold(p2) == p3)
Esempio n. 11
0
def test_S_srepr_is_identity():
    p = Piecewise((10, Eq(x, 0)), (12, True))
    q = S(srepr(p))
    assert p == q
Esempio n. 12
0
def test_piecewise_solve():
    abs2 = Piecewise((-x, x <= 0), (x, x > 0))
    f = abs2.subs(x, x - 2)
    assert solve(f, x) == [2]
    assert solve(f - 1, x) == [1, 3]

    f = Piecewise(((x - 2)**2, x >= 0), (1, True))
    assert solve(f, x) == [2]

    g = Piecewise(((x - 5)**5, x >= 4), (f, True))
    assert solve(g, x) == [2, 5]

    g = Piecewise(((x - 5)**5, x >= 4), (f, x < 4))
    assert solve(g, x) == [2, 5]

    g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2))
    assert solve(g, x) == [5]

    g = Piecewise(((x - 5)**5, x >= 2), (f, True))
    assert solve(g, x) == [5]

    g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False))
    assert solve(g, x) == [5]

    g = Piecewise(((x - 5)**5, x >= 2), (-x + 2, x - 2 <= 0),
                  (x - 2, x - 2 > 0))
    assert solve(g, x) == [5]

    # if no symbol is given the piecewise detection must still work
    assert solve(Piecewise((x - 2, x > 2), (2 - x, True)) - 3) == [-1, 5]

    f = Piecewise(((x - 2)**2, x >= 0), (0, True))
    raises(NotImplementedError, lambda: solve(f, x))

    def nona(ans):
        return list(filter(lambda x: x is not S.NaN, ans))

    p = Piecewise((x**2 - 4, x < y), (x - 2, True))
    ans = solve(p, x)
    assert nona([i.subs(y, -2) for i in ans]) == [2]
    assert nona([i.subs(y, 2) for i in ans]) == [-2, 2]
    assert nona([i.subs(y, 3) for i in ans]) == [-2, 2]
    assert ans == [
        Piecewise((-2, y > -2), (S.NaN, True)),
        Piecewise((2, y <= 2), (S.NaN, True)),
        Piecewise((2, y > 2), (S.NaN, True))
    ]

    # issue 6060
    absxm3 = Piecewise((x - 3, S(0) <= x - 3), (3 - x, S(0) > x - 3))
    assert solve(absxm3 - y, x) == [
        Piecewise((-y + 3, -y < 0), (S.NaN, True)),
        Piecewise((y + 3, y >= 0), (S.NaN, True))
    ]
    p = Symbol('p', positive=True)
    assert solve(absxm3 - p, x) == [-p + 3, p + 3]

    # issue 6989
    f = Function('f')
    assert solve(Eq(-f(x), Piecewise((1, x > 0), (0, True))), f(x)) == \
        [Piecewise((-1, x > 0), (0, True))]

    # issue 8587
    f = Piecewise((2 * x**2, And(S(0) < x, x < 1)), (2, True))
    assert solve(f - 1) == [1 / sqrt(2)]
Esempio n. 13
0
def test_piecewise_simplify():
    p = Piecewise(((x**2 + 1) / x**2, Eq(x * (1 + x) - x**2, 0)),
                  ((-1)**x * (-1), True))
    assert p.simplify() == \
        Piecewise((1 + 1/x**2, Eq(x, 0)), ((-1)**(x + 1), True))
Esempio n. 14
0
def test_piecewise_integrate5_independent_conditions():
    p = Piecewise((0, Eq(y, 0)), (x * y, True))
    assert integrate(p, (x, 1, 3)) == Piecewise((0, Eq(y, 0)), (4 * y, True))
Esempio n. 15
0
def test_normality():
    X, Y = Normal('X', 0, 1), Normal('Y', 0, 1)
    x, z = symbols('x, z', real=True, finite=True)
    dens = density(X - Y, Eq(X + Y, z))

    assert integrate(dens(x), (x, -oo, oo)) == 1
Esempio n. 16
0
def test_FiniteSet_prob():
    E = Exponential('E', 3)
    N = Normal('N', 5, 7)
    assert P(Eq(E, 1)) is S.Zero
    assert P(Eq(N, 2)) is S.Zero
    assert P(Eq(N, x)) is S.Zero
def test_CRootOf___eval_Eq__():
    f = Function('f')
    eq = x**3 + x + 3
    r = rootof(eq, 2)
    r1 = rootof(eq, 1)
    assert Eq(r, r1) is S.false
    assert Eq(r, r) is S.true
    assert Eq(r, x) is S.false
    assert Eq(r, 0) is S.false
    assert Eq(r, S.Infinity) is S.false
    assert Eq(r, I) is S.false
    assert Eq(r, f(0)) is S.false
    assert Eq(r, f(0)) is S.false
    sol = solve(eq)
    for s in sol:
        if s.is_real:
            assert Eq(r, s) is S.false
    r = rootof(eq, 0)
    for s in sol:
        if s.is_real:
            assert Eq(r, s) is S.true
    eq = x**3 + x + 1
    sol = solve(eq)
    assert [Eq(rootof(eq, i), j) for i in range(3) for j in sol
            ] == [False, False, True, False, True, False, True, False, False]
    assert Eq(rootof(eq, 0), 1 + S.ImaginaryUnit) == False
Esempio n. 18
0
def euler_equations(L, funcs=(), vars=()):
    r"""
    Find the Euler-Lagrange equations [1]_ for a given Lagrangian.

    Parameters
    ==========

    L : Expr
        The Lagrangian that should be a function of the functions listed
        in the second argument and their derivatives.

        For example, in the case of two functions `f(x,y)`, `g(x,y)` and
        two independent variables `x`, `y` the Lagrangian would have the form:

            .. math:: L\left(f(x,y),g(x,y),\frac{\partial f(x,y)}{\partial x},
                      \frac{\partial f(x,y)}{\partial y},
                      \frac{\partial g(x,y)}{\partial x},
                      \frac{\partial g(x,y)}{\partial y},x,y\right)

        In many cases it is not necessary to provide anything, except the
        Lagrangian, it will be autodetected (and an error raised if this
        couldn't be done).

    funcs : Function or an iterable of Functions
        The functions that the Lagrangian depends on. The Euler equations
        are differential equations for each of these functions.

    vars : Symbol or an iterable of Symbols
        The Symbols that are the independent variables of the functions.

    Returns
    =======

    eqns : list of Eq
        The list of differential equations, one for each function.

    Examples
    ========

    >>> from sympy import Symbol, Function
    >>> from sympy.calculus.euler import euler_equations
    >>> x = Function('x')
    >>> t = Symbol('t')
    >>> L = (x(t).diff(t))**2/2 - x(t)**2/2
    >>> euler_equations(L, x(t), t)
    [-x(t) - Derivative(x(t), t, t) == 0]
    >>> u = Function('u')
    >>> x = Symbol('x')
    >>> L = (u(t, x).diff(t))**2/2 - (u(t, x).diff(x))**2/2
    >>> euler_equations(L, u(t, x), [t, x])
    [-Derivative(u(t, x), t, t) + Derivative(u(t, x), x, x) == 0]

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Euler%E2%80%93Lagrange_equation

    """

    funcs = tuple(funcs) if iterable(funcs) else (funcs, )

    if not funcs:
        funcs = tuple(L.atoms(Function))
    else:
        for f in funcs:
            if not isinstance(f, Function):
                raise TypeError('Function expected, got: %s' % f)

    vars = tuple(vars) if iterable(vars) else (vars, )

    if not vars:
        vars = funcs[0].args
    else:
        vars = tuple(sympify(var) for var in vars)

    if not all(isinstance(v, Symbol) for v in vars):
        raise TypeError('Variables are not symbols, got %s' % vars)

    for f in funcs:
        if not vars == f.args:
            raise ValueError("Variables %s don't match args: %s" % (vars, f))

    order = max(
        len(d.variables) for d in L.atoms(Derivative) if d.expr in funcs)

    eqns = []
    for f in funcs:
        eq = diff(L, f)
        for i in range(1, order + 1):
            for p in combinations_with_replacement(vars, i):
                eq = eq + S.NegativeOne**i * diff(L, diff(f, *p), *p)
        eqns.append(Eq(eq))

    return eqns
Esempio n. 19
0
def test_random_parameters_given():
    mu = Normal('mu', 2, 3)
    meas = Normal('T', mu, 1)
    assert given(meas, Eq(mu, 5)) == Normal('T', 5, 1)
Esempio n. 20
0
from typing import Any, Dict, List, Tuple, Type

from sympy.integrals.meijerint import _create_lookup_table
from sympy import latex, Eq, Add, Symbol

t = {}  # type: Dict[Tuple[Type, ...], List[Any]]
_create_lookup_table(t)

doc = ""

for about, category in sorted(t.items()):
    if about == ():
        doc += 'Elementary functions:\n\n'
    else:
        doc += 'Functions involving ' + ', '.join(
            '`%s`' % latex(list(category[0][0].atoms(func))[0])
            for func in about) + ':\n\n'
    for formula, gs, cond, hint in category:
        if not isinstance(gs, list):
            g = Symbol('\\text{generated}')
        else:
            g = Add(*[fac * f for (fac, f) in gs])
        obj = Eq(formula, g)
        if cond is True:
            cond = ""
        else:
            cond = ',\\text{ if } %s' % latex(cond)
        doc += ".. math::\n  %s%s\n\n" % (latex(obj), cond)

__doc__ = doc
Esempio n. 21
0
def test_conditional_eq():
    E = Exponential('E', 1)
    assert P(Eq(E, 1), Eq(E, 1)) == 1
    assert P(Eq(E, 1), Eq(E, 2)) == 0
    assert P(E > 1, Eq(E, 2)) == 1
    assert P(E < 1, Eq(E, 2)) == 0
Esempio n. 22
0
def test_issue_6263():
    e = Eq(x * (-x + 1) + x * (x - 1), 0)
    assert cse(e, optimizations='basic') == ([], [True])
Esempio n. 23
0
def test_dsolve_options():
    eq = x * f(x).diff(x) + f(x)
    a = dsolve(eq, hint='all')
    b = dsolve(eq, hint='all', simplify=False)
    c = dsolve(eq, hint='all_Integral')
    keys = [
        '1st_exact', '1st_exact_Integral', '1st_homogeneous_coeff_best',
        '1st_homogeneous_coeff_subs_dep_div_indep',
        '1st_homogeneous_coeff_subs_dep_div_indep_Integral',
        '1st_homogeneous_coeff_subs_indep_div_dep',
        '1st_homogeneous_coeff_subs_indep_div_dep_Integral', '1st_linear',
        '1st_linear_Integral', 'Bernoulli', 'Bernoulli_Integral',
        'almost_linear', 'almost_linear_Integral', 'best', 'best_hint',
        'default', 'lie_group', 'nth_linear_euler_eq_homogeneous', 'order',
        'separable', 'separable_Integral'
    ]
    Integral_keys = [
        '1st_exact_Integral',
        '1st_homogeneous_coeff_subs_dep_div_indep_Integral',
        '1st_homogeneous_coeff_subs_indep_div_dep_Integral',
        '1st_linear_Integral', 'Bernoulli_Integral', 'almost_linear_Integral',
        'best', 'best_hint', 'default', 'nth_linear_euler_eq_homogeneous',
        'order', 'separable_Integral'
    ]
    assert sorted(a.keys()) == keys
    assert a['order'] == ode_order(eq, f(x))
    assert a['best'] == Eq(f(x), C1 / x)
    assert dsolve(eq, hint='best') == Eq(f(x), C1 / x)
    assert a['default'] == 'separable'
    assert a['best_hint'] == 'separable'
    assert not a['1st_exact'].has(Integral)
    assert not a['separable'].has(Integral)
    assert not a['1st_homogeneous_coeff_best'].has(Integral)
    assert not a['1st_homogeneous_coeff_subs_dep_div_indep'].has(Integral)
    assert not a['1st_homogeneous_coeff_subs_indep_div_dep'].has(Integral)
    assert not a['1st_linear'].has(Integral)
    assert a['1st_linear_Integral'].has(Integral)
    assert a['1st_exact_Integral'].has(Integral)
    assert a['1st_homogeneous_coeff_subs_dep_div_indep_Integral'].has(Integral)
    assert a['1st_homogeneous_coeff_subs_indep_div_dep_Integral'].has(Integral)
    assert a['separable_Integral'].has(Integral)
    assert sorted(b.keys()) == keys
    assert b['order'] == ode_order(eq, f(x))
    assert b['best'] == Eq(f(x), C1 / x)
    assert dsolve(eq, hint='best', simplify=False) == Eq(f(x), C1 / x)
    assert b['default'] == 'separable'
    assert b['best_hint'] == '1st_linear'
    assert a['separable'] != b['separable']
    assert a['1st_homogeneous_coeff_subs_dep_div_indep'] != \
        b['1st_homogeneous_coeff_subs_dep_div_indep']
    assert a['1st_homogeneous_coeff_subs_indep_div_dep'] != \
        b['1st_homogeneous_coeff_subs_indep_div_dep']
    assert not b['1st_exact'].has(Integral)
    assert not b['separable'].has(Integral)
    assert not b['1st_homogeneous_coeff_best'].has(Integral)
    assert not b['1st_homogeneous_coeff_subs_dep_div_indep'].has(Integral)
    assert not b['1st_homogeneous_coeff_subs_indep_div_dep'].has(Integral)
    assert not b['1st_linear'].has(Integral)
    assert b['1st_linear_Integral'].has(Integral)
    assert b['1st_exact_Integral'].has(Integral)
    assert b['1st_homogeneous_coeff_subs_dep_div_indep_Integral'].has(Integral)
    assert b['1st_homogeneous_coeff_subs_indep_div_dep_Integral'].has(Integral)
    assert b['separable_Integral'].has(Integral)
    assert sorted(c.keys()) == Integral_keys
    raises(ValueError, lambda: dsolve(eq, hint='notarealhint'))
    raises(ValueError, lambda: dsolve(eq, hint='Liouville'))
    assert dsolve(f(x).diff(x) - 1/f(x)**2, hint='all')['best'] == \
        dsolve(f(x).diff(x) - 1/f(x)**2, hint='best')
    assert dsolve(f(x) + f(x).diff(x) + sin(x).diff(x) + 1, f(x),
                  hint="1st_linear_Integral") == \
        Eq(f(x), (C1 + Integral((-sin(x).diff(x) - 1)*
                exp(Integral(1, x)), x))*exp(-Integral(1, x)))
Esempio n. 24
0
def test_postprocess():
    eq = (x + 1 + exp((x + 1) / (y + 1)) + cos(y + 1))
    assert cse([eq, Eq(x, z + 1), z - 2, (z + 1)*(x + 1)],
        postprocess=cse_main.cse_separate) == \
        [[(x0, y + 1), (x2, z + 1), (x, x2), (x1, x + 1)],
        [x1 + exp(x1/x0) + cos(x0), z - 2, x1*x2]]
Esempio n. 25
0
def test_dsolve_all_hint():
    eq = f(x).diff(x)
    output = dsolve(eq, hint='all')

    # Match the Dummy variables:
    sol1 = output['separable_Integral']
    _y = sol1.lhs.args[1][0]
    sol1 = output['1st_homogeneous_coeff_subs_dep_div_indep_Integral']
    _u1 = sol1.rhs.args[1].args[1][0]

    expected = {
        'Bernoulli_Integral':
        Eq(f(x), C1 + Integral(0, x)),
        '1st_homogeneous_coeff_best':
        Eq(f(x), C1),
        'Bernoulli':
        Eq(f(x), C1),
        'nth_algebraic':
        Eq(f(x), C1),
        'nth_linear_euler_eq_homogeneous':
        Eq(f(x), C1),
        'nth_linear_constant_coeff_homogeneous':
        Eq(f(x), C1),
        'separable':
        Eq(f(x), C1),
        '1st_homogeneous_coeff_subs_indep_div_dep':
        Eq(f(x), C1),
        'nth_algebraic_Integral':
        Eq(f(x), C1),
        '1st_linear':
        Eq(f(x), C1),
        '1st_linear_Integral':
        Eq(f(x), C1 + Integral(0, x)),
        '1st_exact':
        Eq(f(x), C1),
        '1st_exact_Integral':
        Eq(Subs(Integral(0, x) + Integral(1, _y), _y, f(x)), C1),
        'lie_group':
        Eq(f(x), C1),
        '1st_homogeneous_coeff_subs_dep_div_indep':
        Eq(f(x), C1),
        '1st_homogeneous_coeff_subs_dep_div_indep_Integral':
        Eq(log(x), C1 + Integral(-1 / _u1, (_u1, f(x) / x))),
        '1st_power_series':
        Eq(f(x), C1),
        'separable_Integral':
        Eq(Integral(1, (_y, f(x))), C1 + Integral(0, x)),
        '1st_homogeneous_coeff_subs_indep_div_dep_Integral':
        Eq(f(x), C1),
        'best':
        Eq(f(x), C1),
        'best_hint':
        'nth_algebraic',
        'default':
        'nth_algebraic',
        'order':
        1
    }
    assert output == expected

    assert dsolve(eq, hint='best') == Eq(f(x), C1)
Esempio n. 26
0
def test_Piecewise():
    f = Piecewise((-z + x * y, Eq(y, 0)), (-z - x * y, True))
    ans = cse(f)
    actual_ans = ([(x0, -z), (x1, x * y)],
                  [Piecewise((x0 + x1, Eq(y, 0)), (x0 - x1, True))])
    assert ans == actual_ans
Esempio n. 27
0
def test_new_relational():
    x = Symbol('x')

    assert Eq(x) == Relational(x, 0)  # None ==> Equality
    assert Eq(x) == Relational(x, 0, '==')
    assert Eq(x) == Relational(x, 0, 'eq')
    assert Eq(x) == Equality(x, 0)
    assert Eq(x, -1) == Relational(x, -1)  # None ==> Equality
    assert Eq(x, -1) == Relational(x, -1, '==')
    assert Eq(x, -1) == Relational(x, -1, 'eq')
    assert Eq(x, -1) == Equality(x, -1)
    assert Eq(x) != Relational(x, 1)  # None ==> Equality
    assert Eq(x) != Relational(x, 1, '==')
    assert Eq(x) != Relational(x, 1, 'eq')
    assert Eq(x) != Equality(x, 1)
    assert Eq(x, -1) != Relational(x, 1)  # None ==> Equality
    assert Eq(x, -1) != Relational(x, 1, '==')
    assert Eq(x, -1) != Relational(x, 1, 'eq')
    assert Eq(x, -1) != Equality(x, 1)

    assert Ne(x, 0) == Relational(x, 0, '!=')
    assert Ne(x, 0) == Relational(x, 0, '<>')
    assert Ne(x, 0) == Relational(x, 0, 'ne')
    assert Ne(x, 0) == Unequality(x, 0)
    assert Ne(x, 0) != Relational(x, 1, '!=')
    assert Ne(x, 0) != Relational(x, 1, '<>')
    assert Ne(x, 0) != Relational(x, 1, 'ne')
    assert Ne(x, 0) != Unequality(x, 1)

    assert Ge(x, 0) == Relational(x, 0, '>=')
    assert Ge(x, 0) == Relational(x, 0, 'ge')
    assert Ge(x, 0) == GreaterThan(x, 0)
    assert Ge(x, 1) != Relational(x, 0, '>=')
    assert Ge(x, 1) != Relational(x, 0, 'ge')
    assert Ge(x, 1) != GreaterThan(x, 0)
    assert (x >= 1) == Relational(x, 1, '>=')
    assert (x >= 1) == Relational(x, 1, 'ge')
    assert (x >= 1) == GreaterThan(x, 1)
    assert (x >= 0) != Relational(x, 1, '>=')
    assert (x >= 0) != Relational(x, 1, 'ge')
    assert (x >= 0) != GreaterThan(x, 1)

    assert Le(x, 0) == Relational(x, 0, '<=')
    assert Le(x, 0) == Relational(x, 0, 'le')
    assert Le(x, 0) == LessThan(x, 0)
    assert Le(x, 1) != Relational(x, 0, '<=')
    assert Le(x, 1) != Relational(x, 0, 'le')
    assert Le(x, 1) != LessThan(x, 0)
    assert (x <= 1) == Relational(x, 1, '<=')
    assert (x <= 1) == Relational(x, 1, 'le')
    assert (x <= 1) == LessThan(x, 1)
    assert (x <= 0) != Relational(x, 1, '<=')
    assert (x <= 0) != Relational(x, 1, 'le')
    assert (x <= 0) != LessThan(x, 1)

    assert Gt(x, 0) == Relational(x, 0, '>')
    assert Gt(x, 0) == Relational(x, 0, 'gt')
    assert Gt(x, 0) == StrictGreaterThan(x, 0)
    assert Gt(x, 1) != Relational(x, 0, '>')
    assert Gt(x, 1) != Relational(x, 0, 'gt')
    assert Gt(x, 1) != StrictGreaterThan(x, 0)
    assert (x > 1) == Relational(x, 1, '>')
    assert (x > 1) == Relational(x, 1, 'gt')
    assert (x > 1) == StrictGreaterThan(x, 1)
    assert (x > 0) != Relational(x, 1, '>')
    assert (x > 0) != Relational(x, 1, 'gt')
    assert (x > 0) != StrictGreaterThan(x, 1)

    assert Lt(x, 0) == Relational(x, 0, '<')
    assert Lt(x, 0) == Relational(x, 0, 'lt')
    assert Lt(x, 0) == StrictLessThan(x, 0)
    assert Lt(x, 1) != Relational(x, 0, '<')
    assert Lt(x, 1) != Relational(x, 0, 'lt')
    assert Lt(x, 1) != StrictLessThan(x, 0)
    assert (x < 1) == Relational(x, 1, '<')
    assert (x < 1) == Relational(x, 1, 'lt')
    assert (x < 1) == StrictLessThan(x, 1)
    assert (x < 0) != Relational(x, 1, '<')
    assert (x < 0) != Relational(x, 1, 'lt')
    assert (x < 0) != StrictLessThan(x, 1)

    # finally, some fuzz testing
    from random import randint
    from sympy.core.compatibility import unichr
    for i in range(100):
        while 1:
            strtype, length = (unichr, 65535) if randint(0, 1) else (chr, 255)
            relation_type = strtype(randint(0, length))
            if randint(0, 1):
                relation_type += strtype(randint(0, length))
            if relation_type not in ('==', 'eq', '!=', '<>', 'ne', '>=', 'ge',
                                     '<=', 'le', '>', 'gt', '<', 'lt', ':=',
                                     '+=', '-=', '*=', '/=', '%='):
                break

        raises(ValueError, lambda: Relational(x, 1, relation_type))
    assert all(Relational(x, 0, op).rel_op == '==' for op in ('eq', '=='))
    assert all(
        Relational(x, 0, op).rel_op == '!=' for op in ('ne', '<>', '!='))
    assert all(Relational(x, 0, op).rel_op == '>' for op in ('gt', '>'))
    assert all(Relational(x, 0, op).rel_op == '<' for op in ('lt', '<'))
    assert all(Relational(x, 0, op).rel_op == '>=' for op in ('ge', '>='))
    assert all(Relational(x, 0, op).rel_op == '<=' for op in ('le', '<='))
Esempio n. 28
0
def test_tsolve_1():
    a, b = symbols('ab')
    x, y, z = symbols('xyz')
    assert solve(exp(x) - 3, x) == [log(3)]
    assert solve((a * x + b) * (exp(x) - 3), x) == [-b / a, log(3)]
    assert solve(cos(x) - y, x) == [acos(y)]
    assert solve(2 * cos(x) - y, x) == [acos(y / 2)]
    raises(NotImplementedError, "solve(Eq(cos(x), sin(x)), x)")

    # XXX in the following test, log(2*y + 2*...) should -> log(2) + log(y +...)
    assert solve(exp(x) + exp(-x) - y, x) == [
        -log(4) + log(2 * y + 2 * (-4 + y**2)**Rational(1, 2)),
        -log(4) + log(2 * y - 2 * (-4 + y**2)**Rational(1, 2))
    ]
    assert solve(exp(x) - 3, x) == [log(3)]
    assert solve(Eq(exp(x), 3), x) == [log(3)]
    assert solve(log(x) - 3, x) == [exp(3)]
    assert solve(sqrt(3 * x) - 4, x) == [Rational(16, 3)]
    assert solve(3**(x + 2), x) == [-oo]
    assert solve(3**(2 - x), x) == [oo]
    assert solve(4 * 3**(5 * x + 2) - 7,
                 x) == [(-log(4) - 2 * log(3) + log(7)) / (5 * log(3))]
    assert solve(x + 2**x, x) == [-LambertW(log(2)) / log(2)]
    assert solve(3*x+5+2**(-5*x+3), x) in \
        [[-Rational(5,3) + LambertW(-10240*2**Rational(1,3)*log(2)/3)/(5*log(2))],\
        [(-25*log(2) + 3*LambertW(-10240*2**(Rational(1, 3))*log(2)/3))/(15*log(2))]]
    assert solve(5*x-1+3*exp(2-7*x), x) == \
        [Rational(1,5) + LambertW(-21*exp(Rational(3,5))/5)/7]
    assert solve(2*x+5+log(3*x-2), x) == \
        [Rational(2,3) + LambertW(2*exp(-Rational(19,3))/3)/2]
    assert solve(3 * x + log(4 * x), x) == [LambertW(Rational(3, 4)) / 3]
    assert solve((2 * x + 8) * (8 + exp(x)), x) == [-4, log(8) + pi * I]
    assert solve(2*exp(3*x+4)-3, x) in [ [-Rational(4,3)+log(Rational(3,2))/3],\
                                         [Rational(-4, 3) - log(2)/3 + log(3)/3]]
    assert solve(2 * log(3 * x + 4) - 3, x) == [(exp(Rational(3, 2)) - 4) / 3]
    assert solve(exp(x) + 1, x) == [pi * I]
    assert solve(x**2 - 2**x, x) == [2]
    assert solve(x**3 - 3**x, x) == [-3 / log(3) * LambertW(-log(3) / 3)]
    assert solve(2*(3*x+4)**5 - 6*7**(3*x+9), x) in \
        [[Rational(-4,3) - 5/log(7)/3*LambertW(-7*2**Rational(4,5)*6**Rational(1,5)*log(7)/10)],\
         [(-5*LambertW(-7*2**(Rational(4, 5))*6**(Rational(1, 5))*log(7)/10) - 4*log(7))/(3*log(7))], \
         [-((4*log(7) + 5*LambertW(-7*2**Rational(4,5)*6**Rational(1,5)*log(7)/10))/(3*log(7)))]]

    assert solve(z * cos(x) - y, x) == [acos(y / z)]
    assert solve(z * cos(2 * x) - y, x) == [acos(y / z) / 2]
    assert solve(z * cos(sin(x)) - y, x) == [asin(acos(y / z))]

    assert solve(z * cos(x), x) == [acos(0)]

    assert solve(exp(x) + exp(-x) - y, x) == [
        -log(4) + log(2 * y + 2 * (-4 + y**2)**(Rational(1, 2))),
        -log(4) + log(2 * y - 2 * (-4 + y**2)**(Rational(1, 2)))
    ]
    # issue #1409
    assert solve(y - b * x / (a + x), x) == [a * y / (b - y)]
    assert solve(y - b * exp(a / x), x) == [a / (-log(b) + log(y))]
    # issue #1408
    assert solve(y - b / (1 + a * x), x) == [(b - y) / (a * y)]
    # issue #1407
    assert solve(y - a * x**b, x) == [y**(1 / b) * (1 / a)**(1 / b)]
    # issue #1406
    assert solve(z**x - y, x) == [log(y) / log(z)]
    # issue #1405
    assert solve(2**x - 10, x) == [log(10) / log(2)]
Esempio n. 29
0
def test_issue_10304():
    d = cos(1)**2 + sin(1)**2 - 1
    assert d.is_comparable is False  # if this fails, find a new d
    e = 1 + d * I
    assert simplify(Eq(e, 0)) is S.false
Esempio n. 30
0
def test_dice_bayes():
    X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6)

    BayesTest(X > 3, X + Y < 5)
    BayesTest(Eq(X - Y, Z), Z > Y)
    BayesTest(X > 3, X > 2)