forked from chr1shr/am205_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lanczos.py
49 lines (42 loc) · 1012 Bytes
/
lanczos.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
#!/usr/bin/python
import numpy as np
# Define the vector A
n=10
diag_vector=np.concatenate((np.linspace(0.,2.,n),np.array([2.5,3.])))
A=np.diag(diag_vector)
# Initialize Q, alpha and beta
iters=11
Q=np.zeros((n+2,iters+1))
alpha=np.zeros((iters))
beta=np.zeros((iters))
# Define a random initial vector
b=np.random.randn(n+2)
Q[:,0]=b/np.linalg.norm(b)
# The Lanczos iteration loop
for m in range(iters):
v=np.dot(A,Q[:,m])
alpha[m]=np.dot(Q[:,m],v)
if m==0:
v=v-alpha[m]*Q[:,m]
else:
v=v-beta[m-1]*Q[:,m-1]-alpha[m]*Q[:,m]
beta[m]=np.linalg.norm(v)
Q[:,m+1]=v/beta[m]
H_m=np.dot(np.dot(Q.T,A),Q)
[V,D]=np.linalg.eig(H_m)
# Save the characteristic polynomial
p=np.poly(H_m)
f=open("poly","w")
x=-0.5
while x<=3.5:
f.write(str(x)+" "+str(np.polyval(p,x))+"\n")
x+=0.01
f.close()
# Save the roots
f=open("roots","w")
for i in range(n+2):
f.write(str(diag_vector[i])+" 0\n")
f.write("\n\n")
for i in range(iters+1):
f.write(str(V[i])+" 0\n")
f.close()