def test_fixed_step(self, vals): htrue = self._hfun(vals) methods = [ 'central2', 'central', 'multicomplex', 'complex', 'forward', 'backward' ] for order in range(2, 7, 2): steps = nd.MinStepGenerator(num_steps=order + 1, use_exact_steps=True, step_ratio=3., offset=0) note('order = {}'.format(order)) for method in methods: h_fun = nd.Hessdiag(self._fun, step=steps, method=method, order=order, full_output=True) h_val, _info = h_fun(vals) _error = np.abs(h_val - htrue) note('error = {}, error_est = {}'.format( _error, _info.error_estimate)) assert_allclose(h_val, htrue, rtol=1e-5, atol=100 * max(_info.error_estimate))
def test_default_step(self): 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(self._fun, method=method, order=order, full_output=True) hd, _info = Hfun([1, 2, 3]) _error = hd - htrue assert_array_almost_equal(hd, htrue)
def test_default_step(self): htrue = np.array([0., 2., 18.]) methods = [ 'central2', 'central', 'multicomplex', 'complex', 'forward', 'backward' ] for order in range(2, 7, 2): for method in methods: h_fun = nd.Hessdiag(self._fun, method=method, order=order, full_output=True) h_val, _info = h_fun([1, 2, 3]) # _error = h_val - htrue tol = min(1e-8, _info.error_estimate.max()) assert_allclose(h_val, htrue, atol=tol)
def test_complex(self): htrue = np.array([0., 2., 18.]) method = 'complex' for num_steps in range(3, 7, 1): steps = nd.MinStepGenerator(num_steps=num_steps, use_exact_steps=True, step_ratio=2.0, offset=4) h_fun = nd.Hessdiag(self._fun, step=steps, method=method, full_output=True) h_val, _info = h_fun([1, 2, 3]) assert_allclose(h_val, htrue)
def test_complex(self): htrue = np.array([0., 2., 18.]) method = 'complex' for num_steps in range(3, 7, 1): steps = nd.MinStepGenerator(num_steps=num_steps, use_exact_steps=True, step_ratio=2.0, offset=4) Hfun = nd.Hessdiag(self._fun, step=steps, method=method, full_output=True) hd, _info = Hfun([1, 2, 3]) _error = hd - htrue assert_array_almost_equal(hd, htrue)
def test_fixed_step(self): htrue = np.array([0., 2., 18.]) methods = ['multicomplex', 'complex', 'central', 'forward', 'backward'] for order in range(2, 7, 2): steps = nd.MinStepGenerator(num_steps=order + 1, use_exact_steps=True, step_ratio=3., offset=0) for method in methods: Hfun = nd.Hessdiag(self._fun, step=steps, method=method, order=order, full_output=True) hd, _info = Hfun([1, 2, 3]) _error = hd - htrue assert_array_almost_equal(hd, htrue)