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