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