Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def get_q(a, Q, n):
- u = a
- if n != 0:
- for j in range(0, n):
- u = u - (np.dot(a, Q[j]) * Q[j])
- q = u / np.linalg.norm(u)
- return q
- def get_Q(A):
- vectors_number = A.shape[0]
- Q = np.zeros(A.shape)
- for i in range(vectors_number):
- Q[i] = get_q(A[i], Q, i)
- return Q.T
- def get_R(A, Q):
- R = np.dot(Q.T, A)
- return R
- #-------Input section--------#
- elements = list(map(float, input('Enter elements: ').split(' ')))
- n = int(input('Enter rows N: '))
- m = int(input('Enter columns M: '))
- A = np.array(elements).reshape(n, m)
- print ('A : ')
- print (A)
- #-------Calculation--------#
- Q = get_Q(A.T)
- R = get_R(A, Q)
- print ('Q : ')
- print (Q)
- print ('R : ')
- print (R)
- print ('Pass test: ' + str(np.array_equal(A, np.dot(Q, R))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement