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