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


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