Advertisement
575

MATH_MOD

575
Dec 21st, 2023
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. import copy
  4. from scipy.optimize import minimize
  5.  
  6. def cor(V,x,eps):
  7.     s = list()
  8.  
  9.     for i in V:
  10.         s.append(1 / (np.linalg.norm(x - i, ord=2) + eps))
  11.  
  12.     S = list()
  13.  
  14.     for i in s:
  15.         S.append(i / sum(s))
  16.  
  17.     print(S)
  18.     print(V)
  19.     return S[0] * V[0] + S[1] * V[1] + S[2] * V[2] + S[3] * V[3]
  20.  
  21. def coeffs(coef_lambda):
  22.     c = list()
  23.  
  24.     for i in coef_lambda:
  25.         c.append(sum(coef_lambda) / i)
  26.  
  27.     c1 = list()
  28.  
  29.     for i in c:
  30.         c1.append(i / sum(c))
  31.  
  32.     return c1
  33.  
  34. def step_alg(A, eps):
  35.     m = A.shape[1]
  36.     w = np.array([[]])
  37.  
  38.     for i in range(m):
  39.         w = np.append(w, [[1 / m]], axis=1)
  40.  
  41.     lam = pow(10, 10)
  42.     k = 0
  43.     w = w.T
  44.  
  45.     while True:
  46.         k = k + 1
  47.         w1 = copy.copy(w)
  48.         w = np.dot(A, w)
  49.         s = 0
  50.  
  51.         for i in range(A.shape[1]):
  52.             s = s+w[i][0] / w1[i][0]
  53.  
  54.         lam1 = copy.copy(lam)
  55.         lam = 1 / m * s
  56.         m = max(w.T[0])
  57.  
  58.         for i in range(A.shape[1]):
  59.             w[i][0] = w[i][0] / m
  60.  
  61.         if abs(lam - lam1) > eps:
  62.             continue
  63.  
  64.         else:
  65.             s=sum(w.T[0])
  66.  
  67.             for i in range(A.shape[1]):
  68.                 w[i][0] = w[i][0] / s
  69.  
  70.             break;
  71.     return w
  72.  
  73. def St(x):
  74.     sums = list()
  75.  
  76.     for i in V:
  77.         sums.append(np.linalg.norm(x - i, ord=2))
  78.  
  79.     sums = np.array(sums)
  80.     return sums
  81.  
  82. def Stein(x):
  83.     return np.sum(C * St(x))
  84.  
  85. A1=np.array([[1, 3, 7, 9],
  86.              [1/3, 1, 3, 1],
  87.              [1/7, 1/3, 1, 1],
  88.              [1/9, 1, 1, 1]])
  89. A2=np.array([[1, 5, 7, 9],
  90.              [1/5, 1, 3, 1],
  91.              [1/7, 1/3, 1, 1],
  92.              [1/9, 1, 1, 1]])
  93. A3=np.array([[1, 3, 7, 3],
  94.              [1/3, 1, 3, 1],
  95.              [1/7, 1/3, 1, 1,],
  96.              [1/3, 1, 1, 1]])
  97. A4=np.array([[1, 3, 7, 5],
  98.              [1/3, 1, 3, 1],
  99.              [1/7, 1/3, 1, 1],
  100.              [1/5, 1, 1, 1]])
  101.  
  102. w1=step_alg(A1, 0.0001)
  103. w2=step_alg(A2, 0.0001)
  104. w3=step_alg(A3, 0.0001)
  105. w4=step_alg(A4, 0.0001)
  106.  
  107. V = copy.deepcopy(w1.T)
  108.  
  109. V = np.append(V, copy.deepcopy(w2.T), axis=0)
  110. V = np.append(V, copy.deepcopy(w3.T), axis=0)
  111. V = np.append(V, copy.deepcopy(w4.T), axis=0)
  112.  
  113. lambda1,v = np.linalg.eig(A1)
  114. lambda2,v = np.linalg.eig(A2)
  115. lambda3,v1 = np.linalg.eig(A3)
  116. lambda4,v1 = np.linalg.eig(A4)
  117.  
  118. lambda1 = max(lambda1)
  119. lambda2 = max(lambda2)
  120. lambda3 = max(lambda3)
  121. lambda4 = max(lambda4)
  122. print(lambda1, lambda2, lambda3, lambda4)
  123.  
  124. C = np.array(coeffs([lambda1, lambda2, lambda3, lambda4]))
  125.  
  126. x0=np.array([0, 0, 0, 0])
  127. x=minimize(Stein, x0, method='nelder-mead', options={'xtol':1e-3,'disp':True}).x
  128. print(cor(V, x, 0.0001))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement