# How to use the numdifftools.nd_algopy.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.

pbrod / numdifftools / tests / test_nd_algopy.py View on Github
``````def test_high_order_derivative_cos(self):
true_vals = (-1.0, 0.0, 1.0, 0.0) * 5

x = np.pi / 2  # np.linspace(0, np.pi/2, 15)
for method in ['forward', 'reverse']:
nmax = 15 if method in ['forward'] else 2
for n in range(1, nmax):
d3cos = nd.Derivative(np.cos, n=n, method=method)
y = d3cos(x)
assert_array_almost_equal(y, true_vals[n - 1])``````
pbrod / numdifftools / tests / test_nd_algopy.py View on Github
``````def test_derivative_exp(self):
# derivative of exp(x), at x == 0
for method in ['forward', 'reverse']:
dexp = nd.Derivative(np.exp, method=method)
assert_array_almost_equal(dexp(0), np.exp(0), decimal=8)``````
pbrod / numdifftools / tests / test_nd_algopy.py View on Github
``````def test_derivative_cube(self):
'''Test for Issue 7'''
def cube(x):
return x * x * x

shape = (3, 2)
x = np.ones(shape) * 2
for method in ['forward', 'reverse']:
dcube = nd.Derivative(cube, method=method)
dx = dcube(x)
assert_array_almost_equal(list(dx.shape), list(shape),
decimal=13,
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))``````
pbrod / numdifftools / tests / test_nd_algopy.py View on Github
``````def test_derivative_on_sinh(self):
for method in ['forward', ]:  # 'reverse']: # TODO: reverse fails
dsinh = nd.Derivative(np.sinh, method=method)
self.assertAlmostEqual(dsinh(0.0), np.cosh(0.0))``````
pbrod / numdifftools / tests / test_nd_algopy.py View on Github
``````def test_derivative_sin(self):
# Evaluate the indicated (default = first)
# derivative at multiple points
for method in ['forward', 'reverse']:
dsin = nd.Derivative(np.sin, method=method)
x = np.linspace(0, 2. * np.pi, 13)
y = dsin(x)
np.testing.assert_almost_equal(y, np.cos(x), decimal=8)``````
pbrod / numdifftools / tests / test_nd_algopy.py View on Github
``````def test_derivative_on_log(self):

x = np.r_[0.01, 0.1]
for method in ['forward', 'reverse']:
dlog = nd.Derivative(np.log, method=method)

assert_array_almost_equal(dlog(x), 1.0 / x)``````

## numdifftools

Solves automatic numerical differentiation problems in one or more variables.

BSD-3-Clause
Latest version published 1 year ago

47 / 100