<commit_before>from sympy import (Function, dsolve, Symbol, sin, cos, sinh, acos, tan, cosh,
    I, exp, log, simplify, together, powsimp, fraction, radsimp, Eq, sqrt, pi,
    erf,  diff, Rational, asinh, trigsimp, S, RootOf, Poly, Integral, atan,
    Equality, solve, O, LambertW, Dummy)
from sympy.abc import x, y, z
from sympy.core.compatibility import all
from sympy.solvers.ode import ode_order, homogeneous_order, \
    _undetermined_coefficients_match, classify_ode, checkodesol, constant_renumber
from sympy.utilities.pytest import XFAIL, skip, raises

C1 = Symbol('C1')
C2 = Symbol('C2')
C3 = Symbol('C3')
C4 = Symbol('C4')
C5 = Symbol('C5')
C6 = Symbol('C6')
C7 = Symbol('C7')
C8 = Symbol('C8')
C9 = Symbol('C9')
C10 = Symbol('C10')
f = Function('f')
g = Function('g')

# Note that if the ODE solver, the integral engine, solve(), or even simplify(),
# changes, these tests could fail but still be correct, only written differently.
# Also not that in differently formatted solutions, the arbitrary constants
# might not be equal.  Using specific hints in tests can help avoid this.

# Tests of order higher than 1 should run the solutions through constant_renumber
# because it will normalize it (constant_renumber causes dsolve() to return different
# results on different machines)
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, "checkodesol(f(x).diff(x), f(x), x)")
    raises(ValueError, "checkodesol(f(x).diff(x), f(x, y), Eq(f(x), x))")
    assert checkodesol(f(x).diff(x), f(x), Eq(f(x), x)) is not True
    assert checkodesol(f(x).diff(x), f(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), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x)*exp(f(x)), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 2), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 2)*exp(f(x)), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 3), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 3)*exp(f(x)), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 3), f(x), Eq(f(x), x*log(x))) == \
        (False, -9 + 60*x**4*log(x)**2 + 240*x**4*log(x)**3 +
        235*x**4*log(x)**4 + 60*x**4*log(x)**5)
    assert checkodesol(diff(exp(f(x)) + x, x)*x, f(x), Eq(exp(f(x)) + x)) == (True, 0)
    assert checkodesol(diff(exp(f(x)) + x, x)*x, f(x), Eq(exp(f(x)) + x), \
        solve_for_func=False) == (True, 0)
    assert checkodesol(f(x).diff(x, 2), f(x), [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, 2*C2)]
    assert checkodesol(f(x).diff(x, 2), f(x), 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, 2*C2)])
    assert checkodesol(f(x).diff(x) - 1/f(x)/2, f(x), Eq(f(x)**2, x)) == \
        [(True, 0), (True, 0)]
    assert checkodesol(f(x).diff(x) - f(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, f(x), sol3)[1].has(f(x))

def test_dsolve_options():
    eq = x*f(x).diff(x) + f(x)
    a = dsolve(eq, f(x), hint='all')
    b = dsolve(eq, f(x), hint='all', simplify=False)
    c = dsolve(eq, f(x), 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', 'best', 'best_hint', 'default', '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',
    'best', 'best_hint', 'default', '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, f(x), 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, f(x), 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, "dsolve(eq, f(x), 'notarealhint')")
    raises(ValueError, "dsolve(eq, f(x), 'Liouville')")
    assert dsolve(f(x).diff(x) - 1/f(x)**2, f(x), 'all')['best'] == \
        dsolve(f(x).diff(x) - 1/f(x)**2, f(x), 'best')


def test_classify_ode():
    assert classify_ode(f(x).diff(x, 2), f(x)) == \
        ('nth_linear_constant_coeff_homogeneous', 'Liouville', 'Liouville_Integral')
    assert classify_ode(f(x), f(x)) == ()
    assert classify_ode(Eq(f(x).diff(x), 0), f(x)) == ('separable', '1st_linear', \
        '1st_homogeneous_coeff_best', '1st_homogeneous_coeff_subs_indep_div_dep', \
        '1st_homogeneous_coeff_subs_dep_div_indep', \
        'nth_linear_constant_coeff_homogeneous', 'separable_Integral', \
        '1st_linear_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)) == ()
    # 1650: 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 == b == c != ()
    assert classify_ode(2*x*f(x)*f(x).diff(x) + (1 + x)*f(x)**2 - exp(x), f(x)) ==\
        ('Bernoulli', 'Bernoulli_Integral')
    raises(ValueError, "classify_ode(x + f(x, y).diff(x).diff(y), f(x, y))")
    # 2077
    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',
                                'separable_Integral',
                                '1st_exact_Integral')

def test_ode_order():
    f = Function('f')
    g = Function('g')
    x = Symbol('x')
    assert ode_order(3*x*exp(f(x)), f(x)) == 0
    assert ode_order(x*diff(f(x),x)+3*x*f(x)-sin(x)/x, f(x)) == 1
    assert ode_order(x**2*f(x).diff(x,x)+x*diff(f(x),x)-f(x),f(x)) == 2
    assert ode_order(diff(x*exp(f(x)),x,x), f(x)) == 2
    assert ode_order(diff(x*diff(x*exp(f(x)), x,x), x), f(x)) == 3
    assert ode_order(diff(f(x), x, x), g(x)) == 0
    assert ode_order(diff(f(x), x, x)*diff(g(x), x), f(x)) == 2<commit_msg>Fix trivial mistake in test_ode.<commit_after>from sympy import (Function, dsolve, Symbol, sin, cos, sinh, acos, tan, cosh,
    I, exp, log, simplify, together, powsimp, fraction, radsimp, Eq, sqrt, pi,
    erf,  diff, Rational, asinh, trigsimp, S, RootOf, Poly, Integral, atan,
    Equality, solve, O, LambertW, Dummy)
from sympy.abc import x, y, z
from sympy.core.compatibility import all
from sympy.solvers.ode import ode_order, homogeneous_order, \
    _undetermined_coefficients_match, classify_ode, checkodesol, constant_renumber
from sympy.utilities.pytest import XFAIL, skip, raises

C1 = Symbol('C1')
C2 = Symbol('C2')
C3 = Symbol('C3')
C4 = Symbol('C4')
C5 = Symbol('C5')
C6 = Symbol('C6')
C7 = Symbol('C7')
C8 = Symbol('C8')
C9 = Symbol('C9')
C10 = Symbol('C10')
f = Function('f')
g = Function('g')

# Note that if the ODE solver, the integral engine, solve(), or even simplify(),
# changes, these tests could fail but still be correct, only written differently.
# Also not that in differently formatted solutions, the arbitrary constants
# might not be equal.  Using specific hints in tests can help avoid this.

# Tests of order higher than 1 should run the solutions through constant_renumber
# because it will normalize it (constant_renumber causes dsolve() to return different
# results on different machines)
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, "checkodesol(f(x).diff(x), f(x), x)")
    raises(ValueError, "checkodesol(f(x).diff(x), f(x, y), Eq(f(x), x))")
    assert checkodesol(f(x).diff(x), f(x), Eq(f(x), x)) is not True
    assert checkodesol(f(x).diff(x), f(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), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x)*exp(f(x)), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 2), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 2)*exp(f(x)), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 3), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 3)*exp(f(x)), f(x), sol1) == (True, 0)
    assert checkodesol(diff(sol1.lhs, x, 3), f(x), Eq(f(x), x*log(x))) == \
        (False, -9 + 60*x**4*log(x)**2 + 240*x**4*log(x)**3 +
        235*x**4*log(x)**4 + 60*x**4*log(x)**5)
    assert checkodesol(diff(exp(f(x)) + x, x)*x, f(x), Eq(exp(f(x)) + x)) == (True, 0)
    assert checkodesol(diff(exp(f(x)) + x, x)*x, f(x), Eq(exp(f(x)) + x), \
        solve_for_func=False) == (True, 0)
    assert checkodesol(f(x).diff(x, 2), f(x), [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, 2*C2)]
    assert checkodesol(f(x).diff(x, 2), f(x), 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, 2*C2)])
    assert checkodesol(f(x).diff(x) - 1/f(x)/2, f(x), Eq(f(x)**2, x)) == \
        [(True, 0), (True, 0)]
    assert checkodesol(f(x).diff(x) - f(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, f(x), sol3)[1].has(f(x))

def test_dsolve_options():
    eq = x*f(x).diff(x) + f(x)
    a = dsolve(eq, f(x), hint='all')
    b = dsolve(eq, f(x), hint='all', simplify=False)
    c = dsolve(eq, f(x), 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', 'best', 'best_hint', 'default', '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',
    'best', 'best_hint', 'default', '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, f(x), 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, f(x), 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, "dsolve(eq, f(x), 'notarealhint')")
    raises(ValueError, "dsolve(eq, f(x), 'Liouville')")
    assert dsolve(f(x).diff(x) - 1/f(x)**2, f(x), 'all')['best'] == \
        dsolve(f(x).diff(x) - 1/f(x)**2, f(x), 'best')


def test_classify_ode():
    assert classify_ode(f(x).diff(x, 2), f(x)) == \
        ('nth_linear_constant_coeff_homogeneous', 'Liouville', 'Liouville_Integral')
    assert classify_ode(f(x), f(x)) == ()
    assert classify_ode(Eq(f(x).diff(x), 0), f(x)) == ('separable', '1st_linear', \
        '1st_homogeneous_coeff_best', '1st_homogeneous_coeff_subs_indep_div_dep', \
        '1st_homogeneous_coeff_subs_dep_div_indep', \
        'nth_linear_constant_coeff_homogeneous', 'separable_Integral', \
        '1st_linear_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)) == ()
    # 1650: 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 == b == c != ()
    assert classify_ode(2*x*f(x)*f(x).diff(x) + (1 + x)*f(x)**2 - exp(x), f(x)) ==\
        ('Bernoulli', 'Bernoulli_Integral')
    raises(ValueError, "classify_ode(x + f(x, y).diff(x).diff(y), f(x, y))")
    # 2077
    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',
                                'separable_Integral',
                                '1st_exact_Integral')

def test_ode_order():
    f = Function('f')
    g = Function('g')
    x = Symbol('x')
    assert ode_order(3*x*exp(f(x)), f(x)) == 0
    assert ode_order(x*diff(f(x),x)+3*x*f(x)-sin(x)/x, f(x)) == 1
    assert ode_order(x**2*f(x).diff(x,x)+x*diff(f(x),x)-f(x),f(x)) == 2
    assert ode_order(diff(x*exp(f(x)),x,x), f(x)) == 2
    assert ode_order(diff(x*diff(x*exp(f(x)), x,x), x), f(x)) == 3
    assert ode_order(diff(f(x), x, x), g(x)) == 0
    assert ode_order(diff(f(x), x, x)*diff(g(x), x), f(x)) == 2
{'fraction_non_alphanumeric': 0.19046002376506535, 'fraction_numerical': 0.021841226730040176, 'mean_word_length': 4.118447726614538, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5193706', 'n_tokens_mistral': 7453, 'n_tokens_neox': 7252, 'n_words': 1363}