Пример #1
0
def test_issue_15889():
    eq = exp(f(x).diff(x)) - f(x)**2
    sol = Eq(NonElementaryIntegral(1 / log(y**2), (y, f(x))), C1 + x)
    assert sol.dummy_eq(dsolve(eq))
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x)**2 - f(x)**3
    sol = Eq(f(x), 4 / (C1**2 - 2 * C1 * x + x**2))
    assert sol == dsolve(eq)
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x)**2 - f(x)
    sol = Eq(f(x), C1**2 / 4 - C1 * x / 2 + x**2 / 4)
    assert sol == dsolve(eq)
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x)**2 - f(x)**2
    sol = [Eq(f(x), C1 * exp(x)), Eq(f(x), C1 * exp(-x))]
    assert sol == dsolve(eq)
    assert checkodesol(eq, sol) == 2 * [(True, 0)]

    eq = f(x).diff(x)**2 - f(x)**3
    sol = Eq(f(x), 4 / (C1**2 - 2 * C1 * x + x**2))
    assert sol == dsolve(eq)
    assert checkodesol(eq, sol) == (True, 0)
Пример #2
0
def test_issue_15913():
    eq = -C1/x - 2*x*f(x) - f(x) + Derivative(f(x), x)
    sol = C2*exp(x**2 + x) + exp(x**2 + x)*Integral(C1*exp(-x**2 - x)/x, x)
    assert checkodesol(eq, sol) == (True, 0)
    sol = C1 + C2*exp(-x*y)
    eq = Derivative(y*f(x), x) + f(x).diff(x, 2)
    assert checkodesol(eq, sol, f(x)) == (True, 0)
Пример #3
0
def test_nth_algebraic_redundant_solutions():
    # This one has a redundant solution that should be removed
    eqn = f(x) * f(x).diff(x)
    soln = Eq(f(x), C1)
    assert checkodesol(eqn, soln, order=1, solve_for_func=False)[0]
    assert soln == dsolve(eqn, f(x), hint='nth_algebraic')
    assert soln == dsolve(eqn, f(x))

    # This has two integral solutions and no algebraic solutions
    eqn = (diff(f(x)) - x) * (diff(f(x)) + x)
    sol = [Eq(f(x), C1 - x**2 / 2), Eq(f(x), C1 + x**2 / 2)]
    assert all(c[0]
               for c in checkodesol(eqn, sol, order=1, solve_for_func=False))
    assert set(sol) == set(dsolve(eqn, f(x), hint='nth_algebraic'))
    assert set(sol) == set(dsolve(eqn, f(x)))

    eqn = f(x) + f(x) * f(x).diff(x)
    solns = [Eq(f(x), 0), Eq(f(x), C1 - x)]
    assert all(c[0]
               for c in checkodesol(eqn, solns, order=1, solve_for_func=False))
    assert set(solns) == set(dsolve(eqn, f(x)))

    solns = [Eq(f(x), exp(x)), Eq(f(x), C1 * exp(C2 * x))]
    solns_final = _remove_redundant_solutions(eqn, solns, 2, x)
    assert solns_final == [Eq(f(x), C1 * exp(C2 * x))]

    # This one needs a substitution f' = g.
    eqn = -exp(x) + (x * Derivative(f(x), (x, 2)) + Derivative(f(x), x)) / x
    sol = Eq(f(x), C1 + C2 * log(x) + exp(x) - Ei(x))
    assert checkodesol(eqn, sol, order=2, solve_for_func=False)[0]
    assert sol == dsolve(eqn, f(x))
Пример #4
0
def test_2nd_power_series_regular():
    C1, C2 = symbols("C1 C2")
    eq = x**2 * (f(x).diff(x, 2)) - 3 * x * (f(x).diff(x)) + (4 * x + 4) * f(x)
    sol = Eq(f(x),
             C1 * x**2 * (-16 * x**3 / 9 + 4 * x**2 - 4 * x + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = 4 * x**2 * (f(x).diff(
        x, 2)) - 8 * x**2 * (f(x).diff(x)) + (4 * x**2 + 1) * f(x)
    sol = Eq(
        f(x),
        C1 * sqrt(x) * (x**4 / 24 + x**3 / 6 + x**2 / 2 + x + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x**2 * (f(x).diff(x, 2)) - x**2 * (f(x).diff(x)) + (x**2 - 2) * f(x)
    sol = Eq(
        f(x),
        C1 *
        (-x**6 / 720 - 3 * x**5 / 80 - x**4 / 8 + x**2 / 2 + x / 2 + 1) / x +
        C2 * x**2 * (-x**3 / 60 + x**2 / 20 + x / 2 + 1) + O(x**6))
    assert dsolve(eq) == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x**2 * (f(x).diff(
        x, 2)) + x * (f(x).diff(x)) + (x**2 - Rational(1, 4)) * f(x)
    sol = Eq(
        f(x),
        C1 * (x**4 / 24 - x**2 / 2 + 1) / sqrt(x) + C2 * sqrt(x) *
        (x**4 / 120 - x**2 / 6 + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)
Пример #5
0
def test_2nd_power_series_ordinary():
    C1, C2 = symbols("C1 C2")

    eq = f(x).diff(x, 2) - x * f(x)
    assert classify_ode(eq) == ('2nd_linear_airy', '2nd_power_series_ordinary')
    sol = Eq(f(x), C2 * (x**3 / 6 + 1) + C1 * x * (x**3 / 12 + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_ordinary') == sol
    assert checkodesol(eq, sol) == (True, 0)

    sol = Eq(
        f(x),
        C2 * ((x + 2)**4 / 6 + (x + 2)**3 / 6 - (x + 2)**2 + 1) + C1 *
        (x + (x + 2)**4 / 12 - (x + 2)**3 / 3 + S(2)) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_ordinary', x0=-2) == sol
    # FIXME: Solution should be O((x+2)**6)
    # assert checkodesol(eq, sol) == (True, 0)

    sol = Eq(f(x), C2 * x + C1 + O(x**2))
    assert dsolve(eq, hint='2nd_power_series_ordinary', n=2) == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = (1 + x**2) * (f(x).diff(x, 2)) + 2 * x * (f(x).diff(x)) - 2 * f(x)
    assert classify_ode(eq) == ('2nd_hypergeometric',
                                '2nd_hypergeometric_Integral',
                                '2nd_power_series_ordinary')

    sol = Eq(f(x), C2 * (-x**4 / 3 + x**2 + 1) + C1 * x + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_ordinary') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x, 2) + x * (f(x).diff(x)) + f(x)
    assert classify_ode(eq) == ('2nd_power_series_ordinary', )
    sol = Eq(
        f(x),
        C2 * (x**4 / 8 - x**2 / 2 + 1) + C1 * x * (-x**2 / 3 + 1) + O(x**6))
    assert dsolve(eq) == sol
    # FIXME: checkodesol fails for this solution...
    # assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x, 2) + f(x).diff(x) - x * f(x)
    assert classify_ode(eq) == ('2nd_power_series_ordinary', )
    sol = Eq(
        f(x),
        C2 * (-x**4 / 24 + x**3 / 6 + 1) + C1 * x *
        (x**3 / 24 + x**2 / 6 - x / 2 + 1) + O(x**6))
    assert dsolve(eq) == sol
    # FIXME: checkodesol fails for this solution...
    # assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x, 2) + x * f(x)
    assert classify_ode(eq) == ('2nd_linear_airy', '2nd_power_series_ordinary')
    sol = Eq(
        f(x),
        C2 * (x**6 / 180 - x**3 / 6 + 1) + C1 * x * (-x**3 / 12 + 1) + O(x**7))
    assert dsolve(eq, hint='2nd_power_series_ordinary', n=7) == sol
    assert checkodesol(eq, sol) == (True, 0)
Пример #6
0
def test_nth_algebraic():
    eqn = Eq(Derivative(f(x), x), Derivative(g(x), x))
    sol = Eq(f(x), C1 + g(x))
    assert checkodesol(eqn, sol, order=1, solve_for_func=False)[0]
    assert sol == dsolve(eqn, f(x),
                         hint='nth_algebraic'), dsolve(eqn,
                                                       f(x),
                                                       hint='nth_algebraic')
    assert sol == dsolve(eqn, f(x))

    eqn = (diff(f(x)) - x) * (diff(f(x)) + x)
    sol = [Eq(f(x), C1 - x**2 / 2), Eq(f(x), C1 + x**2 / 2)]
    assert checkodesol(eqn, sol, order=1, solve_for_func=False)[0]
    assert set(sol) == set(dsolve(eqn, f(x), hint='nth_algebraic'))
    assert set(sol) == set(dsolve(eqn, f(x)))

    eqn = (1 - sin(f(x))) * f(x).diff(x)
    sol = Eq(f(x), C1)
    assert checkodesol(eqn, sol, order=1, solve_for_func=False)[0]
    assert sol == dsolve(eqn, f(x), hint='nth_algebraic')
    assert sol == dsolve(eqn, f(x))

    M, m, r, t = symbols('M m r t')
    phi = Function('phi')
    eqn = Eq(-M * phi(t).diff(t),
             Rational(3, 2) * m * r**2 * phi(t).diff(t) * phi(t).diff(t, t))
    solns = [
        Eq(phi(t), C1),
        Eq(phi(t), C1 + C2 * t - M * t**2 / (3 * m * r**2))
    ]
    assert checkodesol(eqn, solns[0], order=2, solve_for_func=False)[0]
    assert checkodesol(eqn, solns[1], order=2, solve_for_func=False)[0]
    assert set(solns) == set(dsolve(eqn, phi(t), hint='nth_algebraic'))
    assert set(solns) == set(dsolve(eqn, phi(t)))

    eqn = f(x) * f(x).diff(x) * f(x).diff(x, x)
    sol = Eq(f(x), C1 + C2 * x)
    assert checkodesol(eqn, sol, order=1, solve_for_func=False)[0]
    assert sol == dsolve(eqn, f(x), hint='nth_algebraic')
    assert sol == dsolve(eqn, f(x))

    eqn = f(x) * f(x).diff(x) * f(x).diff(x, x) * (f(x) - 1)
    sol = Eq(f(x), C1 + C2 * x)
    assert checkodesol(eqn, sol, order=1, solve_for_func=False)[0]
    assert sol == dsolve(eqn, f(x), hint='nth_algebraic')
    assert sol == dsolve(eqn, f(x))

    eqn = f(x) * f(x).diff(x) * f(x).diff(x,
                                          x) * (f(x) - 1) * (f(x).diff(x) - x)
    solns = [Eq(f(x), C1 + x**2 / 2), Eq(f(x), C1 + C2 * x)]
    assert checkodesol(eqn, solns[0], order=2, solve_for_func=False)[0]
    assert checkodesol(eqn, solns[1], order=2, solve_for_func=False)[0]
    assert set(solns) == set(dsolve(eqn, f(x), hint='nth_algebraic'))
    assert set(solns) == set(dsolve(eqn, f(x)))
Пример #7
0
def test_series():
    C1 = Symbol("C1")
    eq = f(x).diff(x) - f(x)
    sol = Eq(f(x), C1 + C1*x + C1*x**2/2 + C1*x**3/6 + C1*x**4/24 +
            C1*x**5/120 + O(x**6))
    assert dsolve(eq, hint='1st_power_series') == sol
    assert checkodesol(eq, sol, order=1)[0]

    eq = f(x).diff(x) - x*f(x)
    sol = Eq(f(x), C1*x**4/8 + C1*x**2/2 + C1 + O(x**6))
    assert dsolve(eq, hint='1st_power_series') == sol
    assert checkodesol(eq, sol, order=1)[0]

    eq = f(x).diff(x) - sin(x*f(x))
    sol = Eq(f(x), (x - 2)**2*(1+ sin(4))*cos(4) + (x - 2)*sin(4) + 2 + O(x**3))
    assert dsolve(eq, hint='1st_power_series', ics={f(2): 2}, n=3) == sol
Пример #8
0
def test_user_infinitesimals():
    x = Symbol("x")  # assuming x is real generates an error
    eq = x * (f(x).diff(x)) + 1 - f(x)**2
    sol = Eq(f(x), (C1 + x**2) / (C1 - x**2))
    infinitesimals = {'xi': sqrt(f(x) - 1) / sqrt(f(x) + 1), 'eta': 0}
    assert dsolve(eq, hint='lie_group', **infinitesimals) == sol
    assert checkodesol(eq, sol) == (True, 0)
Пример #9
0
def test_Bernoulli():
    # Type: Bernoulli, f'(x) + p(x)*f(x) == q(x)*f(x)**n
    eq = Eq(x*f(x).diff(x) + f(x) - f(x)**2, 0)
    sol = dsolve(eq, f(x), hint='Bernoulli')
    assert sol == Eq(f(x), 1/(C1*x + 1))
    assert checkodesol(eq, sol, order=1, solve_for_func=False)[0]

    eq = f(x).diff(x) - y*f(x)
    sol = dsolve(eq, hint='Bernoulli')
    assert sol == Eq(f(x), C1*exp(x*y))
    assert checkodesol(eq, sol)[0]

    eq = f(x)*f(x).diff(x) - 1
    sol = dsolve(eq,hint='Bernoulli')
    assert sol == [Eq(f(x), -sqrt(C1 + 2*x)), Eq(f(x), sqrt(C1 + 2*x))]
    assert checkodesol(eq, sol) == [(True, 0), (True, 0)]
Пример #10
0
def test_eval(kamkenumber):
    print("kamkenumber = ",kamkenumber)
    odesol = dsolve(kamke1_1[kamkenumber],y)
    print("odesol = ",odesol)
    #expected = solution_kamke1[kamkenumber]
    #print("odeexpected = ",expected)
    
    #assert odesol == expected
    assert checkodesol(kamke1_1[kamkenumber],odesol)[0]
Пример #11
0
def test_issue_13060():
    A, B = symbols("A B", cls=Function)
    t = Symbol("t")
    eq = [
        Eq(Derivative(A(t), t),
           A(t) * B(t)),
        Eq(Derivative(B(t), t),
           A(t) * B(t))
    ]
    sol = dsolve(eq)
    assert checkodesol(eq, sol) == (True, [0, 0])
Пример #12
0
def find_riccati_ode(ratfunc, x, yf):
    y = ratfunc
    yp = y.diff(x)
    q1 = rand_rational_function(x, 1, 3)
    q2 = rand_rational_function(x, 1, 3)
    while q2 == 0:
        q2 = rand_rational_function(x, 1, 3)
    q0 = ratsimp(yp - q1 * y - q2 * y**2)
    eq = Eq(yf.diff(), q0 + q1 * yf + q2 * yf**2)
    sol = Eq(yf, y)
    assert checkodesol(eq, sol) == (True, 0)
    return eq, q0, q1, q2
Пример #13
0
def _test_all_examples_for_one_hint(our_hint, all_examples=[], runxfail=None):
    if all_examples == []:
        all_examples = _get_all_examples()
    match_list, unsolve_list, exception_list = [], [], []
    for ode_example in all_examples:
        eq = ode_example['eq']
        expected_sol = ode_example['sol']
        example = ode_example['example_name']
        xfail = our_hint in ode_example['XFAIL']
        xpass = True
        if runxfail and not xfail:
            continue
        if our_hint in classify_ode(eq):
            match_list.append(example)
            try:
                dsolve_sol = dsolve(eq, hint=our_hint)
                expected_checkodesol = [(True, 0)
                                        for i in range(len(expected_sol))]
                if len(expected_sol) == 1:
                    expected_checkodesol = (True, 0)

                if checkodesol(eq, dsolve_sol) != expected_checkodesol:
                    unsolve_list.append(example)
                    message = dsol_incorrect_msg.format(hint=our_hint,
                                                        eq=eq,
                                                        sol=expected_sol,
                                                        dsolve_sol=dsolve_sol)
                    if runxfail is not None:
                        raise AssertionError(message)
            except Exception as e:
                exception_list.append(example)
                if runxfail is not None:
                    print(
                        exception_msg.format(e=str(e),
                                             hint=our_hint,
                                             example=example,
                                             eq=eq))
                    traceback.print_exc()
                xpass = False
        if xpass and xfail:
            print(example, "is now passing for the hint", our_hint)
    if runxfail is None:
        match_count = len(match_list)
        solved = len(match_list) - len(unsolve_list) - len(exception_list)
        msg = check_hint_msg.format(hint=our_hint,
                                    matched=match_count,
                                    solve=solved,
                                    unsolve=unsolve_list,
                                    exceptions=exception_list)
        print(msg)
Пример #14
0
def check_dummy_sol(eq, solse, dummy_sym):
    """
    Helper function to check if actual solution
    matches expected solution if actual solution
    contains dummy symbols.
    """
    if isinstance(eq, Eq):
        eq = eq.lhs - eq.rhs
    _, funcs = match_riccati(eq, f, x)

    sols = solve_riccati(f(x), x, *funcs)
    C1 = Dummy('C1')
    sols = [sol.subs(C1, dummy_sym) for sol in sols]

    assert all([x[0] for x in checkodesol(eq, sols)])
    assert all([s1.dummy_eq(s2, dummy_sym) for s1, s2 in zip(sols, solse)])
Пример #15
0
def _ode_solver_test(ode_examples):
    our_hint = ode_examples['hint']
    for example in ode_examples['examples']:
        eq = ode_examples['examples'][example]['eq']
        sol = ode_examples['examples'][example]['sol']
        if our_hint not in classify_ode(eq):
            message = hint_message.format(example=example,
                                          eq=eq,
                                          our_hint=our_hint)
            raise AssertionError(message)

        dsolve_sol = dsolve(eq, hint=our_hint)
        if dsolve_sol not in sol:
            message = expected_sol_message.format(example=example,
                                                  eq=eq,
                                                  sol=sol,
                                                  dsolve_sol=dsolve_sol)
            raise AssertionError(message)

        expected_checkodesol = [(True, 0) for i in range(len(sol))]
        if checkodesol(eq, sol) != expected_checkodesol:
            message = checkodesol.format(example=example, eq=eq)
            raise AssertionError(message)
Пример #16
0
def test_2nd_power_series_regular():
    C1, C2, a = symbols("C1 C2 a")
    eq = x**2 * (f(x).diff(x, 2)) - 3 * x * (f(x).diff(x)) + (4 * x + 4) * f(x)
    sol = Eq(f(x),
             C1 * x**2 * (-16 * x**3 / 9 + 4 * x**2 - 4 * x + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = 4 * x**2 * (f(x).diff(
        x, 2)) - 8 * x**2 * (f(x).diff(x)) + (4 * x**2 + 1) * f(x)
    sol = Eq(
        f(x),
        C1 * sqrt(x) * (x**4 / 24 + x**3 / 6 + x**2 / 2 + x + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x**2 * (f(x).diff(x, 2)) - x**2 * (f(x).diff(x)) + (x**2 - 2) * f(x)
    sol = Eq(
        f(x),
        C1 *
        (-x**6 / 720 - 3 * x**5 / 80 - x**4 / 8 + x**2 / 2 + x / 2 + 1) / x +
        C2 * x**2 * (-x**3 / 60 + x**2 / 20 + x / 2 + 1) + O(x**6))
    assert dsolve(eq) == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x**2 * (f(x).diff(
        x, 2)) + x * (f(x).diff(x)) + (x**2 - Rational(1, 4)) * f(x)
    sol = Eq(
        f(x),
        C1 * (x**4 / 24 - x**2 / 2 + 1) / sqrt(x) + C2 * sqrt(x) *
        (x**4 / 120 - x**2 / 6 + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x * f(x).diff(x, 2) + f(x).diff(x) - a * x * f(x)
    sol = Eq(f(x), C1 * (a**2 * x**4 / 64 + a * x**2 / 4 + 1) + O(x**6))
    assert dsolve(eq, f(x), hint="2nd_power_series_regular") == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x, 2) + ((1 - x) / x) * f(x).diff(x) + (a / x) * f(x)
    sol = Eq(f(x), C1*(-a*x**5*(a - 4)*(a - 3)*(a - 2)*(a - 1)/14400 + \
        a*x**4*(a - 3)*(a - 2)*(a - 1)/576 - a*x**3*(a - 2)*(a - 1)/36 + \
        a*x**2*(a - 1)/4 - a*x + 1) + O(x**6))
    assert dsolve(eq, f(x), hint="2nd_power_series_regular") == sol
    assert checkodesol(eq, sol) == (True, 0)
Пример #17
0
def test_nth_algebraic_prep2():
    eqn = Eq(Derivative(x * Derivative(f(x), x), x) / x, exp(x))
    sol = Eq(f(x), C1 + C2 * log(x) + exp(x) - Ei(x))
    assert checkodesol(eqn, sol, order=2, solve_for_func=False)[0]
    assert sol == dsolve(eqn, f(x), prep=True, hint='nth_algebraic')
    assert sol == dsolve(eqn, f(x))
Пример #18
0
def test_nth_algebraic_prep1():
    eqn = Derivative(x * f(x), x, x, x)
    sol = Eq(f(x), (C1 + C2 * x + C3 * x**2) / x)
    assert checkodesol(eqn, sol, order=3, solve_for_func=False)[0]
    assert sol == dsolve(eqn, f(x), prep=True, hint='nth_algebraic')
    assert sol == dsolve(eqn, f(x))
Пример #19
0
def test_nth_algebraic_issue15999():
    eqn = f(x).diff(x) - C1
    sol = Eq(f(x), C1 * x + C2)  # Correct solution
    assert checkodesol(eqn, sol, order=1, solve_for_func=False) == (True, 0)
    assert dsolve(eqn, f(x), hint='nth_algebraic') == sol
    assert dsolve(eqn, f(x)) == sol
Пример #20
0
def test_checkodesol():
    # For the most part, checkodesol is well tested in the tests below.
    # These tests only handle cases not checked below.
    raises(ValueError, lambda: checkodesol(f(x, y).diff(x), Eq(f(x, y), x)))
    raises(ValueError,
           lambda: checkodesol(f(x).diff(x), Eq(f(x, y), x), f(x, y)))
    assert checkodesol(f(x).diff(x), Eq(f(x, y), x)) == \
        (False, -f(x).diff(x) + f(x, y).diff(x) - 1)
    assert checkodesol(f(x).diff(x), Eq(f(x), x)) is not True
    assert checkodesol(f(x).diff(x), Eq(f(x), x)) == (False, 1)
    sol1 = Eq(f(x)**5 + 11 * f(x) - 2 * f(x) + x, 0)
    assert checkodesol(diff(sol1.lhs, x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x) * exp(f(x)), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 2), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 2) * exp(f(x)), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 3), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 3) * exp(f(x)), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 3), Eq(f(x), x*log(x))) == \
        (False, 60*x**4*((log(x) + 1)**2 + log(x))*(
        log(x) + 1)*log(x)**2 - 5*x**4*log(x)**4 - 9)
    assert checkodesol(diff(exp(f(x)) + x, x)*x, Eq(exp(f(x)) + x, 0)) == \
        (True, 0)
    assert checkodesol(diff(exp(f(x)) + x, x) * x,
                       Eq(exp(f(x)) + x, 0),
                       solve_for_func=False) == (True, 0)
    assert checkodesol(f(x).diff(x, 2), [Eq(f(x), C1 + C2*x),
        Eq(f(x), C2 + C1*x), Eq(f(x), C1*x + C2*x**2)]) == \
        [(True, 0), (True, 0), (False, C2)]
    assert checkodesol(f(x).diff(x, 2), set([Eq(f(x), C1 + C2*x),
        Eq(f(x), C2 + C1*x), Eq(f(x), C1*x + C2*x**2)])) == \
        set([(True, 0), (True, 0), (False, C2)])
    assert checkodesol(f(x).diff(x) - 1/f(x)/2, Eq(f(x)**2, x)) == \
        [(True, 0), (True, 0)]
    assert checkodesol(f(x).diff(x) - f(x), Eq(C1 * exp(x), f(x))) == (True, 0)
    # Based on test_1st_homogeneous_coeff_ode2_eq3sol.  Make sure that
    # checkodesol tries back substituting f(x) when it can.
    eq3 = x * exp(f(x) / x) + f(x) - x * f(x).diff(x)
    sol3 = Eq(f(x), log(log(C1 / x)**(-x)))
    assert not checkodesol(eq3, sol3)[1].has(f(x))
    # This case was failing intermittently depending on hash-seed:
    eqn = Eq(Derivative(x * Derivative(f(x), x), x) / x, exp(x))
    sol = Eq(f(x), C1 + C2 * log(x) + exp(x) - Ei(x))
    assert checkodesol(eqn, sol, order=2, solve_for_func=False)[0]
    eq = x**2 * (f(x).diff(x, 2)) + x * (f(x).diff(x)) + (2 * x**2 + 25) * f(x)
    sol = Eq(
        f(x),
        C1 * besselj(5 * I,
                     sqrt(2) * x) + C2 * bessely(5 * I,
                                                 sqrt(2) * x))
    assert checkodesol(eq, sol) == (True, 0)

    eqs = [Eq(f(x).diff(x), f(x) + g(x)), Eq(g(x).diff(x), f(x) + g(x))]
    sol = [Eq(f(x), -C1 + C2 * exp(2 * x)), Eq(g(x), C1 + C2 * exp(2 * x))]
    assert checkodesol(eqs, sol) == (True, [0, 0])
Пример #21
0
def _test_particular_example(our_hint,
                             ode_example=None,
                             solver_flag=False,
                             example_name=None):
    if example_name is not None:
        all_examples = _get_all_examples()
        for example in all_examples:
            if example['example_name'] == example_name:
                eq = example['eq']
                dsolve(eq, hint=our_hint)
    else:
        eq = ode_example['eq']
        expected_sol = ode_example['sol']
        example = ode_example['example_name']
        xfail = our_hint in ode_example['XFAIL']
        func = ode_example['func']
        result = {'msg': '', 'xpass_msg': ''}
        xpass = True
        if solver_flag:
            if our_hint not in classify_ode(eq, func):
                message = hint_message.format(example=example,
                                              eq=eq,
                                              our_hint=our_hint)
                raise AssertionError(message)

        if our_hint in classify_ode(eq, func):
            result['match_list'] = example
            try:
                dsolve_sol = dsolve(eq, func, hint=our_hint)
                if solver_flag:
                    expect_sol_check = False
                    if type(dsolve_sol) == list:
                        if any(sub_sol not in dsolve_sol
                               for sub_sol in expected_sol):
                            expect_sol_check = True
                    else:
                        expect_sol_check = dsolve_sol not in expected_sol
                    if expect_sol_check:
                        message = expected_sol_message.format(
                            example=example,
                            eq=eq,
                            sol=expected_sol,
                            dsolve_sol=dsolve_sol)
                        raise AssertionError(message)

                expected_checkodesol = [(True, 0)
                                        for i in range(len(expected_sol))]
                if len(expected_sol) == 1:
                    expected_checkodesol = (True, 0)

                if checkodesol(eq, dsolve_sol) != expected_checkodesol:
                    result['unsolve_list'] = example
                    xpass = False
                    message = dsol_incorrect_msg.format(hint=our_hint,
                                                        eq=eq,
                                                        sol=expected_sol,
                                                        dsolve_sol=dsolve_sol)
                    if solver_flag:
                        message = checkodesol.format(example=example, eq=eq)
                        raise AssertionError(message)
                    else:
                        result['msg'] = 'AssertionError: ' + message
            except Exception as e:
                result['exception_list'] = example
                if not solver_flag:
                    traceback.print_exc()
                    result['msg'] = exception_msg.format(e=str(e),
                                                         hint=our_hint,
                                                         example=example,
                                                         eq=eq)
                xpass = False
        if xpass and xfail:
            result[
                'xpass_msg'] = example + "is now passing for the hint" + our_hint
        return result
Пример #22
0
def test_Riccati_special_minus2():
    # Type: Riccati special alpha = -2, a*dy/dx + b*y**2 + c*y/x +d/x**2
    eq = 2 * f(x).diff(x) + f(x)**2 - f(x) / x + 3 * x**(-2)
    sol = dsolve(eq, f(x), hint='Riccati_special_minus2')
    assert checkodesol(eq, sol, order=1, solve_for_func=False)[0]
Пример #23
0
def test_Bernoulli():
    # Type: Bernoulli, f'(x) + p(x)*f(x) == q(x)*f(x)**n
    eq = Eq(x * f(x).diff(x) + f(x) - f(x)**2, 0)
    sol = dsolve(eq, f(x), hint='Bernoulli')
    assert sol == Eq(f(x), 1 / (x * (C1 + 1 / x)))
    assert checkodesol(eq, sol, order=1, solve_for_func=False)[0]
Пример #24
0
def test_factorable():

    eq = f(x) + f(x) * f(x).diff(x)
    sols = [Eq(f(x), C1 - x), Eq(f(x), 0)]
    assert set(sols) == set(dsolve(eq, f(x), hint='factorable'))
    assert checkodesol(eq, sols) == 2 * [(True, 0)]

    eq = f(x) * (f(x).diff(x) + f(x) * x + 2)
    sols = [
        Eq(f(x),
           (C1 - sqrt(2) * sqrt(pi) * erfi(sqrt(2) * x / 2)) * exp(-x**2 / 2)),
        Eq(f(x), 0)
    ]
    assert set(sols) == set(dsolve(eq, f(x), hint='factorable'))
    assert checkodesol(eq, sols) == 2 * [(True, 0)]

    eq = (f(x).diff(x) + f(x) * x**2) * (f(x).diff(x, 2) + x * f(x))
    sols = [
        Eq(f(x),
           C1 * airyai(-x) + C2 * airybi(-x)),
        Eq(f(x), C1 * exp(-x**3 / 3))
    ]
    assert set(sols) == set(dsolve(eq, f(x), hint='factorable'))
    assert checkodesol(eq, sols[1]) == (True, 0)

    eq = (f(x).diff(x) + f(x) * x**2) * (f(x).diff(x, 2) + f(x))
    sols = [Eq(f(x), C1 * exp(-x**3 / 3)), Eq(f(x), C1 * sin(x) + C2 * cos(x))]
    assert set(sols) == set(dsolve(eq, f(x), hint='factorable'))
    assert checkodesol(eq, sols) == 2 * [(True, 0)]

    eq = (f(x).diff(x)**2 - 1) * (f(x).diff(x)**2 - 4)
    sols = [
        Eq(f(x), C1 - x),
        Eq(f(x), C1 + x),
        Eq(f(x), C1 + 2 * x),
        Eq(f(x), C1 - 2 * x)
    ]
    assert set(sols) == set(dsolve(eq, f(x), hint='factorable'))
    assert checkodesol(eq, sols) == 4 * [(True, 0)]

    eq = (f(x).diff(x, 2) - exp(f(x))) * f(x).diff(x)
    sol = Eq(f(x), C1)
    assert sol == dsolve(eq, f(x), hint='factorable')
    assert checkodesol(eq, sol) == (True, 0)

    eq = (f(x).diff(x)**2 - 1) * (f(x) * f(x).diff(x) - 1)
    sol = [
        Eq(f(x), C1 - x),
        Eq(f(x), -sqrt(C1 + 2 * x)),
        Eq(f(x), sqrt(C1 + 2 * x)),
        Eq(f(x), C1 + x)
    ]
    assert set(sol) == set(dsolve(eq, f(x), hint='factorable'))
    assert checkodesol(eq, sol) == 4 * [(True, 0)]

    eq = Derivative(f(x), x)**4 - 2 * Derivative(f(x), x)**2 + 1
    sol = [Eq(f(x), C1 - x), Eq(f(x), C1 + x)]
    assert set(sol) == set(dsolve(eq, f(x), hint='factorable'))
    assert checkodesol(eq, sol) == 2 * [(True, 0)]

    eq = f(x)**2 * Derivative(f(x), x)**6 - 2 * f(x)**2 * Derivative(
        f(x), x)**4 + f(x)**2 * Derivative(f(x), x)**2 - 2 * f(x) * Derivative(
            f(x), x)**5 + 4 * f(x) * Derivative(
                f(x), x)**3 - 2 * f(x) * Derivative(f(x), x) + Derivative(
                    f(x), x)**4 - 2 * Derivative(f(x), x)**2 + 1
    sol = [
        Eq(f(x), C1 - x),
        Eq(f(x), -sqrt(C1 + 2 * x)),
        Eq(f(x), sqrt(C1 + 2 * x)),
        Eq(f(x), C1 + x)
    ]
    assert set(sol) == set(dsolve(eq, f(x), hint='factorable'))
    assert checkodesol(eq, sol) == 4 * [(True, 0)]

    eq = (f(x).diff(x, 2) - exp(f(x))) * (f(x).diff(x, 2) + exp(f(x)))
    raises(NotImplementedError, lambda: dsolve(eq, hint='factorable'))

    eq = x**4 * f(x)**2 + 2 * x**4 * f(x) * Derivative(
        f(x), (x, 2)
    ) + x**4 * Derivative(f(x), (x, 2))**2 + 2 * x**3 * f(x) * Derivative(
        f(x), x) + 2 * x**3 * Derivative(f(x), x) * Derivative(
            f(x), (x, 2)) - 7 * x**2 * f(x)**2 - 7 * x**2 * f(x) * Derivative(
                f(x), (x, 2)) + x**2 * Derivative(f(x), x)**2 - 7 * x * f(
                    x) * Derivative(f(x), x) + 12 * f(x)**2

    sol = [
        Eq(f(x),
           C1 * besselj(2, x) + C2 * bessely(2, x)),
        Eq(f(x),
           C1 * besselj(sqrt(3), x) + C2 * bessely(sqrt(3), x))
    ]

    assert set(sol) == set(dsolve(eq, f(x), hint='factorable'))
    assert checkodesol(eq, sol) == 2 * [(True, 0)]
Пример #25
0
def _test_particular_example(our_hint, ode_example, solver_flag=False):
    eq = ode_example['eq']
    expected_sol = ode_example['sol']
    example = ode_example['example_name']
    xfail = our_hint in ode_example['XFAIL']
    func = ode_example['func']
    result = {'msg': '', 'xpass_msg': ''}
    checkodesol_XFAIL = ode_example['checkodesol_XFAIL']
    xpass = True
    if solver_flag:
        if our_hint not in classify_ode(eq, func):
            message = hint_message.format(example=example, eq=eq, our_hint=our_hint)
            raise AssertionError(message)

    if our_hint in classify_ode(eq, func):
        result['match_list'] = example
        try:
            dsolve_sol = dsolve(eq, func, hint=our_hint)

        except Exception as e:
            dsolve_sol = []
            result['exception_list'] = example
            if not solver_flag:
                traceback.print_exc()
                result['msg'] = exception_msg.format(e=str(e), hint=our_hint, example=example, eq=eq)
            xpass = False

        if solver_flag and dsolve_sol!=[]:
            expect_sol_check = False
            if type(dsolve_sol)==list:
                for sub_sol in expected_sol:
                    if sub_sol.has(Dummy):
                        expect_sol_check = not _test_dummy_sol(sub_sol, dsolve_sol)
                    else:
                        expect_sol_check = sub_sol not in dsolve_sol
                    if expect_sol_check:
                        break
            else:
                expect_sol_check = dsolve_sol not in expected_sol
                for sub_sol in expected_sol:
                    if sub_sol.has(Dummy):
                        expect_sol_check = not _test_dummy_sol(sub_sol, dsolve_sol)

            if expect_sol_check:
                message = expected_sol_message.format(example=example, eq=eq, sol=expected_sol, dsolve_sol=dsolve_sol)
                raise AssertionError(message)

            expected_checkodesol = [(True, 0) for i in range(len(expected_sol))]
            if len(expected_sol) == 1:
                expected_checkodesol = (True, 0)

            if not checkodesol_XFAIL:
                if checkodesol(eq, dsolve_sol, solve_for_func=False) != expected_checkodesol:
                    result['unsolve_list'] = example
                    xpass = False
                    message = dsol_incorrect_msg.format(hint=our_hint, eq=eq, sol=expected_sol,dsolve_sol=dsolve_sol)
                    if solver_flag:
                        message = checkodesol_msg.format(example=example, eq=eq)
                        raise AssertionError(message)
                    else:
                        result['msg'] = 'AssertionError: ' + message

        if xpass and xfail:
            result['xpass_msg'] = example + "is now passing for the hint" + our_hint
    return result