def gen_new_bdarylst(fres_1st,glob_fitness_fun,bdary_lst): for j in fres_1st: try: inp = list(j) except TypeError: inp = [j] idx = 0 for i in inp: if i <= bf.f64max: i = float(i) temp_inp = list(inp) if i > 0: temp_i = i - bf.getulp(i) * 1e12 else: temp_i = i + bf.getulp(i) * 1e12 if i == 0: temp_i = 1e-154 temp_inp[idx] = temp_i temp_res = glob_fitness_fun(temp_inp) if temp_res != 0: bdary_lst[idx].append(i) if i != 0.0: temp_i = -i temp_inp[idx] = temp_i temp_res = glob_fitness_fun(temp_inp) if temp_res == 0: bdary_lst[idx].append(-i) idx = idx + 1 return bdary_lst
def extract_noempty_bound(bound): mp.dps = 30 # bf.fpartition(bound) ret_lst = [] if bound[0]==0: ret_lst.append(bf.f64min) else: ret_lst.append(bound[0] + bf.getulp(bound[0])) if bound[1] == 0: ret_lst.append(bf.f64min) else: ret_lst.append(bound[1] - bf.getulp(bound[1])) fpart_bound = bf.fdistribution_partition(bound[0],bound[1]) a = int(max(len(fpart_bound)/10.0,1)) if a == 0: a = 1 for i in range(0,len(fpart_bound),a): ret_lst.append(random.uniform(fpart_bound[i][0],fpart_bound[i][1])) # step = [random.uniform(0,1) for i in range(0,10)] # mpf1 = mpf(bound[1]) # mpf0 = mpf(bound[0]) # distance = mpf1-mpf0 # for i in step: # ret_lst.append(float(mpf0+distance*i)) return ret_lst
def accuracy_condition(point, rf): der_tuple = [0]*len(point) point_condition = 0.0 for i in range(0,len(point)): temp_tuple = der_tuple temp_tuple[i] = 1 point_condition += diff(rf, tuple(point),tuple(temp_tuple))*bf.getulp(point[i]) ulp_point = bf.getulp(rf(*point)) point_condition = point_condition/ulp_point return math.fabs(point_condition)
def estimate_backward_error(inpdm,rf,pf): input_l = produce_n_input(inpdm, 5) back_err = [] for i in input_l: err = math.fabs((rf(*i)-pf(*i))/bf.getulp(rf(*i))) cod = accuracy_condition(i,rf) back_err.append(err/cod) return max(back_err)
def gen_new_inps(bdary_lst): temp_lst = [] inps_lst = [] for i in bdary_lst: temp_lst.append([]) idx = 0 special_inps = [-1.4916681462400413e-154,1.4916681462400413e-154] # special_inps = [] for i in bdary_lst: if i == []: rand_vals = bf.get_double_random_small() rd_idx = random.randint(0, len(rand_vals)-1) # rd_idx2 = random.randint(0, len(rand_vals)-1) temp_lst[idx].append(rand_vals[rd_idx]) # temp_lst[idx].append(rand_vals[rd_idx2]) # temp_lst[idx] = temp_lst[idx]+special_inps else: i.sort() if len(i)>1: temp_j = i[0] temp_lst[idx].append(temp_j - bf.getulp(temp_j) * 1e13) for j in i[1:]: temp_lst[idx].append((j-temp_j)/2.0 + temp_j) temp_j = j temp_lst[idx].append(i[-1] + bf.getulp(i[-1]) * 1e13) # else: for j in i: if j==0: temp_lst[idx] = temp_lst[idx] + special_inps # rand_vals = bf.get_double_random_small() # rd_idx = random.randint(0, len(rand_vals)-1) # special_inps # temp_lst[idx].append(-np.fabs(rand_vals[rd_idx])) # temp_lst[idx].append(np.fabs(rand_vals[rd_idx])) else: temp_lst[idx].append(j + bf.getulp(j) * 1e12) temp_lst[idx].append(j - bf.getulp(j) * 1e12) rand_vals = bf.get_double_random_small() rd_idx = random.randint(0, len(rand_vals) - 1) temp_lst[idx].append(rand_vals[rd_idx]) temp_lst[idx].append(-rand_vals[rd_idx]) # temp_lst[idx] = temp_lst[idx] + special_inps idx = idx+1 for element in itertools.product(*temp_lst): inps_lst.append(list(element)) return inps_lst
def generate_around_points(point,step): mix_points = [] for i in point: temp_ulp = bf.getulp(i) mix_points.append([i-step*temp_ulp,i,i+step*temp_ulp]) final_points = [] for i in itertools.product(*mix_points): final_points.append(list(i)) final_points.remove(point) return final_points
def pointToBound(th, rf, pf, point): print "Begin Find the bound around the inputs and under the threshold" # right ward iteration step = 4e2 print "Right forward to find the up bound" print point ulp_p = bf.getulp(point) print ulp_p p0_err = bf.getUlpError(rf(point), pf(point)) temp_b1 = point temp_max = p0_err for i in range(0, int(4e2)): temp_b1 = temp_b1 + ulp_p * step max_err_mid, temp_b1 = bf.max_errorOnPoint(rf, pf, temp_b1, step) try: times = np.max([np.log10(max_err_mid / th), 2.0]) except AttributeError: times = 1.0 if (max_err_mid < th) | (temp_max < max_err_mid): if (temp_max < max_err_mid): temp_b1 = step_back2(th, temp_b1, step, rf, pf, -1, ulp_p, temp_max) else: temp_b1 = step_back(th, temp_b1, step, rf, pf, -1, ulp_p) bound_up = temp_b1 break step = int(step * times) temp_max = max_err_mid print "Left forward to find the down bound" step = 4e2 temp_b1 = point temp_max = p0_err for i in range(0, int(4e2)): temp_b1 = temp_b1 - ulp_p * step max_err_mid, temp_b1 = bf.max_errorOnPoint(rf, pf, temp_b1, step) try: times = np.max([np.log10(max_err_mid / th), 2.0]) except AttributeError: times = 1.0 # print step # times = np.max([np.log2(max_err_mid / th), 2.0]) if (max_err_mid < th) | (temp_max < max_err_mid): # print step / times if (temp_max < max_err_mid): temp_b1 = step_back2(th, temp_b1, step, rf, pf, 1, ulp_p, temp_max) else: temp_b1 = step_back(th, temp_b1, step, rf, pf, 1, ulp_p) bound_down = temp_b1 break step = int(step * times) temp_max = max_err_mid return [bound_down, bound_up]
def generate_next_bound(rf,pf,ini_bound,i,ibt,flag_up,th): # print "get the one bound" # print ibt # print bf.getUlpError(ibt[0], ibt[1]) global glob_bound global index_bound index_bound = index_bound + 1 print index_bound if flag_up == 1: temp_bound = ini_bound temp_bound[i] = [get_mid(ibt),ibt[1]] if (bf.getUlpError(ibt[0], ibt[1]) < 1000): # print "reach the out point" ini_bound[i] = [ini_bound[i][0],ibt[1]] glob_bound = ini_bound return 0 temp_res = DDEMC_pure(rf, pf, [temp_bound], 1, 1000) max_err = np.log2(temp_res[0]) if max_err < th: ibt = [ibt[0], get_mid(ibt)] generate_next_bound(rf, pf, ini_bound, i, ibt, flag_up, th) else: ibt = [temp_res[1][i]+bf.getulp(temp_res[1][i]), ibt[1]] generate_next_bound(rf, pf, ini_bound, i, ibt, flag_up, th) else: temp_bound = ini_bound temp_bound[i] = [ibt[0],get_mid(ibt)] if (bf.getUlpError(ibt[0], ibt[1]) < 1000): # print "reach the out point" ini_bound[i] = [ibt[0], ini_bound[i][1]] glob_bound = ini_bound return 0 temp_res = DDEMC_pure(rf, pf, [temp_bound], 1, 1000) max_err = np.log2(temp_res[0]) if max_err < th: ibt = [get_mid(ibt), ibt[1]] generate_next_bound(rf, pf, ini_bound, i, ibt, flag_up, th) else: ibt = [ibt[0], temp_res[1][i]-bf.getulp(temp_res[1][i])] generate_next_bound(rf, pf, ini_bound, i, ibt, flag_up, th)
def extract_noempty_bound(bound): mp.dps = 30 # bf.fpartition(bound) ret_lst = [] if bound[0] == bound[1]: ret_lst.append(bound[0]) return ret_lst if bound[0] == 0: ret_lst.append(bf.f64min) else: ret_lst.append(bound[0] + bf.getulp(bound[0])) if bound[1] == 0: ret_lst.append(-bf.f64min) else: ret_lst.append(bound[1] - bf.getulp(bound[1])) fpart_bound = bf.fdistribution_partition(bound[0], bound[1]) a = int(max(len(fpart_bound) / 10.0, 1)) if a == 0: a = 1 for i in range(0, len(fpart_bound), a): ret_lst.append(random.uniform(fpart_bound[i][0], fpart_bound[i][1])) return ret_lst
def gen_next_test_bound(bound_around_point,id,sign): next_test_bound = [] for i in range(len(bound_around_point)): if i == id-1: ulp_dis = bf.getUlpError(bound_around_point[i][0],bound_around_point[i][1]) ulp_p = bf.getulp(bound_around_point[i][0]) if sign == 1: next_test_bound.append([bound_around_point[i][1],bound_around_point[i][1]+sign*ulp_dis*ulp_p]) else: next_test_bound.append([bound_around_point[i][0] + sign * ulp_dis * ulp_p,bound_around_point[i][0]]) else: next_test_bound.append(bound_around_point[i]) return next_test_bound
def step_back_2v(point,ini_bound,rf,pf,th,id): # print "get the step back point" global glob_bound for i in range(0,len(point)): if i != id-1: ipt = point[i] ibt = ini_bound[i] if ipt < ibt[0]: flag_up = 1 # temp_bound = ini_bound temp_res = DDEMC_pure(rf, pf, [ini_bound], 1, 1000) ibt = [temp_res[1][i]+bf.getulp(temp_res[1][i]), ibt[1]] generate_next_bound(rf,pf,ini_bound, i, ibt, flag_up,th) else: flag_up = 0 # temp_bound = ini_bound temp_res = DDEMC_pure(rf, pf, [ini_bound], 1, 1000) ibt = [ibt[0], temp_res[1][i]-bf.getulp(temp_res[1][i])] generate_next_bound(rf,pf,ini_bound, i, ibt, flag_up,th) new_bound = glob_bound glob_bound = [] return new_bound
def derivingApproximation(th, bound_l, rf, name, filename, inp): bound_idx = 0 num_line = 0 save_line = [] exr.glob_point_l = [] for bound in bound_l: temp_ploy_fit = '' n = int(bf.getFPNum(bound[0], bound[1])) ori_bound = bound print bound print print "%.12e" % float(bf.getFPNum(bound[0], bound[1])) # To make sure the length of bound less than a value (1e12) limit_of_bound = 1e12 samll_bound_l = [] ulp_x = bf.getulp(bound[0]) # Step2: linear iterative approximation # partition the bound according to the limit_of_bound if n / limit_of_bound > 2.0: temp_bound0 = bound[0] temp_dis = limit_of_bound * ulp_x while (temp_bound0 + temp_dis < bound[1]): if (inp < temp_bound0 + temp_dis) & (inp > temp_bound0): samll_bound_l.append([temp_bound0, inp]) samll_bound_l.append([inp, temp_bound0 + temp_dis]) else: samll_bound_l.append([temp_bound0, temp_bound0 + temp_dis]) temp_bound0 = temp_bound0 + temp_dis samll_bound_l.append([temp_bound0, bound[1]]) print len(samll_bound_l) print bound i = 0 for idx_b in samll_bound_l: i = i + 1 iter_liner_build(th, idx_b, rf, n) else: if (inp < bound[1]) & (inp > bound[0]): iter_liner_build(th, [bound[0], inp], rf, n) iter_liner_build(th, [inp, bound[1]], rf, n) else: iter_liner_build(th, bound, rf, n) if len(exr.glob_point_l) >= 30: temp_ploy_fit = generate_fitting(exr.glob_point_l) covertToC(exr.glob_point_l, n, name, bound_idx, filename, ori_bound, temp_ploy_fit) save_line = save_line + exr.glob_point_l num_line = num_line + len(exr.glob_point_l) bound_idx = bound_idx + 1 exr.glob_point_l = [] return save_line, num_line
def generate_bound_id(point,ini_step,new_step,id): ini_bound = [] count = 0 if id == 0: for i in point: ini_bound.append([i-new_step*bf.getulp(i),i+new_step*bf.getulp(i)]) else: count = count + 1 for i in point: if id != count: ini_bound.append([i - new_step * bf.getulp(i), i + new_step * bf.getulp(i)]) else: ini_bound.append([i - ini_step * bf.getulp(i), i + ini_step * bf.getulp(i)]) count = count + 1 return ini_bound
def generate_bound(point,ini_step): ini_bound = [] for i in point: ini_bound.append([i-ini_step*bf.getulp(i),i+ini_step*bf.getulp(i)]) return ini_bound
def DEMC(rf, pf, inpdm, fnm, limit_n, limit_time): st = time.time() file_name = "../experiments/detecting_results/DEMC/" + fnm count = 0 final_max = 0.0 final_x = 0.0 final_count1 = 0 final_count2 = 0 final_bound = [] record_res_l = [] dom_l = bf.fdistribution_partition(inpdm[0], inpdm[1]) glob_fitness_con = np.frompyfunc(lambda x: bf.fitness_fun1(rf, pf, x), 1, 1) glob_fitness_real = np.frompyfunc(lambda x: bf.fitness_fun(rf, pf, x), 1, 1) try: print "Detecting possible maximum error by DEMC algorithm" signal.alarm(limit_time) while (count < limit_n): temp_st = time.time() count1 = 0 count2 = 0 rand_seed = bf.rd_seed[count] np.random.seed(rand_seed) res_l = [] for k in dom_l: temp_max = 0.0 temp_x = 0.0 res = differential_evolution(glob_fitness_con, popsize=15, bounds=[k], polish=False, strategy='best1bin') x = res.x[0] count2 = count2 + res.nfev err = 1.0 / glob_fitness_real(float(x)) if err > temp_max: temp_max = err temp_x = x temp = [temp_max, temp_x, k] res_l.append(temp) t1 = time.time() - temp_st # print t1 res_l = sorted(res_l, reverse=True) temp_max = res_l[0][0] temp_x = res_l[0][1] bound = res_l[0][2] res_lr = [] s_len = np.min([len(res_l), 10]) # print res_l[0:s_len] # glob_fitness_real_temp = lambda x: x*x distan_two_search_x = 1.0 minimizer_kwargs = {"method": "Nelder-Mead"} for j in res_l[0:s_len]: gen_l = produce_interval1(j[1], j[2]) glob_fitness_real_temp = lambda x: bf.fitness_fun( rf, pf, reduce_x1(gen_l[0], gen_l[1], x)) # glob_fitness_real_temp = lambda x: bf.fitness_fun(rf, pf, x) x = math.asin( (2 * j[1] - gen_l[0] - gen_l[1]) / (gen_l[1] - gen_l[0])) # x = j[1] res = basinhopping(glob_fitness_real_temp, x, stepsize=bf.getulp(x) * 1e10, minimizer_kwargs=minimizer_kwargs, niter_success=10, niter=200) count1 = count1 + res.nfev # x = res.x[0] x = reduce_x1(gen_l[0], gen_l[1], res.x[0]) err = 1.0 / res.fun temp = [err, x, gen_l] res_lr.append(temp) if err > temp_max: temp_max = err temp_x = x bound = j[2] distan_two_search_x = bf.getUlpError(temp_x, res_l[0][1]) t2 = time.time() - temp_st temp_l = [ temp_max, temp_x, bound, t2, count1, count2, rand_seed, count, t2 - t1, distan_two_search_x ] # print temp_l # print distan_two_search_x final_count1 = final_count1 + count1 final_count2 = final_count2 + count2 record_res_l.append(temp_l) count = count + 1 distan_two_search_x_final = 1.0 if temp_max > final_max: final_max = temp_max final_x = temp_x final_bound = bound distan_two_search_x_final = distan_two_search_x # distan_two_search_x = bf.getUlpError(final_x, res_l[0][1]) final_time = time.time() - st bf.output_err(record_res_l, file_name, fnm) print "%.20e" % final_x print "%.20e" % final_max return [ final_max, final_x, final_bound, final_time, count, final_count1, final_count2, distan_two_search_x_final ] except TimeoutError: final_time = time.time() - st bf.output_err(record_res_l, file_name, fnm) return [ final_max, final_x, final_bound, final_time, count, final_count1, final_count2, distan_two_search_x_final ]
def produce_interval1(x, k): a = bf.getulp(x) * 1e12 return [np.max([x - a, k[0]]), np.min([x + a, k[1]])]
def produce_interval(inp_l, bound_l): temp_bound = [] for i, j in zip(inp_l, bound_l): a = bf.getulp(i) * 1e10 temp_bound.append([np.max([i - a, j[0]]), np.min([i + a, j[1]])]) return temp_bound
def bound_err_Under_th(rf,pf,re_bound,point,th,kb_fun,dr,file_name): ib = re_bound[dr] ob = re_bound[1-dr] ulp_o = bf.getulp(point[1-dr]) limit_size = bf.getUlpError(ob[0],ob[1]) step = 1e2 sign = 1 up_bound = 0 final_step = 0 temp_step = 0 print limit_size for i in range(0,400): temp_dis = bf.get_next_point(point[1-dr], step, sign) - point[1-dr] # print temp_dis # print point print step print limit_size temp_kb_fun = lambda x: kb_fun(x) + temp_dis if dr == 1: new_rf = lambda y: rf(temp_kb_fun(y), y) new_pf = lambda y: pf(temp_kb_fun(y), y) else: new_rf = lambda x: rf(x, temp_kb_fun(x)) new_pf = lambda x: pf(x, temp_kb_fun(x)) res = DEMC_pure(new_rf, new_pf, ib, 1, 20000) max_err = res[0] max_err2 = random_test_err(new_rf, new_pf, ib) # print max_err # print th # print jump_step # print temp_step max_err = np.max([max_err, max_err2]) print max_err print th try: times = np.max([np.log10(max_err / th), 2.0]) except AttributeError: times = 1.5 if (max_err < th-th/100.0): final_step = step_back_2v(th, point, step, temp_step, rf, pf, -sign, kb_fun, dr, ib) # if (temp_max<max_err): # final_step = step_back_2v2(th, point, step,temp_step, rf, pf, -sign,temp_max,kb_fun,dr,ob) # else: # break temp_step = step flag = checkIn_bound(temp_kb_fun,re_bound,dr) if flag == 0: print "touch flag" return [] if step > limit_size: break step = int(step * times) temp_max = max_err up_bound = final_step step = 1e2 sign = -1 down_bound = 0 temp_step = 0 final_step = 0 for i in range(0, 400): # print "right" # print step temp_dis = bf.get_next_point(point[1-dr], step, sign) - point[1-dr] # print temp_dis # print point temp_kb_fun = lambda x: kb_fun(x) + temp_dis if dr == 1: new_rf = lambda y: rf(temp_kb_fun(y), y) new_pf = lambda y: pf(temp_kb_fun(y), y) else: new_rf = lambda x: rf(x, temp_kb_fun(x)) new_pf = lambda x: pf(x, temp_kb_fun(x)) res = DEMC_pure(new_rf, new_pf, ib, 1, 20000) max_err = res[0] # print max_err # print th max_err2 = random_test_err(new_rf, new_pf, ib) # print max_err # print th # print jump_step # print temp_step max_err = np.max([max_err, max_err2]) print max_err print th try: times = np.max([np.log10(max_err / th), 2.0]) except AttributeError: times = 1.0 if (max_err < th-th/100.0): final_step = step_back_2v(th, point, step, temp_step, rf, pf, -sign, kb_fun, dr, ib) # if (temp_max<max_err): # final_step = step_back_2v2(th, point, step,temp_step, rf, pf, -sign,temp_max,kb_fun,dr,ob) # else: # break flag = checkIn_bound(temp_kb_fun, re_bound, dr) if flag == 0: print "touch flag" return [] if step > limit_size: return [] temp_step = step step = int(step * times) temp_max = max_err down_bound = final_step up_dis = bf.get_next_point(point[1-dr], up_bound, 1) - point[1-dr] print up_dis down_dis = bf.get_next_point(point[1-dr], down_bound, -1) - point[1-dr] print down_dis # if fabs(up_dis)>fabs(down_dis): # down_dis = -up_dis # else: # up_dis = -down_dis up_kb_fun = lambda x: kb_fun(x) + up_dis down_kb_fun = lambda x: kb_fun(x) + down_dis Xi = np.random.uniform(re_bound[dr][0], re_bound[dr][1], 2000) Yi = [up_kb_fun(xi) for xi in Xi] Yi2 = [down_kb_fun(xi) for xi in Xi] # print Yi[0:10] # print Yi2[0:10] up_points = [] down_points = [] for i, j, j2 in zip(Xi, Yi, Yi2): temp_i = [0, 0] temp_i2 = [0, 0] temp_i[dr] = float(i) temp_i[1 - dr] = float(j) temp_i2[dr] = float(i) temp_i2[1 - dr] = float(j2) up_points.append(temp_i) down_points.append(temp_i2) kb_fun_up, kb_up_lst = points_approx_kb(up_points, dr, 1) kb_up = kb_up_lst[0] kb_fun_down, kb_down_lst = points_approx_kb(down_points, dr, 1) kb_down = kb_down_lst[0] # print kb_up_lst # print kb_down_lst # print up_points[0:10] # print down_points[0:10] add_dis_up = 0 add_dis_up_lst = [] for i in up_points: temp_dis = i[1-dr]-kb_fun_up(i[dr]) add_dis_up_lst.append(temp_dis) add_dis_up = np.max(add_dis_up_lst) # print add_dis_up add_dis_down = 0 add_dis_down_lst = [] for i in down_points: temp_dis = i[1 - dr] - kb_fun_down(i[dr]) add_dis_down_lst.append(temp_dis) add_dis_down = np.max(add_dis_down_lst) # print add_dis_down kb_up[0]= kb_up[0]+add_dis_up kb_down[0]= kb_down[0]+add_dis_down store_bound_lst = [up_points[0],kb_up,down_points[0],kb_down] kb_up_fun = lambda x:up_points[0][1-dr]+float(kb_up[0])+(x-up_points[0][dr])*float(kb_up[1]) kb_down_fun = lambda x:down_points[0][1-dr]+float(kb_down[0])+(x-down_points[0][dr])*float(kb_down[1]) X = [] X1 = [] X2 = [] Y = [] Y1 = [] Y2 = [] Z = [] Z2 = [] # input_l = np.random.uniform(new_bound[0], new_bound[1],3000) input_l = bf.produce_n_input(re_bound, 100) for i in input_l: utemp_i = list(i) utemp_i[1-dr] = kb_up_fun(i[dr]) X1.append(utemp_i[0]) Y1.append(utemp_i[1]) dtemp_i = list(i) dtemp_i[1 - dr] = kb_down_fun(i[dr]) X2.append(dtemp_i[0]) Y2.append(dtemp_i[1]) # temp_res = rf(i) # temp_res = np.log2(float(1.0 / glob_fitness_real(i))) temp_res = np.log2(bf.getUlpError(rf(*i), pf(*i))) X.append(i[0]) Y.append(i[1]) Z.append(float(temp_res)) # Z.append(rf(*i)) Z2.append(12) # Z.append(rf(i)-line_fun(i)) print "max_Z" fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot(X, Y, Z, '.') ax.plot(X1, Y1, Z2, '.') ax.plot(X2, Y2, Z2, '.') # ax = fig.add_subplot(122, projection='3d') # ax.plot(X, Y, Z2, '.') # ax = fig.add_subplot(111) # ax.plot(X, Z, '.') ax.legend() fig_name = file_name + "/err_bound.eps" plt.savefig(fig_name, format="eps") # plt.show() return store_bound_lst
def err_tracing_In_domain2(rf,pf,inpdm,point): re_bound = get_repair_bound(inpdm, point) print "**********" # point = [-1.999999998603031e+00, point[1]] # print re_bound # print rf(*point) # print pf(*point) # p_val = rf(*point) # print bf.getUlpError(rf(*point),pf(*point)) point = list(point) new_rf = lambda y: rf(point[0], y) temp_point = [point[0], float(findroot(new_rf, point[1]))] print point print temp_point # print bf.getUlpError(point[1],temp_point[1]) print rf(*temp_point) print rf(*point) print pf(*temp_point) point = temp_point # print bf.getUlpError(rf(*temp_point), pf(*temp_point)) temp_point = list(point)+[] gl_points = [] step = 10 app_points = [] app_points.append(temp_point) for ct in range(100): ar_ps = generate_around_points(temp_point,1) temp_res = [] for i in ar_ps: if i not in gl_points: temp_res.append([fabs(rf(*i)),i,rf(*i),np.log2(bf.getUlpError(rf(*i),pf(*i)))]) gl_points.append(i) gl_points.append(temp_point) temp_res.sort() # print temp_res[0] app_points.append(temp_res[0][1]) # print gl_points # print len(gl_points) temp_point = temp_res[0][1] dr = get_point_direction(app_points) kb_fun = points_approx(app_points, dr,1) new_point = point app_points = [] app_points.append(point) bound_size = get_bound_size(re_bound) min_size = np.min(bound_size) p0=point step = 1e8 ulp_p0 = [bf.getulp(i) for i in p0] print ulp_p0 print point p_val = rf(*point) count = 0 final_points = [] final_points.append(point) for i in range(0,1000): print i new_point[dr] = new_point[dr]-ulp_p0[dr]*step new_point[1-dr] = kb_fun(new_point[dr]) print rf(*new_point) print new_point if dr == 0: new_rf = lambda y: rf(new_point[0], y) new_point = [new_point[0], findroot(new_rf, new_point[1], tol=p_val * p_val*10)] else: new_rf = lambda x: rf(x,new_point[1]) new_point = [findroot(new_rf, new_point[0], tol=p_val*p_val*10),new_point[1]] print rf(*new_point) print float(rf(float(new_point[0]),float(new_point[1]))) p_val = float(rf(float(new_point[0]),float(new_point[1]))) print float(pf(float(new_point[0]),float(new_point[1]))) # print np.log2(bf.getUlpError(rf(float(new_point[0]),float(new_point[1])),pf(float(new_point[0]),float(new_point[1])))) print new_point app_points.append(new_point) if count == 50: dr = get_point_direction(app_points) print dr kb_fun = points_approx(app_points, dr, 1) app_points = [] # app_points.append(point) # app_points.append(new_point) count = 0 step = step*2 count = count+1 print point print re_bound
def generate_point(point,step,p,sgn): x = point[0]+sgn*step*bf.getulp(point[0]) y = p(x) return [x,y]
def find_bound_point(point,re_bound,dr,p_val,kb_fun,step,rf): ulp_p0 = [bf.getulp(i) for i in point] temp_point = list(point) # try to approximate the max error line in a larger region brk_flag = 0 for ir in range(0, 5): app_points = [] new_point = list(point) app_points.append(point) for i in range(0, 100): new_point = gen_new_point(new_point, dr, rf, kb_fun, ulp_p0, step, p_val) # dr = get_point_direction(app_points) kb_fun = build_line_fun(temp_point, new_point, dr) app_points.append(list(new_point)) temp_point = list(new_point) new_point, brk_flag, idx = reach_bound_check(re_bound, list(new_point)) if brk_flag == 1: break if brk_flag == 1: break kb_fun = points_approx(app_points, dr, 2 + ir) step = step * 10 app_points2 = [] step = step/10 new_point = list(point) app_points2.append(point) count = 200 for i in range(0, 1003): new_point = list(gen_new_point(new_point, dr, rf, kb_fun, ulp_p0, step, p_val)) # dr = get_point_direction(app_points) # print new_point fl_point, brk_flag, idx = reach_bound_check(re_bound, list(new_point)) if brk_flag == 1: # print "here" # print dr # print new_point # print temp_point # print fl_point # print i if idx == dr: fl_point[1-dr] = mpf(kb_fun(fl_point[dr])) # fl_point[1-dr] = mpf(temp_point[1-dr]) else: kb_fun = points_approx(app_points2, 1-dr, 10 + i / 100) fl_point[dr] = kb_fun(fl_point[1-dr]) # fl_point[dr] = mpf(temp_point[dr]) break if len(app_points2)>=200: if len(app_points2)==count: kb_fun = points_approx(app_points2, dr, 6+i/200) count = count + 200 # if len(app_points2)==400: # kb_fun = points_approx(app_points2, dr, 12) # if len(app_points2)==600: # kb_fun = points_approx(app_points2, dr, 12) # if len(app_points2)==800: # kb_fun = points_approx(app_points2, dr, 12) else: kb_fun = build_line_fun(temp_point, new_point, dr) app_points2.append(list(new_point)) temp_point = list(new_point) # kb_fun = points_approx(app_points, dr, 6) fl_point = gen_final_point(fl_point, idx, rf, p_val) app_points2.append(list(fl_point)) return fl_point,app_points2
def covertToC(glob_l, n, name, idx, filename, bound, temp_ploy_fit): print "Cover To C code" orig_stdout = sys.stdout name = 'patch_of_' + name len_glob = len(glob_l) x_0 = bound[0] ulp_x = bf.getulp(glob_l[0][0][0]) temp_n = 0.0 temp_n_max = 0.0 f = open(filename + '/' + name + '.c', 'a') name = name.split("gsl_")[1] sys.stdout = f print 'static double array_x_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[0][0] print '};' print 'static double array_y_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[0][1] print '};' print 'static double array_e_y_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[7][1] print '};' print 'static double array_detla_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[1] print '};' print 'static double array_idx_' + name + '_' + str(idx) + '[' + str( len_glob + 1) + '] = {' print "%.18e," % temp_n for i in glob_l: print "%.18e," % (i[2] + temp_n) temp_n = i[2] + temp_n print '};' # print 'static double array_maxX_' + str(idx) + '[' + str(len_glob) + '] = {' # for i in glob_l: # print "%.15e," % (i[3]+temp_n_max) # temp_n_max = (i[2]+temp_n_max) # print '};' print 'static double array_maxE_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[4] print '};' print "double accuracy_improve_patch_of_gsl_" + name + '_' + str( idx) + "(double x)" print "{" print " long int n = " + str(n) + ";" print " int len_glob = " + str(len_glob) + ";" print " double ulp_x = " + repr(ulp_x) + ";" print " double x_0 = " + repr(x_0) + ";" print " double compen = 0.0;" print " double n_x = ((x-x_0)/ulp_x);" if temp_ploy_fit == '': # print " int idx = floor(n_x*len_glob/n);" print " int idx = floor(len_glob/2);" else: print temp_ploy_fit print " if(idx>=len_glob){" print " idx = len_glob-1;" print " }" print " while((idx>=0)&&(idx<len_glob)){" print " if((n_x>array_idx_" + name + '_' + str( idx) + "[idx])&&(n_x<array_idx_" + name + '_' + str(idx) + "[idx+1])){" print " compen = ulp_x*ulp_x * (n_x-array_idx_" + name + '_' + str( idx) + "[idx+1])*(n_x-array_idx_" + name + '_' + str( idx) + "[idx])*array_maxE_" + name + '_' + str(idx) + "[idx];" print " return (x-array_x_" + name + '_' + str( idx) + "[idx])/ulp_x*array_detla_" + name + '_' + str( idx) + "[idx]+array_y_" + name + '_' + str(idx) + "[idx]+compen;" print " }" print " else if(n_x<array_idx_" + name + '_' + str(idx) + "[idx]){" print " idx = idx - 1;" print " }" print " else if(n_x>array_idx_" + name + '_' + str(idx) + "[idx+1]){" print " idx = idx + 1;" print " }" print " else if(x==array_x_" + name + '_' + str(idx) + "[idx]){" print " return array_y_" + name + '_' + str(idx) + "[idx];" print " }" print " else{" print " return array_e_y_" + name + '_' + str(idx) + "[idx];" print " }" print " }" print "}" sys.stdout = orig_stdout f.close()
def err_tracing_In_bound(rf,pf,re_bound,point,file_name,fun_name,th): print "**********" print fun_name filename = file_name + fun_name # generate shell scripts to apply patches if not os.path.exists(filename): # os.remove(filename + "/patch/patch_cmd.sh") os.makedirs(filename) point = list(point) new_rf = lambda y: rf(point[0], y) temp_point = [point[0], float(findroot(new_rf, point[1]))] point = list(temp_point) temp_point = list(point) ulp_p0 = [bf.getulp(i) for i in point] gl_points = [] app_points = [] app_points.append(temp_point) for ct in range(100): ar_ps = generate_around_points(temp_point, 1e1) temp_res = [] for i in ar_ps: if i not in gl_points: temp_res.append([fabs(rf(*i)), i, rf(*i), np.log2(bf.getUlpError(rf(*i), pf(*i)))]) gl_points.append(i) gl_points.append(temp_point) temp_res.sort() app_points.append(temp_res[0][1]) temp_point = temp_res[0][1] dr = get_point_direction(app_points) kb_fun = points_approx(app_points, dr, 1) print dr p_val = rf(*point) final_pointsl = [] final_pointsr = [] fl_pointl = [] fl_pointr = [] pb_dis = get_point_bound_dis(point, re_bound) # print pb_dis dr_disr = pb_dis[dr][1] dr_disl = pb_dis[dr][0] step = np.max([(dr_disr / 1e3) / 1e4, 1000]) # print step # print re_bound # print temp_point fl_pointl, final_pointsl = find_bound_point(point, re_bound, dr, p_val, kb_fun, step, rf) step = -np.max([(dr_disl / 1e3) / 1e4, 1000]) fl_pointr, final_pointsr = find_bound_point(point, re_bound, dr, p_val, kb_fun, step, rf) # print fl_pointl # print fl_pointr # print rf(*fl_pointl) # print rf(*fl_pointr) final_pointsl.reverse() fl_points = final_pointsl + final_pointsr fl_points_name = filename + "/points.txt" pickle_fun(fl_points_name, fl_points) # final_bound = gen_error_bound(re_bound,fl_pointl,fl_pointr) X = [] Y = [] Y1 = [] fresh_points = [] for i in fl_points: fi = [float(i[0]), float(i[1])] X.append(float(i[0])) # print fi temp_err = np.log2(bf.getUlpError(float(rf(*fi)), pf(*fi))) if temp_err > 60: # print rf(*fi) # print pf(*fi) # print fi fresh_points.append(i) # Y.append() Y.append(float(i[1])) Y1.append(temp_err) fig = plt.figure() ax = fig.add_subplot(121) ax.plot(X, Y, '.') ax2 = fig.add_subplot(122) ax2.plot(X, Y1, '.') ax.legend() fig_name = filename + "/max_err_line.eps" plt.savefig(fig_name, format="eps") plt.close() # plt.show() print re_bound print len(fresh_points) kb_fun, kb = points_approx_kb(fresh_points, dr,10) bound_th_res = bound_err_Under_th(rf, pf, re_bound, point, th, kb_fun, dr,filename) bound_th_name = filename + "/bound_th.txt" pickle_fun(bound_th_name, bound_th_res) kb_fun_name = filename + "/kb_fun.txt" pickle_fun(kb_fun_name, kb) # kb_fun2 = points_approx(fresh_points, 1-dr, 17) final_bound = gen_error_bound(re_bound, fl_pointl, fl_pointr) print final_bound print re_bound bounds_c = [final_bound, re_bound] bounds_name = filename + "/bounds.txt" pickle_fun(bounds_name, bounds_c) Xi = np.random.uniform(final_bound[dr][0], final_bound[dr][1], 2000) Yi = [kb_fun(xi) for xi in Xi] Yi2 = [kb_fun(xi) + 1e12 * ulp_p0[1 - dr] for xi in Xi] X = [] Y = [] Y1 = [] Y2 = [] for i, j, j2 in zip(Xi, Yi, Yi2): temp_i = [0, 0] temp_i2 = [0, 0] temp_i[dr] = float(i) temp_i[1 - dr] = float(j) temp_i2[dr] = float(i) temp_i2[1 - dr] = float(j2) a = rf(*temp_i) b = pf(*temp_i) c = rf(*temp_i2) d = pf(*temp_i2) X.append(temp_i[0]) Y.append(temp_i[1]) # Y.append(a) # print temp_i # print temp_i2 # print a # print b # print np.log2(float(bf.getUlpError(a, b))) Y1.append(np.log2(float(bf.getUlpError(a, b)))) Y2.append(np.log2(float(bf.getUlpError(c, d)))) # print np.max(Y) # print np.min(Y) fig = plt.figure() ax = fig.add_subplot(131) ax.plot(X, Y1, '.') ax2 = fig.add_subplot(132) ax2.plot(X, Y2, '.') ax3 = fig.add_subplot(133) ax3.plot(X, Y, '.') ax.legend() # plt.show() fig_name = filename + "/app_max_err_line.eps" plt.savefig(fig_name, format="eps") return 0
def convertToC_taylorOnL(glob_l, n, name, idx, filename, bound, temp_ploy_fit): print "Cover To C code" orig_stdout = sys.stdout name = 'patch_of_' + name len_glob = len(glob_l) x_0 = bound[0] ulp_x = bf.getulp(glob_l[0][0][0]) temp_n = 0.0 temp_n_max = 0.0 f = open(filename + '/' + name + '.c', 'a') name = name.split("gsl_")[1] sys.stdout = f if idx == 0: print 'int factorial(int i){' print ' int fact=1;' print ' for(int k=1;k<=i;k++){' print ' fact=fact*k;' print ' }' print ' return fact;}' print 'static double array_x_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[0][0] print '};' print 'static double array_y_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[0][1] print '};' print 'static double array_e_y_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[1][1] print '};' print 'static double array_detla_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[3] print '};' print 'static double array_idx_' + name + '_' + str(idx) + '[' + str( len_glob + 1) + '] = {' print "%.18e," % temp_n for i in glob_l: print "%.18e," % (i[2] + temp_n) temp_n = i[2] + temp_n print '};' print 'static double array_midpoint_' + name + '_' + str(idx) + '[' + str( len_glob) + '] = {' for i in glob_l: print "%.18e," % i[6] print '};' print 'static double array_Taylor_' + name + '_' + str(idx) + '[' + str( len_glob) + ']' + '[' + str(5) + '] = {' for i in glob_l[0:-1]: print '{' print str(len(i[4])) + ',' if i[4] != []: for j in i[4]: print "%.18e," % j print '},' print '{' print str(len(glob_l[-1][4])) + ',' if glob_l[-1] != []: for j in glob_l[-1][4]: print "%.18e," % j print '}' print '};' print "double accuracy_improve_patch_of_gsl_" + name + '_' + str( idx) + "(double x)" print "{" print " long int n = " + str(n) + ";" print " int len_glob = " + str(len_glob) + ";" print " double ulp_x = " + repr(ulp_x) + ";" print " double x_0 = " + repr(x_0) + ";" print " double compen = 0.0;" print " double n_x = ((x-x_0)/ulp_x);" if temp_ploy_fit == '': # print " int idx = floor(n_x*len_glob/n);" print " int idx = floor(len_glob/2);" else: print temp_ploy_fit print " if(idx>=len_glob){" print " idx = len_glob-1;" print " }" print " while((idx>=0)&&(idx<len_glob)){" print " if((n_x>array_idx_" + name + '_' + str( idx) + "[idx])&&(n_x<array_idx_" + name + '_' + str(idx) + "[idx+1])){" print " double compen = array_Taylor_" + name + '_' + str( idx) + '[idx][1];' print " double tayn = array_Taylor_" + name + '_' + str( idx) + '[idx][0];' print " double midpoint = array_midpoint_" + name + '_' + str( idx) + '[idx];' print " for(int i=2;i<tayn+1;i++){" print " compen = compen+pow(x-midpoint,i-1)*(array_Taylor_" + name + '_' + str( idx) + "[idx][i])/factorial(i-1);" print " }" print " return (x-array_x_" + name + '_' + str( idx) + "[idx])/ulp_x*array_detla_" + name + '_' + str( idx) + "[idx]+array_y_" + name + '_' + str(idx) + "[idx]+compen;" print " }" print " else if(n_x<array_idx_" + name + '_' + str(idx) + "[idx]){" print " idx = idx - 1;" print " }" print " else if(n_x>array_idx_" + name + '_' + str(idx) + "[idx+1]){" print " idx = idx + 1;" print " }" print " else if(x==array_x_" + name + '_' + str(idx) + "[idx]){" print " return array_y_" + name + '_' + str(idx) + "[idx];" print " }" print " else{" print " return array_e_y_" + name + '_' + str(idx) + "[idx];" print " }" print " }" print "}" sys.stdout = orig_stdout f.close()