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.

csprfs.f 13 kB

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