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