lssods.f 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. *DECK LSSODS
  2. SUBROUTINE LSSODS (A, X, B, M, N, NRDA, IFLAG, IRANK, ISCALE, Q,
  3. + DIAG, KPIVOT, ITER, RESNRM, XNORM, Z, R, DIV, TD, SCALES)
  4. C***BEGIN PROLOGUE LSSODS
  5. C***SUBSIDIARY
  6. C***PURPOSE Subsidiary to BVSUP
  7. C***LIBRARY SLATEC
  8. C***TYPE SINGLE PRECISION (LSSODS-S)
  9. C***AUTHOR (UNKNOWN)
  10. C***DESCRIPTION
  11. C
  12. C LSSODS solves the same problem as SODS (in fact, it is called by
  13. C SODS) but is somewhat more flexible in its use. In particular,
  14. C LSSODS allows for iterative refinement of the solution, makes the
  15. C transformation and triangular reduction information more
  16. C accessible, and enables the user to avoid destruction of the
  17. C original matrix A.
  18. C
  19. C Modeled after the ALGOL codes in the articles in the REFERENCES
  20. C section.
  21. C
  22. C **********************************************************************
  23. C INPUT
  24. C **********************************************************************
  25. C
  26. C A -- Contains the matrix of M equations in N unknowns and must
  27. C be dimensioned NRDA by N. A remains unchanged
  28. C X -- Solution array of length at least N
  29. C B -- Given constant vector of length M, B remains unchanged
  30. C M -- Number of equations, M greater or equal to 1
  31. C N -- Number of unknowns, N not larger than M
  32. C NRDA -- Row dimension of A, NRDA greater or equal to M
  33. C IFLAG -- Status indicator
  34. C = 0 for the first call (and for each new problem defined by
  35. C a new matrix A) when the matrix data is treated as exact
  36. C =-K for the first call (and for each new problem defined by
  37. C a new matrix A) when the matrix data is assumed to be
  38. C accurate to about K digits
  39. C = 1 for subsequent calls whenever the matrix A has already
  40. C been decomposed (problems with new vectors B but
  41. C same matrix a can be handled efficiently)
  42. C ISCALE -- Scaling indicator
  43. C =-1 if the matrix A is to be pre-scaled by
  44. C columns when appropriate
  45. C If the scaling indicator is not equal to -1
  46. C no scaling will be attempted
  47. C For most problems scaling will probably not be necessary
  48. C ITER -- Maximum number of iterative improvement steps to be
  49. C performed, 0 .LE. ITER .LE. 10 (SODS uses ITER=0)
  50. C Q -- Matrix used for the transformation, must be dimensioned
  51. C NRDA by N (SODS puts A in the Q location which conserves
  52. C storage but destroys A)
  53. C When iterative improvement of the solution is requested,
  54. C ITER .GT. 0, this additional storage for Q must be
  55. C made available
  56. C DIAG,KPIVOT,Z,R, -- Arrays of length N (except for R which is M)
  57. C DIV,TD,SCALES used for internal storage
  58. C
  59. C **********************************************************************
  60. C OUTPUT
  61. C **********************************************************************
  62. C
  63. C IFLAG -- Status indicator
  64. C =1 if solution was obtained
  65. C =2 if improper input is detected
  66. C =3 if rank of matrix is less than N
  67. C if the minimal length least squares solution is
  68. C desired, simply reset IFLAG=1 and call the code again
  69. C
  70. C The next three IFLAG values can occur only when
  71. C the iterative improvement mode is being used.
  72. C =4 if the problem is ill-conditioned and maximal
  73. C machine accuracy is not achievable
  74. C =5 if the problem is very ill-conditioned and the solution
  75. C IS likely to have no correct digits
  76. C =6 if the allowable number of iterative improvement steps
  77. C has been completed without getting convergence
  78. C X -- Least squares solution of A X = B
  79. C IRANK -- Contains the numerically determined matrix rank
  80. C the user must not alter this value on succeeding calls
  81. C with input values of IFLAG=1
  82. C Q -- Contains the strictly upper triangular part of the reduced
  83. C matrix and the transformation information in the lower
  84. C triangular part
  85. C DIAG -- Contains the diagonal elements of the triangular reduced
  86. C matrix
  87. C KPIVOT -- Contains the pivotal information. The column interchanges
  88. C performed on the original matrix are recorded here
  89. C ITER -- The actual number of iterative corrections used
  90. C RESNRM -- The Euclidean norm of the residual vector B - A X
  91. C XNORM -- The Euclidean norm of the solution vector
  92. C DIV,TD -- Contains transformation information for rank
  93. C deficient problems
  94. C SCALES -- Contains the column scaling parameters
  95. C
  96. C **********************************************************************
  97. C
  98. C***SEE ALSO BVSUP
  99. C***REFERENCES G. Golub, Numerical methods for solving linear least
  100. C squares problems, Numerische Mathematik 7, (1965),
  101. C pp. 206-216.
  102. C P. Businger and G. Golub, Linear least squares
  103. C solutions by Householder transformations, Numerische
  104. C Mathematik 7, (1965), pp. 269-276.
  105. C***ROUTINES CALLED J4SAVE, OHTROR, ORTHOL, R1MACH, SDOT, SDSDOT,
  106. C XERMAX, XERMSG, XGETF, XSETF
  107. C***REVISION HISTORY (YYMMDD)
  108. C 750601 DATE WRITTEN
  109. C 890531 Changed all specific intrinsics to generic. (WRB)
  110. C 890831 Modified array declarations. (WRB)
  111. C 891214 Prologue converted to Version 4.0 format. (BAB)
  112. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  113. C 900402 Added TYPE section. (WRB)
  114. C 910408 Updated the REFERENCES section. (WRB)
  115. C 920501 Reformatted the REFERENCES section. (WRB)
  116. C***END PROLOGUE LSSODS
  117. DIMENSION A(NRDA,*),X(*),B(*),Q(NRDA,*),DIAG(*),
  118. 1 Z(*),KPIVOT(*),R(*),DIV(*),TD(*),SCALES(*)
  119. C
  120. C **********************************************************************
  121. C
  122. C MACHINE PRECISION (COMPUTER UNIT ROUNDOFF VALUE) IS DEFINED
  123. C THE FUNCTION R1MACH.
  124. C
  125. C***FIRST EXECUTABLE STATEMENT LSSODS
  126. URO = R1MACH(3)
  127. C
  128. C **********************************************************************
  129. C
  130. IF (N .LT. 1 .OR. M .LT. N .OR. NRDA .LT. M) GO TO 1
  131. IF (ITER .LT. 0) GO TO 1
  132. IF (IFLAG .LE. 0) GO TO 5
  133. IF (IFLAG .EQ. 1) GO TO 15
  134. C
  135. C INVALID INPUT FOR LSSODS
  136. 1 IFLAG=2
  137. CALL XERMSG ('SLATEC', 'LSSODS', 'INVALID INPUT PARAMETERS.', 2,
  138. + 1)
  139. RETURN
  140. C
  141. 5 CALL XGETF (NFATAL)
  142. MAXMES = J4SAVE (4,0,.FALSE.)
  143. IF (IFLAG .EQ. 0) GO TO 7
  144. NFAT = -1
  145. IF(NFATAL .EQ. 0) NFAT=0
  146. CALL XSETF (NFAT)
  147. CALL XERMAX (1)
  148. C
  149. C COPY MATRIX A INTO MATRIX Q
  150. C
  151. 7 DO 10 J=1,N
  152. DO 10 K=1,M
  153. 10 Q(K,J)=A(K,J)
  154. C
  155. C USE ORTHOGONAL TRANSFORMATIONS TO REDUCE Q TO
  156. C UPPER TRIANGULAR FORM
  157. C
  158. CALL ORTHOL(Q,M,N,NRDA,IFLAG,IRANK,ISCALE,DIAG,KPIVOT,SCALES,Z,TD)
  159. C
  160. CALL XSETF (NFATAL)
  161. CALL XERMAX (MAXMES)
  162. IF (IRANK .EQ. N) GO TO 12
  163. C
  164. C FOR RANK DEFICIENT PROBLEMS USE ADDITIONAL ORTHOGONAL
  165. C TRANSFORMATIONS TO FURTHER REDUCE Q
  166. C
  167. IF (IRANK .NE. 0) CALL OHTROR(Q,N,NRDA,DIAG,IRANK,DIV,TD)
  168. RETURN
  169. C
  170. C STORE DIVISORS FOR THE TRIANGULAR SOLUTION
  171. C
  172. 12 DO 13 K=1,N
  173. 13 DIV(K)=DIAG(K)
  174. C
  175. 15 IRM=IRANK-1
  176. IRP=IRANK+1
  177. ITERP=MIN(ITER+1,11)
  178. ACC=10.*URO
  179. C
  180. C ZERO OUT SOLUTION ARRAY
  181. C
  182. DO 20 K=1,N
  183. 20 X(K)=0.
  184. C
  185. IF (IRANK .GT. 0) GO TO 25
  186. C
  187. C SPECIAL CASE FOR THE NULL MATRIX
  188. ITER=0
  189. XNORM=0.
  190. RESNRM=SQRT(SDOT(M,B(1),1,B(1),1))
  191. RETURN
  192. C
  193. C COPY CONSTANT VECTOR INTO R
  194. C
  195. 25 DO 30 K=1,M
  196. 30 R(K)=B(K)
  197. C
  198. C **********************************************************************
  199. C SOLUTION SECTION
  200. C ITERATIVE REFINEMENT OF THE RESIDUAL VECTOR
  201. C **********************************************************************
  202. C
  203. DO 100 IT=1,ITERP
  204. ITER=IT-1
  205. C
  206. C APPLY ORTHOGONAL TRANSFORMATION TO R
  207. C
  208. DO 35 J=1,IRANK
  209. MJ=M-J+1
  210. GAMMA=SDOT(MJ,Q(J,J),1,R(J),1)/(DIAG(J)*Q(J,J))
  211. DO 35 K=J,M
  212. 35 R(K)=R(K)+GAMMA*Q(K,J)
  213. C
  214. C BACKWARD SUBSTITUTION FOR TRIANGULAR SYSTEM SOLUTION
  215. C
  216. Z(IRANK)=R(IRANK)/DIV(IRANK)
  217. IF (IRM .EQ. 0) GO TO 45
  218. DO 40 L=1,IRM
  219. K=IRANK-L
  220. KP=K+1
  221. 40 Z(K)=(R(K)-SDOT(L,Q(K,KP),NRDA,Z(KP),1))/DIV(K)
  222. C
  223. 45 IF (IRANK .EQ. N) GO TO 60
  224. C
  225. C FOR RANK DEFICIENT PROBLEMS OBTAIN THE
  226. C MINIMAL LENGTH SOLUTION
  227. C
  228. NMIR=N-IRANK
  229. DO 50 K=IRP,N
  230. 50 Z(K)=0.
  231. DO 55 K=1,IRANK
  232. GAM=((TD(K)*Z(K))+SDOT(NMIR,Q(K,IRP),NRDA,Z(IRP),1))/
  233. 1 (TD(K)*DIV(K))
  234. Z(K)=Z(K)+GAM*TD(K)
  235. DO 55 J=IRP,N
  236. 55 Z(J)=Z(J)+GAM*Q(K,J)
  237. C
  238. C REORDER SOLUTION COMPONENTS ACCORDING TO PIVOTAL POINTS
  239. C AND RESCALE ANSWERS AS DICTATED
  240. C
  241. 60 DO 65 K=1,N
  242. Z(K)=Z(K)*SCALES(K)
  243. L=KPIVOT(K)
  244. 65 X(L)=X(L)+Z(K)
  245. C
  246. C COMPUTE CORRECTION VECTOR NORM (SOLUTION NORM)
  247. C
  248. ZNORM=SQRT(SDOT(N,Z(1),1,Z(1),1))
  249. IF (IT .EQ. 1) XNORM=ZNORM
  250. IF (ITERP .GT. 1) GO TO 80
  251. C
  252. C NO ITERATIVE CORRECTIONS TO BE PERFORMED, SO COMPUTE
  253. C THE APPROXIMATE RESIDUAL NORM DEFINED BY THE EQUATIONS
  254. C WHICH ARE NOT SATISFIED BY THE SOLUTION
  255. C THEN WE ARE DONE
  256. C
  257. MMIR=M-IRANK
  258. IF (MMIR .EQ. 0) GO TO 70
  259. RESNRM=SQRT(SDOT(MMIR,R(IRP),1,R(IRP),1))
  260. RETURN
  261. 70 RESNRM=0.
  262. RETURN
  263. C
  264. C COMPUTE RESIDUAL VECTOR FOR THE ITERATIVE IMPROVEMENT PROCESS
  265. C
  266. 80 DO 85 K=1,M
  267. 85 R(K)=-SDSDOT(N,-B(K),A(K,1),NRDA,X(1),1)
  268. RESNRM=SQRT(SDOT(M,R(1),1,R(1),1))
  269. IF (IT .EQ. 1) GO TO 100
  270. C
  271. C TEST FOR CONVERGENCE
  272. C
  273. IF (ZNORM .LE. ACC*XNORM) RETURN
  274. C
  275. C COMPARE SUCCESSIVE REFINEMENT VECTOR NORMS
  276. C FOR LOOP TERMINATION CRITERIA
  277. C
  278. IF (ZNORM .LE. 0.25*ZNRM0) GO TO 100
  279. IF (IT .EQ. 2) GO TO 90
  280. C
  281. IFLAG=4
  282. CALL XERMSG ('SLATEC', 'LSSODS',
  283. + 'PROBLEM MAY BE ILL-CONDITIONED. MAXIMAL MACHINE ACCURACY ' //
  284. + 'IS NOT ACHIEVABLE.', 3, 1)
  285. RETURN
  286. C
  287. 90 IFLAG=5
  288. CALL XERMSG ('SLATEC', 'LSSODS',
  289. + 'PROBLEM IS VERY ILL-CONDITIONED. ITERATIVE ' //
  290. + 'IMPROVEMENT IS INEFFECTIVE.', 8, 1)
  291. RETURN
  292. C
  293. 100 ZNRM0=ZNORM
  294. C **********************************************************************
  295. C
  296. C **********************************************************************
  297. IFLAG=6
  298. CALL XERMSG ('SLATEC', 'LSSODS',
  299. + 'CONVERGENCE HAS NOT BEEN OBTAINED WITH ALLOWABLE ' //
  300. + 'NUMBER OF ITERATIVE IMPROVEMENT STEPS.', 8, 1)
  301. C
  302. RETURN
  303. END