zcgesv.c
Go to the documentation of this file.
00001 /* zcgesv.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 doublecomplex c_b1 = {-1.,0.};
00019 static doublecomplex c_b2 = {1.,0.};
00020 static integer c__1 = 1;
00021 
00022 /* Subroutine */ int zcgesv_(integer *n, integer *nrhs, doublecomplex *a, 
00023         integer *lda, integer *ipiv, doublecomplex *b, integer *ldb, 
00024         doublecomplex *x, integer *ldx, doublecomplex *work, complex *swork, 
00025         doublereal *rwork, integer *iter, integer *info)
00026 {
00027     /* System generated locals */
00028     integer a_dim1, a_offset, b_dim1, b_offset, work_dim1, work_offset, 
00029             x_dim1, x_offset, i__1, i__2;
00030     doublereal d__1, d__2;
00031 
00032     /* Builtin functions */
00033     double sqrt(doublereal), d_imag(doublecomplex *);
00034 
00035     /* Local variables */
00036     integer i__;
00037     doublereal cte, eps, anrm;
00038     integer ptsa;
00039     doublereal rnrm, xnrm;
00040     integer ptsx, iiter;
00041     extern /* Subroutine */ int zgemm_(char *, char *, integer *, integer *, 
00042             integer *, doublecomplex *, doublecomplex *, integer *, 
00043             doublecomplex *, integer *, doublecomplex *, doublecomplex *, 
00044             integer *), zaxpy_(integer *, doublecomplex *, 
00045             doublecomplex *, integer *, doublecomplex *, integer *), clag2z_(
00046             integer *, integer *, complex *, integer *, doublecomplex *, 
00047             integer *, integer *), zlag2c_(integer *, integer *, 
00048             doublecomplex *, integer *, complex *, integer *, integer *);
00049     extern doublereal dlamch_(char *);
00050     extern /* Subroutine */ int cgetrf_(integer *, integer *, complex *, 
00051             integer *, integer *, integer *), xerbla_(char *, integer *);
00052     extern doublereal zlange_(char *, integer *, integer *, doublecomplex *, 
00053             integer *, doublereal *);
00054     extern /* Subroutine */ int cgetrs_(char *, integer *, integer *, complex 
00055             *, integer *, integer *, complex *, integer *, integer *);
00056     extern integer izamax_(integer *, doublecomplex *, integer *);
00057     extern /* Subroutine */ int zlacpy_(char *, integer *, integer *, 
00058             doublecomplex *, integer *, doublecomplex *, integer *), 
00059             zgetrf_(integer *, integer *, doublecomplex *, integer *, integer 
00060             *, integer *), zgetrs_(char *, integer *, integer *, 
00061             doublecomplex *, integer *, integer *, doublecomplex *, integer *, 
00062              integer *);
00063 
00064 
00065 /*  -- LAPACK PROTOTYPE driver routine (version 3.2) -- */
00066 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00067 /*     January 2007 */
00068 
00069 /*     .. */
00070 /*     .. Scalar Arguments .. */
00071 /*     .. */
00072 /*     .. Array Arguments .. */
00073 /*     .. */
00074 
00075 /*  Purpose */
00076 /*  ======= */
00077 
00078 /*  ZCGESV computes the solution to a complex system of linear equations */
00079 /*     A * X = B, */
00080 /*  where A is an N-by-N matrix and X and B are N-by-NRHS matrices. */
00081 
00082 /*  ZCGESV first attempts to factorize the matrix in COMPLEX and use this */
00083 /*  factorization within an iterative refinement procedure to produce a */
00084 /*  solution with COMPLEX*16 normwise backward error quality (see below). */
00085 /*  If the approach fails the method switches to a COMPLEX*16 */
00086 /*  factorization and solve. */
00087 
00088 /*  The iterative refinement is not going to be a winning strategy if */
00089 /*  the ratio COMPLEX performance over COMPLEX*16 performance is too */
00090 /*  small. A reasonable strategy should take the number of right-hand */
00091 /*  sides and the size of the matrix into account. This might be done */
00092 /*  with a call to ILAENV in the future. Up to now, we always try */
00093 /*  iterative refinement. */
00094 
00095 /*  The iterative refinement process is stopped if */
00096 /*      ITER > ITERMAX */
00097 /*  or for all the RHS we have: */
00098 /*      RNRM < SQRT(N)*XNRM*ANRM*EPS*BWDMAX */
00099 /*  where */
00100 /*      o ITER is the number of the current iteration in the iterative */
00101 /*        refinement process */
00102 /*      o RNRM is the infinity-norm of the residual */
00103 /*      o XNRM is the infinity-norm of the solution */
00104 /*      o ANRM is the infinity-operator-norm of the matrix A */
00105 /*      o EPS is the machine epsilon returned by DLAMCH('Epsilon') */
00106 /*  The value ITERMAX and BWDMAX are fixed to 30 and 1.0D+00 */
00107 /*  respectively. */
00108 
00109 /*  Arguments */
00110 /*  ========= */
00111 
00112 /*  N       (input) INTEGER */
00113 /*          The number of linear equations, i.e., the order of the */
00114 /*          matrix A.  N >= 0. */
00115 
00116 /*  NRHS    (input) INTEGER */
00117 /*          The number of right hand sides, i.e., the number of columns */
00118 /*          of the matrix B.  NRHS >= 0. */
00119 
00120 /*  A       (input or input/ouptut) COMPLEX*16 array, */
00121 /*          dimension (LDA,N) */
00122 /*          On entry, the N-by-N coefficient matrix A. */
00123 /*          On exit, if iterative refinement has been successfully used */
00124 /*          (INFO.EQ.0 and ITER.GE.0, see description below), then A is */
00125 /*          unchanged, if double precision factorization has been used */
00126 /*          (INFO.EQ.0 and ITER.LT.0, see description below), then the */
00127 /*          array A contains the factors L and U from the factorization */
00128 /*          A = P*L*U; the unit diagonal elements of L are not stored. */
00129 
00130 /*  LDA     (input) INTEGER */
00131 /*          The leading dimension of the array A.  LDA >= max(1,N). */
00132 
00133 /*  IPIV    (output) INTEGER array, dimension (N) */
00134 /*          The pivot indices that define the permutation matrix P; */
00135 /*          row i of the matrix was interchanged with row IPIV(i). */
00136 /*          Corresponds either to the single precision factorization */
00137 /*          (if INFO.EQ.0 and ITER.GE.0) or the double precision */
00138 /*          factorization (if INFO.EQ.0 and ITER.LT.0). */
00139 
00140 /*  B       (input) COMPLEX*16 array, dimension (LDB,NRHS) */
00141 /*          The N-by-NRHS right hand side matrix B. */
00142 
00143 /*  LDB     (input) INTEGER */
00144 /*          The leading dimension of the array B.  LDB >= max(1,N). */
00145 
00146 /*  X       (output) COMPLEX*16 array, dimension (LDX,NRHS) */
00147 /*          If INFO = 0, the N-by-NRHS solution matrix X. */
00148 
00149 /*  LDX     (input) INTEGER */
00150 /*          The leading dimension of the array X.  LDX >= max(1,N). */
00151 
00152 /*  WORK    (workspace) COMPLEX*16 array, dimension (N*NRHS) */
00153 /*          This array is used to hold the residual vectors. */
00154 
00155 /*  SWORK   (workspace) COMPLEX array, dimension (N*(N+NRHS)) */
00156 /*          This array is used to use the single precision matrix and the */
00157 /*          right-hand sides or solutions in single precision. */
00158 
00159 /*  RWORK   (workspace) DOUBLE PRECISION array, dimension (N) */
00160 
00161 /*  ITER    (output) INTEGER */
00162 /*          < 0: iterative refinement has failed, COMPLEX*16 */
00163 /*               factorization has been performed */
00164 /*               -1 : the routine fell back to full precision for */
00165 /*                    implementation- or machine-specific reasons */
00166 /*               -2 : narrowing the precision induced an overflow, */
00167 /*                    the routine fell back to full precision */
00168 /*               -3 : failure of CGETRF */
00169 /*               -31: stop the iterative refinement after the 30th */
00170 /*                    iterations */
00171 /*          > 0: iterative refinement has been sucessfully used. */
00172 /*               Returns the number of iterations */
00173 
00174 /*  INFO    (output) INTEGER */
00175 /*          = 0:  successful exit */
00176 /*          < 0:  if INFO = -i, the i-th argument had an illegal value */
00177 /*          > 0:  if INFO = i, U(i,i) computed in COMPLEX*16 is exactly */
00178 /*                zero.  The factorization has been completed, but the */
00179 /*                factor U is exactly singular, so the solution */
00180 /*                could not be computed. */
00181 
00182 /*  ========= */
00183 
00184 /*     .. Parameters .. */
00185 
00186 
00187 
00188 
00189 /*     .. Local Scalars .. */
00190 
00191 /*     .. External Subroutines .. */
00192 /*     .. */
00193 /*     .. External Functions .. */
00194 /*     .. */
00195 /*     .. Intrinsic Functions .. */
00196 /*     .. */
00197 /*     .. Statement Functions .. */
00198 /*     .. */
00199 /*     .. Statement Function definitions .. */
00200 /*     .. */
00201 /*     .. Executable Statements .. */
00202 
00203     /* Parameter adjustments */
00204     work_dim1 = *n;
00205     work_offset = 1 + work_dim1;
00206     work -= work_offset;
00207     a_dim1 = *lda;
00208     a_offset = 1 + a_dim1;
00209     a -= a_offset;
00210     --ipiv;
00211     b_dim1 = *ldb;
00212     b_offset = 1 + b_dim1;
00213     b -= b_offset;
00214     x_dim1 = *ldx;
00215     x_offset = 1 + x_dim1;
00216     x -= x_offset;
00217     --swork;
00218     --rwork;
00219 
00220     /* Function Body */
00221     *info = 0;
00222     *iter = 0;
00223 
00224 /*     Test the input parameters. */
00225 
00226     if (*n < 0) {
00227         *info = -1;
00228     } else if (*nrhs < 0) {
00229         *info = -2;
00230     } else if (*lda < max(1,*n)) {
00231         *info = -4;
00232     } else if (*ldb < max(1,*n)) {
00233         *info = -7;
00234     } else if (*ldx < max(1,*n)) {
00235         *info = -9;
00236     }
00237     if (*info != 0) {
00238         i__1 = -(*info);
00239         xerbla_("ZCGESV", &i__1);
00240         return 0;
00241     }
00242 
00243 /*     Quick return if (N.EQ.0). */
00244 
00245     if (*n == 0) {
00246         return 0;
00247     }
00248 
00249 /*     Skip single precision iterative refinement if a priori slower */
00250 /*     than double precision factorization. */
00251 
00252     if (FALSE_) {
00253         *iter = -1;
00254         goto L40;
00255     }
00256 
00257 /*     Compute some constants. */
00258 
00259     anrm = zlange_("I", n, n, &a[a_offset], lda, &rwork[1]);
00260     eps = dlamch_("Epsilon");
00261     cte = anrm * eps * sqrt((doublereal) (*n)) * 1.;
00262 
00263 /*     Set the indices PTSA, PTSX for referencing SA and SX in SWORK. */
00264 
00265     ptsa = 1;
00266     ptsx = ptsa + *n * *n;
00267 
00268 /*     Convert B from double precision to single precision and store the */
00269 /*     result in SX. */
00270 
00271     zlag2c_(n, nrhs, &b[b_offset], ldb, &swork[ptsx], n, info);
00272 
00273     if (*info != 0) {
00274         *iter = -2;
00275         goto L40;
00276     }
00277 
00278 /*     Convert A from double precision to single precision and store the */
00279 /*     result in SA. */
00280 
00281     zlag2c_(n, n, &a[a_offset], lda, &swork[ptsa], n, info);
00282 
00283     if (*info != 0) {
00284         *iter = -2;
00285         goto L40;
00286     }
00287 
00288 /*     Compute the LU factorization of SA. */
00289 
00290     cgetrf_(n, n, &swork[ptsa], n, &ipiv[1], info);
00291 
00292     if (*info != 0) {
00293         *iter = -3;
00294         goto L40;
00295     }
00296 
00297 /*     Solve the system SA*SX = SB. */
00298 
00299     cgetrs_("No transpose", n, nrhs, &swork[ptsa], n, &ipiv[1], &swork[ptsx], 
00300             n, info);
00301 
00302 /*     Convert SX back to double precision */
00303 
00304     clag2z_(n, nrhs, &swork[ptsx], n, &x[x_offset], ldx, info);
00305 
00306 /*     Compute R = B - AX (R is WORK). */
00307 
00308     zlacpy_("All", n, nrhs, &b[b_offset], ldb, &work[work_offset], n);
00309 
00310     zgemm_("No Transpose", "No Transpose", n, nrhs, n, &c_b1, &a[a_offset], 
00311             lda, &x[x_offset], ldx, &c_b2, &work[work_offset], n);
00312 
00313 /*     Check whether the NRHS normwise backward errors satisfy the */
00314 /*     stopping criterion. If yes, set ITER=0 and return. */
00315 
00316     i__1 = *nrhs;
00317     for (i__ = 1; i__ <= i__1; ++i__) {
00318         i__2 = izamax_(n, &x[i__ * x_dim1 + 1], &c__1) + i__ * x_dim1;
00319         xnrm = (d__1 = x[i__2].r, abs(d__1)) + (d__2 = d_imag(&x[izamax_(n, &
00320                 x[i__ * x_dim1 + 1], &c__1) + i__ * x_dim1]), abs(d__2));
00321         i__2 = izamax_(n, &work[i__ * work_dim1 + 1], &c__1) + i__ * 
00322                 work_dim1;
00323         rnrm = (d__1 = work[i__2].r, abs(d__1)) + (d__2 = d_imag(&work[
00324                 izamax_(n, &work[i__ * work_dim1 + 1], &c__1) + i__ * 
00325                 work_dim1]), abs(d__2));
00326         if (rnrm > xnrm * cte) {
00327             goto L10;
00328         }
00329     }
00330 
00331 /*     If we are here, the NRHS normwise backward errors satisfy the */
00332 /*     stopping criterion. We are good to exit. */
00333 
00334     *iter = 0;
00335     return 0;
00336 
00337 L10:
00338 
00339     for (iiter = 1; iiter <= 30; ++iiter) {
00340 
00341 /*        Convert R (in WORK) from double precision to single precision */
00342 /*        and store the result in SX. */
00343 
00344         zlag2c_(n, nrhs, &work[work_offset], n, &swork[ptsx], n, info);
00345 
00346         if (*info != 0) {
00347             *iter = -2;
00348             goto L40;
00349         }
00350 
00351 /*        Solve the system SA*SX = SR. */
00352 
00353         cgetrs_("No transpose", n, nrhs, &swork[ptsa], n, &ipiv[1], &swork[
00354                 ptsx], n, info);
00355 
00356 /*        Convert SX back to double precision and update the current */
00357 /*        iterate. */
00358 
00359         clag2z_(n, nrhs, &swork[ptsx], n, &work[work_offset], n, info);
00360 
00361         i__1 = *nrhs;
00362         for (i__ = 1; i__ <= i__1; ++i__) {
00363             zaxpy_(n, &c_b2, &work[i__ * work_dim1 + 1], &c__1, &x[i__ * 
00364                     x_dim1 + 1], &c__1);
00365         }
00366 
00367 /*        Compute R = B - AX (R is WORK). */
00368 
00369         zlacpy_("All", n, nrhs, &b[b_offset], ldb, &work[work_offset], n);
00370 
00371         zgemm_("No Transpose", "No Transpose", n, nrhs, n, &c_b1, &a[a_offset]
00372 , lda, &x[x_offset], ldx, &c_b2, &work[work_offset], n);
00373 
00374 /*        Check whether the NRHS normwise backward errors satisfy the */
00375 /*        stopping criterion. If yes, set ITER=IITER>0 and return. */
00376 
00377         i__1 = *nrhs;
00378         for (i__ = 1; i__ <= i__1; ++i__) {
00379             i__2 = izamax_(n, &x[i__ * x_dim1 + 1], &c__1) + i__ * x_dim1;
00380             xnrm = (d__1 = x[i__2].r, abs(d__1)) + (d__2 = d_imag(&x[izamax_(
00381                     n, &x[i__ * x_dim1 + 1], &c__1) + i__ * x_dim1]), abs(
00382                     d__2));
00383             i__2 = izamax_(n, &work[i__ * work_dim1 + 1], &c__1) + i__ * 
00384                     work_dim1;
00385             rnrm = (d__1 = work[i__2].r, abs(d__1)) + (d__2 = d_imag(&work[
00386                     izamax_(n, &work[i__ * work_dim1 + 1], &c__1) + i__ * 
00387                     work_dim1]), abs(d__2));
00388             if (rnrm > xnrm * cte) {
00389                 goto L20;
00390             }
00391         }
00392 
00393 /*        If we are here, the NRHS normwise backward errors satisfy the */
00394 /*        stopping criterion, we are good to exit. */
00395 
00396         *iter = iiter;
00397 
00398         return 0;
00399 
00400 L20:
00401 
00402 /* L30: */
00403         ;
00404     }
00405 
00406 /*     If we are at this place of the code, this is because we have */
00407 /*     performed ITER=ITERMAX iterations and never satisified the stopping */
00408 /*     criterion, set up the ITER flag accordingly and follow up on double */
00409 /*     precision routine. */
00410 
00411     *iter = -31;
00412 
00413 L40:
00414 
00415 /*     Single-precision iterative refinement failed to converge to a */
00416 /*     satisfactory solution, so we resort to double precision. */
00417 
00418     zgetrf_(n, n, &a[a_offset], lda, &ipiv[1], info);
00419 
00420     if (*info != 0) {
00421         return 0;
00422     }
00423 
00424     zlacpy_("All", n, nrhs, &b[b_offset], ldb, &x[x_offset], ldx);
00425     zgetrs_("No transpose", n, nrhs, &a[a_offset], lda, &ipiv[1], &x[x_offset]
00426 , ldx, info);
00427 
00428     return 0;
00429 
00430 /*     End of ZCGESV. */
00431 
00432 } /* zcgesv_ */


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