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.

cgerfs.f 13 kB

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