Advertisement
Purposelessness

Untitled

Dec 24th, 2022
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.29 KB | None | 0 0
  1. from tabulate import tabulate
  2. import numpy as np
  3. import math
  4.  
  5. node_arrX = []
  6. n = 15
  7. a = 0
  8. b = 4
  9. filename = "out.txt"
  10.  
  11.  
  12. def function(x):
  13.     return math.exp(-x) - np.arctan(x)
  14.  
  15.  
  16. h = (b - a) / (n - 1)
  17.  
  18. x_list: list[float] = [a + i * h for i in range(n)]
  19. xe_list: list[float] = []
  20. for i in range(n - 1):
  21.     xe_list.append(x_list[i])
  22.     xe_list.append((x_list[i] + x_list[i + 1]) / 2)
  23. xe_list.append(x_list[n - 1])
  24.  
  25. y_list: list[float] = [function(x) for x in x_list]
  26. ye_list: list[float] = [function(xe) for xe in xe_list]
  27.  
  28. # noinspection PyTypeChecker
  29. function_table = tabulate([["y"] + y_list, ["x"] + x_list],
  30.                           tablefmt="simple_outline", numalign="center", floatfmt=".9f")
  31. print(function_table)
  32. with open(filename, "w", encoding="utf-8") as f:
  33.     f.write(function_table)
  34.     f.write("\n\n")
  35.  
  36.  
  37. class Calculations:
  38.     def __init__(self, eps: int = 12):
  39.         self.eps = eps
  40.         self.x_list = [round(x, eps) for x in x_list]
  41.         self.xe_list = [round(xe, eps) for xe in xe_list]
  42.         self.y_list = [round(y, eps) for y in y_list]
  43.         self.ye_list = [round(ye, eps) for ye in ye_list]
  44.  
  45.         self.delta_matrix = self.get_delta_matrix(y_list)
  46.         self.delta_list = self.delta_matrix[0]
  47.         self.interpolation_list = self.get_interpolation_list(self.xe_list, self.y_list, self.x_list, self.delta_list)
  48.         self.delta_interpolation = self.get_delta_interpolation(self.interpolation_list, self.ye_list)
  49.  
  50.     @staticmethod
  51.     def get_delta_matrix(data_list: list[float]) -> list[list[float]]:
  52.         dim: int = len(data_list)
  53.         delta_matrix: list[list[float]] = [[0.0] * dim for _ in range(dim)]
  54.         for i in range(dim):
  55.             delta_matrix[i][0] = data_list[i]
  56.         for i in range(1, dim):
  57.             for j in range(dim - i):
  58.                 delta_matrix[j][i] = delta_matrix[j + 1][i - 1] - delta_matrix[j][i - 1]
  59.         return delta_matrix
  60.  
  61.     @staticmethod
  62.     def get_t_coefficient(t, num: int):
  63.         out = t
  64.         for i in range(num, 1, -1):
  65.             out *= t - i + 1
  66.         return out
  67.  
  68.     @staticmethod
  69.     def get_interpolation_list(xe_data: list[float], y_data: list[float], x_data: list[float],
  70.                                delta_list: list[float]) -> list[float]:
  71.         interpolation_list = []
  72.         for x in xe_data:
  73.             result = y_data[0]
  74.             t = (x - x_data[0]) / h
  75.             for i in range(1, len(delta_list)):
  76.                 coef = Calculations.get_t_coefficient(t, i)
  77.                 result += coef * delta_list[i] / math.factorial(i)
  78.             interpolation_list.append(result)
  79.         return interpolation_list
  80.  
  81.     @staticmethod
  82.     def get_delta_interpolation(interpolation_list: list[float], ye_data: list[float]) -> list[float]:
  83.         return [abs(interpolation_list[i] - ye_data[i]) for i in range(len(interpolation_list))]
  84.  
  85.  
  86. def print_delta_matrix(calc: Calculations) -> None:
  87.     table = calc.delta_matrix
  88.     table = np.insert(table, 0, calc.x_list, axis=1)
  89.     headers = ["x", "f"]
  90.     headers.extend([f"f{i}" for i in range(len(calc.delta_matrix) - 1)])
  91.     delta_table = tabulate(table, tablefmt="simple_outline", headers=headers, numalign="center", floatfmt="0.9f")
  92.     print(delta_table)
  93.     with open(filename, "a", encoding="utf-8") as f_delta:
  94.         f_delta.write(delta_table)
  95.         f_delta.write("\n\n")
  96.  
  97.  
  98. def get_column(data: list, eps=9) -> list:
  99.     column = []
  100.     for i in data:
  101.         i_str = format(i, f".{eps}f")
  102.         column.extend([i_str, " "])
  103.     return column
  104.  
  105.  
  106. def print_calculations(calc: Calculations, ce_list: list[Calculations]) -> None:
  107.     ie_list = [i / 2 for i in range(n * 2)]
  108.     del ie_list[-1]
  109.  
  110.     table = [ie_list, xe_list, calc.ye_list, calc.interpolation_list,
  111.              [abs(calc.interpolation_list[i] - calc.ye_list[i]) for i in range(len(ie_list))],
  112.              [abs((calc.interpolation_list[i] - calc.ye_list[i]) / calc.ye_list[i]) for i in range(len(ie_list))]]
  113.     for i in range(len(ce_list)):
  114.         table.append(get_column(ce_list[i].y_list, ce_list[i].eps))
  115.         table.append(ce_list[i].interpolation_list)
  116.         table.append([abs(ce_list[i].interpolation_list[j] - calc.interpolation_list[j]) for j in range(len(ie_list))])
  117.         table.append([abs((ce_list[i].interpolation_list[j] - calc.interpolation_list[j]) / calc.interpolation_list[j])
  118.                       for j in range(len(ie_list))])
  119.  
  120.     table = zip(*table)
  121.     headers = ["N", "x", "f(x)", "P(x)", "|f(x)-P(x)|", "δ(P(x))"]
  122.     floatfmt = [""]
  123.     floatfmt.extend([".9f" for _ in range(5 + len(ce_list) * 4)])
  124.     for i in range(len(ce_list)):
  125.         headers.extend([f"f*(x); {ce_list[i].eps}", "P*(x)", "|P(x)-P*(x)|", "δ(P*(x))"])
  126.     tab_table = tabulate(table, tablefmt="simple_outline", headers=headers, stralign="center",
  127.                          numalign="center", floatfmt=floatfmt)
  128.     with open(filename, "a", encoding="utf-8") as f_calc:
  129.         f_calc.write(tab_table)
  130.         f_calc.write("\n\n")
  131.     print(tab_table)
  132.  
  133.  
  134. def main() -> None:
  135.     calc: Calculations = Calculations()
  136.     print_delta_matrix(calc)
  137.     ce_list: list[Calculations] = []
  138.     for i in [6, 4, 2]:
  139.         ce_list.append(Calculations(i))
  140.     print_calculations(calc, ce_list)
  141.  
  142.  
  143. if __name__ == "__main__":
  144.     main()
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement