001:       SUBROUTINE SPBRFS( UPLO, N, KD, NRHS, AB, LDAB, AFB, LDAFB, B,
002:      $                   LDB, X, LDX, FERR, BERR, WORK, IWORK, 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 SLACN2 in place of SLACON, 7 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:       INTEGER            IWORK( * )
016:       REAL               AB( LDAB, * ), AFB( LDAFB, * ), B( LDB, * ),
017:      $                   BERR( * ), FERR( * ), WORK( * ), X( LDX, * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  SPBRFS improves the computed solution to a system of linear
024: *  equations when the coefficient matrix is symmetric 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) REAL array, dimension (LDAB,N)
047: *          The upper or lower triangle of the symmetric 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) REAL array, dimension (LDAFB,N)
057: *          The triangular factor U or L from the Cholesky factorization
058: *          A = U**T*U or A = L*L**T of the band matrix A as computed by
059: *          SPBTRF, 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) REAL 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) REAL array, dimension (LDX,NRHS)
071: *          On entry, the solution matrix X, as computed by SPBTRS.
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) REAL 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) REAL 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) REAL array, dimension (3*N)
093: *
094: *  IWORK   (workspace) INTEGER 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:       REAL               ZERO
111:       PARAMETER          ( ZERO = 0.0E+0 )
112:       REAL               ONE
113:       PARAMETER          ( ONE = 1.0E+0 )
114:       REAL               TWO
115:       PARAMETER          ( TWO = 2.0E+0 )
116:       REAL               THREE
117:       PARAMETER          ( THREE = 3.0E+0 )
118: *     ..
119: *     .. Local Scalars ..
120:       LOGICAL            UPPER
121:       INTEGER            COUNT, I, J, K, KASE, L, NZ
122:       REAL               EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
123: *     ..
124: *     .. Local Arrays ..
125:       INTEGER            ISAVE( 3 )
126: *     ..
127: *     .. External Subroutines ..
128:       EXTERNAL           SAXPY, SCOPY, SLACN2, SPBTRS, SSBMV, XERBLA
129: *     ..
130: *     .. Intrinsic Functions ..
131:       INTRINSIC          ABS, MAX, MIN
132: *     ..
133: *     .. External Functions ..
134:       LOGICAL            LSAME
135:       REAL               SLAMCH
136:       EXTERNAL           LSAME, SLAMCH
137: *     ..
138: *     .. Executable Statements ..
139: *
140: *     Test the input parameters.
141: *
142:       INFO = 0
143:       UPPER = LSAME( UPLO, 'U' )
144:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
145:          INFO = -1
146:       ELSE IF( N.LT.0 ) THEN
147:          INFO = -2
148:       ELSE IF( KD.LT.0 ) THEN
149:          INFO = -3
150:       ELSE IF( NRHS.LT.0 ) THEN
151:          INFO = -4
152:       ELSE IF( LDAB.LT.KD+1 ) THEN
153:          INFO = -6
154:       ELSE IF( LDAFB.LT.KD+1 ) THEN
155:          INFO = -8
156:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
157:          INFO = -10
158:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
159:          INFO = -12
160:       END IF
161:       IF( INFO.NE.0 ) THEN
162:          CALL XERBLA( 'SPBRFS', -INFO )
163:          RETURN
164:       END IF
165: *
166: *     Quick return if possible
167: *
168:       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
169:          DO 10 J = 1, NRHS
170:             FERR( J ) = ZERO
171:             BERR( J ) = ZERO
172:    10    CONTINUE
173:          RETURN
174:       END IF
175: *
176: *     NZ = maximum number of nonzero elements in each row of A, plus 1
177: *
178:       NZ = MIN( N+1, 2*KD+2 )
179:       EPS = SLAMCH( 'Epsilon' )
180:       SAFMIN = SLAMCH( 'Safe minimum' )
181:       SAFE1 = NZ*SAFMIN
182:       SAFE2 = SAFE1 / EPS
183: *
184: *     Do for each right hand side
185: *
186:       DO 140 J = 1, NRHS
187: *
188:          COUNT = 1
189:          LSTRES = THREE
190:    20    CONTINUE
191: *
192: *        Loop until stopping criterion is satisfied.
193: *
194: *        Compute residual R = B - A * X
195: *
196:          CALL SCOPY( N, B( 1, J ), 1, WORK( N+1 ), 1 )
197:          CALL SSBMV( UPLO, N, KD, -ONE, AB, LDAB, X( 1, J ), 1, ONE,
198:      $               WORK( N+1 ), 1 )
199: *
200: *        Compute componentwise relative backward error from formula
201: *
202: *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
203: *
204: *        where abs(Z) is the componentwise absolute value of the matrix
205: *        or vector Z.  If the i-th component of the denominator is less
206: *        than SAFE2, then SAFE1 is added to the i-th components of the
207: *        numerator and denominator before dividing.
208: *
209:          DO 30 I = 1, N
210:             WORK( I ) = ABS( B( I, J ) )
211:    30    CONTINUE
212: *
213: *        Compute abs(A)*abs(X) + abs(B).
214: *
215:          IF( UPPER ) THEN
216:             DO 50 K = 1, N
217:                S = ZERO
218:                XK = ABS( X( K, J ) )
219:                L = KD + 1 - K
220:                DO 40 I = MAX( 1, K-KD ), K - 1
221:                   WORK( I ) = WORK( I ) + ABS( AB( L+I, K ) )*XK
222:                   S = S + ABS( AB( L+I, K ) )*ABS( X( I, J ) )
223:    40          CONTINUE
224:                WORK( K ) = WORK( K ) + ABS( AB( KD+1, K ) )*XK + S
225:    50       CONTINUE
226:          ELSE
227:             DO 70 K = 1, N
228:                S = ZERO
229:                XK = ABS( X( K, J ) )
230:                WORK( K ) = WORK( K ) + ABS( AB( 1, K ) )*XK
231:                L = 1 - K
232:                DO 60 I = K + 1, MIN( N, K+KD )
233:                   WORK( I ) = WORK( I ) + ABS( AB( L+I, K ) )*XK
234:                   S = S + ABS( AB( L+I, K ) )*ABS( X( I, J ) )
235:    60          CONTINUE
236:                WORK( K ) = WORK( K ) + S
237:    70       CONTINUE
238:          END IF
239:          S = ZERO
240:          DO 80 I = 1, N
241:             IF( WORK( I ).GT.SAFE2 ) THEN
242:                S = MAX( S, ABS( WORK( N+I ) ) / WORK( I ) )
243:             ELSE
244:                S = MAX( S, ( ABS( WORK( N+I ) )+SAFE1 ) /
245:      $             ( WORK( I )+SAFE1 ) )
246:             END IF
247:    80    CONTINUE
248:          BERR( J ) = S
249: *
250: *        Test stopping criterion. Continue iterating if
251: *           1) The residual BERR(J) is larger than machine epsilon, and
252: *           2) BERR(J) decreased by at least a factor of 2 during the
253: *              last iteration, and
254: *           3) At most ITMAX iterations tried.
255: *
256:          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
257:      $       COUNT.LE.ITMAX ) THEN
258: *
259: *           Update solution and try again.
260: *
261:             CALL SPBTRS( UPLO, N, KD, 1, AFB, LDAFB, WORK( N+1 ), N,
262:      $                   INFO )
263:             CALL SAXPY( N, ONE, WORK( N+1 ), 1, X( 1, J ), 1 )
264:             LSTRES = BERR( J )
265:             COUNT = COUNT + 1
266:             GO TO 20
267:          END IF
268: *
269: *        Bound error from formula
270: *
271: *        norm(X - XTRUE) / norm(X) .le. FERR =
272: *        norm( abs(inv(A))*
273: *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
274: *
275: *        where
276: *          norm(Z) is the magnitude of the largest component of Z
277: *          inv(A) is the inverse of A
278: *          abs(Z) is the componentwise absolute value of the matrix or
279: *             vector Z
280: *          NZ is the maximum number of nonzeros in any row of A, plus 1
281: *          EPS is machine epsilon
282: *
283: *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
284: *        is incremented by SAFE1 if the i-th component of
285: *        abs(A)*abs(X) + abs(B) is less than SAFE2.
286: *
287: *        Use SLACN2 to estimate the infinity-norm of the matrix
288: *           inv(A) * diag(W),
289: *        where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
290: *
291:          DO 90 I = 1, N
292:             IF( WORK( I ).GT.SAFE2 ) THEN
293:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I )
294:             ELSE
295:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I ) + SAFE1
296:             END IF
297:    90    CONTINUE
298: *
299:          KASE = 0
300:   100    CONTINUE
301:          CALL SLACN2( N, WORK( 2*N+1 ), WORK( N+1 ), IWORK, FERR( J ),
302:      $                KASE, ISAVE )
303:          IF( KASE.NE.0 ) THEN
304:             IF( KASE.EQ.1 ) THEN
305: *
306: *              Multiply by diag(W)*inv(A').
307: *
308:                CALL SPBTRS( UPLO, N, KD, 1, AFB, LDAFB, WORK( N+1 ), N,
309:      $                      INFO )
310:                DO 110 I = 1, N
311:                   WORK( N+I ) = WORK( N+I )*WORK( I )
312:   110          CONTINUE
313:             ELSE IF( KASE.EQ.2 ) THEN
314: *
315: *              Multiply by inv(A)*diag(W).
316: *
317:                DO 120 I = 1, N
318:                   WORK( N+I ) = WORK( N+I )*WORK( I )
319:   120          CONTINUE
320:                CALL SPBTRS( UPLO, N, KD, 1, AFB, LDAFB, WORK( N+1 ), N,
321:      $                      INFO )
322:             END IF
323:             GO TO 100
324:          END IF
325: *
326: *        Normalize error.
327: *
328:          LSTRES = ZERO
329:          DO 130 I = 1, N
330:             LSTRES = MAX( LSTRES, ABS( X( I, J ) ) )
331:   130    CONTINUE
332:          IF( LSTRES.NE.ZERO )
333:      $      FERR( J ) = FERR( J ) / LSTRES
334: *
335:   140 CONTINUE
336: *
337:       RETURN
338: *
339: *     End of SPBRFS
340: *
341:       END
342: