sgelsx.c
Go to the documentation of this file.
00001 /* sgelsx.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__0 = 0;
00019 static real c_b13 = 0.f;
00020 static integer c__2 = 2;
00021 static integer c__1 = 1;
00022 static real c_b36 = 1.f;
00023 
00024 /* Subroutine */ int sgelsx_(integer *m, integer *n, integer *nrhs, real *a, 
00025         integer *lda, real *b, integer *ldb, integer *jpvt, real *rcond, 
00026         integer *rank, real *work, integer *info)
00027 {
00028     /* System generated locals */
00029     integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2;
00030     real r__1;
00031 
00032     /* Local variables */
00033     integer i__, j, k;
00034     real c1, c2, s1, s2, t1, t2;
00035     integer mn;
00036     real anrm, bnrm, smin, smax;
00037     integer iascl, ibscl, ismin, ismax;
00038     extern /* Subroutine */ int strsm_(char *, char *, char *, char *, 
00039             integer *, integer *, real *, real *, integer *, real *, integer *
00040 ), slaic1_(integer *, integer *, 
00041             real *, real *, real *, real *, real *, real *, real *), sorm2r_(
00042             char *, char *, integer *, integer *, integer *, real *, integer *
00043 , real *, real *, integer *, real *, integer *), 
00044             slabad_(real *, real *);
00045     extern doublereal slamch_(char *), slange_(char *, integer *, 
00046             integer *, real *, integer *, real *);
00047     extern /* Subroutine */ int xerbla_(char *, integer *);
00048     real bignum;
00049     extern /* Subroutine */ int slascl_(char *, integer *, integer *, real *, 
00050             real *, integer *, integer *, real *, integer *, integer *), sgeqpf_(integer *, integer *, real *, integer *, integer 
00051             *, real *, real *, integer *), slaset_(char *, integer *, integer 
00052             *, real *, real *, real *, integer *);
00053     real sminpr, smaxpr, smlnum;
00054     extern /* Subroutine */ int slatzm_(char *, integer *, integer *, real *, 
00055             integer *, real *, real *, real *, integer *, real *), 
00056             stzrqf_(integer *, integer *, real *, integer *, real *, integer *
00057 );
00058 
00059 
00060 /*  -- LAPACK driver routine (version 3.2) -- */
00061 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00062 /*     November 2006 */
00063 
00064 /*     .. Scalar Arguments .. */
00065 /*     .. */
00066 /*     .. Array Arguments .. */
00067 /*     .. */
00068 
00069 /*  Purpose */
00070 /*  ======= */
00071 
00072 /*  This routine is deprecated and has been replaced by routine SGELSY. */
00073 
00074 /*  SGELSX computes the minimum-norm solution to a real linear least */
00075 /*  squares problem: */
00076 /*      minimize || A * X - B || */
00077 /*  using a complete orthogonal factorization of A.  A is an M-by-N */
00078 /*  matrix which may be rank-deficient. */
00079 
00080 /*  Several right hand side vectors b and solution vectors x can be */
00081 /*  handled in a single call; they are stored as the columns of the */
00082 /*  M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
00083 /*  matrix X. */
00084 
00085 /*  The routine first computes a QR factorization with column pivoting: */
00086 /*      A * P = Q * [ R11 R12 ] */
00087 /*                  [  0  R22 ] */
00088 /*  with R11 defined as the largest leading submatrix whose estimated */
00089 /*  condition number is less than 1/RCOND.  The order of R11, RANK, */
00090 /*  is the effective rank of A. */
00091 
00092 /*  Then, R22 is considered to be negligible, and R12 is annihilated */
00093 /*  by orthogonal transformations from the right, arriving at the */
00094 /*  complete orthogonal factorization: */
00095 /*     A * P = Q * [ T11 0 ] * Z */
00096 /*                 [  0  0 ] */
00097 /*  The minimum-norm solution is then */
00098 /*     X = P * Z' [ inv(T11)*Q1'*B ] */
00099 /*                [        0       ] */
00100 /*  where Q1 consists of the first RANK columns of Q. */
00101 
00102 /*  Arguments */
00103 /*  ========= */
00104 
00105 /*  M       (input) INTEGER */
00106 /*          The number of rows of the matrix A.  M >= 0. */
00107 
00108 /*  N       (input) INTEGER */
00109 /*          The number of columns of the matrix A.  N >= 0. */
00110 
00111 /*  NRHS    (input) INTEGER */
00112 /*          The number of right hand sides, i.e., the number of */
00113 /*          columns of matrices B and X. NRHS >= 0. */
00114 
00115 /*  A       (input/output) REAL array, dimension (LDA,N) */
00116 /*          On entry, the M-by-N matrix A. */
00117 /*          On exit, A has been overwritten by details of its */
00118 /*          complete orthogonal factorization. */
00119 
00120 /*  LDA     (input) INTEGER */
00121 /*          The leading dimension of the array A.  LDA >= max(1,M). */
00122 
00123 /*  B       (input/output) REAL array, dimension (LDB,NRHS) */
00124 /*          On entry, the M-by-NRHS right hand side matrix B. */
00125 /*          On exit, the N-by-NRHS solution matrix X. */
00126 /*          If m >= n and RANK = n, the residual sum-of-squares for */
00127 /*          the solution in the i-th column is given by the sum of */
00128 /*          squares of elements N+1:M in that column. */
00129 
00130 /*  LDB     (input) INTEGER */
00131 /*          The leading dimension of the array B. LDB >= max(1,M,N). */
00132 
00133 /*  JPVT    (input/output) INTEGER array, dimension (N) */
00134 /*          On entry, if JPVT(i) .ne. 0, the i-th column of A is an */
00135 /*          initial column, otherwise it is a free column.  Before */
00136 /*          the QR factorization of A, all initial columns are */
00137 /*          permuted to the leading positions; only the remaining */
00138 /*          free columns are moved as a result of column pivoting */
00139 /*          during the factorization. */
00140 /*          On exit, if JPVT(i) = k, then the i-th column of A*P */
00141 /*          was the k-th column of A. */
00142 
00143 /*  RCOND   (input) REAL */
00144 /*          RCOND is used to determine the effective rank of A, which */
00145 /*          is defined as the order of the largest leading triangular */
00146 /*          submatrix R11 in the QR factorization with pivoting of A, */
00147 /*          whose estimated condition number < 1/RCOND. */
00148 
00149 /*  RANK    (output) INTEGER */
00150 /*          The effective rank of A, i.e., the order of the submatrix */
00151 /*          R11.  This is the same as the order of the submatrix T11 */
00152 /*          in the complete orthogonal factorization of A. */
00153 
00154 /*  WORK    (workspace) REAL array, dimension */
00155 /*                      (max( min(M,N)+3*N, 2*min(M,N)+NRHS )), */
00156 
00157 /*  INFO    (output) INTEGER */
00158 /*          = 0:  successful exit */
00159 /*          < 0:  if INFO = -i, the i-th argument had an illegal value */
00160 
00161 /*  ===================================================================== */
00162 
00163 /*     .. Parameters .. */
00164 /*     .. */
00165 /*     .. Local Scalars .. */
00166 /*     .. */
00167 /*     .. External Functions .. */
00168 /*     .. */
00169 /*     .. External Subroutines .. */
00170 /*     .. */
00171 /*     .. Intrinsic Functions .. */
00172 /*     .. */
00173 /*     .. Executable Statements .. */
00174 
00175     /* Parameter adjustments */
00176     a_dim1 = *lda;
00177     a_offset = 1 + a_dim1;
00178     a -= a_offset;
00179     b_dim1 = *ldb;
00180     b_offset = 1 + b_dim1;
00181     b -= b_offset;
00182     --jpvt;
00183     --work;
00184 
00185     /* Function Body */
00186     mn = min(*m,*n);
00187     ismin = mn + 1;
00188     ismax = (mn << 1) + 1;
00189 
00190 /*     Test the input arguments. */
00191 
00192     *info = 0;
00193     if (*m < 0) {
00194         *info = -1;
00195     } else if (*n < 0) {
00196         *info = -2;
00197     } else if (*nrhs < 0) {
00198         *info = -3;
00199     } else if (*lda < max(1,*m)) {
00200         *info = -5;
00201     } else /* if(complicated condition) */ {
00202 /* Computing MAX */
00203         i__1 = max(1,*m);
00204         if (*ldb < max(i__1,*n)) {
00205             *info = -7;
00206         }
00207     }
00208 
00209     if (*info != 0) {
00210         i__1 = -(*info);
00211         xerbla_("SGELSX", &i__1);
00212         return 0;
00213     }
00214 
00215 /*     Quick return if possible */
00216 
00217 /* Computing MIN */
00218     i__1 = min(*m,*n);
00219     if (min(i__1,*nrhs) == 0) {
00220         *rank = 0;
00221         return 0;
00222     }
00223 
00224 /*     Get machine parameters */
00225 
00226     smlnum = slamch_("S") / slamch_("P");
00227     bignum = 1.f / smlnum;
00228     slabad_(&smlnum, &bignum);
00229 
00230 /*     Scale A, B if max elements outside range [SMLNUM,BIGNUM] */
00231 
00232     anrm = slange_("M", m, n, &a[a_offset], lda, &work[1]);
00233     iascl = 0;
00234     if (anrm > 0.f && anrm < smlnum) {
00235 
00236 /*        Scale matrix norm up to SMLNUM */
00237 
00238         slascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda, 
00239                 info);
00240         iascl = 1;
00241     } else if (anrm > bignum) {
00242 
00243 /*        Scale matrix norm down to BIGNUM */
00244 
00245         slascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda, 
00246                 info);
00247         iascl = 2;
00248     } else if (anrm == 0.f) {
00249 
00250 /*        Matrix all zero. Return zero solution. */
00251 
00252         i__1 = max(*m,*n);
00253         slaset_("F", &i__1, nrhs, &c_b13, &c_b13, &b[b_offset], ldb);
00254         *rank = 0;
00255         goto L100;
00256     }
00257 
00258     bnrm = slange_("M", m, nrhs, &b[b_offset], ldb, &work[1]);
00259     ibscl = 0;
00260     if (bnrm > 0.f && bnrm < smlnum) {
00261 
00262 /*        Scale matrix norm up to SMLNUM */
00263 
00264         slascl_("G", &c__0, &c__0, &bnrm, &smlnum, m, nrhs, &b[b_offset], ldb, 
00265                  info);
00266         ibscl = 1;
00267     } else if (bnrm > bignum) {
00268 
00269 /*        Scale matrix norm down to BIGNUM */
00270 
00271         slascl_("G", &c__0, &c__0, &bnrm, &bignum, m, nrhs, &b[b_offset], ldb, 
00272                  info);
00273         ibscl = 2;
00274     }
00275 
00276 /*     Compute QR factorization with column pivoting of A: */
00277 /*        A * P = Q * R */
00278 
00279     sgeqpf_(m, n, &a[a_offset], lda, &jpvt[1], &work[1], &work[mn + 1], info);
00280 
00281 /*     workspace 3*N. Details of Householder rotations stored */
00282 /*     in WORK(1:MN). */
00283 
00284 /*     Determine RANK using incremental condition estimation */
00285 
00286     work[ismin] = 1.f;
00287     work[ismax] = 1.f;
00288     smax = (r__1 = a[a_dim1 + 1], dabs(r__1));
00289     smin = smax;
00290     if ((r__1 = a[a_dim1 + 1], dabs(r__1)) == 0.f) {
00291         *rank = 0;
00292         i__1 = max(*m,*n);
00293         slaset_("F", &i__1, nrhs, &c_b13, &c_b13, &b[b_offset], ldb);
00294         goto L100;
00295     } else {
00296         *rank = 1;
00297     }
00298 
00299 L10:
00300     if (*rank < mn) {
00301         i__ = *rank + 1;
00302         slaic1_(&c__2, rank, &work[ismin], &smin, &a[i__ * a_dim1 + 1], &a[
00303                 i__ + i__ * a_dim1], &sminpr, &s1, &c1);
00304         slaic1_(&c__1, rank, &work[ismax], &smax, &a[i__ * a_dim1 + 1], &a[
00305                 i__ + i__ * a_dim1], &smaxpr, &s2, &c2);
00306 
00307         if (smaxpr * *rcond <= sminpr) {
00308             i__1 = *rank;
00309             for (i__ = 1; i__ <= i__1; ++i__) {
00310                 work[ismin + i__ - 1] = s1 * work[ismin + i__ - 1];
00311                 work[ismax + i__ - 1] = s2 * work[ismax + i__ - 1];
00312 /* L20: */
00313             }
00314             work[ismin + *rank] = c1;
00315             work[ismax + *rank] = c2;
00316             smin = sminpr;
00317             smax = smaxpr;
00318             ++(*rank);
00319             goto L10;
00320         }
00321     }
00322 
00323 /*     Logically partition R = [ R11 R12 ] */
00324 /*                             [  0  R22 ] */
00325 /*     where R11 = R(1:RANK,1:RANK) */
00326 
00327 /*     [R11,R12] = [ T11, 0 ] * Y */
00328 
00329     if (*rank < *n) {
00330         stzrqf_(rank, n, &a[a_offset], lda, &work[mn + 1], info);
00331     }
00332 
00333 /*     Details of Householder rotations stored in WORK(MN+1:2*MN) */
00334 
00335 /*     B(1:M,1:NRHS) := Q' * B(1:M,1:NRHS) */
00336 
00337     sorm2r_("Left", "Transpose", m, nrhs, &mn, &a[a_offset], lda, &work[1], &
00338             b[b_offset], ldb, &work[(mn << 1) + 1], info);
00339 
00340 /*     workspace NRHS */
00341 
00342 /*     B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS) */
00343 
00344     strsm_("Left", "Upper", "No transpose", "Non-unit", rank, nrhs, &c_b36, &
00345             a[a_offset], lda, &b[b_offset], ldb);
00346 
00347     i__1 = *n;
00348     for (i__ = *rank + 1; i__ <= i__1; ++i__) {
00349         i__2 = *nrhs;
00350         for (j = 1; j <= i__2; ++j) {
00351             b[i__ + j * b_dim1] = 0.f;
00352 /* L30: */
00353         }
00354 /* L40: */
00355     }
00356 
00357 /*     B(1:N,1:NRHS) := Y' * B(1:N,1:NRHS) */
00358 
00359     if (*rank < *n) {
00360         i__1 = *rank;
00361         for (i__ = 1; i__ <= i__1; ++i__) {
00362             i__2 = *n - *rank + 1;
00363             slatzm_("Left", &i__2, nrhs, &a[i__ + (*rank + 1) * a_dim1], lda, 
00364                     &work[mn + i__], &b[i__ + b_dim1], &b[*rank + 1 + b_dim1], 
00365                      ldb, &work[(mn << 1) + 1]);
00366 /* L50: */
00367         }
00368     }
00369 
00370 /*     workspace NRHS */
00371 
00372 /*     B(1:N,1:NRHS) := P * B(1:N,1:NRHS) */
00373 
00374     i__1 = *nrhs;
00375     for (j = 1; j <= i__1; ++j) {
00376         i__2 = *n;
00377         for (i__ = 1; i__ <= i__2; ++i__) {
00378             work[(mn << 1) + i__] = 1.f;
00379 /* L60: */
00380         }
00381         i__2 = *n;
00382         for (i__ = 1; i__ <= i__2; ++i__) {
00383             if (work[(mn << 1) + i__] == 1.f) {
00384                 if (jpvt[i__] != i__) {
00385                     k = i__;
00386                     t1 = b[k + j * b_dim1];
00387                     t2 = b[jpvt[k] + j * b_dim1];
00388 L70:
00389                     b[jpvt[k] + j * b_dim1] = t1;
00390                     work[(mn << 1) + k] = 0.f;
00391                     t1 = t2;
00392                     k = jpvt[k];
00393                     t2 = b[jpvt[k] + j * b_dim1];
00394                     if (jpvt[k] != i__) {
00395                         goto L70;
00396                     }
00397                     b[i__ + j * b_dim1] = t1;
00398                     work[(mn << 1) + k] = 0.f;
00399                 }
00400             }
00401 /* L80: */
00402         }
00403 /* L90: */
00404     }
00405 
00406 /*     Undo scaling */
00407 
00408     if (iascl == 1) {
00409         slascl_("G", &c__0, &c__0, &anrm, &smlnum, n, nrhs, &b[b_offset], ldb, 
00410                  info);
00411         slascl_("U", &c__0, &c__0, &smlnum, &anrm, rank, rank, &a[a_offset], 
00412                 lda, info);
00413     } else if (iascl == 2) {
00414         slascl_("G", &c__0, &c__0, &anrm, &bignum, n, nrhs, &b[b_offset], ldb, 
00415                  info);
00416         slascl_("U", &c__0, &c__0, &bignum, &anrm, rank, rank, &a[a_offset], 
00417                 lda, info);
00418     }
00419     if (ibscl == 1) {
00420         slascl_("G", &c__0, &c__0, &smlnum, &bnrm, n, nrhs, &b[b_offset], ldb, 
00421                  info);
00422     } else if (ibscl == 2) {
00423         slascl_("G", &c__0, &c__0, &bignum, &bnrm, n, nrhs, &b[b_offset], ldb, 
00424                  info);
00425     }
00426 
00427 L100:
00428 
00429     return 0;
00430 
00431 /*     End of SGELSX */
00432 
00433 } /* sgelsx_ */


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