001:       SUBROUTINE DTRRFS( UPLO, TRANS, DIAG, N, NRHS, A, LDA, B, LDB, X,
002:      $                   LDX, FERR, BERR, WORK, IWORK, 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 DLACN2 in place of DLACON, 5 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:       INTEGER            IWORK( * )
016:       DOUBLE PRECISION   A( LDA, * ), B( LDB, * ), BERR( * ), FERR( * ),
017:      $                   WORK( * ), X( LDX, * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  DTRRFS 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 DTRTRS or some other
028: *  means before entering this routine.  DTRRFS 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 = 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION array, dimension (3*N)
096: *
097: *  IWORK   (workspace) INTEGER 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:       DOUBLE PRECISION   ZERO
107:       PARAMETER          ( ZERO = 0.0D+0 )
108:       DOUBLE PRECISION   ONE
109:       PARAMETER          ( ONE = 1.0D+0 )
110: *     ..
111: *     .. Local Scalars ..
112:       LOGICAL            NOTRAN, NOUNIT, UPPER
113:       CHARACTER          TRANST
114:       INTEGER            I, J, K, KASE, NZ
115:       DOUBLE PRECISION   EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
116: *     ..
117: *     .. Local Arrays ..
118:       INTEGER            ISAVE( 3 )
119: *     ..
120: *     .. External Subroutines ..
121:       EXTERNAL           DAXPY, DCOPY, DLACN2, DTRMV, DTRSV, XERBLA
122: *     ..
123: *     .. Intrinsic Functions ..
124:       INTRINSIC          ABS, MAX
125: *     ..
126: *     .. External Functions ..
127:       LOGICAL            LSAME
128:       DOUBLE PRECISION   DLAMCH
129:       EXTERNAL           LSAME, DLAMCH
130: *     ..
131: *     .. Executable Statements ..
132: *
133: *     Test the input parameters.
134: *
135:       INFO = 0
136:       UPPER = LSAME( UPLO, 'U' )
137:       NOTRAN = LSAME( TRANS, 'N' )
138:       NOUNIT = LSAME( DIAG, 'N' )
139: *
140:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
141:          INFO = -1
142:       ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
143:      $         LSAME( TRANS, 'C' ) ) THEN
144:          INFO = -2
145:       ELSE IF( .NOT.NOUNIT .AND. .NOT.LSAME( DIAG, 'U' ) ) THEN
146:          INFO = -3
147:       ELSE IF( N.LT.0 ) THEN
148:          INFO = -4
149:       ELSE IF( NRHS.LT.0 ) THEN
150:          INFO = -5
151:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
152:          INFO = -7
153:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
154:          INFO = -9
155:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
156:          INFO = -11
157:       END IF
158:       IF( INFO.NE.0 ) THEN
159:          CALL XERBLA( 'DTRRFS', -INFO )
160:          RETURN
161:       END IF
162: *
163: *     Quick return if possible
164: *
165:       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
166:          DO 10 J = 1, NRHS
167:             FERR( J ) = ZERO
168:             BERR( J ) = ZERO
169:    10    CONTINUE
170:          RETURN
171:       END IF
172: *
173:       IF( NOTRAN ) THEN
174:          TRANST = 'T'
175:       ELSE
176:          TRANST = 'N'
177:       END IF
178: *
179: *     NZ = maximum number of nonzero elements in each row of A, plus 1
180: *
181:       NZ = N + 1
182:       EPS = DLAMCH( 'Epsilon' )
183:       SAFMIN = DLAMCH( 'Safe minimum' )
184:       SAFE1 = NZ*SAFMIN
185:       SAFE2 = SAFE1 / EPS
186: *
187: *     Do for each right hand side
188: *
189:       DO 250 J = 1, NRHS
190: *
191: *        Compute residual R = B - op(A) * X,
192: *        where op(A) = A or A', depending on TRANS.
193: *
194:          CALL DCOPY( N, X( 1, J ), 1, WORK( N+1 ), 1 )
195:          CALL DTRMV( UPLO, TRANS, DIAG, N, A, LDA, WORK( N+1 ), 1 )
196:          CALL DAXPY( N, -ONE, B( 1, J ), 1, WORK( N+1 ), 1 )
197: *
198: *        Compute componentwise relative backward error from formula
199: *
200: *        max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) )
201: *
202: *        where abs(Z) is the componentwise absolute value of the matrix
203: *        or vector Z.  If the i-th component of the denominator is less
204: *        than SAFE2, then SAFE1 is added to the i-th components of the
205: *        numerator and denominator before dividing.
206: *
207:          DO 20 I = 1, N
208:             WORK( I ) = ABS( B( I, J ) )
209:    20    CONTINUE
210: *
211:          IF( NOTRAN ) THEN
212: *
213: *           Compute abs(A)*abs(X) + abs(B).
214: *
215:             IF( UPPER ) THEN
216:                IF( NOUNIT ) THEN
217:                   DO 40 K = 1, N
218:                      XK = ABS( X( K, J ) )
219:                      DO 30 I = 1, K
220:                         WORK( I ) = WORK( I ) + ABS( A( I, K ) )*XK
221:    30                CONTINUE
222:    40             CONTINUE
223:                ELSE
224:                   DO 60 K = 1, N
225:                      XK = ABS( X( K, J ) )
226:                      DO 50 I = 1, K - 1
227:                         WORK( I ) = WORK( I ) + ABS( A( I, K ) )*XK
228:    50                CONTINUE
229:                      WORK( K ) = WORK( K ) + XK
230:    60             CONTINUE
231:                END IF
232:             ELSE
233:                IF( NOUNIT ) THEN
234:                   DO 80 K = 1, N
235:                      XK = ABS( X( K, J ) )
236:                      DO 70 I = K, N
237:                         WORK( I ) = WORK( I ) + ABS( A( I, K ) )*XK
238:    70                CONTINUE
239:    80             CONTINUE
240:                ELSE
241:                   DO 100 K = 1, N
242:                      XK = ABS( X( K, J ) )
243:                      DO 90 I = K + 1, N
244:                         WORK( I ) = WORK( I ) + ABS( A( I, K ) )*XK
245:    90                CONTINUE
246:                      WORK( K ) = WORK( K ) + XK
247:   100             CONTINUE
248:                END IF
249:             END IF
250:          ELSE
251: *
252: *           Compute abs(A')*abs(X) + abs(B).
253: *
254:             IF( UPPER ) THEN
255:                IF( NOUNIT ) THEN
256:                   DO 120 K = 1, N
257:                      S = ZERO
258:                      DO 110 I = 1, K
259:                         S = S + ABS( A( I, K ) )*ABS( X( I, J ) )
260:   110                CONTINUE
261:                      WORK( K ) = WORK( K ) + S
262:   120             CONTINUE
263:                ELSE
264:                   DO 140 K = 1, N
265:                      S = ABS( X( K, J ) )
266:                      DO 130 I = 1, K - 1
267:                         S = S + ABS( A( I, K ) )*ABS( X( I, J ) )
268:   130                CONTINUE
269:                      WORK( K ) = WORK( K ) + S
270:   140             CONTINUE
271:                END IF
272:             ELSE
273:                IF( NOUNIT ) THEN
274:                   DO 160 K = 1, N
275:                      S = ZERO
276:                      DO 150 I = K, N
277:                         S = S + ABS( A( I, K ) )*ABS( X( I, J ) )
278:   150                CONTINUE
279:                      WORK( K ) = WORK( K ) + S
280:   160             CONTINUE
281:                ELSE
282:                   DO 180 K = 1, N
283:                      S = ABS( X( K, J ) )
284:                      DO 170 I = K + 1, N
285:                         S = S + ABS( A( I, K ) )*ABS( X( I, J ) )
286:   170                CONTINUE
287:                      WORK( K ) = WORK( K ) + S
288:   180             CONTINUE
289:                END IF
290:             END IF
291:          END IF
292:          S = ZERO
293:          DO 190 I = 1, N
294:             IF( WORK( I ).GT.SAFE2 ) THEN
295:                S = MAX( S, ABS( WORK( N+I ) ) / WORK( I ) )
296:             ELSE
297:                S = MAX( S, ( ABS( WORK( N+I ) )+SAFE1 ) /
298:      $             ( WORK( I )+SAFE1 ) )
299:             END IF
300:   190    CONTINUE
301:          BERR( J ) = S
302: *
303: *        Bound error from formula
304: *
305: *        norm(X - XTRUE) / norm(X) .le. FERR =
306: *        norm( abs(inv(op(A)))*
307: *           ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X)
308: *
309: *        where
310: *          norm(Z) is the magnitude of the largest component of Z
311: *          inv(op(A)) is the inverse of op(A)
312: *          abs(Z) is the componentwise absolute value of the matrix or
313: *             vector Z
314: *          NZ is the maximum number of nonzeros in any row of A, plus 1
315: *          EPS is machine epsilon
316: *
317: *        The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B))
318: *        is incremented by SAFE1 if the i-th component of
319: *        abs(op(A))*abs(X) + abs(B) is less than SAFE2.
320: *
321: *        Use DLACN2 to estimate the infinity-norm of the matrix
322: *           inv(op(A)) * diag(W),
323: *        where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) )))
324: *
325:          DO 200 I = 1, N
326:             IF( WORK( I ).GT.SAFE2 ) THEN
327:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I )
328:             ELSE
329:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I ) + SAFE1
330:             END IF
331:   200    CONTINUE
332: *
333:          KASE = 0
334:   210    CONTINUE
335:          CALL DLACN2( N, WORK( 2*N+1 ), WORK( N+1 ), IWORK, FERR( J ),
336:      $                KASE, ISAVE )
337:          IF( KASE.NE.0 ) THEN
338:             IF( KASE.EQ.1 ) THEN
339: *
340: *              Multiply by diag(W)*inv(op(A)').
341: *
342:                CALL DTRSV( UPLO, TRANST, DIAG, N, A, LDA, WORK( N+1 ),
343:      $                     1 )
344:                DO 220 I = 1, N
345:                   WORK( N+I ) = WORK( I )*WORK( N+I )
346:   220          CONTINUE
347:             ELSE
348: *
349: *              Multiply by inv(op(A))*diag(W).
350: *
351:                DO 230 I = 1, N
352:                   WORK( N+I ) = WORK( I )*WORK( N+I )
353:   230          CONTINUE
354:                CALL DTRSV( UPLO, TRANS, DIAG, N, A, LDA, WORK( N+1 ),
355:      $                     1 )
356:             END IF
357:             GO TO 210
358:          END IF
359: *
360: *        Normalize error.
361: *
362:          LSTRES = ZERO
363:          DO 240 I = 1, N
364:             LSTRES = MAX( LSTRES, ABS( X( I, J ) ) )
365:   240    CONTINUE
366:          IF( LSTRES.NE.ZERO )
367:      $      FERR( J ) = FERR( J ) / LSTRES
368: *
369:   250 CONTINUE
370: *
371:       RETURN
372: *
373: *     End of DTRRFS
374: *
375:       END
376: