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])
def _print_jn(self, expr): from sympy.functions import sqrt, besselj x = expr.argument expr2 = sqrt(S.Pi/(2*x))*besselj(expr.order + S.Half, x) return self._print(expr2)
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)]
def _get_examples_ode_sol_factorable(): """ some hints are marked as xfail for examples because they missed additional algebraic solution which could be found by Factorable hint. Fact_01 raise exception for nth_linear_constant_coeff_undetermined_coefficients""" return { 'hint': "factorable", 'func': f(x), 'examples': { 'fact_01': { 'eq': f(x) + f(x) * f(x).diff(x), 'sol': [Eq(f(x), 0), Eq(f(x), C1 - x)], 'XFAIL': [ 'separable', '1st_exact', '1st_linear', 'Bernoulli', '1st_homogeneous_coeff_best', '1st_homogeneous_coeff_subs_indep_div_dep', '1st_homogeneous_coeff_subs_dep_div_indep', 'lie_group', 'nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients', 'nth_linear_constant_coeff_variation_of_parameters', 'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters', 'nth_linear_constant_coeff_undetermined_coefficients' ] }, 'fact_02': { 'eq': f(x) * (f(x).diff(x) + f(x) * x + 2), 'sol': [ Eq(f(x), (C1 - sqrt(2) * sqrt(pi) * erfi(sqrt(2) * x / 2)) * exp(-x**2 / 2)), Eq(f(x), 0) ], 'XFAIL': ['Bernoulli', '1st_linear', 'lie_group'] }, 'fact_03': { 'eq': (f(x).diff(x) + f(x) * x**2) * (f(x).diff(x, 2) + x * f(x)), 'sol': [ Eq(f(x), C1 * airyai(-x) + C2 * airybi(-x)), Eq(f(x), C1 * exp(-x**3 / 3)) ] }, 'fact_04': { 'eq': (f(x).diff(x) + f(x) * x**2) * (f(x).diff(x, 2) + f(x)), 'sol': [ Eq(f(x), C1 * exp(-x**3 / 3)), Eq(f(x), C1 * sin(x) + C2 * cos(x)) ] }, 'fact_05': { 'eq': (f(x).diff(x)**2 - 1) * (f(x).diff(x)**2 - 4), 'sol': [ Eq(f(x), C1 - x), Eq(f(x), C1 + x), Eq(f(x), C1 + 2 * x), Eq(f(x), C1 - 2 * x) ] }, 'fact_06': { 'eq': (f(x).diff(x, 2) - exp(f(x))) * f(x).diff(x), 'sol': [Eq(f(x), C1)] }, 'fact_07': { '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) ] }, 'fact_08': { 'eq': Derivative(f(x), x)**4 - 2 * Derivative(f(x), x)**2 + 1, 'sol': [Eq(f(x), C1 - x), Eq(f(x), C1 + x)] }, 'fact_09': { '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) ] }, 'fact_10': { '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)) ] }, 'fact_11': { 'eq': (f(x).diff(x, 2) - exp(f(x))) * (f(x).diff(x, 2) + exp(f(x))), 'sol': [], #currently dsolve doesn't return any solution for this example 'XFAIL': ['factorable'] }, } }
def _get_examples_ode_sol_factorable(): """ some hints are marked as xfail for examples because they missed additional algebraic solution which could be found by Factorable hint. Fact_01 raise exception for nth_linear_constant_coeff_undetermined_coefficients""" y = Dummy('y') return { 'hint': "factorable", 'func': f(x), 'examples':{ 'fact_01': { 'eq': f(x) + f(x)*f(x).diff(x), 'sol': [Eq(f(x), 0), Eq(f(x), C1 - x)], 'XFAIL': ['separable', '1st_exact', '1st_linear', 'Bernoulli', '1st_homogeneous_coeff_best', '1st_homogeneous_coeff_subs_indep_div_dep', '1st_homogeneous_coeff_subs_dep_div_indep', 'lie_group', 'nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients', 'nth_linear_constant_coeff_variation_of_parameters', 'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters', 'nth_linear_constant_coeff_undetermined_coefficients'] }, 'fact_02': { 'eq': f(x)*(f(x).diff(x)+f(x)*x+2), 'sol': [Eq(f(x), (C1 - sqrt(2)*sqrt(pi)*erfi(sqrt(2)*x/2))*exp(-x**2/2)), Eq(f(x), 0)], 'XFAIL': ['Bernoulli', '1st_linear', 'lie_group'] }, 'fact_03': { 'eq': (f(x).diff(x)+f(x)*x**2)*(f(x).diff(x, 2) + x*f(x)), 'sol': [Eq(f(x), C1*airyai(-x) + C2*airybi(-x)),Eq(f(x), C1*exp(-x**3/3))] }, 'fact_04': { 'eq': (f(x).diff(x)+f(x)*x**2)*(f(x).diff(x, 2) + f(x)), 'sol': [Eq(f(x), C1*exp(-x**3/3)), Eq(f(x), C1*sin(x) + C2*cos(x))] }, 'fact_05': { 'eq': (f(x).diff(x)**2-1)*(f(x).diff(x)**2-4), 'sol': [Eq(f(x), C1 - x), Eq(f(x), C1 + x), Eq(f(x), C1 + 2*x), Eq(f(x), C1 - 2*x)] }, 'fact_06': { 'eq': (f(x).diff(x, 2)-exp(f(x)))*f(x).diff(x), 'sol': [Eq(f(x), C1)] }, 'fact_07': { '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)] }, 'fact_08': { 'eq': Derivative(f(x), x)**4 - 2*Derivative(f(x), x)**2 + 1, 'sol': [Eq(f(x), C1 - x), Eq(f(x), C1 + x)] }, 'fact_09': { '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)] }, 'fact_10': { '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))] }, 'fact_11': { 'eq': (f(x).diff(x, 2)-exp(f(x)))*(f(x).diff(x, 2)+exp(f(x))), 'sol': [], #currently dsolve doesn't return any solution for this example 'XFAIL': ['factorable'] }, #Below examples were added for the issue: https://github.com/sympy/sympy/issues/15889 'fact_12': { 'eq': exp(f(x).diff(x))-f(x)**2, 'sol': [Eq(NonElementaryIntegral(1/log(y**2), (y, f(x))), C1 + x)], 'XFAIL': ['lie_group'] #It shows not implemented error for lie_group. }, 'fact_13': { 'eq': f(x).diff(x)**2 - f(x)**3, 'sol': [Eq(f(x), 4/(C1**2 - 2*C1*x + x**2))], 'XFAIL': ['lie_group'] #It shows not implemented error for lie_group. }, 'fact_14': { 'eq': f(x).diff(x)**2 - f(x), 'sol': [Eq(f(x), C1**2/4 - C1*x/2 + x**2/4)] }, 'fact_15': { 'eq': f(x).diff(x)**2 - f(x)**2, 'sol': [Eq(f(x), C1*exp(x)), Eq(f(x), C1*exp(-x))] }, 'fact_16': { 'eq': f(x).diff(x)**2 - f(x)**3, 'sol': [Eq(f(x), 4/(C1**2 - 2*C1*x + x**2))] }, } }