How to use the numdifftools.core 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 pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def _example_fd_mat(self):
        fdmat = nd.Derivative._fd_matrix(step_ratio=2.0, parity=1, nterms=3)
        _fd_rules = np.linalg.pinv(fdmat)
        self.assert_(False)
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def testdea3(self):
        def linfun(k):
            return np.linspace(0, np.pi / 2., 2. ** (k + 5) + 1)
        Ei = np.zeros(3)
        for k in np.arange(3):
            x = linfun(k)
            Ei[k] = np.trapz(np.sin(x), x)
        [En, err] = nd.dea3(Ei[0], Ei[1], Ei[2])
        self.assertTrue(np.abs(En - 1) < err)
        assert_array_almost_equal(En, 1.0, decimal=8)
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_weights(self):
        x = np.r_[-1, 0, 1]
        xbar = 0
        k = 1
        weights = nd.fornberg_weights(x, xbar, k)
        np.testing.assert_allclose(weights, [-.5, 0, .5])
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_default_base_step(self):
        step_gen = nd.MinStepGenerator(num_steps=1, offset=0)
        h = [h for h in step_gen(0)]
        desired = nd.EPS ** (1. / 2.5)
        assert_array_almost_equal((h[0] - desired) / desired, 0)
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_default_step(self):
        def fun(x):
            return x[0] + x[1] ** 2 + x[2] ** 3
        htrue = np.array([0., 2., 18.])
        methods = ['central2', 'central', 'multicomplex', 'complex', 'forward',
                   'backward']
        for order in range(2, 7, 2):
            for method in methods:
                Hfun = nd.Hessdiag(fun, method=method, order=order,
                                   full_output=True)
                hd, _info = Hfun([1, 2, 3])
                _error = hd - htrue
                assert_array_almost_equal(hd, htrue)
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def testjacobian(self):
        xdata = np.reshape(np.arange(0, 1, 0.1), (-1, 1))
        ydata = 1 + 2 * np.exp(0.75 * xdata)

        def fun(c):
            return (c[0] + c[1] * np.exp(c[2] * xdata) - ydata) ** 2

        for method in ['complex', 'central', 'forward', 'backward']:
            for order in [2, 4]:
                Jfun = nd.Jacobian(fun, method=method, order=order)
                J = Jfun([1, 2, 0.75])  # should be numerically zero
                assert_array_almost_equal(J, np.zeros(J.shape))
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_default_scale(self):
        for method, scale in zip(['complex', 'central', 'forward', 'backward',
                                  'multicomplex'],
                                 [1.35, 2.5, 2.5, 2.5, 1.35]):
            np.testing.assert_allclose(scale, nd.default_scale(method, n=1))
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def _example_(self):
        def f(x, h):
            return (np.exp(x + h) - np.exp(x - h)) / (2.)
        # f = lambda x, h: (np.exp(x+h)-np.exp(x))
        steps = [h for h in 2.0**-np.arange(10)]
        df = [f(1, h) for h in steps]
        print([dfi / hi for dfi, hi in zip(df, steps)])
        step = nd.MaxStepGenerator(step_ratio=2.0)
        for method in ['central']:
            d = nd.Derivative(np.exp, step=step, method=method)
            for order in [2, 6]:
                d.order = order
                r_extrap = nd.Richardson(step_ratio=2.0, method=method,
                                         num_terms=2, order=order)

                fd_rule = d._get_finite_difference_rule(step_ratio=2.0)
                print(fd_rule)
                df1, stepsi, _shape = d._apply_fd_rule(fd_rule, df, steps)

                rule = r_extrap._get_richardson_rule()
                df2, error, hi = r_extrap(df1, stepsi)

                print(rule)
                print(np.hstack((df2, error)))

        self.assert_(False)
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def _example_(self):
        def f(x, h):
            return (np.exp(x + h) - np.exp(x - h)) / (2.)
        # f = lambda x, h: (np.exp(x+h)-np.exp(x))
        steps = [h for h in 2.0**-np.arange(10)]
        df = [f(1, h) for h in steps]
        print([dfi / hi for dfi, hi in zip(df, steps)])
        step = nd.MaxStepGenerator(step_ratio=2.0)
        for method in ['central']:
            d = nd.Derivative(np.exp, step=step, method=method)
            for order in [2, 6]:
                d.order = order
                r_extrap = nd.Richardson(step_ratio=2.0, method=method,
                                         num_terms=2, order=order)

                fd_rule = d._get_finite_difference_rule(step_ratio=2.0)
                print(fd_rule)
                df1, stepsi, _shape = d._apply_fd_rule(fd_rule, df, steps)

                rule = r_extrap._get_richardson_rule()
                df2, error, hi = r_extrap(df1, stepsi)

                print(rule)
                print(np.hstack((df2, error)))
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_fun_with_additional_parameters(self):
        '''Test for issue #9'''
        def func(x, a, b=1):
            return b * a * x * x * x
        methods = ['forward', 'backward', 'central', 'complex', 'multicomplex']
        dfuns = [nd.Gradient, nd.Derivative,  nd.Jacobian, nd.Hessdiag,
                 nd.Hessian]
        for dfun in dfuns:
            for method in methods:
                df = dfun(func, method=method)
                val = df(0.0, 1.0, b=2)

                assert_array_almost_equal(val, 0)