-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQR2.py
53 lines (42 loc) · 1.23 KB
/
QR2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import math
from copy import deepcopy
from numpy import dot
# np.dot умножение матриц
def func():
return 2 * (x**3) + 3 * (x**2) + 5 * x + 7
A = np.loadtxt("1.txt")
x = np.loadtxt
def QR(A):
# копирование и транспонирование матрицы
At = A.transpose()
shape = np.shape(At)
Qt = np.zeros((shape[0], shape[1]))
r = np.zeros((shape[0], shape[0]))
for i in range(shape[0]):
for j in range(shape[1]):
Qt[i][j] = At[i][j]
norm = np.dot(Qt[0], Qt[0])
for i in range(shape[1]):
Qt[0][i] = Qt[0][i] / math.sqrt(norm)
for k in range(1, shape[0]):
for j in range(shape[0]):
if j < k:
Vkj = np.dot(Qt[j], Qt[k])
Qt[k] = Qt[k] - Vkj * Qt[j]
norm2 = dot(Qt[k], Qt[k])
for i in range(shape[1]):
Qt[k][i] = Qt[k][i] / math.sqrt(norm2)
for i in range(shape[0]):
for j in range(shape[0]):
if j >= i:
r[i][j] = np.dot(At[j], Qt[i])
else:
r[i][j] = 0
print(Qt)
print("\n", r)
return 0
QR(A)
Qb, rD = np.linalg.qr(A)
print("\n", Qb.T)
print("\n", rD)