def test_derivative_sin(): # 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) assert_allclose(y, np.cos(x))
def test_derivative_on_sinh(self, x): true_val = np.cosh(x) for method in ['forward', 'backward']: dsinh = nd.Derivative(np.sinh, method=method) val = dsinh(x) if np.isnan(true_val): assert np.isnan(val) == np.isnan(true_val) else: assert_allclose(val, true_val)
def test_high_order_derivative_cos(): 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_allclose(y, true_vals[n - 1], atol=1e-15)
def test_derivative_cube(): """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_allclose(list(dx.shape), list(shape), 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_allclose(val, tval, err_msg=txt % (i, val, tval))
def test_derivative_on_log(x): for method in ['forward', 'reverse']: dlog = nd.Derivative(np.log, method=method) assert_allclose(dlog(x), 1.0 / x)
def test_derivative_exp(x): for method in ['forward', 'reverse']: dexp = nd.Derivative(np.exp, method=method) assert_allclose(dexp(x), np.exp(x))