1       SUBROUTINE DPORFS( UPLO, N, NRHS, A, LDA, AF, LDAF, B, LDB, X,
  2      $                   LDX, FERR, BERR, WORK, IWORK, INFO )
  3 *
  4 *  -- LAPACK routine (version 3.2) --
  5 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
  6 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  7 *     November 2006
  8 *
  9 *     Modified to call DLACN2 in place of DLACON, 5 Feb 03, SJH.
 10 *
 11 *     .. Scalar Arguments ..
 12       CHARACTER          UPLO
 13       INTEGER            INFO, LDA, LDAF, LDB, LDX, N, NRHS
 14 *     ..
 15 *     .. Array Arguments ..
 16       INTEGER            IWORK( * )
 17       DOUBLE PRECISION   A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
 18      $                   BERR( * ), FERR( * ), WORK( * ), X( LDX, * )
 19 *     ..
 20 *
 21 *  Purpose
 22 *  =======
 23 *
 24 *  DPORFS improves the computed solution to a system of linear
 25 *  equations when the coefficient matrix is symmetric positive definite,
 26 *  and provides error bounds and backward error estimates for the
 27 *  solution.
 28 *
 29 *  Arguments
 30 *  =========
 31 *
 32 *  UPLO    (input) CHARACTER*1
 33 *          = 'U':  Upper triangle of A is stored;
 34 *          = 'L':  Lower triangle of A is stored.
 35 *
 36 *  N       (input) INTEGER
 37 *          The order of the matrix A.  N >= 0.
 38 *
 39 *  NRHS    (input) INTEGER
 40 *          The number of right hand sides, i.e., the number of columns
 41 *          of the matrices B and X.  NRHS >= 0.
 42 *
 43 *  A       (input) DOUBLE PRECISION array, dimension (LDA,N)
 44 *          The symmetric matrix A.  If UPLO = 'U', the leading N-by-N
 45 *          upper triangular part of A contains the upper triangular part
 46 *          of the matrix A, and the strictly lower triangular part of A
 47 *          is not referenced.  If UPLO = 'L', the leading N-by-N lower
 48 *          triangular part of A contains the lower triangular part of
 49 *          the matrix A, and the strictly upper triangular part of A is
 50 *          not referenced.
 51 *
 52 *  LDA     (input) INTEGER
 53 *          The leading dimension of the array A.  LDA >= max(1,N).
 54 *
 55 *  AF      (input) DOUBLE PRECISION array, dimension (LDAF,N)
 56 *          The triangular factor U or L from the Cholesky factorization
 57 *          A = U**T*U or A = L*L**T, as computed by DPOTRF.
 58 *
 59 *  LDAF    (input) INTEGER
 60 *          The leading dimension of the array AF.  LDAF >= max(1,N).
 61 *
 62 *  B       (input) DOUBLE PRECISION array, dimension (LDB,NRHS)
 63 *          The right hand side matrix B.
 64 *
 65 *  LDB     (input) INTEGER
 66 *          The leading dimension of the array B.  LDB >= max(1,N).
 67 *
 68 *  X       (input/output) DOUBLE PRECISION array, dimension (LDX,NRHS)
 69 *          On entry, the solution matrix X, as computed by DPOTRS.
 70 *          On exit, the improved solution matrix X.
 71 *
 72 *  LDX     (input) INTEGER
 73 *          The leading dimension of the array X.  LDX >= max(1,N).
 74 *
 75 *  FERR    (output) DOUBLE PRECISION array, dimension (NRHS)
 76 *          The estimated forward error bound for each solution vector
 77 *          X(j) (the j-th column of the solution matrix X).
 78 *          If XTRUE is the true solution corresponding to X(j), FERR(j)
 79 *          is an estimated upper bound for the magnitude of the largest
 80 *          element in (X(j) - XTRUE) divided by the magnitude of the
 81 *          largest element in X(j).  The estimate is as reliable as
 82 *          the estimate for RCOND, and is almost always a slight
 83 *          overestimate of the true error.
 84 *
 85 *  BERR    (output) DOUBLE PRECISION array, dimension (NRHS)
 86 *          The componentwise relative backward error of each solution
 87 *          vector X(j) (i.e., the smallest relative change in
 88 *          any element of A or B that makes X(j) an exact solution).
 89 *
 90 *  WORK    (workspace) DOUBLE PRECISION array, dimension (3*N)
 91 *
 92 *  IWORK   (workspace) INTEGER array, dimension (N)
 93 *
 94 *  INFO    (output) INTEGER
 95 *          = 0:  successful exit
 96 *          < 0:  if INFO = -i, the i-th argument had an illegal value
 97 *
 98 *  Internal Parameters
 99 *  ===================
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       DOUBLE PRECISION   ZERO
109       PARAMETER          ( ZERO = 0.0D+0 )
110       DOUBLE PRECISION   ONE
111       PARAMETER          ( ONE = 1.0D+0 )
112       DOUBLE PRECISION   TWO
113       PARAMETER          ( TWO = 2.0D+0 )
114       DOUBLE PRECISION   THREE
115       PARAMETER          ( THREE = 3.0D+0 )
116 *     ..
117 *     .. Local Scalars ..
118       LOGICAL            UPPER
119       INTEGER            COUNT, I, J, K, KASE, NZ
120       DOUBLE PRECISION   EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
121 *     ..
122 *     .. Local Arrays ..
123       INTEGER            ISAVE( 3 )
124 *     ..
125 *     .. External Subroutines ..
126       EXTERNAL           DAXPY, DCOPY, DLACN2, DPOTRS, DSYMV, XERBLA
127 *     ..
128 *     .. Intrinsic Functions ..
129       INTRINSIC          ABSMAX
130 *     ..
131 *     .. External Functions ..
132       LOGICAL            LSAME
133       DOUBLE PRECISION   DLAMCH
134       EXTERNAL           LSAME, DLAMCH
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.MAX1, N ) ) THEN
149          INFO = -5
150       ELSE IF( LDAF.LT.MAX1, N ) ) THEN
151          INFO = -7
152       ELSE IF( LDB.LT.MAX1, N ) ) THEN
153          INFO = -9
154       ELSE IF( LDX.LT.MAX1, N ) ) THEN
155          INFO = -11
156       END IF
157       IF( INFO.NE.0 ) THEN
158          CALL XERBLA( 'DPORFS'-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 = DLAMCH( 'Epsilon' )
176       SAFMIN = DLAMCH( '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 DCOPY( N, B( 1, J ), 1, WORK( N+1 ), 1 )
193          CALL DSYMV( 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 DPOTRS( UPLO, N, 1, AF, LDAF, WORK( N+1 ), N, INFO )
256             CALL DAXPY( 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 DLACN2 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 DLACN2( 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**T).
300 *
301                CALL DPOTRS( 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 DPOTRS( 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 DPORFS
331 *
332       END