def _print_summary(method, order, x_values, scales): print(scales) header = 'method="{}", order={}, x_values={}:'.format( method, order, str(x_values)) print(header) for n in scales: print('n={}, mean scale={:.2f}, median scale={:.2f}'.format( n, np.mean(scales[n]), np.median(scales[n]))) print('Default scale with ' + header) for n in scales: print('n={}, scale={:.2f}'.format(n, default_scale(method, n, order)))
def benchmark(x=0.0001, dfun=None, fd=None, name='', scales=None, show_plot=True): if scales is None: scales = np.arange(1.0, 35, 0.25) n, method, order = fd.n, fd.method, fd.order if dfun is None: return dict(n=n, order=order, method=method, fun=name, error=np.nan, scale=np.nan, x=np.nan) relativ_errors = _compute_relative_errors(x, dfun, fd, scales) if not np.isfinite(relativ_errors).any(): return dict(n=n, order=order, method=method, fun=name, error=np.nan, scale=np.nan) if show_plot: txt = ['', "1'st", "2'nd", "3'rd", "4'th", "5'th", "6'th", "7th" ] + ["%d'th" % i for i in range(8, 25)] title = "The %s derivative using %s, order=%d" % (txt[n], method, order) scale0 = default_scale(method, n, order) plot_error(scales, relativ_errors, scale0, title, label=name) i = np.nanargmin(relativ_errors) error = float('{:.3g}'.format(relativ_errors[i])) return dict(n=n, order=order, method=method, fun=name, error=error, scale=scales[i], x=x)
def test_default_scale(): for method, scale in zip( ['complex', 'central', 'forward', 'backward', 'multicomplex'], [1.35, 2.5, 2.5, 2.5, 1.35]): assert_allclose(scale, default_scale(method, n=1))
def _example3(x=0.0001, fun_name='cos', epsilon=None, method='central', scale=None, n=1, order=2): fun0, dfun = get_function(fun_name, n) if dfun is None: return dict(n=n, order=order, method=method, fun=fun_name, error=np.nan, scale=np.nan) fd = Derivative(fun0, step=epsilon, method=method, n=n, order=order) t = [] scales = np.arange(1.0, 35, 0.25) for scale in scales: fd.step.scale = scale try: val = fd(x) except Exception: val = np.nan t.append(val) t = np.array(t) tt = dfun(x) relativ_error = np.abs(t - tt) / (np.maximum(np.abs(tt), 1)) + 1e-16 # weights = np.ones((3,))/3 # relativ_error = convolve1d(relativ_error, weights) # smooth curve if np.isnan(relativ_error).all(): return dict(n=n, order=order, method=method, fun=fun_name, error=np.nan, scale=np.nan) if True: # False: # plt.semilogy(scales, relativ_error, label=fun_name) plt.vlines(default_scale(fd.method, n, order), np.nanmin(relativ_error), 1) plt.xlabel('scales') plt.ylabel('Relative error') txt = ['', "1'st", "2'nd", "3'rd", "4'th", "5'th", "6'th", "7th" ] + ["%d'th" % i for i in range(8, 25)] plt.title("The %s derivative using %s, order=%d" % (txt[n], method, order)) plt.legend(frameon=False, framealpha=0.5) plt.axis([min(scales), max(scales), np.nanmin(relativ_error), 1]) # plt.figure() # plt.show('hold') i = np.nanargmin(relativ_error) return dict(n=n, order=order, method=method, fun=fun_name, error=relativ_error[i], scale=scales[i])
order=order) print(r) scale_n = scales.setdefault(n, []) scale = r['scale'] if np.isfinite(scale): scale_n.append(scale) plt.vlines(np.mean(scale_n), 1e-12, 1, 'r', linewidth=3) print(scales) print('method={}, order={}'.format(method, order)) for n in scales: print('n={}, scale={}'.format(n, np.mean(scales[n]))) print('Default scale') for n in scales: print('n={}, scale={}'.format(n, default_scale(method, n, order))) plt.show('hold') # method=complex, order=2 # n=1, scale=1.05188679245 # n=2, scale=5.55424528302 # n=3, scale=8.34669811321 # n=4, scale=9.08072916667 # n=5, scale=10.1958333333 # n=6, scale=9.13020833333 # n=7, scale=13.859375 # n=8, scale=14.125 # n=9, scale=12.8385416667 # n=10, scale=14.5416666667
def test_default_scale(): 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, default_scale(method, n=1))