zgerfs.c
Go to the documentation of this file.
00001 /* zgerfs.f -- translated by f2c (version 20061008).
00002    You must link the resulting object file with libf2c:
00003         on Microsoft Windows system, link with libf2c.lib;
00004         on Linux or Unix systems, link with .../path/to/libf2c.a -lm
00005         or, if you install libf2c.a in a standard place, with -lf2c -lm
00006         -- in that order, at the end of the command line, as in
00007                 cc *.o -lf2c -lm
00008         Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
00009 
00010                 http://www.netlib.org/f2c/libf2c.zip
00011 */
00012 
00013 #include "f2c.h"
00014 #include "blaswrap.h"
00015 
00016 /* Table of constant values */
00017 
00018 static doublecomplex c_b1 = {1.,0.};
00019 static integer c__1 = 1;
00020 
00021 /* Subroutine */ int zgerfs_(char *trans, integer *n, integer *nrhs, 
00022         doublecomplex *a, integer *lda, doublecomplex *af, integer *ldaf, 
00023         integer *ipiv, doublecomplex *b, integer *ldb, doublecomplex *x, 
00024         integer *ldx, doublereal *ferr, doublereal *berr, doublecomplex *work, 
00025          doublereal *rwork, integer *info)
00026 {
00027     /* System generated locals */
00028     integer a_dim1, a_offset, af_dim1, af_offset, b_dim1, b_offset, x_dim1, 
00029             x_offset, i__1, i__2, i__3, i__4, i__5;
00030     doublereal d__1, d__2, d__3, d__4;
00031     doublecomplex z__1;
00032 
00033     /* Builtin functions */
00034     double d_imag(doublecomplex *);
00035 
00036     /* Local variables */
00037     integer i__, j, k;
00038     doublereal s, xk;
00039     integer nz;
00040     doublereal eps;
00041     integer kase;
00042     doublereal safe1, safe2;
00043     extern logical lsame_(char *, char *);
00044     integer isave[3], count;
00045     extern /* Subroutine */ int zgemv_(char *, integer *, integer *, 
00046             doublecomplex *, doublecomplex *, integer *, doublecomplex *, 
00047             integer *, doublecomplex *, doublecomplex *, integer *), 
00048             zcopy_(integer *, doublecomplex *, integer *, doublecomplex *, 
00049             integer *), zaxpy_(integer *, doublecomplex *, doublecomplex *, 
00050             integer *, doublecomplex *, integer *), zlacn2_(integer *, 
00051             doublecomplex *, doublecomplex *, doublereal *, integer *, 
00052             integer *);
00053     extern doublereal dlamch_(char *);
00054     doublereal safmin;
00055     extern /* Subroutine */ int xerbla_(char *, integer *);
00056     logical notran;
00057     char transn[1], transt[1];
00058     doublereal lstres;
00059     extern /* Subroutine */ int zgetrs_(char *, integer *, integer *, 
00060             doublecomplex *, integer *, integer *, doublecomplex *, integer *, 
00061              integer *);
00062 
00063 
00064 /*  -- LAPACK routine (version 3.2) -- */
00065 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00066 /*     November 2006 */
00067 
00068 /*     Modified to call ZLACN2 in place of ZLACON, 10 Feb 03, SJH. */
00069 
00070 /*     .. Scalar Arguments .. */
00071 /*     .. */
00072 /*     .. Array Arguments .. */
00073 /*     .. */
00074 
00075 /*  Purpose */
00076 /*  ======= */
00077 
00078 /*  ZGERFS improves the computed solution to a system of linear */
00079 /*  equations and provides error bounds and backward error estimates for */
00080 /*  the solution. */
00081 
00082 /*  Arguments */
00083 /*  ========= */
00084 
00085 /*  TRANS   (input) CHARACTER*1 */
00086 /*          Specifies the form of the system of equations: */
00087 /*          = 'N':  A * X = B     (No transpose) */
00088 /*          = 'T':  A**T * X = B  (Transpose) */
00089 /*          = 'C':  A**H * X = B  (Conjugate transpose) */
00090 
00091 /*  N       (input) INTEGER */
00092 /*          The order of the matrix A.  N >= 0. */
00093 
00094 /*  NRHS    (input) INTEGER */
00095 /*          The number of right hand sides, i.e., the number of columns */
00096 /*          of the matrices B and X.  NRHS >= 0. */
00097 
00098 /*  A       (input) COMPLEX*16 array, dimension (LDA,N) */
00099 /*          The original N-by-N matrix A. */
00100 
00101 /*  LDA     (input) INTEGER */
00102 /*          The leading dimension of the array A.  LDA >= max(1,N). */
00103 
00104 /*  AF      (input) COMPLEX*16 array, dimension (LDAF,N) */
00105 /*          The factors L and U from the factorization A = P*L*U */
00106 /*          as computed by ZGETRF. */
00107 
00108 /*  LDAF    (input) INTEGER */
00109 /*          The leading dimension of the array AF.  LDAF >= max(1,N). */
00110 
00111 /*  IPIV    (input) INTEGER array, dimension (N) */
00112 /*          The pivot indices from ZGETRF; for 1<=i<=N, row i of the */
00113 /*          matrix was interchanged with row IPIV(i). */
00114 
00115 /*  B       (input) COMPLEX*16 array, dimension (LDB,NRHS) */
00116 /*          The right hand side matrix B. */
00117 
00118 /*  LDB     (input) INTEGER */
00119 /*          The leading dimension of the array B.  LDB >= max(1,N). */
00120 
00121 /*  X       (input/output) COMPLEX*16 array, dimension (LDX,NRHS) */
00122 /*          On entry, the solution matrix X, as computed by ZGETRS. */
00123 /*          On exit, the improved solution matrix X. */
00124 
00125 /*  LDX     (input) INTEGER */
00126 /*          The leading dimension of the array X.  LDX >= max(1,N). */
00127 
00128 /*  FERR    (output) DOUBLE PRECISION array, dimension (NRHS) */
00129 /*          The estimated forward error bound for each solution vector */
00130 /*          X(j) (the j-th column of the solution matrix X). */
00131 /*          If XTRUE is the true solution corresponding to X(j), FERR(j) */
00132 /*          is an estimated upper bound for the magnitude of the largest */
00133 /*          element in (X(j) - XTRUE) divided by the magnitude of the */
00134 /*          largest element in X(j).  The estimate is as reliable as */
00135 /*          the estimate for RCOND, and is almost always a slight */
00136 /*          overestimate of the true error. */
00137 
00138 /*  BERR    (output) DOUBLE PRECISION array, dimension (NRHS) */
00139 /*          The componentwise relative backward error of each solution */
00140 /*          vector X(j) (i.e., the smallest relative change in */
00141 /*          any element of A or B that makes X(j) an exact solution). */
00142 
00143 /*  WORK    (workspace) COMPLEX*16 array, dimension (2*N) */
00144 
00145 /*  RWORK   (workspace) DOUBLE PRECISION array, dimension (N) */
00146 
00147 /*  INFO    (output) INTEGER */
00148 /*          = 0:  successful exit */
00149 /*          < 0:  if INFO = -i, the i-th argument had an illegal value */
00150 
00151 /*  Internal Parameters */
00152 /*  =================== */
00153 
00154 /*  ITMAX is the maximum number of steps of iterative refinement. */
00155 
00156 /*  ===================================================================== */
00157 
00158 /*     .. Parameters .. */
00159 /*     .. */
00160 /*     .. Local Scalars .. */
00161 /*     .. */
00162 /*     .. Local Arrays .. */
00163 /*     .. */
00164 /*     .. External Functions .. */
00165 /*     .. */
00166 /*     .. External Subroutines .. */
00167 /*     .. */
00168 /*     .. Intrinsic Functions .. */
00169 /*     .. */
00170 /*     .. Statement Functions .. */
00171 /*     .. */
00172 /*     .. Statement Function definitions .. */
00173 /*     .. */
00174 /*     .. Executable Statements .. */
00175 
00176 /*     Test the input parameters. */
00177 
00178     /* Parameter adjustments */
00179     a_dim1 = *lda;
00180     a_offset = 1 + a_dim1;
00181     a -= a_offset;
00182     af_dim1 = *ldaf;
00183     af_offset = 1 + af_dim1;
00184     af -= af_offset;
00185     --ipiv;
00186     b_dim1 = *ldb;
00187     b_offset = 1 + b_dim1;
00188     b -= b_offset;
00189     x_dim1 = *ldx;
00190     x_offset = 1 + x_dim1;
00191     x -= x_offset;
00192     --ferr;
00193     --berr;
00194     --work;
00195     --rwork;
00196 
00197     /* Function Body */
00198     *info = 0;
00199     notran = lsame_(trans, "N");
00200     if (! notran && ! lsame_(trans, "T") && ! lsame_(
00201             trans, "C")) {
00202         *info = -1;
00203     } else if (*n < 0) {
00204         *info = -2;
00205     } else if (*nrhs < 0) {
00206         *info = -3;
00207     } else if (*lda < max(1,*n)) {
00208         *info = -5;
00209     } else if (*ldaf < max(1,*n)) {
00210         *info = -7;
00211     } else if (*ldb < max(1,*n)) {
00212         *info = -10;
00213     } else if (*ldx < max(1,*n)) {
00214         *info = -12;
00215     }
00216     if (*info != 0) {
00217         i__1 = -(*info);
00218         xerbla_("ZGERFS", &i__1);
00219         return 0;
00220     }
00221 
00222 /*     Quick return if possible */
00223 
00224     if (*n == 0 || *nrhs == 0) {
00225         i__1 = *nrhs;
00226         for (j = 1; j <= i__1; ++j) {
00227             ferr[j] = 0.;
00228             berr[j] = 0.;
00229 /* L10: */
00230         }
00231         return 0;
00232     }
00233 
00234     if (notran) {
00235         *(unsigned char *)transn = 'N';
00236         *(unsigned char *)transt = 'C';
00237     } else {
00238         *(unsigned char *)transn = 'C';
00239         *(unsigned char *)transt = 'N';
00240     }
00241 
00242 /*     NZ = maximum number of nonzero elements in each row of A, plus 1 */
00243 
00244     nz = *n + 1;
00245     eps = dlamch_("Epsilon");
00246     safmin = dlamch_("Safe minimum");
00247     safe1 = nz * safmin;
00248     safe2 = safe1 / eps;
00249 
00250 /*     Do for each right hand side */
00251 
00252     i__1 = *nrhs;
00253     for (j = 1; j <= i__1; ++j) {
00254 
00255         count = 1;
00256         lstres = 3.;
00257 L20:
00258 
00259 /*        Loop until stopping criterion is satisfied. */
00260 
00261 /*        Compute residual R = B - op(A) * X, */
00262 /*        where op(A) = A, A**T, or A**H, depending on TRANS. */
00263 
00264         zcopy_(n, &b[j * b_dim1 + 1], &c__1, &work[1], &c__1);
00265         z__1.r = -1., z__1.i = -0.;
00266         zgemv_(trans, n, n, &z__1, &a[a_offset], lda, &x[j * x_dim1 + 1], &
00267                 c__1, &c_b1, &work[1], &c__1);
00268 
00269 /*        Compute componentwise relative backward error from formula */
00270 
00271 /*        max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) ) */
00272 
00273 /*        where abs(Z) is the componentwise absolute value of the matrix */
00274 /*        or vector Z.  If the i-th component of the denominator is less */
00275 /*        than SAFE2, then SAFE1 is added to the i-th components of the */
00276 /*        numerator and denominator before dividing. */
00277 
00278         i__2 = *n;
00279         for (i__ = 1; i__ <= i__2; ++i__) {
00280             i__3 = i__ + j * b_dim1;
00281             rwork[i__] = (d__1 = b[i__3].r, abs(d__1)) + (d__2 = d_imag(&b[
00282                     i__ + j * b_dim1]), abs(d__2));
00283 /* L30: */
00284         }
00285 
00286 /*        Compute abs(op(A))*abs(X) + abs(B). */
00287 
00288         if (notran) {
00289             i__2 = *n;
00290             for (k = 1; k <= i__2; ++k) {
00291                 i__3 = k + j * x_dim1;
00292                 xk = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(&x[k + j *
00293                          x_dim1]), abs(d__2));
00294                 i__3 = *n;
00295                 for (i__ = 1; i__ <= i__3; ++i__) {
00296                     i__4 = i__ + k * a_dim1;
00297                     rwork[i__] += ((d__1 = a[i__4].r, abs(d__1)) + (d__2 = 
00298                             d_imag(&a[i__ + k * a_dim1]), abs(d__2))) * xk;
00299 /* L40: */
00300                 }
00301 /* L50: */
00302             }
00303         } else {
00304             i__2 = *n;
00305             for (k = 1; k <= i__2; ++k) {
00306                 s = 0.;
00307                 i__3 = *n;
00308                 for (i__ = 1; i__ <= i__3; ++i__) {
00309                     i__4 = i__ + k * a_dim1;
00310                     i__5 = i__ + j * x_dim1;
00311                     s += ((d__1 = a[i__4].r, abs(d__1)) + (d__2 = d_imag(&a[
00312                             i__ + k * a_dim1]), abs(d__2))) * ((d__3 = x[i__5]
00313                             .r, abs(d__3)) + (d__4 = d_imag(&x[i__ + j * 
00314                             x_dim1]), abs(d__4)));
00315 /* L60: */
00316                 }
00317                 rwork[k] += s;
00318 /* L70: */
00319             }
00320         }
00321         s = 0.;
00322         i__2 = *n;
00323         for (i__ = 1; i__ <= i__2; ++i__) {
00324             if (rwork[i__] > safe2) {
00325 /* Computing MAX */
00326                 i__3 = i__;
00327                 d__3 = s, d__4 = ((d__1 = work[i__3].r, abs(d__1)) + (d__2 = 
00328                         d_imag(&work[i__]), abs(d__2))) / rwork[i__];
00329                 s = max(d__3,d__4);
00330             } else {
00331 /* Computing MAX */
00332                 i__3 = i__;
00333                 d__3 = s, d__4 = ((d__1 = work[i__3].r, abs(d__1)) + (d__2 = 
00334                         d_imag(&work[i__]), abs(d__2)) + safe1) / (rwork[i__] 
00335                         + safe1);
00336                 s = max(d__3,d__4);
00337             }
00338 /* L80: */
00339         }
00340         berr[j] = s;
00341 
00342 /*        Test stopping criterion. Continue iterating if */
00343 /*           1) The residual BERR(J) is larger than machine epsilon, and */
00344 /*           2) BERR(J) decreased by at least a factor of 2 during the */
00345 /*              last iteration, and */
00346 /*           3) At most ITMAX iterations tried. */
00347 
00348         if (berr[j] > eps && berr[j] * 2. <= lstres && count <= 5) {
00349 
00350 /*           Update solution and try again. */
00351 
00352             zgetrs_(trans, n, &c__1, &af[af_offset], ldaf, &ipiv[1], &work[1], 
00353                      n, info);
00354             zaxpy_(n, &c_b1, &work[1], &c__1, &x[j * x_dim1 + 1], &c__1);
00355             lstres = berr[j];
00356             ++count;
00357             goto L20;
00358         }
00359 
00360 /*        Bound error from formula */
00361 
00362 /*        norm(X - XTRUE) / norm(X) .le. FERR = */
00363 /*        norm( abs(inv(op(A)))* */
00364 /*           ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X) */
00365 
00366 /*        where */
00367 /*          norm(Z) is the magnitude of the largest component of Z */
00368 /*          inv(op(A)) is the inverse of op(A) */
00369 /*          abs(Z) is the componentwise absolute value of the matrix or */
00370 /*             vector Z */
00371 /*          NZ is the maximum number of nonzeros in any row of A, plus 1 */
00372 /*          EPS is machine epsilon */
00373 
00374 /*        The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B)) */
00375 /*        is incremented by SAFE1 if the i-th component of */
00376 /*        abs(op(A))*abs(X) + abs(B) is less than SAFE2. */
00377 
00378 /*        Use ZLACN2 to estimate the infinity-norm of the matrix */
00379 /*           inv(op(A)) * diag(W), */
00380 /*        where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) */
00381 
00382         i__2 = *n;
00383         for (i__ = 1; i__ <= i__2; ++i__) {
00384             if (rwork[i__] > safe2) {
00385                 i__3 = i__;
00386                 rwork[i__] = (d__1 = work[i__3].r, abs(d__1)) + (d__2 = 
00387                         d_imag(&work[i__]), abs(d__2)) + nz * eps * rwork[i__]
00388                         ;
00389             } else {
00390                 i__3 = i__;
00391                 rwork[i__] = (d__1 = work[i__3].r, abs(d__1)) + (d__2 = 
00392                         d_imag(&work[i__]), abs(d__2)) + nz * eps * rwork[i__]
00393                          + safe1;
00394             }
00395 /* L90: */
00396         }
00397 
00398         kase = 0;
00399 L100:
00400         zlacn2_(n, &work[*n + 1], &work[1], &ferr[j], &kase, isave);
00401         if (kase != 0) {
00402             if (kase == 1) {
00403 
00404 /*              Multiply by diag(W)*inv(op(A)**H). */
00405 
00406                 zgetrs_(transt, n, &c__1, &af[af_offset], ldaf, &ipiv[1], &
00407                         work[1], n, info);
00408                 i__2 = *n;
00409                 for (i__ = 1; i__ <= i__2; ++i__) {
00410                     i__3 = i__;
00411                     i__4 = i__;
00412                     i__5 = i__;
00413                     z__1.r = rwork[i__4] * work[i__5].r, z__1.i = rwork[i__4] 
00414                             * work[i__5].i;
00415                     work[i__3].r = z__1.r, work[i__3].i = z__1.i;
00416 /* L110: */
00417                 }
00418             } else {
00419 
00420 /*              Multiply by inv(op(A))*diag(W). */
00421 
00422                 i__2 = *n;
00423                 for (i__ = 1; i__ <= i__2; ++i__) {
00424                     i__3 = i__;
00425                     i__4 = i__;
00426                     i__5 = i__;
00427                     z__1.r = rwork[i__4] * work[i__5].r, z__1.i = rwork[i__4] 
00428                             * work[i__5].i;
00429                     work[i__3].r = z__1.r, work[i__3].i = z__1.i;
00430 /* L120: */
00431                 }
00432                 zgetrs_(transn, n, &c__1, &af[af_offset], ldaf, &ipiv[1], &
00433                         work[1], n, info);
00434             }
00435             goto L100;
00436         }
00437 
00438 /*        Normalize error. */
00439 
00440         lstres = 0.;
00441         i__2 = *n;
00442         for (i__ = 1; i__ <= i__2; ++i__) {
00443 /* Computing MAX */
00444             i__3 = i__ + j * x_dim1;
00445             d__3 = lstres, d__4 = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = 
00446                     d_imag(&x[i__ + j * x_dim1]), abs(d__2));
00447             lstres = max(d__3,d__4);
00448 /* L130: */
00449         }
00450         if (lstres != 0.) {
00451             ferr[j] /= lstres;
00452         }
00453 
00454 /* L140: */
00455     }
00456 
00457     return 0;
00458 
00459 /*     End of ZGERFS */
00460 
00461 } /* zgerfs_ */


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:56:33