dbdsdc.c
Go to the documentation of this file.
00001 /* dbdsdc.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__9 = 9;
00019 static integer c__0 = 0;
00020 static doublereal c_b15 = 1.;
00021 static integer c__1 = 1;
00022 static doublereal c_b29 = 0.;
00023 
00024 /* Subroutine */ int dbdsdc_(char *uplo, char *compq, integer *n, doublereal *
00025         d__, doublereal *e, doublereal *u, integer *ldu, doublereal *vt, 
00026         integer *ldvt, doublereal *q, integer *iq, doublereal *work, integer *
00027         iwork, integer *info)
00028 {
00029     /* System generated locals */
00030     integer u_dim1, u_offset, vt_dim1, vt_offset, i__1, i__2;
00031     doublereal d__1;
00032 
00033     /* Builtin functions */
00034     double d_sign(doublereal *, doublereal *), log(doublereal);
00035 
00036     /* Local variables */
00037     integer i__, j, k;
00038     doublereal p, r__;
00039     integer z__, ic, ii, kk;
00040     doublereal cs;
00041     integer is, iu;
00042     doublereal sn;
00043     integer nm1;
00044     doublereal eps;
00045     integer ivt, difl, difr, ierr, perm, mlvl, sqre;
00046     extern logical lsame_(char *, char *);
00047     extern /* Subroutine */ int dlasr_(char *, char *, char *, integer *, 
00048             integer *, doublereal *, doublereal *, doublereal *, integer *), dcopy_(integer *, doublereal *, integer *
00049 , doublereal *, integer *), dswap_(integer *, doublereal *, 
00050             integer *, doublereal *, integer *);
00051     integer poles, iuplo, nsize, start;
00052     extern /* Subroutine */ int dlasd0_(integer *, integer *, doublereal *, 
00053             doublereal *, doublereal *, integer *, doublereal *, integer *, 
00054             integer *, integer *, doublereal *, integer *);
00055     extern doublereal dlamch_(char *);
00056     extern /* Subroutine */ int dlasda_(integer *, integer *, integer *, 
00057             integer *, doublereal *, doublereal *, doublereal *, integer *, 
00058             doublereal *, integer *, doublereal *, doublereal *, doublereal *, 
00059              doublereal *, integer *, integer *, integer *, integer *, 
00060             doublereal *, doublereal *, doublereal *, doublereal *, integer *, 
00061              integer *), dlascl_(char *, integer *, integer *, doublereal *, 
00062             doublereal *, integer *, integer *, doublereal *, integer *, 
00063             integer *), dlasdq_(char *, integer *, integer *, integer 
00064             *, integer *, integer *, doublereal *, doublereal *, doublereal *, 
00065              integer *, doublereal *, integer *, doublereal *, integer *, 
00066             doublereal *, integer *), dlaset_(char *, integer *, 
00067             integer *, doublereal *, doublereal *, doublereal *, integer *), dlartg_(doublereal *, doublereal *, doublereal *, 
00068             doublereal *, doublereal *);
00069     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
00070             integer *, integer *);
00071     extern /* Subroutine */ int xerbla_(char *, integer *);
00072     integer givcol;
00073     extern doublereal dlanst_(char *, integer *, doublereal *, doublereal *);
00074     integer icompq;
00075     doublereal orgnrm;
00076     integer givnum, givptr, qstart, smlsiz, wstart, smlszp;
00077 
00078 
00079 /*  -- LAPACK routine (version 3.2) -- */
00080 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00081 /*     November 2006 */
00082 
00083 /*     .. Scalar Arguments .. */
00084 /*     .. */
00085 /*     .. Array Arguments .. */
00086 /*     .. */
00087 
00088 /*  Purpose */
00089 /*  ======= */
00090 
00091 /*  DBDSDC computes the singular value decomposition (SVD) of a real */
00092 /*  N-by-N (upper or lower) bidiagonal matrix B:  B = U * S * VT, */
00093 /*  using a divide and conquer method, where S is a diagonal matrix */
00094 /*  with non-negative diagonal elements (the singular values of B), and */
00095 /*  U and VT are orthogonal matrices of left and right singular vectors, */
00096 /*  respectively. DBDSDC can be used to compute all singular values, */
00097 /*  and optionally, singular vectors or singular vectors in compact form. */
00098 
00099 /*  This code makes very mild assumptions about floating point */
00100 /*  arithmetic. It will work on machines with a guard digit in */
00101 /*  add/subtract, or on those binary machines without guard digits */
00102 /*  which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or Cray-2. */
00103 /*  It could conceivably fail on hexadecimal or decimal machines */
00104 /*  without guard digits, but we know of none.  See DLASD3 for details. */
00105 
00106 /*  The code currently calls DLASDQ if singular values only are desired. */
00107 /*  However, it can be slightly modified to compute singular values */
00108 /*  using the divide and conquer method. */
00109 
00110 /*  Arguments */
00111 /*  ========= */
00112 
00113 /*  UPLO    (input) CHARACTER*1 */
00114 /*          = 'U':  B is upper bidiagonal. */
00115 /*          = 'L':  B is lower bidiagonal. */
00116 
00117 /*  COMPQ   (input) CHARACTER*1 */
00118 /*          Specifies whether singular vectors are to be computed */
00119 /*          as follows: */
00120 /*          = 'N':  Compute singular values only; */
00121 /*          = 'P':  Compute singular values and compute singular */
00122 /*                  vectors in compact form; */
00123 /*          = 'I':  Compute singular values and singular vectors. */
00124 
00125 /*  N       (input) INTEGER */
00126 /*          The order of the matrix B.  N >= 0. */
00127 
00128 /*  D       (input/output) DOUBLE PRECISION array, dimension (N) */
00129 /*          On entry, the n diagonal elements of the bidiagonal matrix B. */
00130 /*          On exit, if INFO=0, the singular values of B. */
00131 
00132 /*  E       (input/output) DOUBLE PRECISION array, dimension (N-1) */
00133 /*          On entry, the elements of E contain the offdiagonal */
00134 /*          elements of the bidiagonal matrix whose SVD is desired. */
00135 /*          On exit, E has been destroyed. */
00136 
00137 /*  U       (output) DOUBLE PRECISION array, dimension (LDU,N) */
00138 /*          If  COMPQ = 'I', then: */
00139 /*             On exit, if INFO = 0, U contains the left singular vectors */
00140 /*             of the bidiagonal matrix. */
00141 /*          For other values of COMPQ, U is not referenced. */
00142 
00143 /*  LDU     (input) INTEGER */
00144 /*          The leading dimension of the array U.  LDU >= 1. */
00145 /*          If singular vectors are desired, then LDU >= max( 1, N ). */
00146 
00147 /*  VT      (output) DOUBLE PRECISION array, dimension (LDVT,N) */
00148 /*          If  COMPQ = 'I', then: */
00149 /*             On exit, if INFO = 0, VT' contains the right singular */
00150 /*             vectors of the bidiagonal matrix. */
00151 /*          For other values of COMPQ, VT is not referenced. */
00152 
00153 /*  LDVT    (input) INTEGER */
00154 /*          The leading dimension of the array VT.  LDVT >= 1. */
00155 /*          If singular vectors are desired, then LDVT >= max( 1, N ). */
00156 
00157 /*  Q       (output) DOUBLE PRECISION array, dimension (LDQ) */
00158 /*          If  COMPQ = 'P', then: */
00159 /*             On exit, if INFO = 0, Q and IQ contain the left */
00160 /*             and right singular vectors in a compact form, */
00161 /*             requiring O(N log N) space instead of 2*N**2. */
00162 /*             In particular, Q contains all the DOUBLE PRECISION data in */
00163 /*             LDQ >= N*(11 + 2*SMLSIZ + 8*INT(LOG_2(N/(SMLSIZ+1)))) */
00164 /*             words of memory, where SMLSIZ is returned by ILAENV and */
00165 /*             is equal to the maximum size of the subproblems at the */
00166 /*             bottom of the computation tree (usually about 25). */
00167 /*          For other values of COMPQ, Q is not referenced. */
00168 
00169 /*  IQ      (output) INTEGER array, dimension (LDIQ) */
00170 /*          If  COMPQ = 'P', then: */
00171 /*             On exit, if INFO = 0, Q and IQ contain the left */
00172 /*             and right singular vectors in a compact form, */
00173 /*             requiring O(N log N) space instead of 2*N**2. */
00174 /*             In particular, IQ contains all INTEGER data in */
00175 /*             LDIQ >= N*(3 + 3*INT(LOG_2(N/(SMLSIZ+1)))) */
00176 /*             words of memory, where SMLSIZ is returned by ILAENV and */
00177 /*             is equal to the maximum size of the subproblems at the */
00178 /*             bottom of the computation tree (usually about 25). */
00179 /*          For other values of COMPQ, IQ is not referenced. */
00180 
00181 /*  WORK    (workspace) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
00182 /*          If COMPQ = 'N' then LWORK >= (4 * N). */
00183 /*          If COMPQ = 'P' then LWORK >= (6 * N). */
00184 /*          If COMPQ = 'I' then LWORK >= (3 * N**2 + 4 * N). */
00185 
00186 /*  IWORK   (workspace) INTEGER array, dimension (8*N) */
00187 
00188 /*  INFO    (output) INTEGER */
00189 /*          = 0:  successful exit. */
00190 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
00191 /*          > 0:  The algorithm failed to compute an singular value. */
00192 /*                The update process of divide and conquer failed. */
00193 
00194 /*  Further Details */
00195 /*  =============== */
00196 
00197 /*  Based on contributions by */
00198 /*     Ming Gu and Huan Ren, Computer Science Division, University of */
00199 /*     California at Berkeley, USA */
00200 
00201 /*  ===================================================================== */
00202 /*  Changed dimension statement in comment describing E from (N) to */
00203 /*  (N-1).  Sven, 17 Feb 05. */
00204 /*  ===================================================================== */
00205 
00206 /*     .. Parameters .. */
00207 /*     .. */
00208 /*     .. Local Scalars .. */
00209 /*     .. */
00210 /*     .. External Functions .. */
00211 /*     .. */
00212 /*     .. External Subroutines .. */
00213 /*     .. */
00214 /*     .. Intrinsic Functions .. */
00215 /*     .. */
00216 /*     .. Executable Statements .. */
00217 
00218 /*     Test the input parameters. */
00219 
00220     /* Parameter adjustments */
00221     --d__;
00222     --e;
00223     u_dim1 = *ldu;
00224     u_offset = 1 + u_dim1;
00225     u -= u_offset;
00226     vt_dim1 = *ldvt;
00227     vt_offset = 1 + vt_dim1;
00228     vt -= vt_offset;
00229     --q;
00230     --iq;
00231     --work;
00232     --iwork;
00233 
00234     /* Function Body */
00235     *info = 0;
00236 
00237     iuplo = 0;
00238     if (lsame_(uplo, "U")) {
00239         iuplo = 1;
00240     }
00241     if (lsame_(uplo, "L")) {
00242         iuplo = 2;
00243     }
00244     if (lsame_(compq, "N")) {
00245         icompq = 0;
00246     } else if (lsame_(compq, "P")) {
00247         icompq = 1;
00248     } else if (lsame_(compq, "I")) {
00249         icompq = 2;
00250     } else {
00251         icompq = -1;
00252     }
00253     if (iuplo == 0) {
00254         *info = -1;
00255     } else if (icompq < 0) {
00256         *info = -2;
00257     } else if (*n < 0) {
00258         *info = -3;
00259     } else if (*ldu < 1 || icompq == 2 && *ldu < *n) {
00260         *info = -7;
00261     } else if (*ldvt < 1 || icompq == 2 && *ldvt < *n) {
00262         *info = -9;
00263     }
00264     if (*info != 0) {
00265         i__1 = -(*info);
00266         xerbla_("DBDSDC", &i__1);
00267         return 0;
00268     }
00269 
00270 /*     Quick return if possible */
00271 
00272     if (*n == 0) {
00273         return 0;
00274     }
00275     smlsiz = ilaenv_(&c__9, "DBDSDC", " ", &c__0, &c__0, &c__0, &c__0);
00276     if (*n == 1) {
00277         if (icompq == 1) {
00278             q[1] = d_sign(&c_b15, &d__[1]);
00279             q[smlsiz * *n + 1] = 1.;
00280         } else if (icompq == 2) {
00281             u[u_dim1 + 1] = d_sign(&c_b15, &d__[1]);
00282             vt[vt_dim1 + 1] = 1.;
00283         }
00284         d__[1] = abs(d__[1]);
00285         return 0;
00286     }
00287     nm1 = *n - 1;
00288 
00289 /*     If matrix lower bidiagonal, rotate to be upper bidiagonal */
00290 /*     by applying Givens rotations on the left */
00291 
00292     wstart = 1;
00293     qstart = 3;
00294     if (icompq == 1) {
00295         dcopy_(n, &d__[1], &c__1, &q[1], &c__1);
00296         i__1 = *n - 1;
00297         dcopy_(&i__1, &e[1], &c__1, &q[*n + 1], &c__1);
00298     }
00299     if (iuplo == 2) {
00300         qstart = 5;
00301         wstart = (*n << 1) - 1;
00302         i__1 = *n - 1;
00303         for (i__ = 1; i__ <= i__1; ++i__) {
00304             dlartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
00305             d__[i__] = r__;
00306             e[i__] = sn * d__[i__ + 1];
00307             d__[i__ + 1] = cs * d__[i__ + 1];
00308             if (icompq == 1) {
00309                 q[i__ + (*n << 1)] = cs;
00310                 q[i__ + *n * 3] = sn;
00311             } else if (icompq == 2) {
00312                 work[i__] = cs;
00313                 work[nm1 + i__] = -sn;
00314             }
00315 /* L10: */
00316         }
00317     }
00318 
00319 /*     If ICOMPQ = 0, use DLASDQ to compute the singular values. */
00320 
00321     if (icompq == 0) {
00322         dlasdq_("U", &c__0, n, &c__0, &c__0, &c__0, &d__[1], &e[1], &vt[
00323                 vt_offset], ldvt, &u[u_offset], ldu, &u[u_offset], ldu, &work[
00324                 wstart], info);
00325         goto L40;
00326     }
00327 
00328 /*     If N is smaller than the minimum divide size SMLSIZ, then solve */
00329 /*     the problem with another solver. */
00330 
00331     if (*n <= smlsiz) {
00332         if (icompq == 2) {
00333             dlaset_("A", n, n, &c_b29, &c_b15, &u[u_offset], ldu);
00334             dlaset_("A", n, n, &c_b29, &c_b15, &vt[vt_offset], ldvt);
00335             dlasdq_("U", &c__0, n, n, n, &c__0, &d__[1], &e[1], &vt[vt_offset]
00336 , ldvt, &u[u_offset], ldu, &u[u_offset], ldu, &work[
00337                     wstart], info);
00338         } else if (icompq == 1) {
00339             iu = 1;
00340             ivt = iu + *n;
00341             dlaset_("A", n, n, &c_b29, &c_b15, &q[iu + (qstart - 1) * *n], n);
00342             dlaset_("A", n, n, &c_b29, &c_b15, &q[ivt + (qstart - 1) * *n], n);
00343             dlasdq_("U", &c__0, n, n, n, &c__0, &d__[1], &e[1], &q[ivt + (
00344                     qstart - 1) * *n], n, &q[iu + (qstart - 1) * *n], n, &q[
00345                     iu + (qstart - 1) * *n], n, &work[wstart], info);
00346         }
00347         goto L40;
00348     }
00349 
00350     if (icompq == 2) {
00351         dlaset_("A", n, n, &c_b29, &c_b15, &u[u_offset], ldu);
00352         dlaset_("A", n, n, &c_b29, &c_b15, &vt[vt_offset], ldvt);
00353     }
00354 
00355 /*     Scale. */
00356 
00357     orgnrm = dlanst_("M", n, &d__[1], &e[1]);
00358     if (orgnrm == 0.) {
00359         return 0;
00360     }
00361     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b15, n, &c__1, &d__[1], n, &ierr);
00362     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b15, &nm1, &c__1, &e[1], &nm1, &
00363             ierr);
00364 
00365     eps = dlamch_("Epsilon");
00366 
00367     mlvl = (integer) (log((doublereal) (*n) / (doublereal) (smlsiz + 1)) / 
00368             log(2.)) + 1;
00369     smlszp = smlsiz + 1;
00370 
00371     if (icompq == 1) {
00372         iu = 1;
00373         ivt = smlsiz + 1;
00374         difl = ivt + smlszp;
00375         difr = difl + mlvl;
00376         z__ = difr + (mlvl << 1);
00377         ic = z__ + mlvl;
00378         is = ic + 1;
00379         poles = is + 1;
00380         givnum = poles + (mlvl << 1);
00381 
00382         k = 1;
00383         givptr = 2;
00384         perm = 3;
00385         givcol = perm + mlvl;
00386     }
00387 
00388     i__1 = *n;
00389     for (i__ = 1; i__ <= i__1; ++i__) {
00390         if ((d__1 = d__[i__], abs(d__1)) < eps) {
00391             d__[i__] = d_sign(&eps, &d__[i__]);
00392         }
00393 /* L20: */
00394     }
00395 
00396     start = 1;
00397     sqre = 0;
00398 
00399     i__1 = nm1;
00400     for (i__ = 1; i__ <= i__1; ++i__) {
00401         if ((d__1 = e[i__], abs(d__1)) < eps || i__ == nm1) {
00402 
00403 /*        Subproblem found. First determine its size and then */
00404 /*        apply divide and conquer on it. */
00405 
00406             if (i__ < nm1) {
00407 
00408 /*        A subproblem with E(I) small for I < NM1. */
00409 
00410                 nsize = i__ - start + 1;
00411             } else if ((d__1 = e[i__], abs(d__1)) >= eps) {
00412 
00413 /*        A subproblem with E(NM1) not too small but I = NM1. */
00414 
00415                 nsize = *n - start + 1;
00416             } else {
00417 
00418 /*        A subproblem with E(NM1) small. This implies an */
00419 /*        1-by-1 subproblem at D(N). Solve this 1-by-1 problem */
00420 /*        first. */
00421 
00422                 nsize = i__ - start + 1;
00423                 if (icompq == 2) {
00424                     u[*n + *n * u_dim1] = d_sign(&c_b15, &d__[*n]);
00425                     vt[*n + *n * vt_dim1] = 1.;
00426                 } else if (icompq == 1) {
00427                     q[*n + (qstart - 1) * *n] = d_sign(&c_b15, &d__[*n]);
00428                     q[*n + (smlsiz + qstart - 1) * *n] = 1.;
00429                 }
00430                 d__[*n] = (d__1 = d__[*n], abs(d__1));
00431             }
00432             if (icompq == 2) {
00433                 dlasd0_(&nsize, &sqre, &d__[start], &e[start], &u[start + 
00434                         start * u_dim1], ldu, &vt[start + start * vt_dim1], 
00435                         ldvt, &smlsiz, &iwork[1], &work[wstart], info);
00436             } else {
00437                 dlasda_(&icompq, &smlsiz, &nsize, &sqre, &d__[start], &e[
00438                         start], &q[start + (iu + qstart - 2) * *n], n, &q[
00439                         start + (ivt + qstart - 2) * *n], &iq[start + k * *n], 
00440                          &q[start + (difl + qstart - 2) * *n], &q[start + (
00441                         difr + qstart - 2) * *n], &q[start + (z__ + qstart - 
00442                         2) * *n], &q[start + (poles + qstart - 2) * *n], &iq[
00443                         start + givptr * *n], &iq[start + givcol * *n], n, &
00444                         iq[start + perm * *n], &q[start + (givnum + qstart - 
00445                         2) * *n], &q[start + (ic + qstart - 2) * *n], &q[
00446                         start + (is + qstart - 2) * *n], &work[wstart], &
00447                         iwork[1], info);
00448                 if (*info != 0) {
00449                     return 0;
00450                 }
00451             }
00452             start = i__ + 1;
00453         }
00454 /* L30: */
00455     }
00456 
00457 /*     Unscale */
00458 
00459     dlascl_("G", &c__0, &c__0, &c_b15, &orgnrm, n, &c__1, &d__[1], n, &ierr);
00460 L40:
00461 
00462 /*     Use Selection Sort to minimize swaps of singular vectors */
00463 
00464     i__1 = *n;
00465     for (ii = 2; ii <= i__1; ++ii) {
00466         i__ = ii - 1;
00467         kk = i__;
00468         p = d__[i__];
00469         i__2 = *n;
00470         for (j = ii; j <= i__2; ++j) {
00471             if (d__[j] > p) {
00472                 kk = j;
00473                 p = d__[j];
00474             }
00475 /* L50: */
00476         }
00477         if (kk != i__) {
00478             d__[kk] = d__[i__];
00479             d__[i__] = p;
00480             if (icompq == 1) {
00481                 iq[i__] = kk;
00482             } else if (icompq == 2) {
00483                 dswap_(n, &u[i__ * u_dim1 + 1], &c__1, &u[kk * u_dim1 + 1], &
00484                         c__1);
00485                 dswap_(n, &vt[i__ + vt_dim1], ldvt, &vt[kk + vt_dim1], ldvt);
00486             }
00487         } else if (icompq == 1) {
00488             iq[i__] = i__;
00489         }
00490 /* L60: */
00491     }
00492 
00493 /*     If ICOMPQ = 1, use IQ(N,1) as the indicator for UPLO */
00494 
00495     if (icompq == 1) {
00496         if (iuplo == 1) {
00497             iq[*n] = 1;
00498         } else {
00499             iq[*n] = 0;
00500         }
00501     }
00502 
00503 /*     If B is lower bidiagonal, update U by those Givens rotations */
00504 /*     which rotated B to be upper bidiagonal */
00505 
00506     if (iuplo == 2 && icompq == 2) {
00507         dlasr_("L", "V", "B", n, n, &work[1], &work[*n], &u[u_offset], ldu);
00508     }
00509 
00510     return 0;
00511 
00512 /*     End of DBDSDC */
00513 
00514 } /* dbdsdc_ */


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