Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 23.05 KB | None | 0 0
  1. # TO BE COMPILED WITH PYTHON >= 3.0
  2.  
  3. from fraction import *
  4. import matplotlib.pyplot as plt
  5. import math
  6. import time
  7. import random as rnd
  8. from decimal import *
  9. from typing import TypeVar, Generic
  10.  
  11. T = TypeVar('T')
  12.  
  13.  
  14. class Matrix:
  15.     def __init__(self, T, n):
  16.         self.T = T
  17.         self.n = n
  18.         self.A = [[T(rnd.randint(1, 1000)) for i in range(n)] for j in range(n)]
  19.         self.X = [T(rnd.randint(1, 1000)) for i in range(n)]
  20.         self.B = [self.T(0) for i in range(n)]
  21.         self.C = [[T(0) for i in range(self.n + 1)] for j in range(self.n + 1)]
  22.  
  23.         self.find_B(True)
  24.  
  25.     def find_B(self, new_mults):
  26.         n = self.n
  27.         for i in range(n):
  28.             for j in range(n):
  29.                 self.B[i] += self.A[i][j] * self.X[j]
  30.  
  31.         self.create_C_matrix(new_mults)
  32.  
  33.     def convert_types(self, T):
  34.         n = self.n
  35.         self.C = [[T(0) for i in range(self.n + 1)] for j in range(self.n + 1)]
  36.         m = len(self.C[0])
  37.         newX = []
  38.         newB = []
  39.  
  40.         if isinstance(self.A[0][0], Fraction):
  41.             for i in range(n):
  42.                 for j in range(n):
  43.                     self.A[i][j] = self.A[i][j].decimal
  44.                 newX.append(self.X[i].decimal)
  45.                 newB.append(self.B[i].decimal)
  46.         else:
  47.             for i in range(n):
  48.                 for j in range(n):
  49.                     self.A[i][j] = T(self.A[i][j])
  50.                 newX.append(T(self.X[i]))
  51.                 newB.append(T(self.B[i]))
  52.         self.X = newX
  53.         self.B = newB
  54.         self.find_B(False)
  55.  
  56.     def create_C_matrix(self, new_mults):
  57.         if new_mults:
  58.             r1 = self.generate_mult()
  59.             r2 = self.generate_mult()
  60.         else:
  61.             r1 = 1
  62.             r2 = 1
  63.         for i in range(self.n):
  64.             for j in range(self.n):
  65.                 self.C[i][j] = self.A[i][j] * r1
  66.         for i in range(self.n):
  67.             self.C[i][self.n] = self.B[i] * r2
  68.  
  69.     def print_A(self):
  70.         s = "MATRIX A\n"
  71.         for i in range(self.n):
  72.             for j in range(self.n):
  73.                 s += str(self.A[i][j]) + "   "
  74.             s += "\n"
  75.         print(s)
  76.  
  77.     def print_B(self):
  78.         s = "MATRIX B\n"
  79.         for i in range(self.n):
  80.             s += str(self.B[i]) + "\n"
  81.         print(s)
  82.  
  83.     def print_C(self):
  84.         s = "MATRIX C\n"
  85.         for i in range(self.n):
  86.             for j in range(self.n + 1):
  87.                 s += str(self.C[i][j]) + "   "
  88.             s += "\n"
  89.         print(s)
  90.  
  91.     def print_X(self):
  92.         s = "MATRIX X\n"
  93.         for i in range(self.n):
  94.             s += str(self.X[i]) + "   "
  95.         s += "\n"
  96.         print(s)
  97.  
  98.     def print_All(self):
  99.         self.print_A()
  100.         self.print_B()
  101.         self.print_C()
  102.         self.print_X()
  103.  
  104.     def generate_mult(self):
  105.         return self.T(rnd.randint(-65536, 65535) / 65536)
  106.  
  107.     def swap_rows(self, C, current_i, current_j):
  108.         n = len(C)
  109.         m = len(C[0])
  110.         T = self.T
  111.  
  112.         max_index = 0
  113.         max1 = T(0)
  114.         found = False
  115.         for i in range(current_i, n):
  116.             if T(C[i][current_j]) > max1:
  117.                 max1 = C[i][current_j]
  118.                 max_index = i
  119.                 found = True
  120.         if found:
  121.             for j in range(m):
  122.                 temp = C[current_i][j]
  123.                 C[current_i][j] = C[max_index][j]
  124.                 C[max_index][j] = temp
  125.  
  126.         self.print_C()
  127.         return C
  128.  
  129.     def swap_all(self, C, current_i, current_j):
  130.         n = len(C)
  131.         m = len(C[0])
  132.         T = self.T
  133.  
  134.         max1 = 0
  135.         max_row = 0
  136.         max_col = 0
  137.         found = False
  138.  
  139.         for i in range(current_i, n):
  140.             for j in range(current_j, m):
  141.                 if C[i][j] > max1:
  142.                     max1 = C[i][j]
  143.                     max_row = i
  144.                     max_col = j
  145.                     found = True
  146.         if found:
  147.             for j in range(m):
  148.                 temp = C[current_i][j]
  149.                 C[current_i][j] = C[max_row][j]
  150.                 C[max_row][j] = temp
  151.             for i in range(n):
  152.                 temp = C[i][current_j]
  153.                 C[i][current_j] = C[i][max_col]
  154.                 C[i][max_col] = temp
  155.  
  156.         self.print_C()
  157.         return C
  158.  
  159.     def gauss(self):
  160.         n = self.n
  161.         ret = [self.T(0) for i in range(n)]
  162.         C = self.C
  163.         m = len(C)
  164.  
  165.         print("C BEFORE:\n")
  166.         self.print_C()
  167.  
  168.         for i in range(n):
  169.             if C[i][i] == 0:
  170.                 return
  171.             first = C[i][i]
  172.             for k in range(m):
  173.                 C[i][k] = C[i][k] / first
  174.  
  175.             for j in range(i + 1, n + 1):
  176.                 for k in range(m):
  177.                     C[j][k] -= C[i][k] * C[j][i]
  178.  
  179.         for i in range(n - 1, -1, -1):
  180.             ret[i] = C[i][n] / C[i][i]
  181.             for k in range(i - 1, -1, -1):
  182.                 C[k][n] -= C[k][i] * ret[i]
  183.         print("C AFTER:\n")
  184.         self.print_C()
  185.         self.print_X()
  186.         return ret
  187.  
  188.     def gauss_part(self):
  189.         n = self.n
  190.         ret = [self.T(0) for i in range(n)]
  191.         C = self.C
  192.         m = len(C)
  193.  
  194.         print("C BEFORE:\n")
  195.         self.print_C()
  196.  
  197.         for i in range(n):
  198.  
  199.             C = self.swap_rows(C, i, i) ## dodane
  200.  
  201.             if C[i][i] == 0:
  202.                 C = self.swap_rows(C, i, i)
  203.             first = C[i][i]
  204.             for k in range(m):
  205.                 C[i][k] /= first
  206.  
  207.             for j in range(i + 1, n + 1):
  208.                 for k in range(m):
  209.                     C[j][k] -= C[i][k] * C[j][i]
  210.             self.print_C()
  211.  
  212.         for i in range(n - 1, -1, -1):
  213.             ret[i] = C[i][n] / C[i][i]
  214.             for k in range(i - 1, -1, -1):
  215.                 C[k][n] -= C[k][i] * ret[i]
  216.  
  217.         print("C AFTER:\n")
  218.         self.print_C()
  219.         self.print_X()
  220.         return ret
  221.  
  222.     def gauss_full(self):
  223.         n = self.n
  224.         ret = [self.T(0) for i in range(n)]
  225.         C = self.C
  226.         m = len(C)
  227.  
  228.         print("C BEFORE:\n")
  229.         self.print_C()
  230.  
  231.         for i in range(n):
  232.  
  233.             C = self.swap_all(C, i, i) ## dodane
  234.  
  235.             if C[i][i] == 0:
  236.                 C = self.swap_all(C, i, i)
  237.  
  238.             first = C[i][i]
  239.             for k in range(m):
  240.                 C[i][k] /= first
  241.  
  242.             for j in range(i + 1, n + 1):
  243.                 for k in range(m):
  244.                     C[j][k] -= C[i][k] * C[j][i]
  245.             self.print_C()
  246.  
  247.         for i in range(n - 1, -1, -1):
  248.             ret[i] = C[i][n] / C[i][i]
  249.             for k in range(i - 1, -1, -1):
  250.                 C[k][n] -= C[k][i] * ret[i]
  251.  
  252.         print("C AFTER:\n")
  253.         self.print_C()
  254.         self.print_X()
  255.         return ret
  256.  
  257.  
  258. class Tests:
  259.     global sizes_array
  260.     global results_Q1_method1, results_Q1_method2
  261.     global results_Q2_tf, results_Q2_td, results_Q2_tc
  262.  
  263.     def __init__(self):
  264.         self.tf_result = 0
  265.         self.tc_result = 0
  266.         self.td_result = 0
  267.  
  268.  
  269.     @staticmethod
  270.     def count_error(X, Xprim, size):  # counting an error with fraction matrix result usage
  271.         error_summary = 0
  272.         # if isinstance(X[0], Fraction):
  273.         #     for i in range(size):
  274.         #         error_summary = (X[i] - Xprim[i]).abs()
  275.         #         size_fract = Fraction(size)
  276.         #     return (error_summary / size_fract)
  277.         # else:
  278.         for i in range(size):
  279.             error_summary += math.fabs(X[i] - Xprim[i])
  280.         return math.log(error_summary / size)
  281.  
  282.     # zbadanie czasu i bledu dla n = size_large dla macierzy kazdego typu z uzyciem kazdej metody
  283.  
  284.     def test_1(self):  # dla odpowiedzenia na H1, H2, H3
  285.         # tc_error_sum_method1 = 0
  286.         # tc_error_sum_method2 = 0
  287.         # tc_error_sum_method3 = 0
  288.  
  289.         tf_error_sum_method1 = 0
  290.         tf_error_sum_method2 = 0
  291.         tf_error_sum_method3 = 0
  292.  
  293.         td_error_sum_method1 = 0
  294.         td_error_sum_method2 = 0
  295.         td_error_sum_method3 = 0
  296.  
  297.         tf_time_sum_method1 = 0
  298.         tf_time_sum_method2 = 0
  299.         tf_time_sum_method3 = 0
  300.         #
  301.         # tc_time_sum_method1 = 0
  302.         # tc_time_sum_method2 = 0
  303.         # tc_time_sum_method3 = 0
  304.  
  305.         td_time_sum_method1 = 0
  306.         td_time_sum_method2 = 0
  307.         td_time_sum_method3 = 0
  308.  
  309.         size = 50
  310.         repeats = 5
  311.         for i in range(repeats):
  312.             getcontext().prec = 7
  313.             matrix = Matrix(Decimal, size)  # Tutaj podmienic rozmiar macierzy (zaleznie od sizes_array, czy jak wolisz)
  314.  
  315.             ####################################################3 stare
  316.             # METODA I gauss
  317.             # typ ulamkowy
  318.  
  319.             # start_time = time.time()
  320.             # self.tc_result = matrix.gauss()
  321.             # elapsed_time = time.time() - start_time
  322.             # tc_time_method1 = elapsed_time
  323.             #
  324.             # tc_error_method1 = self.count_error(self.tc_result, matrix.X, size)
  325.  
  326.             # typ pojedynczej prezycji
  327.             getcontext().prec = 7
  328.             matrix.convert_types(Decimal)
  329.  
  330.             start_time = time.time()
  331.             self.tf_result = matrix.gauss()
  332.             elapsed_time = time.time() - start_time
  333.             tf_time_method1 = elapsed_time
  334.  
  335.             tf_error_method1 = self.count_error(self.tf_result, matrix.X, size)
  336.  
  337.             # typ podwojnej prezycji
  338.             getcontext().prec = 16
  339.             matrix.convert_types(Decimal)
  340.  
  341.             start_time = time.time()
  342.             self.td_result = matrix.gauss()
  343.             elapsed_time = time.time() - start_time
  344.             td_time_method1 = elapsed_time
  345.  
  346.             td_error_method1 = self.count_error(self.td_result, matrix.X, size)
  347.  
  348.             # METODA II gauss_part
  349.             # typ ulamkowy
  350.  
  351.             # matrix.convert_types(Fraction)
  352.             #
  353.             # start_time = time.time()
  354.             # self.tc_result = matrix.gauss_part()
  355.             # elapsed_time = time.time() - start_time
  356.             # tc_time_method2 = elapsed_time
  357.             #
  358.             # tc_error_method2 = self.count_error(self.tc_result, matrix.X, size)
  359.  
  360.             # typ pojedynczej precyzji
  361.             getcontext().prec = 7
  362.             matrix.convert_types(Decimal)
  363.  
  364.             start_time = time.time()
  365.             self.tf_result = matrix.gauss_part()
  366.             elapsed_time = time.time() - start_time
  367.             tf_time_method2 = elapsed_time
  368.  
  369.             tf_error_method2 = self.count_error(self.tf_result, matrix.X, size)
  370.  
  371.             # typ podwojnej precyzji
  372.             getcontext().prec = 16
  373.             matrix.convert_types(Decimal)
  374.  
  375.             start_time = time.time()
  376.             self.td_result = matrix.gauss_part()
  377.             elapsed_time = time.time() - start_time
  378.             td_time_method2 = elapsed_time
  379.  
  380.             td_error_method2 = self.count_error(self.td_result, matrix.X, size)
  381.  
  382.             # metoda III gauss_full
  383.             # typ ulamkowy
  384.             # matrix.convert_types(Fraction)
  385.             #
  386.             # start_time = time.time()
  387.             # self.tc_result = matrix.gauss_full()
  388.             # elapsed_time = time.time() - start_time
  389.             # tc_time_method3 = elapsed_time
  390.             #
  391.             # tc_error_method3 = self.count_error(self.tc_result, matrix.X, size)
  392.  
  393.             # typ pojedynczej precyzji
  394.             getcontext().prec = 7
  395.             matrix.convert_types(Decimal)
  396.  
  397.             start_time = time.time()
  398.             self.tf_result = matrix.gauss_full()
  399.             elapsed_time = time.time() - start_time
  400.             tf_time_method3 = elapsed_time
  401.  
  402.             tf_error_method3 = self.count_error(self.tf_result, matrix.X, size)
  403.  
  404.             # typ podwojnej precyzji
  405.             getcontext().prec = 16
  406.             matrix.convert_types(Decimal)
  407.  
  408.             start_time = time.time()
  409.             self.td_result = matrix.gauss_full()
  410.             elapsed_time = time.time() - start_time
  411.             td_time_method3 = elapsed_time
  412.  
  413.             td_error_method3 = self.count_error(self.td_result, matrix.X, size)
  414.  
  415.             # dla policzenia srednich bledow
  416.  
  417.             # tc_error_sum_method1 += tc_error_method1
  418.             # tc_error_sum_method2 += tc_error_method2
  419.             # tc_error_sum_method3 += tc_error_method3
  420.  
  421.             tf_error_sum_method1 += tf_error_method1
  422.             tf_error_sum_method2 += tf_error_method2
  423.             tf_error_sum_method3 += tf_error_method3
  424.  
  425.             td_error_sum_method1 += td_error_method1
  426.             td_error_sum_method2 += td_error_method2
  427.             td_error_sum_method3 += td_error_method3
  428.  
  429.             # dla policzenia srednich czasow
  430.  
  431.             tf_time_sum_method1 += tf_time_method1
  432.             tf_time_sum_method2 += tf_time_method2
  433.             tf_time_sum_method3 += tf_time_method3
  434.  
  435.             # tc_time_sum_method1 += tc_time_method1
  436.             # tc_time_sum_method2 += tc_time_method2
  437.             # tc_time_sum_method3 += tc_time_method3
  438.  
  439.             td_time_sum_method1 += td_time_method1
  440.             td_time_sum_method2 += td_time_method2
  441.             td_time_sum_method3 += td_time_method3
  442.  
  443.         # tc_error_sum_method1 /= repeats
  444.         # tc_error_sum_method2 /= repeats
  445.         # tc_error_sum_method3 /= repeats
  446.  
  447.         tf_error_sum_method1 /= repeats
  448.         tf_error_sum_method2 /= repeats
  449.         tf_error_sum_method3 /= repeats
  450.  
  451.         td_error_sum_method1 /= repeats
  452.         td_error_sum_method2 /= repeats
  453.         td_error_sum_method3 /= repeats
  454.  
  455.         tf_time_sum_method1 /= repeats
  456.         tf_time_sum_method2 /= repeats
  457.         tf_time_sum_method3 /= repeats
  458.  
  459.         # tc_time_sum_method1 /= repeats
  460.         # tc_time_sum_method2 /= repeats
  461.         # tc_time_sum_method3 /= repeats
  462.  
  463.         td_time_sum_method1 /= repeats
  464.         td_time_sum_method2 /= repeats
  465.         td_time_sum_method3 /= repeats
  466.  
  467.  
  468.         plt.scatter([1, 2, 3], [tf_error_sum_method1, tf_error_sum_method2, tf_error_sum_method3],
  469.                     label="dla dokladnosci pojedynczej precyzji", marker=".")
  470.         plt.scatter([1, 2, 3], [td_error_sum_method1, td_error_sum_method2, td_error_sum_method3],
  471.                     label="dla dokladnosci podwojnej precyzji", marker=".")
  472.         # plt.scatter([tc_error_sum_method1, tc_error_sum_method2, tc_error_sum_method3], [1, 2, 3],
  473.         #             label="dla typu fraction", marker=".")
  474.         plt.xlabel('Numery metod 1-G, 2-PG, 3-FG')
  475.         plt.ylabel('Srednia logarytmu z bledu dokladnosci')
  476.         plt.title('Wykres pokazujacy log z dokladnosci wynikow\n w zaleznosci od metody i rodzaju precyzji danych')
  477.         plt.legend()
  478.         plt.show()
  479.  
  480.         plt.scatter([1, 2, 3], [tf_time_sum_method1, tf_time_sum_method2, tf_time_sum_method3],
  481.                     label="dla dokladnosci pojedynczej precyzji", marker=".")
  482.         plt.scatter([1, 2, 3], [td_time_sum_method1, td_time_sum_method2, td_time_sum_method3],
  483.                     label="dla dokladnosci podwojnej precyzji", marker=".")
  484.         # plt.scatter([tc_time_sum_method1, tc_time_sum_method2, tc_time_sum_method3], [1, 2, 3],
  485.         #             label="dla dokladnosci własnego typu",
  486.         #             marker=".")
  487.         plt.xlabel('Numery metod 1-G, 2-PG, 3-FG')
  488.         plt.ylabel('Czas potrzbeny na wyliczenie metod')
  489.         plt.title('Wykres pokazujacy czas obliczania\n w zaleznosci od metody i rodzaju dokladnosci danych')
  490.         plt.legend()
  491.         plt.show()
  492.  
  493.     def test_2(self):  # dla odpowiedzi na Q1
  494.         getcontext().prec = 16
  495.         repeats = 5
  496.  
  497.         for size in sizes_array:
  498.             results_Q1_method1_sum = 0
  499.             results_Q1_method2_sum = 0
  500.             results_Q2_td_sum = 0
  501.             for i in range(repeats):
  502.                 matrix = Matrix(Decimal, size)
  503.                 # metoda gauss
  504.                 start_time = time.time()
  505.                 self.td_result = matrix.gauss()
  506.                 elapsed_time = time.time() - start_time
  507.                 results_Q2_td_sum += elapsed_time
  508.  
  509.                 results_Q1_method1_sum += (self.count_error(self.td_result, matrix.X, size))
  510.  
  511.                 # metoda gauss_part
  512.                 self.td_result = matrix.gauss_part()
  513.  
  514.                 results_Q1_method2_sum += (self.count_error(self.td_result, matrix.X, size))
  515.  
  516.             results_Q1_method1.append(results_Q1_method1_sum/repeats)
  517.             results_Q1_method2.append(results_Q1_method2_sum/repeats)
  518.             results_Q2_td.append(results_Q2_td_sum/repeats) # liczymy juz czas ktory bedzie wykorzystywany w test_3
  519.  
  520.         plt.scatter(sizes_array, results_Q1_method1, label="dla metody G", marker=".")
  521.         plt.scatter(sizes_array, results_Q1_method2, label="dla metody GP", marker=".")
  522.         plt.xlabel("Rozmiary macierzy")
  523.         plt.ylabel('Logarytm ze sredniego bledu dokladnosci')
  524.         plt.title(
  525.             'Wykres pokazujacy dokladnosc wynikow\n w zaleznosci od ilosci skladnikow dla typu podwojnej precyzji')
  526.         plt.legend()
  527.         plt.show()
  528.  
  529.     def test_3(self):  # dla odpowiedzi na Q2
  530.         getcontext().prec = 7
  531.         repeats = 5
  532.  
  533.         for size in sizes_array:  # dla metody G-gauss
  534.             results_Q2_tf_sum = 0
  535.             results_Q2_tc_sum = 0
  536.             for i in range(repeats):
  537.                 getcontext().prec = 7
  538.                 matrix = Matrix(Decimal, size)
  539.  
  540.                 start_time = time.time()
  541.                 self.tf_result = matrix.gauss()
  542.                 elapsed_time = time.time() - start_time
  543.                 results_Q2_tf_sum += elapsed_time
  544.  
  545.                 # matrix.convert_types(Fraction)
  546.                 # start_time = time.time()
  547.                 # self.tc_result = matrix.gauss()
  548.                 # elapsed_time = time.time() - start_time
  549.                 # results_Q2_tf_sum += elapsed_time
  550.  
  551.             results_Q2_tf.append(results_Q2_tf_sum/repeats)
  552.             results_Q2_tc.append(results_Q2_tc_sum/repeats)
  553.  
  554.         # czasy dla tej metody dla typow frac i double policzylismy w test_2
  555.  
  556.         plt.scatter(sizes_array, results_Q2_tf, label="dla typu pojedynczej precyzji", marker=".")
  557.         plt.scatter(sizes_array, results_Q2_td, label="dla typu podwojnen precyzji", marker=".") # policzone w test_2
  558.         # plt.scatter(results_Q2_tc, sizes_array, label="dla typu fraction", marker=".")
  559.         plt.xlabel("Rozmiary macierzy")
  560.         plt.ylabel('Czas trwania obliczania metody G-gauss')
  561.         plt.title('Wykres pokazujacy czas obliczania w zaleznosci od roznych typow macierzy')
  562.         plt.legend()
  563.         plt.show()
  564.  
  565.     def test_4(self):  # dla odpowiedzenia na H1, H2, H3
  566.  
  567.         tf_time_sum_method1 = 0
  568.         tf_time_sum_method2 = 0
  569.         tf_time_sum_method3 = 0
  570.  
  571.         # tc_time_sum_method1 = 0
  572.         # tc_time_sum_method2 = 0
  573.         # tc_time_sum_method3 = 0
  574.  
  575.         td_time_sum_method1 = 0
  576.         td_time_sum_method2 = 0
  577.         td_time_sum_method3 = 0
  578.  
  579.         size = 500
  580.         getcontext().prec = 7
  581.         matrix = Matrix(Decimal, size)  # Tutaj podmienic rozmiar macierzy (zaleznie od sizes_array, czy jak wolisz)
  582.  
  583.         ####################################################3 stare
  584.         # METODA I gauss
  585.         # typ ulamkowy
  586.  
  587.         # start_time = time.time()
  588.         # self.tc_result = matrix.gauss()
  589.         # elapsed_time = time.time() - start_time
  590.         # tc_time_method1 = elapsed_time
  591.  
  592.         # typ pojedynczej prezycji
  593.         getcontext().prec = 7
  594.         matrix.convert_types(Decimal)
  595.  
  596.         start_time = time.time()
  597.         self.tf_result = matrix.gauss()
  598.         elapsed_time = time.time() - start_time
  599.         tf_time_method1 = elapsed_time
  600.  
  601.         # typ podwojnej prezycji
  602.         getcontext().prec = 16
  603.         matrix.convert_types(Decimal)
  604.  
  605.         start_time = time.time()
  606.         self.td_result = matrix.gauss()
  607.         elapsed_time = time.time() - start_time
  608.         td_time_method1 = elapsed_time
  609.  
  610.         # METODA II gauss_part
  611.         # typ ulamkowy
  612.  
  613.         # matrix.convert_types(Fraction)
  614.         #
  615.         # start_time = time.time()
  616.         # self.tc_result = matrix.gauss_part()
  617.         # elapsed_time = time.time() - start_time
  618.         # tc_time_method2 = elapsed_time
  619.  
  620.         # typ pojedynczej precyzji
  621.         getcontext().prec = 7
  622.         matrix.convert_types(Decimal)
  623.  
  624.         start_time = time.time()
  625.         self.tf_result = matrix.gauss_part()
  626.         elapsed_time = time.time() - start_time
  627.         tf_time_method2 = elapsed_time
  628.  
  629.         # typ podwojnej precyzji
  630.         getcontext().prec = 16
  631.         matrix.convert_types(Decimal)
  632.  
  633.         start_time = time.time()
  634.         self.td_result = matrix.gauss_part()
  635.         elapsed_time = time.time() - start_time
  636.         td_time_method2 = elapsed_time
  637.  
  638.         # metoda III gauss_full
  639.         # typ ulamkowy
  640.         # matrix.convert_types(Fraction)
  641.         #
  642.         # start_time = time.time()
  643.         # self.tc_result = matrix.gauss_full()
  644.         # elapsed_time = time.time() - start_time
  645.         # tc_time_method3 = elapsed_time
  646.  
  647.         # typ pojedynczej precyzji
  648.         getcontext().prec = 7
  649.         matrix.convert_types(Decimal)
  650.  
  651.         start_time = time.time()
  652.         self.tf_result = matrix.gauss_full()
  653.         elapsed_time = time.time() - start_time
  654.         tf_time_method3 = elapsed_time
  655.  
  656.         # typ podwojnej precyzji
  657.         getcontext().prec = 16
  658.         matrix.convert_types(Decimal)
  659.  
  660.         start_time = time.time()
  661.         self.td_result = matrix.gauss_full()
  662.         elapsed_time = time.time() - start_time
  663.         td_time_method3 = elapsed_time
  664.  
  665.         # dla policzenia srednich czasow
  666.  
  667.         tf_time_sum_method1 += tf_time_method1
  668.         tf_time_sum_method2 += tf_time_method2
  669.         tf_time_sum_method3 += tf_time_method3
  670.  
  671.         # tc_time_sum_method1 += tc_time_method1
  672.         # tc_time_sum_method2 += tc_time_method2
  673.         # tc_time_sum_method3 += tc_time_method3
  674.  
  675.         td_time_sum_method1 += td_time_method1
  676.         td_time_sum_method2 += td_time_method2
  677.         td_time_sum_method3 += td_time_method3
  678.  
  679.  
  680.         plt.scatter([1, 2, 3], [tf_time_sum_method1, tf_time_sum_method2, tf_time_sum_method3],
  681.                     label="dla dokladnosci pojedynczej precyzji", marker=".")
  682.         plt.scatter([1, 2, 3], [td_time_sum_method1, td_time_sum_method2, td_time_sum_method3],
  683.                     label="dla dokladnosci podwojnej precyzji", marker=".")
  684.         # plt.scatter([tc_time_sum_method1, tc_time_sum_method2, tc_time_sum_method3], [1, 2, 3],
  685.         #             label="dla dokladnosci własnego typu",
  686.         #             marker=".")
  687.         plt.xlabel('Numery metod 1-G, 2-PG, 3-FG')
  688.         plt.ylabel('Czas potrzbeny na wyliczenie metod')
  689.         plt.title('Wykres pokazujacy czas obliczania\n w zaleznosci od metody i rodzaju dokladnosci danych')
  690.         plt.legend()
  691.         plt.show()
  692.  
  693. # MAIN
  694.  
  695. sizes_array = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]
  696. results_Q1_method1 = []
  697. results_Q1_method2 = []
  698.  
  699. results_Q2_tf = []
  700. results_Q2_td = []
  701. results_Q2_tc = []
  702.  
  703. test = Tests()
  704. test.test_1()
  705. test.test_2()
  706. test.test_3() # trzeba odpalic po test_2, bo tam sa wyliczane czesciowe wyniki uzyte w test_3
  707. # test.test_4()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement