dqrslv.f 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. *DECK DQRSLV
  2. SUBROUTINE DQRSLV (N, R, LDR, IPVT, DIAG, QTB, X, SIGMA, WA)
  3. C***BEGIN PROLOGUE DQRSLV
  4. C***SUBSIDIARY
  5. C***PURPOSE Subsidiary to DNLS1 and DNLS1E
  6. C***LIBRARY SLATEC
  7. C***TYPE DOUBLE PRECISION (QRSOLV-S, DQRSLV-D)
  8. C***AUTHOR (UNKNOWN)
  9. C***DESCRIPTION
  10. C
  11. C **** Double Precision version of QRSOLV ****
  12. C
  13. C Given an M by N matrix A, an N by N diagonal matrix D,
  14. C and an M-vector B, the problem is to determine an X which
  15. C solves the system
  16. C
  17. C A*X = B , D*X = 0 ,
  18. C
  19. C in the least squares sense.
  20. C
  21. C This subroutine completes the solution of the problem
  22. C if it is provided with the necessary information from the
  23. C QR factorization, with column pivoting, of A. That is, if
  24. C A*P = Q*R, where P is a permutation matrix, Q has orthogonal
  25. C columns, and R is an upper triangular matrix with diagonal
  26. C elements of nonincreasing magnitude, then DQRSLV expects
  27. C the full upper triangle of R, the permutation matrix P,
  28. C and the first N components of (Q TRANSPOSE)*B. The system
  29. C A*X = B, D*X = 0, is then equivalent to
  30. C
  31. C T T
  32. C R*Z = Q *B , P *D*P*Z = 0 ,
  33. C
  34. C where X = P*Z. If this system does not have full rank,
  35. C then a least squares solution is obtained. On output DQRSLV
  36. C also provides an upper triangular matrix S such that
  37. C
  38. C T T T
  39. C P *(A *A + D*D)*P = S *S .
  40. C
  41. C S is computed within DQRSLV and may be of separate interest.
  42. C
  43. C The subroutine statement is
  44. C
  45. C SUBROUTINE DQRSLV(N,R,LDR,IPVT,DIAG,QTB,X,SIGMA,WA)
  46. C
  47. C where
  48. C
  49. C N is a positive integer input variable set to the order of R.
  50. C
  51. C R is an N by N array. On input the full upper triangle
  52. C must contain the full upper triangle of the matrix R.
  53. C On output the full upper triangle is unaltered, and the
  54. C strict lower triangle contains the strict upper triangle
  55. C (transposed) of the upper triangular matrix S.
  56. C
  57. C LDR is a positive integer input variable not less than N
  58. C which specifies the leading dimension of the array R.
  59. C
  60. C IPVT is an integer input array of length N which defines the
  61. C permutation matrix P such that A*P = Q*R. Column J of P
  62. C is column IPVT(J) of the identity matrix.
  63. C
  64. C DIAG is an input array of length N which must contain the
  65. C diagonal elements of the matrix D.
  66. C
  67. C QTB is an input array of length N which must contain the first
  68. C N elements of the vector (Q TRANSPOSE)*B.
  69. C
  70. C X is an output array of length N which contains the least
  71. C squares solution of the system A*X = B, D*X = 0.
  72. C
  73. C SIGMA is an output array of length N which contains the
  74. C diagonal elements of the upper triangular matrix S.
  75. C
  76. C WA is a work array of length N.
  77. C
  78. C***SEE ALSO DNLS1, DNLS1E
  79. C***ROUTINES CALLED (NONE)
  80. C***REVISION HISTORY (YYMMDD)
  81. C 800301 DATE WRITTEN
  82. C 890531 Changed all specific intrinsics to generic. (WRB)
  83. C 890831 Modified array declarations. (WRB)
  84. C 891214 Prologue converted to Version 4.0 format. (BAB)
  85. C 900326 Removed duplicate information from DESCRIPTION section.
  86. C (WRB)
  87. C 900328 Added TYPE section. (WRB)
  88. C***END PROLOGUE DQRSLV
  89. INTEGER N,LDR
  90. INTEGER IPVT(*)
  91. DOUBLE PRECISION R(LDR,*),DIAG(*),QTB(*),X(*),SIGMA(*),WA(*)
  92. INTEGER I,J,JP1,K,KP1,L,NSING
  93. DOUBLE PRECISION COS,COTAN,P5,P25,QTBPJ,SIN,SUM,TAN,TEMP,ZERO
  94. SAVE P5, P25, ZERO
  95. DATA P5,P25,ZERO /5.0D-1,2.5D-1,0.0D0/
  96. C***FIRST EXECUTABLE STATEMENT DQRSLV
  97. DO 20 J = 1, N
  98. DO 10 I = J, N
  99. R(I,J) = R(J,I)
  100. 10 CONTINUE
  101. X(J) = R(J,J)
  102. WA(J) = QTB(J)
  103. 20 CONTINUE
  104. C
  105. C ELIMINATE THE DIAGONAL MATRIX D USING A GIVENS ROTATION.
  106. C
  107. DO 100 J = 1, N
  108. C
  109. C PREPARE THE ROW OF D TO BE ELIMINATED, LOCATING THE
  110. C DIAGONAL ELEMENT USING P FROM THE QR FACTORIZATION.
  111. C
  112. L = IPVT(J)
  113. IF (DIAG(L) .EQ. ZERO) GO TO 90
  114. DO 30 K = J, N
  115. SIGMA(K) = ZERO
  116. 30 CONTINUE
  117. SIGMA(J) = DIAG(L)
  118. C
  119. C THE TRANSFORMATIONS TO ELIMINATE THE ROW OF D
  120. C MODIFY ONLY A SINGLE ELEMENT OF (Q TRANSPOSE)*B
  121. C BEYOND THE FIRST N, WHICH IS INITIALLY ZERO.
  122. C
  123. QTBPJ = ZERO
  124. DO 80 K = J, N
  125. C
  126. C DETERMINE A GIVENS ROTATION WHICH ELIMINATES THE
  127. C APPROPRIATE ELEMENT IN THE CURRENT ROW OF D.
  128. C
  129. IF (SIGMA(K) .EQ. ZERO) GO TO 70
  130. IF (ABS(R(K,K)) .GE. ABS(SIGMA(K))) GO TO 40
  131. COTAN = R(K,K)/SIGMA(K)
  132. SIN = P5/SQRT(P25+P25*COTAN**2)
  133. COS = SIN*COTAN
  134. GO TO 50
  135. 40 CONTINUE
  136. TAN = SIGMA(K)/R(K,K)
  137. COS = P5/SQRT(P25+P25*TAN**2)
  138. SIN = COS*TAN
  139. 50 CONTINUE
  140. C
  141. C COMPUTE THE MODIFIED DIAGONAL ELEMENT OF R AND
  142. C THE MODIFIED ELEMENT OF ((Q TRANSPOSE)*B,0).
  143. C
  144. R(K,K) = COS*R(K,K) + SIN*SIGMA(K)
  145. TEMP = COS*WA(K) + SIN*QTBPJ
  146. QTBPJ = -SIN*WA(K) + COS*QTBPJ
  147. WA(K) = TEMP
  148. C
  149. C ACCUMULATE THE TRANSFORMATION IN THE ROW OF S.
  150. C
  151. KP1 = K + 1
  152. IF (N .LT. KP1) GO TO 70
  153. DO 60 I = KP1, N
  154. TEMP = COS*R(I,K) + SIN*SIGMA(I)
  155. SIGMA(I) = -SIN*R(I,K) + COS*SIGMA(I)
  156. R(I,K) = TEMP
  157. 60 CONTINUE
  158. 70 CONTINUE
  159. 80 CONTINUE
  160. 90 CONTINUE
  161. C
  162. C STORE THE DIAGONAL ELEMENT OF S AND RESTORE
  163. C THE CORRESPONDING DIAGONAL ELEMENT OF R.
  164. C
  165. SIGMA(J) = R(J,J)
  166. R(J,J) = X(J)
  167. 100 CONTINUE
  168. C
  169. C SOLVE THE TRIANGULAR SYSTEM FOR Z. IF THE SYSTEM IS
  170. C SINGULAR, THEN OBTAIN A LEAST SQUARES SOLUTION.
  171. C
  172. NSING = N
  173. DO 110 J = 1, N
  174. IF (SIGMA(J) .EQ. ZERO .AND. NSING .EQ. N) NSING = J - 1
  175. IF (NSING .LT. N) WA(J) = ZERO
  176. 110 CONTINUE
  177. IF (NSING .LT. 1) GO TO 150
  178. DO 140 K = 1, NSING
  179. J = NSING - K + 1
  180. SUM = ZERO
  181. JP1 = J + 1
  182. IF (NSING .LT. JP1) GO TO 130
  183. DO 120 I = JP1, NSING
  184. SUM = SUM + R(I,J)*WA(I)
  185. 120 CONTINUE
  186. 130 CONTINUE
  187. WA(J) = (WA(J) - SUM)/SIGMA(J)
  188. 140 CONTINUE
  189. 150 CONTINUE
  190. C
  191. C PERMUTE THE COMPONENTS OF Z BACK TO COMPONENTS OF X.
  192. C
  193. DO 160 J = 1, N
  194. L = IPVT(J)
  195. X(L) = WA(J)
  196. 160 CONTINUE
  197. RETURN
  198. C
  199. C LAST CARD OF SUBROUTINE DQRSLV.
  200. C
  201. END