findifftool/core.py
def grad(f, delta=DELTA):
    """
    Returns numerical gradient function of given input function
    Input: f, scalar function of one or two variables
           delta(optional), finite difference step
    Output: gradient function object
    """
    def grad_f(*args, **kwargs):
        if len(args) == 1:
            x, = args
            gradf_x = (
                f(x+delta/2) - f(x-delta/2)
                )/delta
            return gradf_x
        elif len(args) == 2:
            x, y = args
            if type(x) in [float, int] and type(y) in [float, int]:
                gradf_x = (f(x + delta/2, y) - f(x - delta/2, y))/delta
                gradf_y = (f(x, y + delta/2) - f(x, y - delta/2))/delta
                return gradf_x, gradf_y
    return grad_f
def grad(f, delta=DELTA):
    """
    Returns numerical gradient function of given input function
    Input: f, scalar function of one or two variables
           delta(optional), finite difference step
    Output: gradient function object
    """
    def grad_f(*args, **kwargs):
        if len(args) == 1:
            x, = args
            gradf_x = (
                f(x+delta/2) - f(x-delta/2)
                )/delta
            return gradf_x
        elif len(args) == 2:
            x, y = args
            if type(x) in [float, int] and type(y) in [float, int]:
                gradf_x = (f(x + delta/2, y) - f(x - delta/2, y))/delta
                gradf_y = (f(x, y + delta/2) - f(x, y - delta/2))/delta
                return gradf_x, gradf_y
    return grad_f
Returns numerical gradient function of given input function
    Input: f, scalar function of one or two variables
           delta(optional), finite difference step
    Output: gradient function object