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


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:55:26