def test_exponential(self): degree = 5 p = approximate_taylor_polynomial(np.exp, 0, degree, 1, 15) for i in xrange(degree+1): assert_almost_equal(p(0),1) p = p.deriv() assert_almost_equal(p(0),0)
def test_exponential(self): degree = 5 p = approximate_taylor_polynomial(np.exp, 0, degree, 1, 15) for i in xrange(degree+1): assert_almost_equal(p(0),1) p = p.deriv() assert_almost_equal(p(0),0)
def taylor(self, gr, x, degree): def f(grs): res = [] for i in grs: res += [self.npv(gr=i)] return res #approx = approximate_taylor_polynomial(f, x=x, degree=degree, scale=gr) return approximate_taylor_polynomial(f, x=x, degree=degree, scale=gr)
def _get_polynomial(self, order, scale): # this is the function for K, given a normalized (0,1) bandwidth def f(x): return (np.tan(x / 2) - 1) / (np.tan(x / 2) + 1) # calculate taylor series coefficients p = approximate_taylor_polynomial(f, x=self.x0, degree=order, scale=scale) return p.coeffs.astype('float32')
def _get_polynomial(self, order, scale): # this is the function for the gain, given a normalized bandwidth def f(x): return 1 / (1 + np.tan(x * np.pi / 2)) # calculate taylor series coefficients p = approximate_taylor_polynomial(f, x=self.x0, degree=order, scale=scale) return p.coeffs.astype('float32')
def pade_propagator_coefs_m(*, pade_order, diff2, k0, dx, spe=False, alpha=0): if spe: def sqrt_1plus(x): return 1 + x / 2 elif alpha == 0: def sqrt_1plus(x): return np.sqrt(1 + x) else: raise Exception('alpha not supported') def propagator_func(s): return np.exp(1j * k0 * dx * (sqrt_1plus(diff2(s)) - 1)) taylor_coefs = approximate_taylor_polynomial( propagator_func, 0, pade_order[0] + pade_order[1] + 5, 0.01) p, q = pade(taylor_coefs, pade_order[0], pade_order[1]) pade_coefs = list( zip_longest([-1 / complex(v) for v in np.roots(p)], [-1 / complex(v) for v in np.roots(q)], fillvalue=0.0j)) return pade_coefs
from scipy import sin xvals = np.linspace(-10, 10) def fitfunc(x): """Function to fit with Taylor series.""" return sin(x) yf = fitfunc(xvals) x0 = 0 n = 2 scale = 0.5 tfit = approximate_taylor_polynomial(fitfunc, x0, n, scale) yt = tfit(xvals) # create bokeh data sources for the two graphs fsource = ColumnDataSource(data=dict(x=xvals, y=yf)) tsource = ColumnDataSource(data=dict(x=xvals, y=yt)) # Set up plot plot = figure(plot_height=400, plot_width=600, title="Series Approximation", tools="crosshair,pan,reset,save,wheel_zoom", y_range=[-2, 2], x_range=[-6, 6]) # colors are dark for use in the dark optics labs