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


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