1       SUBROUTINE ZHPRFS( UPLO, N, NRHS, AP, AFP, IPIV, B, LDB, X, LDX,
  2      $                   FERR, 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       INTEGER            IPIV( * )
 17       DOUBLE PRECISION   BERR( * ), FERR( * ), RWORK( * )
 18       COMPLEX*16         AFP( * ), AP( * ), B( LDB, * ), WORK( * ),
 19      $                   X( LDX, * )
 20 *     ..
 21 *
 22 *  Purpose
 23 *  =======
 24 *
 25 *  ZHPRFS improves the computed solution to a system of linear
 26 *  equations when the coefficient matrix is Hermitian indefinite
 27 *  and packed, and provides error bounds and backward error estimates
 28 *  for the solution.
 29 *
 30 *  Arguments
 31 *  =========
 32 *
 33 *  UPLO    (input) CHARACTER*1
 34 *          = 'U':  Upper triangle of A is stored;
 35 *          = 'L':  Lower triangle of A is stored.
 36 *
 37 *  N       (input) INTEGER
 38 *          The order of the matrix A.  N >= 0.
 39 *
 40 *  NRHS    (input) INTEGER
 41 *          The number of right hand sides, i.e., the number of columns
 42 *          of the matrices B and X.  NRHS >= 0.
 43 *
 44 *  AP      (input) COMPLEX*16 array, dimension (N*(N+1)/2)
 45 *          The upper or lower triangle of the Hermitian matrix A, packed
 46 *          columnwise in a linear array.  The j-th column of A is stored
 47 *          in the array AP as follows:
 48 *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
 49 *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
 50 *
 51 *  AFP     (input) COMPLEX*16 array, dimension (N*(N+1)/2)
 52 *          The factored form of the matrix A.  AFP contains the block
 53 *          diagonal matrix D and the multipliers used to obtain the
 54 *          factor U or L from the factorization A = U*D*U**H or
 55 *          A = L*D*L**H as computed by ZHPTRF, stored as a packed
 56 *          triangular matrix.
 57 *
 58 *  IPIV    (input) INTEGER array, dimension (N)
 59 *          Details of the interchanges and the block structure of D
 60 *          as determined by ZHPTRF.
 61 *
 62 *  B       (input) COMPLEX*16 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) COMPLEX*16 array, dimension (LDX,NRHS)
 69 *          On entry, the solution matrix X, as computed by ZHPTRS.
 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) COMPLEX*16 array, dimension (2*N)
 91 *
 92 *  RWORK   (workspace) DOUBLE PRECISION 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       COMPLEX*16         ONE
111       PARAMETER          ( ONE = ( 1.0D+00.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, IK, J, K, KASE, KK, NZ
120       DOUBLE PRECISION   EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
121       COMPLEX*16         ZDUM
122 *     ..
123 *     .. Local Arrays ..
124       INTEGER            ISAVE( 3 )
125 *     ..
126 *     .. External Subroutines ..
127       EXTERNAL           XERBLA, ZAXPY, ZCOPY, ZHPMV, ZHPTRS, ZLACN2
128 *     ..
129 *     .. Intrinsic Functions ..
130       INTRINSIC          ABSDBLEDIMAGMAX
131 *     ..
132 *     .. External Functions ..
133       LOGICAL            LSAME
134       DOUBLE PRECISION   DLAMCH
135       EXTERNAL           LSAME, DLAMCH
136 *     ..
137 *     .. Statement Functions ..
138       DOUBLE PRECISION   CABS1
139 *     ..
140 *     .. Statement Function definitions ..
141       CABS1( ZDUM ) = ABSDBLE( ZDUM ) ) + ABSDIMAG( ZDUM ) )
142 *     ..
143 *     .. Executable Statements ..
144 *
145 *     Test the input parameters.
146 *
147       INFO = 0
148       UPPER = LSAME( UPLO, 'U' )
149       IF.NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
150          INFO = -1
151       ELSE IF( N.LT.0 ) THEN
152          INFO = -2
153       ELSE IF( NRHS.LT.0 ) THEN
154          INFO = -3
155       ELSE IF( LDB.LT.MAX1, N ) ) THEN
156          INFO = -8
157       ELSE IF( LDX.LT.MAX1, N ) ) THEN
158          INFO = -10
159       END IF
160       IF( INFO.NE.0 ) THEN
161          CALL XERBLA( 'ZHPRFS'-INFO )
162          RETURN
163       END IF
164 *
165 *     Quick return if possible
166 *
167       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
168          DO 10 J = 1, NRHS
169             FERR( J ) = ZERO
170             BERR( J ) = ZERO
171    10    CONTINUE
172          RETURN
173       END IF
174 *
175 *     NZ = maximum number of nonzero elements in each row of A, plus 1
176 *
177       NZ = N + 1
178       EPS = DLAMCH( 'Epsilon' )
179       SAFMIN = DLAMCH( 'Safe minimum' )
180       SAFE1 = NZ*SAFMIN
181       SAFE2 = SAFE1 / EPS
182 *
183 *     Do for each right hand side
184 *
185       DO 140 J = 1, NRHS
186 *
187          COUNT = 1
188          LSTRES = THREE
189    20    CONTINUE
190 *
191 *        Loop until stopping criterion is satisfied.
192 *
193 *        Compute residual R = B - A * X
194 *
195          CALL ZCOPY( N, B( 1, J ), 1, WORK, 1 )
196          CALL ZHPMV( UPLO, N, -ONE, AP, X( 1, J ), 1, ONE, WORK, 1 )
197 *
198 *        Compute componentwise relative backward error from formula
199 *
200 *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
201 *
202 *        where abs(Z) is the componentwise absolute value of the matrix
203 *        or vector Z.  If the i-th component of the denominator is less
204 *        than SAFE2, then SAFE1 is added to the i-th components of the
205 *        numerator and denominator before dividing.
206 *
207          DO 30 I = 1, N
208             RWORK( I ) = CABS1( B( I, J ) )
209    30    CONTINUE
210 *
211 *        Compute abs(A)*abs(X) + abs(B).
212 *
213          KK = 1
214          IF( UPPER ) THEN
215             DO 50 K = 1, N
216                S = ZERO
217                XK = CABS1( X( K, J ) )
218                IK = KK
219                DO 40 I = 1, K - 1
220                   RWORK( I ) = RWORK( I ) + CABS1( AP( IK ) )*XK
221                   S = S + CABS1( AP( IK ) )*CABS1( X( I, J ) )
222                   IK = IK + 1
223    40          CONTINUE
224                RWORK( K ) = RWORK( K ) + ABSDBLE( AP( KK+K-1 ) ) )*
225      $                      XK + S
226                KK = KK + K
227    50       CONTINUE
228          ELSE
229             DO 70 K = 1, N
230                S = ZERO
231                XK = CABS1( X( K, J ) )
232                RWORK( K ) = RWORK( K ) + ABSDBLE( AP( KK ) ) )*XK
233                IK = KK + 1
234                DO 60 I = K + 1, N
235                   RWORK( I ) = RWORK( I ) + CABS1( AP( IK ) )*XK
236                   S = S + CABS1( AP( IK ) )*CABS1( X( I, J ) )
237                   IK = IK + 1
238    60          CONTINUE
239                RWORK( K ) = RWORK( K ) + S
240                KK = KK + ( N-K+1 )
241    70       CONTINUE
242          END IF
243          S = ZERO
244          DO 80 I = 1, N
245             IF( RWORK( I ).GT.SAFE2 ) THEN
246                S = MAX( S, CABS1( WORK( I ) ) / RWORK( I ) )
247             ELSE
248                S = MAX( S, ( CABS1( WORK( I ) )+SAFE1 ) /
249      $             ( RWORK( I )+SAFE1 ) )
250             END IF
251    80    CONTINUE
252          BERR( J ) = S
253 *
254 *        Test stopping criterion. Continue iterating if
255 *           1) The residual BERR(J) is larger than machine epsilon, and
256 *           2) BERR(J) decreased by at least a factor of 2 during the
257 *              last iteration, and
258 *           3) At most ITMAX iterations tried.
259 *
260          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
261      $       COUNT.LE.ITMAX ) THEN
262 *
263 *           Update solution and try again.
264 *
265             CALL ZHPTRS( UPLO, N, 1, AFP, IPIV, WORK, N, INFO )
266             CALL ZAXPY( N, ONE, WORK, 1, X( 1, J ), 1 )
267             LSTRES = BERR( J )
268             COUNT = COUNT + 1
269             GO TO 20
270          END IF
271 *
272 *        Bound error from formula
273 *
274 *        norm(X - XTRUE) / norm(X) .le. FERR =
275 *        norm( abs(inv(A))*
276 *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
277 *
278 *        where
279 *          norm(Z) is the magnitude of the largest component of Z
280 *          inv(A) is the inverse of A
281 *          abs(Z) is the componentwise absolute value of the matrix or
282 *             vector Z
283 *          NZ is the maximum number of nonzeros in any row of A, plus 1
284 *          EPS is machine epsilon
285 *
286 *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
287 *        is incremented by SAFE1 if the i-th component of
288 *        abs(A)*abs(X) + abs(B) is less than SAFE2.
289 *
290 *        Use ZLACN2 to estimate the infinity-norm of the matrix
291 *           inv(A) * diag(W),
292 *        where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
293 *
294          DO 90 I = 1, N
295             IF( RWORK( I ).GT.SAFE2 ) THEN
296                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I )
297             ELSE
298                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I ) +
299      $                      SAFE1
300             END IF
301    90    CONTINUE
302 *
303          KASE = 0
304   100    CONTINUE
305          CALL ZLACN2( N, WORK( N+1 ), WORK, FERR( J ), KASE, ISAVE )
306          IF( KASE.NE.0 ) THEN
307             IF( KASE.EQ.1 ) THEN
308 *
309 *              Multiply by diag(W)*inv(A**H).
310 *
311                CALL ZHPTRS( UPLO, N, 1, AFP, IPIV, WORK, N, INFO )
312                DO 110 I = 1, N
313                   WORK( I ) = RWORK( I )*WORK( I )
314   110          CONTINUE
315             ELSE IF( KASE.EQ.2 ) THEN
316 *
317 *              Multiply by inv(A)*diag(W).
318 *
319                DO 120 I = 1, N
320                   WORK( I ) = RWORK( I )*WORK( I )
321   120          CONTINUE
322                CALL ZHPTRS( UPLO, N, 1, AFP, IPIV, WORK, N, INFO )
323             END IF
324             GO TO 100
325          END IF
326 *
327 *        Normalize error.
328 *
329          LSTRES = ZERO
330          DO 130 I = 1, N
331             LSTRES = MAX( LSTRES, CABS1( X( I, J ) ) )
332   130    CONTINUE
333          IF( LSTRES.NE.ZERO )
334      $      FERR( J ) = FERR( J ) / LSTRES
335 *
336   140 CONTINUE
337 *
338       RETURN
339 *
340 *     End of ZHPRFS
341 *
342       END