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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
SUBROUTINE sla_SVD (M, N, MP, NP, A, W, V, WORK, JSTAT)
*+
* - - - -
* S V D
* - - - -
*
* Singular value decomposition (double precision)
*
* This routine expresses a given matrix A as the product of
* three matrices U, W, V:
*
* A = U x W x VT
*
* Where:
*
* A is any M (rows) x N (columns) matrix, where M.GE.N
* U is an M x N column-orthogonal matrix
* W is an N x N diagonal matrix with W(I,I).GE.0
* VT is the transpose of an N x N orthogonal matrix
*
* Note that M and N, above, are the LOGICAL dimensions of the
* matrices and vectors concerned, which can be located in
* arrays of larger PHYSICAL dimensions, given by MP and NP.
*
* Given:
* M,N i numbers of rows and columns in matrix A
* MP,NP i physical dimensions of array containing matrix A
* A d(MP,NP) array containing MxN matrix A
*
* Returned:
* A d(MP,NP) array containing MxN column-orthogonal matrix U
* W d(N) NxN diagonal matrix W (diagonal elements only)
* V d(NP,NP) array containing NxN orthogonal matrix V
* WORK d(N) workspace
* JSTAT i 0 = OK, -1 = A wrong shape, >0 = index of W
* for which convergence failed. See note 2, below.
*
* Notes:
*
* 1) V contains matrix V, not the transpose of matrix V.
*
* 2) If the status JSTAT is greater than zero, this need not
* necessarily be treated as a failure. It means that, due to
* chance properties of the matrix A, the QR transformation
* phase of the routine did not fully converge in a predefined
* number of iterations, something that very seldom occurs.
* When this condition does arise, it is possible that the
* elements of the diagonal matrix W have not been correctly
* found. However, in practice the results are likely to
* be trustworthy. Applications should report the condition
* as a warning, but then proceed normally.
*
* References:
* The algorithm is an adaptation of the routine SVD in the EISPACK
* library (Garbow et al 1977, EISPACK Guide Extension, Springer
* Verlag), which is a FORTRAN 66 implementation of the Algol
* routine SVD of Wilkinson & Reinsch 1971 (Handbook for Automatic
* Computation, vol 2, ed Bauer et al, Springer Verlag). These
* references give full details of the algorithm used here. A good
* account of the use of SVD in least squares problems is given in
* Numerical Recipes (Press et al 1986, Cambridge University Press),
* which includes another variant of the EISPACK code.
*
* P.T.Wallace Starlink 22 December 1993
*
* Copyright (C) 1995 Rutherford Appleton Laboratory
*-
IMPLICIT NONE
INTEGER M,N,MP,NP
DOUBLE PRECISION A(MP,NP),W(N),V(NP,NP),WORK(N)
INTEGER JSTAT
* Maximum number of iterations in QR phase
INTEGER ITMAX
PARAMETER (ITMAX=30)
INTEGER I,K,L,J,K1,ITS,L1,I1
LOGICAL CANCEL
DOUBLE PRECISION G,SCALE,AN,S,X,F,H,C,Y,Z
* Check that the matrix is the right shape
IF (M.LT.N) THEN
* No: error status
JSTAT = -1
ELSE
* Yes: preset the status to OK
JSTAT = 0
*
* Householder reduction to bidiagonal form
* ----------------------------------------
G = 0D0
SCALE = 0D0
AN = 0D0
DO I=1,N
L = I+1
WORK(I) = SCALE*G
G = 0D0
S = 0D0
SCALE = 0D0
IF (I.LE.M) THEN
DO K=I,M
SCALE = SCALE+ABS(A(K,I))
END DO
IF (SCALE.NE.0D0) THEN
DO K=I,M
X = A(K,I)/SCALE
A(K,I) = X
S = S+X*X
END DO
F = A(I,I)
G = -SIGN(SQRT(S),F)
H = F*G-S
A(I,I) = F-G
IF (I.NE.N) THEN
DO J=L,N
S = 0D0
DO K=I,M
S = S+A(K,I)*A(K,J)
END DO
F = S/H
DO K=I,M
A(K,J) = A(K,J)+F*A(K,I)
END DO
END DO
END IF
DO K=I,M
A(K,I) = SCALE*A(K,I)
END DO
END IF
END IF
W(I) = SCALE*G
G = 0D0
S = 0D0
SCALE = 0D0
IF (I.LE.M .AND. I.NE.N) THEN
DO K=L,N
SCALE = SCALE+ABS(A(I,K))
END DO
IF (SCALE.NE.0D0) THEN
DO K=L,N
X = A(I,K)/SCALE
A(I,K) = X
S = S+X*X
END DO
F = A(I,L)
G = -SIGN(SQRT(S),F)
H = F*G-S
A(I,L) = F-G
DO K=L,N
WORK(K) = A(I,K)/H
END DO
IF (I.NE.M) THEN
DO J=L,M
S = 0D0
DO K=L,N
S = S+A(J,K)*A(I,K)
END DO
DO K=L,N
A(J,K) = A(J,K)+S*WORK(K)
END DO
END DO
END IF
DO K=L,N
A(I,K) = SCALE*A(I,K)
END DO
END IF
END IF
* Overestimate of largest column norm for convergence test
AN = MAX(AN,ABS(W(I))+ABS(WORK(I)))
END DO
*
* Accumulation of right-hand transformations
* ------------------------------------------
DO I=N,1,-1
IF (I.NE.N) THEN
IF (G.NE.0D0) THEN
DO J=L,N
V(J,I) = (A(I,J)/A(I,L))/G
END DO
DO J=L,N
S = 0D0
DO K=L,N
S = S+A(I,K)*V(K,J)
END DO
DO K=L,N
V(K,J) = V(K,J)+S*V(K,I)
END DO
END DO
END IF
DO J=L,N
V(I,J) = 0D0
V(J,I) = 0D0
END DO
END IF
V(I,I) = 1D0
G = WORK(I)
L = I
END DO
*
* Accumulation of left-hand transformations
* -----------------------------------------
DO I=N,1,-1
L = I+1
G = W(I)
IF (I.NE.N) THEN
DO J=L,N
A(I,J) = 0D0
END DO
END IF
IF (G.NE.0D0) THEN
IF (I.NE.N) THEN
DO J=L,N
S = 0D0
DO K=L,M
S = S+A(K,I)*A(K,J)
END DO
F = (S/A(I,I))/G
DO K=I,M
A(K,J) = A(K,J)+F*A(K,I)
END DO
END DO
END IF
DO J=I,M
A(J,I) = A(J,I)/G
END DO
ELSE
DO J=I,M
A(J,I) = 0D0
END DO
END IF
A(I,I) = A(I,I)+1D0
END DO
*
* Diagonalisation of the bidiagonal form
* --------------------------------------
DO K=N,1,-1
K1 = K-1
* Iterate until converged
ITS = 0
DO WHILE (ITS.LT.ITMAX)
ITS = ITS+1
* Test for splitting into submatrices
CANCEL = .TRUE.
DO L=K,1,-1
L1 = L-1
IF (AN+ABS(WORK(L)).EQ.AN) THEN
CANCEL = .FALSE.
GO TO 10
END IF
* (Following never attempted for L=1 because WORK(1) is zero)
IF (AN+ABS(W(L1)).EQ.AN) GO TO 10
END DO
10 CONTINUE
* Cancellation of WORK(L) if L>1
IF (CANCEL) THEN
C = 0D0
S = 1D0
DO I=L,K
F = S*WORK(I)
IF (AN+ABS(F).EQ.AN) GO TO 20
G = W(I)
H = SQRT(F*F+G*G)
W(I) = H
C = G/H
S = -F/H
DO J=1,M
Y = A(J,L1)
Z = A(J,I)
A(J,L1) = Y*C+Z*S
A(J,I) = -Y*S+Z*C
END DO
END DO
20 CONTINUE
END IF
* Converged?
Z = W(K)
IF (L.EQ.K) THEN
* Yes: stop iterating
ITS = ITMAX
* Ensure singular values non-negative
IF (Z.LT.0D0) THEN
W(K) = -Z
DO J=1,N
V(J,K) = -V(J,K)
END DO
END IF
ELSE
* Not converged yet: set status if iteration limit reached
IF (ITS.EQ.ITMAX) JSTAT = K
* Shift from bottom 2x2 minor
X = W(L)
Y = W(K1)
G = WORK(K1)
H = WORK(K)
F = ((Y-Z)*(Y+Z)+(G-H)*(G+H))/(2D0*H*Y)
IF (ABS(F).LE.1D15) THEN
G = SQRT(F*F+1D0)
ELSE
G = ABS(F)
END IF
F = ((X-Z)*(X+Z)+H*(Y/(F+SIGN(G,F))-H))/X
* Next QR transformation
C = 1D0
S = 1D0
DO I1=L,K1
I = I1+1
G = WORK(I)
Y = W(I)
H = S*G
G = C*G
Z = SQRT(F*F+H*H)
WORK(I1) = Z
IF (Z.NE.0D0) THEN
C = F/Z
S = H/Z
ELSE
C = 1D0
S = 0D0
END IF
F = X*C+G*S
G = -X*S+G*C
H = Y*S
Y = Y*C
DO J=1,N
X = V(J,I1)
Z = V(J,I)
V(J,I1) = X*C+Z*S
V(J,I) = -X*S+Z*C
END DO
Z = SQRT(F*F+H*H)
W(I1) = Z
IF (Z.NE.0D0) THEN
C = F/Z
S = H/Z
END IF
F = C*G+S*Y
X = -S*G+C*Y
DO J=1,M
Y = A(J,I1)
Z = A(J,I)
A(J,I1) = Y*C+Z*S
A(J,I) = -Y*S+Z*C
END DO
END DO
WORK(L) = 0D0
WORK(K) = F
W(K) = X
END IF
END DO
END DO
END IF
END
|