You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ssprfs.f 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. *> \brief \b SSPRFS
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download SSPRFS + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/ssprfs.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/ssprfs.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/ssprfs.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE SSPRFS( UPLO, N, NRHS, AP, AFP, IPIV, B, LDB, X, LDX,
  22. * FERR, BERR, WORK, IWORK, INFO )
  23. *
  24. * .. Scalar Arguments ..
  25. * CHARACTER UPLO
  26. * INTEGER INFO, LDB, LDX, N, NRHS
  27. * ..
  28. * .. Array Arguments ..
  29. * INTEGER IPIV( * ), IWORK( * )
  30. * REAL AFP( * ), AP( * ), B( LDB, * ), BERR( * ),
  31. * $ FERR( * ), WORK( * ), X( LDX, * )
  32. * ..
  33. *
  34. *
  35. *> \par Purpose:
  36. * =============
  37. *>
  38. *> \verbatim
  39. *>
  40. *> SSPRFS improves the computed solution to a system of linear
  41. *> equations when the coefficient matrix is symmetric indefinite
  42. *> and packed, and provides error bounds and backward error estimates
  43. *> for the solution.
  44. *> \endverbatim
  45. *
  46. * Arguments:
  47. * ==========
  48. *
  49. *> \param[in] UPLO
  50. *> \verbatim
  51. *> UPLO is CHARACTER*1
  52. *> = 'U': Upper triangle of A is stored;
  53. *> = 'L': Lower triangle of A is stored.
  54. *> \endverbatim
  55. *>
  56. *> \param[in] N
  57. *> \verbatim
  58. *> N is INTEGER
  59. *> The order of the matrix A. N >= 0.
  60. *> \endverbatim
  61. *>
  62. *> \param[in] NRHS
  63. *> \verbatim
  64. *> NRHS is INTEGER
  65. *> The number of right hand sides, i.e., the number of columns
  66. *> of the matrices B and X. NRHS >= 0.
  67. *> \endverbatim
  68. *>
  69. *> \param[in] AP
  70. *> \verbatim
  71. *> AP is REAL array, dimension (N*(N+1)/2)
  72. *> The upper or lower triangle of the symmetric matrix A, packed
  73. *> columnwise in a linear array. The j-th column of A is stored
  74. *> in the array AP as follows:
  75. *> if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
  76. *> if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
  77. *> \endverbatim
  78. *>
  79. *> \param[in] AFP
  80. *> \verbatim
  81. *> AFP is REAL array, dimension (N*(N+1)/2)
  82. *> The factored form of the matrix A. AFP contains the block
  83. *> diagonal matrix D and the multipliers used to obtain the
  84. *> factor U or L from the factorization A = U*D*U**T or
  85. *> A = L*D*L**T as computed by SSPTRF, stored as a packed
  86. *> triangular matrix.
  87. *> \endverbatim
  88. *>
  89. *> \param[in] IPIV
  90. *> \verbatim
  91. *> IPIV is INTEGER array, dimension (N)
  92. *> Details of the interchanges and the block structure of D
  93. *> as determined by SSPTRF.
  94. *> \endverbatim
  95. *>
  96. *> \param[in] B
  97. *> \verbatim
  98. *> B is REAL array, dimension (LDB,NRHS)
  99. *> The right hand side matrix B.
  100. *> \endverbatim
  101. *>
  102. *> \param[in] LDB
  103. *> \verbatim
  104. *> LDB is INTEGER
  105. *> The leading dimension of the array B. LDB >= max(1,N).
  106. *> \endverbatim
  107. *>
  108. *> \param[in,out] X
  109. *> \verbatim
  110. *> X is REAL array, dimension (LDX,NRHS)
  111. *> On entry, the solution matrix X, as computed by SSPTRS.
  112. *> On exit, the improved solution matrix X.
  113. *> \endverbatim
  114. *>
  115. *> \param[in] LDX
  116. *> \verbatim
  117. *> LDX is INTEGER
  118. *> The leading dimension of the array X. LDX >= max(1,N).
  119. *> \endverbatim
  120. *>
  121. *> \param[out] FERR
  122. *> \verbatim
  123. *> FERR is REAL array, dimension (NRHS)
  124. *> The estimated forward error bound for each solution vector
  125. *> X(j) (the j-th column of the solution matrix X).
  126. *> If XTRUE is the true solution corresponding to X(j), FERR(j)
  127. *> is an estimated upper bound for the magnitude of the largest
  128. *> element in (X(j) - XTRUE) divided by the magnitude of the
  129. *> largest element in X(j). The estimate is as reliable as
  130. *> the estimate for RCOND, and is almost always a slight
  131. *> overestimate of the true error.
  132. *> \endverbatim
  133. *>
  134. *> \param[out] BERR
  135. *> \verbatim
  136. *> BERR is REAL array, dimension (NRHS)
  137. *> The componentwise relative backward error of each solution
  138. *> vector X(j) (i.e., the smallest relative change in
  139. *> any element of A or B that makes X(j) an exact solution).
  140. *> \endverbatim
  141. *>
  142. *> \param[out] WORK
  143. *> \verbatim
  144. *> WORK is REAL array, dimension (3*N)
  145. *> \endverbatim
  146. *>
  147. *> \param[out] IWORK
  148. *> \verbatim
  149. *> IWORK is INTEGER array, dimension (N)
  150. *> \endverbatim
  151. *>
  152. *> \param[out] INFO
  153. *> \verbatim
  154. *> INFO is INTEGER
  155. *> = 0: successful exit
  156. *> < 0: if INFO = -i, the i-th argument had an illegal value
  157. *> \endverbatim
  158. *
  159. *> \par Internal Parameters:
  160. * =========================
  161. *>
  162. *> \verbatim
  163. *> ITMAX is the maximum number of steps of iterative refinement.
  164. *> \endverbatim
  165. *
  166. * Authors:
  167. * ========
  168. *
  169. *> \author Univ. of Tennessee
  170. *> \author Univ. of California Berkeley
  171. *> \author Univ. of Colorado Denver
  172. *> \author NAG Ltd.
  173. *
  174. *> \ingroup realOTHERcomputational
  175. *
  176. * =====================================================================
  177. SUBROUTINE SSPRFS( UPLO, N, NRHS, AP, AFP, IPIV, B, LDB, X, LDX,
  178. $ FERR, BERR, WORK, IWORK, INFO )
  179. *
  180. * -- LAPACK computational routine --
  181. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  182. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  183. *
  184. * .. Scalar Arguments ..
  185. CHARACTER UPLO
  186. INTEGER INFO, LDB, LDX, N, NRHS
  187. * ..
  188. * .. Array Arguments ..
  189. INTEGER IPIV( * ), IWORK( * )
  190. REAL AFP( * ), AP( * ), B( LDB, * ), BERR( * ),
  191. $ FERR( * ), WORK( * ), X( LDX, * )
  192. * ..
  193. *
  194. * =====================================================================
  195. *
  196. * .. Parameters ..
  197. INTEGER ITMAX
  198. PARAMETER ( ITMAX = 5 )
  199. REAL ZERO
  200. PARAMETER ( ZERO = 0.0E+0 )
  201. REAL ONE
  202. PARAMETER ( ONE = 1.0E+0 )
  203. REAL TWO
  204. PARAMETER ( TWO = 2.0E+0 )
  205. REAL THREE
  206. PARAMETER ( THREE = 3.0E+0 )
  207. * ..
  208. * .. Local Scalars ..
  209. LOGICAL UPPER
  210. INTEGER COUNT, I, IK, J, K, KASE, KK, NZ
  211. REAL EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
  212. * ..
  213. * .. Local Arrays ..
  214. INTEGER ISAVE( 3 )
  215. * ..
  216. * .. External Subroutines ..
  217. EXTERNAL SAXPY, SCOPY, SLACN2, SSPMV, SSPTRS, XERBLA
  218. * ..
  219. * .. Intrinsic Functions ..
  220. INTRINSIC ABS, MAX
  221. * ..
  222. * .. External Functions ..
  223. LOGICAL LSAME
  224. REAL SLAMCH
  225. EXTERNAL LSAME, SLAMCH
  226. * ..
  227. * .. Executable Statements ..
  228. *
  229. * Test the input parameters.
  230. *
  231. INFO = 0
  232. UPPER = LSAME( UPLO, 'U' )
  233. IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
  234. INFO = -1
  235. ELSE IF( N.LT.0 ) THEN
  236. INFO = -2
  237. ELSE IF( NRHS.LT.0 ) THEN
  238. INFO = -3
  239. ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
  240. INFO = -8
  241. ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
  242. INFO = -10
  243. END IF
  244. IF( INFO.NE.0 ) THEN
  245. CALL XERBLA( 'SSPRFS', -INFO )
  246. RETURN
  247. END IF
  248. *
  249. * Quick return if possible
  250. *
  251. IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
  252. DO 10 J = 1, NRHS
  253. FERR( J ) = ZERO
  254. BERR( J ) = ZERO
  255. 10 CONTINUE
  256. RETURN
  257. END IF
  258. *
  259. * NZ = maximum number of nonzero elements in each row of A, plus 1
  260. *
  261. NZ = N + 1
  262. EPS = SLAMCH( 'Epsilon' )
  263. SAFMIN = SLAMCH( 'Safe minimum' )
  264. SAFE1 = NZ*SAFMIN
  265. SAFE2 = SAFE1 / EPS
  266. *
  267. * Do for each right hand side
  268. *
  269. DO 140 J = 1, NRHS
  270. *
  271. COUNT = 1
  272. LSTRES = THREE
  273. 20 CONTINUE
  274. *
  275. * Loop until stopping criterion is satisfied.
  276. *
  277. * Compute residual R = B - A * X
  278. *
  279. CALL SCOPY( N, B( 1, J ), 1, WORK( N+1 ), 1 )
  280. CALL SSPMV( UPLO, N, -ONE, AP, X( 1, J ), 1, ONE, WORK( N+1 ),
  281. $ 1 )
  282. *
  283. * Compute componentwise relative backward error from formula
  284. *
  285. * max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
  286. *
  287. * where abs(Z) is the componentwise absolute value of the matrix
  288. * or vector Z. If the i-th component of the denominator is less
  289. * than SAFE2, then SAFE1 is added to the i-th components of the
  290. * numerator and denominator before dividing.
  291. *
  292. DO 30 I = 1, N
  293. WORK( I ) = ABS( B( I, J ) )
  294. 30 CONTINUE
  295. *
  296. * Compute abs(A)*abs(X) + abs(B).
  297. *
  298. KK = 1
  299. IF( UPPER ) THEN
  300. DO 50 K = 1, N
  301. S = ZERO
  302. XK = ABS( X( K, J ) )
  303. IK = KK
  304. DO 40 I = 1, K - 1
  305. WORK( I ) = WORK( I ) + ABS( AP( IK ) )*XK
  306. S = S + ABS( AP( IK ) )*ABS( X( I, J ) )
  307. IK = IK + 1
  308. 40 CONTINUE
  309. WORK( K ) = WORK( K ) + ABS( AP( KK+K-1 ) )*XK + S
  310. KK = KK + K
  311. 50 CONTINUE
  312. ELSE
  313. DO 70 K = 1, N
  314. S = ZERO
  315. XK = ABS( X( K, J ) )
  316. WORK( K ) = WORK( K ) + ABS( AP( KK ) )*XK
  317. IK = KK + 1
  318. DO 60 I = K + 1, N
  319. WORK( I ) = WORK( I ) + ABS( AP( IK ) )*XK
  320. S = S + ABS( AP( IK ) )*ABS( X( I, J ) )
  321. IK = IK + 1
  322. 60 CONTINUE
  323. WORK( K ) = WORK( K ) + S
  324. KK = KK + ( N-K+1 )
  325. 70 CONTINUE
  326. END IF
  327. S = ZERO
  328. DO 80 I = 1, N
  329. IF( WORK( I ).GT.SAFE2 ) THEN
  330. S = MAX( S, ABS( WORK( N+I ) ) / WORK( I ) )
  331. ELSE
  332. S = MAX( S, ( ABS( WORK( N+I ) )+SAFE1 ) /
  333. $ ( WORK( I )+SAFE1 ) )
  334. END IF
  335. 80 CONTINUE
  336. BERR( J ) = S
  337. *
  338. * Test stopping criterion. Continue iterating if
  339. * 1) The residual BERR(J) is larger than machine epsilon, and
  340. * 2) BERR(J) decreased by at least a factor of 2 during the
  341. * last iteration, and
  342. * 3) At most ITMAX iterations tried.
  343. *
  344. IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
  345. $ COUNT.LE.ITMAX ) THEN
  346. *
  347. * Update solution and try again.
  348. *
  349. CALL SSPTRS( UPLO, N, 1, AFP, IPIV, WORK( N+1 ), N, INFO )
  350. CALL SAXPY( N, ONE, WORK( N+1 ), 1, X( 1, J ), 1 )
  351. LSTRES = BERR( J )
  352. COUNT = COUNT + 1
  353. GO TO 20
  354. END IF
  355. *
  356. * Bound error from formula
  357. *
  358. * norm(X - XTRUE) / norm(X) .le. FERR =
  359. * norm( abs(inv(A))*
  360. * ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
  361. *
  362. * where
  363. * norm(Z) is the magnitude of the largest component of Z
  364. * inv(A) is the inverse of A
  365. * abs(Z) is the componentwise absolute value of the matrix or
  366. * vector Z
  367. * NZ is the maximum number of nonzeros in any row of A, plus 1
  368. * EPS is machine epsilon
  369. *
  370. * The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
  371. * is incremented by SAFE1 if the i-th component of
  372. * abs(A)*abs(X) + abs(B) is less than SAFE2.
  373. *
  374. * Use SLACN2 to estimate the infinity-norm of the matrix
  375. * inv(A) * diag(W),
  376. * where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
  377. *
  378. DO 90 I = 1, N
  379. IF( WORK( I ).GT.SAFE2 ) THEN
  380. WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I )
  381. ELSE
  382. WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I ) + SAFE1
  383. END IF
  384. 90 CONTINUE
  385. *
  386. KASE = 0
  387. 100 CONTINUE
  388. CALL SLACN2( N, WORK( 2*N+1 ), WORK( N+1 ), IWORK, FERR( J ),
  389. $ KASE, ISAVE )
  390. IF( KASE.NE.0 ) THEN
  391. IF( KASE.EQ.1 ) THEN
  392. *
  393. * Multiply by diag(W)*inv(A**T).
  394. *
  395. CALL SSPTRS( UPLO, N, 1, AFP, IPIV, WORK( N+1 ), N,
  396. $ INFO )
  397. DO 110 I = 1, N
  398. WORK( N+I ) = WORK( I )*WORK( N+I )
  399. 110 CONTINUE
  400. ELSE IF( KASE.EQ.2 ) THEN
  401. *
  402. * Multiply by inv(A)*diag(W).
  403. *
  404. DO 120 I = 1, N
  405. WORK( N+I ) = WORK( I )*WORK( N+I )
  406. 120 CONTINUE
  407. CALL SSPTRS( UPLO, N, 1, AFP, IPIV, WORK( N+1 ), N,
  408. $ INFO )
  409. END IF
  410. GO TO 100
  411. END IF
  412. *
  413. * Normalize error.
  414. *
  415. LSTRES = ZERO
  416. DO 130 I = 1, N
  417. LSTRES = MAX( LSTRES, ABS( X( I, J ) ) )
  418. 130 CONTINUE
  419. IF( LSTRES.NE.ZERO )
  420. $ FERR( J ) = FERR( J ) / LSTRES
  421. *
  422. 140 CONTINUE
  423. *
  424. RETURN
  425. *
  426. * End of SSPRFS
  427. *
  428. END