def iteration_checker(size, test_num, val_range, res_opt=0): for i in range(0, test_num): (a, b, x) = generate_linear_system(size, val_range) a_float = np.array(a, dtype=np.float64) b_float = np.array(b, dtype=np.float64) x1 = [] l, u = [], [] (x1, l, u, fra, frb, fr) = rdft.rdft_lu_solver_with_lu(a, b) x0 = x1 x1_after = np.array(x1) x1_after = iteration.iteration(fra, l, u, frb, x1_after, linalg.cond(fra)) x1_after = iteration.remove_imag(x1_after) (x2, pl, pu, swapped_a, swapped_b) = pp.solve(a_float, b_float) x3 = iteration.iteration(swapped_a, pl, pu, swapped_b, x2, linalg.cond(a_float)) #x2 = lib.lu_solver(a,b) #x2 = lib.direct_solver(a_float,b_float) #x4 = lib.direct_lu_solver(a,b) if res_opt == 1: print("---error---") print("cond:", linalg.cond(a)) print("[0] error", linalg.norm(b - a.dot(x0))) print("[1] error", linalg.norm(b - a.dot(x1))) #print("[2] error", linalg.norm(b - a.dot(x2))) #print("[3] error", linalg.norm(b - a.dot(x3))) elif res_opt == 2: print( str(linalg.cond(a)) + " " + str(linalg.norm(x - x0)) + " " + str(linalg.norm(x - x1_before)) + " " + str(linalg.norm(x - x1_after)) + " " + str(linalg.norm(x - x5)) + " " + str(linalg.norm(x - x2)) + " " + str(linalg.norm(x - x3))) elif res_opt == 3: step = [] for j in x4_step: step.append(linalg.norm(x - j)) print( str(linalg.cond(a)) + " " + str(linalg.norm(x - x0)) + " " + str(linalg.norm(x - x1_before)) + " " + str(linalg.norm(x - x1_after)) + " " + str(step) + " " + str(linalg.norm(x - x2))) # partial pivot only elif res_opt == 4: print( str(linalg.cond(a)) + " " + str(linalg.norm(x - x0)) + " " + str(linalg.norm(x - x2)) + " " + str(linalg.norm(x - x4))) # partial pivot only elif res_opt != 1: print("---error---") print("cond:", linalg.cond(a)) print("[0] error", linalg.norm(x - x0)) print("[1] error", linalg.norm(x - x1_before)) # remove_imag => iteration print("[2] error", linalg.norm(x - x1_after)) # iteration => remove_imag print("[3] error", linalg.norm(x - x2))
def iteration_checker(size, test_num, val_range, res_opt=0): for i in range(0,test_num): (a,b,x) = generate_linear_system(size, val_range) a_float = np.array(a,dtype=np.float64) b_float = np.array(b,dtype=np.float64) x1 = [] l,u = [], [] (x1, l, u, fra, frb, fr) = rdft.rdft_lu_solver_with_lu(a,b) x0 = x1 x1_after = np.array(x1) x1_after = iteration.iteration(fra, l, u, frb, x1_after, linalg.cond(fra)) x1_after = iteration.remove_imag(x1_after) (x2, pl, pu, swapped_a, swapped_b) = pp.solve(a_float,b_float) x3 = iteration.iteration(swapped_a, pl, pu, swapped_b, x2, linalg.cond(a_float)) #x2 = lib.lu_solver(a,b) #x2 = lib.direct_solver(a_float,b_float) #x4 = lib.direct_lu_solver(a,b) if res_opt == 1: print("---error---") print("cond:", linalg.cond(a)) print("[0] error", linalg.norm(b - a.dot(x0))) print("[1] error", linalg.norm(b - a.dot(x1))) #print("[2] error", linalg.norm(b - a.dot(x2))) #print("[3] error", linalg.norm(b - a.dot(x3))) elif res_opt == 2: print(str(linalg.cond(a)) + " " + str(linalg.norm(x - x0)) + " " + str(linalg.norm(x - x1_before)) + " " + str(linalg.norm(x - x1_after)) + " " + str(linalg.norm(x - x5)) + " " + str(linalg.norm(x - x2)) + " " + str(linalg.norm(x - x3))) elif res_opt == 3: step = [] for j in x4_step: step.append(linalg.norm(x - j)) print(str(linalg.cond(a)) + " " + str(linalg.norm(x - x0)) + " " + str(linalg.norm(x - x1_before)) + " " + str(linalg.norm(x - x1_after)) + " " + str(step) + " " + str(linalg.norm(x - x2))) # partial pivot only elif res_opt == 4: print(str(linalg.cond(a)) + " " + str(linalg.norm(x - x0)) + " " + str(linalg.norm(x - x2)) + " " + str(linalg.norm(x - x4))) # partial pivot only elif res_opt != 1: print("---error---") print("cond:", linalg.cond(a)) print("[0] error", linalg.norm(x - x0)) print("[1] error", linalg.norm(x - x1_before)) # remove_imag => iteration print("[2] error", linalg.norm(x - x1_after)) # iteration => remove_imag print("[3] error", linalg.norm(x - x2))
def solve_rdft(a, b, x): (size, _) = a.shape f = rdft.generate_f(size) r = rdft.generate_r(size) fr = f.dot(r) ra = r.dot(a) (x1, l, u, fra, frb, _) = rdft.rdft_lu_solver_with_lu(a, b, r) x2 = np.array(x1) x2 = iteration.iteration(fra, l, u, frb, x2, linalg.cond(fra)) x2 = iteration.remove_imag(x2) return (x2, fra, fr, ra, x1)
def solve_rdft(a,b,x): (size,_) = a.shape f = rdft.generate_f(size) r = rdft.generate_r(size) fr = f.dot(r) ra = r.dot(a) (x1, l, u, fra, frb, _) = rdft.rdft_lu_solver_with_lu(a,b,r) x2 = np.array(x1) x2 = iteration.iteration(fra, l, u, frb, x2, linalg.cond(fra)) x2 = iteration.remove_imag(x2) return (x2, fra, fr, ra, x1)
def main(): from procedural_city_generation.roadmap.config import config from copy import copy singleton = config() front = copy(singleton.global_lists.vertex_list) front.pop(0) front.pop() vertex_queue = copy(singleton.global_lists.vertex_queue) from iteration import iteration singleton.iterationszaehler = 0 if singleton.plot == 1: if gui is None: import matplotlib.pyplot as plt plt.close() fig = plt.figure() ax = plt.subplot(111) fig.canvas.draw() ax.set_xlim((-singleton.border[0], singleton.border[0])) ax.set_ylim((-singleton.border[1], singleton.border[1])) else: gui.set_xlim((-singleton.border[0], singleton.border[0])) gui.set_ylim((-singleton.border[1], singleton.border[1])) i = 0 while (front != [] or singleton.global_lists.vertex_queue != []): i += 1 front = iteration(front) if singleton.plot == 1: if i % singleton.plot_counter == 0: if gui is None: plt.pause(0.001) fig.canvas.blit(ax.bbox) fig.canvas.flush_events() else: gui.update() singleton.iterationszaehler = 0 from procedural_city_generation.additional_stuff.jsontools import save_vertexlist print "Roadmap is complete" # if singleton.plot==1: # plt.show() save_vertexlist(singleton.global_lists.vertex_list, "output", singleton.savefig) return 0
def main(): from procedural_city_generation.roadmap.config import config from copy import copy singleton=config() front=copy(singleton.global_lists.vertex_list) front.pop(0) front.pop() vertex_queue = copy(singleton.global_lists.vertex_queue) from iteration import iteration singleton.iterationszaehler=0 if singleton.plot==1: import matplotlib.pyplot as plt plt.close() fig=plt.figure() ax=plt.subplot(111) fig.canvas.draw() ax.set_xlim((-singleton.border[0],singleton.border[0])) ax.set_ylim((-singleton.border[1],singleton.border[1])) i=0 while (front!=[] or singleton.global_lists.vertex_queue !=[]): i+=1 front=iteration(front) if singleton.plot==1: if i%singleton.plotabstand==0: plt.pause(0.001) fig.canvas.blit(ax.bbox) fig.canvas.flush_events() singleton.iterationszaehler=0 from procedural_city_generation.additional_stuff.jsontools import save_vertexlist print "Roadmap is complete" if singleton.plot==1: plt.show() save_vertexlist(singleton.global_lists.vertex_list,"output",singleton.savefig) singleton.kill() return 0
# question 1 def g(x): return (math.sin(math.pi * x) -x**2)/(2.0 * math.pi) + x def f(x): return math.sin(math.pi * x) - x**2 # tolerances and number of iterations tol_err = 1e-5 tol_res = 1e-5 itmax = 20 # try different initial points x0 = 1.0 print("Trying initial point %f..." % (x0)) x,err,res = iteration(f, g, x0, tol_err, tol_res, itmax) x0 = 0.0 print("Trying initial point %f..." % (x0)) x,err,res = iteration(f, g, x0, tol_err, tol_res, itmax) x0 = -0.1 print("Trying initial point %f..." % (x0)) x,err,res = iteration(f, g, x0, tol_err, tol_res, itmax) # in the final run, the approximate solutions approaches minus infinity... # now call the recursively programmed function x0 = 1.0 print("Trying initial point %f with recursion..." % (x0)) x,err,res = recursion(f, g, x0, tol_err, tol_res, itmax)
from config import config from iteration import iteration from eta import eta import matplotlib.pyplot as plt def set_up(): import numpy as np config['delta_r'] = config['r_range'] / config['N'] delta_k = np.pi / (config['N'] * config['delta_r']) beta = np.reciprocal(config['T'] * config['kb']) config['delta_k'] = delta_k config['beta'] = beta if __name__ == '__main__': set_up() eta = eta() h_i = iteration(eta) gr = h_i + 1. plt.plot(eta.r, gr) plt.show()
def turboTop(resFile, rate = 12, it = 5, n = 4, r = 5, p = 48, d = 0, mu = 8, sigma = 6, l = 20, m = 10, q = 8): """ Turbo decoder top level. resFile -- files where the results will be saved rate -- code rate (e.g. 12 for rate 1/2) it -- number of iterations for the turbo decoding n -- number of bits for the sampling of the signals - a, b, y1, y2 r -- number of bits for the coding of the extrinsic information p -- interleaver frame size in bit couples d -- additional delay through the noiser - 0 means the noiser adds 2 clock cycles mu -- mean value for the noise distribution (additive noise) sigma -- standard deviation of the noise distribution (0 means no noise) l -- first trellis' length m -- second trellis' length q -- number of bits for the coding of the accumulated distances """ # Signal declarations: clk = Signal(bool(0)) rst = Signal(bool(1)) flipflop = Signal(bool(0)) aClean = Signal(bool(0)) bClean = Signal(bool(0)) y1Clean = Signal(bool(0)) y2Clean = Signal(bool(0)) aCleanDel = Signal(bool(0)) bCleanDel = Signal(bool(0)) y1CleanDel = Signal(bool(0)) y2CleanDel = Signal(bool(0)) y1IntDel = Signal(bool(0)) y2IntDel = Signal(bool(0)) aNoisy = Signal(int(0)) bNoisy = Signal(int(0)) y1Noisy = Signal(int(0)) y2Noisy = Signal(int(0)) y1IntNoisy = Signal(int(0)) y2IntNoisy = Signal(int(0)) y1Full = Signal(intbv(0, -(2**(n-1)), 2**(n-1))) y2Full = Signal(intbv(0, -(2**(n-1)), 2**(n-1))) y1IntFull = Signal(intbv(0, -(2**(n-1)), 2**(n-1))) y2IntFull = Signal(intbv(0, -(2**(n-1)), 2**(n-1))) aLim = [Signal(intbv(0, -(2**(n-1)), 2**(n-1))) for i in range(it + 1)] bLim = [Signal(intbv(0, -(2**(n-1)), 2**(n-1))) for i in range(it + 1)] y1Lim = [Signal(intbv(0, -(2**(n-1)), 2**(n-1))) for i in range(it + 1)] y2Lim = [Signal(intbv(0, -(2**(n-1)), 2**(n-1))) for i in range(it + 1)] y1IntLim = [Signal(intbv(0, -(2**(n-1)), 2**(n-1))) for i in range(it + 1)] y2IntLim = [Signal(intbv(0, -(2**(n-1)), 2**(n-1))) for i in range(it + 1)] z = [[Signal(intbv(0, 0, 2**r)) for i in range(4)] for i in range(it + 1)] zSorted = [Signal(intbv(0, 0, 2**r)) for i in range(4)] aDec = [Signal(bool(0)) for i in range(it + 1)] bDec = [Signal(bool(0)) for i in range(it + 1)] aDel = [Signal(bool(0)) for i in range(it + 1)] bDel = [Signal(bool(0)) for i in range(it + 1)] abInt = [Signal(bool(0)) for i in range(2)] y1Int = Signal(bool(0)) y2Int = Signal(bool(0)) abCleanPerm = [Signal(bool(0)) for i in range(2)] delayer_ia = [None for i in range(it + 1)] delayer_ib = [None for i in range(it + 1)] ber_i = [None for i in range(it + 1)] iteration_i = [None for i in range(it)] # Reset and clock generation: rstGen_i0 = rstGen(rst) clkGen_i0 = clkGen(clk) # Random data generation: randGen_i0 = randGen(clk, rst, aClean, bClean) # Interleaving and permuting: clkDiv_i0 = clkDiv(clk, rst, flipflop) abPermut_i0 = abPermut(flipflop, aClean, bClean, abCleanPerm, 1) interleaver_i0 = interleaver(clk, rst, abCleanPerm, abInt, p, 0, 0, 2, 2, 0) # Coder: coder_i0 = coder(clk, rst, aClean, bClean, y1Clean, y2Clean) coder_i1 = coder(clk, rst, abInt[1], abInt[0], y1Int, y2Int) # Additional delay through the channel: delayer_i0 = delayer(clk, rst, aClean, aCleanDel, d, 0, 2) delayer_i1 = delayer(clk, rst, bClean, bCleanDel, d, 0, 2) delayer_i2 = delayer(clk, rst, y1Clean, y1CleanDel, d, 0, 2) delayer_i3 = delayer(clk, rst, y2Clean, y2CleanDel, d, 0, 2) delayer_i4 = delayer(clk, rst, y1Int, y1IntDel, d, 0, 2) delayer_i5 = delayer(clk, rst, y2Int, y2IntDel, d, 0, 2) # Channel noiser: noiser_i0 = noiser(clk, rst, aCleanDel, bCleanDel, y1CleanDel, y2CleanDel, y1IntDel, y2IntDel, aNoisy, bNoisy, y1Noisy, y2Noisy, y1IntNoisy, y2IntNoisy, n, mu, sigma) # Decoder: limiter_i0 = limiter(aNoisy, bNoisy, y1Noisy, y2Noisy, y1IntNoisy, y2IntNoisy, aLim[0], bLim[0], y1Full, y2Full, y1IntFull, y2IntFull, n) punct_i0 = punct(clk, rst, y1Full, y2Full, y1IntFull, y2IntFull, y1Lim[0], y2Lim[0], y1IntLim[0], y2IntLim[0], rate) for i in range(it): iteration_i[i] = iteration(clk, rst, flipflop, aLim[i], bLim[i], y1Lim[i], y2Lim[i], y1IntLim[i], y2IntLim[i], z[i], z[i + 1], aDec[i + 1], bDec[i + 1], aLim[i + 1], bLim[i + 1], y1Lim[i + 1], y2Lim[i + 1], y1IntLim[i + 1], y2IntLim[i + 1], l, m, q, p, r, n, 2 * i * (l + m + 2) + 2 * i * p + 2) # Bit Error Rate monitoring: siho_i0 = siho(aLim[0], bLim[0], aDec[0], bDec[0]) delayer_ia[0] = delayer(clk, rst, aClean, aDel[0], 1 + d, 0, 2) delayer_ib[0] = delayer(clk, rst, bClean, bDel[0], 1 + d, 0, 2) ber_i[0] = ber(clk, rst, aDel[0], bDel[0], aDec[0], bDec[0], d + 100, resFile[0]) for i in range(it): delayer_ia[i + 1] = delayer(clk, rst, aClean, aDel[i + 1], (2 * i + 1) * (l + m + 2) + 2 * i * p + d, 0, 2) delayer_ib[i + 1] = delayer(clk, rst, bClean, bDel[i + 1], (2 * i + 1) * (l + m + 2) + 2 * i * p + d, 0, 2) ber_i[i + 1] = ber(clk, rst, aDel[i + 1], bDel[i + 1], aDec[i + 1], bDec[i + 1], (2 * i + 1) * (l + m + 2) + 2 * i * p + d + 100, resFile[i + 1]) return rstGen_i0, clkGen_i0, randGen_i0, clkDiv_i0, abPermut_i0, interleaver_i0, coder_i0, coder_i1, delayer_i0, delayer_i1, delayer_i2, delayer_i3, delayer_i4, delayer_i5, noiser_i0, limiter_i0, punct_i0, siho_i0, delayer_ia, delayer_ib, ber_i, iteration_i
""" Assignment 1 Question 1 test script for both iteration.py and recursion.py Author: Hassan Tariq, 100657119 """ from iteration import iteration from recursion import recursion import math pi = math.pi # Define both function def f(x): return math.sin(pi*x) - x*x # Define derivative of function def g(x): return 1/(2*math.pi) * math.sin(math.pi*x) - 1/(2*math.pi) * x**2 + x #Initialize variables x0 = 1 tolx = tolf = 1e-6 kmax = 50 print("\n\tIterative solution for question 1") iteration(f, g, x0, kmax, tolx, tolf) # Initialize extra recursion counter variable i = 0 print("\n\n\tRecursive solution for question 1") recursion(f, g, x0, tolx, tolf, kmax, i)
.getOrCreate() import create as tc import os_file as to import iteration as it from pai_pyhdfs import wait_hdfs_file, hdfs_save f = wait_hdfs_file(work_path, 'distance_matrix.npy', delete=False) m = np.load(f, allow_pickle=True) #save distance_matrix in m. def get_res(s1, s2): if tc.cost(s1, m) < tc.cost(s2, m): return s1 else: return s2 while True: f = wait_hdfs_file(work_path, 'generations.npy', delete=False) generations = np.load(f) print("generation information getted. Now setting the gen_rdd.") gen_rdd = spark.sparkContext.parallelize(generations) print("rdd setted,now maping.") res = gen_rdd.map(lambda x: it.iteration(x, m)) print("mapping over,now reducing.") res2 = res.reduce(get_res) print("The best solution is:", res2) c = tc.cost(res2, m) print("The lowest cost is :", c) hdfs_save(work_path, 'final_solution.npy', [res2, c])