dlssud.f 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. *DECK DLSSUD
  2. SUBROUTINE DLSSUD (A, X, B, N, M, NRDA, U, NRDU, IFLAG, MLSO,
  3. + IRANK, ISCALE, Q, DIAG, KPIVOT, S, DIV, TD, ISFLG, SCALES)
  4. C***BEGIN PROLOGUE DLSSUD
  5. C***SUBSIDIARY
  6. C***PURPOSE Subsidiary to DBVSUP and DSUDS
  7. C***LIBRARY SLATEC
  8. C***TYPE DOUBLE PRECISION (LSSUDS-S, DLSSUD-D)
  9. C***AUTHOR Watts, H. A., (SNLA)
  10. C***DESCRIPTION
  11. C
  12. C DLSSUD solves the underdetermined system of equations A Z = B,
  13. C where A is N by M and N .LE. M. In particular, if rank A equals
  14. C IRA, a vector X and a matrix U are determined such that X is the
  15. C UNIQUE solution of smallest length, satisfying A X = B, and the
  16. C columns of U form an orthonormal basis for the null space of A,
  17. C satisfying A U = 0 . Then all solutions Z are given by
  18. C Z = X + C(1)*U(1) + ..... + C(M-IRA)*U(M-IRA)
  19. C where U(J) represents the J-th column of U and the C(J) are
  20. C arbitrary constants.
  21. C If the system of equations are not compatible, only the least
  22. C squares solution of minimal length is computed.
  23. C
  24. C *********************************************************************
  25. C INPUT
  26. C *********************************************************************
  27. C
  28. C A -- Contains the matrix of N equations in M unknowns, A remains
  29. C unchanged, must be dimensioned NRDA by M.
  30. C X -- Solution array of length at least M.
  31. C B -- Given constant vector of length N, B remains unchanged.
  32. C N -- Number of equations, N greater or equal to 1.
  33. C M -- Number of unknowns, M greater or equal to N.
  34. C NRDA -- Row dimension of A, NRDA greater or equal to N.
  35. C U -- Matrix used for solution, must be dimensioned NRDU by
  36. C (M - rank of A).
  37. C (storage for U may be ignored when only the minimal length
  38. C solution X is desired)
  39. C NRDU -- Row dimension of U, NRDU greater or equal to M.
  40. C (if only the minimal length solution is wanted,
  41. C NRDU=0 is acceptable)
  42. C IFLAG -- Status indicator
  43. C =0 for the first call (and for each new problem defined by
  44. C a new matrix A) when the matrix data is treated as exact
  45. C =-K for the first call (and for each new problem defined by
  46. C a new matrix A) when the matrix data is assumed to be
  47. C accurate to about K digits.
  48. C =1 for subsequent calls whenever the matrix A has already
  49. C been decomposed (problems with new vectors B but
  50. C same matrix A can be handled efficiently).
  51. C MLSO -- =0 if only the minimal length solution is wanted.
  52. C =1 if the complete solution is wanted, includes the
  53. C linear space defined by the matrix U.
  54. C IRANK -- Variable used for the rank of A, set by the code.
  55. C ISCALE -- Scaling indicator
  56. C =-1 if the matrix A is to be pre-scaled by
  57. C columns when appropriate.
  58. C If the scaling indicator is not equal to -1
  59. C no scaling will be attempted.
  60. C For most problems scaling will probably not be necessary.
  61. C Q -- Matrix used for the transformation, must be dimensioned
  62. C NRDA by M.
  63. C DIAG,KPIVOT,S, -- Arrays of length at least N used for internal
  64. C DIV,TD,SCALES storage (except for SCALES which is M).
  65. C ISFLG -- Storage for an internal variable.
  66. C
  67. C *********************************************************************
  68. C OUTPUT
  69. C *********************************************************************
  70. C
  71. C IFLAG -- Status indicator
  72. C =1 if solution was obtained.
  73. C =2 if improper input is detected.
  74. C =3 if rank of matrix is less than N.
  75. C To continue, simply reset IFLAG=1 and call DLSSUD again.
  76. C =4 if the system of equations appears to be inconsistent.
  77. C However, the least squares solution of minimal length
  78. C was obtained.
  79. C X -- Minimal length least squares solution of A Z = B
  80. C IRANK -- Numerically determined rank of A, must not be altered
  81. C on succeeding calls with input values of IFLAG=1.
  82. C U -- Matrix whose M-IRANK columns are mutually orthogonal unit
  83. C vectors which span the null space of A. This is to be ignored
  84. C when MLSO was set to zero or IFLAG=4 on output.
  85. C Q -- Contains the strictly upper triangular part of the reduced
  86. C matrix and transformation information.
  87. C DIAG -- Contains the diagonal elements of the triangular reduced
  88. C matrix.
  89. C KPIVOT -- Contains the pivotal information. The row interchanges
  90. C performed on the original matrix are recorded here.
  91. C S -- Contains the solution of the lower triangular system.
  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 DBVSUP, DSUDS
  99. C***REFERENCES H. A. Watts, Solving linear least squares problems
  100. C using SODS/SUDS/CODS, Sandia Report SAND77-0683,
  101. C Sandia Laboratories, 1977.
  102. C***ROUTINES CALLED D1MACH, DDOT, DOHTRL, DORTHR, J4SAVE, XERMAX,
  103. C XERMSG, XGETF, XSETF
  104. C***REVISION HISTORY (YYMMDD)
  105. C 750601 DATE WRITTEN
  106. C 890531 Changed all specific intrinsics to generic. (WRB)
  107. C 891214 Prologue converted to Version 4.0 format. (BAB)
  108. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  109. C 900328 Added TYPE section. (WRB)
  110. C 910408 Updated the AUTHOR and REFERENCES sections. (WRB)
  111. C 920501 Reformatted the REFERENCES section. (WRB)
  112. C***END PROLOGUE DLSSUD
  113. INTEGER J4SAVE
  114. DOUBLE PRECISION DDOT, D1MACH
  115. INTEGER I, IFLAG, IRANK, IRP, ISCALE, ISFLG, J, JR, K, KP,
  116. 1 KPIVOT(*), L, M, MAXMES, MJ, MLSO, N, NFAT, NFATAL, NMIR,
  117. 2 NRDA, NRDU, NU
  118. DOUBLE PRECISION A(NRDA,*), B(*), DIAG(*), DIV(*), GAM, GAMMA,
  119. 1 Q(NRDA,*), RES, S(*), SCALES(*), SS, TD(*), U(NRDU,*), URO,
  120. 2 X(*)
  121. C
  122. C ******************************************************************
  123. C
  124. C MACHINE PRECISION (COMPUTER UNIT ROUNDOFF VALUE) IS DEFINED
  125. C BY THE FUNCTION D1MACH.
  126. C
  127. C ******************************************************************
  128. C
  129. C BEGIN BLOCK PERMITTING ...EXITS TO 310
  130. C BEGIN BLOCK PERMITTING ...EXITS TO 80
  131. C***FIRST EXECUTABLE STATEMENT DLSSUD
  132. URO = D1MACH(4)
  133. C
  134. IF (N .LT. 1 .OR. M .LT. N .OR. NRDA .LT. N) GO TO 70
  135. IF (NRDU .NE. 0 .AND. NRDU .LT. M) GO TO 70
  136. IF (IFLAG .GT. 0) GO TO 60
  137. C
  138. CALL XGETF(NFATAL)
  139. MAXMES = J4SAVE(4,0,.FALSE.)
  140. ISFLG = -15
  141. IF (IFLAG .EQ. 0) GO TO 10
  142. ISFLG = IFLAG
  143. NFAT = -1
  144. IF (NFATAL .EQ. 0) NFAT = 0
  145. CALL XSETF(NFAT)
  146. CALL XERMAX(1)
  147. 10 CONTINUE
  148. C
  149. C COPY MATRIX A INTO MATRIX Q
  150. C
  151. DO 30 K = 1, M
  152. DO 20 J = 1, N
  153. Q(J,K) = A(J,K)
  154. 20 CONTINUE
  155. 30 CONTINUE
  156. C
  157. C USE ORTHOGONAL TRANSFORMATIONS TO REDUCE Q TO LOWER
  158. C TRIANGULAR FORM
  159. C
  160. CALL DORTHR(Q,N,M,NRDA,IFLAG,IRANK,ISCALE,DIAG,KPIVOT,
  161. 1 SCALES,DIV,TD)
  162. C
  163. CALL XSETF(NFATAL)
  164. CALL XERMAX(MAXMES)
  165. IF (IRANK .EQ. N) GO TO 40
  166. C
  167. C FOR RANK DEFICIENT PROBLEMS USE ADDITIONAL
  168. C ORTHOGONAL TRANSFORMATIONS TO FURTHER REDUCE Q
  169. C
  170. IF (IRANK .NE. 0)
  171. 1 CALL DOHTRL(Q,N,NRDA,DIAG,IRANK,DIV,TD)
  172. C ...............EXIT
  173. GO TO 310
  174. 40 CONTINUE
  175. C
  176. C STORE DIVISORS FOR THE TRIANGULAR SOLUTION
  177. C
  178. DO 50 K = 1, N
  179. DIV(K) = DIAG(K)
  180. 50 CONTINUE
  181. C .........EXIT
  182. GO TO 80
  183. 60 CONTINUE
  184. C ......EXIT
  185. IF (IFLAG .EQ. 1) GO TO 80
  186. 70 CONTINUE
  187. C
  188. C INVALID INPUT FOR DLSSUD
  189. IFLAG = 2
  190. CALL XERMSG ('SLATEC', 'DLSSUD',
  191. + 'INVALID IMPUT PARAMETERS.', 2, 1)
  192. C ......EXIT
  193. GO TO 310
  194. 80 CONTINUE
  195. C
  196. C
  197. IF (IRANK .GT. 0) GO TO 130
  198. C
  199. C SPECIAL CASE FOR THE NULL MATRIX
  200. DO 110 K = 1, M
  201. X(K) = 0.0D0
  202. IF (MLSO .EQ. 0) GO TO 100
  203. U(K,K) = 1.0D0
  204. DO 90 J = 1, M
  205. IF (J .NE. K) U(J,K) = 0.0D0
  206. 90 CONTINUE
  207. 100 CONTINUE
  208. 110 CONTINUE
  209. DO 120 K = 1, N
  210. IF (B(K) .GT. 0.0D0) IFLAG = 4
  211. 120 CONTINUE
  212. GO TO 300
  213. 130 CONTINUE
  214. C BEGIN BLOCK PERMITTING ...EXITS TO 180
  215. C
  216. C COPY CONSTANT VECTOR INTO S AFTER FIRST INTERCHANGING
  217. C THE ELEMENTS ACCORDING TO THE PIVOTAL SEQUENCE
  218. C
  219. DO 140 K = 1, N
  220. KP = KPIVOT(K)
  221. X(K) = B(KP)
  222. 140 CONTINUE
  223. DO 150 K = 1, N
  224. S(K) = X(K)
  225. 150 CONTINUE
  226. C
  227. IRP = IRANK + 1
  228. NU = 1
  229. IF (MLSO .EQ. 0) NU = 0
  230. C ...EXIT
  231. IF (IRANK .EQ. N) GO TO 180
  232. C
  233. C FOR RANK DEFICIENT PROBLEMS WE MUST APPLY THE
  234. C ORTHOGONAL TRANSFORMATION TO S
  235. C WE ALSO CHECK TO SEE IF THE SYSTEM APPEARS TO BE
  236. C INCONSISTENT
  237. C
  238. NMIR = N - IRANK
  239. SS = DDOT(N,S(1),1,S(1),1)
  240. DO 170 L = 1, IRANK
  241. K = IRP - L
  242. GAM = ((TD(K)*S(K)) + DDOT(NMIR,Q(IRP,K),1,S(IRP),1))
  243. 1 /(TD(K)*DIV(K))
  244. S(K) = S(K) + GAM*TD(K)
  245. DO 160 J = IRP, N
  246. S(J) = S(J) + GAM*Q(J,K)
  247. 160 CONTINUE
  248. 170 CONTINUE
  249. RES = DDOT(NMIR,S(IRP),1,S(IRP),1)
  250. C ...EXIT
  251. IF (RES
  252. 1 .LE. SS*(10.0D0*MAX(10.0D0**ISFLG,10.0D0*URO))**2)
  253. 2 GO TO 180
  254. C
  255. C INCONSISTENT SYSTEM
  256. IFLAG = 4
  257. NU = 0
  258. 180 CONTINUE
  259. C
  260. C APPLY FORWARD SUBSTITUTION TO SOLVE LOWER TRIANGULAR SYSTEM
  261. C
  262. S(1) = S(1)/DIV(1)
  263. IF (IRANK .LT. 2) GO TO 200
  264. DO 190 K = 2, IRANK
  265. S(K) = (S(K) - DDOT(K-1,Q(K,1),NRDA,S(1),1))/DIV(K)
  266. 190 CONTINUE
  267. 200 CONTINUE
  268. C
  269. C INITIALIZE X VECTOR AND THEN APPLY ORTHOGONAL TRANSFORMATION
  270. C
  271. DO 210 K = 1, M
  272. X(K) = 0.0D0
  273. IF (K .LE. IRANK) X(K) = S(K)
  274. 210 CONTINUE
  275. C
  276. DO 230 JR = 1, IRANK
  277. J = IRP - JR
  278. MJ = M - J + 1
  279. GAMMA = DDOT(MJ,Q(J,J),NRDA,X(J),1)/(DIAG(J)*Q(J,J))
  280. DO 220 K = J, M
  281. X(K) = X(K) + GAMMA*Q(J,K)
  282. 220 CONTINUE
  283. 230 CONTINUE
  284. C
  285. C RESCALE ANSWERS AS DICTATED
  286. C
  287. DO 240 K = 1, M
  288. X(K) = X(K)*SCALES(K)
  289. 240 CONTINUE
  290. C
  291. IF (NU .EQ. 0 .OR. M .EQ. IRANK) GO TO 290
  292. C
  293. C INITIALIZE U MATRIX AND THEN APPLY ORTHOGONAL
  294. C TRANSFORMATION
  295. C
  296. L = M - IRANK
  297. DO 280 K = 1, L
  298. DO 250 I = 1, M
  299. U(I,K) = 0.0D0
  300. IF (I .EQ. IRANK + K) U(I,K) = 1.0D0
  301. 250 CONTINUE
  302. C
  303. DO 270 JR = 1, IRANK
  304. J = IRP - JR
  305. MJ = M - J + 1
  306. GAMMA = DDOT(MJ,Q(J,J),NRDA,U(J,K),1)
  307. 1 /(DIAG(J)*Q(J,J))
  308. DO 260 I = J, M
  309. U(I,K) = U(I,K) + GAMMA*Q(J,I)
  310. 260 CONTINUE
  311. 270 CONTINUE
  312. 280 CONTINUE
  313. 290 CONTINUE
  314. 300 CONTINUE
  315. 310 CONTINUE
  316. C
  317. RETURN
  318. END