def integrate_clenshaw(f, a, b, maxn = 2**16, tol = 10e-16, debug_info = True, debug_plot = True): n = 3 prevI = None nodes = None cm = convergence_monitor() while n <= maxn: coeffs = clenshaw_coefficients(n) if nodes is None: nodes = cheb_nodes(n, a, b) fs = f(nodes) else: new_nodes = incremental_cheb_nodes(n, a, b) new_fs = f(new_nodes) nodes, fs = combine_interpolation_nodes_fast(nodes, fs, new_nodes, new_fs) I = dot(fs, coeffs) * 0.5 * (b - a) if prevI is not None: err = abs(I - prevI) cm.add(err, I) if cm.test_convergence()[0]: break prevI = I n = 2 * n - 1 if debug_info: print "====" if debug_plot and n >= maxn: # problems with integration debug_plot(a, b, nodes, fs, coeffs) return I, err
def integrate_fejer2(f, a, b, par=None, maxn=2**10, tol=finfo(double).eps, debug_info=False, debug_plot=False): if par is not None: maxn = par.maxn debug_plot = par.debug_plot debug_info = par.debug_info cm = convergence_monitor(par=par.convergence) else: cm = convergence_monitor() n = 65 prevI = None nodes = None while n <= maxn: coeffs = fejer2_coefficients(n) if nodes is None: nodes = cheb_nodes(n, a, b)[1:-1] fs = f(nodes) else: new_nodes = incremental_cheb_nodes(n, a, b) new_fs = f(new_nodes) # roles of new and old nodes are reversed in the call below nodes, fs = combine_interpolation_nodes_fast( new_nodes, new_fs, nodes, fs) I = dot(fs, coeffs) * (b - a) / 2 if prevI is not None: err = abs(I - prevI) if debug_info: print repr(I), err, n, I + err == I, err <= abs(I) * tol, min( nodes), max(nodes), min(fs), max(fs) cm.add(err, I) if cm.test_convergence()[0]: break prevI = I n = 2 * n - 1 if debug_info: print "====" if debug_plot and n >= maxn: # problems with integration debug_plot(a, b, nodes, fs, coeffs) # return currently best result I, err, _extra = cm.get_best_result() return I, err
def integrate_fejer2(f, a, b, par = None, maxn = 2**10, tol = finfo(double).eps, debug_info = False, debug_plot = False): if par is not None: maxn = par.maxn debug_plot = par.debug_plot debug_info = par.debug_info cm = convergence_monitor(par = par.convergence) else: cm = convergence_monitor() n = 65 prevI = None nodes = None while n <= maxn: coeffs = fejer2_coefficients(n) if nodes is None: nodes = cheb_nodes(n, a, b)[1:-1] fs = f(nodes) else: new_nodes = incremental_cheb_nodes(n, a, b) new_fs = f(new_nodes) # roles of new and old nodes are reversed in the call below nodes, fs = combine_interpolation_nodes_fast(new_nodes, new_fs, nodes, fs) I = dot(fs, coeffs) * (b - a) / 2 if prevI is not None: err = abs(I - prevI) if debug_info: print repr(I), err, n, I + err == I, err <= abs(I) * tol, min(nodes), max(nodes), min(fs), max(fs) cm.add(err, I) if cm.test_convergence()[0]: break prevI = I n = 2 * n - 1 if debug_info: print "====" if debug_plot and n >= maxn: # problems with integration debug_plot(a, b, nodes, fs, coeffs) # return currently best result I, err, _extra = cm.get_best_result() return I, err
def add_nodes(self, new_Xs, new_Ys): """Add new interpolation nodes. A faster version assuming nesting is possible here.""" self.Xs, self.Ys = combine_interpolation_nodes_fast(self.Xs, self.Ys, new_Xs, new_Ys) self.init_weights(self.Xs)