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


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