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