1       SUBROUTINE ZPPRFS( UPLO, N, NRHS, AP, AFP, B, LDB, X, LDX, FERR,
  2      $                   BERR, WORK, RWORK, 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 ZLACN2 in place of ZLACON, 10 Feb 03, SJH.
 10 *
 11 *     .. Scalar Arguments ..
 12       CHARACTER          UPLO
 13       INTEGER            INFO, LDB, LDX, N, NRHS
 14 *     ..
 15 *     .. Array Arguments ..
 16       DOUBLE PRECISION   BERR( * ), FERR( * ), RWORK( * )
 17       COMPLEX*16         AFP( * ), AP( * ), B( LDB, * ), WORK( * ),
 18      $                   X( LDX, * )
 19 *     ..
 20 *
 21 *  Purpose
 22 *  =======
 23 *
 24 *  ZPPRFS improves the computed solution to a system of linear
 25 *  equations when the coefficient matrix is Hermitian 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) COMPLEX*16 array, dimension (N*(N+1)/2)
 44 *          The upper or lower triangle of the Hermitian 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) COMPLEX*16 array, dimension (N*(N+1)/2)
 51 *          The triangular factor U or L from the Cholesky factorization
 52 *          A = U**H*U or A = L*L**H, 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) COMPLEX*16 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) COMPLEX*16 array, dimension (LDX,NRHS)
 63 *          On entry, the solution matrix X, as computed by ZPPTRS.
 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) COMPLEX*16 array, dimension (2*N)
 85 *
 86 *  RWORK   (workspace) DOUBLE PRECISION 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       COMPLEX*16         CONE
105       PARAMETER          ( CONE = ( 1.0D+00.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       COMPLEX*16         ZDUM
116 *     ..
117 *     .. Local Arrays ..
118       INTEGER            ISAVE( 3 )
119 *     ..
120 *     .. External Subroutines ..
121       EXTERNAL           XERBLA, ZAXPY, ZCOPY, ZHPMV, ZLACN2, ZPPTRS
122 *     ..
123 *     .. Intrinsic Functions ..
124       INTRINSIC          ABSDBLEDIMAGMAX
125 *     ..
126 *     .. External Functions ..
127       LOGICAL            LSAME
128       DOUBLE PRECISION   DLAMCH
129       EXTERNAL           LSAME, DLAMCH
130 *     ..
131 *     .. Statement Functions ..
132       DOUBLE PRECISION   CABS1
133 *     ..
134 *     .. Statement Function definitions ..
135       CABS1( ZDUM ) = ABSDBLE( ZDUM ) ) + ABSDIMAG( ZDUM ) )
136 *     ..
137 *     .. Executable Statements ..
138 *
139 *     Test the input parameters.
140 *
141       INFO = 0
142       UPPER = LSAME( UPLO, 'U' )
143       IF.NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
144          INFO = -1
145       ELSE IF( N.LT.0 ) THEN
146          INFO = -2
147       ELSE IF( NRHS.LT.0 ) THEN
148          INFO = -3
149       ELSE IF( LDB.LT.MAX1, N ) ) THEN
150          INFO = -7
151       ELSE IF( LDX.LT.MAX1, N ) ) THEN
152          INFO = -9
153       END IF
154       IF( INFO.NE.0 ) THEN
155          CALL XERBLA( 'ZPPRFS'-INFO )
156          RETURN
157       END IF
158 *
159 *     Quick return if possible
160 *
161       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
162          DO 10 J = 1, NRHS
163             FERR( J ) = ZERO
164             BERR( J ) = ZERO
165    10    CONTINUE
166          RETURN
167       END IF
168 *
169 *     NZ = maximum number of nonzero elements in each row of A, plus 1
170 *
171       NZ = N + 1
172       EPS = DLAMCH( 'Epsilon' )
173       SAFMIN = DLAMCH( 'Safe minimum' )
174       SAFE1 = NZ*SAFMIN
175       SAFE2 = SAFE1 / EPS
176 *
177 *     Do for each right hand side
178 *
179       DO 140 J = 1, NRHS
180 *
181          COUNT = 1
182          LSTRES = THREE
183    20    CONTINUE
184 *
185 *        Loop until stopping criterion is satisfied.
186 *
187 *        Compute residual R = B - A * X
188 *
189          CALL ZCOPY( N, B( 1, J ), 1, WORK, 1 )
190          CALL ZHPMV( UPLO, N, -CONE, AP, X( 1, J ), 1, CONE, WORK, 1 )
191 *
192 *        Compute componentwise relative backward error from formula
193 *
194 *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
195 *
196 *        where abs(Z) is the componentwise absolute value of the matrix
197 *        or vector Z.  If the i-th component of the denominator is less
198 *        than SAFE2, then SAFE1 is added to the i-th components of the
199 *        numerator and denominator before dividing.
200 *
201          DO 30 I = 1, N
202             RWORK( I ) = CABS1( B( I, J ) )
203    30    CONTINUE
204 *
205 *        Compute abs(A)*abs(X) + abs(B).
206 *
207          KK = 1
208          IF( UPPER ) THEN
209             DO 50 K = 1, N
210                S = ZERO
211                XK = CABS1( X( K, J ) )
212                IK = KK
213                DO 40 I = 1, K - 1
214                   RWORK( I ) = RWORK( I ) + CABS1( AP( IK ) )*XK
215                   S = S + CABS1( AP( IK ) )*CABS1( X( I, J ) )
216                   IK = IK + 1
217    40          CONTINUE
218                RWORK( K ) = RWORK( K ) + ABSDBLE( AP( KK+K-1 ) ) )*
219      $                      XK + S
220                KK = KK + K
221    50       CONTINUE
222          ELSE
223             DO 70 K = 1, N
224                S = ZERO
225                XK = CABS1( X( K, J ) )
226                RWORK( K ) = RWORK( K ) + ABSDBLE( AP( KK ) ) )*XK
227                IK = KK + 1
228                DO 60 I = K + 1, N
229                   RWORK( I ) = RWORK( I ) + CABS1( AP( IK ) )*XK
230                   S = S + CABS1( AP( IK ) )*CABS1( X( I, J ) )
231                   IK = IK + 1
232    60          CONTINUE
233                RWORK( K ) = RWORK( K ) + S
234                KK = KK + ( N-K+1 )
235    70       CONTINUE
236          END IF
237          S = ZERO
238          DO 80 I = 1, N
239             IF( RWORK( I ).GT.SAFE2 ) THEN
240                S = MAX( S, CABS1( WORK( I ) ) / RWORK( I ) )
241             ELSE
242                S = MAX( S, ( CABS1( WORK( I ) )+SAFE1 ) /
243      $             ( RWORK( I )+SAFE1 ) )
244             END IF
245    80    CONTINUE
246          BERR( J ) = S
247 *
248 *        Test stopping criterion. Continue iterating if
249 *           1) The residual BERR(J) is larger than machine epsilon, and
250 *           2) BERR(J) decreased by at least a factor of 2 during the
251 *              last iteration, and
252 *           3) At most ITMAX iterations tried.
253 *
254          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
255      $       COUNT.LE.ITMAX ) THEN
256 *
257 *           Update solution and try again.
258 *
259             CALL ZPPTRS( UPLO, N, 1, AFP, WORK, N, INFO )
260             CALL ZAXPY( N, CONE, WORK, 1, X( 1, J ), 1 )
261             LSTRES = BERR( J )
262             COUNT = COUNT + 1
263             GO TO 20
264          END IF
265 *
266 *        Bound error from formula
267 *
268 *        norm(X - XTRUE) / norm(X) .le. FERR =
269 *        norm( abs(inv(A))*
270 *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
271 *
272 *        where
273 *          norm(Z) is the magnitude of the largest component of Z
274 *          inv(A) is the inverse of A
275 *          abs(Z) is the componentwise absolute value of the matrix or
276 *             vector Z
277 *          NZ is the maximum number of nonzeros in any row of A, plus 1
278 *          EPS is machine epsilon
279 *
280 *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
281 *        is incremented by SAFE1 if the i-th component of
282 *        abs(A)*abs(X) + abs(B) is less than SAFE2.
283 *
284 *        Use ZLACN2 to estimate the infinity-norm of the matrix
285 *           inv(A) * diag(W),
286 *        where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
287 *
288          DO 90 I = 1, N
289             IF( RWORK( I ).GT.SAFE2 ) THEN
290                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I )
291             ELSE
292                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I ) +
293      $                      SAFE1
294             END IF
295    90    CONTINUE
296 *
297          KASE = 0
298   100    CONTINUE
299          CALL ZLACN2( N, WORK( N+1 ), WORK, FERR( J ), KASE, ISAVE )
300          IF( KASE.NE.0 ) THEN
301             IF( KASE.EQ.1 ) THEN
302 *
303 *              Multiply by diag(W)*inv(A**H).
304 *
305                CALL ZPPTRS( UPLO, N, 1, AFP, WORK, N, INFO )
306                DO 110 I = 1, N
307                   WORK( I ) = RWORK( I )*WORK( I )
308   110          CONTINUE
309             ELSE IF( KASE.EQ.2 ) THEN
310 *
311 *              Multiply by inv(A)*diag(W).
312 *
313                DO 120 I = 1, N
314                   WORK( I ) = RWORK( I )*WORK( I )
315   120          CONTINUE
316                CALL ZPPTRS( UPLO, N, 1, AFP, WORK, N, 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, CABS1( 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 ZPPRFS
335 *
336       END