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


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