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