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