How to use the numdifftools.Derivative function in numdifftools

To help you get started, we’ve selected a few numdifftools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github oxfordcontrol / Bayesian-Optimization / tests / test_derivatives.py View on Github external
def solution(om):
        '''
        Return [vec(M); vec(Y_0); ...; vec(Y_k)] at omega=om
        '''
        M, Y = sdp(om, fmin)[1:3]
        solution = M.flatten()
        for i in range(len(Y)):
            solution = np.concatenate((solution, Y[i].flatten()))
        return solution

    d_opt_val = nd.Derivative(
        lambda x: sdp(omega + x*direction, fmin)[0]
    )(0)

    d_solution = nd.Derivative(
        lambda x: solution(omega + x*direction)
    )(0)

    return d_opt_val, d_solution
github vlad17 / runlmc / runlmc / kern / test_matern32.py View on Github external
def deriv(x):
            return nd.Derivative(lambda l: Matern32(l).from_dist(x))
        expected = [deriv(x)(self.inv_lengthscale)[0] for x in self.cases]
github ScattPy / scikits.scattpy / scikits / scattpy / laboratory.py View on Github external
def _Rd(self, theta):
        """1st derivative of the surface equation in spherical coordinates:
              r=r\'(theta)"""
        return Derivative(self._R)(theta)
github pbrod / numdifftools / numdifftools / nd_cstep.py View on Github external
print(val - 2 * np.dot(x.T, x))
    print(err)
    erri = [v.max() for v in errt]

    plt.loglog(epsi[1:-1], erri)
    plt.show('hold')
    hnd = nd.Hessian(lambda a: fun2(a, y, x))
    hessnd = hnd(xk)
    print('numdiff')
    print(hessnd - 2 * np.dot(x.T, x))
    # assert_almost_equal(hessnd, he[0])
    gnd = nd.Gradient(lambda a: fun2(a, y, x))
    _gradnd = gnd(xk)

    print(Derivative(np.cosh)(0))
    print(nd.Derivative(np.cosh)(0))
github pbrod / numdifftools / src / numdifftools / _find_default_scale.py View on Github external
def run_all_benchmarks(method='forward', order=4, x_values=(0.1, 0.5, 1.0, 5), n_max=11,
                       show_plot=True):

    epsilon = MinStepGenerator(num_steps=3, scale=None, step_nom=None)
    scales = {}
    for n in range(1, n_max):
        plt.figure(n)
        scale_n = scales.setdefault(n, [])
        # for (name, x) in itertools.product( function_names, x_values):
        for name in function_names:
            fun0, dfun = get_function(name, n)
            if dfun is None:
                continue
            fd = Derivative(fun0, step=epsilon, method=method, n=n, order=order)
            for x in x_values:
                r = benchmark(x=x, dfun=dfun, fd=fd, name=name, scales=None, show_plot=show_plot)
                print(r)
                scale = r['scale']
                if np.isfinite(scale):
                    scale_n.append(scale)

        plt.vlines(np.mean(scale_n), 1e-12, 1, 'r', linewidth=3)
        plt.vlines(np.median(scale_n), 1e-12, 1, 'b', linewidth=3)

    _print_summary(method, order, x_values, scales)
github ScattPy / scikits.scattpy / scikits / scattpy / laboratory.py View on Github external
def _Rdd(self, theta):
        """2nd derivative of the surface equation in spherical coordinates:
              r=r\'\'(theta)"""
        return Derivative(self._R, derOrder=2)(theta)
github rparini / cxroots / cxroots / CountRoots.py View on Github external
if verbose:
                if k > 1:
                    print(k, 'I', I[-1], 'err', I[-2] - I[-1])
                else:
                    print(k, 'I', I[-1])

            if callback is not None:
                err = abs(I[-2] - I[-1]) if k > 1 else None
                if callback(I[-1], err, k):
                    break

        return I[-1], abs(I[-2] - I[-1])

    elif intMethod == 'quad':
        if df is None:
            df = numdifftools.Derivative(f, order=m)
            # df = lambda z: scipy.misc.derivative(f, z, dx=1e-8, n=1, order=3)

            ### Too slow
            # ndf.derivative returns an array [f, f', f'', ...]
            # df = np.vectorize(lambda z: ndf.derivative(f, z, n=1)[1])

        I, err = 0, 0
        for segment in C.segments:
            integrand_cache = {}
            def integrand(t):
                if t in integrand_cache.keys():
                    i = integrand_cache[t]
                else:
                    z = segment(t)
                    i = (df(z)/f(z))/(2j*pi) * segment.dzdt(t)
                    if phi is not None:
github pbrod / numdifftools / examples / fun.py View on Github external
import numdifftools as nd
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 100)
for i in range(0, 10):
    df = nd.Derivative(np.tanh, n=i)
    y = df(x)
    plt.plot(x, y/np.abs(y).max())

plt.axis('off')
plt.axis('tight')
plt.savefig("fun.png")
plt.clf()