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


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