1       SUBROUTINE DPPRFS( UPLO, N, NRHS, AP, AFP, B, LDB, X, LDX, FERR,
  2      $                   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, LDB, LDX, N, NRHS
 14 *     ..
 15 *     .. Array Arguments ..
 16       INTEGER            IWORK( * )
 17       DOUBLE PRECISION   AFP( * ), AP( * ), B( LDB, * ), BERR( * ),
 18      $                   FERR( * ), WORK( * ), X( LDX, * )
 19 *     ..
 20 *
 21 *  Purpose
 22 *  =======
 23 *
 24 *  DPPRFS improves the computed solution to a system of linear
 25 *  equations when the coefficient matrix is symmetric positive definite
 26 *  and packed, and provides error bounds and backward error estimates
 27 *  for the 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 *  AP      (input) DOUBLE PRECISION array, dimension (N*(N+1)/2)
 44 *          The upper or lower triangle of the symmetric matrix A, packed
 45 *          columnwise in a linear array.  The j-th column of A is stored
 46 *          in the array AP as follows:
 47 *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
 48 *          if UPLO = 'L', AP(i + (j-1)*(2n-j)/2) = A(i,j) for j<=i<=n.
 49 *
 50 *  AFP     (input) DOUBLE PRECISION array, dimension (N*(N+1)/2)
 51 *          The triangular factor U or L from the Cholesky factorization
 52 *          A = U**T*U or A = L*L**T, as computed by DPPTRF/ZPPTRF,
 53 *          packed columnwise in a linear array in the same format as A
 54 *          (see AP).
 55 *
 56 *  B       (input) DOUBLE PRECISION array, dimension (LDB,NRHS)
 57 *          The right hand side matrix B.
 58 *
 59 *  LDB     (input) INTEGER
 60 *          The leading dimension of the array B.  LDB >= max(1,N).
 61 *
 62 *  X       (input/output) DOUBLE PRECISION array, dimension (LDX,NRHS)
 63 *          On entry, the solution matrix X, as computed by DPPTRS.
 64 *          On exit, the improved solution matrix X.
 65 *
 66 *  LDX     (input) INTEGER
 67 *          The leading dimension of the array X.  LDX >= max(1,N).
 68 *
 69 *  FERR    (output) DOUBLE PRECISION array, dimension (NRHS)
 70 *          The estimated forward error bound for each solution vector
 71 *          X(j) (the j-th column of the solution matrix X).
 72 *          If XTRUE is the true solution corresponding to X(j), FERR(j)
 73 *          is an estimated upper bound for the magnitude of the largest
 74 *          element in (X(j) - XTRUE) divided by the magnitude of the
 75 *          largest element in X(j).  The estimate is as reliable as
 76 *          the estimate for RCOND, and is almost always a slight
 77 *          overestimate of the true error.
 78 *
 79 *  BERR    (output) DOUBLE PRECISION array, dimension (NRHS)
 80 *          The componentwise relative backward error of each solution
 81 *          vector X(j) (i.e., the smallest relative change in
 82 *          any element of A or B that makes X(j) an exact solution).
 83 *
 84 *  WORK    (workspace) DOUBLE PRECISION array, dimension (3*N)
 85 *
 86 *  IWORK   (workspace) INTEGER array, dimension (N)
 87 *
 88 *  INFO    (output) INTEGER
 89 *          = 0:  successful exit
 90 *          < 0:  if INFO = -i, the i-th argument had an illegal value
 91 *
 92 *  Internal Parameters
 93 *  ===================
 94 *
 95 *  ITMAX is the maximum number of steps of iterative refinement.
 96 *
 97 *  =====================================================================
 98 *
 99 *     .. Parameters ..
100       INTEGER            ITMAX
101       PARAMETER          ( ITMAX = 5 )
102       DOUBLE PRECISION   ZERO
103       PARAMETER          ( ZERO = 0.0D+0 )
104       DOUBLE PRECISION   ONE
105       PARAMETER          ( ONE = 1.0D+0 )
106       DOUBLE PRECISION   TWO
107       PARAMETER          ( TWO = 2.0D+0 )
108       DOUBLE PRECISION   THREE
109       PARAMETER          ( THREE = 3.0D+0 )
110 *     ..
111 *     .. Local Scalars ..
112       LOGICAL            UPPER
113       INTEGER            COUNT, I, IK, J, K, KASE, KK, NZ
114       DOUBLE PRECISION   EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
115 *     ..
116 *     .. Local Arrays ..
117       INTEGER            ISAVE( 3 )
118 *     ..
119 *     .. External Subroutines ..
120       EXTERNAL           DAXPY, DCOPY, DLACN2, DPPTRS, DSPMV, XERBLA
121 *     ..
122 *     .. Intrinsic Functions ..
123       INTRINSIC          ABSMAX
124 *     ..
125 *     .. External Functions ..
126       LOGICAL            LSAME
127       DOUBLE PRECISION   DLAMCH
128       EXTERNAL           LSAME, DLAMCH
129 *     ..
130 *     .. Executable Statements ..
131 *
132 *     Test the input parameters.
133 *
134       INFO = 0
135       UPPER = LSAME( UPLO, 'U' )
136       IF.NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
137          INFO = -1
138       ELSE IF( N.LT.0 ) THEN
139          INFO = -2
140       ELSE IF( NRHS.LT.0 ) THEN
141          INFO = -3
142       ELSE IF( LDB.LT.MAX1, N ) ) THEN
143          INFO = -7
144       ELSE IF( LDX.LT.MAX1, N ) ) THEN
145          INFO = -9
146       END IF
147       IF( INFO.NE.0 ) THEN
148          CALL XERBLA( 'DPPRFS'-INFO )
149          RETURN
150       END IF
151 *
152 *     Quick return if possible
153 *
154       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
155          DO 10 J = 1, NRHS
156             FERR( J ) = ZERO
157             BERR( J ) = ZERO
158    10    CONTINUE
159          RETURN
160       END IF
161 *
162 *     NZ = maximum number of nonzero elements in each row of A, plus 1
163 *
164       NZ = N + 1
165       EPS = DLAMCH( 'Epsilon' )
166       SAFMIN = DLAMCH( 'Safe minimum' )
167       SAFE1 = NZ*SAFMIN
168       SAFE2 = SAFE1 / EPS
169 *
170 *     Do for each right hand side
171 *
172       DO 140 J = 1, NRHS
173 *
174          COUNT = 1
175          LSTRES = THREE
176    20    CONTINUE
177 *
178 *        Loop until stopping criterion is satisfied.
179 *
180 *        Compute residual R = B - A * X
181 *
182          CALL DCOPY( N, B( 1, J ), 1, WORK( N+1 ), 1 )
183          CALL DSPMV( UPLO, N, -ONE, AP, X( 1, J ), 1, ONE, WORK( N+1 ),
184      $               1 )
185 *
186 *        Compute componentwise relative backward error from formula
187 *
188 *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
189 *
190 *        where abs(Z) is the componentwise absolute value of the matrix
191 *        or vector Z.  If the i-th component of the denominator is less
192 *        than SAFE2, then SAFE1 is added to the i-th components of the
193 *        numerator and denominator before dividing.
194 *
195          DO 30 I = 1, N
196             WORK( I ) = ABS( B( I, J ) )
197    30    CONTINUE
198 *
199 *        Compute abs(A)*abs(X) + abs(B).
200 *
201          KK = 1
202          IF( UPPER ) THEN
203             DO 50 K = 1, N
204                S = ZERO
205                XK = ABS( X( K, J ) )
206                IK = KK
207                DO 40 I = 1, K - 1
208                   WORK( I ) = WORK( I ) + ABS( AP( IK ) )*XK
209                   S = S + ABS( AP( IK ) )*ABS( X( I, J ) )
210                   IK = IK + 1
211    40          CONTINUE
212                WORK( K ) = WORK( K ) + ABS( AP( KK+K-1 ) )*XK + S
213                KK = KK + K
214    50       CONTINUE
215          ELSE
216             DO 70 K = 1, N
217                S = ZERO
218                XK = ABS( X( K, J ) )
219                WORK( K ) = WORK( K ) + ABS( AP( KK ) )*XK
220                IK = KK + 1
221                DO 60 I = K + 1, N
222                   WORK( I ) = WORK( I ) + ABS( AP( IK ) )*XK
223                   S = S + ABS( AP( IK ) )*ABS( X( I, J ) )
224                   IK = IK + 1
225    60          CONTINUE
226                WORK( K ) = WORK( K ) + S
227                KK = KK + ( N-K+1 )
228    70       CONTINUE
229          END IF
230          S = ZERO
231          DO 80 I = 1, N
232             IF( WORK( I ).GT.SAFE2 ) THEN
233                S = MAX( S, ABS( WORK( N+I ) ) / WORK( I ) )
234             ELSE
235                S = MAX( S, ( ABS( WORK( N+I ) )+SAFE1 ) /
236      $             ( WORK( I )+SAFE1 ) )
237             END IF
238    80    CONTINUE
239          BERR( J ) = S
240 *
241 *        Test stopping criterion. Continue iterating if
242 *           1) The residual BERR(J) is larger than machine epsilon, and
243 *           2) BERR(J) decreased by at least a factor of 2 during the
244 *              last iteration, and
245 *           3) At most ITMAX iterations tried.
246 *
247          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
248      $       COUNT.LE.ITMAX ) THEN
249 *
250 *           Update solution and try again.
251 *
252             CALL DPPTRS( UPLO, N, 1, AFP, WORK( N+1 ), N, INFO )
253             CALL DAXPY( N, ONE, WORK( N+1 ), 1, X( 1, J ), 1 )
254             LSTRES = BERR( J )
255             COUNT = COUNT + 1
256             GO TO 20
257          END IF
258 *
259 *        Bound error from formula
260 *
261 *        norm(X - XTRUE) / norm(X) .le. FERR =
262 *        norm( abs(inv(A))*
263 *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
264 *
265 *        where
266 *          norm(Z) is the magnitude of the largest component of Z
267 *          inv(A) is the inverse of A
268 *          abs(Z) is the componentwise absolute value of the matrix or
269 *             vector Z
270 *          NZ is the maximum number of nonzeros in any row of A, plus 1
271 *          EPS is machine epsilon
272 *
273 *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
274 *        is incremented by SAFE1 if the i-th component of
275 *        abs(A)*abs(X) + abs(B) is less than SAFE2.
276 *
277 *        Use DLACN2 to estimate the infinity-norm of the matrix
278 *           inv(A) * diag(W),
279 *        where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
280 *
281          DO 90 I = 1, N
282             IF( WORK( I ).GT.SAFE2 ) THEN
283                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I )
284             ELSE
285                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I ) + SAFE1
286             END IF
287    90    CONTINUE
288 *
289          KASE = 0
290   100    CONTINUE
291          CALL DLACN2( N, WORK( 2*N+1 ), WORK( N+1 ), IWORK, FERR( J ),
292      $                KASE, ISAVE )
293          IF( KASE.NE.0 ) THEN
294             IF( KASE.EQ.1 ) THEN
295 *
296 *              Multiply by diag(W)*inv(A**T).
297 *
298                CALL DPPTRS( UPLO, N, 1, AFP, WORK( N+1 ), N, INFO )
299                DO 110 I = 1, N
300                   WORK( N+I ) = WORK( I )*WORK( N+I )
301   110          CONTINUE
302             ELSE IF( KASE.EQ.2 ) THEN
303 *
304 *              Multiply by inv(A)*diag(W).
305 *
306                DO 120 I = 1, N
307                   WORK( N+I ) = WORK( I )*WORK( N+I )
308   120          CONTINUE
309                CALL DPPTRS( UPLO, N, 1, AFP, WORK( N+1 ), N, INFO )
310             END IF
311             GO TO 100
312          END IF
313 *
314 *        Normalize error.
315 *
316          LSTRES = ZERO
317          DO 130 I = 1, N
318             LSTRES = MAX( LSTRES, ABS( X( I, J ) ) )
319   130    CONTINUE
320          IF( LSTRES.NE.ZERO )
321      $      FERR( J ) = FERR( J ) / LSTRES
322 *
323   140 CONTINUE
324 *
325       RETURN
326 *
327 *     End of DPPRFS
328 *
329       END