Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
Archivo: main.py Proyecto: warelle/rdft
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))
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)