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