001:       SUBROUTINE ZHPRFS( UPLO, N, NRHS, AP, AFP, IPIV, B, LDB, X, LDX,
002:      $                   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, LDB, LDX, N, NRHS
014: *     ..
015: *     .. Array Arguments ..
016:       INTEGER            IPIV( * )
017:       DOUBLE PRECISION   BERR( * ), FERR( * ), RWORK( * )
018:       COMPLEX*16         AFP( * ), AP( * ), B( LDB, * ), WORK( * ),
019:      $                   X( LDX, * )
020: *     ..
021: *
022: *  Purpose
023: *  =======
024: *
025: *  ZHPRFS improves the computed solution to a system of linear
026: *  equations when the coefficient matrix is Hermitian indefinite
027: *  and packed, and provides error bounds and backward error estimates
028: *  for the solution.
029: *
030: *  Arguments
031: *  =========
032: *
033: *  UPLO    (input) CHARACTER*1
034: *          = 'U':  Upper triangle of A is stored;
035: *          = 'L':  Lower triangle of A is stored.
036: *
037: *  N       (input) INTEGER
038: *          The order of the matrix A.  N >= 0.
039: *
040: *  NRHS    (input) INTEGER
041: *          The number of right hand sides, i.e., the number of columns
042: *          of the matrices B and X.  NRHS >= 0.
043: *
044: *  AP      (input) COMPLEX*16 array, dimension (N*(N+1)/2)
045: *          The upper or lower triangle of the Hermitian matrix A, packed
046: *          columnwise in a linear array.  The j-th column of A is stored
047: *          in the array AP as follows:
048: *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
049: *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
050: *
051: *  AFP     (input) COMPLEX*16 array, dimension (N*(N+1)/2)
052: *          The factored form of the matrix A.  AFP contains the block
053: *          diagonal matrix D and the multipliers used to obtain the
054: *          factor U or L from the factorization A = U*D*U**H or
055: *          A = L*D*L**H as computed by ZHPTRF, stored as a packed
056: *          triangular matrix.
057: *
058: *  IPIV    (input) INTEGER array, dimension (N)
059: *          Details of the interchanges and the block structure of D
060: *          as determined by ZHPTRF.
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 ZHPTRS.
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, IK, J, K, KASE, KK, 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, ZHPMV, ZHPTRS, ZLACN2
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( LDB.LT.MAX( 1, N ) ) THEN
156:          INFO = -8
157:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
158:          INFO = -10
159:       END IF
160:       IF( INFO.NE.0 ) THEN
161:          CALL XERBLA( 'ZHPRFS', -INFO )
162:          RETURN
163:       END IF
164: *
165: *     Quick return if possible
166: *
167:       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
168:          DO 10 J = 1, NRHS
169:             FERR( J ) = ZERO
170:             BERR( J ) = ZERO
171:    10    CONTINUE
172:          RETURN
173:       END IF
174: *
175: *     NZ = maximum number of nonzero elements in each row of A, plus 1
176: *
177:       NZ = N + 1
178:       EPS = DLAMCH( 'Epsilon' )
179:       SAFMIN = DLAMCH( 'Safe minimum' )
180:       SAFE1 = NZ*SAFMIN
181:       SAFE2 = SAFE1 / EPS
182: *
183: *     Do for each right hand side
184: *
185:       DO 140 J = 1, NRHS
186: *
187:          COUNT = 1
188:          LSTRES = THREE
189:    20    CONTINUE
190: *
191: *        Loop until stopping criterion is satisfied.
192: *
193: *        Compute residual R = B - A * X
194: *
195:          CALL ZCOPY( N, B( 1, J ), 1, WORK, 1 )
196:          CALL ZHPMV( UPLO, N, -ONE, AP, X( 1, J ), 1, ONE, WORK, 1 )
197: *
198: *        Compute componentwise relative backward error from formula
199: *
200: *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
201: *
202: *        where abs(Z) is the componentwise absolute value of the matrix
203: *        or vector Z.  If the i-th component of the denominator is less
204: *        than SAFE2, then SAFE1 is added to the i-th components of the
205: *        numerator and denominator before dividing.
206: *
207:          DO 30 I = 1, N
208:             RWORK( I ) = CABS1( B( I, J ) )
209:    30    CONTINUE
210: *
211: *        Compute abs(A)*abs(X) + abs(B).
212: *
213:          KK = 1
214:          IF( UPPER ) THEN
215:             DO 50 K = 1, N
216:                S = ZERO
217:                XK = CABS1( X( K, J ) )
218:                IK = KK
219:                DO 40 I = 1, K - 1
220:                   RWORK( I ) = RWORK( I ) + CABS1( AP( IK ) )*XK
221:                   S = S + CABS1( AP( IK ) )*CABS1( X( I, J ) )
222:                   IK = IK + 1
223:    40          CONTINUE
224:                RWORK( K ) = RWORK( K ) + ABS( DBLE( AP( KK+K-1 ) ) )*
225:      $                      XK + S
226:                KK = KK + K
227:    50       CONTINUE
228:          ELSE
229:             DO 70 K = 1, N
230:                S = ZERO
231:                XK = CABS1( X( K, J ) )
232:                RWORK( K ) = RWORK( K ) + ABS( DBLE( AP( KK ) ) )*XK
233:                IK = KK + 1
234:                DO 60 I = K + 1, N
235:                   RWORK( I ) = RWORK( I ) + CABS1( AP( IK ) )*XK
236:                   S = S + CABS1( AP( IK ) )*CABS1( X( I, J ) )
237:                   IK = IK + 1
238:    60          CONTINUE
239:                RWORK( K ) = RWORK( K ) + S
240:                KK = KK + ( N-K+1 )
241:    70       CONTINUE
242:          END IF
243:          S = ZERO
244:          DO 80 I = 1, N
245:             IF( RWORK( I ).GT.SAFE2 ) THEN
246:                S = MAX( S, CABS1( WORK( I ) ) / RWORK( I ) )
247:             ELSE
248:                S = MAX( S, ( CABS1( WORK( I ) )+SAFE1 ) /
249:      $             ( RWORK( I )+SAFE1 ) )
250:             END IF
251:    80    CONTINUE
252:          BERR( J ) = S
253: *
254: *        Test stopping criterion. Continue iterating if
255: *           1) The residual BERR(J) is larger than machine epsilon, and
256: *           2) BERR(J) decreased by at least a factor of 2 during the
257: *              last iteration, and
258: *           3) At most ITMAX iterations tried.
259: *
260:          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
261:      $       COUNT.LE.ITMAX ) THEN
262: *
263: *           Update solution and try again.
264: *
265:             CALL ZHPTRS( UPLO, N, 1, AFP, IPIV, WORK, N, INFO )
266:             CALL ZAXPY( N, ONE, WORK, 1, X( 1, J ), 1 )
267:             LSTRES = BERR( J )
268:             COUNT = COUNT + 1
269:             GO TO 20
270:          END IF
271: *
272: *        Bound error from formula
273: *
274: *        norm(X - XTRUE) / norm(X) .le. FERR =
275: *        norm( abs(inv(A))*
276: *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
277: *
278: *        where
279: *          norm(Z) is the magnitude of the largest component of Z
280: *          inv(A) is the inverse of A
281: *          abs(Z) is the componentwise absolute value of the matrix or
282: *             vector Z
283: *          NZ is the maximum number of nonzeros in any row of A, plus 1
284: *          EPS is machine epsilon
285: *
286: *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
287: *        is incremented by SAFE1 if the i-th component of
288: *        abs(A)*abs(X) + abs(B) is less than SAFE2.
289: *
290: *        Use ZLACN2 to estimate the infinity-norm of the matrix
291: *           inv(A) * diag(W),
292: *        where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
293: *
294:          DO 90 I = 1, N
295:             IF( RWORK( I ).GT.SAFE2 ) THEN
296:                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I )
297:             ELSE
298:                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I ) +
299:      $                      SAFE1
300:             END IF
301:    90    CONTINUE
302: *
303:          KASE = 0
304:   100    CONTINUE
305:          CALL ZLACN2( N, WORK( N+1 ), WORK, FERR( J ), KASE, ISAVE )
306:          IF( KASE.NE.0 ) THEN
307:             IF( KASE.EQ.1 ) THEN
308: *
309: *              Multiply by diag(W)*inv(A').
310: *
311:                CALL ZHPTRS( UPLO, N, 1, AFP, IPIV, WORK, N, INFO )
312:                DO 110 I = 1, N
313:                   WORK( I ) = RWORK( I )*WORK( I )
314:   110          CONTINUE
315:             ELSE IF( KASE.EQ.2 ) THEN
316: *
317: *              Multiply by inv(A)*diag(W).
318: *
319:                DO 120 I = 1, N
320:                   WORK( I ) = RWORK( I )*WORK( I )
321:   120          CONTINUE
322:                CALL ZHPTRS( UPLO, N, 1, AFP, IPIV, WORK, N, INFO )
323:             END IF
324:             GO TO 100
325:          END IF
326: *
327: *        Normalize error.
328: *
329:          LSTRES = ZERO
330:          DO 130 I = 1, N
331:             LSTRES = MAX( LSTRES, CABS1( X( I, J ) ) )
332:   130    CONTINUE
333:          IF( LSTRES.NE.ZERO )
334:      $      FERR( J ) = FERR( J ) / LSTRES
335: *
336:   140 CONTINUE
337: *
338:       RETURN
339: *
340: *     End of ZHPRFS
341: *
342:       END
343: