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