-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqrfact.py
203 lines (153 loc) · 5.21 KB
/
qrfact.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import numpy
def qr_mgs( A ):
"""QR decomposition of A.
Modified Gram-Schmidt algorithm, row version (Bjorck alg. 2.1)"""
A = numpy.array(A, dtype=float)
m,n = A.shape
Q = numpy.zeros( (m,n) )
R = numpy.zeros( (n,n) )
for k in range( 0, n ) :
R[k,k] = numpy.linalg.norm( A[:,k] )
Q[:,k] = A[:,k] / R[k,k]
for j in range( k+1, n ) :
R[k,j] = numpy.dot( Q[:,k], A[:,j] )
A[:,j] = A[:,j] - Q[:,k] * R[k,j]
return Q,R
def qri_cgs( A, alpha ):
"""QR decomposition of A.
Takes as arguments the matrix A and also a tolerance parameter alpha.
Iterated CGS algorithm (Bjorck alg. 6.1(1); Hoffman section 3)"""
A = numpy.array(A, dtype=float)
m,n = A.shape
Q1 = numpy.zeros( (m,n) )
R = numpy.zeros( (n,n) )
for k in range( 0, n ) :
Qhat = A[:,k]
#ii = 0
while True : # iterate
s = numpy.dot( numpy.transpose( Q1 ), Qhat )
Qhat2 = Qhat - numpy.dot( Q1, s )
R[:,k] = R[:,k] + s
#ii = ii + 1
Qhat2len = numpy.linalg.norm( Qhat2 )
Qhatlen = numpy.linalg.norm( Qhat )
if ( Qhat2len > alpha * Qhatlen ) :
Qhat = Qhat2
#print ii
break
Qhat = Qhat2
R[k,k] = numpy.linalg.norm( Qhat )
Q1[:,k] = Qhat / R[k,k]
return Q1,R
def qri_mgs( A, alpha ):
"""QR decomposition of A.
Takes as arguments the matrix A and also a tolerance parameter alpha.
Iterated MGS algorithm, column version (Bjorck alg. 6.1(2); Hoffman section 3)"""
A = numpy.array(A, dtype=float)
m,n = A.shape
Q1 = numpy.zeros( (m,n) )
R = numpy.zeros( (n,n) )
for k in range( 0, n ) :
Qhat = A[:,k]
Qhat2 = Qhat
ii = 0
while True : # iterate
for i in range( 0, k ) :
s = numpy.dot( Q1[:,i], Qhat )
Qhat2 = Qhat2 - s * Q1[:,i]
R[i,k] = R[i,k] + s
ii = ii + 1
Qhat2len = numpy.linalg.norm( Qhat2 )
Qhatlen = numpy.linalg.norm( Qhat )
if (Qhat2len > alpha * Qhatlen) :
Qhat = Qhat2
print ii
break
Qhat = Qhat2
R[k,k] = numpy.linalg.norm( Qhat )
Q1[:,k] = Qhat / R[k,k]
return Q1,R
def qrtest( A ):
"""QR decomposition of A.
Bootleg reorthogonalization.
Simply re-do the MGS QR factorization on Q to reorthogonalize."""
A = numpy.array(A, dtype=float)
m,n = A.shape
Q = numpy.zeros( (m,n) )
R1 = numpy.zeros( (m,n) )
R = numpy.zeros( (n,n) )
Q,R = qr_mgs(A)
for ii in range(0,1) : # repeat how many times
Q,R1 = qr_mgs(Q)
R = numpy.dot(R1,R)
return Q,R
def qri_mgs_piv( A, alpha=0.5 ):
"""QR decomposition of A, with column pivoting; returns Q,R,P.
Takes an optional tolerance parameter alpha.
Iterated MGS with column pivoting (Dax 1999)."""
Q = numpy.array(A, dtype=float)
m,n = Q.shape
R = numpy.zeros( (n,n) )
Qnorms = numpy.zeros( n )
piv = numpy.zeros( n )
P = numpy.eye( n )
for k in range( 0, n ) :
# step 0
for j in range ( k, n ) :
Qnorms[j] = numpy.linalg.norm( Q[:,j] )
#print Qnorms
j = numpy.where(Qnorms == max(Qnorms[k:n]))[0][0]
Qnorms[k] = 0
#print Q
#print R
#piv[k] = j
if (j != k) :
#print "switching columns", k, "and", j
P[:, [j, k]] = P[:, [k, j]]
Q[:, [j, k]] = Q[:, [k, j]]
#if (k > 0) :
# R[0:k, [j, k]] = R[0:k, [k, j]]
R[:, [j, k]] = R[:, [k, j]]
#print Q
#print R
# step 1
vl2norm = numpy.linalg.norm( Q[:,k] )
ii = 0
while True : # iterate
for i in range( 0, k ) :
s = numpy.dot( Q[:,i], Q[:,k] )
Q[:,k] = Q[:,k] - s * Q[:,i]
R[i,k] = R[i,k] + s
ii = ii + 1
vlnorm = vl2norm
vl2norm = numpy.linalg.norm( Q[:,k] )
if (vl2norm > alpha * vlnorm) :
#print "on column", k, "used", ii, "orthogonalizations"
break
# step 2
R[k,k] = numpy.linalg.norm( Q[:,k] )
Q[:,k] = Q[:,k] / R[k,k]
# step 3
if (k == n) :
break
else :
for j in range( k+1, n ) :
R[k,j] = numpy.dot( Q[:,k], Q[:,j] )
Q[:,j] = Q[:,j] - R[k,j] * Q[:,k]
# step 4
#Qhat = Q[:,k]
#Qhat2 = Qhat
for j in range( k+1, n ) :
ii = 0
vl2norm = numpy.linalg.norm( Q[:,j] )
while True : # iterate
s = numpy.dot( Q[:,j], Q[:,k] )
R[k,j] = R[k,j] + s
Q[:,j] = Q[:,j] - s * Q[:,k]
ii = ii + 1
vlnorm = vl2norm
vl2norm = numpy.linalg.norm( Q[:,j] )
if (vl2norm > alpha * vlnorm) :
#print "on column", j, "used", ii, "orthogonalizations"
break
return Q,R,P