forked from agezerlis/NumericalMethodsPhysicsWithPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqrdec.py
More file actions
32 lines (27 loc) · 727 Bytes
/
qrdec.py
File metadata and controls
32 lines (27 loc) · 727 Bytes
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
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from triang import testcreate
from power import mag
import numpy as np
def qrdec(A):
n = A.shape[0]
Ap = np.copy(A)
Q = np.zeros((n,n))
R = np.zeros((n,n))
for j in range(n):
for i in range(j):
R[i,j] = Q[:,i]@A[:,j]
Ap[:,j] -= R[i,j]*Q[:,i]
R[j,j] = mag(Ap[:,j])
Q[:,j] = Ap[:,j]/R[j,j]
return Q, R
def testqrdec(A):
n = A.shape[0]
Q, R = qrdec(A)
diffa = A - Q@R
diffq = np.transpose(Q)@Q - np.identity(n)
print(n, mag(diffa), mag(diffq))
if __name__ == '__main__':
for n in range(4,10,2):
A, bs = testcreate(n,21)
testqrdec(A)