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


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