001:       SUBROUTINE CTRRFS( UPLO, TRANS, DIAG, N, NRHS, A, LDA, B, LDB, X,
002:      $                   LDX, FERR, BERR, WORK, RWORK, INFO )
003: *
004: *  -- LAPACK routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     Modified to call CLACN2 in place of CLACON, 10 Feb 03, SJH.
009: *
010: *     .. Scalar Arguments ..
011:       CHARACTER          DIAG, TRANS, UPLO
012:       INTEGER            INFO, LDA, LDB, LDX, N, NRHS
013: *     ..
014: *     .. Array Arguments ..
015:       REAL               BERR( * ), FERR( * ), RWORK( * )
016:       COMPLEX            A( LDA, * ), B( LDB, * ), WORK( * ),
017:      $                   X( LDX, * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  CTRRFS provides error bounds and backward error estimates for the
024: *  solution to a system of linear equations with a triangular
025: *  coefficient matrix.
026: *
027: *  The solution matrix X must be computed by CTRTRS or some other
028: *  means before entering this routine.  CTRRFS does not do iterative
029: *  refinement because doing so cannot improve the backward error.
030: *
031: *  Arguments
032: *  =========
033: *
034: *  UPLO    (input) CHARACTER*1
035: *          = 'U':  A is upper triangular;
036: *          = 'L':  A is lower triangular.
037: *
038: *  TRANS   (input) CHARACTER*1
039: *          Specifies the form of the system of equations:
040: *          = 'N':  A * X = B     (No transpose)
041: *          = 'T':  A**T * X = B  (Transpose)
042: *          = 'C':  A**H * X = B  (Conjugate transpose)
043: *
044: *  DIAG    (input) CHARACTER*1
045: *          = 'N':  A is non-unit triangular;
046: *          = 'U':  A is unit triangular.
047: *
048: *  N       (input) INTEGER
049: *          The order of the matrix A.  N >= 0.
050: *
051: *  NRHS    (input) INTEGER
052: *          The number of right hand sides, i.e., the number of columns
053: *          of the matrices B and X.  NRHS >= 0.
054: *
055: *  A       (input) COMPLEX array, dimension (LDA,N)
056: *          The triangular matrix A.  If UPLO = 'U', the leading N-by-N
057: *          upper triangular part of the array A contains the upper
058: *          triangular matrix, and the strictly lower triangular part of
059: *          A is not referenced.  If UPLO = 'L', the leading N-by-N lower
060: *          triangular part of the array A contains the lower triangular
061: *          matrix, and the strictly upper triangular part of A is not
062: *          referenced.  If DIAG = 'U', the diagonal elements of A are
063: *          also not referenced and are assumed to be 1.
064: *
065: *  LDA     (input) INTEGER
066: *          The leading dimension of the array A.  LDA >= max(1,N).
067: *
068: *  B       (input) COMPLEX array, dimension (LDB,NRHS)
069: *          The right hand side matrix B.
070: *
071: *  LDB     (input) INTEGER
072: *          The leading dimension of the array B.  LDB >= max(1,N).
073: *
074: *  X       (input) COMPLEX array, dimension (LDX,NRHS)
075: *          The solution matrix X.
076: *
077: *  LDX     (input) INTEGER
078: *          The leading dimension of the array X.  LDX >= max(1,N).
079: *
080: *  FERR    (output) REAL array, dimension (NRHS)
081: *          The estimated forward error bound for each solution vector
082: *          X(j) (the j-th column of the solution matrix X).
083: *          If XTRUE is the true solution corresponding to X(j), FERR(j)
084: *          is an estimated upper bound for the magnitude of the largest
085: *          element in (X(j) - XTRUE) divided by the magnitude of the
086: *          largest element in X(j).  The estimate is as reliable as
087: *          the estimate for RCOND, and is almost always a slight
088: *          overestimate of the true error.
089: *
090: *  BERR    (output) REAL array, dimension (NRHS)
091: *          The componentwise relative backward error of each solution
092: *          vector X(j) (i.e., the smallest relative change in
093: *          any element of A or B that makes X(j) an exact solution).
094: *
095: *  WORK    (workspace) COMPLEX array, dimension (2*N)
096: *
097: *  RWORK   (workspace) REAL array, dimension (N)
098: *
099: *  INFO    (output) INTEGER
100: *          = 0:  successful exit
101: *          < 0:  if INFO = -i, the i-th argument had an illegal value
102: *
103: *  =====================================================================
104: *
105: *     .. Parameters ..
106:       REAL               ZERO
107:       PARAMETER          ( ZERO = 0.0E+0 )
108:       COMPLEX            ONE
109:       PARAMETER          ( ONE = ( 1.0E+0, 0.0E+0 ) )
110: *     ..
111: *     .. Local Scalars ..
112:       LOGICAL            NOTRAN, NOUNIT, UPPER
113:       CHARACTER          TRANSN, TRANST
114:       INTEGER            I, J, K, KASE, NZ
115:       REAL               EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
116:       COMPLEX            ZDUM
117: *     ..
118: *     .. Local Arrays ..
119:       INTEGER            ISAVE( 3 )
120: *     ..
121: *     .. External Subroutines ..
122:       EXTERNAL           CAXPY, CCOPY, CLACN2, CTRMV, CTRSV, XERBLA
123: *     ..
124: *     .. Intrinsic Functions ..
125:       INTRINSIC          ABS, AIMAG, MAX, REAL
126: *     ..
127: *     .. External Functions ..
128:       LOGICAL            LSAME
129:       REAL               SLAMCH
130:       EXTERNAL           LSAME, SLAMCH
131: *     ..
132: *     .. Statement Functions ..
133:       REAL               CABS1
134: *     ..
135: *     .. Statement Function definitions ..
136:       CABS1( ZDUM ) = ABS( REAL( ZDUM ) ) + ABS( AIMAG( ZDUM ) )
137: *     ..
138: *     .. Executable Statements ..
139: *
140: *     Test the input parameters.
141: *
142:       INFO = 0
143:       UPPER = LSAME( UPLO, 'U' )
144:       NOTRAN = LSAME( TRANS, 'N' )
145:       NOUNIT = LSAME( DIAG, 'N' )
146: *
147:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
148:          INFO = -1
149:       ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
150:      $         LSAME( TRANS, 'C' ) ) THEN
151:          INFO = -2
152:       ELSE IF( .NOT.NOUNIT .AND. .NOT.LSAME( DIAG, 'U' ) ) THEN
153:          INFO = -3
154:       ELSE IF( N.LT.0 ) THEN
155:          INFO = -4
156:       ELSE IF( NRHS.LT.0 ) THEN
157:          INFO = -5
158:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
159:          INFO = -7
160:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
161:          INFO = -9
162:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
163:          INFO = -11
164:       END IF
165:       IF( INFO.NE.0 ) THEN
166:          CALL XERBLA( 'CTRRFS', -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 = 'C'
183:       ELSE
184:          TRANSN = 'C'
185:          TRANST = 'N'
186:       END IF
187: *
188: *     NZ = maximum number of nonzero elements in each row of A, plus 1
189: *
190:       NZ = N + 1
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 250 J = 1, NRHS
199: *
200: *        Compute residual R = B - op(A) * X,
201: *        where op(A) = A, A**T, or A**H, depending on TRANS.
202: *
203:          CALL CCOPY( N, X( 1, J ), 1, WORK, 1 )
204:          CALL CTRMV( UPLO, TRANS, DIAG, N, A, LDA, WORK, 1 )
205:          CALL CAXPY( N, -ONE, B( 1, J ), 1, WORK, 1 )
206: *
207: *        Compute componentwise relative backward error from formula
208: *
209: *        max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) )
210: *
211: *        where abs(Z) is the componentwise absolute value of the matrix
212: *        or vector Z.  If the i-th component of the denominator is less
213: *        than SAFE2, then SAFE1 is added to the i-th components of the
214: *        numerator and denominator before dividing.
215: *
216:          DO 20 I = 1, N
217:             RWORK( I ) = CABS1( B( I, J ) )
218:    20    CONTINUE
219: *
220:          IF( NOTRAN ) THEN
221: *
222: *           Compute abs(A)*abs(X) + abs(B).
223: *
224:             IF( UPPER ) THEN
225:                IF( NOUNIT ) THEN
226:                   DO 40 K = 1, N
227:                      XK = CABS1( X( K, J ) )
228:                      DO 30 I = 1, K
229:                         RWORK( I ) = RWORK( I ) + CABS1( A( I, K ) )*XK
230:    30                CONTINUE
231:    40             CONTINUE
232:                ELSE
233:                   DO 60 K = 1, N
234:                      XK = CABS1( X( K, J ) )
235:                      DO 50 I = 1, K - 1
236:                         RWORK( I ) = RWORK( I ) + CABS1( A( I, K ) )*XK
237:    50                CONTINUE
238:                      RWORK( K ) = RWORK( K ) + XK
239:    60             CONTINUE
240:                END IF
241:             ELSE
242:                IF( NOUNIT ) THEN
243:                   DO 80 K = 1, N
244:                      XK = CABS1( X( K, J ) )
245:                      DO 70 I = K, N
246:                         RWORK( I ) = RWORK( I ) + CABS1( A( I, K ) )*XK
247:    70                CONTINUE
248:    80             CONTINUE
249:                ELSE
250:                   DO 100 K = 1, N
251:                      XK = CABS1( X( K, J ) )
252:                      DO 90 I = K + 1, N
253:                         RWORK( I ) = RWORK( I ) + CABS1( A( I, K ) )*XK
254:    90                CONTINUE
255:                      RWORK( K ) = RWORK( K ) + XK
256:   100             CONTINUE
257:                END IF
258:             END IF
259:          ELSE
260: *
261: *           Compute abs(A**H)*abs(X) + abs(B).
262: *
263:             IF( UPPER ) THEN
264:                IF( NOUNIT ) THEN
265:                   DO 120 K = 1, N
266:                      S = ZERO
267:                      DO 110 I = 1, K
268:                         S = S + CABS1( A( I, K ) )*CABS1( X( I, J ) )
269:   110                CONTINUE
270:                      RWORK( K ) = RWORK( K ) + S
271:   120             CONTINUE
272:                ELSE
273:                   DO 140 K = 1, N
274:                      S = CABS1( X( K, J ) )
275:                      DO 130 I = 1, K - 1
276:                         S = S + CABS1( A( I, K ) )*CABS1( X( I, J ) )
277:   130                CONTINUE
278:                      RWORK( K ) = RWORK( K ) + S
279:   140             CONTINUE
280:                END IF
281:             ELSE
282:                IF( NOUNIT ) THEN
283:                   DO 160 K = 1, N
284:                      S = ZERO
285:                      DO 150 I = K, N
286:                         S = S + CABS1( A( I, K ) )*CABS1( X( I, J ) )
287:   150                CONTINUE
288:                      RWORK( K ) = RWORK( K ) + S
289:   160             CONTINUE
290:                ELSE
291:                   DO 180 K = 1, N
292:                      S = CABS1( X( K, J ) )
293:                      DO 170 I = K + 1, N
294:                         S = S + CABS1( A( I, K ) )*CABS1( X( I, J ) )
295:   170                CONTINUE
296:                      RWORK( K ) = RWORK( K ) + S
297:   180             CONTINUE
298:                END IF
299:             END IF
300:          END IF
301:          S = ZERO
302:          DO 190 I = 1, N
303:             IF( RWORK( I ).GT.SAFE2 ) THEN
304:                S = MAX( S, CABS1( WORK( I ) ) / RWORK( I ) )
305:             ELSE
306:                S = MAX( S, ( CABS1( WORK( I ) )+SAFE1 ) /
307:      $             ( RWORK( I )+SAFE1 ) )
308:             END IF
309:   190    CONTINUE
310:          BERR( J ) = S
311: *
312: *        Bound error from formula
313: *
314: *        norm(X - XTRUE) / norm(X) .le. FERR =
315: *        norm( abs(inv(op(A)))*
316: *           ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X)
317: *
318: *        where
319: *          norm(Z) is the magnitude of the largest component of Z
320: *          inv(op(A)) is the inverse of op(A)
321: *          abs(Z) is the componentwise absolute value of the matrix or
322: *             vector Z
323: *          NZ is the maximum number of nonzeros in any row of A, plus 1
324: *          EPS is machine epsilon
325: *
326: *        The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B))
327: *        is incremented by SAFE1 if the i-th component of
328: *        abs(op(A))*abs(X) + abs(B) is less than SAFE2.
329: *
330: *        Use CLACN2 to estimate the infinity-norm of the matrix
331: *           inv(op(A)) * diag(W),
332: *        where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) )))
333: *
334:          DO 200 I = 1, N
335:             IF( RWORK( I ).GT.SAFE2 ) THEN
336:                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I )
337:             ELSE
338:                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I ) +
339:      $                      SAFE1
340:             END IF
341:   200    CONTINUE
342: *
343:          KASE = 0
344:   210    CONTINUE
345:          CALL CLACN2( N, WORK( N+1 ), WORK, FERR( J ), KASE, ISAVE )
346:          IF( KASE.NE.0 ) THEN
347:             IF( KASE.EQ.1 ) THEN
348: *
349: *              Multiply by diag(W)*inv(op(A)**H).
350: *
351:                CALL CTRSV( UPLO, TRANST, DIAG, N, A, LDA, WORK, 1 )
352:                DO 220 I = 1, N
353:                   WORK( I ) = RWORK( I )*WORK( I )
354:   220          CONTINUE
355:             ELSE
356: *
357: *              Multiply by inv(op(A))*diag(W).
358: *
359:                DO 230 I = 1, N
360:                   WORK( I ) = RWORK( I )*WORK( I )
361:   230          CONTINUE
362:                CALL CTRSV( UPLO, TRANSN, DIAG, N, A, LDA, WORK, 1 )
363:             END IF
364:             GO TO 210
365:          END IF
366: *
367: *        Normalize error.
368: *
369:          LSTRES = ZERO
370:          DO 240 I = 1, N
371:             LSTRES = MAX( LSTRES, CABS1( X( I, J ) ) )
372:   240    CONTINUE
373:          IF( LSTRES.NE.ZERO )
374:      $      FERR( J ) = FERR( J ) / LSTRES
375: *
376:   250 CONTINUE
377: *
378:       RETURN
379: *
380: *     End of CTRRFS
381: *
382:       END
383: