Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Решение СЛАУ с помощью QR-разложения
- import numpy as np
- import math
- def vec_in_numb(a, b, n):
- count = 0
- for i in range(n):
- count += a[i] * b[i]
- return count
- def Gramm_Schmidt(matrix):
- A = matrix.copy()
- B = np.array(np.zeros((A.shape[0], A.shape[1])))
- B[0][...] = A[0][...]
- for i in range(1, A.shape[0]):
- a = A[i][...]
- countR = np.array(np.zeros((A.shape[0])))
- for j in range(0, i):
- b = B[j][...]
- countB = vec_in_numb(a, b, A.shape[0])
- countA = vec_in_numb(b, b, A.shape[0])
- countB = countB / countA
- countR -= countB * b
- B[i][...] = a + countR
- return Orthog(B)
- def Orthog(B):
- for i in range(B.shape[0]):
- count = 0
- for k in range(B.shape[0]):
- count += math.fabs(B[i][k])**2
- B[i][...] /= round(math.sqrt(count), 3)
- return B
- def find_R(Q, A):
- R = np.array(np.zeros((Q.shape[0], Q.shape[1])))
- for i in range(Q.shape[0]):
- for j in range(Q.shape[0]):
- for k in range(Q.shape[0]):
- R[i][j] += round(Q[i][k] * A[k][j], 3)
- return R
- A = np.array([[2.2, 4, -3, 1.5, 0.6, 2, 0.7],
- [4, 3.2, 1.5, -0.7, -0.8, 3, 1],
- [-3, 1.5, 1.8, 0.9, 3, 2, 2],
- [1.5, -0.7, 0.9, 2.2, 4, 3, 1],
- [0.6, -0.8, 3, 4, 3.2, 0.6, 0.7],
- [2, 3, 2, 3, 0.6, 2.2, 4],
- [0.7, 1, 2, 1, 0.7, 4, 3.2]])
- Q = Gramm_Schmidt(np.transpose(A))
- R = find_R(Q, A)
- for i in range(Q.shape[0]):
- for j in range(Q.shape[0]):
- Q[i][j] = round(Q[i][j], 4)
- R[i][j] = round(R[i][j], 4)
- #print(Q)
- print(R)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement