dhfti.f 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. *DECK DHFTI
  2. SUBROUTINE DHFTI (A, MDA, M, N, B, MDB, NB, TAU, KRANK, RNORM, H,
  3. + G, IP)
  4. C***BEGIN PROLOGUE DHFTI
  5. C***PURPOSE Solve a least squares problem for banded matrices using
  6. C sequential accumulation of rows of the data matrix.
  7. C Exactly one right-hand side vector is permitted.
  8. C***LIBRARY SLATEC
  9. C***CATEGORY D9
  10. C***TYPE DOUBLE PRECISION (HFTI-S, DHFTI-D)
  11. C***KEYWORDS CURVE FITTING, LEAST SQUARES
  12. C***AUTHOR Lawson, C. L., (JPL)
  13. C Hanson, R. J., (SNLA)
  14. C***DESCRIPTION
  15. C
  16. C DIMENSION A(MDA,N),(B(MDB,NB) or B(M)),RNORM(NB),H(N),G(N),IP(N)
  17. C
  18. C This subroutine solves a linear least squares problem or a set of
  19. C linear least squares problems having the same matrix but different
  20. C right-side vectors. The problem data consists of an M by N matrix
  21. C A, an M by NB matrix B, and an absolute tolerance parameter TAU
  22. C whose usage is described below. The NB column vectors of B
  23. C represent right-side vectors for NB distinct linear least squares
  24. C problems.
  25. C
  26. C This set of problems can also be written as the matrix least
  27. C squares problem
  28. C
  29. C AX = B,
  30. C
  31. C where X is the N by NB solution matrix.
  32. C
  33. C Note that if B is the M by M identity matrix, then X will be the
  34. C pseudo-inverse of A.
  35. C
  36. C This subroutine first transforms the augmented matrix (A B) to a
  37. C matrix (R C) using premultiplying Householder transformations with
  38. C column interchanges. All subdiagonal elements in the matrix R are
  39. C zero and its diagonal elements satisfy
  40. C
  41. C ABS(R(I,I)).GE.ABS(R(I+1,I+1)),
  42. C
  43. C I = 1,...,L-1, where
  44. C
  45. C L = MIN(M,N).
  46. C
  47. C The subroutine will compute an integer, KRANK, equal to the number
  48. C of diagonal terms of R that exceed TAU in magnitude. Then a
  49. C solution of minimum Euclidean length is computed using the first
  50. C KRANK rows of (R C).
  51. C
  52. C To be specific we suggest that the user consider an easily
  53. C computable matrix norm, such as, the maximum of all column sums of
  54. C magnitudes.
  55. C
  56. C Now if the relative uncertainty of B is EPS, (norm of uncertainty/
  57. C norm of B), it is suggested that TAU be set approximately equal to
  58. C EPS*(norm of A).
  59. C
  60. C The user must dimension all arrays appearing in the call list..
  61. C A(MDA,N),(B(MDB,NB) or B(M)),RNORM(NB),H(N),G(N),IP(N). This
  62. C permits the solution of a range of problems in the same array
  63. C space.
  64. C
  65. C The entire set of parameters for DHFTI are
  66. C
  67. C INPUT.. All TYPE REAL variables are DOUBLE PRECISION
  68. C
  69. C A(*,*),MDA,M,N The array A(*,*) initially contains the M by N
  70. C matrix A of the least squares problem AX = B.
  71. C The first dimensioning parameter of the array
  72. C A(*,*) is MDA, which must satisfy MDA.GE.M
  73. C Either M.GE.N or M.LT.N is permitted. There
  74. C is no restriction on the rank of A. The
  75. C condition MDA.LT.M is considered an error.
  76. C
  77. C B(*),MDB,NB If NB = 0 the subroutine will perform the
  78. C orthogonal decomposition but will make no
  79. C references to the array B(*). If NB.GT.0
  80. C the array B(*) must initially contain the M by
  81. C NB matrix B of the least squares problem AX =
  82. C B. If NB.GE.2 the array B(*) must be doubly
  83. C subscripted with first dimensioning parameter
  84. C MDB.GE.MAX(M,N). If NB = 1 the array B(*) may
  85. C be either doubly or singly subscripted. In
  86. C the latter case the value of MDB is arbitrary
  87. C but it should be set to some valid integer
  88. C value such as MDB = M.
  89. C
  90. C The condition of NB.GT.1.AND.MDB.LT. MAX(M,N)
  91. C is considered an error.
  92. C
  93. C TAU Absolute tolerance parameter provided by user
  94. C for pseudorank determination.
  95. C
  96. C H(*),G(*),IP(*) Arrays of working space used by DHFTI.
  97. C
  98. C OUTPUT.. All TYPE REAL variables are DOUBLE PRECISION
  99. C
  100. C A(*,*) The contents of the array A(*,*) will be
  101. C modified by the subroutine. These contents
  102. C are not generally required by the user.
  103. C
  104. C B(*) On return the array B(*) will contain the N by
  105. C NB solution matrix X.
  106. C
  107. C KRANK Set by the subroutine to indicate the
  108. C pseudorank of A.
  109. C
  110. C RNORM(*) On return, RNORM(J) will contain the Euclidean
  111. C norm of the residual vector for the problem
  112. C defined by the J-th column vector of the array
  113. C B(*,*) for J = 1,...,NB.
  114. C
  115. C H(*),G(*) On return these arrays respectively contain
  116. C elements of the pre- and post-multiplying
  117. C Householder transformations used to compute
  118. C the minimum Euclidean length solution.
  119. C
  120. C IP(*) Array in which the subroutine records indices
  121. C describing the permutation of column vectors.
  122. C The contents of arrays H(*),G(*) and IP(*)
  123. C are not generally required by the user.
  124. C
  125. C***REFERENCES C. L. Lawson and R. J. Hanson, Solving Least Squares
  126. C Problems, Prentice-Hall, Inc., 1974, Chapter 14.
  127. C***ROUTINES CALLED D1MACH, DH12, XERMSG
  128. C***REVISION HISTORY (YYMMDD)
  129. C 790101 DATE WRITTEN
  130. C 890531 Changed all specific intrinsics to generic. (WRB)
  131. C 891006 Cosmetic changes to prologue. (WRB)
  132. C 891006 REVISION DATE from Version 3.2
  133. C 891214 Prologue converted to Version 4.0 format. (BAB)
  134. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  135. C 901005 Replace usage of DDIFF with usage of D1MACH. (RWC)
  136. C 920501 Reformatted the REFERENCES section. (WRB)
  137. C***END PROLOGUE DHFTI
  138. INTEGER I, II, IOPT, IP(*), IP1, J, JB, JJ, K, KP1, KRANK, L,
  139. * LDIAG, LMAX, M, MDA, MDB, N, NB, NERR
  140. DOUBLE PRECISION A, B, D1MACH, DZERO, FACTOR,
  141. * G, H, HMAX, RELEPS, RNORM, SM, SM1, SZERO, TAU, TMP
  142. DIMENSION A(MDA,*),B(MDB,*),H(*),G(*),RNORM(*)
  143. SAVE RELEPS
  144. DATA RELEPS /0.D0/
  145. C BEGIN BLOCK PERMITTING ...EXITS TO 360
  146. C***FIRST EXECUTABLE STATEMENT DHFTI
  147. IF (RELEPS.EQ.0.D0) RELEPS = D1MACH(4)
  148. SZERO = 0.0D0
  149. DZERO = 0.0D0
  150. FACTOR = 0.001D0
  151. C
  152. K = 0
  153. LDIAG = MIN(M,N)
  154. IF (LDIAG .LE. 0) GO TO 350
  155. C BEGIN BLOCK PERMITTING ...EXITS TO 130
  156. C BEGIN BLOCK PERMITTING ...EXITS TO 120
  157. IF (MDA .GE. M) GO TO 10
  158. NERR = 1
  159. IOPT = 2
  160. CALL XERMSG ('SLATEC', 'DHFTI',
  161. + 'MDA.LT.M, PROBABLE ERROR.',
  162. + NERR, IOPT)
  163. C ...............EXIT
  164. GO TO 360
  165. 10 CONTINUE
  166. C
  167. IF (NB .LE. 1 .OR. MAX(M,N) .LE. MDB) GO TO 20
  168. NERR = 2
  169. IOPT = 2
  170. CALL XERMSG ('SLATEC', 'DHFTI',
  171. + 'MDB.LT.MAX(M,N).AND.NB.GT.1. PROBABLE ERROR.',
  172. + NERR, IOPT)
  173. C ...............EXIT
  174. GO TO 360
  175. 20 CONTINUE
  176. C
  177. DO 100 J = 1, LDIAG
  178. C BEGIN BLOCK PERMITTING ...EXITS TO 70
  179. IF (J .EQ. 1) GO TO 40
  180. C
  181. C UPDATE SQUARED COLUMN LENGTHS AND FIND LMAX
  182. C ..
  183. LMAX = J
  184. DO 30 L = J, N
  185. H(L) = H(L) - A(J-1,L)**2
  186. IF (H(L) .GT. H(LMAX)) LMAX = L
  187. 30 CONTINUE
  188. C ......EXIT
  189. IF (FACTOR*H(LMAX) .GT. HMAX*RELEPS) GO TO 70
  190. 40 CONTINUE
  191. C
  192. C COMPUTE SQUARED COLUMN LENGTHS AND FIND LMAX
  193. C ..
  194. LMAX = J
  195. DO 60 L = J, N
  196. H(L) = 0.0D0
  197. DO 50 I = J, M
  198. H(L) = H(L) + A(I,L)**2
  199. 50 CONTINUE
  200. IF (H(L) .GT. H(LMAX)) LMAX = L
  201. 60 CONTINUE
  202. HMAX = H(LMAX)
  203. 70 CONTINUE
  204. C ..
  205. C LMAX HAS BEEN DETERMINED
  206. C
  207. C DO COLUMN INTERCHANGES IF NEEDED.
  208. C ..
  209. IP(J) = LMAX
  210. IF (IP(J) .EQ. J) GO TO 90
  211. DO 80 I = 1, M
  212. TMP = A(I,J)
  213. A(I,J) = A(I,LMAX)
  214. A(I,LMAX) = TMP
  215. 80 CONTINUE
  216. H(LMAX) = H(J)
  217. 90 CONTINUE
  218. C
  219. C COMPUTE THE J-TH TRANSFORMATION AND APPLY IT TO A
  220. C AND B.
  221. C ..
  222. CALL DH12(1,J,J+1,M,A(1,J),1,H(J),A(1,J+1),1,MDA,
  223. * N-J)
  224. CALL DH12(2,J,J+1,M,A(1,J),1,H(J),B,1,MDB,NB)
  225. 100 CONTINUE
  226. C
  227. C DETERMINE THE PSEUDORANK, K, USING THE TOLERANCE,
  228. C TAU.
  229. C ..
  230. DO 110 J = 1, LDIAG
  231. C ......EXIT
  232. IF (ABS(A(J,J)) .LE. TAU) GO TO 120
  233. 110 CONTINUE
  234. K = LDIAG
  235. C ......EXIT
  236. GO TO 130
  237. 120 CONTINUE
  238. K = J - 1
  239. 130 CONTINUE
  240. KP1 = K + 1
  241. C
  242. C COMPUTE THE NORMS OF THE RESIDUAL VECTORS.
  243. C
  244. IF (NB .LT. 1) GO TO 170
  245. DO 160 JB = 1, NB
  246. TMP = SZERO
  247. IF (M .LT. KP1) GO TO 150
  248. DO 140 I = KP1, M
  249. TMP = TMP + B(I,JB)**2
  250. 140 CONTINUE
  251. 150 CONTINUE
  252. RNORM(JB) = SQRT(TMP)
  253. 160 CONTINUE
  254. 170 CONTINUE
  255. C SPECIAL FOR PSEUDORANK = 0
  256. IF (K .GT. 0) GO TO 210
  257. IF (NB .LT. 1) GO TO 200
  258. DO 190 JB = 1, NB
  259. DO 180 I = 1, N
  260. B(I,JB) = SZERO
  261. 180 CONTINUE
  262. 190 CONTINUE
  263. 200 CONTINUE
  264. GO TO 340
  265. 210 CONTINUE
  266. C
  267. C IF THE PSEUDORANK IS LESS THAN N COMPUTE HOUSEHOLDER
  268. C DECOMPOSITION OF FIRST K ROWS.
  269. C ..
  270. IF (K .EQ. N) GO TO 230
  271. DO 220 II = 1, K
  272. I = KP1 - II
  273. CALL DH12(1,I,KP1,N,A(I,1),MDA,G(I),A,MDA,1,I-1)
  274. 220 CONTINUE
  275. 230 CONTINUE
  276. C
  277. C
  278. IF (NB .LT. 1) GO TO 330
  279. DO 320 JB = 1, NB
  280. C
  281. C SOLVE THE K BY K TRIANGULAR SYSTEM.
  282. C ..
  283. DO 260 L = 1, K
  284. SM = DZERO
  285. I = KP1 - L
  286. IP1 = I + 1
  287. IF (K .LT. IP1) GO TO 250
  288. DO 240 J = IP1, K
  289. SM = SM + A(I,J)*B(J,JB)
  290. 240 CONTINUE
  291. 250 CONTINUE
  292. SM1 = SM
  293. B(I,JB) = (B(I,JB) - SM1)/A(I,I)
  294. 260 CONTINUE
  295. C
  296. C COMPLETE COMPUTATION OF SOLUTION VECTOR.
  297. C ..
  298. IF (K .EQ. N) GO TO 290
  299. DO 270 J = KP1, N
  300. B(J,JB) = SZERO
  301. 270 CONTINUE
  302. DO 280 I = 1, K
  303. CALL DH12(2,I,KP1,N,A(I,1),MDA,G(I),B(1,JB),1,
  304. * MDB,1)
  305. 280 CONTINUE
  306. 290 CONTINUE
  307. C
  308. C RE-ORDER THE SOLUTION VECTOR TO COMPENSATE FOR THE
  309. C COLUMN INTERCHANGES.
  310. C ..
  311. DO 310 JJ = 1, LDIAG
  312. J = LDIAG + 1 - JJ
  313. IF (IP(J) .EQ. J) GO TO 300
  314. L = IP(J)
  315. TMP = B(L,JB)
  316. B(L,JB) = B(J,JB)
  317. B(J,JB) = TMP
  318. 300 CONTINUE
  319. 310 CONTINUE
  320. 320 CONTINUE
  321. 330 CONTINUE
  322. 340 CONTINUE
  323. 350 CONTINUE
  324. C ..
  325. C THE SOLUTION VECTORS, X, ARE NOW
  326. C IN THE FIRST N ROWS OF THE ARRAY B(,).
  327. C
  328. KRANK = K
  329. 360 CONTINUE
  330. RETURN
  331. END