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


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