findifftool/core.py
def ndgrad(f, delta=DELTA):
    """
    Returns numerical gradient function of given input function
    Input: f, scalar function of an numpy array object
           delta(optional), finite difference step
    Output: gradient function object
    """
    def grad_f(*args, **kwargs):
        x = args[0]
        grad_val = numpy.zeros(x.shape)
        it = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
        for xi in it:
            i = it.multi_index
            xi += delta/2
            fp = f(*args, **kwargs)
            xi -= delta
            fm = f(*args, **kwargs)
            xi += delta/2
            grad_val[i] = (fp - fm)/delta
        return grad_val
    return grad_f
def ndgrad(f, delta=DELTA):
    """
    Returns numerical gradient function of given input function
    Input: f, scalar function of an numpy array object
           delta(optional), finite difference step
    Output: gradient function object
    """
    def grad_f(*args, **kwargs):
        x = args[0]
        grad_val = numpy.zeros(x.shape)
        it = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
        for xi in it:
            i = it.multi_index
            xi += delta/2
            fp = f(*args, **kwargs)
            xi -= delta
            fm = f(*args, **kwargs)
            xi += delta/2
            grad_val[i] = (fp - fm)/delta
        return grad_val
    return grad_f
def ndgrad ( f , delta = DELTA ) : def grad_f ( * args , * * kwargs ) : x = args [ 0 ] grad_val = numpy . zeros ( x . shape ) it = numpy . nditer ( x , op_flags = [ 'readwrite' ] , flags = [ 'multi_index' ] ) for xi in it : i = it . multi_index xi += delta / 2 fp = f ( * args , * * kwargs ) xi -= delta fm = f ( * args , * * kwargs ) xi += delta / 2 grad_val [ i ] = ( fp - fm ) / delta return grad_val return grad_f
Returns numerical gradient function of given input function
    Input: f, scalar function of an numpy array object
           delta(optional), finite difference step
    Output: gradient function object
Returns numerical gradient function of given input function Input : f scalar function of an numpy array object delta ( optional ) finite difference step Output : gradient function object