Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. def Multiplication(low, main, up, vector):
  2. result_vector = []
  3. size = len(vector) - 1
  4. for i in range(0, len(vector)):
  5. tmp = main[i] * vector[i]
  6. if i != 0:
  7. tmp += low[i] * vector[i - 1]
  8. if i != size:
  9. tmp += up[i] * vector[i + 1]
  10. result_vector.append(tmp)
  11. return result_vector
  12.  
  13.  
  14. def Discrepancy(left, right):
  15. ttl = 0
  16. for i in range(0, len(left)):
  17. ttl += (left[i] - right[i]) ** 2
  18. return ttl ** 0.5
  19.  
  20.  
  21. class Matrix:
  22. def __init__(self, Up, Main, Low, F):
  23. self.up = Up
  24. self.main = Main
  25. self.low = Low
  26. self.f = F
  27.  
  28.  
  29. def Matvec(m, res, mode):
  30. if not mode:
  31. global tay
  32. return list(map(lambda x: x * tay, Multiplication(low=m.low, main=m.main, up=m.up, vector=res)))
  33.  
  34.  
  35. def SI_Solver(m, res0, mode):
  36. res = res0
  37. disc = disc0 = Discrepancy(Multiplication(low=m.low, main=m.main, up=m.up, vector=res0), m.f)
  38. disc_vector = [disc0]
  39. disc_size = 1
  40. while (1):
  41. v = Matvec(m, res, 0)
  42. disc = Discrepancy(v, m.f)
  43. disc_vector.append(disc)
  44. disc_size += 1
  45. if (disc / disc0 < 1e-4):
  46. break
  47. res = list(map(lambda f, x, v: f + x - v, m.f, res, v))
  48. return [res, disc_vector]
  49.  
  50.  
  51. up = [-1 for x in range(0, 999)] + [0]
  52. main = [12] + [2.001 for x in range(0, 999)]
  53. low = [0] + [-1 for x in range(0, 999)]
  54.  
  55. F = [1 if x >= 495 and x <= 505 else 0 for x in range(0, 1000)]
  56. result0 = [0 for x in range(0, 1000)]
  57.  
  58. # MPP
  59. tay = 0.1651512666112257
  60. mpp = list(map(lambda x: x*tay, m.f))
  61. m = Matrix(Up=up, Main=main, Low=low, F=mpp)
  62. answ = SI_Solver(m=m, res0=result0, mode=0)
  63. print(answ[0])
  64. print(len(answ[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement