findifftool/core.py
def ndhess(f, delta=DELTA):
    """
    Returns numerical hessian function of given input function
    Input: f, scalar function of an numpy array object
           delta(optional), finite difference step
    Output: hessian function object
    """
    def hess_f(*args, **kwargs):
        x = args[0]
        hess_val = numpy.zeros(x.shape + x.shape)
        it = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
        for xi in it:
            i = it.multi_index
            jt = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
            for xj in jt:
                j = jt.multi_index
                xi += delta/2
                xj += delta/2
                fpp = f(x)
                xj -= delta
                fpm = f(x)
                xi -= delta
                fmm = f(x)
                xj += delta
                fmp = f(x)
                xi += delta/2
                xj -= delta/2
                hess_val[i + j] = (fpp + fmm - fpm - fmp)/delta**2
        return hess_val
    return hess_f
def ndhess(f, delta=DELTA):
    """
    Returns numerical hessian function of given input function
    Input: f, scalar function of an numpy array object
           delta(optional), finite difference step
    Output: hessian function object
    """
    def hess_f(*args, **kwargs):
        x = args[0]
        hess_val = numpy.zeros(x.shape + x.shape)
        it = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
        for xi in it:
            i = it.multi_index
            jt = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
            for xj in jt:
                j = jt.multi_index
                xi += delta/2
                xj += delta/2
                fpp = f(x)
                xj -= delta
                fpm = f(x)
                xi -= delta
                fmm = f(x)
                xj += delta
                fmp = f(x)
                xi += delta/2
                xj -= delta/2
                hess_val[i + j] = (fpp + fmm - fpm - fmp)/delta**2
        return hess_val
    return hess_f
Returns numerical hessian function of given input function
    Input: f, scalar function of an numpy array object
           delta(optional), finite difference step
    Output: hessian function object