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

Code before:
               HomogeneousCoeffSubsDepDivIndep._verify(self, fx)

    def _get_general_solution(self, *, simplify_flag: bool = True):
        # There are two substitutions that solve the equation, u1=y/x and u2=x/y
        # # They produce different integrals, so try them both and see which
        # # one is easier
        [sol1] = HomogeneousCoeffSubsIndepDivDep._get_general_solution(self)
        [sol2] = HomogeneousCoeffSubsDepDivIndep._get_general_solution(self)
        fx = self.ode_problem.func
        if simplify_flag:
            sol1 = odesimp(self.ode_problem.eq, sol1, fx, "1st_homogeneous_coeff_subs_indep_div_dep")
            sol2 = odesimp(self.ode_problem.eq, sol2, fx, "1st_homogeneous_coeff_subs_dep_div_indep")
        if not isinstance(sol1, list):
            sol1 = [sol1]
        if not isinstance(sol2, list):
            sol2 = [sol2]
        simplest_of_sol1 = min(sol1, key=lambda x: ode_sol_simplicity(x, fx, trysolving=not simplify_flag))
        simplest_of_sol2 = min(sol2, key=lambda x: ode_sol_simplicity(x, fx, trysolving=not simplify_flag))
        simplest_of_sol1_complexity = ode_sol_simplicity(simplest_of_sol1, fx, trysolving=not simplify_flag)
        simplest_of_sol2_complexity = ode_sol_simplicity(simplest_of_sol2, fx, trysolving=not simplify_flag)
        if simplest_of_sol1_complexity < simplest_of_sol2_complexity:
            return sol1
        elif simplest_of_sol2_complexity < simplest_of_sol1_complexity:
            return sol2
        else:
            return sol1


class LinearCoefficients(HomogeneousCoeffBest):
    r"""
    Solves a differential equation with linear coefficients.

    The general form of a differential equation with linear coefficients is

    .. math:: y' + F\left(\!\frac{a_1 x + b_1 y + c_1}{a_2 x + b_2 y +
                c_2}\!\right) = 0\text{,}

    where `a_1`, `b_1`, `c_1`, `a_2`, `b_2`, `c_2` are constants and `a_1 b_2
    - a_2 b_1 \ne 0`.

    This can be solved by substituting:

    .. math:: x = x' + \frac{b_2 c_1 - b_1 c_2}{a_2 b_1 - a_1 b_2}

              y = y' + \frac{a_1 c_2 - a_2 c_1}{a_2 b_1 - a_1
                  b_2}\text{.}

    This substitution reduces the equation to a homogeneous differential
    equation.

    See Also

Review comment (suggestion):
This is probably better than adding the scores but it should be obvious that this code can be improved:

- It should not compute `ode_sol_simplicity` more than once for the same solution.
- We don't need three cases in the `if/elif` chain.

Code after:
               HomogeneousCoeffSubsDepDivIndep._verify(self, fx)

    def _get_general_solution(self, *, simplify_flag: bool = True):
        # There are two substitutions that solve the equation, u1=y/x and u2=x/y
        # # They produce different integrals, so try them both and see which
        # # one is easier
        [sol1] = HomogeneousCoeffSubsIndepDivDep._get_general_solution(self)
        [sol2] = HomogeneousCoeffSubsDepDivIndep._get_general_solution(self)
        fx = self.ode_problem.func
        if simplify_flag:
            sol1 = odesimp(self.ode_problem.eq, sol1, fx, "1st_homogeneous_coeff_subs_indep_div_dep")
            sol2 = odesimp(self.ode_problem.eq, sol2, fx, "1st_homogeneous_coeff_subs_dep_div_indep")
        if not isinstance(sol1, list):
            sol1 = [sol1]
        if not isinstance(sol2, list):
            sol2 = [sol2]
        sol1_max_complexity = max(ode_sol_simplicity(sol, fx, trysolving= not simplify_flag) for sol in sol1)
        sol2_max_complexity = max(ode_sol_simplicity(sol, fx, trysolving= not simplify_flag) for sol in sol2)
        if sol2_max_complexity < sol1_max_complexity:
            return sol2
        else:
            return sol1


class LinearCoefficients(HomogeneousCoeffBest):
    r"""
    Solves a differential equation with linear coefficients.

    The general form of a differential equation with linear coefficients is

    .. math:: y' + F\left(\!\frac{a_1 x + b_1 y + c_1}{a_2 x + b_2 y +
                c_2}\!\right) = 0\text{,}

    where `a_1`, `b_1`, `c_1`, `a_2`, `b_2`, `c_2` are constants and `a_1 b_2
    - a_2 b_1 \ne 0`.

    This can be solved by substituting:

    .. math:: x = x' + \frac{b_2 c_1 - b_1 c_2}{a_2 b_1 - a_1 b_2}

              y = y' + \frac{a_1 c_2 - a_2 c_1}{a_2 b_1 - a_1
                  b_2}\text{.}

    This substitution reduces the equation to a homogeneous differential
    equation.

    See Also
    ========
    :obj:`sympy.solvers.ode.single.HomogeneousCoeffBest`
    :obj:`sympy.solvers.ode.single.HomogeneousCoeffSubsIndepDivDep`
    :obj:`sympy.solvers.ode.single.HomogeneousCoeffSubsDepDivIndep`