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