1*> \brief \b CGTRFS
2*
3*  =========== DOCUMENTATION ===========
4*
5* Online html documentation available at
6*            http://www.netlib.org/lapack/explore-html/
7*
8*> \htmlonly
9*> Download CGTRFS + dependencies
10*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgtrfs.f">
11*> [TGZ]</a>
12*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgtrfs.f">
13*> [ZIP]</a>
14*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgtrfs.f">
15*> [TXT]</a>
16*> \endhtmlonly
17*
18*  Definition:
19*  ===========
20*
21*       SUBROUTINE CGTRFS( TRANS, N, NRHS, DL, D, DU, DLF, DF, DUF, DU2,
22*                          IPIV, B, LDB, X, LDX, FERR, BERR, WORK, RWORK,
23*                          INFO )
24*
25*       .. Scalar Arguments ..
26*       CHARACTER          TRANS
27*       INTEGER            INFO, LDB, LDX, N, NRHS
28*       ..
29*       .. Array Arguments ..
30*       INTEGER            IPIV( * )
31*       REAL               BERR( * ), FERR( * ), RWORK( * )
32*       COMPLEX            B( LDB, * ), D( * ), DF( * ), DL( * ),
33*      $                   DLF( * ), DU( * ), DU2( * ), DUF( * ),
34*      $                   WORK( * ), X( LDX, * )
35*       ..
36*
37*
38*> \par Purpose:
39*  =============
40*>
41*> \verbatim
42*>
43*> CGTRFS improves the computed solution to a system of linear
44*> equations when the coefficient matrix is tridiagonal, and provides
45*> error bounds and backward error estimates for the solution.
46*> \endverbatim
47*
48*  Arguments:
49*  ==========
50*
51*> \param[in] TRANS
52*> \verbatim
53*>          TRANS is CHARACTER*1
54*>          Specifies the form of the system of equations:
55*>          = 'N':  A * X = B     (No transpose)
56*>          = 'T':  A**T * X = B  (Transpose)
57*>          = 'C':  A**H * X = B  (Conjugate transpose)
58*> \endverbatim
59*>
60*> \param[in] N
61*> \verbatim
62*>          N is INTEGER
63*>          The order of the matrix A.  N >= 0.
64*> \endverbatim
65*>
66*> \param[in] NRHS
67*> \verbatim
68*>          NRHS is INTEGER
69*>          The number of right hand sides, i.e., the number of columns
70*>          of the matrix B.  NRHS >= 0.
71*> \endverbatim
72*>
73*> \param[in] DL
74*> \verbatim
75*>          DL is COMPLEX array, dimension (N-1)
76*>          The (n-1) subdiagonal elements of A.
77*> \endverbatim
78*>
79*> \param[in] D
80*> \verbatim
81*>          D is COMPLEX array, dimension (N)
82*>          The diagonal elements of A.
83*> \endverbatim
84*>
85*> \param[in] DU
86*> \verbatim
87*>          DU is COMPLEX array, dimension (N-1)
88*>          The (n-1) superdiagonal elements of A.
89*> \endverbatim
90*>
91*> \param[in] DLF
92*> \verbatim
93*>          DLF is COMPLEX array, dimension (N-1)
94*>          The (n-1) multipliers that define the matrix L from the
95*>          LU factorization of A as computed by CGTTRF.
96*> \endverbatim
97*>
98*> \param[in] DF
99*> \verbatim
100*>          DF is COMPLEX array, dimension (N)
101*>          The n diagonal elements of the upper triangular matrix U from
102*>          the LU factorization of A.
103*> \endverbatim
104*>
105*> \param[in] DUF
106*> \verbatim
107*>          DUF is COMPLEX array, dimension (N-1)
108*>          The (n-1) elements of the first superdiagonal of U.
109*> \endverbatim
110*>
111*> \param[in] DU2
112*> \verbatim
113*>          DU2 is COMPLEX array, dimension (N-2)
114*>          The (n-2) elements of the second superdiagonal of U.
115*> \endverbatim
116*>
117*> \param[in] IPIV
118*> \verbatim
119*>          IPIV is INTEGER array, dimension (N)
120*>          The pivot indices; for 1 <= i <= n, row i of the matrix was
121*>          interchanged with row IPIV(i).  IPIV(i) will always be either
122*>          i or i+1; IPIV(i) = i indicates a row interchange was not
123*>          required.
124*> \endverbatim
125*>
126*> \param[in] B
127*> \verbatim
128*>          B is COMPLEX array, dimension (LDB,NRHS)
129*>          The right hand side matrix B.
130*> \endverbatim
131*>
132*> \param[in] LDB
133*> \verbatim
134*>          LDB is INTEGER
135*>          The leading dimension of the array B.  LDB >= max(1,N).
136*> \endverbatim
137*>
138*> \param[in,out] X
139*> \verbatim
140*>          X is COMPLEX array, dimension (LDX,NRHS)
141*>          On entry, the solution matrix X, as computed by CGTTRS.
142*>          On exit, the improved solution matrix X.
143*> \endverbatim
144*>
145*> \param[in] LDX
146*> \verbatim
147*>          LDX is INTEGER
148*>          The leading dimension of the array X.  LDX >= max(1,N).
149*> \endverbatim
150*>
151*> \param[out] FERR
152*> \verbatim
153*>          FERR is REAL array, dimension (NRHS)
154*>          The estimated forward error bound for each solution vector
155*>          X(j) (the j-th column of the solution matrix X).
156*>          If XTRUE is the true solution corresponding to X(j), FERR(j)
157*>          is an estimated upper bound for the magnitude of the largest
158*>          element in (X(j) - XTRUE) divided by the magnitude of the
159*>          largest element in X(j).  The estimate is as reliable as
160*>          the estimate for RCOND, and is almost always a slight
161*>          overestimate of the true error.
162*> \endverbatim
163*>
164*> \param[out] BERR
165*> \verbatim
166*>          BERR is REAL array, dimension (NRHS)
167*>          The componentwise relative backward error of each solution
168*>          vector X(j) (i.e., the smallest relative change in
169*>          any element of A or B that makes X(j) an exact solution).
170*> \endverbatim
171*>
172*> \param[out] WORK
173*> \verbatim
174*>          WORK is COMPLEX array, dimension (2*N)
175*> \endverbatim
176*>
177*> \param[out] RWORK
178*> \verbatim
179*>          RWORK is REAL array, dimension (N)
180*> \endverbatim
181*>
182*> \param[out] INFO
183*> \verbatim
184*>          INFO is INTEGER
185*>          = 0:  successful exit
186*>          < 0:  if INFO = -i, the i-th argument had an illegal value
187*> \endverbatim
188*
189*> \par Internal Parameters:
190*  =========================
191*>
192*> \verbatim
193*>  ITMAX is the maximum number of steps of iterative refinement.
194*> \endverbatim
195*
196*  Authors:
197*  ========
198*
199*> \author Univ. of Tennessee
200*> \author Univ. of California Berkeley
201*> \author Univ. of Colorado Denver
202*> \author NAG Ltd.
203*
204*> \ingroup complexGTcomputational
205*
206*  =====================================================================
207      SUBROUTINE CGTRFS( TRANS, N, NRHS, DL, D, DU, DLF, DF, DUF, DU2,
208     $                   IPIV, B, LDB, X, LDX, FERR, BERR, WORK, RWORK,
209     $                   INFO )
210*
211*  -- LAPACK computational routine --
212*  -- LAPACK is a software package provided by Univ. of Tennessee,    --
213*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
214*
215*     .. Scalar Arguments ..
216      CHARACTER          TRANS
217      INTEGER            INFO, LDB, LDX, N, NRHS
218*     ..
219*     .. Array Arguments ..
220      INTEGER            IPIV( * )
221      REAL               BERR( * ), FERR( * ), RWORK( * )
222      COMPLEX            B( LDB, * ), D( * ), DF( * ), DL( * ),
223     $                   DLF( * ), DU( * ), DU2( * ), DUF( * ),
224     $                   WORK( * ), X( LDX, * )
225*     ..
226*
227*  =====================================================================
228*
229*     .. Parameters ..
230      INTEGER            ITMAX
231      PARAMETER          ( ITMAX = 5 )
232      REAL               ZERO, ONE
233      PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
234      REAL               TWO
235      PARAMETER          ( TWO = 2.0E+0 )
236      REAL               THREE
237      PARAMETER          ( THREE = 3.0E+0 )
238*     ..
239*     .. Local Scalars ..
240      LOGICAL            NOTRAN
241      CHARACTER          TRANSN, TRANST
242      INTEGER            COUNT, I, J, KASE, NZ
243      REAL               EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN
244      COMPLEX            ZDUM
245*     ..
246*     .. Local Arrays ..
247      INTEGER            ISAVE( 3 )
248*     ..
249*     .. External Subroutines ..
250      EXTERNAL           CAXPY, CCOPY, CGTTRS, CLACN2, CLAGTM, XERBLA
251*     ..
252*     .. Intrinsic Functions ..
253      INTRINSIC          ABS, AIMAG, CMPLX, MAX, REAL
254*     ..
255*     .. External Functions ..
256      LOGICAL            LSAME
257      REAL               SLAMCH
258      EXTERNAL           LSAME, SLAMCH
259*     ..
260*     .. Statement Functions ..
261      REAL               CABS1
262*     ..
263*     .. Statement Function definitions ..
264      CABS1( ZDUM ) = ABS( REAL( ZDUM ) ) + ABS( AIMAG( ZDUM ) )
265*     ..
266*     .. Executable Statements ..
267*
268*     Test the input parameters.
269*
270      INFO = 0
271      NOTRAN = LSAME( TRANS, 'N' )
272      IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
273     $    LSAME( TRANS, 'C' ) ) THEN
274         INFO = -1
275      ELSE IF( N.LT.0 ) THEN
276         INFO = -2
277      ELSE IF( NRHS.LT.0 ) THEN
278         INFO = -3
279      ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
280         INFO = -13
281      ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
282         INFO = -15
283      END IF
284      IF( INFO.NE.0 ) THEN
285         CALL XERBLA( 'CGTRFS', -INFO )
286         RETURN
287      END IF
288*
289*     Quick return if possible
290*
291      IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
292         DO 10 J = 1, NRHS
293            FERR( J ) = ZERO
294            BERR( J ) = ZERO
295   10    CONTINUE
296         RETURN
297      END IF
298*
299      IF( NOTRAN ) THEN
300         TRANSN = 'N'
301         TRANST = 'C'
302      ELSE
303         TRANSN = 'C'
304         TRANST = 'N'
305      END IF
306*
307*     NZ = maximum number of nonzero elements in each row of A, plus 1
308*
309      NZ = 4
310      EPS = SLAMCH( 'Epsilon' )
311      SAFMIN = SLAMCH( 'Safe minimum' )
312      SAFE1 = NZ*SAFMIN
313      SAFE2 = SAFE1 / EPS
314*
315*     Do for each right hand side
316*
317      DO 110 J = 1, NRHS
318*
319         COUNT = 1
320         LSTRES = THREE
321   20    CONTINUE
322*
323*        Loop until stopping criterion is satisfied.
324*
325*        Compute residual R = B - op(A) * X,
326*        where op(A) = A, A**T, or A**H, depending on TRANS.
327*
328         CALL CCOPY( N, B( 1, J ), 1, WORK, 1 )
329         CALL CLAGTM( TRANS, N, 1, -ONE, DL, D, DU, X( 1, J ), LDX, ONE,
330     $                WORK, N )
331*
332*        Compute abs(op(A))*abs(x) + abs(b) for use in the backward
333*        error bound.
334*
335         IF( NOTRAN ) THEN
336            IF( N.EQ.1 ) THEN
337               RWORK( 1 ) = CABS1( B( 1, J ) ) +
338     $                      CABS1( D( 1 ) )*CABS1( X( 1, J ) )
339            ELSE
340               RWORK( 1 ) = CABS1( B( 1, J ) ) +
341     $                      CABS1( D( 1 ) )*CABS1( X( 1, J ) ) +
342     $                      CABS1( DU( 1 ) )*CABS1( X( 2, J ) )
343               DO 30 I = 2, N - 1
344                  RWORK( I ) = CABS1( B( I, J ) ) +
345     $                         CABS1( DL( I-1 ) )*CABS1( X( I-1, J ) ) +
346     $                         CABS1( D( I ) )*CABS1( X( I, J ) ) +
347     $                         CABS1( DU( I ) )*CABS1( X( I+1, J ) )
348   30          CONTINUE
349               RWORK( N ) = CABS1( B( N, J ) ) +
350     $                      CABS1( DL( N-1 ) )*CABS1( X( N-1, J ) ) +
351     $                      CABS1( D( N ) )*CABS1( X( N, J ) )
352            END IF
353         ELSE
354            IF( N.EQ.1 ) THEN
355               RWORK( 1 ) = CABS1( B( 1, J ) ) +
356     $                      CABS1( D( 1 ) )*CABS1( X( 1, J ) )
357            ELSE
358               RWORK( 1 ) = CABS1( B( 1, J ) ) +
359     $                      CABS1( D( 1 ) )*CABS1( X( 1, J ) ) +
360     $                      CABS1( DL( 1 ) )*CABS1( X( 2, J ) )
361               DO 40 I = 2, N - 1
362                  RWORK( I ) = CABS1( B( I, J ) ) +
363     $                         CABS1( DU( I-1 ) )*CABS1( X( I-1, J ) ) +
364     $                         CABS1( D( I ) )*CABS1( X( I, J ) ) +
365     $                         CABS1( DL( I ) )*CABS1( X( I+1, J ) )
366   40          CONTINUE
367               RWORK( N ) = CABS1( B( N, J ) ) +
368     $                      CABS1( DU( N-1 ) )*CABS1( X( N-1, J ) ) +
369     $                      CABS1( D( N ) )*CABS1( X( N, J ) )
370            END IF
371         END IF
372*
373*        Compute componentwise relative backward error from formula
374*
375*        max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) )
376*
377*        where abs(Z) is the componentwise absolute value of the matrix
378*        or vector Z.  If the i-th component of the denominator is less
379*        than SAFE2, then SAFE1 is added to the i-th components of the
380*        numerator and denominator before dividing.
381*
382         S = ZERO
383         DO 50 I = 1, N
384            IF( RWORK( I ).GT.SAFE2 ) THEN
385               S = MAX( S, CABS1( WORK( I ) ) / RWORK( I ) )
386            ELSE
387               S = MAX( S, ( CABS1( WORK( I ) )+SAFE1 ) /
388     $             ( RWORK( I )+SAFE1 ) )
389            END IF
390   50    CONTINUE
391         BERR( J ) = S
392*
393*        Test stopping criterion. Continue iterating if
394*           1) The residual BERR(J) is larger than machine epsilon, and
395*           2) BERR(J) decreased by at least a factor of 2 during the
396*              last iteration, and
397*           3) At most ITMAX iterations tried.
398*
399         IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
400     $       COUNT.LE.ITMAX ) THEN
401*
402*           Update solution and try again.
403*
404            CALL CGTTRS( TRANS, N, 1, DLF, DF, DUF, DU2, IPIV, WORK, N,
405     $                   INFO )
406            CALL CAXPY( N, CMPLX( ONE ), WORK, 1, X( 1, J ), 1 )
407            LSTRES = BERR( J )
408            COUNT = COUNT + 1
409            GO TO 20
410         END IF
411*
412*        Bound error from formula
413*
414*        norm(X - XTRUE) / norm(X) .le. FERR =
415*        norm( abs(inv(op(A)))*
416*           ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X)
417*
418*        where
419*          norm(Z) is the magnitude of the largest component of Z
420*          inv(op(A)) is the inverse of op(A)
421*          abs(Z) is the componentwise absolute value of the matrix or
422*             vector Z
423*          NZ is the maximum number of nonzeros in any row of A, plus 1
424*          EPS is machine epsilon
425*
426*        The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B))
427*        is incremented by SAFE1 if the i-th component of
428*        abs(op(A))*abs(X) + abs(B) is less than SAFE2.
429*
430*        Use CLACN2 to estimate the infinity-norm of the matrix
431*           inv(op(A)) * diag(W),
432*        where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) )))
433*
434         DO 60 I = 1, N
435            IF( RWORK( I ).GT.SAFE2 ) THEN
436               RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I )
437            ELSE
438               RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I ) +
439     $                      SAFE1
440            END IF
441   60    CONTINUE
442*
443         KASE = 0
444   70    CONTINUE
445         CALL CLACN2( N, WORK( N+1 ), WORK, FERR( J ), KASE, ISAVE )
446         IF( KASE.NE.0 ) THEN
447            IF( KASE.EQ.1 ) THEN
448*
449*              Multiply by diag(W)*inv(op(A)**H).
450*
451               CALL CGTTRS( TRANST, N, 1, DLF, DF, DUF, DU2, IPIV, WORK,
452     $                      N, INFO )
453               DO 80 I = 1, N
454                  WORK( I ) = RWORK( I )*WORK( I )
455   80          CONTINUE
456            ELSE
457*
458*              Multiply by inv(op(A))*diag(W).
459*
460               DO 90 I = 1, N
461                  WORK( I ) = RWORK( I )*WORK( I )
462   90          CONTINUE
463               CALL CGTTRS( TRANSN, N, 1, DLF, DF, DUF, DU2, IPIV, WORK,
464     $                      N, INFO )
465            END IF
466            GO TO 70
467         END IF
468*
469*        Normalize error.
470*
471         LSTRES = ZERO
472         DO 100 I = 1, N
473            LSTRES = MAX( LSTRES, CABS1( X( I, J ) ) )
474  100    CONTINUE
475         IF( LSTRES.NE.ZERO )
476     $      FERR( J ) = FERR( J ) / LSTRES
477*
478  110 CONTINUE
479*
480      RETURN
481*
482*     End of CGTRFS
483*
484      END
485