ratqr.f 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. *DECK RATQR
  2. SUBROUTINE RATQR (N, EPS1, D, E, E2, M, W, IND, BD, TYPE, IDEF,
  3. + IERR)
  4. C***BEGIN PROLOGUE RATQR
  5. C***PURPOSE Compute the largest or smallest eigenvalues of a symmetric
  6. C tridiagonal matrix using the rational QR method with Newton
  7. C correction.
  8. C***LIBRARY SLATEC (EISPACK)
  9. C***CATEGORY D4A5, D4C2A
  10. C***TYPE SINGLE PRECISION (RATQR-S)
  11. C***KEYWORDS EIGENVALUES, EIGENVECTORS, EISPACK
  12. C***AUTHOR Smith, B. T., et al.
  13. C***DESCRIPTION
  14. C
  15. C This subroutine is a translation of the ALGOL procedure RATQR,
  16. C NUM. MATH. 11, 264-272(1968) by REINSCH and BAUER.
  17. C HANDBOOK FOR AUTO. COMP., VOL.II-LINEAR ALGEBRA, 257-265(1971).
  18. C
  19. C This subroutine finds the algebraically smallest or largest
  20. C eigenvalues of a SYMMETRIC TRIDIAGONAL matrix by the
  21. C rational QR method with Newton corrections.
  22. C
  23. C On Input
  24. C
  25. C N is the order of the matrix. N is an INTEGER variable.
  26. C
  27. C EPS1 is a theoretical absolute error tolerance for the
  28. C computed eigenvalues. If the input EPS1 is non-positive, or
  29. C indeed smaller than its default value, it is reset at each
  30. C iteration to the respective default value, namely, the
  31. C product of the relative machine precision and the magnitude
  32. C of the current eigenvalue iterate. The theoretical absolute
  33. C error in the K-th eigenvalue is usually not greater than
  34. C K times EPS1. EPS1 is a REAL variable.
  35. C
  36. C D contains the diagonal elements of the symmetric tridiagonal
  37. C matrix. D is a one-dimensional REAL array, dimensioned D(N).
  38. C
  39. C E contains the subdiagonal elements of the symmetric
  40. C tridiagonal matrix in its last N-1 positions. E(1) is
  41. C arbitrary. E is a one-dimensional REAL array, dimensioned
  42. C E(N).
  43. C
  44. C E2 contains the squares of the corresponding elements of E in
  45. C its last N-1 positions. E2(1) is arbitrary. E2 is a one-
  46. C dimensional REAL array, dimensioned E2(N).
  47. C
  48. C M is the number of eigenvalues to be found. M is an INTEGER
  49. C variable.
  50. C
  51. C IDEF should be set to 1 if the input matrix is known to be
  52. C positive definite, to -1 if the input matrix is known to
  53. C be negative definite, and to 0 otherwise. IDEF is an
  54. C INTEGER variable.
  55. C
  56. C TYPE should be set to .TRUE. if the smallest eigenvalues are
  57. C to be found, and to .FALSE. if the largest eigenvalues are
  58. C to be found. TYPE is a LOGICAL variable.
  59. C
  60. C On Output
  61. C
  62. C EPS1 is unaltered unless it has been reset to its
  63. C (last) default value.
  64. C
  65. C D and E are unaltered (unless W overwrites D).
  66. C
  67. C Elements of E2, corresponding to elements of E regarded as
  68. C negligible, have been replaced by zero causing the matrix
  69. C to split into a direct sum of submatrices. E2(1) is set
  70. C to 0.0e0 if the smallest eigenvalues have been found, and
  71. C to 2.0e0 if the largest eigenvalues have been found. E2
  72. C is otherwise unaltered (unless overwritten by BD).
  73. C
  74. C W contains the M algebraically smallest eigenvalues in
  75. C ascending order, or the M largest eigenvalues in descending
  76. C order. If an error exit is made because of an incorrect
  77. C specification of IDEF, no eigenvalues are found. If the
  78. C Newton iterates for a particular eigenvalue are not monotone,
  79. C the best estimate obtained is returned and IERR is set.
  80. C W is a one-dimensional REAL array, dimensioned W(N). W need
  81. C not be distinct from D.
  82. C
  83. C IND contains in its first M positions the submatrix indices
  84. C associated with the corresponding eigenvalues in W --
  85. C 1 for eigenvalues belonging to the first submatrix from
  86. C the top, 2 for those belonging to the second submatrix, etc.
  87. C IND is an one-dimensional INTEGER array, dimensioned IND(N).
  88. C
  89. C BD contains refined bounds for the theoretical errors of the
  90. C corresponding eigenvalues in W. These bounds are usually
  91. C within the tolerance specified by EPS1. BD is a one-
  92. C dimensional REAL array, dimensioned BD(N). BD need not be
  93. C distinct from E2.
  94. C
  95. C IERR is an INTEGER flag set to
  96. C Zero for normal return,
  97. C 6*N+1 if IDEF is set to 1 and TYPE to .TRUE.
  98. C when the matrix is NOT positive definite, or
  99. C if IDEF is set to -1 and TYPE to .FALSE.
  100. C when the matrix is NOT negative definite,
  101. C no eigenvalues are computed, or
  102. C M is greater than N,
  103. C 5*N+K if successive iterates to the K-th eigenvalue
  104. C are NOT monotone increasing, where K refers
  105. C to the last such occurrence.
  106. C
  107. C Note that subroutine TRIDIB is generally faster and more
  108. C accurate than RATQR if the eigenvalues are clustered.
  109. C
  110. C Questions and comments should be directed to B. S. Garbow,
  111. C APPLIED MATHEMATICS DIVISION, ARGONNE NATIONAL LABORATORY
  112. C ------------------------------------------------------------------
  113. C
  114. C***REFERENCES B. T. Smith, J. M. Boyle, J. J. Dongarra, B. S. Garbow,
  115. C Y. Ikebe, V. C. Klema and C. B. Moler, Matrix Eigen-
  116. C system Routines - EISPACK Guide, Springer-Verlag,
  117. C 1976.
  118. C***ROUTINES CALLED R1MACH
  119. C***REVISION HISTORY (YYMMDD)
  120. C 760101 DATE WRITTEN
  121. C 890531 Changed all specific intrinsics to generic. (WRB)
  122. C 890831 Modified array declarations. (WRB)
  123. C 890831 REVISION DATE from Version 3.2
  124. C 891214 Prologue converted to Version 4.0 format. (BAB)
  125. C 920501 Reformatted the REFERENCES section. (WRB)
  126. C***END PROLOGUE RATQR
  127. C
  128. INTEGER I,J,K,M,N,II,JJ,K1,IDEF,IERR,JDEF
  129. REAL D(*),E(*),E2(*),W(*),BD(*)
  130. REAL F,P,Q,R,S,EP,QP,ERR,TOT,EPS1,DELTA,MACHEP
  131. INTEGER IND(*)
  132. LOGICAL FIRST, TYPE
  133. C
  134. SAVE FIRST, MACHEP
  135. DATA FIRST /.TRUE./
  136. C***FIRST EXECUTABLE STATEMENT RATQR
  137. IF (FIRST) THEN
  138. MACHEP = R1MACH(4)
  139. ENDIF
  140. FIRST = .FALSE.
  141. C
  142. IERR = 0
  143. JDEF = IDEF
  144. C .......... COPY D ARRAY INTO W ..........
  145. DO 20 I = 1, N
  146. 20 W(I) = D(I)
  147. C
  148. IF (TYPE) GO TO 40
  149. J = 1
  150. GO TO 400
  151. 40 ERR = 0.0E0
  152. S = 0.0E0
  153. C .......... LOOK FOR SMALL SUB-DIAGONAL ENTRIES AND DEFINE
  154. C INITIAL SHIFT FROM LOWER GERSCHGORIN BOUND.
  155. C COPY E2 ARRAY INTO BD ..........
  156. TOT = W(1)
  157. Q = 0.0E0
  158. J = 0
  159. C
  160. DO 100 I = 1, N
  161. P = Q
  162. IF (I .EQ. 1) GO TO 60
  163. IF (P .GT. MACHEP * (ABS(D(I)) + ABS(D(I-1)))) GO TO 80
  164. 60 E2(I) = 0.0E0
  165. 80 BD(I) = E2(I)
  166. C .......... COUNT ALSO IF ELEMENT OF E2 HAS UNDERFLOWED ..........
  167. IF (E2(I) .EQ. 0.0E0) J = J + 1
  168. IND(I) = J
  169. Q = 0.0E0
  170. IF (I .NE. N) Q = ABS(E(I+1))
  171. TOT = MIN(W(I)-P-Q,TOT)
  172. 100 CONTINUE
  173. C
  174. IF (JDEF .EQ. 1 .AND. TOT .LT. 0.0E0) GO TO 140
  175. C
  176. DO 110 I = 1, N
  177. 110 W(I) = W(I) - TOT
  178. C
  179. GO TO 160
  180. 140 TOT = 0.0E0
  181. C
  182. 160 DO 360 K = 1, M
  183. C .......... NEXT QR TRANSFORMATION ..........
  184. 180 TOT = TOT + S
  185. DELTA = W(N) - S
  186. I = N
  187. F = ABS(MACHEP*TOT)
  188. IF (EPS1 .LT. F) EPS1 = F
  189. IF (DELTA .GT. EPS1) GO TO 190
  190. IF (DELTA .LT. (-EPS1)) GO TO 1000
  191. GO TO 300
  192. C .......... REPLACE SMALL SUB-DIAGONAL SQUARES BY ZERO
  193. C TO REDUCE THE INCIDENCE OF UNDERFLOWS ..........
  194. 190 IF (K .EQ. N) GO TO 210
  195. K1 = K + 1
  196. DO 200 J = K1, N
  197. IF (BD(J) .LE. (MACHEP*(W(J)+W(J-1))) ** 2) BD(J) = 0.0E0
  198. 200 CONTINUE
  199. C
  200. 210 F = BD(N) / DELTA
  201. QP = DELTA + F
  202. P = 1.0E0
  203. IF (K .EQ. N) GO TO 260
  204. K1 = N - K
  205. C .......... FOR I=N-1 STEP -1 UNTIL K DO -- ..........
  206. DO 240 II = 1, K1
  207. I = N - II
  208. Q = W(I) - S - F
  209. R = Q / QP
  210. P = P * R + 1.0E0
  211. EP = F * R
  212. W(I+1) = QP + EP
  213. DELTA = Q - EP
  214. IF (DELTA .GT. EPS1) GO TO 220
  215. IF (DELTA .LT. (-EPS1)) GO TO 1000
  216. GO TO 300
  217. 220 F = BD(I) / Q
  218. QP = DELTA + F
  219. BD(I+1) = QP * EP
  220. 240 CONTINUE
  221. C
  222. 260 W(K) = QP
  223. S = QP / P
  224. IF (TOT + S .GT. TOT) GO TO 180
  225. C .......... SET ERROR -- IRREGULAR END OF ITERATION.
  226. C DEFLATE MINIMUM DIAGONAL ELEMENT ..........
  227. IERR = 5 * N + K
  228. S = 0.0E0
  229. DELTA = QP
  230. C
  231. DO 280 J = K, N
  232. IF (W(J) .GT. DELTA) GO TO 280
  233. I = J
  234. DELTA = W(J)
  235. 280 CONTINUE
  236. C .......... CONVERGENCE ..........
  237. 300 IF (I .LT. N) BD(I+1) = BD(I) * F / QP
  238. II = IND(I)
  239. IF (I .EQ. K) GO TO 340
  240. K1 = I - K
  241. C .......... FOR J=I-1 STEP -1 UNTIL K DO -- ..........
  242. DO 320 JJ = 1, K1
  243. J = I - JJ
  244. W(J+1) = W(J) - S
  245. BD(J+1) = BD(J)
  246. IND(J+1) = IND(J)
  247. 320 CONTINUE
  248. C
  249. 340 W(K) = TOT
  250. ERR = ERR + ABS(DELTA)
  251. BD(K) = ERR
  252. IND(K) = II
  253. 360 CONTINUE
  254. C
  255. IF (TYPE) GO TO 1001
  256. F = BD(1)
  257. E2(1) = 2.0E0
  258. BD(1) = F
  259. J = 2
  260. C .......... NEGATE ELEMENTS OF W FOR LARGEST VALUES ..........
  261. 400 DO 500 I = 1, N
  262. 500 W(I) = -W(I)
  263. C
  264. JDEF = -JDEF
  265. GO TO (40,1001), J
  266. C .......... SET ERROR -- IDEF SPECIFIED INCORRECTLY ..........
  267. 1000 IERR = 6 * N + 1
  268. 1001 RETURN
  269. END