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