001:       SUBROUTINE SPORFS( UPLO, N, NRHS, A, LDA, AF, LDAF, B, LDB, X,
002:      $                   LDX, 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 SLACN2 in place of SLACON, 7 Feb 03, SJH.
009: *
010: *     .. Scalar Arguments ..
011:       CHARACTER          UPLO
012:       INTEGER            INFO, LDA, LDAF, LDB, LDX, N, NRHS
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IWORK( * )
016:       REAL               A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
017:      $                   BERR( * ), FERR( * ), WORK( * ), X( LDX, * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  SPORFS improves the computed solution to a system of linear
024: *  equations when the coefficient matrix is symmetric positive definite,
025: *  and provides error bounds and backward error estimates for the
026: *  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: *  A       (input) REAL array, dimension (LDA,N)
043: *          The symmetric matrix A.  If UPLO = 'U', the leading N-by-N
044: *          upper triangular part of A contains the upper triangular part
045: *          of the matrix A, and the strictly lower triangular part of A
046: *          is not referenced.  If UPLO = 'L', the leading N-by-N lower
047: *          triangular part of A contains the lower triangular part of
048: *          the matrix A, and the strictly upper triangular part of A is
049: *          not referenced.
050: *
051: *  LDA     (input) INTEGER
052: *          The leading dimension of the array A.  LDA >= max(1,N).
053: *
054: *  AF      (input) REAL array, dimension (LDAF,N)
055: *          The triangular factor U or L from the Cholesky factorization
056: *          A = U**T*U or A = L*L**T, as computed by SPOTRF.
057: *
058: *  LDAF    (input) INTEGER
059: *          The leading dimension of the array AF.  LDAF >= max(1,N).
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 SPOTRS.
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, J, K, KASE, 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, SPOTRS, SSYMV, 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( LDA.LT.MAX( 1, N ) ) THEN
148:          INFO = -5
149:       ELSE IF( LDAF.LT.MAX( 1, N ) ) THEN
150:          INFO = -7
151:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
152:          INFO = -9
153:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
154:          INFO = -11
155:       END IF
156:       IF( INFO.NE.0 ) THEN
157:          CALL XERBLA( 'SPORFS', -INFO )
158:          RETURN
159:       END IF
160: *
161: *     Quick return if possible
162: *
163:       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
164:          DO 10 J = 1, NRHS
165:             FERR( J ) = ZERO
166:             BERR( J ) = ZERO
167:    10    CONTINUE
168:          RETURN
169:       END IF
170: *
171: *     NZ = maximum number of nonzero elements in each row of A, plus 1
172: *
173:       NZ = N + 1
174:       EPS = SLAMCH( 'Epsilon' )
175:       SAFMIN = SLAMCH( 'Safe minimum' )
176:       SAFE1 = NZ*SAFMIN
177:       SAFE2 = SAFE1 / EPS
178: *
179: *     Do for each right hand side
180: *
181:       DO 140 J = 1, NRHS
182: *
183:          COUNT = 1
184:          LSTRES = THREE
185:    20    CONTINUE
186: *
187: *        Loop until stopping criterion is satisfied.
188: *
189: *        Compute residual R = B - A * X
190: *
191:          CALL SCOPY( N, B( 1, J ), 1, WORK( N+1 ), 1 )
192:          CALL SSYMV( UPLO, N, -ONE, A, LDA, X( 1, J ), 1, ONE,
193:      $               WORK( N+1 ), 1 )
194: *
195: *        Compute componentwise relative backward error from formula
196: *
197: *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
198: *
199: *        where abs(Z) is the componentwise absolute value of the matrix
200: *        or vector Z.  If the i-th component of the denominator is less
201: *        than SAFE2, then SAFE1 is added to the i-th components of the
202: *        numerator and denominator before dividing.
203: *
204:          DO 30 I = 1, N
205:             WORK( I ) = ABS( B( I, J ) )
206:    30    CONTINUE
207: *
208: *        Compute abs(A)*abs(X) + abs(B).
209: *
210:          IF( UPPER ) THEN
211:             DO 50 K = 1, N
212:                S = ZERO
213:                XK = ABS( X( K, J ) )
214:                DO 40 I = 1, K - 1
215:                   WORK( I ) = WORK( I ) + ABS( A( I, K ) )*XK
216:                   S = S + ABS( A( I, K ) )*ABS( X( I, J ) )
217:    40          CONTINUE
218:                WORK( K ) = WORK( K ) + ABS( A( K, K ) )*XK + S
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( A( K, K ) )*XK
225:                DO 60 I = K + 1, N
226:                   WORK( I ) = WORK( I ) + ABS( A( I, K ) )*XK
227:                   S = S + ABS( A( I, K ) )*ABS( X( I, J ) )
228:    60          CONTINUE
229:                WORK( K ) = WORK( K ) + S
230:    70       CONTINUE
231:          END IF
232:          S = ZERO
233:          DO 80 I = 1, N
234:             IF( WORK( I ).GT.SAFE2 ) THEN
235:                S = MAX( S, ABS( WORK( N+I ) ) / WORK( I ) )
236:             ELSE
237:                S = MAX( S, ( ABS( WORK( N+I ) )+SAFE1 ) /
238:      $             ( WORK( I )+SAFE1 ) )
239:             END IF
240:    80    CONTINUE
241:          BERR( J ) = S
242: *
243: *        Test stopping criterion. Continue iterating if
244: *           1) The residual BERR(J) is larger than machine epsilon, and
245: *           2) BERR(J) decreased by at least a factor of 2 during the
246: *              last iteration, and
247: *           3) At most ITMAX iterations tried.
248: *
249:          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
250:      $       COUNT.LE.ITMAX ) THEN
251: *
252: *           Update solution and try again.
253: *
254:             CALL SPOTRS( UPLO, N, 1, AF, LDAF, WORK( N+1 ), N, INFO )
255:             CALL SAXPY( N, ONE, WORK( N+1 ), 1, X( 1, J ), 1 )
256:             LSTRES = BERR( J )
257:             COUNT = COUNT + 1
258:             GO TO 20
259:          END IF
260: *
261: *        Bound error from formula
262: *
263: *        norm(X - XTRUE) / norm(X) .le. FERR =
264: *        norm( abs(inv(A))*
265: *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
266: *
267: *        where
268: *          norm(Z) is the magnitude of the largest component of Z
269: *          inv(A) is the inverse of A
270: *          abs(Z) is the componentwise absolute value of the matrix or
271: *             vector Z
272: *          NZ is the maximum number of nonzeros in any row of A, plus 1
273: *          EPS is machine epsilon
274: *
275: *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
276: *        is incremented by SAFE1 if the i-th component of
277: *        abs(A)*abs(X) + abs(B) is less than SAFE2.
278: *
279: *        Use SLACN2 to estimate the infinity-norm of the matrix
280: *           inv(A) * diag(W),
281: *        where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
282: *
283:          DO 90 I = 1, N
284:             IF( WORK( I ).GT.SAFE2 ) THEN
285:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I )
286:             ELSE
287:                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I ) + SAFE1
288:             END IF
289:    90    CONTINUE
290: *
291:          KASE = 0
292:   100    CONTINUE
293:          CALL SLACN2( N, WORK( 2*N+1 ), WORK( N+1 ), IWORK, FERR( J ),
294:      $                KASE, ISAVE )
295:          IF( KASE.NE.0 ) THEN
296:             IF( KASE.EQ.1 ) THEN
297: *
298: *              Multiply by diag(W)*inv(A').
299: *
300:                CALL SPOTRS( UPLO, N, 1, AF, LDAF, WORK( N+1 ), N, INFO )
301:                DO 110 I = 1, N
302:                   WORK( N+I ) = WORK( I )*WORK( N+I )
303:   110          CONTINUE
304:             ELSE IF( KASE.EQ.2 ) THEN
305: *
306: *              Multiply by inv(A)*diag(W).
307: *
308:                DO 120 I = 1, N
309:                   WORK( N+I ) = WORK( I )*WORK( N+I )
310:   120          CONTINUE
311:                CALL SPOTRS( UPLO, N, 1, AF, LDAF, WORK( N+1 ), N, INFO )
312:             END IF
313:             GO TO 100
314:          END IF
315: *
316: *        Normalize error.
317: *
318:          LSTRES = ZERO
319:          DO 130 I = 1, N
320:             LSTRES = MAX( LSTRES, ABS( X( I, J ) ) )
321:   130    CONTINUE
322:          IF( LSTRES.NE.ZERO )
323:      $      FERR( J ) = FERR( J ) / LSTRES
324: *
325:   140 CONTINUE
326: *
327:       RETURN
328: *
329: *     End of SPORFS
330: *
331:       END
332: