sgelsy.c
Go to the documentation of this file.
00001 /* sgelsy.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 integer c__0 = 0;
00021 static real c_b31 = 0.f;
00022 static integer c__2 = 2;
00023 static real c_b54 = 1.f;
00024 
00025 /* Subroutine */ int sgelsy_(integer *m, integer *n, integer *nrhs, real *a, 
00026         integer *lda, real *b, integer *ldb, integer *jpvt, real *rcond, 
00027         integer *rank, real *work, integer *lwork, integer *info)
00028 {
00029     /* System generated locals */
00030     integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2;
00031     real r__1, r__2;
00032 
00033     /* Local variables */
00034     integer i__, j;
00035     real c1, c2, s1, s2;
00036     integer nb, mn, nb1, nb2, nb3, nb4;
00037     real anrm, bnrm, smin, smax;
00038     integer iascl, ibscl, ismin, ismax;
00039     extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *, 
00040             integer *);
00041     real wsize;
00042     extern /* Subroutine */ int strsm_(char *, char *, char *, char *, 
00043             integer *, integer *, real *, real *, integer *, real *, integer *
00044 ), slaic1_(integer *, integer *, 
00045             real *, real *, real *, real *, real *, real *, real *), sgeqp3_(
00046             integer *, integer *, real *, integer *, integer *, real *, real *
00047 , integer *, integer *), slabad_(real *, real *);
00048     extern doublereal slamch_(char *), slange_(char *, integer *, 
00049             integer *, real *, integer *, real *);
00050     extern /* Subroutine */ int xerbla_(char *, integer *);
00051     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
00052             integer *, integer *);
00053     real bignum;
00054     extern /* Subroutine */ int slascl_(char *, integer *, integer *, real *, 
00055             real *, integer *, integer *, real *, integer *, integer *), slaset_(char *, integer *, integer *, real *, real *, 
00056             real *, integer *);
00057     integer lwkmin;
00058     real sminpr, smaxpr, smlnum;
00059     integer lwkopt;
00060     logical lquery;
00061     extern /* Subroutine */ int sormqr_(char *, char *, integer *, integer *, 
00062             integer *, real *, integer *, real *, real *, integer *, real *, 
00063             integer *, integer *), sormrz_(char *, char *, 
00064             integer *, integer *, integer *, integer *, real *, integer *, 
00065             real *, real *, integer *, real *, integer *, integer *), stzrzf_(integer *, integer *, real *, integer *, real *, 
00066             real *, integer *, integer *);
00067 
00068 
00069 /*  -- LAPACK driver routine (version 3.2) -- */
00070 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00071 /*     November 2006 */
00072 
00073 /*     .. Scalar Arguments .. */
00074 /*     .. */
00075 /*     .. Array Arguments .. */
00076 /*     .. */
00077 
00078 /*  Purpose */
00079 /*  ======= */
00080 
00081 /*  SGELSY computes the minimum-norm solution to a real linear least */
00082 /*  squares problem: */
00083 /*      minimize || A * X - B || */
00084 /*  using a complete orthogonal factorization of A.  A is an M-by-N */
00085 /*  matrix which may be rank-deficient. */
00086 
00087 /*  Several right hand side vectors b and solution vectors x can be */
00088 /*  handled in a single call; they are stored as the columns of the */
00089 /*  M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
00090 /*  matrix X. */
00091 
00092 /*  The routine first computes a QR factorization with column pivoting: */
00093 /*      A * P = Q * [ R11 R12 ] */
00094 /*                  [  0  R22 ] */
00095 /*  with R11 defined as the largest leading submatrix whose estimated */
00096 /*  condition number is less than 1/RCOND.  The order of R11, RANK, */
00097 /*  is the effective rank of A. */
00098 
00099 /*  Then, R22 is considered to be negligible, and R12 is annihilated */
00100 /*  by orthogonal transformations from the right, arriving at the */
00101 /*  complete orthogonal factorization: */
00102 /*     A * P = Q * [ T11 0 ] * Z */
00103 /*                 [  0  0 ] */
00104 /*  The minimum-norm solution is then */
00105 /*     X = P * Z' [ inv(T11)*Q1'*B ] */
00106 /*                [        0       ] */
00107 /*  where Q1 consists of the first RANK columns of Q. */
00108 
00109 /*  This routine is basically identical to the original xGELSX except */
00110 /*  three differences: */
00111 /*    o The call to the subroutine xGEQPF has been substituted by the */
00112 /*      the call to the subroutine xGEQP3. This subroutine is a Blas-3 */
00113 /*      version of the QR factorization with column pivoting. */
00114 /*    o Matrix B (the right hand side) is updated with Blas-3. */
00115 /*    o The permutation of matrix B (the right hand side) is faster and */
00116 /*      more simple. */
00117 
00118 /*  Arguments */
00119 /*  ========= */
00120 
00121 /*  M       (input) INTEGER */
00122 /*          The number of rows of the matrix A.  M >= 0. */
00123 
00124 /*  N       (input) INTEGER */
00125 /*          The number of columns of the matrix A.  N >= 0. */
00126 
00127 /*  NRHS    (input) INTEGER */
00128 /*          The number of right hand sides, i.e., the number of */
00129 /*          columns of matrices B and X. NRHS >= 0. */
00130 
00131 /*  A       (input/output) REAL array, dimension (LDA,N) */
00132 /*          On entry, the M-by-N matrix A. */
00133 /*          On exit, A has been overwritten by details of its */
00134 /*          complete orthogonal factorization. */
00135 
00136 /*  LDA     (input) INTEGER */
00137 /*          The leading dimension of the array A.  LDA >= max(1,M). */
00138 
00139 /*  B       (input/output) REAL array, dimension (LDB,NRHS) */
00140 /*          On entry, the M-by-NRHS right hand side matrix B. */
00141 /*          On exit, the N-by-NRHS solution matrix X. */
00142 
00143 /*  LDB     (input) INTEGER */
00144 /*          The leading dimension of the array B. LDB >= max(1,M,N). */
00145 
00146 /*  JPVT    (input/output) INTEGER array, dimension (N) */
00147 /*          On entry, if JPVT(i) .ne. 0, the i-th column of A is permuted */
00148 /*          to the front of AP, otherwise column i is a free column. */
00149 /*          On exit, if JPVT(i) = k, then the i-th column of AP */
00150 /*          was the k-th column of A. */
00151 
00152 /*  RCOND   (input) REAL */
00153 /*          RCOND is used to determine the effective rank of A, which */
00154 /*          is defined as the order of the largest leading triangular */
00155 /*          submatrix R11 in the QR factorization with pivoting of A, */
00156 /*          whose estimated condition number < 1/RCOND. */
00157 
00158 /*  RANK    (output) INTEGER */
00159 /*          The effective rank of A, i.e., the order of the submatrix */
00160 /*          R11.  This is the same as the order of the submatrix T11 */
00161 /*          in the complete orthogonal factorization of A. */
00162 
00163 /*  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK)) */
00164 /*          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
00165 
00166 /*  LWORK   (input) INTEGER */
00167 /*          The dimension of the array WORK. */
00168 /*          The unblocked strategy requires that: */
00169 /*             LWORK >= MAX( MN+3*N+1, 2*MN+NRHS ), */
00170 /*          where MN = min( M, N ). */
00171 /*          The block algorithm requires that: */
00172 /*             LWORK >= MAX( MN+2*N+NB*(N+1), 2*MN+NB*NRHS ), */
00173 /*          where NB is an upper bound on the blocksize returned */
00174 /*          by ILAENV for the routines SGEQP3, STZRZF, STZRQF, SORMQR, */
00175 /*          and SORMRZ. */
00176 
00177 /*          If LWORK = -1, then a workspace query is assumed; the routine */
00178 /*          only calculates the optimal size of the WORK array, returns */
00179 /*          this value as the first entry of the WORK array, and no error */
00180 /*          message related to LWORK is issued by XERBLA. */
00181 
00182 /*  INFO    (output) INTEGER */
00183 /*          = 0: successful exit */
00184 /*          < 0: If INFO = -i, the i-th argument had an illegal value. */
00185 
00186 /*  Further Details */
00187 /*  =============== */
00188 
00189 /*  Based on contributions by */
00190 /*    A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA */
00191 /*    E. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain */
00192 /*    G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain */
00193 
00194 /*  ===================================================================== */
00195 
00196 /*     .. Parameters .. */
00197 /*     .. */
00198 /*     .. Local Scalars .. */
00199 /*     .. */
00200 /*     .. External Functions .. */
00201 /*     .. */
00202 /*     .. External Subroutines .. */
00203 /*     .. */
00204 /*     .. Intrinsic Functions .. */
00205 /*     .. */
00206 /*     .. Executable Statements .. */
00207 
00208     /* Parameter adjustments */
00209     a_dim1 = *lda;
00210     a_offset = 1 + a_dim1;
00211     a -= a_offset;
00212     b_dim1 = *ldb;
00213     b_offset = 1 + b_dim1;
00214     b -= b_offset;
00215     --jpvt;
00216     --work;
00217 
00218     /* Function Body */
00219     mn = min(*m,*n);
00220     ismin = mn + 1;
00221     ismax = (mn << 1) + 1;
00222 
00223 /*     Test the input arguments. */
00224 
00225     *info = 0;
00226     lquery = *lwork == -1;
00227     if (*m < 0) {
00228         *info = -1;
00229     } else if (*n < 0) {
00230         *info = -2;
00231     } else if (*nrhs < 0) {
00232         *info = -3;
00233     } else if (*lda < max(1,*m)) {
00234         *info = -5;
00235     } else /* if(complicated condition) */ {
00236 /* Computing MAX */
00237         i__1 = max(1,*m);
00238         if (*ldb < max(i__1,*n)) {
00239             *info = -7;
00240         }
00241     }
00242 
00243 /*     Figure out optimal block size */
00244 
00245     if (*info == 0) {
00246         if (mn == 0 || *nrhs == 0) {
00247             lwkmin = 1;
00248             lwkopt = 1;
00249         } else {
00250             nb1 = ilaenv_(&c__1, "SGEQRF", " ", m, n, &c_n1, &c_n1);
00251             nb2 = ilaenv_(&c__1, "SGERQF", " ", m, n, &c_n1, &c_n1);
00252             nb3 = ilaenv_(&c__1, "SORMQR", " ", m, n, nrhs, &c_n1);
00253             nb4 = ilaenv_(&c__1, "SORMRQ", " ", m, n, nrhs, &c_n1);
00254 /* Computing MAX */
00255             i__1 = max(nb1,nb2), i__1 = max(i__1,nb3);
00256             nb = max(i__1,nb4);
00257 /* Computing MAX */
00258             i__1 = mn << 1, i__2 = *n + 1, i__1 = max(i__1,i__2), i__2 = mn + 
00259                     *nrhs;
00260             lwkmin = mn + max(i__1,i__2);
00261 /* Computing MAX */
00262             i__1 = lwkmin, i__2 = mn + (*n << 1) + nb * (*n + 1), i__1 = max(
00263                     i__1,i__2), i__2 = (mn << 1) + nb * *nrhs;
00264             lwkopt = max(i__1,i__2);
00265         }
00266         work[1] = (real) lwkopt;
00267 
00268         if (*lwork < lwkmin && ! lquery) {
00269             *info = -12;
00270         }
00271     }
00272 
00273     if (*info != 0) {
00274         i__1 = -(*info);
00275         xerbla_("SGELSY", &i__1);
00276         return 0;
00277     } else if (lquery) {
00278         return 0;
00279     }
00280 
00281 /*     Quick return if possible */
00282 
00283     if (mn == 0 || *nrhs == 0) {
00284         *rank = 0;
00285         return 0;
00286     }
00287 
00288 /*     Get machine parameters */
00289 
00290     smlnum = slamch_("S") / slamch_("P");
00291     bignum = 1.f / smlnum;
00292     slabad_(&smlnum, &bignum);
00293 
00294 /*     Scale A, B if max entries outside range [SMLNUM,BIGNUM] */
00295 
00296     anrm = slange_("M", m, n, &a[a_offset], lda, &work[1]);
00297     iascl = 0;
00298     if (anrm > 0.f && anrm < smlnum) {
00299 
00300 /*        Scale matrix norm up to SMLNUM */
00301 
00302         slascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda, 
00303                 info);
00304         iascl = 1;
00305     } else if (anrm > bignum) {
00306 
00307 /*        Scale matrix norm down to BIGNUM */
00308 
00309         slascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda, 
00310                 info);
00311         iascl = 2;
00312     } else if (anrm == 0.f) {
00313 
00314 /*        Matrix all zero. Return zero solution. */
00315 
00316         i__1 = max(*m,*n);
00317         slaset_("F", &i__1, nrhs, &c_b31, &c_b31, &b[b_offset], ldb);
00318         *rank = 0;
00319         goto L70;
00320     }
00321 
00322     bnrm = slange_("M", m, nrhs, &b[b_offset], ldb, &work[1]);
00323     ibscl = 0;
00324     if (bnrm > 0.f && bnrm < smlnum) {
00325 
00326 /*        Scale matrix norm up to SMLNUM */
00327 
00328         slascl_("G", &c__0, &c__0, &bnrm, &smlnum, m, nrhs, &b[b_offset], ldb, 
00329                  info);
00330         ibscl = 1;
00331     } else if (bnrm > bignum) {
00332 
00333 /*        Scale matrix norm down to BIGNUM */
00334 
00335         slascl_("G", &c__0, &c__0, &bnrm, &bignum, m, nrhs, &b[b_offset], ldb, 
00336                  info);
00337         ibscl = 2;
00338     }
00339 
00340 /*     Compute QR factorization with column pivoting of A: */
00341 /*        A * P = Q * R */
00342 
00343     i__1 = *lwork - mn;
00344     sgeqp3_(m, n, &a[a_offset], lda, &jpvt[1], &work[1], &work[mn + 1], &i__1, 
00345              info);
00346     wsize = mn + work[mn + 1];
00347 
00348 /*     workspace: MN+2*N+NB*(N+1). */
00349 /*     Details of Householder rotations stored in WORK(1:MN). */
00350 
00351 /*     Determine RANK using incremental condition estimation */
00352 
00353     work[ismin] = 1.f;
00354     work[ismax] = 1.f;
00355     smax = (r__1 = a[a_dim1 + 1], dabs(r__1));
00356     smin = smax;
00357     if ((r__1 = a[a_dim1 + 1], dabs(r__1)) == 0.f) {
00358         *rank = 0;
00359         i__1 = max(*m,*n);
00360         slaset_("F", &i__1, nrhs, &c_b31, &c_b31, &b[b_offset], ldb);
00361         goto L70;
00362     } else {
00363         *rank = 1;
00364     }
00365 
00366 L10:
00367     if (*rank < mn) {
00368         i__ = *rank + 1;
00369         slaic1_(&c__2, rank, &work[ismin], &smin, &a[i__ * a_dim1 + 1], &a[
00370                 i__ + i__ * a_dim1], &sminpr, &s1, &c1);
00371         slaic1_(&c__1, rank, &work[ismax], &smax, &a[i__ * a_dim1 + 1], &a[
00372                 i__ + i__ * a_dim1], &smaxpr, &s2, &c2);
00373 
00374         if (smaxpr * *rcond <= sminpr) {
00375             i__1 = *rank;
00376             for (i__ = 1; i__ <= i__1; ++i__) {
00377                 work[ismin + i__ - 1] = s1 * work[ismin + i__ - 1];
00378                 work[ismax + i__ - 1] = s2 * work[ismax + i__ - 1];
00379 /* L20: */
00380             }
00381             work[ismin + *rank] = c1;
00382             work[ismax + *rank] = c2;
00383             smin = sminpr;
00384             smax = smaxpr;
00385             ++(*rank);
00386             goto L10;
00387         }
00388     }
00389 
00390 /*     workspace: 3*MN. */
00391 
00392 /*     Logically partition R = [ R11 R12 ] */
00393 /*                             [  0  R22 ] */
00394 /*     where R11 = R(1:RANK,1:RANK) */
00395 
00396 /*     [R11,R12] = [ T11, 0 ] * Y */
00397 
00398     if (*rank < *n) {
00399         i__1 = *lwork - (mn << 1);
00400         stzrzf_(rank, n, &a[a_offset], lda, &work[mn + 1], &work[(mn << 1) + 
00401                 1], &i__1, info);
00402     }
00403 
00404 /*     workspace: 2*MN. */
00405 /*     Details of Householder rotations stored in WORK(MN+1:2*MN) */
00406 
00407 /*     B(1:M,1:NRHS) := Q' * B(1:M,1:NRHS) */
00408 
00409     i__1 = *lwork - (mn << 1);
00410     sormqr_("Left", "Transpose", m, nrhs, &mn, &a[a_offset], lda, &work[1], &
00411             b[b_offset], ldb, &work[(mn << 1) + 1], &i__1, info);
00412 /* Computing MAX */
00413     r__1 = wsize, r__2 = (mn << 1) + work[(mn << 1) + 1];
00414     wsize = dmax(r__1,r__2);
00415 
00416 /*     workspace: 2*MN+NB*NRHS. */
00417 
00418 /*     B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS) */
00419 
00420     strsm_("Left", "Upper", "No transpose", "Non-unit", rank, nrhs, &c_b54, &
00421             a[a_offset], lda, &b[b_offset], ldb);
00422 
00423     i__1 = *nrhs;
00424     for (j = 1; j <= i__1; ++j) {
00425         i__2 = *n;
00426         for (i__ = *rank + 1; i__ <= i__2; ++i__) {
00427             b[i__ + j * b_dim1] = 0.f;
00428 /* L30: */
00429         }
00430 /* L40: */
00431     }
00432 
00433 /*     B(1:N,1:NRHS) := Y' * B(1:N,1:NRHS) */
00434 
00435     if (*rank < *n) {
00436         i__1 = *n - *rank;
00437         i__2 = *lwork - (mn << 1);
00438         sormrz_("Left", "Transpose", n, nrhs, rank, &i__1, &a[a_offset], lda, 
00439                 &work[mn + 1], &b[b_offset], ldb, &work[(mn << 1) + 1], &i__2, 
00440                  info);
00441     }
00442 
00443 /*     workspace: 2*MN+NRHS. */
00444 
00445 /*     B(1:N,1:NRHS) := P * B(1:N,1:NRHS) */
00446 
00447     i__1 = *nrhs;
00448     for (j = 1; j <= i__1; ++j) {
00449         i__2 = *n;
00450         for (i__ = 1; i__ <= i__2; ++i__) {
00451             work[jpvt[i__]] = b[i__ + j * b_dim1];
00452 /* L50: */
00453         }
00454         scopy_(n, &work[1], &c__1, &b[j * b_dim1 + 1], &c__1);
00455 /* L60: */
00456     }
00457 
00458 /*     workspace: N. */
00459 
00460 /*     Undo scaling */
00461 
00462     if (iascl == 1) {
00463         slascl_("G", &c__0, &c__0, &anrm, &smlnum, n, nrhs, &b[b_offset], ldb, 
00464                  info);
00465         slascl_("U", &c__0, &c__0, &smlnum, &anrm, rank, rank, &a[a_offset], 
00466                 lda, info);
00467     } else if (iascl == 2) {
00468         slascl_("G", &c__0, &c__0, &anrm, &bignum, n, nrhs, &b[b_offset], ldb, 
00469                  info);
00470         slascl_("U", &c__0, &c__0, &bignum, &anrm, rank, rank, &a[a_offset], 
00471                 lda, info);
00472     }
00473     if (ibscl == 1) {
00474         slascl_("G", &c__0, &c__0, &smlnum, &bnrm, n, nrhs, &b[b_offset], ldb, 
00475                  info);
00476     } else if (ibscl == 2) {
00477         slascl_("G", &c__0, &c__0, &bignum, &bnrm, n, nrhs, &b[b_offset], ldb, 
00478                  info);
00479     }
00480 
00481 L70:
00482     work[1] = (real) lwkopt;
00483 
00484     return 0;
00485 
00486 /*     End of SGELSY */
00487 
00488 } /* sgelsy_ */


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