1       SUBROUTINE ZPORFS( UPLO, N, NRHS, A, LDA, AF, LDAF, B, LDB, X,
  2      $                   LDX, 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, LDA, LDAF, LDB, LDX, N, NRHS
 14 *     ..
 15 *     .. Array Arguments ..
 16       DOUBLE PRECISION   BERR( * ), FERR( * ), RWORK( * )
 17       COMPLEX*16         A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
 18      $                   WORK( * ), X( LDX, * )
 19 *     ..
 20 *
 21 *  Purpose
 22 *  =======
 23 *
 24 *  ZPORFS improves the computed solution to a system of linear
 25 *  equations when the coefficient matrix is Hermitian 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) COMPLEX*16 array, dimension (LDA,N)
 44 *          The Hermitian 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) COMPLEX*16 array, dimension (LDAF,N)
 56 *          The triangular factor U or L from the Cholesky factorization
 57 *          A = U**H*U or A = L*L**H, as computed by ZPOTRF.
 58 *
 59 *  LDAF    (input) INTEGER
 60 *          The leading dimension of the array AF.  LDAF >= max(1,N).
 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 ZPOTRS.
 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, J, K, KASE, 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, ZHEMV, ZLACN2, ZPOTRS
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( LDA.LT.MAX1, N ) ) THEN
156          INFO = -5
157       ELSE IF( LDAF.LT.MAX1, N ) ) THEN
158          INFO = -7
159       ELSE IF( LDB.LT.MAX1, N ) ) THEN
160          INFO = -9
161       ELSE IF( LDX.LT.MAX1, N ) ) THEN
162          INFO = -11
163       END IF
164       IF( INFO.NE.0 ) THEN
165          CALL XERBLA( 'ZPORFS'-INFO )
166          RETURN
167       END IF
168 *
169 *     Quick return if possible
170 *
171       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
172          DO 10 J = 1, NRHS
173             FERR( J ) = ZERO
174             BERR( J ) = ZERO
175    10    CONTINUE
176          RETURN
177       END IF
178 *
179 *     NZ = maximum number of nonzero elements in each row of A, plus 1
180 *
181       NZ = N + 1
182       EPS = DLAMCH( 'Epsilon' )
183       SAFMIN = DLAMCH( 'Safe minimum' )
184       SAFE1 = NZ*SAFMIN
185       SAFE2 = SAFE1 / EPS
186 *
187 *     Do for each right hand side
188 *
189       DO 140 J = 1, NRHS
190 *
191          COUNT = 1
192          LSTRES = THREE
193    20    CONTINUE
194 *
195 *        Loop until stopping criterion is satisfied.
196 *
197 *        Compute residual R = B - A * X
198 *
199          CALL ZCOPY( N, B( 1, J ), 1, WORK, 1 )
200          CALL ZHEMV( UPLO, N, -ONE, A, LDA, X( 1, J ), 1, ONE, WORK, 1 )
201 *
202 *        Compute componentwise relative backward error from formula
203 *
204 *        max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) )
205 *
206 *        where abs(Z) is the componentwise absolute value of the matrix
207 *        or vector Z.  If the i-th component of the denominator is less
208 *        than SAFE2, then SAFE1 is added to the i-th components of the
209 *        numerator and denominator before dividing.
210 *
211          DO 30 I = 1, N
212             RWORK( I ) = CABS1( B( I, J ) )
213    30    CONTINUE
214 *
215 *        Compute abs(A)*abs(X) + abs(B).
216 *
217          IF( UPPER ) THEN
218             DO 50 K = 1, N
219                S = ZERO
220                XK = CABS1( X( K, J ) )
221                DO 40 I = 1, K - 1
222                   RWORK( I ) = RWORK( I ) + CABS1( A( I, K ) )*XK
223                   S = S + CABS1( A( I, K ) )*CABS1( X( I, J ) )
224    40          CONTINUE
225                RWORK( K ) = RWORK( K ) + ABSDBLE( A( K, K ) ) )*XK + S
226    50       CONTINUE
227          ELSE
228             DO 70 K = 1, N
229                S = ZERO
230                XK = CABS1( X( K, J ) )
231                RWORK( K ) = RWORK( K ) + ABSDBLE( A( K, K ) ) )*XK
232                DO 60 I = K + 1, N
233                   RWORK( I ) = RWORK( I ) + CABS1( A( I, K ) )*XK
234                   S = S + CABS1( A( I, K ) )*CABS1( X( I, J ) )
235    60          CONTINUE
236                RWORK( K ) = RWORK( K ) + S
237    70       CONTINUE
238          END IF
239          S = ZERO
240          DO 80 I = 1, N
241             IF( RWORK( I ).GT.SAFE2 ) THEN
242                S = MAX( S, CABS1( WORK( I ) ) / RWORK( I ) )
243             ELSE
244                S = MAX( S, ( CABS1( WORK( I ) )+SAFE1 ) /
245      $             ( RWORK( I )+SAFE1 ) )
246             END IF
247    80    CONTINUE
248          BERR( J ) = S
249 *
250 *        Test stopping criterion. Continue iterating if
251 *           1) The residual BERR(J) is larger than machine epsilon, and
252 *           2) BERR(J) decreased by at least a factor of 2 during the
253 *              last iteration, and
254 *           3) At most ITMAX iterations tried.
255 *
256          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
257      $       COUNT.LE.ITMAX ) THEN
258 *
259 *           Update solution and try again.
260 *
261             CALL ZPOTRS( UPLO, N, 1, AF, LDAF, WORK, N, INFO )
262             CALL ZAXPY( N, ONE, WORK, 1, X( 1, J ), 1 )
263             LSTRES = BERR( J )
264             COUNT = COUNT + 1
265             GO TO 20
266          END IF
267 *
268 *        Bound error from formula
269 *
270 *        norm(X - XTRUE) / norm(X) .le. FERR =
271 *        norm( abs(inv(A))*
272 *           ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X)
273 *
274 *        where
275 *          norm(Z) is the magnitude of the largest component of Z
276 *          inv(A) is the inverse of A
277 *          abs(Z) is the componentwise absolute value of the matrix or
278 *             vector Z
279 *          NZ is the maximum number of nonzeros in any row of A, plus 1
280 *          EPS is machine epsilon
281 *
282 *        The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B))
283 *        is incremented by SAFE1 if the i-th component of
284 *        abs(A)*abs(X) + abs(B) is less than SAFE2.
285 *
286 *        Use ZLACN2 to estimate the infinity-norm of the matrix
287 *           inv(A) * diag(W),
288 *        where W = abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) )))
289 *
290          DO 90 I = 1, N
291             IF( RWORK( I ).GT.SAFE2 ) THEN
292                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I )
293             ELSE
294                RWORK( I ) = CABS1( WORK( I ) ) + NZ*EPS*RWORK( I ) +
295      $                      SAFE1
296             END IF
297    90    CONTINUE
298 *
299          KASE = 0
300   100    CONTINUE
301          CALL ZLACN2( N, WORK( N+1 ), WORK, FERR( J ), KASE, ISAVE )
302          IF( KASE.NE.0 ) THEN
303             IF( KASE.EQ.1 ) THEN
304 *
305 *              Multiply by diag(W)*inv(A**H).
306 *
307                CALL ZPOTRS( UPLO, N, 1, AF, LDAF, WORK, N, INFO )
308                DO 110 I = 1, N
309                   WORK( I ) = RWORK( I )*WORK( I )
310   110          CONTINUE
311             ELSE IF( KASE.EQ.2 ) THEN
312 *
313 *              Multiply by inv(A)*diag(W).
314 *
315                DO 120 I = 1, N
316                   WORK( I ) = RWORK( I )*WORK( I )
317   120          CONTINUE
318                CALL ZPOTRS( UPLO, N, 1, AF, LDAF, WORK, N, INFO )
319             END IF
320             GO TO 100
321          END IF
322 *
323 *        Normalize error.
324 *
325          LSTRES = ZERO
326          DO 130 I = 1, N
327             LSTRES = MAX( LSTRES, CABS1( X( I, J ) ) )
328   130    CONTINUE
329          IF( LSTRES.NE.ZERO )
330      $      FERR( J ) = FERR( J ) / LSTRES
331 *
332   140 CONTINUE
333 *
334       RETURN
335 *
336 *     End of ZPORFS
337 *
338       END