2
SUBROUTINE HQR2 (NM, N, LOW, IGH, H, WR, WI, Z, IERR)
3
C***BEGIN PROLOGUE HQR2
4
C***PURPOSE Compute the eigenvalues and eigenvectors of a real upper
5
C Hessenberg matrix using QR method.
6
C***LIBRARY SLATEC (EISPACK)
8
C***TYPE SINGLE PRECISION (HQR2-S, COMQR2-C)
9
C***KEYWORDS EIGENVALUES, EIGENVECTORS, EISPACK
10
C***AUTHOR Smith, B. T., et al.
13
C This subroutine is a translation of the ALGOL procedure HQR2,
14
C NUM. MATH. 16, 181-204(1970) by Peters and Wilkinson.
15
C HANDBOOK FOR AUTO. COMP., VOL.II-LINEAR ALGEBRA, 372-395(1971).
17
C This subroutine finds the eigenvalues and eigenvectors
18
C of a REAL UPPER Hessenberg matrix by the QR method. The
19
C eigenvectors of a REAL GENERAL matrix can also be found
20
C if ELMHES and ELTRAN or ORTHES and ORTRAN have
21
C been used to reduce this general matrix to Hessenberg form
22
C and to accumulate the similarity transformations.
26
C NM must be set to the row dimension of the two-dimensional
27
C array parameters, H and Z, as declared in the calling
28
C program dimension statement. NM is an INTEGER variable.
30
C N is the order of the matrix H. N is an INTEGER variable.
31
C N must be less than or equal to NM.
33
C LOW and IGH are two INTEGER variables determined by the
34
C balancing subroutine BALANC. If BALANC has not been
35
C used, set LOW=1 and IGH equal to the order of the matrix, N.
37
C H contains the upper Hessenberg matrix. H is a two-dimensional
38
C REAL array, dimensioned H(NM,N).
40
C Z contains the transformation matrix produced by ELTRAN
41
C after the reduction by ELMHES, or by ORTRAN after the
42
C reduction by ORTHES, if performed. If the eigenvectors
43
C of the Hessenberg matrix are desired, Z must contain the
44
C identity matrix. Z is a two-dimensional REAL array,
45
C dimensioned Z(NM,M).
49
C H has been destroyed.
51
C WR and WI contain the real and imaginary parts, respectively,
52
C of the eigenvalues. The eigenvalues are unordered except
53
C that complex conjugate pairs of values appear consecutively
54
C with the eigenvalue having the positive imaginary part first.
55
C If an error exit is made, the eigenvalues should be correct
56
C for indices IERR+1, IERR+2, ..., N. WR and WI are one-
57
C dimensional REAL arrays, dimensioned WR(N) and WI(N).
59
C Z contains the real and imaginary parts of the eigenvectors.
60
C If the J-th eigenvalue is real, the J-th column of Z
61
C contains its eigenvector. If the J-th eigenvalue is complex
62
C with positive imaginary part, the J-th and (J+1)-th
63
C columns of Z contain the real and imaginary parts of its
64
C eigenvector. The eigenvectors are unnormalized. If an
65
C error exit is made, none of the eigenvectors has been found.
67
C IERR is an INTEGER flag set to
68
C Zero for normal return,
69
C J if the J-th eigenvalue has not been
70
C determined after a total of 30*N iterations.
71
C The eigenvalues should be correct for indices
72
C IERR+1, IERR+2, ..., N, but no eigenvectors are
75
C Calls CDIV for complex division.
77
C Questions and comments should be directed to B. S. Garbow,
78
C APPLIED MATHEMATICS DIVISION, ARGONNE NATIONAL LABORATORY
79
C ------------------------------------------------------------------
81
C***REFERENCES B. T. Smith, J. M. Boyle, J. J. Dongarra, B. S. Garbow,
82
C Y. Ikebe, V. C. Klema and C. B. Moler, Matrix Eigen-
83
C system Routines - EISPACK Guide, Springer-Verlag,
85
C***ROUTINES CALLED CDIV
86
C***REVISION HISTORY (YYMMDD)
88
C 890531 Changed all specific intrinsics to generic. (WRB)
89
C 890831 Modified array declarations. (WRB)
90
C 890831 REVISION DATE from Version 3.2
91
C 891214 Prologue converted to Version 4.0 format. (BAB)
92
C 920501 Reformatted the REFERENCES section. (WRB)
95
INTEGER I,J,K,L,M,N,EN,II,JJ,LL,MM,NA,NM,NN
96
INTEGER IGH,ITN,ITS,LOW,MP2,ENM2,IERR
97
REAL H(NM,*),WR(*),WI(*),Z(NM,*)
98
REAL P,Q,R,S,T,W,X,Y,RA,SA,VI,VR,ZZ,NORM,S1,S2
101
C***FIRST EXECUTABLE STATEMENT HQR2
105
C .......... STORE ROOTS ISOLATED BY BALANC
106
C AND COMPUTE MATRIX NORM ..........
110
40 NORM = NORM + ABS(H(I,J))
113
IF (I .GE. LOW .AND. I .LE. IGH) GO TO 50
121
C .......... SEARCH FOR NEXT EIGENVALUES ..........
122
60 IF (EN .LT. LOW) GO TO 340
126
C .......... LOOK FOR SINGLE SMALL SUB-DIAGONAL ELEMENT
127
C FOR L=EN STEP -1 UNTIL LOW DO -- ..........
128
70 DO 80 LL = LOW, EN
130
IF (L .EQ. LOW) GO TO 100
131
S = ABS(H(L-1,L-1)) + ABS(H(L,L))
132
IF (S .EQ. 0.0E0) S = NORM
133
S2 = S + ABS(H(L,L-1))
134
IF (S2 .EQ. S) GO TO 100
136
C .......... FORM SHIFT ..........
138
IF (L .EQ. EN) GO TO 270
140
W = H(EN,NA) * H(NA,EN)
141
IF (L .EQ. NA) GO TO 280
142
IF (ITN .EQ. 0) GO TO 1000
143
IF (ITS .NE. 10 .AND. ITS .NE. 20) GO TO 130
144
C .......... FORM EXCEPTIONAL SHIFT ..........
148
120 H(I,I) = H(I,I) - X
150
S = ABS(H(EN,NA)) + ABS(H(NA,ENM2))
153
W = -0.4375E0 * S * S
156
C .......... LOOK FOR TWO CONSECUTIVE SMALL
157
C SUB-DIAGONAL ELEMENTS.
158
C FOR M=EN-2 STEP -1 UNTIL L DO -- ..........
164
P = (R * S - W) / H(M+1,M) + H(M,M+1)
165
Q = H(M+1,M+1) - ZZ - R - S
167
S = ABS(P) + ABS(Q) + ABS(R)
171
IF (M .EQ. L) GO TO 150
172
S1 = ABS(P) * (ABS(H(M-1,M-1)) + ABS(ZZ) + ABS(H(M+1,M+1)))
173
S2 = S1 + ABS(H(M,M-1)) * (ABS(Q) + ABS(R))
174
IF (S2 .EQ. S1) GO TO 150
181
IF (I .EQ. MP2) GO TO 160
184
C .......... DOUBLE QR STEP INVOLVING ROWS L TO EN AND
185
C COLUMNS M TO EN ..........
188
IF (K .EQ. M) GO TO 170
192
IF (NOTLAS) R = H(K+2,K-1)
193
X = ABS(P) + ABS(Q) + ABS(R)
194
IF (X .EQ. 0.0E0) GO TO 260
198
170 S = SIGN(SQRT(P*P+Q*Q+R*R),P)
199
IF (K .EQ. M) GO TO 180
202
180 IF (L .NE. M) H(K,K-1) = -H(K,K-1)
209
C .......... ROW MODIFICATION ..........
211
P = H(K,J) + Q * H(K+1,J)
212
IF (.NOT. NOTLAS) GO TO 200
214
H(K+2,J) = H(K+2,J) - P * ZZ
215
200 H(K+1,J) = H(K+1,J) - P * Y
216
H(K,J) = H(K,J) - P * X
220
C .......... COLUMN MODIFICATION ..........
222
P = X * H(I,K) + Y * H(I,K+1)
223
IF (.NOT. NOTLAS) GO TO 220
224
P = P + ZZ * H(I,K+2)
225
H(I,K+2) = H(I,K+2) - P * R
226
220 H(I,K+1) = H(I,K+1) - P * Q
229
C .......... ACCUMULATE TRANSFORMATIONS ..........
231
P = X * Z(I,K) + Y * Z(I,K+1)
232
IF (.NOT. NOTLAS) GO TO 240
233
P = P + ZZ * Z(I,K+2)
234
Z(I,K+2) = Z(I,K+2) - P * R
235
240 Z(I,K+1) = Z(I,K+1) - P * Q
242
C .......... ONE ROOT FOUND ..........
248
C .......... TWO ROOTS FOUND ..........
249
280 P = (Y - X) / 2.0E0
255
IF (Q .LT. 0.0E0) GO TO 320
256
C .......... REAL PAIR ..........
260
IF (ZZ .NE. 0.0E0) WR(EN) = X - W / ZZ
270
C .......... ROW MODIFICATION ..........
273
H(NA,J) = Q * ZZ + P * H(EN,J)
274
H(EN,J) = Q * H(EN,J) - P * ZZ
276
C .......... COLUMN MODIFICATION ..........
279
H(I,NA) = Q * ZZ + P * H(I,EN)
280
H(I,EN) = Q * H(I,EN) - P * ZZ
282
C .......... ACCUMULATE TRANSFORMATIONS ..........
285
Z(I,NA) = Q * ZZ + P * Z(I,EN)
286
Z(I,EN) = Q * Z(I,EN) - P * ZZ
290
C .......... COMPLEX PAIR ..........
297
C .......... ALL ROOTS FOUND. BACKSUBSTITUTE TO FIND
298
C VECTORS OF UPPER TRIANGULAR FORM ..........
299
340 IF (NORM .EQ. 0.0E0) GO TO 1001
300
C .......... FOR EN=N STEP -1 UNTIL 1 DO -- ..........
307
C .......... REAL VECTOR ..........
310
IF (NA .EQ. 0) GO TO 800
311
C .......... FOR I=EN-1 STEP -1 UNTIL 1 DO -- ..........
316
IF (M .GT. NA) GO TO 620
319
610 R = R + H(I,J) * H(J,EN)
321
620 IF (WI(I) .GE. 0.0E0) GO TO 630
326
IF (WI(I) .NE. 0.0E0) GO TO 640
328
IF (T .NE. 0.0E0) GO TO 635
331
IF (NORM + T .GT. NORM) GO TO 632
335
C .......... SOLVE REAL EQUATIONS ..........
338
Q = (WR(I) - P) * (WR(I) - P) + WI(I) * WI(I)
339
T = (X * S - ZZ * R) / Q
341
IF (ABS(X) .LE. ABS(ZZ)) GO TO 650
342
H(I+1,EN) = (-R - W * T) / X
344
650 H(I+1,EN) = (-S - Y * T) / ZZ
346
C .......... END REAL VECTOR ..........
348
C .......... COMPLEX VECTOR ..........
350
C .......... LAST VECTOR COMPONENT CHOSEN IMAGINARY SO THAT
351
C EIGENVECTOR MATRIX IS TRIANGULAR ..........
352
IF (ABS(H(EN,NA)) .LE. ABS(H(NA,EN))) GO TO 720
353
H(NA,NA) = Q / H(EN,NA)
354
H(NA,EN) = -(H(EN,EN) - P) / H(EN,NA)
356
720 CALL CDIV(0.0E0,-H(NA,EN),H(NA,NA)-P,Q,H(NA,NA),H(NA,EN))
360
IF (ENM2 .EQ. 0) GO TO 800
361
C .......... FOR I=EN-2 STEP -1 UNTIL 1 DO -- ..........
369
RA = RA + H(I,J) * H(J,NA)
370
SA = SA + H(I,J) * H(J,EN)
373
IF (WI(I) .GE. 0.0E0) GO TO 770
379
IF (WI(I) .NE. 0.0E0) GO TO 780
380
CALL CDIV(-RA,-SA,W,Q,H(I,NA),H(I,EN))
382
C .......... SOLVE COMPLEX EQUATIONS ..........
385
VR = (WR(I) - P) * (WR(I) - P) + WI(I) * WI(I) - Q * Q
386
VI = (WR(I) - P) * 2.0E0 * Q
387
IF (VR .NE. 0.0E0 .OR. VI .NE. 0.0E0) GO TO 783
388
S1 = NORM * (ABS(W)+ABS(Q)+ABS(X)+ABS(Y)+ABS(ZZ))
391
IF (S1 + VR .GT. S1) GO TO 782
393
783 CALL CDIV(X*R-ZZ*RA+Q*SA,X*S-ZZ*SA-Q*RA,VR,VI,
395
IF (ABS(X) .LE. ABS(ZZ) + ABS(Q)) GO TO 785
396
H(I+1,NA) = (-RA - W * H(I,NA) + Q * H(I,EN)) / X
397
H(I+1,EN) = (-SA - W * H(I,EN) - Q * H(I,NA)) / X
399
785 CALL CDIV(-R-Y*H(I,NA),-S-Y*H(I,EN),ZZ,Q,
400
1 H(I+1,NA),H(I+1,EN))
402
C .......... END COMPLEX VECTOR ..........
404
C .......... END BACK SUBSTITUTION.
405
C VECTORS OF ISOLATED ROOTS ..........
407
IF (I .GE. LOW .AND. I .LE. IGH) GO TO 840
413
C .......... MULTIPLY BY TRANSFORMATION MATRIX TO GIVE
414
C VECTORS OF ORIGINAL FULL MATRIX.
415
C FOR J=N STEP -1 UNTIL LOW DO -- ..........
424
860 ZZ = ZZ + Z(I,K) * H(K,J)
430
C .......... SET ERROR -- NO CONVERGENCE TO AN
431
C EIGENVALUE AFTER 30*N ITERATIONS ..........