001:       SUBROUTINE SPTRFS( N, NRHS, D, E, DF, EF, B, LDB, X, LDX, FERR,
002:      $                   BERR, WORK, 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: *     .. Scalar Arguments ..
010:       INTEGER            INFO, LDB, LDX, N, NRHS
011: *     ..
012: *     .. Array Arguments ..
013:       REAL               B( LDB, * ), BERR( * ), D( * ), DF( * ),
014:      $                   E( * ), EF( * ), FERR( * ), WORK( * ),
015:      $                   X( LDX, * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  SPTRFS improves the computed solution to a system of linear
022: *  equations when the coefficient matrix is symmetric positive definite
023: *  and tridiagonal, and provides error bounds and backward error
024: *  estimates for the solution.
025: *
026: *  Arguments
027: *  =========
028: *
029: *  N       (input) INTEGER
030: *          The order of the matrix A.  N >= 0.
031: *
032: *  NRHS    (input) INTEGER
033: *          The number of right hand sides, i.e., the number of columns
034: *          of the matrix B.  NRHS >= 0.
035: *
036: *  D       (input) REAL array, dimension (N)
037: *          The n diagonal elements of the tridiagonal matrix A.
038: *
039: *  E       (input) REAL array, dimension (N-1)
040: *          The (n-1) subdiagonal elements of the tridiagonal matrix A.
041: *
042: *  DF      (input) REAL array, dimension (N)
043: *          The n diagonal elements of the diagonal matrix D from the
044: *          factorization computed by SPTTRF.
045: *
046: *  EF      (input) REAL array, dimension (N-1)
047: *          The (n-1) subdiagonal elements of the unit bidiagonal factor
048: *          L from the factorization computed by SPTTRF.
049: *
050: *  B       (input) REAL array, dimension (LDB,NRHS)
051: *          The right hand side matrix B.
052: *
053: *  LDB     (input) INTEGER
054: *          The leading dimension of the array B.  LDB >= max(1,N).
055: *
056: *  X       (input/output) REAL array, dimension (LDX,NRHS)
057: *          On entry, the solution matrix X, as computed by SPTTRS.
058: *          On exit, the improved solution matrix X.
059: *
060: *  LDX     (input) INTEGER
061: *          The leading dimension of the array X.  LDX >= max(1,N).
062: *
063: *  FERR    (output) REAL array, dimension (NRHS)
064: *          The forward error bound for each solution vector
065: *          X(j) (the j-th column of the solution matrix X).
066: *          If XTRUE is the true solution corresponding to X(j), FERR(j)
067: *          is an estimated upper bound for the magnitude of the largest
068: *          element in (X(j) - XTRUE) divided by the magnitude of the
069: *          largest element in X(j).
070: *
071: *  BERR    (output) REAL array, dimension (NRHS)
072: *          The componentwise relative backward error of each solution
073: *          vector X(j) (i.e., the smallest relative change in
074: *          any element of A or B that makes X(j) an exact solution).
075: *
076: *  WORK    (workspace) REAL array, dimension (2*N)
077: *
078: *  INFO    (output) INTEGER
079: *          = 0:  successful exit
080: *          < 0:  if INFO = -i, the i-th argument had an illegal value
081: *
082: *  Internal Parameters
083: *  ===================
084: *
085: *  ITMAX is the maximum number of steps of iterative refinement.
086: *
087: *  =====================================================================
088: *
089: *     .. Parameters ..
090:       INTEGER            ITMAX
091:       PARAMETER          ( ITMAX = 5 )
092:       REAL               ZERO
093:       PARAMETER          ( ZERO = 0.0E+0 )
094:       REAL               ONE
095:       PARAMETER          ( ONE = 1.0E+0 )
096:       REAL               TWO
097:       PARAMETER          ( TWO = 2.0E+0 )
098:       REAL               THREE
099:       PARAMETER          ( THREE = 3.0E+0 )
100: *     ..
101: *     .. Local Scalars ..
102:       INTEGER            COUNT, I, IX, J, NZ
103:       REAL               BI, CX, DX, EPS, EX, LSTRES, S, SAFE1, SAFE2,
104:      $                   SAFMIN
105: *     ..
106: *     .. External Subroutines ..
107:       EXTERNAL           SAXPY, SPTTRS, XERBLA
108: *     ..
109: *     .. Intrinsic Functions ..
110:       INTRINSIC          ABS, MAX
111: *     ..
112: *     .. External Functions ..
113:       INTEGER            ISAMAX
114:       REAL               SLAMCH
115:       EXTERNAL           ISAMAX, SLAMCH
116: *     ..
117: *     .. Executable Statements ..
118: *
119: *     Test the input parameters.
120: *
121:       INFO = 0
122:       IF( N.LT.0 ) THEN
123:          INFO = -1
124:       ELSE IF( NRHS.LT.0 ) THEN
125:          INFO = -2
126:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
127:          INFO = -8
128:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
129:          INFO = -10
130:       END IF
131:       IF( INFO.NE.0 ) THEN
132:          CALL XERBLA( 'SPTRFS', -INFO )
133:          RETURN
134:       END IF
135: *
136: *     Quick return if possible
137: *
138:       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
139:          DO 10 J = 1, NRHS
140:             FERR( J ) = ZERO
141:             BERR( J ) = ZERO
142:    10    CONTINUE
143:          RETURN
144:       END IF
145: *
146: *     NZ = maximum number of nonzero elements in each row of A, plus 1
147: *
148:       NZ = 4
149:       EPS = SLAMCH( 'Epsilon' )
150:       SAFMIN = SLAMCH( 'Safe minimum' )
151:       SAFE1 = NZ*SAFMIN
152:       SAFE2 = SAFE1 / EPS
153: *
154: *     Do for each right hand side
155: *
156:       DO 90 J = 1, NRHS
157: *
158:          COUNT = 1
159:          LSTRES = THREE
160:    20    CONTINUE
161: *
162: *        Loop until stopping criterion is satisfied.
163: *
164: *        Compute residual R = B - A * X.  Also compute
165: *        abs(A)*abs(x) + abs(b) for use in the backward error bound.
166: *
167:          IF( N.EQ.1 ) THEN
168:             BI = B( 1, J )
169:             DX = D( 1 )*X( 1, J )
170:             WORK( N+1 ) = BI - DX
171:             WORK( 1 ) = ABS( BI ) + ABS( DX )
172:          ELSE
173:             BI = B( 1, J )
174:             DX = D( 1 )*X( 1, J )
175:             EX = E( 1 )*X( 2, J )
176:             WORK( N+1 ) = BI - DX - EX
177:             WORK( 1 ) = ABS( BI ) + ABS( DX ) + ABS( EX )
178:             DO 30 I = 2, N - 1
179:                BI = B( I, J )
180:                CX = E( I-1 )*X( I-1, J )
181:                DX = D( I )*X( I, J )
182:                EX = E( I )*X( I+1, J )
183:                WORK( N+I ) = BI - CX - DX - EX
184:                WORK( I ) = ABS( BI ) + ABS( CX ) + ABS( DX ) + ABS( EX )
185:    30       CONTINUE
186:             BI = B( N, J )
187:             CX = E( N-1 )*X( N-1, J )
188:             DX = D( N )*X( N, J )
189:             WORK( N+N ) = BI - CX - DX
190:             WORK( N ) = ABS( BI ) + ABS( CX ) + ABS( DX )
191:          END IF
192: *
193: *        Compute componentwise relative backward error from formula
194: *
195: *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
196: *
197: *        where abs(Z) is the componentwise absolute value of the matrix
198: *        or vector Z.  If the i-th component of the denominator is less
199: *        than SAFE2, then SAFE1 is added to the i-th components of the
200: *        numerator and denominator before dividing.
201: *
202:          S = ZERO
203:          DO 40 I = 1, N
204:             IF( WORK( I ).GT.SAFE2 ) THEN
205:                S = MAX( S, ABS( WORK( N+I ) ) / WORK( I ) )
206:             ELSE
207:                S = MAX( S, ( ABS( WORK( N+I ) )+SAFE1 ) /
208:      $             ( WORK( I )+SAFE1 ) )
209:             END IF
210:    40    CONTINUE
211:          BERR( J ) = S
212: *
213: *        Test stopping criterion. Continue iterating if
214: *           1) The residual BERR(J) is larger than machine epsilon, and
215: *           2) BERR(J) decreased by at least a factor of 2 during the
216: *              last iteration, and
217: *           3) At most ITMAX iterations tried.
218: *
219:          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
220:      $       COUNT.LE.ITMAX ) THEN
221: *
222: *           Update solution and try again.
223: *
224:             CALL SPTTRS( N, 1, DF, EF, WORK( N+1 ), N, INFO )
225:             CALL SAXPY( N, ONE, WORK( N+1 ), 1, X( 1, J ), 1 )
226:             LSTRES = BERR( J )
227:             COUNT = COUNT + 1
228:             GO TO 20
229:          END IF
230: *
231: *        Bound error from formula
232: *
233: *        norm(X - XTRUE) / norm(X) .le. FERR =
234: *        norm( abs(inv(A))*
235: *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
236: *
237: *        where
238: *          norm(Z) is the magnitude of the largest component of Z
239: *          inv(A) is the inverse of A
240: *          abs(Z) is the componentwise absolute value of the matrix or
241: *             vector Z
242: *          NZ is the maximum number of nonzeros in any row of A, plus 1
243: *          EPS is machine epsilon
244: *
245: *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
246: *        is incremented by SAFE1 if the i-th component of
247: *        abs(A)*abs(X) + abs(B) is less than SAFE2.
248: *
249:          DO 50 I = 1, N
250:             IF( WORK( I ).GT.SAFE2 ) THEN
251:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I )
252:             ELSE
253:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I ) + SAFE1
254:             END IF
255:    50    CONTINUE
256:          IX = ISAMAX( N, WORK, 1 )
257:          FERR( J ) = WORK( IX )
258: *
259: *        Estimate the norm of inv(A).
260: *
261: *        Solve M(A) * x = e, where M(A) = (m(i,j)) is given by
262: *
263: *           m(i,j) =  abs(A(i,j)), i = j,
264: *           m(i,j) = -abs(A(i,j)), i .ne. j,
265: *
266: *        and e = [ 1, 1, ..., 1 ]'.  Note M(A) = M(L)*D*M(L)'.
267: *
268: *        Solve M(L) * x = e.
269: *
270:          WORK( 1 ) = ONE
271:          DO 60 I = 2, N
272:             WORK( I ) = ONE + WORK( I-1 )*ABS( EF( I-1 ) )
273:    60    CONTINUE
274: *
275: *        Solve D * M(L)' * x = b.
276: *
277:          WORK( N ) = WORK( N ) / DF( N )
278:          DO 70 I = N - 1, 1, -1
279:             WORK( I ) = WORK( I ) / DF( I ) + WORK( I+1 )*ABS( EF( I ) )
280:    70    CONTINUE
281: *
282: *        Compute norm(inv(A)) = max(x(i)), 1<=i<=n.
283: *
284:          IX = ISAMAX( N, WORK, 1 )
285:          FERR( J ) = FERR( J )*ABS( WORK( IX ) )
286: *
287: *        Normalize error.
288: *
289:          LSTRES = ZERO
290:          DO 80 I = 1, N
291:             LSTRES = MAX( LSTRES, ABS( X( I, J ) ) )
292:    80    CONTINUE
293:          IF( LSTRES.NE.ZERO )
294:      $      FERR( J ) = FERR( J ) / LSTRES
295: *
296:    90 CONTINUE
297: *
298:       RETURN
299: *
300: *     End of SPTRFS
301: *
302:       END
303: