def test_chebroots(self): assert_almost_equal(cheb.chebroots([1]), []) assert_almost_equal(cheb.chebroots([1, 2]), [-.5]) for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = cheb.chebroots(cheb.chebfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt))
def test_chebroots(self) : assert_almost_equal(cheb.chebroots([1]), []) assert_almost_equal(cheb.chebroots([1, 2]), [-.5]) for i in range(2,5) : tgt = np.linspace(-1, 1, i) res = cheb.chebroots(cheb.chebfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt))
def test_collocation_points_chebyshev_gauss(self): for num_coll_points in range(2, 13): coefs = np.zeros(num_coll_points + 1) coefs[-1] = 1. coll_points_numpy = chebyshev.chebroots(coefs) mesh = Mesh1D(num_coll_points, Basis.Chebyshev, Quadrature.Gauss) coll_points_spectre = collocation_points(mesh) np.testing.assert_allclose(coll_points_numpy, coll_points_spectre, 1e-12, 1e-12)
def cheb_roots_vec(): roots = {} # loop over all degrees up to MAXD for i in range(0, MAXD): c = np.zeros(MAXD) c[i] = 1 r = cheb.chebroots(c) roots.update({i: r}) return roots
def get_cheb_roots(): roots = {} # loop over all degrees up to MAXD for i in range(0, MAXD): c = np.zeros(MAXD) c[i] = 1 r = cheb.chebroots(c) roots.update({i: r}) # save roots to file w = csv.writer(open(CHEBF, "w")) for key, val in roots.items(): w.writerow([key, val]) return
def chebroots(cs): from numpy.polynomial.chebyshev import chebroots return chebroots(cs)
coeff[:, 0] /= 2.0 tmp = int(coeff[0, 0]) coeff[0, 0] -= tmp coeff = np.float64(coeff) t0 = np.float64(t0 - stt_date) * 86400.0 t1 = np.float64(t1 - stt_date) * 86400.0 dt = (np.array([0, nbin0]) * tsamp + stt_sec - t0) / (t1 - t0) * 2 - 1 coeff1 = coeff.sum(1) phase = nc.chebval(dt, coeff1) + dispc / freq_end**2 phase0 = np.ceil(phase[0]) nperiod = int( np.floor(phase[-1] - phase0 + dispc / freq_start**2 - dispc / freq_end**2)) coeff[0, 0] -= phase0 coeff1 = coeff.sum(1) roots = nc.chebroots(coeff1) roots = np.real(roots[np.isreal(roots)]) root = roots[np.argmin(np.abs(roots - dt[0]))] period = 1. / np.polyval(np.polyder(nc.cheb2poly(coeff1)[::-1]), root) / 2.0 * (t1 - t0) stt_sec = (root + 1) / 2.0 * (t1 - t0) + t0 stt_date = stt_date + stt_sec // 86400 stt_sec = stt_sec % 86400 info['phase0'] = int(phase0) + tmp phase -= phase0 # info['nperiod'] = nperiod info['period'] = period # nbin_max = (nbin0 - 1) / (np.max(phase) - np.min(phase)) if args.nbin:
def chebroots(cs): from numpy.polynomial.chebyshev import chebroots return chebroots(cs)
raise RuntimeError("Process pool not initialized") pmap = params.general.process_pool.map else: pmap = map return pmap if __name__ == "__main__": from pacal import * import time import numpy.polynomial.chebyshev as ch c = array([1, 2, 3, 1]) CT =cheb1companion(array([1, 2, 3, 1])) print(CT) print(chebroots(c)) print(ch.chebroots(c)) print(chebroots(c) - ch.chebroots(c)) 0/0 #print taylor_coeff(lambda x:exp(x), 30) N = NormalDistr() fun = N.mgf() #fun.plot() t0 = time.time() t_i = taylor_coeff(fun, 100) print(time.time()-t0) sil=1 t0 = time.time() for i in range(50): if i==0: sil=1
def lagrange(N, x0=x0, x1=x1, C_a=1.3, C_f=1.0, grid='cheb0'): #x = np.linspace(x0, x1, N+1) if grid == 'cheb0': dummy = np.zeros(N) dummy[-1] = 1 x = np.sort(np.array(cheb.chebroots(dummy).tolist() + [-1, 1])) x = 0.5 * (x1 - x0) * x + 0.5 * (x1 + x0) if grid == 'cheb1': dummy = np.zeros(N + 1) dummy[-1] = 1 x = np.sort( np.array(cheb.chebroots(cheb.chebder(dummy)).tolist() + [-1, 1])) x = 0.5 * (x1 - x0) * x + 0.5 * (x1 + x0) if grid == 'legendre0': dummy = np.zeros(N) dummy[-1] = 1 x = np.sort(np.array(leg.legroots(dummy).tolist() + [-1, 1])) x = 0.5 * (x1 - x0) * x + 0.5 * (x1 + x0) if grid == 'legendre1': dummy = np.zeros(N + 1) dummy[-1] = 1 x = np.sort( np.array(leg.legroots(leg.legder(dummy)).tolist() + [-1, 1])) x = 0.5 * (x1 - x0) * x + 0.5 * (x1 + x0) #x = 0.5*(x1-x0)*np.cos( np.pi / N * np.arange(N,-1,-1) )+ 0.5*(x1+x0) #x = 0.5*(x1-x0)*np.cos( np.pi / (N+1) * ( np.arange(N+1,0,-1) -0.5) )+ 0.5*(x1+x0) xx, yy = np.meshgrid(x, x) AA = yy - xx AA[AA == 0] = 1 a = np.prod(AA, axis=1) #mth lagrangian polynomial def phi(y, m): """Evaluates m-th Lagrangian polynomial at point y""" return np.prod((y - x)[np.arange(len(x)) != m]) / a[m] np.seterr(all='ignore') D = 1 / AA np.seterr(all='raise') D = D - np.diag(np.diag(D)) D_diag = np.diag(np.sum(D, axis=1)) D = np.multiply(np.multiply(D.T, a).T, 1 / a) + D_diag #Initialization of nonlinear part Uxy = np.zeros([N + 1, N + 1, N + 1]) #Integration matrix for integrals of between x_k and x_N #W1 = np.zeros( [N+1, N+1] ) #Integration matrix for integrals of between x_0 and x_k for kk in xrange(N + 1): for ii in xrange(N + 1): #if kk<=ii: #W1[kk, ii] = quad(phi, x[kk] , x[-1] , args=(ii,) )[0] if kk >= ii: #W2[kk, ii] = quad(phi, x[0] , x[kk] , args=(ii,) )[0] dummy = x[kk] - x[ii] - xx dummy[np.diag_indices(N + 1)] = 1 Uxy[kk, ii, :] = np.prod(dummy, axis=1) / a #Aggregation in a_ker = np.vectorize(partial(agg_kernel, C_a=C_a)) Ain = 0.5 * a_ker(yy - xx, xx) Ain[0] = 0 #Aggregation out Aout = a_ker(yy, xx - yy) Aout[-1] = 0 #Initialization of fragmentation in frag = np.vectorize(partial(fragmentation, C_f=C_f)) Fin = gam(yy, xx) Fin[-1] = 0 Fin = np.multiply(Fin, frag(x)) #Fin[np.diag_indices(N+1)]*=0.5 return x, D, Fin, Ain, Aout, Uxy
N = 4 A = -3 B = 3 x = IndexedBase('x') y = IndexedBase('y') i = sympy.symbols('i', cls=Idx) L = Sum( y[i] * Product((a - x[j]) / (x[i] - x[j]), (j, 0, i - 1)).doit() * Product( (a - x[j]) / (x[i] - x[j]), (j, i + 1, N)).doit(), (i, 0, N)).doit() L_lambda = lambdify([a, DeferredVector('x'), DeferredVector('y')], L, 'numpy') display_grid = np.linspace(A, B, 1000) interpol_grid = np.linspace(A, B, N + 1) che_interpol_grid = (cheb.chebroots((0, 0, 0, 0, 0, 1)) * (B - A) + A + B) / 2 plt.plot(display_grid, L_lambda(a=display_grid, x=interpol_grid, y=np.sin(interpol_grid)), color='red', label='Lagrange polynomial (equidistant nodes)') plt.plot(display_grid, L_lambda(a=display_grid, x=che_interpol_grid, y=np.sin(che_interpol_grid)), color='blue', label='Lagrange polynomial (roots-of-Chebyshev-series nodes)') plt.plot(display_grid, np.sin(display_grid), color='green', label='sin(x)') plt.legend(loc='upper left') plt.show()
plt.plot(x, e5, label='order 5 approximation error') plt.plot(x, e10, label='order 10 approximation error') plt.title("Monomial interpolation errors of $f(x)=e^{x}$") plt.legend(loc='upper left') plt.show() ## CHEBYSHEV INTERPOLATION AND MONOMIALS: x = np.linspace(-1, 1, 10) def f(x): return np.exp(1 / x) ch = cheb.chebroots(x) f_c = f(ch) # Obtaining the polynomial values, where the number is the order of the polynomial: p3 = np.polyfit(ch, f_c, 3) v3 = np.polyval(p3, ch) p5 = np.polyfit(ch, f_c, 5) v5 = np.polyval(p5, ch) p10 = np.polyfit(ch, f_c, 10) v10 = np.polyval(p10, ch) print(p10) print(v10)
dm_new = np.float64(info['dm']) for s in np.arange(nsub_new[k]): data = d.read_period(s + sub_s)[chanstart:chanend, :, 0] if not args.dm_corr: fftdata = fft.rfft(data, axis=1) tmp = np.shape(fftdata)[-1] const = (1 / freq_real**2 * 4148.808 / np.float64(info['period']) * np.pi * 2.0).repeat(tmp).reshape(-1, tmp) * np.arange(tmp) data1 = shift(fftdata, const * ddm) tpdata = data1[rchan].mean(0) middle_int = middle_phase.date[s] - phase_start middle_offs = middle_phase.second[s] dp, dpe = poa(tpdata0, tpdata) dp0 = dp + np.round(middle_offs - dp) roots = nc.chebroots(chebc - ([dp0 + middle_int] + [0] * (int(nperiod / 2) + 1))) roots = np.real(roots[np.isreal(roots)]) root = roots[np.argmin(np.abs(roots))] toa = te.time(np.float64(info['stt_date']), root + np.float64(info['stt_sec'])) period0 = 1 / nc.chebval(root, chebd) toae = dpe * period0 if output == 'screen': sys.stdout = sys.__stdout__ print(toa.date[0], toa.second[0], dpe, freq, dm_new, period0) sys.stdout = open(os.devnull, 'w') else: result[cumsub[k] + s] = [ toa.date[0], toa.second[0], toae, freq, dm_new, period0 ] else:
times_test = te.times(time_test) timing_test_end = pm.psr_timing(psr, times_test, freq_end) timing_test_start = pm.psr_timing(psr, times_test, freq_start) phase_start = timing_test_end.phase.date[0] + 1 phase_end = timing_test_start.phase.date[-1] phase = timing_test_end.phase.date - phase_start + timing_test_end.phase.second nperiod = phase_end - phase_start period = ( (time_test.date[-1] - time_test.date[0]) * time_test.unit + time_test.second[-1] - time_test.second[0]) / ( timing_test_end.phase.date[-1] - timing_test_end.phase.date[0] + timing_test_end.phase.second[-1] - timing_test_end.phase.second[0]) cheb_end = nc.chebfit( chebx_test, timing_test_end.phase.date - phase_start + timing_test_end.phase.second, args.ncoeff - 1) roots = nc.chebroots(cheb_end) roots = np.real(roots[np.isreal(roots)]) root = roots[np.argmin(np.abs(roots))] stt_time_test = te.time(file_time[0][-1], (root + 1) / 2 * nbin0 * tsamp + file_time[0][:-1].sum() - delay, scale='local') stt_sec = stt_time_test.second stt_date = stt_time_test.date info['phase0'] = phase_start ncoeff_freq = 10 phase_tmp = np.zeros([ncoeff_freq, args.ncoeff + 2]) disp_tmp = np.zeros(ncoeff_freq) cheby = nc.chebpts1(ncoeff_freq) freqy = (cheby + 1) / 2 * bandwidth + freq_start for i in np.arange(ncoeff_freq): timing_test = pm.psr_timing(psr, times_test, freqy[i])
params.general.nprocs, initializer=init_pacal_thread) pmap = params.general.process_pool.map else: pmap = list_map return pmap if __name__ == "__main__": from pacal import * import time import numpy.polynomial.chebyshev as ch c = array([1, 2, 3, 1]) CT = cheb1companion(array([1, 2, 3, 1])) print(CT) print(chebroots(c)) print(ch.chebroots(c)) print(chebroots(c) - ch.chebroots(c)) 0 / 0 #print taylor_coeff(lambda x:exp(x), 30) N = NormalDistr() fun = N.mgf() #fun.plot() t0 = time.time() t_i = taylor_coeff(fun, 100) print(time.time() - t0) sil = 1 t0 = time.time() for i in range(50): if i == 0: sil = 1
raise RuntimeError("Process pool not initialized") pmap = params.general.process_pool.map else: pmap = map return pmap if __name__ == "__main__": from pacal import * import time import numpy.polynomial.chebyshev as ch c = array([1, 2, 3, 1]) CT =cheb1companion(array([1, 2, 3, 1])) print CT print chebroots(c) print ch.chebroots(c) print chebroots(c) - ch.chebroots(c) 0/0 #print taylor_coeff(lambda x:exp(x), 30) N = NormalDistr() fun = N.mgf() #fun.plot() t0 = time.time() t_i = taylor_coeff(fun, 100) print time.time()-t0 sil=1 t0 = time.time() for i in range(50): if i==0: sil=1