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