def solve_rdft_improved(a,b,x): (size,_) = a.shape f = rdft.generate_f(size) r = rdft.generate_random_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_another(a, l, u, fr, b, x2, linalg.cond(fra)) x2 = iteration.remove_imag(x2) return (x2, fra, fr, ra, x1)
def solve_rdft_improved(a, b, x): (size, _) = a.shape f = rdft.generate_f(size) r = rdft.generate_random_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_another(a, l, u, fr, b, x2, linalg.cond(fra)) x2 = iteration.remove_imag(x2) return (x2, fra, fr, ra, x1)
def solve_rdft_givens(a, b, x): (size, _) = a.shape f = rdft.generate_f(size) r = rdft.generate_r(size) ## === care === ## g = givens.givens_generation(size) fr = f.dot(r) frg = fr.dot(g) rg = r.dot(g) (x1, l, u, fra, frb, _) = rdft.rdft_lu_solver_with_lu(a, b, rg) x2 = np.array(x1) x2 = iteration.iteration_another(a, l, u, frg, b, x2, linalg.cond(fra)) x2 = iteration.remove_imag(x2) return (x2, x1)
def solve_rdft_givens(a,b,x): (size,_) = a.shape f = rdft.generate_f(size) r = rdft.generate_r(size) ## === care === ## g = givens.givens_generation(size) fr = f.dot(r) frg = fr.dot(g) rg = r.dot(g) (x1, l, u, fra, frb, _) = rdft.rdft_lu_solver_with_lu(a,b,rg) x2 = np.array(x1) x2 = iteration.iteration_another(a, l, u, frg, b, x2, linalg.cond(fra)) x2 = iteration.remove_imag(x2) return (x2, x1)
def const_r(): for i in range(0, 50): size = 200 mat = np.zeros((size, size)) r = rdft.generate_r(size) # use constant r for j in range(0, size): for k in range(0, size): mat[j, k] = random.uniform(-100, 100) f = rdft.generate_f(size) fr = f.dot(r) fra = fr.dot(mat) (a_maxcond, _, _) = rdft.get_leading_maxcond(mat) (fra_maxcond, _, _) = rdft.get_leading_maxcond(fra) a_cond = linalg.cond(mat) fra_cond = linalg.cond(fra) print("A: ", a_maxcond / a_cond) print("FRA:", fra_maxcond / fra_cond)
def const_r(): for i in range(0, 50): size = 200 mat = np.zeros((size,size)) r = rdft.generate_r(size) # use constant r for j in range(0,size): for k in range(0,size): mat[j,k] = random.uniform(-100,100) f = rdft.generate_f(size) fr = f.dot(r) fra = fr.dot(mat) (a_maxcond,_,_) = rdft.get_leading_maxcond(mat) (fra_maxcond,_,_) = rdft.get_leading_maxcond(fra) a_cond = linalg.cond(mat) fra_cond = linalg.cond(fra) print("A: ", a_maxcond/a_cond) print("FRA:", fra_maxcond/fra_cond)
def const_a(sample, size, rand_range): result = [] mat = np.zeros((size,size)) for j in range(0,size): for k in range(0,size): mat[j,k] = random.uniform(-rand_range,rand_range) for i in range(0, sample): f = rdft.generate_f(size) r = rdft.generate_r(size) fr = f.dot(r) fra = fr.dot(mat) (a_maxcond,_,a_subcond) = rdft.get_leading_maxcond(mat) (fra_maxcond,_,fra_subcond) = rdft.get_leading_maxcond(fra) a_cond = linalg.cond(mat) fra_cond = linalg.cond(fra) result.append([mat, a_maxcond/a_cond, fra_maxcond/fra_cond, fra, a_subcond, fra_subcond]) #print("A: ", a_maxcond/a_cond) #print("FRA:", fra_maxcond/fra_cond) return result
def solve_rdft_givens_both(a,b,x): (size,_) = a.shape f = rdft.generate_f(size) r = rdft.generate_r(size) g1 = givens.givens_generation(size) g2 = givens.givens_generation(size) fr = f.dot(r) frg = fr.dot(g1) frgb = frg.dot(b) ag = a.dot(g2) frgag = frg.dot(ag) (l,u) = lu.lu(frgag) y = lu.l_step(l,frgb) z = lu.u_step(u,y) x1 = g2.dot(z) x2 = np.array(x1) x2 = iteration.iteration_another(a, l, u, frg, b, x2, linalg.cond(frgag), g2) x2 = iteration.remove_imag(x2) return (x2, x1)
def solve_rdft_givens_both(a, b, x): (size, _) = a.shape f = rdft.generate_f(size) r = rdft.generate_r(size) g1 = givens.givens_generation(size) g2 = givens.givens_generation(size) fr = f.dot(r) frg = fr.dot(g1) frgb = frg.dot(b) ag = a.dot(g2) frgag = frg.dot(ag) (l, u) = lu.lu(frgag) y = lu.l_step(l, frgb) z = lu.u_step(u, y) x1 = g2.dot(z) x2 = np.array(x1) x2 = iteration.iteration_another(a, l, u, frg, b, x2, linalg.cond(frgag), g2) x2 = iteration.remove_imag(x2) return (x2, x1)
def const_a(sample, size, rand_range): result = [] mat = np.zeros((size, size)) for j in range(0, size): for k in range(0, size): mat[j, k] = random.uniform(-rand_range, rand_range) for i in range(0, sample): f = rdft.generate_f(size) r = rdft.generate_r(size) fr = f.dot(r) fra = fr.dot(mat) (a_maxcond, _, a_subcond) = rdft.get_leading_maxcond(mat) (fra_maxcond, _, fra_subcond) = rdft.get_leading_maxcond(fra) a_cond = linalg.cond(mat) fra_cond = linalg.cond(fra) result.append([ mat, a_maxcond / a_cond, fra_maxcond / fra_cond, fra, a_subcond, fra_subcond ]) #print("A: ", a_maxcond/a_cond) #print("FRA:", fra_maxcond/fra_cond) return result
def const_a_test(test_num, sample, size, rand_range, way=-1, way_option=[]): counter = 0 sample_counter = 0 for i in range(0, test_num): [cd, fs, mat, mca, mcfra, fras, a_subcond, fra_subconds] = const_a_detection(sample, size, rand_range) a = np.array(mat) if way == 0: mat = mat.T elif way == 1: for (k, j) in way_option: mat = pp.swap(mat, k, j) if cd > sample * (2 / 3): np.savetxt("./test_result/test_result" + str(counter) + "_a.txt", a) os.mkdir("./test_result/fra_" + str(counter)) for i in range(0, len(fras)): np.savetxt( "./test_result/fra_" + str(counter) + "/" + str(i) + ".txt", fras[i]) output = open( "./test_result/test_result" + str(counter) + "_a_subcond.txt", 'w') output.write(str(a_subcond)) output.close() output = open( "./test_result/test_result" + str(counter) + "_fra_subcond.txt", 'w') output.write(str(fra_subconds)) output.close() output = open( "./test_result/test_result" + str(counter) + "_mca.txt", 'w') output.write(str(mca)) output = open( "./test_result/test_result" + str(counter) + "_mcfra.txt", 'w') output.write(str(mcfra)) output.close() output = open( "./test_result/test_result" + str(counter) + "_det.txt", 'w') output.write(str(np.linalg.det(a))) output.close() output = open( "./test_result/test_result" + str(counter) + "_cd.txt", 'w') output.write(str(cd)) output.close() output = open( "./test_result/test_result" + str(counter) + "_cdnum.txt", 'w') output.write(str(linalg.cond(a))) output.close() sample_counter = 0 a_subcond, fra_subconds = [], [] while sample_counter < 100: f = rdft.generate_f(size) r = rdft.generate_r(size) fr = f.dot(r) fra = fr.dot(mat) (a_maxcond, _, a_subcond) = rdft.get_leading_maxcond(mat) (fra_maxcond, _, fra_subcond) = rdft.get_leading_maxcond(fra) fra_subconds.append(fra_subcond) sample_counter = sample_counter + 1 np.savetxt( "./test_result/test_result" + str(counter) + "_z_mod_a.txt", mat) os.mkdir("./test_result/fra_" + str(counter) + "_" + str(sample_counter)) for i in range(0, len(fras)): np.savetxt( "./test_result/fra_" + str(counter) + "_" + str(sample_counter) + "/" + str(i) + ".txt", fras[i]) output = open( "./test_result/test_result" + str(counter) + "_z_a_subcond.txt", 'w') output.write(str(a_subcond)) output.close() output = open( "./test_result/test_result" + str(counter) + "_z_fra_subcond.txt", 'w') output.write(str(fra_subconds)) output.close() output = open( "./test_result/test_result" + str(counter) + "_z_mca.txt", 'w') output.write(str(mca)) output = open( "./test_result/test_result" + str(counter) + "_z_mcfra.txt", 'w') output.write(str(mcfra)) output.close() output = open( "./test_result/test_result" + str(counter) + "_z_det.txt", 'w') output.write(str(np.linalg.det(mat))) output.close() output = open( "./test_result/test_result" + str(counter) + "_z_cdnum.txt", 'w') output.write(str(linalg.cond(mat))) output.close() counter = counter + 1 print(str(counter) + "detected")
def const_a_test(test_num, sample, size, rand_range, way=-1, way_option=[]): counter = 0 sample_counter = 0 for i in range(0, test_num): [cd, fs, mat, mca, mcfra, fras, a_subcond, fra_subconds] = const_a_detection(sample, size, rand_range) a = np.array(mat) if way == 0: mat = mat.T elif way == 1: for (k,j) in way_option: mat = pp.swap(mat,k,j) if cd > sample*(2/3): np.savetxt("./test_result/test_result" + str(counter) + "_a.txt",a) os.mkdir("./test_result/fra_" + str(counter)) for i in range(0,len(fras)): np.savetxt("./test_result/fra_" + str(counter) + "/" + str(i) + ".txt", fras[i]) output = open("./test_result/test_result" + str(counter) + "_a_subcond.txt",'w') output.write(str(a_subcond)) output.close() output = open("./test_result/test_result" + str(counter) + "_fra_subcond.txt",'w') output.write(str(fra_subconds)) output.close() output = open("./test_result/test_result" + str(counter) + "_mca.txt",'w') output.write(str(mca)) output = open("./test_result/test_result" + str(counter) + "_mcfra.txt",'w') output.write(str(mcfra)) output.close() output = open("./test_result/test_result" + str(counter) + "_det.txt",'w') output.write(str(np.linalg.det(a))) output.close() output = open("./test_result/test_result" + str(counter) + "_cd.txt",'w') output.write(str(cd)) output.close() output = open("./test_result/test_result" + str(counter) + "_cdnum.txt",'w') output.write(str(linalg.cond(a))) output.close() sample_counter = 0 a_subcond, fra_subconds = [], [] while sample_counter < 100: f = rdft.generate_f(size) r = rdft.generate_r(size) fr = f.dot(r) fra = fr.dot(mat) (a_maxcond,_,a_subcond) = rdft.get_leading_maxcond(mat) (fra_maxcond,_,fra_subcond) = rdft.get_leading_maxcond(fra) fra_subconds.append(fra_subcond) sample_counter = sample_counter + 1 np.savetxt("./test_result/test_result" + str(counter) + "_z_mod_a.txt",mat) os.mkdir("./test_result/fra_" + str(counter) + "_" + str(sample_counter)) for i in range(0,len(fras)): np.savetxt("./test_result/fra_" + str(counter) + "_" + str(sample_counter) + "/" + str(i) + ".txt", fras[i]) output = open("./test_result/test_result" + str(counter) + "_z_a_subcond.txt",'w') output.write(str(a_subcond)) output.close() output = open("./test_result/test_result" + str(counter) + "_z_fra_subcond.txt",'w') output.write(str(fra_subconds)) output.close() output = open("./test_result/test_result" + str(counter) + "_z_mca.txt",'w') output.write(str(mca)) output = open("./test_result/test_result" + str(counter) + "_z_mcfra.txt",'w') output.write(str(mcfra)) output.close() output = open("./test_result/test_result" + str(counter) + "_z_det.txt",'w') output.write(str(np.linalg.det(mat))) output.close() output = open("./test_result/test_result" + str(counter) + "_z_cdnum.txt",'w') output.write(str(linalg.cond(mat))) output.close() counter = counter + 1 print(str(counter) + "detected")