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