How to use the numdifftools.core.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 pbrod / numdifftools / tests / test_numdifftools.py View on Github external
(2, 2, 2): [0.0002614379084968331, -0.07111111111111235,
                        1.070849673202616],
            (1, 2, 6): [0.0002614379084968331, -0.07111111111111235,
                        1.070849673202616],
            (4, 2, 6): [0.0002614379084968331, -0.07111111111111235,
                        1.070849673202616],
            (1, 1, 2): [-0.33333333333333304, 1.333333333333333],
            (3, 2, 8): [9.576480164718605e-07, -0.004167684167715291,
                        1.004166726519699],
            (2, 2, 6): [0.0002614379084968331, -0.07111111111111235,
                        1.070849673202616]}
        # t = dict()
        for n in [1, 2, 3, 4]:
            for num_terms in [1, 2]:
                for order in range(2, 9, 2):
                    d = nd.Derivative(np.exp, n=n, method='complex',
                                      order=order)
                    d._set_richardson_rule(step_ratio=2.0, num_terms=num_terms)
                    rule = d._richardson_extrapolate._get_richardson_rule()
                    # t[(n, num_terms, order)] = rule.tolist()
                    assert_array_almost_equal(rule,
                                              truth[(n, num_terms, order)])
        # print(t)
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_backward_derivative_on_sinh(self):
        # Compute the derivative of a function using a backward difference
        # scheme.  A backward scheme will only look below x0.
        dsinh = nd.Derivative(np.sinh, method='backward')
        self.assertAlmostEqual(dsinh(0.0), np.cosh(0.0))
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_infinite_functions(self):
        def finf(x):
            return np.inf
        df = nd.Derivative(finf)
        val = df(0)
        self.assert_(np.isnan(val))
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
true_vals = {(1, 1): [-0.33333333, 1.33333333],
                     (1, 2): [-0.33333333, 1.33333333],
                     (1, 3): [-0.33333333, 1.33333333],
                     (1, 4): [-0.06666667, 1.06666667],
                     (1, 5): [-0.06666667, 1.06666667],
                     (1, 6): [-0.01587302, 1.01587302],
                     (2, 1): [0.02222222, -0.44444444, 1.42222222],
                     (2, 2): [0.02222222, -0.44444444, 1.42222222],
                     (2, 3): [0.02222222, -0.44444444, 1.42222222],
                     (2, 4): [1.05820106e-03, -8.46560847e-02, 1.08359788e+00],
                     (2, 5): [1.05820106e-03, -8.46560847e-02, 1.08359788e+00],
                     (2, 6): [6.22471211e-05, -1.99190787e-02, 1.01985683e+00]}

        for num_terms in [1, 2]:
            for order in range(1, 7):
                d = nd.Derivative(np.exp, method=method, order=order)
                d._set_richardson_rule(step_ratio=2.0, num_terms=num_terms)
                rule = d._richardson_extrapolate._get_richardson_rule()
                assert_array_almost_equal(rule,
                                          true_vals[(num_terms, order)])
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_high_order_derivative_cos(self):
        true_vals = (-1.0, 0.0, 1.0, 0.0, -1.0, 0.0)
        methods = ['complex', 'multicomplex', 'central',
                   'forward', 'backward']
        for method in methods:
            n_max = dict(multicomplex=2, central=6).get(method, 5)
            for n in range(1, n_max + 1):
                true_val = true_vals[n - 1]
                for order in range(2, 9, 2):
                    d3cos = nd.Derivative(np.cos, n=n, order=order,
                                          method=method, full_output=True)
                    y, info = d3cos(np.pi / 2.0)
                    error = np.abs(y - true_val)
                    small = error <= info.error_estimate
                    if not small:
                        small = error < 10**(-12 + n)
                    if not small:
                        print('method=%s, n=%d, order=%d' % (method, n, order))
                        print(error, info.error_estimate)
                    # self.assertTrue(small)
                    assert_array_almost_equal(y, true_val, decimal=4)
        # self.assert_(False)
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_derivative_cube(self):
        '''Test for Issue 7'''
        def cube(x):
            return x * x * x
        dcube = nd.Derivative(cube)
        shape = (3, 2)
        x = np.ones(shape) * 2
        dx = dcube(x)
        assert_array_almost_equal(list(dx.shape), list(shape),
                                  decimal=8,
                                  err_msg='Shape mismatch')
        txt = 'First differing element %d\n value = %g,\n true value = %g'
        for i, (val, tval) in enumerate(zip(dx.ravel(), (3 * x**2).ravel())):
            assert_array_almost_equal(val, tval, decimal=8,
                                      err_msg=txt % (i, val, tval))
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_central_and_forward_derivative_on_log(self):
        # Although a central rule may put some samples in the wrong places, it
        # may still succeed
        epsilon = nd.MinStepGenerator(num_steps=15, offset=0, step_ratio=2)
        dlog = nd.Derivative(np.log, method='central', step=epsilon)
        x = 0.001
        self.assertAlmostEqual(dlog(x), 1.0 / x)

        # But forcing the use of a one-sided rule may be smart anyway
        dlog = nd.Derivative(np.log, method='forward', step=epsilon)
        self.assertAlmostEqual(dlog(x), 1 / x)
github pbrod / numdifftools / tests / test_numdifftools.py View on Github external
def test_derivative_of_cos_x(self):
        x = np.r_[0, np.pi / 6.0, np.pi / 2.0]
        true_vals = (-np.sin(x), -np.cos(x), np.sin(x), np.cos(x), -np.sin(x),
                     -np.cos(x))
        for method in ['complex', 'central', 'forward', 'backward']:
            n_max = dict(complex=2, central=6).get(method, 5)
            for n in range(1, n_max + 1):
                true_val = true_vals[n - 1]
                start, stop, step = dict(central=(2, 7, 2),
                                         complex=(2, 3, 1)).get(method,
                                                                (1, 5, 1))
                for order in range(start, stop, step):
                    d3cos = nd.Derivative(np.cos, n=n, order=order,
                                          method=method, full_output=True)
                    y, info = d3cos(x)
                    error = np.abs(y - true_val)
                    small = error <= info.error_estimate
                    if not small.all():
                        small = np.where(small, small, error <= 10**(-11 + n))
                    if not small.all():
                        print('method=%s, n=%d, order=%d' % (method, n, order))
                        print(error, info.error_estimate)
                    assert_array_almost_equal(y, true_val, decimal=4)
                    # self.assertTrue(small.all())