001:       SUBROUTINE SSPRFS( UPLO, N, NRHS, AP, AFP, IPIV, B, LDB, X, LDX,
002:      $                   FERR, BERR, WORK, IWORK, 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 SLACN2 in place of SLACON, 5 Feb 03, SJH.
010: *
011: *     .. Scalar Arguments ..
012:       CHARACTER          UPLO
013:       INTEGER            INFO, LDB, LDX, N, NRHS
014: *     ..
015: *     .. Array Arguments ..
016:       INTEGER            IPIV( * ), IWORK( * )
017:       REAL               AFP( * ), AP( * ), B( LDB, * ), BERR( * ),
018:      $                   FERR( * ), WORK( * ), X( LDX, * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  SSPRFS 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) REAL 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) REAL 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 SSPTRF, 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 SSPTRF.
060: *
061: *  B       (input) REAL 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) REAL array, dimension (LDX,NRHS)
068: *          On entry, the solution matrix X, as computed by SSPTRS.
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) REAL 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) REAL 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) REAL array, dimension (3*N)
090: *
091: *  IWORK   (workspace) INTEGER 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:       REAL               ZERO
108:       PARAMETER          ( ZERO = 0.0E+0 )
109:       REAL               ONE
110:       PARAMETER          ( ONE = 1.0E+0 )
111:       REAL               TWO
112:       PARAMETER          ( TWO = 2.0E+0 )
113:       REAL               THREE
114:       PARAMETER          ( THREE = 3.0E+0 )
115: *     ..
116: *     .. Local Scalars ..
117:       LOGICAL            UPPER
118:       INTEGER            COUNT, I, IK, J, K, KASE, KK, NZ
119:       REAL               EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
120: *     ..
121: *     .. Local Arrays ..
122:       INTEGER            ISAVE( 3 )
123: *     ..
124: *     .. External Subroutines ..
125:       EXTERNAL           SAXPY, SCOPY, SLACN2, SSPMV, SSPTRS, XERBLA
126: *     ..
127: *     .. Intrinsic Functions ..
128:       INTRINSIC          ABS, MAX
129: *     ..
130: *     .. External Functions ..
131:       LOGICAL            LSAME
132:       REAL               SLAMCH
133:       EXTERNAL           LSAME, SLAMCH
134: *     ..
135: *     .. Executable Statements ..
136: *
137: *     Test the input parameters.
138: *
139:       INFO = 0
140:       UPPER = LSAME( UPLO, 'U' )
141:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
142:          INFO = -1
143:       ELSE IF( N.LT.0 ) THEN
144:          INFO = -2
145:       ELSE IF( NRHS.LT.0 ) THEN
146:          INFO = -3
147:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
148:          INFO = -8
149:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
150:          INFO = -10
151:       END IF
152:       IF( INFO.NE.0 ) THEN
153:          CALL XERBLA( 'SSPRFS', -INFO )
154:          RETURN
155:       END IF
156: *
157: *     Quick return if possible
158: *
159:       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
160:          DO 10 J = 1, NRHS
161:             FERR( J ) = ZERO
162:             BERR( J ) = ZERO
163:    10    CONTINUE
164:          RETURN
165:       END IF
166: *
167: *     NZ = maximum number of nonzero elements in each row of A, plus 1
168: *
169:       NZ = N + 1
170:       EPS = SLAMCH( 'Epsilon' )
171:       SAFMIN = SLAMCH( 'Safe minimum' )
172:       SAFE1 = NZ*SAFMIN
173:       SAFE2 = SAFE1 / EPS
174: *
175: *     Do for each right hand side
176: *
177:       DO 140 J = 1, NRHS
178: *
179:          COUNT = 1
180:          LSTRES = THREE
181:    20    CONTINUE
182: *
183: *        Loop until stopping criterion is satisfied.
184: *
185: *        Compute residual R = B - A * X
186: *
187:          CALL SCOPY( N, B( 1, J ), 1, WORK( N+1 ), 1 )
188:          CALL SSPMV( UPLO, N, -ONE, AP, X( 1, J ), 1, ONE, WORK( N+1 ),
189:      $               1 )
190: *
191: *        Compute componentwise relative backward error from formula
192: *
193: *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
194: *
195: *        where abs(Z) is the componentwise absolute value of the matrix
196: *        or vector Z.  If the i-th component of the denominator is less
197: *        than SAFE2, then SAFE1 is added to the i-th components of the
198: *        numerator and denominator before dividing.
199: *
200:          DO 30 I = 1, N
201:             WORK( I ) = ABS( B( I, J ) )
202:    30    CONTINUE
203: *
204: *        Compute abs(A)*abs(X) + abs(B).
205: *
206:          KK = 1
207:          IF( UPPER ) THEN
208:             DO 50 K = 1, N
209:                S = ZERO
210:                XK = ABS( X( K, J ) )
211:                IK = KK
212:                DO 40 I = 1, K - 1
213:                   WORK( I ) = WORK( I ) + ABS( AP( IK ) )*XK
214:                   S = S + ABS( AP( IK ) )*ABS( X( I, J ) )
215:                   IK = IK + 1
216:    40          CONTINUE
217:                WORK( K ) = WORK( K ) + ABS( AP( KK+K-1 ) )*XK + S
218:                KK = KK + K
219:    50       CONTINUE
220:          ELSE
221:             DO 70 K = 1, N
222:                S = ZERO
223:                XK = ABS( X( K, J ) )
224:                WORK( K ) = WORK( K ) + ABS( AP( KK ) )*XK
225:                IK = KK + 1
226:                DO 60 I = K + 1, N
227:                   WORK( I ) = WORK( I ) + ABS( AP( IK ) )*XK
228:                   S = S + ABS( AP( IK ) )*ABS( X( I, J ) )
229:                   IK = IK + 1
230:    60          CONTINUE
231:                WORK( K ) = WORK( K ) + S
232:                KK = KK + ( N-K+1 )
233:    70       CONTINUE
234:          END IF
235:          S = ZERO
236:          DO 80 I = 1, N
237:             IF( WORK( I ).GT.SAFE2 ) THEN
238:                S = MAX( S, ABS( WORK( N+I ) ) / WORK( I ) )
239:             ELSE
240:                S = MAX( S, ( ABS( WORK( N+I ) )+SAFE1 ) /
241:      $             ( WORK( I )+SAFE1 ) )
242:             END IF
243:    80    CONTINUE
244:          BERR( J ) = S
245: *
246: *        Test stopping criterion. Continue iterating if
247: *           1) The residual BERR(J) is larger than machine epsilon, and
248: *           2) BERR(J) decreased by at least a factor of 2 during the
249: *              last iteration, and
250: *           3) At most ITMAX iterations tried.
251: *
252:          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
253:      $       COUNT.LE.ITMAX ) THEN
254: *
255: *           Update solution and try again.
256: *
257:             CALL SSPTRS( UPLO, N, 1, AFP, IPIV, WORK( N+1 ), N, INFO )
258:             CALL SAXPY( N, ONE, WORK( N+1 ), 1, X( 1, J ), 1 )
259:             LSTRES = BERR( J )
260:             COUNT = COUNT + 1
261:             GO TO 20
262:          END IF
263: *
264: *        Bound error from formula
265: *
266: *        norm(X - XTRUE) / norm(X) .le. FERR =
267: *        norm( abs(inv(A))*
268: *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
269: *
270: *        where
271: *          norm(Z) is the magnitude of the largest component of Z
272: *          inv(A) is the inverse of A
273: *          abs(Z) is the componentwise absolute value of the matrix or
274: *             vector Z
275: *          NZ is the maximum number of nonzeros in any row of A, plus 1
276: *          EPS is machine epsilon
277: *
278: *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
279: *        is incremented by SAFE1 if the i-th component of
280: *        abs(A)*abs(X) + abs(B) is less than SAFE2.
281: *
282: *        Use SLACN2 to estimate the infinity-norm of the matrix
283: *           inv(A) * diag(W),
284: *        where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
285: *
286:          DO 90 I = 1, N
287:             IF( WORK( I ).GT.SAFE2 ) THEN
288:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I )
289:             ELSE
290:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I ) + SAFE1
291:             END IF
292:    90    CONTINUE
293: *
294:          KASE = 0
295:   100    CONTINUE
296:          CALL SLACN2( N, WORK( 2*N+1 ), WORK( N+1 ), IWORK, FERR( J ),
297:      $                KASE, ISAVE )
298:          IF( KASE.NE.0 ) THEN
299:             IF( KASE.EQ.1 ) THEN
300: *
301: *              Multiply by diag(W)*inv(A').
302: *
303:                CALL SSPTRS( UPLO, N, 1, AFP, IPIV, WORK( N+1 ), N,
304:      $                      INFO )
305:                DO 110 I = 1, N
306:                   WORK( N+I ) = WORK( I )*WORK( N+I )
307:   110          CONTINUE
308:             ELSE IF( KASE.EQ.2 ) THEN
309: *
310: *              Multiply by inv(A)*diag(W).
311: *
312:                DO 120 I = 1, N
313:                   WORK( N+I ) = WORK( I )*WORK( N+I )
314:   120          CONTINUE
315:                CALL SSPTRS( UPLO, N, 1, AFP, IPIV, WORK( N+1 ), N,
316:      $                      INFO )
317:             END IF
318:             GO TO 100
319:          END IF
320: *
321: *        Normalize error.
322: *
323:          LSTRES = ZERO
324:          DO 130 I = 1, N
325:             LSTRES = MAX( LSTRES, ABS( X( I, J ) ) )
326:   130    CONTINUE
327:          IF( LSTRES.NE.ZERO )
328:      $      FERR( J ) = FERR( J ) / LSTRES
329: *
330:   140 CONTINUE
331: *
332:       RETURN
333: *
334: *     End of SSPRFS
335: *
336:       END
337: