-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvpowershift.py
More file actions
40 lines (32 loc) · 948 Bytes
/
invpowershift.py
File metadata and controls
40 lines (32 loc) · 948 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
33
34
35
36
37
38
39
40
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from triang import forsub, backsub, testcreate
from ludec import ludec
from jacobi import termcrit
from power import mag, testeigone
import numpy as np
#def invpowershift(A,shift=20,kmax=200,tol=1.e-2):
def invpowershift(A,shift=20,kmax=200,tol=1.e-8):
n = A.shape[0]
znews = np.ones(n)
qnews = znews/mag(znews)
Astar = A - np.identity(n)*shift
L, U = ludec(Astar)
for k in range(1,kmax):
qs = np.copy(qnews)
ys = forsub(L,qs)
znews = backsub(U,ys)
qnews = znews/mag(znews)
if qs@qnews<0:
qnews = -qnews
err = termcrit(qs,qnews)
print(k, qnews, err)
if err < tol:
lam = qnews@A@qnews
break
else:
lam = qnews = None
return lam, qnews
if __name__ == '__main__':
A, bs = testcreate(4,21)
testeigone(invpowershift,A)