001:       SUBROUTINE CPORFS( UPLO, N, NRHS, A, LDA, AF, LDAF, B, LDB, X,
002:      $                   LDX, FERR, BERR, WORK, RWORK, INFO )
003: *
004: *  -- LAPACK routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     Modified to call CLACN2 in place of CLACON, 10 Feb 03, SJH.
009: *
010: *     .. Scalar Arguments ..
011:       CHARACTER          UPLO
012:       INTEGER            INFO, LDA, LDAF, LDB, LDX, N, NRHS
013: *     ..
014: *     .. Array Arguments ..
015:       REAL               BERR( * ), FERR( * ), RWORK( * )
016:       COMPLEX            A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
017:      $                   WORK( * ), X( LDX, * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  CPORFS improves the computed solution to a system of linear
024: *  equations when the coefficient matrix is Hermitian positive definite,
025: *  and provides error bounds and backward error estimates for the
026: *  solution.
027: *
028: *  Arguments
029: *  =========
030: *
031: *  UPLO    (input) CHARACTER*1
032: *          = 'U':  Upper triangle of A is stored;
033: *          = 'L':  Lower triangle of A is stored.
034: *
035: *  N       (input) INTEGER
036: *          The order of the matrix A.  N >= 0.
037: *
038: *  NRHS    (input) INTEGER
039: *          The number of right hand sides, i.e., the number of columns
040: *          of the matrices B and X.  NRHS >= 0.
041: *
042: *  A       (input) COMPLEX array, dimension (LDA,N)
043: *          The Hermitian matrix A.  If UPLO = 'U', the leading N-by-N
044: *          upper triangular part of A contains the upper triangular part
045: *          of the matrix A, and the strictly lower triangular part of A
046: *          is not referenced.  If UPLO = 'L', the leading N-by-N lower
047: *          triangular part of A contains the lower triangular part of
048: *          the matrix A, and the strictly upper triangular part of A is
049: *          not referenced.
050: *
051: *  LDA     (input) INTEGER
052: *          The leading dimension of the array A.  LDA >= max(1,N).
053: *
054: *  AF      (input) COMPLEX array, dimension (LDAF,N)
055: *          The triangular factor U or L from the Cholesky factorization
056: *          A = U**H*U or A = L*L**H, as computed by CPOTRF.
057: *
058: *  LDAF    (input) INTEGER
059: *          The leading dimension of the array AF.  LDAF >= max(1,N).
060: *
061: *  B       (input) COMPLEX array, dimension (LDB,NRHS)
062: *          The right hand side matrix B.
063: *
064: *  LDB     (input) INTEGER
065: *          The leading dimension of the array B.  LDB >= max(1,N).
066: *
067: *  X       (input/output) COMPLEX array, dimension (LDX,NRHS)
068: *          On entry, the solution matrix X, as computed by CPOTRS.
069: *          On exit, the improved solution matrix X.
070: *
071: *  LDX     (input) INTEGER
072: *          The leading dimension of the array X.  LDX >= max(1,N).
073: *
074: *  FERR    (output) REAL array, dimension (NRHS)
075: *          The estimated forward error bound for each solution vector
076: *          X(j) (the j-th column of the solution matrix X).
077: *          If XTRUE is the true solution corresponding to X(j), FERR(j)
078: *          is an estimated upper bound for the magnitude of the largest
079: *          element in (X(j) - XTRUE) divided by the magnitude of the
080: *          largest element in X(j).  The estimate is as reliable as
081: *          the estimate for RCOND, and is almost always a slight
082: *          overestimate of the true error.
083: *
084: *  BERR    (output) REAL array, dimension (NRHS)
085: *          The componentwise relative backward error of each solution
086: *          vector X(j) (i.e., the smallest relative change in
087: *          any element of A or B that makes X(j) an exact solution).
088: *
089: *  WORK    (workspace) COMPLEX array, dimension (2*N)
090: *
091: *  RWORK   (workspace) REAL array, dimension (N)
092: *
093: *  INFO    (output) INTEGER
094: *          = 0:  successful exit
095: *          < 0:  if INFO = -i, the i-th argument had an illegal value
096: *
097: *  Internal Parameters
098: *  ===================
099: *
100: *  ITMAX is the maximum number of steps of iterative refinement.
101: *
102: *  ====================================================================
103: *
104: *     .. Parameters ..
105:       INTEGER            ITMAX
106:       PARAMETER          ( ITMAX = 5 )
107:       REAL               ZERO
108:       PARAMETER          ( ZERO = 0.0E+0 )
109:       COMPLEX            ONE
110:       PARAMETER          ( ONE = ( 1.0E+0, 0.0E+0 ) )
111:       REAL               TWO
112:       PARAMETER          ( TWO = 2.0E+0 )
113:       REAL               THREE
114:       PARAMETER          ( THREE = 3.0E+0 )
115: *     ..
116: *     .. Local Scalars ..
117:       LOGICAL            UPPER
118:       INTEGER            COUNT, I, J, K, KASE, NZ
119:       REAL               EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
120:       COMPLEX            ZDUM
121: *     ..
122: *     .. Local Arrays ..
123:       INTEGER            ISAVE( 3 )
124: *     ..
125: *     .. External Subroutines ..
126:       EXTERNAL           CAXPY, CCOPY, CHEMV, CLACN2, CPOTRS, XERBLA
127: *     ..
128: *     .. Intrinsic Functions ..
129:       INTRINSIC          ABS, AIMAG, MAX, REAL
130: *     ..
131: *     .. External Functions ..
132:       LOGICAL            LSAME
133:       REAL               SLAMCH
134:       EXTERNAL           LSAME, SLAMCH
135: *     ..
136: *     .. Statement Functions ..
137:       REAL               CABS1
138: *     ..
139: *     .. Statement Function definitions ..
140:       CABS1( ZDUM ) = ABS( REAL( ZDUM ) ) + ABS( AIMAG( ZDUM ) )
141: *     ..
142: *     .. Executable Statements ..
143: *
144: *     Test the input parameters.
145: *
146:       INFO = 0
147:       UPPER = LSAME( UPLO, 'U' )
148:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
149:          INFO = -1
150:       ELSE IF( N.LT.0 ) THEN
151:          INFO = -2
152:       ELSE IF( NRHS.LT.0 ) THEN
153:          INFO = -3
154:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
155:          INFO = -5
156:       ELSE IF( LDAF.LT.MAX( 1, N ) ) THEN
157:          INFO = -7
158:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
159:          INFO = -9
160:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
161:          INFO = -11
162:       END IF
163:       IF( INFO.NE.0 ) THEN
164:          CALL XERBLA( 'CPORFS', -INFO )
165:          RETURN
166:       END IF
167: *
168: *     Quick return if possible
169: *
170:       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
171:          DO 10 J = 1, NRHS
172:             FERR( J ) = ZERO
173:             BERR( J ) = ZERO
174:    10    CONTINUE
175:          RETURN
176:       END IF
177: *
178: *     NZ = maximum number of nonzero elements in each row of A, plus 1
179: *
180:       NZ = N + 1
181:       EPS = SLAMCH( 'Epsilon' )
182:       SAFMIN = SLAMCH( 'Safe minimum' )
183:       SAFE1 = NZ*SAFMIN
184:       SAFE2 = SAFE1 / EPS
185: *
186: *     Do for each right hand side
187: *
188:       DO 140 J = 1, NRHS
189: *
190:          COUNT = 1
191:          LSTRES = THREE
192:    20    CONTINUE
193: *
194: *        Loop until stopping criterion is satisfied.
195: *
196: *        Compute residual R = B - A * X
197: *
198:          CALL CCOPY( N, B( 1, J ), 1, WORK, 1 )
199:          CALL CHEMV( UPLO, N, -ONE, A, LDA, X( 1, J ), 1, ONE, WORK, 1 )
200: *
201: *        Compute componentwise relative backward error from formula
202: *
203: *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
204: *
205: *        where abs(Z) is the componentwise absolute value of the matrix
206: *        or vector Z.  If the i-th component of the denominator is less
207: *        than SAFE2, then SAFE1 is added to the i-th components of the
208: *        numerator and denominator before dividing.
209: *
210:          DO 30 I = 1, N
211:             RWORK( I ) = CABS1( B( I, J ) )
212:    30    CONTINUE
213: *
214: *        Compute abs(A)*abs(X) + abs(B).
215: *
216:          IF( UPPER ) THEN
217:             DO 50 K = 1, N
218:                S = ZERO
219:                XK = CABS1( X( K, J ) )
220:                DO 40 I = 1, K - 1
221:                   RWORK( I ) = RWORK( I ) + CABS1( A( I, K ) )*XK
222:                   S = S + CABS1( A( I, K ) )*CABS1( X( I, J ) )
223:    40          CONTINUE
224:                RWORK( K ) = RWORK( K ) + ABS( REAL( A( K, K ) ) )*XK + S
225:    50       CONTINUE
226:          ELSE
227:             DO 70 K = 1, N
228:                S = ZERO
229:                XK = CABS1( X( K, J ) )
230:                RWORK( K ) = RWORK( K ) + ABS( REAL( A( K, K ) ) )*XK
231:                DO 60 I = K + 1, N
232:                   RWORK( I ) = RWORK( I ) + CABS1( A( I, K ) )*XK
233:                   S = S + CABS1( A( I, K ) )*CABS1( X( I, J ) )
234:    60          CONTINUE
235:                RWORK( K ) = RWORK( K ) + S
236:    70       CONTINUE
237:          END IF
238:          S = ZERO
239:          DO 80 I = 1, N
240:             IF( RWORK( I ).GT.SAFE2 ) THEN
241:                S = MAX( S, CABS1( WORK( I ) ) / RWORK( I ) )
242:             ELSE
243:                S = MAX( S, ( CABS1( WORK( I ) )+SAFE1 ) /
244:      $             ( RWORK( I )+SAFE1 ) )
245:             END IF
246:    80    CONTINUE
247:          BERR( J ) = S
248: *
249: *        Test stopping criterion. Continue iterating if
250: *           1) The residual BERR(J) is larger than machine epsilon, and
251: *           2) BERR(J) decreased by at least a factor of 2 during the
252: *              last iteration, and
253: *           3) At most ITMAX iterations tried.
254: *
255:          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
256:      $       COUNT.LE.ITMAX ) THEN
257: *
258: *           Update solution and try again.
259: *
260:             CALL CPOTRS( UPLO, N, 1, AF, LDAF, WORK, N, INFO )
261:             CALL CAXPY( N, ONE, WORK, 1, X( 1, J ), 1 )
262:             LSTRES = BERR( J )
263:             COUNT = COUNT + 1
264:             GO TO 20
265:          END IF
266: *
267: *        Bound error from formula
268: *
269: *        norm(X - XTRUE) / norm(X) .le. FERR =
270: *        norm( abs(inv(A))*
271: *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
272: *
273: *        where
274: *          norm(Z) is the magnitude of the largest component of Z
275: *          inv(A) is the inverse of A
276: *          abs(Z) is the componentwise absolute value of the matrix or
277: *             vector Z
278: *          NZ is the maximum number of nonzeros in any row of A, plus 1
279: *          EPS is machine epsilon
280: *
281: *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
282: *        is incremented by SAFE1 if the i-th component of
283: *        abs(A)*abs(X) + abs(B) is less than SAFE2.
284: *
285: *        Use CLACN2 to estimate the infinity-norm of the matrix
286: *           inv(A) * diag(W),
287: *        where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
288: *
289:          DO 90 I = 1, N
290:             IF( RWORK( I ).GT.SAFE2 ) THEN
291:                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I )
292:             ELSE
293:                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I ) +
294:      $                      SAFE1
295:             END IF
296:    90    CONTINUE
297: *
298:          KASE = 0
299:   100    CONTINUE
300:          CALL CLACN2( N, WORK( N+1 ), WORK, FERR( J ), KASE, ISAVE )
301:          IF( KASE.NE.0 ) THEN
302:             IF( KASE.EQ.1 ) THEN
303: *
304: *              Multiply by diag(W)*inv(A').
305: *
306:                CALL CPOTRS( UPLO, N, 1, AF, LDAF, WORK, N, INFO )
307:                DO 110 I = 1, N
308:                   WORK( I ) = RWORK( I )*WORK( I )
309:   110          CONTINUE
310:             ELSE IF( KASE.EQ.2 ) THEN
311: *
312: *              Multiply by inv(A)*diag(W).
313: *
314:                DO 120 I = 1, N
315:                   WORK( I ) = RWORK( I )*WORK( I )
316:   120          CONTINUE
317:                CALL CPOTRS( UPLO, N, 1, AF, LDAF, WORK, N, INFO )
318:             END IF
319:             GO TO 100
320:          END IF
321: *
322: *        Normalize error.
323: *
324:          LSTRES = ZERO
325:          DO 130 I = 1, N
326:             LSTRES = MAX( LSTRES, CABS1( X( I, J ) ) )
327:   130    CONTINUE
328:          IF( LSTRES.NE.ZERO )
329:      $      FERR( J ) = FERR( J ) / LSTRES
330: *
331:   140 CONTINUE
332: *
333:       RETURN
334: *
335: *     End of CPORFS
336: *
337:       END
338: