bndsol.f 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. *DECK BNDSOL
  2. SUBROUTINE BNDSOL (MODE, G, MDG, NB, IP, IR, X, N, RNORM)
  3. C***BEGIN PROLOGUE BNDSOL
  4. C***PURPOSE Solve the least squares problem for a banded matrix using
  5. C sequential accumulation of rows of the data matrix.
  6. C Exactly one right-hand side vector is permitted.
  7. C***LIBRARY SLATEC
  8. C***CATEGORY D9
  9. C***TYPE SINGLE PRECISION (BNDSOL-S, DBNDSL-D)
  10. C***KEYWORDS BANDED MATRIX, CURVE FITTING, LEAST SQUARES
  11. C***AUTHOR Lawson, C. L., (JPL)
  12. C Hanson, R. J., (SNLA)
  13. C***DESCRIPTION
  14. C
  15. C These subroutines solve the least squares problem Ax = b for
  16. C banded matrices A using sequential accumulation of rows of the
  17. C data matrix. Exactly one right-hand side vector is permitted.
  18. C
  19. C These subroutines are intended for the type of least squares
  20. C systems that arise in applications such as curve or surface
  21. C fitting of data. The least squares equations are accumulated and
  22. C processed using only part of the data. This requires a certain
  23. C user interaction during the solution of Ax = b.
  24. C
  25. C Specifically, suppose the data matrix (A B) is row partitioned
  26. C into Q submatrices. Let (E F) be the T-th one of these
  27. C submatrices where E = (0 C 0). Here the dimension of E is MT by N
  28. C and the dimension of C is MT by NB. The value of NB is the
  29. C bandwidth of A. The dimensions of the leading block of zeros in E
  30. C are MT by JT-1.
  31. C
  32. C The user of the subroutine BNDACC provides MT,JT,C and F for
  33. C T=1,...,Q. Not all of this data must be supplied at once.
  34. C
  35. C Following the processing of the various blocks (E F), the matrix
  36. C (A B) has been transformed to the form (R D) where R is upper
  37. C triangular and banded with bandwidth NB. The least squares
  38. C system Rx = d is then easily solved using back substitution by
  39. C executing the statement CALL BNDSOL(1,...). The sequence of
  40. C values for JT must be nondecreasing. This may require some
  41. C preliminary interchanges of rows and columns of the matrix A.
  42. C
  43. C The primary reason for these subroutines is that the total
  44. C processing can take place in a working array of dimension MU by
  45. C NB+1. An acceptable value for MU is
  46. C
  47. C MU = MAX(MT + N + 1),
  48. C
  49. C where N is the number of unknowns.
  50. C
  51. C Here the maximum is taken over all values of MT for T=1,...,Q.
  52. C Notice that MT can be taken to be a small as one, showing that
  53. C MU can be as small as N+2. The subprogram BNDACC processes the
  54. C rows more efficiently if MU is large enough so that each new
  55. C block (C F) has a distinct value of JT.
  56. C
  57. C The four principle parts of these algorithms are obtained by the
  58. C following call statements
  59. C
  60. C CALL BNDACC(...) Introduce new blocks of data.
  61. C
  62. C CALL BNDSOL(1,...)Compute solution vector and length of
  63. C residual vector.
  64. C
  65. C CALL BNDSOL(2,...)Given any row vector H solve YR = H for the
  66. C row vector Y.
  67. C
  68. C CALL BNDSOL(3,...)Given any column vector W solve RZ = W for
  69. C the column vector Z.
  70. C
  71. C The dots in the above call statements indicate additional
  72. C arguments that will be specified in the following paragraphs.
  73. C
  74. C The user must dimension the array appearing in the call list..
  75. C G(MDG,NB+1)
  76. C
  77. C Description of calling sequence for BNDACC..
  78. C
  79. C The entire set of parameters for BNDACC are
  80. C
  81. C Input..
  82. C
  83. C G(*,*) The working array into which the user will
  84. C place the MT by NB+1 block (C F) in rows IR
  85. C through IR+MT-1, columns 1 through NB+1.
  86. C See descriptions of IR and MT below.
  87. C
  88. C MDG The number of rows in the working array
  89. C G(*,*). The value of MDG should be .GE. MU.
  90. C The value of MU is defined in the abstract
  91. C of these subprograms.
  92. C
  93. C NB The bandwidth of the data matrix A.
  94. C
  95. C IP Set by the user to the value 1 before the
  96. C first call to BNDACC. Its subsequent value
  97. C is controlled by BNDACC to set up for the
  98. C next call to BNDACC.
  99. C
  100. C IR Index of the row of G(*,*) where the user is
  101. C the user to the value 1 before the first call
  102. C to BNDACC. Its subsequent value is controlled
  103. C by BNDACC. A value of IR .GT. MDG is considered
  104. C an error.
  105. C
  106. C MT,JT Set by the user to indicate respectively the
  107. C number of new rows of data in the block and
  108. C the index of the first nonzero column in that
  109. C set of rows (E F) = (0 C 0 F) being processed.
  110. C Output..
  111. C
  112. C G(*,*) The working array which will contain the
  113. C processed rows of that part of the data
  114. C matrix which has been passed to BNDACC.
  115. C
  116. C IP,IR The values of these arguments are advanced by
  117. C BNDACC to be ready for storing and processing
  118. C a new block of data in G(*,*).
  119. C
  120. C Description of calling sequence for BNDSOL..
  121. C
  122. C The user must dimension the arrays appearing in the call list..
  123. C
  124. C G(MDG,NB+1), X(N)
  125. C
  126. C The entire set of parameters for BNDSOL are
  127. C
  128. C Input..
  129. C
  130. C MODE Set by the user to one of the values 1, 2, or
  131. C 3. These values respectively indicate that
  132. C the solution of AX = B, YR = H or RZ = W is
  133. C required.
  134. C
  135. C G(*,*),MDG, These arguments all have the same meaning and
  136. C NB,IP,IR contents as following the last call to BNDACC.
  137. C
  138. C X(*) With mode=2 or 3 this array contains,
  139. C respectively, the right-side vectors H or W of
  140. C the systems YR = H or RZ = W.
  141. C
  142. C N The number of variables in the solution
  143. C vector. If any of the N diagonal terms are
  144. C zero the subroutine BNDSOL prints an
  145. C appropriate message. This condition is
  146. C considered an error.
  147. C
  148. C Output..
  149. C
  150. C X(*) This array contains the solution vectors X,
  151. C Y or Z of the systems AX = B, YR = H or
  152. C RZ = W depending on the value of MODE=1,
  153. C 2 or 3.
  154. C
  155. C RNORM If MODE=1 RNORM is the Euclidean length of the
  156. C residual vector AX-B. When MODE=2 or 3 RNORM
  157. C is set to zero.
  158. C
  159. C Remarks..
  160. C
  161. C To obtain the upper triangular matrix and transformed right-hand
  162. C side vector D so that the super diagonals of R form the columns
  163. C of G(*,*), execute the following Fortran statements.
  164. C
  165. C NBP1=NB+1
  166. C
  167. C DO 10 J=1, NBP1
  168. C
  169. C 10 G(IR,J) = 0.E0
  170. C
  171. C MT=1
  172. C
  173. C JT=N+1
  174. C
  175. C CALL BNDACC(G,MDG,NB,IP,IR,MT,JT)
  176. C
  177. C***REFERENCES C. L. Lawson and R. J. Hanson, Solving Least Squares
  178. C Problems, Prentice-Hall, Inc., 1974, Chapter 27.
  179. C***ROUTINES CALLED XERMSG
  180. C***REVISION HISTORY (YYMMDD)
  181. C 790101 DATE WRITTEN
  182. C 890531 Changed all specific intrinsics to generic. (WRB)
  183. C 890831 Modified array declarations. (WRB)
  184. C 891006 Cosmetic changes to prologue. (WRB)
  185. C 891006 REVISION DATE from Version 3.2
  186. C 891214 Prologue converted to Version 4.0 format. (BAB)
  187. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  188. C 900326 Removed duplicate information from DESCRIPTION section.
  189. C (WRB)
  190. C 920501 Reformatted the REFERENCES section. (WRB)
  191. C***END PROLOGUE BNDSOL
  192. DIMENSION G(MDG,*),X(*)
  193. C***FIRST EXECUTABLE STATEMENT BNDSOL
  194. ZERO=0.
  195. C
  196. RNORM=ZERO
  197. GO TO (10,90,50), MODE
  198. C ********************* MODE = 1
  199. C ALG. STEP 26
  200. 10 DO 20 J=1,N
  201. X(J)=G(J,NB+1)
  202. 20 CONTINUE
  203. RSQ=ZERO
  204. NP1=N+1
  205. IRM1=IR-1
  206. IF (NP1.GT.IRM1) GO TO 40
  207. DO 30 J=NP1,IRM1
  208. RSQ=RSQ+G(J,NB+1)**2
  209. 30 CONTINUE
  210. RNORM=SQRT(RSQ)
  211. 40 CONTINUE
  212. C ********************* MODE = 3
  213. C ALG. STEP 27
  214. 50 DO 80 II=1,N
  215. I=N+1-II
  216. C ALG. STEP 28
  217. S=ZERO
  218. L=MAX(0,I-IP)
  219. C ALG. STEP 29
  220. IF (I.EQ.N) GO TO 70
  221. C ALG. STEP 30
  222. IE=MIN(N+1-I,NB)
  223. DO 60 J=2,IE
  224. JG=J+L
  225. IX=I-1+J
  226. S=S+G(I,JG)*X(IX)
  227. 60 CONTINUE
  228. C ALG. STEP 31
  229. 70 IF (G(I,L+1)) 80,130,80
  230. 80 X(I)=(X(I)-S)/G(I,L+1)
  231. C ALG. STEP 32
  232. RETURN
  233. C ********************* MODE = 2
  234. 90 DO 120 J=1,N
  235. S=ZERO
  236. IF (J.EQ.1) GO TO 110
  237. I1=MAX(1,J-NB+1)
  238. I2=J-1
  239. DO 100 I=I1,I2
  240. L=J-I+1+MAX(0,I-IP)
  241. S=S+X(I)*G(I,L)
  242. 100 CONTINUE
  243. 110 L=MAX(0,J-IP)
  244. IF (G(J,L+1)) 120,130,120
  245. 120 X(J)=(X(J)-S)/G(J,L+1)
  246. RETURN
  247. C
  248. 130 CONTINUE
  249. NERR=1
  250. IOPT=2
  251. CALL XERMSG ('SLATEC', 'BNDSOL',
  252. + 'A ZERO DIAGONAL TERM IS IN THE N BY N UPPER TRIANGULAR ' //
  253. + 'MATRIX.', NERR, IOPT)
  254. RETURN
  255. END