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