001:       SUBROUTINE ZTBRFS( UPLO, TRANS, DIAG, N, KD, NRHS, AB, LDAB, 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          DIAG, TRANS, UPLO
012:       INTEGER            INFO, KD, LDAB, LDB, LDX, N, NRHS
013: *     ..
014: *     .. Array Arguments ..
015:       DOUBLE PRECISION   BERR( * ), FERR( * ), RWORK( * )
016:       COMPLEX*16         AB( LDAB, * ), B( LDB, * ), WORK( * ),
017:      $                   X( LDX, * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  ZTBRFS provides error bounds and backward error estimates for the
024: *  solution to a system of linear equations with a triangular band
025: *  coefficient matrix.
026: *
027: *  The solution matrix X must be computed by ZTBTRS or some other
028: *  means before entering this routine.  ZTBRFS does not do iterative
029: *  refinement because doing so cannot improve the backward error.
030: *
031: *  Arguments
032: *  =========
033: *
034: *  UPLO    (input) CHARACTER*1
035: *          = 'U':  A is upper triangular;
036: *          = 'L':  A is lower triangular.
037: *
038: *  TRANS   (input) CHARACTER*1
039: *          Specifies the form of the system of equations:
040: *          = 'N':  A * X = B     (No transpose)
041: *          = 'T':  A**T * X = B  (Transpose)
042: *          = 'C':  A**H * X = B  (Conjugate transpose)
043: *
044: *  DIAG    (input) CHARACTER*1
045: *          = 'N':  A is non-unit triangular;
046: *          = 'U':  A is unit triangular.
047: *
048: *  N       (input) INTEGER
049: *          The order of the matrix A.  N >= 0.
050: *
051: *  KD      (input) INTEGER
052: *          The number of superdiagonals or subdiagonals of the
053: *          triangular band matrix A.  KD >= 0.
054: *
055: *  NRHS    (input) INTEGER
056: *          The number of right hand sides, i.e., the number of columns
057: *          of the matrices B and X.  NRHS >= 0.
058: *
059: *  AB      (input) COMPLEX*16 array, dimension (LDAB,N)
060: *          The upper or lower triangular band matrix A, stored in the
061: *          first kd+1 rows of the array. The j-th column of A is stored
062: *          in the j-th column of the array AB as follows:
063: *          if UPLO = 'U', AB(kd+1+i-j,j) = A(i,j) for max(1,j-kd)<=i<=j;
064: *          if UPLO = 'L', AB(1+i-j,j)    = A(i,j) for j<=i<=min(n,j+kd).
065: *          If DIAG = 'U', the diagonal elements of A are not referenced
066: *          and are assumed to be 1.
067: *
068: *  LDAB    (input) INTEGER
069: *          The leading dimension of the array AB.  LDAB >= KD+1.
070: *
071: *  B       (input) COMPLEX*16 array, dimension (LDB,NRHS)
072: *          The right hand side matrix B.
073: *
074: *  LDB     (input) INTEGER
075: *          The leading dimension of the array B.  LDB >= max(1,N).
076: *
077: *  X       (input) COMPLEX*16 array, dimension (LDX,NRHS)
078: *          The solution matrix X.
079: *
080: *  LDX     (input) INTEGER
081: *          The leading dimension of the array X.  LDX >= max(1,N).
082: *
083: *  FERR    (output) DOUBLE PRECISION array, dimension (NRHS)
084: *          The estimated forward error bound for each solution vector
085: *          X(j) (the j-th column of the solution matrix X).
086: *          If XTRUE is the true solution corresponding to X(j), FERR(j)
087: *          is an estimated upper bound for the magnitude of the largest
088: *          element in (X(j) - XTRUE) divided by the magnitude of the
089: *          largest element in X(j).  The estimate is as reliable as
090: *          the estimate for RCOND, and is almost always a slight
091: *          overestimate of the true error.
092: *
093: *  BERR    (output) DOUBLE PRECISION array, dimension (NRHS)
094: *          The componentwise relative backward error of each solution
095: *          vector X(j) (i.e., the smallest relative change in
096: *          any element of A or B that makes X(j) an exact solution).
097: *
098: *  WORK    (workspace) COMPLEX*16 array, dimension (2*N)
099: *
100: *  RWORK   (workspace) DOUBLE PRECISION array, dimension (N)
101: *
102: *  INFO    (output) INTEGER
103: *          = 0:  successful exit
104: *          < 0:  if INFO = -i, the i-th argument had an illegal value
105: *
106: *  =====================================================================
107: *
108: *     .. Parameters ..
109:       DOUBLE PRECISION   ZERO
110:       PARAMETER          ( ZERO = 0.0D+0 )
111:       COMPLEX*16         ONE
112:       PARAMETER          ( ONE = ( 1.0D+0, 0.0D+0 ) )
113: *     ..
114: *     .. Local Scalars ..
115:       LOGICAL            NOTRAN, NOUNIT, UPPER
116:       CHARACTER          TRANSN, TRANST
117:       INTEGER            I, J, K, KASE, NZ
118:       DOUBLE PRECISION   EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
119:       COMPLEX*16         ZDUM
120: *     ..
121: *     .. Local Arrays ..
122:       INTEGER            ISAVE( 3 )
123: *     ..
124: *     .. External Subroutines ..
125:       EXTERNAL           XERBLA, ZAXPY, ZCOPY, ZLACN2, ZTBMV, ZTBSV
126: *     ..
127: *     .. Intrinsic Functions ..
128:       INTRINSIC          ABS, DBLE, DIMAG, MAX, MIN
129: *     ..
130: *     .. External Functions ..
131:       LOGICAL            LSAME
132:       DOUBLE PRECISION   DLAMCH
133:       EXTERNAL           LSAME, DLAMCH
134: *     ..
135: *     .. Statement Functions ..
136:       DOUBLE PRECISION   CABS1
137: *     ..
138: *     .. Statement Function definitions ..
139:       CABS1( ZDUM ) = ABS( DBLE( ZDUM ) ) + ABS( DIMAG( ZDUM ) )
140: *     ..
141: *     .. Executable Statements ..
142: *
143: *     Test the input parameters.
144: *
145:       INFO = 0
146:       UPPER = LSAME( UPLO, 'U' )
147:       NOTRAN = LSAME( TRANS, 'N' )
148:       NOUNIT = LSAME( DIAG, 'N' )
149: *
150:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
151:          INFO = -1
152:       ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
153:      $         LSAME( TRANS, 'C' ) ) THEN
154:          INFO = -2
155:       ELSE IF( .NOT.NOUNIT .AND. .NOT.LSAME( DIAG, 'U' ) ) THEN
156:          INFO = -3
157:       ELSE IF( N.LT.0 ) THEN
158:          INFO = -4
159:       ELSE IF( KD.LT.0 ) THEN
160:          INFO = -5
161:       ELSE IF( NRHS.LT.0 ) THEN
162:          INFO = -6
163:       ELSE IF( LDAB.LT.KD+1 ) THEN
164:          INFO = -8
165:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
166:          INFO = -10
167:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
168:          INFO = -12
169:       END IF
170:       IF( INFO.NE.0 ) THEN
171:          CALL XERBLA( 'ZTBRFS', -INFO )
172:          RETURN
173:       END IF
174: *
175: *     Quick return if possible
176: *
177:       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
178:          DO 10 J = 1, NRHS
179:             FERR( J ) = ZERO
180:             BERR( J ) = ZERO
181:    10    CONTINUE
182:          RETURN
183:       END IF
184: *
185:       IF( NOTRAN ) THEN
186:          TRANSN = 'N'
187:          TRANST = 'C'
188:       ELSE
189:          TRANSN = 'C'
190:          TRANST = 'N'
191:       END IF
192: *
193: *     NZ = maximum number of nonzero elements in each row of A, plus 1
194: *
195:       NZ = KD + 2
196:       EPS = DLAMCH( 'Epsilon' )
197:       SAFMIN = DLAMCH( 'Safe minimum' )
198:       SAFE1 = NZ*SAFMIN
199:       SAFE2 = SAFE1 / EPS
200: *
201: *     Do for each right hand side
202: *
203:       DO 250 J = 1, NRHS
204: *
205: *        Compute residual R = B - op(A) * X,
206: *        where op(A) = A, A**T, or A**H, depending on TRANS.
207: *
208:          CALL ZCOPY( N, X( 1, J ), 1, WORK, 1 )
209:          CALL ZTBMV( UPLO, TRANS, DIAG, N, KD, AB, LDAB, WORK, 1 )
210:          CALL ZAXPY( N, -ONE, B( 1, J ), 1, WORK, 1 )
211: *
212: *        Compute componentwise relative backward error from formula
213: *
214: *        max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) )
215: *
216: *        where abs(Z) is the componentwise absolute value of the matrix
217: *        or vector Z.  If the i-th component of the denominator is less
218: *        than SAFE2, then SAFE1 is added to the i-th components of the
219: *        numerator and denominator before dividing.
220: *
221:          DO 20 I = 1, N
222:             RWORK( I ) = CABS1( B( I, J ) )
223:    20    CONTINUE
224: *
225:          IF( NOTRAN ) THEN
226: *
227: *           Compute abs(A)*abs(X) + abs(B).
228: *
229:             IF( UPPER ) THEN
230:                IF( NOUNIT ) THEN
231:                   DO 40 K = 1, N
232:                      XK = CABS1( X( K, J ) )
233:                      DO 30 I = MAX( 1, K-KD ), K
234:                         RWORK( I ) = RWORK( I ) +
235:      $                               CABS1( AB( KD+1+I-K, K ) )*XK
236:    30                CONTINUE
237:    40             CONTINUE
238:                ELSE
239:                   DO 60 K = 1, N
240:                      XK = CABS1( X( K, J ) )
241:                      DO 50 I = MAX( 1, K-KD ), K - 1
242:                         RWORK( I ) = RWORK( I ) +
243:      $                               CABS1( AB( KD+1+I-K, K ) )*XK
244:    50                CONTINUE
245:                      RWORK( K ) = RWORK( K ) + XK
246:    60             CONTINUE
247:                END IF
248:             ELSE
249:                IF( NOUNIT ) THEN
250:                   DO 80 K = 1, N
251:                      XK = CABS1( X( K, J ) )
252:                      DO 70 I = K, MIN( N, K+KD )
253:                         RWORK( I ) = RWORK( I ) +
254:      $                               CABS1( AB( 1+I-K, K ) )*XK
255:    70                CONTINUE
256:    80             CONTINUE
257:                ELSE
258:                   DO 100 K = 1, N
259:                      XK = CABS1( X( K, J ) )
260:                      DO 90 I = K + 1, MIN( N, K+KD )
261:                         RWORK( I ) = RWORK( I ) +
262:      $                               CABS1( AB( 1+I-K, K ) )*XK
263:    90                CONTINUE
264:                      RWORK( K ) = RWORK( K ) + XK
265:   100             CONTINUE
266:                END IF
267:             END IF
268:          ELSE
269: *
270: *           Compute abs(A**H)*abs(X) + abs(B).
271: *
272:             IF( UPPER ) THEN
273:                IF( NOUNIT ) THEN
274:                   DO 120 K = 1, N
275:                      S = ZERO
276:                      DO 110 I = MAX( 1, K-KD ), K
277:                         S = S + CABS1( AB( KD+1+I-K, K ) )*
278:      $                      CABS1( X( I, J ) )
279:   110                CONTINUE
280:                      RWORK( K ) = RWORK( K ) + S
281:   120             CONTINUE
282:                ELSE
283:                   DO 140 K = 1, N
284:                      S = CABS1( X( K, J ) )
285:                      DO 130 I = MAX( 1, K-KD ), K - 1
286:                         S = S + CABS1( AB( KD+1+I-K, K ) )*
287:      $                      CABS1( X( I, J ) )
288:   130                CONTINUE
289:                      RWORK( K ) = RWORK( K ) + S
290:   140             CONTINUE
291:                END IF
292:             ELSE
293:                IF( NOUNIT ) THEN
294:                   DO 160 K = 1, N
295:                      S = ZERO
296:                      DO 150 I = K, MIN( N, K+KD )
297:                         S = S + CABS1( AB( 1+I-K, K ) )*
298:      $                      CABS1( X( I, J ) )
299:   150                CONTINUE
300:                      RWORK( K ) = RWORK( K ) + S
301:   160             CONTINUE
302:                ELSE
303:                   DO 180 K = 1, N
304:                      S = CABS1( X( K, J ) )
305:                      DO 170 I = K + 1, MIN( N, K+KD )
306:                         S = S + CABS1( AB( 1+I-K, K ) )*
307:      $                      CABS1( X( I, J ) )
308:   170                CONTINUE
309:                      RWORK( K ) = RWORK( K ) + S
310:   180             CONTINUE
311:                END IF
312:             END IF
313:          END IF
314:          S = ZERO
315:          DO 190 I = 1, N
316:             IF( RWORK( I ).GT.SAFE2 ) THEN
317:                S = MAX( S, CABS1( WORK( I ) ) / RWORK( I ) )
318:             ELSE
319:                S = MAX( S, ( CABS1( WORK( I ) )+SAFE1 ) /
320:      $             ( RWORK( I )+SAFE1 ) )
321:             END IF
322:   190    CONTINUE
323:          BERR( J ) = S
324: *
325: *        Bound error from formula
326: *
327: *        norm(X - XTRUE) / norm(X) .le. FERR =
328: *        norm( abs(inv(op(A)))*
329: *           ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X)
330: *
331: *        where
332: *          norm(Z) is the magnitude of the largest component of Z
333: *          inv(op(A)) is the inverse of op(A)
334: *          abs(Z) is the componentwise absolute value of the matrix or
335: *             vector Z
336: *          NZ is the maximum number of nonzeros in any row of A, plus 1
337: *          EPS is machine epsilon
338: *
339: *        The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B))
340: *        is incremented by SAFE1 if the i-th component of
341: *        abs(op(A))*abs(X) + abs(B) is less than SAFE2.
342: *
343: *        Use ZLACN2 to estimate the infinity-norm of the matrix
344: *           inv(op(A)) * diag(W),
345: *        where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) )))
346: *
347:          DO 200 I = 1, N
348:             IF( RWORK( I ).GT.SAFE2 ) THEN
349:                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I )
350:             ELSE
351:                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I ) +
352:      $                      SAFE1
353:             END IF
354:   200    CONTINUE
355: *
356:          KASE = 0
357:   210    CONTINUE
358:          CALL ZLACN2( N, WORK( N+1 ), WORK, FERR( J ), KASE, ISAVE )
359:          IF( KASE.NE.0 ) THEN
360:             IF( KASE.EQ.1 ) THEN
361: *
362: *              Multiply by diag(W)*inv(op(A)**H).
363: *
364:                CALL ZTBSV( UPLO, TRANST, DIAG, N, KD, AB, LDAB, WORK,
365:      $                     1 )
366:                DO 220 I = 1, N
367:                   WORK( I ) = RWORK( I )*WORK( I )
368:   220          CONTINUE
369:             ELSE
370: *
371: *              Multiply by inv(op(A))*diag(W).
372: *
373:                DO 230 I = 1, N
374:                   WORK( I ) = RWORK( I )*WORK( I )
375:   230          CONTINUE
376:                CALL ZTBSV( UPLO, TRANSN, DIAG, N, KD, AB, LDAB, WORK,
377:      $                     1 )
378:             END IF
379:             GO TO 210
380:          END IF
381: *
382: *        Normalize error.
383: *
384:          LSTRES = ZERO
385:          DO 240 I = 1, N
386:             LSTRES = MAX( LSTRES, CABS1( X( I, J ) ) )
387:   240    CONTINUE
388:          IF( LSTRES.NE.ZERO )
389:      $      FERR( J ) = FERR( J ) / LSTRES
390: *
391:   250 CONTINUE
392: *
393:       RETURN
394: *
395: *     End of ZTBRFS
396: *
397:       END
398: