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