Repository: sympy/sympy
File: sympy/simplify/hyperexpand_doc.py (Python)

Code before:
        """Extract special function names from an expression"""
        func_names = set()
        for atom in expr.atoms(Function):
            if not isinstance(atom, AppliedUndef):
                func_name = atom.func.__name__
                if func_name in FUNCTION_LINKS:
                    func_names.add(func_name)
        return sorted(func_names)

    c = FormulaCollection()

    doc = ""

    for f in c.formulae:
        obj = Eq(hyper(f.func.ap, f.func.bq, f.z),
                f.closed_form.rewrite('nonrepsmall'))
        doc += ".. math::\n  %s\n" % latex(obj)
        # Add links to special functions used
        special_funcs = get_special_functions_used(f.closed_form)
        if special_funcs:
            doc += "\n"
            if len(special_funcs) == 1:
                doc += "This formula uses %s.\n" % FUNCTION_LINKS[special_funcs[0]]
            else:
                func_links = ', '.join(FUNCTION_LINKS[fn] for fn in special_funcs[:-1])
                doc += "This formula uses %s and %s.\n" % (func_links, FUNCTION_LINKS[special_funcs[-1]])
            doc += "\n"
    return doc

__doc__ = _generate_doc()


Review comment (suggestion):
This assumes that the number of functions is exactly 2 but without checking that. I think that the code should be written a way that would raise an error in that case if there are not currently any examples where there are more than 2 functions.

Rather than building doc as a string with repeated `+=` it is better to make a list of strings and join them with `doc = '\n'.join(lines)` at the end.

Code after:
        """Extract special function names from an expression."""
        func_names = set()
        for atom in expr.atoms(Function):
            if not isinstance(atom, AppliedUndef):
                func_name = atom.func.__name__
                if func_name in FUNCTION_LINKS:
                    func_names.add(func_name)
        return sorted(func_names)

    c = FormulaCollection()

    lines = []

    for f in c.formulae:
        obj = Eq(hyper(f.func.ap, f.func.bq, f.z),
                f.closed_form.rewrite('nonrepsmall'))
        lines.append(".. math::")
        lines.append("")
        lines.append("    %s" % latex(obj))
        lines.append("")
        # Add links to special functions used
        special_funcs = get_special_functions_used(f.closed_form)
        if special_funcs:
            lines.append("")
            n = len(special_funcs)
            if n == 1:
                lines.append("This formula involves %s." % FUNCTION_LINKS[special_funcs[0]])
            elif n == 2:
                lines.append("This formula involves %s and %s." % (
                    FUNCTION_LINKS[special_funcs[0]],
                    FUNCTION_LINKS[special_funcs[1]]
                ))
            else:
                func_links = ', '.join(FUNCTION_LINKS[fn] for fn in special_funcs[:-1])
                lines.append("This formula involves %s, and %s."% (
                    func_links,
                    FUNCTION_LINKS[special_funcs[-1]]
                ))
            lines.append("")
    return '\n'.join(lines)

__doc__ = _generate_doc()