1       SUBROUTINE ZSYRFS( UPLO, N, NRHS, A, LDA, AF, LDAF, IPIV, B, LDB,
  2      $                   X, LDX, FERR, BERR, WORK, RWORK, INFO )
  3 *
  4 *  -- LAPACK routine (version 3.2) --
  5 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
  6 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  7 *     November 2006
  8 *
  9 *     Modified to call ZLACN2 in place of ZLACON, 10 Feb 03, SJH.
 10 *
 11 *     .. Scalar Arguments ..
 12       CHARACTER          UPLO
 13       INTEGER            INFO, LDA, LDAF, LDB, LDX, N, NRHS
 14 *     ..
 15 *     .. Array Arguments ..
 16       INTEGER            IPIV( * )
 17       DOUBLE PRECISION   BERR( * ), FERR( * ), RWORK( * )
 18       COMPLEX*16         A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
 19      $                   WORK( * ), X( LDX, * )
 20 *     ..
 21 *
 22 *  Purpose
 23 *  =======
 24 *
 25 *  ZSYRFS improves the computed solution to a system of linear
 26 *  equations when the coefficient matrix is symmetric indefinite, and
 27 *  provides error bounds and backward error estimates for the solution.
 28 *
 29 *  Arguments
 30 *  =========
 31 *
 32 *  UPLO    (input) CHARACTER*1
 33 *          = 'U':  Upper triangle of A is stored;
 34 *          = 'L':  Lower triangle of A is stored.
 35 *
 36 *  N       (input) INTEGER
 37 *          The order of the matrix A.  N >= 0.
 38 *
 39 *  NRHS    (input) INTEGER
 40 *          The number of right hand sides, i.e., the number of columns
 41 *          of the matrices B and X.  NRHS >= 0.
 42 *
 43 *  A       (input) COMPLEX*16 array, dimension (LDA,N)
 44 *          The symmetric matrix A.  If UPLO = 'U', the leading N-by-N
 45 *          upper triangular part of A contains the upper triangular part
 46 *          of the matrix A, and the strictly lower triangular part of A
 47 *          is not referenced.  If UPLO = 'L', the leading N-by-N lower
 48 *          triangular part of A contains the lower triangular part of
 49 *          the matrix A, and the strictly upper triangular part of A is
 50 *          not referenced.
 51 *
 52 *  LDA     (input) INTEGER
 53 *          The leading dimension of the array A.  LDA >= max(1,N).
 54 *
 55 *  AF      (input) COMPLEX*16 array, dimension (LDAF,N)
 56 *          The factored form of the matrix A.  AF contains the block
 57 *          diagonal matrix D and the multipliers used to obtain the
 58 *          factor U or L from the factorization A = U*D*U**T or
 59 *          A = L*D*L**T as computed by ZSYTRF.
 60 *
 61 *  LDAF    (input) INTEGER
 62 *          The leading dimension of the array AF.  LDAF >= max(1,N).
 63 *
 64 *  IPIV    (input) INTEGER array, dimension (N)
 65 *          Details of the interchanges and the block structure of D
 66 *          as determined by ZSYTRF.
 67 *
 68 *  B       (input) COMPLEX*16 array, dimension (LDB,NRHS)
 69 *          The right hand side matrix B.
 70 *
 71 *  LDB     (input) INTEGER
 72 *          The leading dimension of the array B.  LDB >= max(1,N).
 73 *
 74 *  X       (input/output) COMPLEX*16 array, dimension (LDX,NRHS)
 75 *          On entry, the solution matrix X, as computed by ZSYTRS.
 76 *          On exit, the improved solution matrix X.
 77 *
 78 *  LDX     (input) INTEGER
 79 *          The leading dimension of the array X.  LDX >= max(1,N).
 80 *
 81 *  FERR    (output) DOUBLE PRECISION array, dimension (NRHS)
 82 *          The estimated forward error bound for each solution vector
 83 *          X(j) (the j-th column of the solution matrix X).
 84 *          If XTRUE is the true solution corresponding to X(j), FERR(j)
 85 *          is an estimated upper bound for the magnitude of the largest
 86 *          element in (X(j) - XTRUE) divided by the magnitude of the
 87 *          largest element in X(j).  The estimate is as reliable as
 88 *          the estimate for RCOND, and is almost always a slight
 89 *          overestimate of the true error.
 90 *
 91 *  BERR    (output) DOUBLE PRECISION array, dimension (NRHS)
 92 *          The componentwise relative backward error of each solution
 93 *          vector X(j) (i.e., the smallest relative change in
 94 *          any element of A or B that makes X(j) an exact solution).
 95 *
 96 *  WORK    (workspace) COMPLEX*16 array, dimension (2*N)
 97 *
 98 *  RWORK   (workspace) DOUBLE PRECISION array, dimension (N)
 99 *
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+00.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          ABSDBLEDIMAGMAX
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 ) = ABSDBLE( ZDUM ) ) + ABSDIMAG( 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.MAX1, N ) ) THEN
162          INFO = -5
163       ELSE IF( LDAF.LT.MAX1, N ) ) THEN
164          INFO = -7
165       ELSE IF( LDB.LT.MAX1, N ) ) THEN
166          INFO = -10
167       ELSE IF( LDX.LT.MAX1, 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**T).
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