dgels.c
Go to the documentation of this file.
00001 /* dgels.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 doublereal c_b33 = 0.;
00021 static integer c__0 = 0;
00022 
00023 /* Subroutine */ int dgels_(char *trans, integer *m, integer *n, integer *
00024         nrhs, doublereal *a, integer *lda, doublereal *b, integer *ldb, 
00025         doublereal *work, 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     doublereal anrm, bnrm;
00033     integer brow;
00034     logical tpsd;
00035     integer iascl, ibscl;
00036     extern logical lsame_(char *, char *);
00037     integer wsize;
00038     doublereal rwork[1];
00039     extern /* Subroutine */ int dlabad_(doublereal *, doublereal *);
00040     extern doublereal dlamch_(char *), dlange_(char *, integer *, 
00041             integer *, doublereal *, integer *, doublereal *);
00042     extern /* Subroutine */ int dgelqf_(integer *, integer *, doublereal *, 
00043             integer *, doublereal *, doublereal *, integer *, integer *), 
00044             dlascl_(char *, integer *, integer *, doublereal *, doublereal *, 
00045             integer *, integer *, doublereal *, integer *, integer *),
00046              dgeqrf_(integer *, integer *, doublereal *, integer *, 
00047             doublereal *, doublereal *, integer *, integer *), dlaset_(char *, 
00048              integer *, integer *, doublereal *, doublereal *, doublereal *, 
00049             integer *), xerbla_(char *, integer *);
00050     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
00051             integer *, integer *);
00052     integer scllen;
00053     doublereal bignum;
00054     extern /* Subroutine */ int dormlq_(char *, char *, integer *, integer *, 
00055             integer *, doublereal *, integer *, doublereal *, doublereal *, 
00056             integer *, doublereal *, integer *, integer *), 
00057             dormqr_(char *, char *, integer *, integer *, integer *, 
00058             doublereal *, integer *, doublereal *, doublereal *, integer *, 
00059             doublereal *, integer *, integer *);
00060     doublereal smlnum;
00061     logical lquery;
00062     extern /* Subroutine */ int dtrtrs_(char *, char *, char *, integer *, 
00063             integer *, doublereal *, integer *, doublereal *, integer *, 
00064             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 /*  DGELS solves overdetermined or underdetermined real linear systems */
00080 /*  involving an M-by-N matrix A, or its transpose, using a QR or LQ */
00081 /*  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 = 'T' and m >= n:  find the minimum norm solution of */
00093 /*     an undetermined system A**T * X = B. */
00094 
00095 /*  4. If TRANS = 'T' and m < n:  find the least squares solution of */
00096 /*     an overdetermined system, i.e., solve the least squares problem */
00097 /*                  minimize || B - A**T * 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 /*          = 'T': the linear system involves A**T. */
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) DOUBLE PRECISION array, dimension (LDA,N) */
00122 /*          On entry, the M-by-N matrix A. */
00123 /*          On exit, */
00124 /*            if M >= N, A is overwritten by details of its QR */
00125 /*                       factorization as returned by DGEQRF; */
00126 /*            if M <  N, A is overwritten by details of its LQ */
00127 /*                       factorization as returned by DGELQF. */
00128 
00129 /*  LDA     (input) INTEGER */
00130 /*          The leading dimension of the array A.  LDA >= max(1,M). */
00131 
00132 /*  B       (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS) */
00133 /*          On entry, the matrix B of right hand side vectors, stored */
00134 /*          columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS */
00135 /*          if TRANS = 'T'. */
00136 /*          On exit, if INFO = 0, B is overwritten by the solution */
00137 /*          vectors, stored columnwise: */
00138 /*          if TRANS = 'N' and m >= n, rows 1 to n of B contain the least */
00139 /*          squares solution vectors; the residual sum of squares for the */
00140 /*          solution in each column is given by the sum of squares of */
00141 /*          elements N+1 to M in that column; */
00142 /*          if TRANS = 'N' and m < n, rows 1 to N 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 /*          minimum norm solution vectors; */
00146 /*          if TRANS = 'T' and m < n, rows 1 to M of B contain the */
00147 /*          least squares solution vectors; the residual sum of squares */
00148 /*          for the solution in each column is given by the sum of */
00149 /*          squares of elements M+1 to N in that column. */
00150 
00151 /*  LDB     (input) INTEGER */
00152 /*          The leading dimension of the array B. LDB >= MAX(1,M,N). */
00153 
00154 /*  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
00155 /*          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
00156 
00157 /*  LWORK   (input) INTEGER */
00158 /*          The dimension of the array WORK. */
00159 /*          LWORK >= max( 1, MN + max( MN, NRHS ) ). */
00160 /*          For optimal performance, */
00161 /*          LWORK >= max( 1, MN + max( MN, NRHS )*NB ). */
00162 /*          where MN = min(M,N) and NB is the optimum block size. */
00163 
00164 /*          If LWORK = -1, then a workspace query is assumed; the routine */
00165 /*          only calculates the optimal size of the WORK array, returns */
00166 /*          this value as the first entry of the WORK array, and no error */
00167 /*          message related to LWORK is issued by XERBLA. */
00168 
00169 /*  INFO    (output) INTEGER */
00170 /*          = 0:  successful exit */
00171 /*          < 0:  if INFO = -i, the i-th argument had an illegal value */
00172 /*          > 0:  if INFO =  i, the i-th diagonal element of the */
00173 /*                triangular factor of A is zero, so that A does not have */
00174 /*                full rank; the least squares solution could not be */
00175 /*                computed. */
00176 
00177 /*  ===================================================================== */
00178 
00179 /*     .. Parameters .. */
00180 /*     .. */
00181 /*     .. Local Scalars .. */
00182 /*     .. */
00183 /*     .. Local Arrays .. */
00184 /*     .. */
00185 /*     .. External Functions .. */
00186 /*     .. */
00187 /*     .. External Subroutines .. */
00188 /*     .. */
00189 /*     .. Intrinsic Functions .. */
00190 /*     .. */
00191 /*     .. Executable Statements .. */
00192 
00193 /*     Test the input arguments. */
00194 
00195     /* Parameter adjustments */
00196     a_dim1 = *lda;
00197     a_offset = 1 + a_dim1;
00198     a -= a_offset;
00199     b_dim1 = *ldb;
00200     b_offset = 1 + b_dim1;
00201     b -= b_offset;
00202     --work;
00203 
00204     /* Function Body */
00205     *info = 0;
00206     mn = min(*m,*n);
00207     lquery = *lwork == -1;
00208     if (! (lsame_(trans, "N") || lsame_(trans, "T"))) {
00209         *info = -1;
00210     } else if (*m < 0) {
00211         *info = -2;
00212     } else if (*n < 0) {
00213         *info = -3;
00214     } else if (*nrhs < 0) {
00215         *info = -4;
00216     } else if (*lda < max(1,*m)) {
00217         *info = -6;
00218     } else /* if(complicated condition) */ {
00219 /* Computing MAX */
00220         i__1 = max(1,*m);
00221         if (*ldb < max(i__1,*n)) {
00222             *info = -8;
00223         } else /* if(complicated condition) */ {
00224 /* Computing MAX */
00225             i__1 = 1, i__2 = mn + max(mn,*nrhs);
00226             if (*lwork < max(i__1,i__2) && ! lquery) {
00227                 *info = -10;
00228             }
00229         }
00230     }
00231 
00232 /*     Figure out optimal block size */
00233 
00234     if (*info == 0 || *info == -10) {
00235 
00236         tpsd = TRUE_;
00237         if (lsame_(trans, "N")) {
00238             tpsd = FALSE_;
00239         }
00240 
00241         if (*m >= *n) {
00242             nb = ilaenv_(&c__1, "DGEQRF", " ", m, n, &c_n1, &c_n1);
00243             if (tpsd) {
00244 /* Computing MAX */
00245                 i__1 = nb, i__2 = ilaenv_(&c__1, "DORMQR", "LN", m, nrhs, n, &
00246                         c_n1);
00247                 nb = max(i__1,i__2);
00248             } else {
00249 /* Computing MAX */
00250                 i__1 = nb, i__2 = ilaenv_(&c__1, "DORMQR", "LT", m, nrhs, n, &
00251                         c_n1);
00252                 nb = max(i__1,i__2);
00253             }
00254         } else {
00255             nb = ilaenv_(&c__1, "DGELQF", " ", m, n, &c_n1, &c_n1);
00256             if (tpsd) {
00257 /* Computing MAX */
00258                 i__1 = nb, i__2 = ilaenv_(&c__1, "DORMLQ", "LT", n, nrhs, m, &
00259                         c_n1);
00260                 nb = max(i__1,i__2);
00261             } else {
00262 /* Computing MAX */
00263                 i__1 = nb, i__2 = ilaenv_(&c__1, "DORMLQ", "LN", n, nrhs, m, &
00264                         c_n1);
00265                 nb = max(i__1,i__2);
00266             }
00267         }
00268 
00269 /* Computing MAX */
00270         i__1 = 1, i__2 = mn + max(mn,*nrhs) * nb;
00271         wsize = max(i__1,i__2);
00272         work[1] = (doublereal) wsize;
00273 
00274     }
00275 
00276     if (*info != 0) {
00277         i__1 = -(*info);
00278         xerbla_("DGELS ", &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         dlaset_("Full", &i__1, nrhs, &c_b33, &c_b33, &b[b_offset], ldb);
00291         return 0;
00292     }
00293 
00294 /*     Get machine parameters */
00295 
00296     smlnum = dlamch_("S") / dlamch_("P");
00297     bignum = 1. / smlnum;
00298     dlabad_(&smlnum, &bignum);
00299 
00300 /*     Scale A, B if max element outside range [SMLNUM,BIGNUM] */
00301 
00302     anrm = dlange_("M", m, n, &a[a_offset], lda, rwork);
00303     iascl = 0;
00304     if (anrm > 0. && anrm < smlnum) {
00305 
00306 /*        Scale matrix norm up to SMLNUM */
00307 
00308         dlascl_("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         dlascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda, 
00316                 info);
00317         iascl = 2;
00318     } else if (anrm == 0.) {
00319 
00320 /*        Matrix all zero. Return zero solution. */
00321 
00322         i__1 = max(*m,*n);
00323         dlaset_("F", &i__1, nrhs, &c_b33, &c_b33, &b[b_offset], ldb);
00324         goto L50;
00325     }
00326 
00327     brow = *m;
00328     if (tpsd) {
00329         brow = *n;
00330     }
00331     bnrm = dlange_("M", &brow, nrhs, &b[b_offset], ldb, rwork);
00332     ibscl = 0;
00333     if (bnrm > 0. && bnrm < smlnum) {
00334 
00335 /*        Scale matrix norm up to SMLNUM */
00336 
00337         dlascl_("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         dlascl_("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         dgeqrf_(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             dormqr_("Left", "Transpose", m, nrhs, n, &a[a_offset], lda, &work[
00367                     1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
00368 
00369 /*           workspace at least NRHS, optimally NRHS*NB */
00370 
00371 /*           B(1:N,1:NRHS) := inv(R) * B(1:N,1:NRHS) */
00372 
00373             dtrtrs_("Upper", "No transpose", "Non-unit", n, nrhs, &a[a_offset]
00374 , lda, &b[b_offset], ldb, info);
00375 
00376             if (*info > 0) {
00377                 return 0;
00378             }
00379 
00380             scllen = *n;
00381 
00382         } else {
00383 
00384 /*           Overdetermined system of equations A' * X = B */
00385 
00386 /*           B(1:N,1:NRHS) := inv(R') * B(1:N,1:NRHS) */
00387 
00388             dtrtrs_("Upper", "Transpose", "Non-unit", n, nrhs, &a[a_offset], 
00389                     lda, &b[b_offset], ldb, info);
00390 
00391             if (*info > 0) {
00392                 return 0;
00393             }
00394 
00395 /*           B(N+1:M,1:NRHS) = ZERO */
00396 
00397             i__1 = *nrhs;
00398             for (j = 1; j <= i__1; ++j) {
00399                 i__2 = *m;
00400                 for (i__ = *n + 1; i__ <= i__2; ++i__) {
00401                     b[i__ + j * b_dim1] = 0.;
00402 /* L10: */
00403                 }
00404 /* L20: */
00405             }
00406 
00407 /*           B(1:M,1:NRHS) := Q(1:N,:) * B(1:N,1:NRHS) */
00408 
00409             i__1 = *lwork - mn;
00410             dormqr_("Left", "No transpose", m, nrhs, n, &a[a_offset], lda, &
00411                     work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
00412 
00413 /*           workspace at least NRHS, optimally NRHS*NB */
00414 
00415             scllen = *m;
00416 
00417         }
00418 
00419     } else {
00420 
00421 /*        Compute LQ factorization of A */
00422 
00423         i__1 = *lwork - mn;
00424         dgelqf_(m, n, &a[a_offset], lda, &work[1], &work[mn + 1], &i__1, info)
00425                 ;
00426 
00427 /*        workspace at least M, optimally M*NB. */
00428 
00429         if (! tpsd) {
00430 
00431 /*           underdetermined system of equations A * X = B */
00432 
00433 /*           B(1:M,1:NRHS) := inv(L) * B(1:M,1:NRHS) */
00434 
00435             dtrtrs_("Lower", "No transpose", "Non-unit", m, nrhs, &a[a_offset]
00436 , lda, &b[b_offset], ldb, info);
00437 
00438             if (*info > 0) {
00439                 return 0;
00440             }
00441 
00442 /*           B(M+1:N,1:NRHS) = 0 */
00443 
00444             i__1 = *nrhs;
00445             for (j = 1; j <= i__1; ++j) {
00446                 i__2 = *n;
00447                 for (i__ = *m + 1; i__ <= i__2; ++i__) {
00448                     b[i__ + j * b_dim1] = 0.;
00449 /* L30: */
00450                 }
00451 /* L40: */
00452             }
00453 
00454 /*           B(1:N,1:NRHS) := Q(1:N,:)' * B(1:M,1:NRHS) */
00455 
00456             i__1 = *lwork - mn;
00457             dormlq_("Left", "Transpose", n, nrhs, m, &a[a_offset], lda, &work[
00458                     1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
00459 
00460 /*           workspace at least NRHS, optimally NRHS*NB */
00461 
00462             scllen = *n;
00463 
00464         } else {
00465 
00466 /*           overdetermined system min || A' * X - B || */
00467 
00468 /*           B(1:N,1:NRHS) := Q * B(1:N,1:NRHS) */
00469 
00470             i__1 = *lwork - mn;
00471             dormlq_("Left", "No transpose", n, nrhs, m, &a[a_offset], lda, &
00472                     work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
00473 
00474 /*           workspace at least NRHS, optimally NRHS*NB */
00475 
00476 /*           B(1:M,1:NRHS) := inv(L') * B(1:M,1:NRHS) */
00477 
00478             dtrtrs_("Lower", "Transpose", "Non-unit", m, nrhs, &a[a_offset], 
00479                     lda, &b[b_offset], ldb, info);
00480 
00481             if (*info > 0) {
00482                 return 0;
00483             }
00484 
00485             scllen = *m;
00486 
00487         }
00488 
00489     }
00490 
00491 /*     Undo scaling */
00492 
00493     if (iascl == 1) {
00494         dlascl_("G", &c__0, &c__0, &anrm, &smlnum, &scllen, nrhs, &b[b_offset]
00495 , ldb, info);
00496     } else if (iascl == 2) {
00497         dlascl_("G", &c__0, &c__0, &anrm, &bignum, &scllen, nrhs, &b[b_offset]
00498 , ldb, info);
00499     }
00500     if (ibscl == 1) {
00501         dlascl_("G", &c__0, &c__0, &smlnum, &bnrm, &scllen, nrhs, &b[b_offset]
00502 , ldb, info);
00503     } else if (ibscl == 2) {
00504         dlascl_("G", &c__0, &c__0, &bignum, &bnrm, &scllen, nrhs, &b[b_offset]
00505 , ldb, info);
00506     }
00507 
00508 L50:
00509     work[1] = (doublereal) wsize;
00510 
00511     return 0;
00512 
00513 /*     End of DGELS */
00514 
00515 } /* dgels_ */


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