dlalsd.c
Go to the documentation of this file.
00001 /* dlalsd.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 doublereal c_b6 = 0.;
00020 static integer c__0 = 0;
00021 static doublereal c_b11 = 1.;
00022 
00023 /* Subroutine */ int dlalsd_(char *uplo, integer *smlsiz, integer *n, integer 
00024         *nrhs, doublereal *d__, doublereal *e, doublereal *b, integer *ldb, 
00025         doublereal *rcond, integer *rank, doublereal *work, integer *iwork, 
00026         integer *info)
00027 {
00028     /* System generated locals */
00029     integer b_dim1, b_offset, i__1, i__2;
00030     doublereal d__1;
00031 
00032     /* Builtin functions */
00033     double log(doublereal), d_sign(doublereal *, doublereal *);
00034 
00035     /* Local variables */
00036     integer c__, i__, j, k;
00037     doublereal r__;
00038     integer s, u, z__;
00039     doublereal cs;
00040     integer bx;
00041     doublereal sn;
00042     integer st, vt, nm1, st1;
00043     doublereal eps;
00044     integer iwk;
00045     doublereal tol;
00046     integer difl, difr;
00047     doublereal rcnd;
00048     integer perm, nsub;
00049     extern /* Subroutine */ int drot_(integer *, doublereal *, integer *, 
00050             doublereal *, integer *, doublereal *, doublereal *);
00051     integer nlvl, sqre, bxst;
00052     extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *, 
00053             integer *, doublereal *, doublereal *, integer *, doublereal *, 
00054             integer *, doublereal *, doublereal *, integer *),
00055              dcopy_(integer *, doublereal *, integer *, doublereal *, integer 
00056             *);
00057     integer poles, sizei, nsize, nwork, icmpq1, icmpq2;
00058     extern doublereal dlamch_(char *);
00059     extern /* Subroutine */ int dlasda_(integer *, integer *, integer *, 
00060             integer *, doublereal *, doublereal *, doublereal *, integer *, 
00061             doublereal *, integer *, doublereal *, doublereal *, doublereal *, 
00062              doublereal *, integer *, integer *, integer *, integer *, 
00063             doublereal *, doublereal *, doublereal *, doublereal *, integer *, 
00064              integer *), dlalsa_(integer *, integer *, integer *, integer *, 
00065             doublereal *, integer *, doublereal *, integer *, doublereal *, 
00066             integer *, doublereal *, integer *, doublereal *, doublereal *, 
00067             doublereal *, doublereal *, integer *, integer *, integer *, 
00068             integer *, doublereal *, doublereal *, doublereal *, doublereal *, 
00069              integer *, integer *), dlascl_(char *, integer *, integer *, 
00070             doublereal *, doublereal *, integer *, integer *, doublereal *, 
00071             integer *, integer *);
00072     extern integer idamax_(integer *, doublereal *, integer *);
00073     extern /* Subroutine */ int dlasdq_(char *, integer *, integer *, integer 
00074             *, integer *, integer *, doublereal *, doublereal *, doublereal *, 
00075              integer *, doublereal *, integer *, doublereal *, integer *, 
00076             doublereal *, integer *), dlacpy_(char *, integer *, 
00077             integer *, doublereal *, integer *, doublereal *, integer *), dlartg_(doublereal *, doublereal *, doublereal *, 
00078             doublereal *, doublereal *), dlaset_(char *, integer *, integer *, 
00079              doublereal *, doublereal *, doublereal *, integer *), 
00080             xerbla_(char *, integer *);
00081     integer givcol;
00082     extern doublereal dlanst_(char *, integer *, doublereal *, doublereal *);
00083     extern /* Subroutine */ int dlasrt_(char *, integer *, doublereal *, 
00084             integer *);
00085     doublereal orgnrm;
00086     integer givnum, givptr, smlszp;
00087 
00088 
00089 /*  -- LAPACK routine (version 3.2) -- */
00090 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00091 /*     November 2006 */
00092 
00093 /*     .. Scalar Arguments .. */
00094 /*     .. */
00095 /*     .. Array Arguments .. */
00096 /*     .. */
00097 
00098 /*  Purpose */
00099 /*  ======= */
00100 
00101 /*  DLALSD uses the singular value decomposition of A to solve the least */
00102 /*  squares problem of finding X to minimize the Euclidean norm of each */
00103 /*  column of A*X-B, where A is N-by-N upper bidiagonal, and X and B */
00104 /*  are N-by-NRHS. The solution X overwrites B. */
00105 
00106 /*  The singular values of A smaller than RCOND times the largest */
00107 /*  singular value are treated as zero in solving the least squares */
00108 /*  problem; in this case a minimum norm solution is returned. */
00109 /*  The actual singular values are returned in D in ascending order. */
00110 
00111 /*  This code makes very mild assumptions about floating point */
00112 /*  arithmetic. It will work on machines with a guard digit in */
00113 /*  add/subtract, or on those binary machines without guard digits */
00114 /*  which subtract like the Cray XMP, Cray YMP, Cray C 90, or Cray 2. */
00115 /*  It could conceivably fail on hexadecimal or decimal machines */
00116 /*  without guard digits, but we know of none. */
00117 
00118 /*  Arguments */
00119 /*  ========= */
00120 
00121 /*  UPLO   (input) CHARACTER*1 */
00122 /*         = 'U': D and E define an upper bidiagonal matrix. */
00123 /*         = 'L': D and E define a  lower bidiagonal matrix. */
00124 
00125 /*  SMLSIZ (input) INTEGER */
00126 /*         The maximum size of the subproblems at the bottom of the */
00127 /*         computation tree. */
00128 
00129 /*  N      (input) INTEGER */
00130 /*         The dimension of the  bidiagonal matrix.  N >= 0. */
00131 
00132 /*  NRHS   (input) INTEGER */
00133 /*         The number of columns of B. NRHS must be at least 1. */
00134 
00135 /*  D      (input/output) DOUBLE PRECISION array, dimension (N) */
00136 /*         On entry D contains the main diagonal of the bidiagonal */
00137 /*         matrix. On exit, if INFO = 0, D contains its singular values. */
00138 
00139 /*  E      (input/output) DOUBLE PRECISION array, dimension (N-1) */
00140 /*         Contains the super-diagonal entries of the bidiagonal matrix. */
00141 /*         On exit, E has been destroyed. */
00142 
00143 /*  B      (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS) */
00144 /*         On input, B contains the right hand sides of the least */
00145 /*         squares problem. On output, B contains the solution X. */
00146 
00147 /*  LDB    (input) INTEGER */
00148 /*         The leading dimension of B in the calling subprogram. */
00149 /*         LDB must be at least max(1,N). */
00150 
00151 /*  RCOND  (input) DOUBLE PRECISION */
00152 /*         The singular values of A less than or equal to RCOND times */
00153 /*         the largest singular value are treated as zero in solving */
00154 /*         the least squares problem. If RCOND is negative, */
00155 /*         machine precision is used instead. */
00156 /*         For example, if diag(S)*X=B were the least squares problem, */
00157 /*         where diag(S) is a diagonal matrix of singular values, the */
00158 /*         solution would be X(i) = B(i) / S(i) if S(i) is greater than */
00159 /*         RCOND*max(S), and X(i) = 0 if S(i) is less than or equal to */
00160 /*         RCOND*max(S). */
00161 
00162 /*  RANK   (output) INTEGER */
00163 /*         The number of singular values of A greater than RCOND times */
00164 /*         the largest singular value. */
00165 
00166 /*  WORK   (workspace) DOUBLE PRECISION array, dimension at least */
00167 /*         (9*N + 2*N*SMLSIZ + 8*N*NLVL + N*NRHS + (SMLSIZ+1)**2), */
00168 /*         where NLVL = max(0, INT(log_2 (N/(SMLSIZ+1))) + 1). */
00169 
00170 /*  IWORK  (workspace) INTEGER array, dimension at least */
00171 /*         (3*N*NLVL + 11*N) */
00172 
00173 /*  INFO   (output) INTEGER */
00174 /*         = 0:  successful exit. */
00175 /*         < 0:  if INFO = -i, the i-th argument had an illegal value. */
00176 /*         > 0:  The algorithm failed to compute an singular value while */
00177 /*               working on the submatrix lying in rows and columns */
00178 /*               INFO/(N+1) through MOD(INFO,N+1). */
00179 
00180 /*  Further Details */
00181 /*  =============== */
00182 
00183 /*  Based on contributions by */
00184 /*     Ming Gu and Ren-Cang Li, Computer Science Division, University of */
00185 /*       California at Berkeley, USA */
00186 /*     Osni Marques, LBNL/NERSC, USA */
00187 
00188 /*  ===================================================================== */
00189 
00190 /*     .. Parameters .. */
00191 /*     .. */
00192 /*     .. Local Scalars .. */
00193 /*     .. */
00194 /*     .. External Functions .. */
00195 /*     .. */
00196 /*     .. External Subroutines .. */
00197 /*     .. */
00198 /*     .. Intrinsic Functions .. */
00199 /*     .. */
00200 /*     .. Executable Statements .. */
00201 
00202 /*     Test the input parameters. */
00203 
00204     /* Parameter adjustments */
00205     --d__;
00206     --e;
00207     b_dim1 = *ldb;
00208     b_offset = 1 + b_dim1;
00209     b -= b_offset;
00210     --work;
00211     --iwork;
00212 
00213     /* Function Body */
00214     *info = 0;
00215 
00216     if (*n < 0) {
00217         *info = -3;
00218     } else if (*nrhs < 1) {
00219         *info = -4;
00220     } else if (*ldb < 1 || *ldb < *n) {
00221         *info = -8;
00222     }
00223     if (*info != 0) {
00224         i__1 = -(*info);
00225         xerbla_("DLALSD", &i__1);
00226         return 0;
00227     }
00228 
00229     eps = dlamch_("Epsilon");
00230 
00231 /*     Set up the tolerance. */
00232 
00233     if (*rcond <= 0. || *rcond >= 1.) {
00234         rcnd = eps;
00235     } else {
00236         rcnd = *rcond;
00237     }
00238 
00239     *rank = 0;
00240 
00241 /*     Quick return if possible. */
00242 
00243     if (*n == 0) {
00244         return 0;
00245     } else if (*n == 1) {
00246         if (d__[1] == 0.) {
00247             dlaset_("A", &c__1, nrhs, &c_b6, &c_b6, &b[b_offset], ldb);
00248         } else {
00249             *rank = 1;
00250             dlascl_("G", &c__0, &c__0, &d__[1], &c_b11, &c__1, nrhs, &b[
00251                     b_offset], ldb, info);
00252             d__[1] = abs(d__[1]);
00253         }
00254         return 0;
00255     }
00256 
00257 /*     Rotate the matrix if it is lower bidiagonal. */
00258 
00259     if (*(unsigned char *)uplo == 'L') {
00260         i__1 = *n - 1;
00261         for (i__ = 1; i__ <= i__1; ++i__) {
00262             dlartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
00263             d__[i__] = r__;
00264             e[i__] = sn * d__[i__ + 1];
00265             d__[i__ + 1] = cs * d__[i__ + 1];
00266             if (*nrhs == 1) {
00267                 drot_(&c__1, &b[i__ + b_dim1], &c__1, &b[i__ + 1 + b_dim1], &
00268                         c__1, &cs, &sn);
00269             } else {
00270                 work[(i__ << 1) - 1] = cs;
00271                 work[i__ * 2] = sn;
00272             }
00273 /* L10: */
00274         }
00275         if (*nrhs > 1) {
00276             i__1 = *nrhs;
00277             for (i__ = 1; i__ <= i__1; ++i__) {
00278                 i__2 = *n - 1;
00279                 for (j = 1; j <= i__2; ++j) {
00280                     cs = work[(j << 1) - 1];
00281                     sn = work[j * 2];
00282                     drot_(&c__1, &b[j + i__ * b_dim1], &c__1, &b[j + 1 + i__ *
00283                              b_dim1], &c__1, &cs, &sn);
00284 /* L20: */
00285                 }
00286 /* L30: */
00287             }
00288         }
00289     }
00290 
00291 /*     Scale. */
00292 
00293     nm1 = *n - 1;
00294     orgnrm = dlanst_("M", n, &d__[1], &e[1]);
00295     if (orgnrm == 0.) {
00296         dlaset_("A", n, nrhs, &c_b6, &c_b6, &b[b_offset], ldb);
00297         return 0;
00298     }
00299 
00300     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, n, &c__1, &d__[1], n, info);
00301     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, &nm1, &c__1, &e[1], &nm1, 
00302             info);
00303 
00304 /*     If N is smaller than the minimum divide size SMLSIZ, then solve */
00305 /*     the problem with another solver. */
00306 
00307     if (*n <= *smlsiz) {
00308         nwork = *n * *n + 1;
00309         dlaset_("A", n, n, &c_b6, &c_b11, &work[1], n);
00310         dlasdq_("U", &c__0, n, n, &c__0, nrhs, &d__[1], &e[1], &work[1], n, &
00311                 work[1], n, &b[b_offset], ldb, &work[nwork], info);
00312         if (*info != 0) {
00313             return 0;
00314         }
00315         tol = rcnd * (d__1 = d__[idamax_(n, &d__[1], &c__1)], abs(d__1));
00316         i__1 = *n;
00317         for (i__ = 1; i__ <= i__1; ++i__) {
00318             if (d__[i__] <= tol) {
00319                 dlaset_("A", &c__1, nrhs, &c_b6, &c_b6, &b[i__ + b_dim1], ldb);
00320             } else {
00321                 dlascl_("G", &c__0, &c__0, &d__[i__], &c_b11, &c__1, nrhs, &b[
00322                         i__ + b_dim1], ldb, info);
00323                 ++(*rank);
00324             }
00325 /* L40: */
00326         }
00327         dgemm_("T", "N", n, nrhs, n, &c_b11, &work[1], n, &b[b_offset], ldb, &
00328                 c_b6, &work[nwork], n);
00329         dlacpy_("A", n, nrhs, &work[nwork], n, &b[b_offset], ldb);
00330 
00331 /*        Unscale. */
00332 
00333         dlascl_("G", &c__0, &c__0, &c_b11, &orgnrm, n, &c__1, &d__[1], n, 
00334                 info);
00335         dlasrt_("D", n, &d__[1], info);
00336         dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, n, nrhs, &b[b_offset], 
00337                 ldb, info);
00338 
00339         return 0;
00340     }
00341 
00342 /*     Book-keeping and setting up some constants. */
00343 
00344     nlvl = (integer) (log((doublereal) (*n) / (doublereal) (*smlsiz + 1)) / 
00345             log(2.)) + 1;
00346 
00347     smlszp = *smlsiz + 1;
00348 
00349     u = 1;
00350     vt = *smlsiz * *n + 1;
00351     difl = vt + smlszp * *n;
00352     difr = difl + nlvl * *n;
00353     z__ = difr + (nlvl * *n << 1);
00354     c__ = z__ + nlvl * *n;
00355     s = c__ + *n;
00356     poles = s + *n;
00357     givnum = poles + (nlvl << 1) * *n;
00358     bx = givnum + (nlvl << 1) * *n;
00359     nwork = bx + *n * *nrhs;
00360 
00361     sizei = *n + 1;
00362     k = sizei + *n;
00363     givptr = k + *n;
00364     perm = givptr + *n;
00365     givcol = perm + nlvl * *n;
00366     iwk = givcol + (nlvl * *n << 1);
00367 
00368     st = 1;
00369     sqre = 0;
00370     icmpq1 = 1;
00371     icmpq2 = 0;
00372     nsub = 0;
00373 
00374     i__1 = *n;
00375     for (i__ = 1; i__ <= i__1; ++i__) {
00376         if ((d__1 = d__[i__], abs(d__1)) < eps) {
00377             d__[i__] = d_sign(&eps, &d__[i__]);
00378         }
00379 /* L50: */
00380     }
00381 
00382     i__1 = nm1;
00383     for (i__ = 1; i__ <= i__1; ++i__) {
00384         if ((d__1 = e[i__], abs(d__1)) < eps || i__ == nm1) {
00385             ++nsub;
00386             iwork[nsub] = st;
00387 
00388 /*           Subproblem found. First determine its size and then */
00389 /*           apply divide and conquer on it. */
00390 
00391             if (i__ < nm1) {
00392 
00393 /*              A subproblem with E(I) small for I < NM1. */
00394 
00395                 nsize = i__ - st + 1;
00396                 iwork[sizei + nsub - 1] = nsize;
00397             } else if ((d__1 = e[i__], abs(d__1)) >= eps) {
00398 
00399 /*              A subproblem with E(NM1) not too small but I = NM1. */
00400 
00401                 nsize = *n - st + 1;
00402                 iwork[sizei + nsub - 1] = nsize;
00403             } else {
00404 
00405 /*              A subproblem with E(NM1) small. This implies an */
00406 /*              1-by-1 subproblem at D(N), which is not solved */
00407 /*              explicitly. */
00408 
00409                 nsize = i__ - st + 1;
00410                 iwork[sizei + nsub - 1] = nsize;
00411                 ++nsub;
00412                 iwork[nsub] = *n;
00413                 iwork[sizei + nsub - 1] = 1;
00414                 dcopy_(nrhs, &b[*n + b_dim1], ldb, &work[bx + nm1], n);
00415             }
00416             st1 = st - 1;
00417             if (nsize == 1) {
00418 
00419 /*              This is a 1-by-1 subproblem and is not solved */
00420 /*              explicitly. */
00421 
00422                 dcopy_(nrhs, &b[st + b_dim1], ldb, &work[bx + st1], n);
00423             } else if (nsize <= *smlsiz) {
00424 
00425 /*              This is a small subproblem and is solved by DLASDQ. */
00426 
00427                 dlaset_("A", &nsize, &nsize, &c_b6, &c_b11, &work[vt + st1], 
00428                         n);
00429                 dlasdq_("U", &c__0, &nsize, &nsize, &c__0, nrhs, &d__[st], &e[
00430                         st], &work[vt + st1], n, &work[nwork], n, &b[st + 
00431                         b_dim1], ldb, &work[nwork], info);
00432                 if (*info != 0) {
00433                     return 0;
00434                 }
00435                 dlacpy_("A", &nsize, nrhs, &b[st + b_dim1], ldb, &work[bx + 
00436                         st1], n);
00437             } else {
00438 
00439 /*              A large problem. Solve it using divide and conquer. */
00440 
00441                 dlasda_(&icmpq1, smlsiz, &nsize, &sqre, &d__[st], &e[st], &
00442                         work[u + st1], n, &work[vt + st1], &iwork[k + st1], &
00443                         work[difl + st1], &work[difr + st1], &work[z__ + st1], 
00444                          &work[poles + st1], &iwork[givptr + st1], &iwork[
00445                         givcol + st1], n, &iwork[perm + st1], &work[givnum + 
00446                         st1], &work[c__ + st1], &work[s + st1], &work[nwork], 
00447                         &iwork[iwk], info);
00448                 if (*info != 0) {
00449                     return 0;
00450                 }
00451                 bxst = bx + st1;
00452                 dlalsa_(&icmpq2, smlsiz, &nsize, nrhs, &b[st + b_dim1], ldb, &
00453                         work[bxst], n, &work[u + st1], n, &work[vt + st1], &
00454                         iwork[k + st1], &work[difl + st1], &work[difr + st1], 
00455                         &work[z__ + st1], &work[poles + st1], &iwork[givptr + 
00456                         st1], &iwork[givcol + st1], n, &iwork[perm + st1], &
00457                         work[givnum + st1], &work[c__ + st1], &work[s + st1], 
00458                         &work[nwork], &iwork[iwk], info);
00459                 if (*info != 0) {
00460                     return 0;
00461                 }
00462             }
00463             st = i__ + 1;
00464         }
00465 /* L60: */
00466     }
00467 
00468 /*     Apply the singular values and treat the tiny ones as zero. */
00469 
00470     tol = rcnd * (d__1 = d__[idamax_(n, &d__[1], &c__1)], abs(d__1));
00471 
00472     i__1 = *n;
00473     for (i__ = 1; i__ <= i__1; ++i__) {
00474 
00475 /*        Some of the elements in D can be negative because 1-by-1 */
00476 /*        subproblems were not solved explicitly. */
00477 
00478         if ((d__1 = d__[i__], abs(d__1)) <= tol) {
00479             dlaset_("A", &c__1, nrhs, &c_b6, &c_b6, &work[bx + i__ - 1], n);
00480         } else {
00481             ++(*rank);
00482             dlascl_("G", &c__0, &c__0, &d__[i__], &c_b11, &c__1, nrhs, &work[
00483                     bx + i__ - 1], n, info);
00484         }
00485         d__[i__] = (d__1 = d__[i__], abs(d__1));
00486 /* L70: */
00487     }
00488 
00489 /*     Now apply back the right singular vectors. */
00490 
00491     icmpq2 = 1;
00492     i__1 = nsub;
00493     for (i__ = 1; i__ <= i__1; ++i__) {
00494         st = iwork[i__];
00495         st1 = st - 1;
00496         nsize = iwork[sizei + i__ - 1];
00497         bxst = bx + st1;
00498         if (nsize == 1) {
00499             dcopy_(nrhs, &work[bxst], n, &b[st + b_dim1], ldb);
00500         } else if (nsize <= *smlsiz) {
00501             dgemm_("T", "N", &nsize, nrhs, &nsize, &c_b11, &work[vt + st1], n, 
00502                      &work[bxst], n, &c_b6, &b[st + b_dim1], ldb);
00503         } else {
00504             dlalsa_(&icmpq2, smlsiz, &nsize, nrhs, &work[bxst], n, &b[st + 
00505                     b_dim1], ldb, &work[u + st1], n, &work[vt + st1], &iwork[
00506                     k + st1], &work[difl + st1], &work[difr + st1], &work[z__ 
00507                     + st1], &work[poles + st1], &iwork[givptr + st1], &iwork[
00508                     givcol + st1], n, &iwork[perm + st1], &work[givnum + st1], 
00509                      &work[c__ + st1], &work[s + st1], &work[nwork], &iwork[
00510                     iwk], info);
00511             if (*info != 0) {
00512                 return 0;
00513             }
00514         }
00515 /* L80: */
00516     }
00517 
00518 /*     Unscale and sort the singular values. */
00519 
00520     dlascl_("G", &c__0, &c__0, &c_b11, &orgnrm, n, &c__1, &d__[1], n, info);
00521     dlasrt_("D", n, &d__[1], info);
00522     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, n, nrhs, &b[b_offset], ldb, 
00523             info);
00524 
00525     return 0;
00526 
00527 /*     End of DLALSD */
00528 
00529 } /* dlalsd_ */


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