SHARE
TWEET

Untitled

a guest Dec 2nd, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Решение СЛАУ с помощью QR-разложения
  2.  
  3. import numpy as np
  4. import math
  5.  
  6. def vec_in_numb(a, b, n):
  7.     count = 0
  8.     for i in range(n):
  9.         count += a[i] * b[i]
  10.     return count
  11.  
  12. def Gramm_Schmidt(matrix):
  13.     A = matrix.copy()
  14.     B = np.array(np.zeros((A.shape[0], A.shape[1])))
  15.     B[0][...] = A[0][...]
  16.     for i in range(1, A.shape[0]):
  17.         a = A[i][...]
  18.         countR = np.array(np.zeros((A.shape[0])))
  19.         for j in range(0, i):
  20.             b = B[j][...]
  21.             countB = vec_in_numb(a, b, A.shape[0])
  22.             countA = vec_in_numb(b, b, A.shape[0])
  23.             countB = countB / countA
  24.             countR -= countB * b
  25.         B[i][...] = a + countR
  26.     return Orthog(B)
  27.  
  28. def Orthog(B):
  29.     for i in range(B.shape[0]):
  30.         count = 0
  31.         for k in range(B.shape[0]):
  32.             count += math.fabs(B[i][k])**2
  33.         B[i][...] /= round(math.sqrt(count), 3)
  34.     return B
  35.  
  36. def find_R(Q, A):
  37.     R = np.array(np.zeros((Q.shape[0], Q.shape[1])))
  38.     for i in range(Q.shape[0]):
  39.         for j in range(Q.shape[0]):
  40.             for k in range(Q.shape[0]):
  41.                 R[i][j] += round(Q[i][k] * A[k][j], 3)
  42.     return R
  43.  
  44.  
  45.  
  46. A = np.array([[2.2, 4, -3, 1.5, 0.6, 2, 0.7],
  47.               [4, 3.2, 1.5, -0.7, -0.8, 3, 1],
  48.               [-3, 1.5, 1.8, 0.9, 3, 2, 2],
  49.               [1.5, -0.7, 0.9, 2.2, 4, 3, 1],
  50.               [0.6, -0.8, 3, 4, 3.2, 0.6, 0.7],
  51.               [2, 3, 2, 3, 0.6, 2.2, 4],
  52.               [0.7, 1, 2, 1, 0.7, 4, 3.2]])
  53.  
  54. Q = Gramm_Schmidt(np.transpose(A))
  55. R = find_R(Q, A)
  56.  
  57. for i in range(Q.shape[0]):
  58.     for j in range(Q.shape[0]):
  59.         Q[i][j] = round(Q[i][j], 4)
  60.         R[i][j] = round(R[i][j], 4)
  61.  
  62. #print(Q)
  63. print(R)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top