cgels.c
Go to the documentation of this file.
00001 /* cgels.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 = {0.f,0.f};
00019 static integer c__1 = 1;
00020 static integer c_n1 = -1;
00021 static integer c__0 = 0;
00022 
00023 /* Subroutine */ int cgels_(char *trans, integer *m, integer *n, integer *
00024         nrhs, complex *a, integer *lda, complex *b, integer *ldb, complex *
00025         work, integer *lwork, integer *info)
00026 {
00027     /* System generated locals */
00028     integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2, i__3;
00029     real r__1;
00030 
00031     /* Local variables */
00032     integer i__, j, nb, mn;
00033     real anrm, bnrm;
00034     integer brow;
00035     logical tpsd;
00036     integer iascl, ibscl;
00037     extern logical lsame_(char *, char *);
00038     integer wsize;
00039     real rwork[1];
00040     extern /* Subroutine */ int slabad_(real *, real *);
00041     extern doublereal clange_(char *, integer *, integer *, complex *, 
00042             integer *, real *);
00043     extern /* Subroutine */ int cgelqf_(integer *, integer *, complex *, 
00044             integer *, complex *, complex *, integer *, integer *), clascl_(
00045             char *, integer *, integer *, real *, real *, integer *, integer *
00046 , complex *, integer *, integer *);
00047     extern doublereal slamch_(char *);
00048     extern /* Subroutine */ int cgeqrf_(integer *, integer *, complex *, 
00049             integer *, complex *, complex *, integer *, integer *), claset_(
00050             char *, integer *, integer *, complex *, complex *, complex *, 
00051             integer *), xerbla_(char *, integer *);
00052     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
00053             integer *, integer *);
00054     integer scllen;
00055     real bignum;
00056     extern /* Subroutine */ int cunmlq_(char *, char *, integer *, integer *, 
00057             integer *, complex *, integer *, complex *, complex *, integer *, 
00058             complex *, integer *, integer *), cunmqr_(char *, 
00059             char *, integer *, integer *, integer *, complex *, integer *, 
00060             complex *, complex *, integer *, complex *, integer *, integer *);
00061     real smlnum;
00062     logical lquery;
00063     extern /* Subroutine */ int ctrtrs_(char *, char *, char *, integer *, 
00064             integer *, complex *, integer *, complex *, integer *, integer *);
00065 
00066 
00067 /*  -- LAPACK driver routine (version 3.2) -- */
00068 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00069 /*     November 2006 */
00070 
00071 /*     .. Scalar Arguments .. */
00072 /*     .. */
00073 /*     .. Array Arguments .. */
00074 /*     .. */
00075 
00076 /*  Purpose */
00077 /*  ======= */
00078 
00079 /*  CGELS solves overdetermined or underdetermined complex linear systems */
00080 /*  involving an M-by-N matrix A, or its conjugate-transpose, using a QR */
00081 /*  or LQ factorization of A.  It is assumed that A has full rank. */
00082 
00083 /*  The following options are provided: */
00084 
00085 /*  1. If TRANS = 'N' and m >= n:  find the least squares solution of */
00086 /*     an overdetermined system, i.e., solve the least squares problem */
00087 /*                  minimize || B - A*X ||. */
00088 
00089 /*  2. If TRANS = 'N' and m < n:  find the minimum norm solution of */
00090 /*     an underdetermined system A * X = B. */
00091 
00092 /*  3. If TRANS = 'C' and m >= n:  find the minimum norm solution of */
00093 /*     an undetermined system A**H * X = B. */
00094 
00095 /*  4. If TRANS = 'C' and m < n:  find the least squares solution of */
00096 /*     an overdetermined system, i.e., solve the least squares problem */
00097 /*                  minimize || B - A**H * X ||. */
00098 
00099 /*  Several right hand side vectors b and solution vectors x can be */
00100 /*  handled in a single call; they are stored as the columns of the */
00101 /*  M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
00102 /*  matrix X. */
00103 
00104 /*  Arguments */
00105 /*  ========= */
00106 
00107 /*  TRANS   (input) CHARACTER*1 */
00108 /*          = 'N': the linear system involves A; */
00109 /*          = 'C': the linear system involves A**H. */
00110 
00111 /*  M       (input) INTEGER */
00112 /*          The number of rows of the matrix A.  M >= 0. */
00113 
00114 /*  N       (input) INTEGER */
00115 /*          The number of columns of the matrix A.  N >= 0. */
00116 
00117 /*  NRHS    (input) INTEGER */
00118 /*          The number of right hand sides, i.e., the number of */
00119 /*          columns of the matrices B and X. NRHS >= 0. */
00120 
00121 /*  A       (input/output) COMPLEX array, dimension (LDA,N) */
00122 /*          On entry, the M-by-N matrix A. */
00123 /*            if M >= N, A is overwritten by details of its QR */
00124 /*                       factorization as returned by CGEQRF; */
00125 /*            if M <  N, A is overwritten by details of its LQ */
00126 /*                       factorization as returned by CGELQF. */
00127 
00128 /*  LDA     (input) INTEGER */
00129 /*          The leading dimension of the array A.  LDA >= max(1,M). */
00130 
00131 /*  B       (input/output) COMPLEX array, dimension (LDB,NRHS) */
00132 /*          On entry, the matrix B of right hand side vectors, stored */
00133 /*          columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS */
00134 /*          if TRANS = 'C'. */
00135 /*          On exit, if INFO = 0, B is overwritten by the solution */
00136 /*          vectors, stored columnwise: */
00137 /*          if TRANS = 'N' and m >= n, rows 1 to n of B contain the least */
00138 /*          squares solution vectors; the residual sum of squares for the */
00139 /*          solution in each column is given by the sum of squares of the */
00140 /*          modulus of elements N+1 to M in that column; */
00141 /*          if TRANS = 'N' and m < n, rows 1 to N of B contain the */
00142 /*          minimum norm solution vectors; */
00143 /*          if TRANS = 'C' and m >= n, rows 1 to M of B contain the */
00144 /*          minimum norm solution vectors; */
00145 /*          if TRANS = 'C' and m < n, rows 1 to M of B contain the */
00146 /*          least squares solution vectors; the residual sum of squares */
00147 /*          for the solution in each column is given by the sum of */
00148 /*          squares of the modulus of elements M+1 to N in that column. */
00149 
00150 /*  LDB     (input) INTEGER */
00151 /*          The leading dimension of the array B. LDB >= MAX(1,M,N). */
00152 
00153 /*  WORK    (workspace/output) COMPLEX array, dimension (MAX(1,LWORK)) */
00154 /*          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
00155 
00156 /*  LWORK   (input) INTEGER */
00157 /*          The dimension of the array WORK. */
00158 /*          LWORK >= max( 1, MN + max( MN, NRHS ) ). */
00159 /*          For optimal performance, */
00160 /*          LWORK >= max( 1, MN + max( MN, NRHS )*NB ). */
00161 /*          where MN = min(M,N) and NB is the optimum block size. */
00162 
00163 /*          If LWORK = -1, then a workspace query is assumed; the routine */
00164 /*          only calculates the optimal size of the WORK array, returns */
00165 /*          this value as the first entry of the WORK array, and no error */
00166 /*          message related to LWORK is issued by XERBLA. */
00167 
00168 /*  INFO    (output) INTEGER */
00169 /*          = 0:  successful exit */
00170 /*          < 0:  if INFO = -i, the i-th argument had an illegal value */
00171 /*          > 0:  if INFO =  i, the i-th diagonal element of the */
00172 /*                triangular factor of A is zero, so that A does not have */
00173 /*                full rank; the least squares solution could not be */
00174 /*                computed. */
00175 
00176 /*  ===================================================================== */
00177 
00178 /*     .. Parameters .. */
00179 /*     .. */
00180 /*     .. Local Scalars .. */
00181 /*     .. */
00182 /*     .. Local Arrays .. */
00183 /*     .. */
00184 /*     .. External Functions .. */
00185 /*     .. */
00186 /*     .. External Subroutines .. */
00187 /*     .. */
00188 /*     .. Intrinsic Functions .. */
00189 /*     .. */
00190 /*     .. Executable Statements .. */
00191 
00192 /*     Test the input arguments. */
00193 
00194     /* Parameter adjustments */
00195     a_dim1 = *lda;
00196     a_offset = 1 + a_dim1;
00197     a -= a_offset;
00198     b_dim1 = *ldb;
00199     b_offset = 1 + b_dim1;
00200     b -= b_offset;
00201     --work;
00202 
00203     /* Function Body */
00204     *info = 0;
00205     mn = min(*m,*n);
00206     lquery = *lwork == -1;
00207     if (! (lsame_(trans, "N") || lsame_(trans, "C"))) {
00208         *info = -1;
00209     } else if (*m < 0) {
00210         *info = -2;
00211     } else if (*n < 0) {
00212         *info = -3;
00213     } else if (*nrhs < 0) {
00214         *info = -4;
00215     } else if (*lda < max(1,*m)) {
00216         *info = -6;
00217     } else /* if(complicated condition) */ {
00218 /* Computing MAX */
00219         i__1 = max(1,*m);
00220         if (*ldb < max(i__1,*n)) {
00221             *info = -8;
00222         } else /* if(complicated condition) */ {
00223 /* Computing MAX */
00224             i__1 = 1, i__2 = mn + max(mn,*nrhs);
00225             if (*lwork < max(i__1,i__2) && ! lquery) {
00226                 *info = -10;
00227             }
00228         }
00229     }
00230 
00231 /*     Figure out optimal block size */
00232 
00233     if (*info == 0 || *info == -10) {
00234 
00235         tpsd = TRUE_;
00236         if (lsame_(trans, "N")) {
00237             tpsd = FALSE_;
00238         }
00239 
00240         if (*m >= *n) {
00241             nb = ilaenv_(&c__1, "CGEQRF", " ", m, n, &c_n1, &c_n1);
00242             if (tpsd) {
00243 /* Computing MAX */
00244                 i__1 = nb, i__2 = ilaenv_(&c__1, "CUNMQR", "LN", m, nrhs, n, &
00245                         c_n1);
00246                 nb = max(i__1,i__2);
00247             } else {
00248 /* Computing MAX */
00249                 i__1 = nb, i__2 = ilaenv_(&c__1, "CUNMQR", "LC", m, nrhs, n, &
00250                         c_n1);
00251                 nb = max(i__1,i__2);
00252             }
00253         } else {
00254             nb = ilaenv_(&c__1, "CGELQF", " ", m, n, &c_n1, &c_n1);
00255             if (tpsd) {
00256 /* Computing MAX */
00257                 i__1 = nb, i__2 = ilaenv_(&c__1, "CUNMLQ", "LC", n, nrhs, m, &
00258                         c_n1);
00259                 nb = max(i__1,i__2);
00260             } else {
00261 /* Computing MAX */
00262                 i__1 = nb, i__2 = ilaenv_(&c__1, "CUNMLQ", "LN", n, nrhs, m, &
00263                         c_n1);
00264                 nb = max(i__1,i__2);
00265             }
00266         }
00267 
00268 /* Computing MAX */
00269         i__1 = 1, i__2 = mn + max(mn,*nrhs) * nb;
00270         wsize = max(i__1,i__2);
00271         r__1 = (real) wsize;
00272         work[1].r = r__1, work[1].i = 0.f;
00273 
00274     }
00275 
00276     if (*info != 0) {
00277         i__1 = -(*info);
00278         xerbla_("CGELS ", &i__1);
00279         return 0;
00280     } else if (lquery) {
00281         return 0;
00282     }
00283 
00284 /*     Quick return if possible */
00285 
00286 /* Computing MIN */
00287     i__1 = min(*m,*n);
00288     if (min(i__1,*nrhs) == 0) {
00289         i__1 = max(*m,*n);
00290         claset_("Full", &i__1, nrhs, &c_b1, &c_b1, &b[b_offset], ldb);
00291         return 0;
00292     }
00293 
00294 /*     Get machine parameters */
00295 
00296     smlnum = slamch_("S") / slamch_("P");
00297     bignum = 1.f / smlnum;
00298     slabad_(&smlnum, &bignum);
00299 
00300 /*     Scale A, B if max element outside range [SMLNUM,BIGNUM] */
00301 
00302     anrm = clange_("M", m, n, &a[a_offset], lda, rwork);
00303     iascl = 0;
00304     if (anrm > 0.f && anrm < smlnum) {
00305 
00306 /*        Scale matrix norm up to SMLNUM */
00307 
00308         clascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda, 
00309                 info);
00310         iascl = 1;
00311     } else if (anrm > bignum) {
00312 
00313 /*        Scale matrix norm down to BIGNUM */
00314 
00315         clascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda, 
00316                 info);
00317         iascl = 2;
00318     } else if (anrm == 0.f) {
00319 
00320 /*        Matrix all zero. Return zero solution. */
00321 
00322         i__1 = max(*m,*n);
00323         claset_("F", &i__1, nrhs, &c_b1, &c_b1, &b[b_offset], ldb);
00324         goto L50;
00325     }
00326 
00327     brow = *m;
00328     if (tpsd) {
00329         brow = *n;
00330     }
00331     bnrm = clange_("M", &brow, nrhs, &b[b_offset], ldb, rwork);
00332     ibscl = 0;
00333     if (bnrm > 0.f && bnrm < smlnum) {
00334 
00335 /*        Scale matrix norm up to SMLNUM */
00336 
00337         clascl_("G", &c__0, &c__0, &bnrm, &smlnum, &brow, nrhs, &b[b_offset], 
00338                 ldb, info);
00339         ibscl = 1;
00340     } else if (bnrm > bignum) {
00341 
00342 /*        Scale matrix norm down to BIGNUM */
00343 
00344         clascl_("G", &c__0, &c__0, &bnrm, &bignum, &brow, nrhs, &b[b_offset], 
00345                 ldb, info);
00346         ibscl = 2;
00347     }
00348 
00349     if (*m >= *n) {
00350 
00351 /*        compute QR factorization of A */
00352 
00353         i__1 = *lwork - mn;
00354         cgeqrf_(m, n, &a[a_offset], lda, &work[1], &work[mn + 1], &i__1, info)
00355                 ;
00356 
00357 /*        workspace at least N, optimally N*NB */
00358 
00359         if (! tpsd) {
00360 
00361 /*           Least-Squares Problem min || A * X - B || */
00362 
00363 /*           B(1:M,1:NRHS) := Q' * B(1:M,1:NRHS) */
00364 
00365             i__1 = *lwork - mn;
00366             cunmqr_("Left", "Conjugate transpose", m, nrhs, n, &a[a_offset], 
00367                     lda, &work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, 
00368                     info);
00369 
00370 /*           workspace at least NRHS, optimally NRHS*NB */
00371 
00372 /*           B(1:N,1:NRHS) := inv(R) * B(1:N,1:NRHS) */
00373 
00374             ctrtrs_("Upper", "No transpose", "Non-unit", n, nrhs, &a[a_offset]
00375 , lda, &b[b_offset], ldb, info);
00376 
00377             if (*info > 0) {
00378                 return 0;
00379             }
00380 
00381             scllen = *n;
00382 
00383         } else {
00384 
00385 /*           Overdetermined system of equations A' * X = B */
00386 
00387 /*           B(1:N,1:NRHS) := inv(R') * B(1:N,1:NRHS) */
00388 
00389             ctrtrs_("Upper", "Conjugate transpose", "Non-unit", n, nrhs, &a[
00390                     a_offset], lda, &b[b_offset], ldb, info);
00391 
00392             if (*info > 0) {
00393                 return 0;
00394             }
00395 
00396 /*           B(N+1:M,1:NRHS) = ZERO */
00397 
00398             i__1 = *nrhs;
00399             for (j = 1; j <= i__1; ++j) {
00400                 i__2 = *m;
00401                 for (i__ = *n + 1; i__ <= i__2; ++i__) {
00402                     i__3 = i__ + j * b_dim1;
00403                     b[i__3].r = 0.f, b[i__3].i = 0.f;
00404 /* L10: */
00405                 }
00406 /* L20: */
00407             }
00408 
00409 /*           B(1:M,1:NRHS) := Q(1:N,:) * B(1:N,1:NRHS) */
00410 
00411             i__1 = *lwork - mn;
00412             cunmqr_("Left", "No transpose", m, nrhs, n, &a[a_offset], lda, &
00413                     work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
00414 
00415 /*           workspace at least NRHS, optimally NRHS*NB */
00416 
00417             scllen = *m;
00418 
00419         }
00420 
00421     } else {
00422 
00423 /*        Compute LQ factorization of A */
00424 
00425         i__1 = *lwork - mn;
00426         cgelqf_(m, n, &a[a_offset], lda, &work[1], &work[mn + 1], &i__1, info)
00427                 ;
00428 
00429 /*        workspace at least M, optimally M*NB. */
00430 
00431         if (! tpsd) {
00432 
00433 /*           underdetermined system of equations A * X = B */
00434 
00435 /*           B(1:M,1:NRHS) := inv(L) * B(1:M,1:NRHS) */
00436 
00437             ctrtrs_("Lower", "No transpose", "Non-unit", m, nrhs, &a[a_offset]
00438 , lda, &b[b_offset], ldb, info);
00439 
00440             if (*info > 0) {
00441                 return 0;
00442             }
00443 
00444 /*           B(M+1:N,1:NRHS) = 0 */
00445 
00446             i__1 = *nrhs;
00447             for (j = 1; j <= i__1; ++j) {
00448                 i__2 = *n;
00449                 for (i__ = *m + 1; i__ <= i__2; ++i__) {
00450                     i__3 = i__ + j * b_dim1;
00451                     b[i__3].r = 0.f, b[i__3].i = 0.f;
00452 /* L30: */
00453                 }
00454 /* L40: */
00455             }
00456 
00457 /*           B(1:N,1:NRHS) := Q(1:N,:)' * B(1:M,1:NRHS) */
00458 
00459             i__1 = *lwork - mn;
00460             cunmlq_("Left", "Conjugate transpose", n, nrhs, m, &a[a_offset], 
00461                     lda, &work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, 
00462                     info);
00463 
00464 /*           workspace at least NRHS, optimally NRHS*NB */
00465 
00466             scllen = *n;
00467 
00468         } else {
00469 
00470 /*           overdetermined system min || A' * X - B || */
00471 
00472 /*           B(1:N,1:NRHS) := Q * B(1:N,1:NRHS) */
00473 
00474             i__1 = *lwork - mn;
00475             cunmlq_("Left", "No transpose", n, nrhs, m, &a[a_offset], lda, &
00476                     work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
00477 
00478 /*           workspace at least NRHS, optimally NRHS*NB */
00479 
00480 /*           B(1:M,1:NRHS) := inv(L') * B(1:M,1:NRHS) */
00481 
00482             ctrtrs_("Lower", "Conjugate transpose", "Non-unit", m, nrhs, &a[
00483                     a_offset], lda, &b[b_offset], ldb, info);
00484 
00485             if (*info > 0) {
00486                 return 0;
00487             }
00488 
00489             scllen = *m;
00490 
00491         }
00492 
00493     }
00494 
00495 /*     Undo scaling */
00496 
00497     if (iascl == 1) {
00498         clascl_("G", &c__0, &c__0, &anrm, &smlnum, &scllen, nrhs, &b[b_offset]
00499 , ldb, info);
00500     } else if (iascl == 2) {
00501         clascl_("G", &c__0, &c__0, &anrm, &bignum, &scllen, nrhs, &b[b_offset]
00502 , ldb, info);
00503     }
00504     if (ibscl == 1) {
00505         clascl_("G", &c__0, &c__0, &smlnum, &bnrm, &scllen, nrhs, &b[b_offset]
00506 , ldb, info);
00507     } else if (ibscl == 2) {
00508         clascl_("G", &c__0, &c__0, &bignum, &bnrm, &scllen, nrhs, &b[b_offset]
00509 , ldb, info);
00510     }
00511 
00512 L50:
00513     r__1 = (real) wsize;
00514     work[1].r = r__1, work[1].i = 0.f;
00515 
00516     return 0;
00517 
00518 /*     End of CGELS */
00519 
00520 } /* cgels_ */


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