Repository: sympy/sympy
File: sympy/solvers/ode/single.py (Python)

Code before:


        # Store the matched values
        self.match_dict = {
            'A': pattern_match[A_sym],
            'k': k_val,
        }

        return True

    def _get_general_solution(self, *, simplify_flag: bool = True):
        f = self.ode_problem.func.func
        x = self.ode_problem.sym
        (C1, C2) = self.ode_problem.get_numbered_constants(num=2)

        A = self.match_dict['A']
        k = self.match_dict['k']

        # Special case: k = -2 causes division by zero
        # The equation x^(-2)*y + y'' = 0 is an Euler equation
        # and should be handled differently
        if k == -2:
        # This is x^2*y'' + y = 0 after multiplying by x^2
        # Solution is y = sqrt(x)*(C1*sin(sqrt(3)*log(x)/2) + C2*cos(sqrt(3)*log(x)/2))
        # But we shouldn't handle this case here. Instead, raise an error if hint is provided explicitly.
            raise NotImplementedError("k=-2 case should be handled by nth_linear_euler_eq_homogeneous")

        # For y'' + A*x^k*y = 0
        # The Bessel order and argument are:
        nu = 1 / (k + 2)

        if A == 1 or A == S.One:
            z = (2 / (k + 2)) * x**((k + 2)/2)
        else:
            z = (2 * sqrt(Abs(A)) / (k + 2)) * x**((k + 2)/2)

        # General solution
        solution = sqrt(x) * (C1 * besselj(nu, z) + C2 * bessely(nu, z))

        return [Eq(f(x), solution)]

class SecondLinearAiry(SingleODESolver):
    r"""
    Gives solution of the Airy differential equation

    .. math :: \frac{d^2y}{dx^2} + (a + b x) y(x) = 0

    in terms of Airy special functions airyai and airybi.

    Examples
    ========

Review comment (suggestion):
If this solver is going to reject this equation then it should reject it in the `_matches` method rather than raising an exception here.

I think if we know here what the solution is though then it is reasonable to just return it.

Code after:

        return True

    def _get_general_solution(self, *, simplify_flag: bool = True):
        f = self.ode_problem.func.func
        x = self.ode_problem.sym
        (C1, C2) = self.ode_problem.get_numbered_constants(num=2)

        A = self.match_dict['A']
        k = self.match_dict['k']

        # For y'' + A*x^k*y = 0
        # The Bessel order and argument are:
        nu = 1 / (k + 2)

        z = (2 * sqrt(Abs(A)) / (k + 2)) * x**((k + 2)/2)

        # General solution
        return [Eq(f(x), sqrt(x) * (C1*besselj(nu, z) + C2*bessely(nu, z)))]


class SecondLinearAiry(SingleODESolver):
    r"""
    Gives solution of the Airy differential equation

    .. math :: \frac{d^2y}{dx^2} + (a + b x) y(x) = 0

    in terms of Airy special functions airyai and airybi.

    Examples
    ========

    >>> from sympy import dsolve, Function
    >>> from sympy.abc import x
    >>> f = Function("f")
    >>> eq = f(x).diff(x, 2) - x*f(x)
    >>> dsolve(eq)
    Eq(f(x), C1*airyai(x) + C2*airybi(x))
    """
    hint = "2nd_linear_airy"
    has_integral = False

    def _matches(self):
        eq = self.ode_problem.eq_high_order_free
        f = self.ode_problem.func
        order = self.ode_problem.order
        x = self.ode_problem.sym
        df = f.diff(x)
        a4 = Wild('a4', exclude=[x,f,df])
        b4 = Wild('b4', exclude=[x,f,df])
        match = self.ode_problem.get_linear_coefficients(eq, f, order)