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


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