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