Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def get_q(a, Q, n):
  4.     u = a
  5.     if n != 0:    
  6.         for j in range(0, n):
  7.             u = u - (np.dot(a, Q[j]) * Q[j])
  8.     q = u / np.linalg.norm(u)
  9.     return q
  10.  
  11. def get_Q(A):
  12.     vectors_number = A.shape[0]
  13.     Q = np.zeros(A.shape)
  14.  
  15.     for i in range(vectors_number):
  16.         Q[i] = get_q(A[i], Q, i)
  17.    
  18.     return Q.T
  19.  
  20. def get_R(A, Q):
  21.     R = np.dot(Q.T, A)
  22.     return R
  23.  
  24.  
  25. #-------Input section--------#
  26. elements = list(map(float, input('Enter elements: ').split(' ')))
  27. n = int(input('Enter rows N: '))
  28. m = int(input('Enter columns M: '))
  29. A = np.array(elements).reshape(n, m)
  30. print ('A : ')
  31. print (A)
  32. #-------Calculation--------#
  33. Q = get_Q(A.T)
  34. R = get_R(A, Q)
  35. print ('Q : ')
  36. print (Q)
  37. print ('R : ')
  38. print (R)
  39. print ('Pass test: ' + str(np.array_equal(A, np.dot(Q, R))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement