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