def __init__(self): self.best_E = 1.0e100 self.second_E = 1.0e100 self.third_E = 1.0e100 self.best_route = numx.arange(n_cities) self.second_route = numx.arange(n_cities) self.third_route = numx.arange(n_cities) self.tsp = Tsp() self.tsp.SetNCities(n_cities) self.r_cities = numx.arange(n_cities) self.__runs = 0 self.__total_runs = 68588312 self.__time_stamp = 0
def search(self): initial_route = numx.arange(n_cities) self.__time_stamp = int(time.time()) sys.stdout.write(" " * 79 + "\n") self.do_all_perms(initial_route, 1) print " " * 78 print "\n" print "Initial route: " for i in initial_route: print cities[i] print "Required %d runs estimated %d runs" % (self.__runs, self.__total_runs) print print "Best route: " for i in self.best_route: print cities[i] print print "Second route: " for i in self.second_route: print cities[i] print print "Third route: " for i in self.third_route: print cities[i]
def run(): # Here should go some meaningful data sample to test the transform. # Can you write one? n = 512 data = numx.zeros((n,)) x = numx.arange(n) * (2 * numx.pi / n) y = numx.cos(x*5) #result = transform(y) wavefreq, resampled = compress(y) pylab.figure(1) pylab.hold(0) pylab.plot(x,y,x,resampled) pylab.grid(1) pylab.figure(2) pylab.hold(0) pylab.plot(wavefreq[:30], '.-') pylab.grid(1) pylab.figure(3) pylab.hold(0) pylab.plot(wavefreq[:50], '.-') pylab.grid(1)
def run(): # Here should go some meaningful data sample to test the transform. # Can you write one? n = 512 data = numx.zeros((n, )) x = numx.arange(n) * (2 * numx.pi / n) y = numx.cos(x * 5) #result = transform(y) wavefreq, resampled = compress(y) pylab.figure(1) pylab.hold(0) pylab.plot(x, y, x, resampled) pylab.grid(1) pylab.figure(2) pylab.hold(0) pylab.plot(wavefreq[:30], '.-') pylab.grid(1) pylab.figure(3) pylab.hold(0) pylab.plot(wavefreq[:50], '.-') pylab.grid(1)
def generate_data(): r = pygsl.rng.mt19937() a = numx.arange(20) / 10.# + .1 y0 = numx.exp(a) sigma = 0.1 * y0 dy = numx.array(map(r.gaussian, sigma)) return a, y0+dy, sigma
def run_driver(): mu = 10 sys = odeiv2.system(func, jac, 2, mu) #sys = odeiv2.system(func, None, 2, mu) T = odeiv2.step_rk8pd pygsl.set_debug_level(0) d = driver_y_new(sys, T, 1e-6, 1e-6, 0.0); d.reset() d.reset_hstart(2) d.set_hmin(0.0) d.set_hmax(1.0) d.set_nmax(2) d.set_nmax(0) d.set_hmax(1e-6) d.set_hmin(2e-6) y = numx.array([1.0, 0.0]) N = 100 t = 0.0 t1 = N * 1. ti = (numx.arange(N) +1) / float(N) pygsl.set_debug_level(0) for tnext in ti: #pygsl.set_debug_level(3) t, y = d.apply(t, tnext, y) print(t, y)
def setUp(self): t = Numeric.arange(self._getn()) y = testfunc(t, self.A, self.lambda_, self.b) sigma = Numeric.ones(self._getn()) * 0.1 self.data = Numeric.array((t, y, sigma), Float) self.sys = multifit_nlin.gsl_multifit_function_fdf( exp_f, exp_df, exp_fdf, self.data, self._getn(), self._getp())
def run(): # The geometric function func = lambda x,y : 1 / (1 + x) mysys = chebyshev.gsl_function(func, None) # The series series = chebyshev.cheb_series(6) series.init(mysys, 0, 2) # and its coefficients # Make a new series object. series1 = chebyshev.cheb_series(6) #series1.init(mysys, 0, 2) series1.set_a(series.get_a()) series1.set_b(series.get_b()) series1.set_coefficients(series.get_coefficients()*2) # Two items of the gsl_cheb_struct the documentation does not mention. series1.set_order_sp(series.get_order_sp()) series1.set_f(series.get_f()) x = numx.arange(100) / 50. y = map(lambda x, s=series: s.eval_n(6, x), x) y1 = map(lambda x, s=series1: s.eval_n(6, x), x)
def setUp(self): x = Numeric.arange(100) y = 2 * x + 1 self.x = x self.y1 = y[:80] self.interp = self._testtype(100) self.interp.init(x,y)
def real_example_simple1(): n = 32 data = numx.cos(1 * (numx.arange(n) * (2 * numx.pi / n))).astype( numx.Float32) print(data) result = fft.real_transform_float(data) print(result)
def testCosSpace(self): x = numx.arange(self.n) * (2 * numx.pi / self.n) space = self.workspace(self.n) assert(space.get_n() == self.n) table = self.wavetable(self.n) assert(table.get_n() == self.n) for i in range(1,self.n/2): self.CosOne(x,i, (space,table))
def testCosSpace(self): x = numx.arange(self.n) * (2 * numx.pi / self.n) space = self.workspace(self.n) self.assertEqual(space.get_n(), self.n) table = self.wavetable(self.n) self.assertEqual(table.get_n(), self.n) for i in range(1, self._GetN2()): self.CosOne(x, i, (space, table))
def halfcomplex_radix2_unpack(): n = 32 a = numx.arange(n) c = numx.zeros((n, )) c[1:n / 2 + 1] = a[1::2] c[n / 2 + 1:] = a[-2:1:-2] print(c) print(fft.halfcomplex_radix2_unpack(c))
def halfcomplex_radix2_unpack(): n = 32 a = numx.arange(n) c = numx.zeros((n,)) c[1:n/2+1]=a[1::2] c[n/2+1:]=a[-2:1:-2] print c print fft.halfcomplex_radix2_unpack(c)
def testCosSpace(self): x = numx.arange(self.n) * (2 * numx.pi / self.n) space = self.workspace(self.n) assert (space.get_n() == self.n) table = self.wavetable(self.n) assert (table.get_n() == self.n) for i in range(1, self.n / 2): self.CosOne(x, i, (space, table))
def generate_data(): r = pygsl.rng.mt19937() a = numx.arange(20) / 10. # + .1 y0 = numx.exp(a) sigma = 0.1 * y0 tmp = tuple(map(r.gaussian, sigma)) dy = numx.array(tmp) tmp = y0 + dy return a, tmp, sigma
def test_dirichlet(self): a = Numeric.arange(10) * .1 + .1 d = self.rng.dirichlet(a) array_check(d, Float, (a.shape[0], )) ra = Numeric.reshape(a, (a.shape[0], -1)) ra = Numeric.transpose(ra) p = rngmodule.dirichlet_pdf(d, ra) d = self.rng.dirichlet(a, 100) array_check(d, Float, (100, a.shape[0]))
def testCos(self): x = numx.arange(self.n) * (2 * numx.pi / self.n) for i in range(1,self.n/2): if self.__class__.__name__ == "testrealforwardfloat": pygsl.set_debug_level(0) try: self.CosOne(x,i) finally: pygsl.set_debug_level(0)
def testSinReturnSaveSpaces(self): space = self.workspace(self.n) table = self.wavetable(self.n) x = numx.arange(self.n) * ((2+0j) * numx.pi / self.n) for i in range(1,self.n/2): y = numx.sin(x * i) tmp = self.convert(y) f = self.transform(tmp, space, table, tmp) self._CheckSinResult(f, i)
def testCos(self): x = numx.arange(self.n) * (2 * numx.pi / self.n) for i in range(1, self.n / 2): if self.__class__.__name__ == "testrealforwardfloat": pygsl.set_debug_level(0) try: self.CosOne(x, i) finally: pygsl.set_debug_level(0)
def test_dirichlet(self): a = Numeric.arange(10) * 0.1 + 0.1 d = self.rng.dirichlet(a) array_check(d, Float, (a.shape[0],)) ra = Numeric.reshape(a, (a.shape[0], -1)) ra = Numeric.transpose(ra) p = rngmodule.dirichlet_pdf(d, ra) d = self.rng.dirichlet(a, 100) array_check(d, Float, (100, a.shape[0]))
def testSinReturnSaveSpaces(self): space = self.workspace(self.n) table = self.wavetable(self.n) x = numx.arange(self.n) * ((2 + 0j) * numx.pi / self.n) for i in range(1, self.n / 2): y = numx.sin(x * i) tmp = self.convert(y) f = self.transform(tmp, space, table, tmp) self._CheckSinResult(f, i)
def setUp(self): t = Numeric.arange(self._getn()); y = testfunc(t, self.A, self.lambda_, self.b) sigma = Numeric.ones(self._getn()) * 0.1 self.data = Numeric.array((t,y,sigma), Float) self.sys = multifit_nlin.gsl_multifit_function_fdf(exp_f, exp_df, exp_fdf, self.data, self._getn(), self._getp())
def setUp(self): self.dim = 100 self.param = 2 self.a = 1000.1 self.b = 100 self.x = Numeric.arange(self.dim) x = self.x self.y = self.a + self.b * self.x self.w = Numeric.ones((self.dim,)) self.ws = multifit.linear_workspace(self.dim, self.param) self.X = Numeric.transpose(Numeric.array((Numeric.ones(self.dim,), x)))
def setUp(self): self.dim = 100 self.param = 2 self.a = 1000.1 self.b = 100 self.x = Numeric.arange(self.dim) x = self.x self.y = self.a + self.b * self.x self.w = Numeric.ones((self.dim, )) self.ws = multifit.linear_workspace(self.dim, self.param) self.X = Numeric.transpose(Numeric.array( (Numeric.ones(self.dim, ), x)))
def complex_example_simple(): n = 16 data = numx.arange(n) * (2 * numx.pi / n) data = numx.cos(data) +0j space = fft.complex_workspace(n) wavetable = fft.complex_wavetable(n) print data #data[:11] = 1.0 #data[11:] = 1.0 tmp = fft.complex_backward(data, space, wavetable) print "tmp", tmp
def complex_example_simple(): n = 16 data = numx.arange(n) * (2 * numx.pi / n) data = numx.cos(data) + 0j space = fft.complex_workspace(n) wavetable = fft.complex_wavetable(n) print(data) #data[:11] = 1.0 #data[11:] = 1.0 pygsl.init.set_debug_level(0) tmp = fft.complex_backward(data, space, wavetable) print("tmp", tmp)
def run_fdfsolver(): A = 1. lambda_ = .1 b = .5 n = 40 p = 3 t = numx.arange(n); y = testfunc(t, A, lambda_, b) sigma = numx.ones(n) * 0.1 data = numx.array((t,y,sigma), numx.Float) #mysys = multifit_nlin.gsl_multifit_function_fdf(exp_f, exp_df, exp_fdf, # data, n,p) pygsl.set_debug_level(0) solver = multifit_nlin.lmsder(n, p) pygsl.set_debug_level(0) #solver = multifit_nlin.lmder(mysys, n, p) x = numx.array((1.0, 0.0, 0.0)) pygsl.set_debug_level(0) solver.set(exp_f, exp_df, exp_fdf, x, data) pygsl.set_debug_level(0) print "# Testing solver ", solver.name() print "# %5s %9s %9s %9s %10s" % ("iter", "A", "lambda", "b", "|f(x)|") for iter in range(20): status = solver.iterate() x = solver.x() dx = solver.dx() f = solver.f() J = solver.J() #tdx = multifit_nlin.gradient(J, f) #status = multifit_nlin.test_delta(dx, x, 1e-8, 1e-8) status = solver.test_delta(1e-8, 1e-8) fn = numx.sqrt(numx.sum(f*f)) if status == 0: print "# Convereged :" if status == 0: break print " %5d % .7f % .7f % .7f % .7f" %(iter, x[0], x[1], x[2], fn) else: raise ValueError, "Number of Iterations exceeded!" J = solver.J() covar = multifit_nlin.covar(solver.J(), 0.0) print "# A = % .5f +/- % .5f" % (x[0], covar[0,0]) print "# lambda = % .5f +/- % .5f" % (x[1], covar[1,1]) print "# b = % .5f +/- % .5f" % (x[2], covar[2,2])
def run(): N = 1024 x = numx.arange(N) * (numx.pi * 2 / N) y = numx.sin(x) b = bspline(10, nbreak) b.knots_uniform(x[0], x[-1]) X = numx.zeros((N, ncoeffs)) X = b.eval_vector(x) c, cov, chisq = multifit.linear(X, y) res_x = x[::N / 64] X = b.eval_vector(res_x) res_y, res_y_err = multifit.linear_est_matrix(X, c, cov) pylab.plot(x, y, '-') pylab.errorbar(res_x, res_y, fmt='g-', xerr=res_y_err)
def run_fdfsolver(): A = 1. lambda_ = .1 b = .5 n = 40 p = 3 t = numx.arange(n) y = testfunc(t, A, lambda_, b) sigma = numx.ones(n) * 0.1 data = numx.array((t, y, sigma), ) mysys = multifit_nlin.gsl_multifit_function_fdf(exp_f, exp_df, exp_fdf, data, n, p) solver = multifit_nlin.lmsder(mysys, n, p) #solver = multifit_nlin.lmder(mysys, n, p) x = numx.array((1.0, 0.0, 0.0)) solver.set(x) print("# Testing solver ", solver.name()) print("# %5s %9s %9s %9s %10s" % ("iter", "A", "lambda", "b", "|f(x)|")) for iter in range(20): status = solver.iterate() x = solver.getx() dx = solver.getdx() f = solver.getf() J = solver.getJ() tdx = multifit_nlin.gradient(J, f) status = multifit_nlin.test_delta(dx, x, 1e-8, 1e-8) fn = numx.sqrt(numx.sum(f * f)) if status == errno.GSL_SUCCESS: print("# Convereged :") if status == errno.GSL_SUCCESS: break print(" %5d % .7f % .7f % .7f % .7f" % (iter, x[0], x[1], x[2], fn)) else: raise ValueError("Number of Iterations exceeded!") J = solver.getJ() covar = multifit_nlin.covar(solver.getJ(), 0.0) print("# A = % .5f +/- % .5f" % (x[0], covar[0, 0])) print("# lambda = % .5f +/- % .5f" % (x[1], covar[1, 1])) print("# b = % .5f +/- % .5f" % (x[2], covar[2, 2]))
def run(): N = 1024 x = numx.arange(N) * (numx.pi * 2 / N) y = numx.sin(x) b = bspline(10, nbreak) b.knots_uniform(x[0], x[-1]) X = numx.zeros((N, ncoeffs)) X = b.eval_vector(x) c, cov, chisq = multifit.linear(X, y) res_x = x[::N/64] X = b.eval_vector(res_x) res_y, res_y_err = multifit.linear_est_matrix(X, c, cov) pylab.plot(x,y, '-') pylab.errorbar(res_x, res_y, fmt='g-', xerr=res_y_err)
def siman_exp(): prepare_distance_matrix() # set up a trivial initial route */ print("# initial order of cities:\n") for city in cities: print city #print " # distance matrix is:" #print_distance_matrix(); mytsp = Tsp() mytsp.SetRoute(numx.arange(n_cities)) mytsp.SetNCities(n_cities) result = siman.solve(rng.rng(), mytsp) route = result.GetRoute() print("# final order of cities:\n") for i in route: print cities[i]
def run(): r = rng.rng() # slope a = 1.45 # intercept b = 3.88 # Generate the data n = 20 i = numx.arange(n - 3) dx = 10.0 / (n - 1.0) e = r.uniform(n) x = -5.0 + i * dx y = a * x + b #outliers xo = numx.array([4.1, 3.5, 4.7]) yo = numx.array([-6.0, -6.7, -8.3]) x = numx.concatenate((x, xo)) y = numx.concatenate((y, yo)) X = numx.array([x * 0 + 1, x]).transpose() ws = fr.workspace(*((fr.ols, ) + X.shape)) c_ols, cov_ols = ws.fit(X, y) ws = fr.workspace(*((fr.bisquare, ) + X.shape)) c, cov = ws.fit(X, y) y_rob = numx.zeros(n, numx.float_) y_ols = numx.zeros(n, numx.float_) yerr_rob = numx.zeros(n, numx.float_) yerr_ols = numx.zeros(n, numx.float_) pygsl.set_debug_level(0) fr.est(X[0, :], c, cov) y_rob, yerr_rob = fr.est_vector(X, c, cov) pygsl.set_debug_level(0) y_ols, yerr_ols = fr.est_vector(X, c_ols, cov_ols) return x, y, (y_rob, yerr_rob), (y_ols, yerr_ols)
def run(): N = 1024 x = numx.arange(N) * (numx.pi * 2 / N) y = numx.sin(x) b = bspline(4, nbreak) k = b.get_internal_knots() pygsl.set_debug_level(10) b.knots(k) X = b.eval_vector(x) c, cov, chisq = multifit.linear(X, y) b.set_coefficients_and_covariance_matrix(c, cov) res_x = x[::N / 64] res_y, res_y_err = b.eval_dep_yerr_vector(res_x) #res_y = b.eval_dep_vector(res_x) print(res_y) pylab.plot(x, y, '-') pylab.errorbar(res_x, res_y, fmt='g-', xerr=res_y_err)
def run(): N = 1024 x = numx.arange(N) * (numx.pi * 2 / N) y = numx.sin(x) b = bspline(4, nbreak) k = b.get_internal_knots() pygsl.set_debug_level(10) b.knots(k) X = b.eval_vector(x) c, cov, chisq = multifit.linear(X, y) b.set_coefficients_and_covariance_matrix(c, cov) res_x = x[:: N / 64] res_y, res_y_err = b.eval_dep_yerr_vector(res_x) # res_y = b.eval_dep_vector(res_x) print res_y pylab.plot(x, y, "-") pylab.errorbar(res_x, res_y, fmt="g-", xerr=res_y_err)
def run(): r = rng.rng() bw = bspline(4, nbreak) # Data to be fitted x = 15. / (N - 1) * numx.arange(N) y = numx.cos(x) * numx.exp(0.1 * x) sigma = .1 w = 1.0 / sigma**2 * numx.ones(N) dy = r.gaussian(sigma, N) y = y + dy # use uniform breakpoints on [0, 15] bw.knots_uniform(0.0, 15.0) X = numx.zeros((N, ncoeffs)) for i in range(N): B = bw.eval(x[i]) X[i, :] = B # do the fit c, cov, chisq = multifit.wlinear(X, w, y, multifit.linear_workspace(N, ncoeffs)) # output the smoothed curve res_y = [] res_y_err = [] for i in range(N): B = bw.eval(x[i]) yi, yi_err = multifit.linear_est(B, c, cov) res_y.append(yi) res_y_err.append(yi_err) #print yi, yerr res_y = numx.array(res_y) res_y_err = numx.array(res_y_err) return ( x, y, ), (x, res_y), res_y_err
def run(): r = rng.rng() bw = bspline(4, nbreak) # Data to be fitted x = 15. / (N-1) * numx.arange(N) y = numx.cos(x) * numx.exp(0.1 * x) sigma = .1 w = 1.0 / sigma**2 * numx.ones(N) dy = r.gaussian(sigma, N) y = y + dy # use uniform breakpoints on [0, 15] bw.knots_uniform(0.0, 15.0) X = numx.zeros((N, ncoeffs)) for i in range(N): B = bw.eval(x[i]) X[i,:] = B # do the fit c, cov, chisq = multifit.wlinear(X, w, y, multifit.linear_workspace(N, ncoeffs)) # output the smoothed curve res_y = [] res_y_err = [] for i in range(N): B = bw.eval(x[i]) yi, yi_err = multifit.linear_est(B, c, cov) res_y.append(yi) res_y_err.append(yi_err) #print yi, yerr res_y = numx.array(res_y) res_y_err = numx.array(res_y_err) return (x, y,), (x, res_y), res_y_err
def real_example_simple1(): n = 32 data = numx.cos(1*(numx.arange(n) * (2 * numx.pi / n))).astype(numx.Float32) print data result = fft.real_transform_float(data) print result
def polydd(): xa = numx.arange(100) / 100.0 * 2. * numx.pi ya = numx.sin(xa) dd = poly.poly_dd(xa, ya) dd.eval(0) c = dd.taylor(0.0)
import pygsl def run(array): # Initalise the wavelet and the workspace w = wavelet.daubechies(4) ws = wavelet.workspace(len(array)) # Transform forward result = w.transform_forward(array, ws) # Select the largest 20 coefficients abscoeff = numx.absolute(result) indices = numx.argsort(abscoeff) # ascending order tmp = numx.zeros(result.shape, numx.float_) for i in indices[-20:]: tmp[i] = result[i] # Set all others to zero # And back result2 = w.transform_inverse(tmp, ws) #print result2 #print result2 - array if __name__ == '__main__': a = numx.arange(256) b = numx.sin(a*(2*numx.pi / 16.)) #b = a * 0.0 run(b)
def SetNCities(self, n_cities): self.n_cities = n_cities self.r_cities = numx.arange(n_cities)
def run(array): # Initalise the wavelet and the workspace w = wavelet.daubechies(4) ws = wavelet.workspace(len(array)) # Transform forward result = w.transform_forward(array, ws) # Select the largest 20 coefficients abscoeff = numx.absolute(result) indices = numx.argsort(abscoeff) # ascending order tmp = numx.zeros(result.shape, numx.float_) for i in indices[-20:]: tmp[i] = result[i] # Set all others to zero # And back result2 = w.transform_inverse(tmp, ws) #print result2 #print result2 - array if __name__ == '__main__': a = numx.arange(256) b = numx.sin(a * (2 * numx.pi / 16.)) #b = a * 0.0 run(b)
#!/usr/bin/env python from pygsl import hankel from pygsl import _numobj as numx a = numx.arange(1000) h = hankel.DiscreteHankelTransform(100) h.init(10,10) h.apply(a[:100]) #print "Finished!"
def setUp(self): xa = Numeric.arange(100) / 100.0 * 2. * Numeric.pi ya = Numeric.sin(xa) self.dd = poly.poly_dd(xa, ya)
def testGet(self): tmp = self.qrng() self.__testreturn(tmp, 1) for i in (Numeric.arange(10) + 1): tmp = self.qrng(i) self.__testreturn(tmp, i)
def testSin(self): x = numx.arange(self.n) * (2 * numx.pi / self.n) for i in range(1,self.n/2): self.SinOne(x,i)
def setUp(self): self.a = 0.0 self.b = 100 self.x = Numeric.arange(100) self.y = self.a + self.b * self.x self.w = Numeric.ones(100) * 1000000
The output of this script is designed to be displayed by the GNU plotutils 'graph' program. e.g $ python ./interpolation.py > interp.dat $ graph -T ps < interp.dat > interp.ps The result shows a smooth interpolation of the original points. The interpolation method can changed simply by varing spline. . """ from pygsl import spline, errors from pygsl import _numobj as numx print "#m=0,S=2" n = 10 a = numx.arange(n) x = a + 0.5 * numx.sin(a) y = a + numx.cos(a**2) for i in a: print x[i], y[i] print "#m=1,S=0" # Generation of the spline object ... Acceleration is handled internally myspline = spline.cspline(n) myspline = spline.linear(n) acc = myspline._object.get_accel_object() #print "Accel object", dir(acc) # initalise with the vector of the independent and the dependent myspline.init(x,y) x1 = numx.arange(n * 20) / 20. for xi in x1:
def testSin(self): x = numx.arange(self.n) * (2 * numx.pi / self.n) for i in range(1, self.n / 2): self.SinOne(x, i)