(f, symbol)
return f.diff(symbol)
Derivative of f in form expected by find_substitutions

    SymPy's derivatives for some trig functions (like cot) aren't in a form
    that works well with finding substitutions; this replaces the
    derivatives for those particular forms with something that works better.
Derivative of f in form expected by find_substitutions
Derivative of f in form expected by find_substitutions
def manual_diff(f, symbol):
    """Derivative of f in form expected by find_substitutions

    SymPy's derivatives for some trig functions (like cot) aren't in a form
    that works well with finding substitutions; this replaces the
    derivatives for those particular forms with something that works better.

    """
    if f.args:
        arg = f.args[0]
        if isinstance(f, sympy.tan):
            return arg.diff(symbol) * sympy.sec(arg)**2
        elif isinstance(f, sympy.cot):
            return -arg.diff(symbol) * sympy.csc(arg)**2
        elif isinstance(f, sympy.sec):
            return arg.diff(symbol) * sympy.sec(arg) * sympy.tan(arg)
        elif isinstance(f, sympy.csc):
            return -arg.diff(symbol) * sympy.csc(arg) * sympy.cot(arg)
        elif isinstance(f, sympy.Add):
            return sum([manual_diff(arg, symbol) for arg in f.args])
        elif isinstance(f, sympy.Mul):
            if len(f.args) == 2 and isinstance(f.args[0], sympy.Number):
                return f.args[0] * manual_diff(f.args[1], symbol)
    return f.diff(symbol)
def manual_diff ( f , symbol ) : if f . args : arg = f . args [ 0 ] if isinstance ( f , sympy . tan ) : return arg . diff ( symbol ) * sympy . sec ( arg ) ** 2 elif isinstance ( f , sympy . cot ) : return - arg . diff ( symbol ) * sympy . csc ( arg ) ** 2 elif isinstance ( f , sympy . sec ) : return arg . diff ( symbol ) * sympy . sec ( arg ) * sympy . tan ( arg ) elif isinstance ( f , sympy . csc ) : return - arg . diff ( symbol ) * sympy . csc ( arg ) * sympy . cot ( arg ) elif isinstance ( f , sympy . Add ) : return sum ( [ manual_diff ( arg , symbol ) for arg in f . args ] ) elif isinstance ( f , sympy . Mul ) : if len ( f . args ) == 2 and isinstance ( f . args [ 0 ] , sympy . Number ) : return f . args [ 0 ] * manual_diff ( f . args [ 1 ] , symbol ) return f . diff ( symbol )