Advertisement
adrjan

boikwd

Oct 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.12 KB | None | 0 0
  1. from numpy import array, log10, ones, eye
  2. from numpy.linalg import solve
  3. import functools
  4. import operator
  5.  
  6. # data
  7. A = array([
  8.     [1, 2 / 3, 2, 5 / 2, 5 / 3, 5], [
  9.         3 / 2, 1, 3, 10 / 3, 3, 9], [
  10.         1 / 2, 1 / 3, 1, 4 / 3, 7 / 8, 5 / 2], [
  11.         2 / 5, 3 / 10, 3 / 4, 1, 5 / 6, 12 / 5], [
  12.         3 / 5, 1 / 3, 8 / 7, 6 / 5, 1, 3], [
  13.         1 / 5, 1 / 9, 2 / 5, 5 / 12, 1 / 3, 1]])
  14. vA = array([3, 1])
  15. positionsA = [5, 6]
  16. B = array([
  17.     [1, 2 / 5, 3, 7 / 3, 1 / 2, 1], [
  18.         5 / 2, 1, 4 / 7, 5 / 8, 1 / 3, 3], [
  19.         1 / 3, 7 / 4, 1, 1 / 2, 2, 1 / 2], [
  20.         3 / 7, 8 / 5, 2, 1, 4, 2], [
  21.         2, 3, 1 / 2, 1 / 4, 1, 1 / 2], [
  22.         1, 1 / 3, 2, 1 / 2, 2, 1]])
  23. vB = array([2, 1 / 2, 1])
  24. positionsB = [4, 5, 6]
  25. C = array([
  26.     [1, 17 / 4, 17 / 20, 8 / 5, 23 / 6, 8 / 3], [
  27.         4 / 17, 1, 1 / 5, 2 / 5, 9 / 10, 2 / 3], [
  28.         20 / 17, 5, 1, 21 / 10, 51 / 10, 10 / 3], [
  29.         5 / 8, 5 / 2, 10 / 21, 1, 5 / 2, 11 / 6], [
  30.         6 / 23, 10 / 9, 10 / 51, 2 / 5, 1, 19 / 30], [
  31.         3 / 8, 3 / 2, 3 / 10, 6 / 11, 30 / 19, 1]])
  32. vC = array([2, 5])
  33. positionsC = [2, 4]
  34.  
  35.  
  36. def get_koczkodaj_inconsistency_index(matrix: array):
  37.     max_val = 0
  38.     n = len(matrix)
  39.     for i in range(n):
  40.         for j in range(n):
  41.             for k in range(n):
  42.                 min_val = min(abs(1 - matrix[i][k] * matrix[k][j] / matrix[i][j]),
  43.                               abs(1 - matrix[i][j] / (matrix[i][k] * matrix[k][j])))
  44.                 max_val = max(max_val, min_val)
  45.     return max_val
  46.  
  47.  
  48. def create_A_B_matrices(matrix, known_values, positions):
  49.     n = len(matrix)
  50.     k = len(known_values)
  51.  
  52.     # check if swap is needed
  53.     swap_wrong_columns_and_rows(matrix, n, positions)
  54.  
  55.     A = matrix[0:(n - k), 0:(n - k)]
  56.     B = matrix[0:(n - k), (n - k):n]
  57.  
  58.     for i in range(n - k):
  59.         for j in range(n - k):
  60.             if i != j:
  61.                 A[i][j] = A[i][j] * (-1 / (n - 1))
  62.  
  63.     B = array(B).dot(known_values)
  64.     B = B * (1 / (n - 1))
  65.     return [A, B]
  66.  
  67.  
  68. def swap_wrong_columns_and_rows(matrix, n, positions):
  69.     do_swap = False
  70.     index = n
  71.     k = len(positions)
  72.     for i in reversed(positions):
  73.         if index != i:
  74.             do_swap = True
  75.             break
  76.         else:
  77.             index = index - 1
  78.     if do_swap:
  79.         # mix matrice so vectors 'from' position are last in matrix
  80.         index = n - 1
  81.         for position in reversed(positions):
  82.             matrix[:, [position-1, index]] = matrix[:, [index, position-1]]
  83.             index = index - 1
  84.  
  85.         index = n - 1
  86.         for position in reversed(positions):
  87.             matrix[[position - 1, index], :] = matrix[[index, position - 1], :]
  88.             index = index - 1
  89.  
  90.  
  91. def insert_on_correct_positions(known_values, positions, w):
  92.     for i in range(len(positions)):
  93.         w.insert(positions[i] - 1, known_values[i])
  94.  
  95.  
  96. def HRE_arithmetic(matrix: array, known_values: array, positions: list):
  97.     matrix = matrix.copy()
  98.     koczkodaj_index = get_koczkodaj_inconsistency_index(matrix)
  99.     n = len(matrix)
  100.     k = len(known_values)
  101.     has_solution = koczkodaj_index < 1 - (1 + (4 * (n - 1) * (n - k - 2)) ** 0.5) / (2 * (n - 1))
  102.  
  103.     [A, B] = create_A_B_matrices(matrix, known_values, positions)
  104.     solution = solve(A, B).tolist()
  105.  
  106.     insert_on_correct_positions(known_values, positions, solution)
  107.  
  108.     for s in solution:
  109.         if s < 0:
  110.             has_solution = False
  111.             break
  112.     return [has_solution, solution]
  113.  
  114.  
  115. def HRE_geometric(matrix: array, known_values: array, positions: list):
  116.     def foldl(func, acc, xs):
  117.         return functools.reduce(func, xs, acc)
  118.  
  119.     matrix = matrix.copy()
  120.     n = len(matrix)
  121.     swap_wrong_columns_and_rows(matrix, n, positions)
  122.  
  123.     k = len(positions)
  124.     b_ = []
  125.     multiplied_know_values = foldl(operator.mul, 1, known_values)
  126.     for i in range(n - k):
  127.         tmp = foldl(operator.mul, multiplied_know_values, matrix[i, :])
  128.         b_.append(log10(tmp))
  129.  
  130.     A_ = ones((n - k, n - k)) * (-1) + eye(n - k) * n
  131.  
  132.     W_ = solve(A_, b_)
  133.     solution = [10 ** x for x in W_]
  134.  
  135.     insert_on_correct_positions(known_values, positions, solution)
  136.     return solution
  137.  
  138.  
  139. def foldl(func, acc, xs):
  140.     return functools.reduce(func, xs, acc)
  141.  
  142.  
  143. # # A
  144. # [has_solution, solution] = HRE_arithmetic(A, vA, positionsA)
  145. # s = "It probably doesn't have solution -> "
  146. # if has_solution:
  147. #     s = "It has solution -> "
  148. # print("Arithmetic HRE for matrix A.", s, solution)
  149. # print("Geometric HRE for matrix A", HRE_geometric(A, vA, positionsA))
  150. #
  151. # # B
  152. # [has_solution, solution] = HRE_arithmetic(B, vB, positionsB)
  153. # s = "It probably doesn't have solution -> "
  154. # if has_solution:
  155. #     s = "It has solution -> "
  156. # print("Arithmetic HRE for matrix B", s, solution)
  157. # print("Geometric HRE for matrix B", HRE_geometric(B, vB, positionsB))
  158. #
  159. # # C
  160. # [has_solution, solution] = HRE_arithmetic(C, vC, positionsC)
  161. # s = "It probably doesn't have solution -> "
  162. # if has_solution:
  163. #     s = "It has solution -> "
  164. # print("Arithmetic HRE for matrix C", s, solution)
  165. print("Geometric HRE for matrix C", HRE_geometric(C, vC, positionsC))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement