dgejsv.c
Go to the documentation of this file.
00001 /* dgejsv.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_b34 = 0.;
00020 static doublereal c_b35 = 1.;
00021 static integer c__0 = 0;
00022 static integer c_n1 = -1;
00023 
00024 /* Subroutine */ int dgejsv_(char *joba, char *jobu, char *jobv, char *jobr, 
00025         char *jobt, char *jobp, integer *m, integer *n, doublereal *a, 
00026         integer *lda, doublereal *sva, doublereal *u, integer *ldu, 
00027         doublereal *v, integer *ldv, doublereal *work, integer *lwork, 
00028         integer *iwork, integer *info)
00029 {
00030     /* System generated locals */
00031     integer a_dim1, a_offset, u_dim1, u_offset, v_dim1, v_offset, i__1, i__2, 
00032             i__3, i__4, i__5, i__6, i__7, i__8, i__9, i__10;
00033     doublereal d__1, d__2, d__3, d__4;
00034 
00035     /* Builtin functions */
00036     double sqrt(doublereal), log(doublereal), d_sign(doublereal *, doublereal 
00037             *);
00038     integer i_dnnt(doublereal *);
00039 
00040     /* Local variables */
00041     integer p, q, n1, nr;
00042     doublereal big, xsc, big1;
00043     logical defr;
00044     doublereal aapp, aaqq;
00045     logical kill;
00046     integer ierr;
00047     extern doublereal dnrm2_(integer *, doublereal *, integer *);
00048     doublereal temp1;
00049     logical jracc;
00050     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
00051             integer *);
00052     extern logical lsame_(char *, char *);
00053     doublereal small, entra, sfmin;
00054     logical lsvec;
00055     extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
00056             doublereal *, integer *), dswap_(integer *, doublereal *, integer 
00057             *, doublereal *, integer *);
00058     doublereal epsln;
00059     logical rsvec;
00060     extern /* Subroutine */ int dtrsm_(char *, char *, char *, char *, 
00061             integer *, integer *, doublereal *, doublereal *, integer *, 
00062             doublereal *, integer *);
00063     logical l2aber;
00064     extern /* Subroutine */ int dgeqp3_(integer *, integer *, doublereal *, 
00065             integer *, integer *, doublereal *, doublereal *, integer *, 
00066             integer *);
00067     doublereal condr1, condr2, uscal1, uscal2;
00068     logical l2kill, l2rank, l2tran, l2pert;
00069     extern doublereal dlamch_(char *);
00070     extern /* Subroutine */ int dgelqf_(integer *, integer *, doublereal *, 
00071             integer *, doublereal *, doublereal *, integer *, integer *);
00072     extern integer idamax_(integer *, doublereal *, integer *);
00073     doublereal scalem;
00074     extern /* Subroutine */ int dlascl_(char *, integer *, integer *, 
00075             doublereal *, doublereal *, integer *, integer *, doublereal *, 
00076             integer *, integer *);
00077     doublereal sconda;
00078     logical goscal;
00079     doublereal aatmin;
00080     extern /* Subroutine */ int dgeqrf_(integer *, integer *, doublereal *, 
00081             integer *, doublereal *, doublereal *, integer *, integer *);
00082     doublereal aatmax;
00083     extern /* Subroutine */ int dlacpy_(char *, integer *, integer *, 
00084             doublereal *, integer *, doublereal *, integer *), 
00085             dlaset_(char *, integer *, integer *, doublereal *, doublereal *, 
00086             doublereal *, integer *), xerbla_(char *, integer *);
00087     logical noscal;
00088     extern /* Subroutine */ int dpocon_(char *, integer *, doublereal *, 
00089             integer *, doublereal *, doublereal *, doublereal *, integer *, 
00090             integer *), dgesvj_(char *, char *, char *, integer *, 
00091             integer *, doublereal *, integer *, doublereal *, integer *, 
00092             doublereal *, integer *, doublereal *, integer *, integer *), dlassq_(integer *, doublereal *, integer 
00093             *, doublereal *, doublereal *), dlaswp_(integer *, doublereal *, 
00094             integer *, integer *, integer *, integer *, integer *);
00095     doublereal entrat;
00096     logical almort;
00097     extern /* Subroutine */ int dorgqr_(integer *, integer *, integer *, 
00098             doublereal *, integer *, doublereal *, doublereal *, integer *, 
00099             integer *), dormlq_(char *, char *, integer *, integer *, integer 
00100             *, doublereal *, integer *, doublereal *, doublereal *, integer *, 
00101              doublereal *, integer *, integer *);
00102     doublereal maxprj;
00103     logical errest;
00104     extern /* Subroutine */ int dormqr_(char *, char *, integer *, integer *, 
00105             integer *, doublereal *, integer *, doublereal *, doublereal *, 
00106             integer *, doublereal *, integer *, integer *);
00107     logical transp, rowpiv;
00108     doublereal cond_ok__;
00109     integer warning, numrank;
00110 
00111 
00112 /*  -- LAPACK routine (version 3.2)                                    -- */
00113 
00114 /*  -- Contributed by Zlatko Drmac of the University of Zagreb and     -- */
00115 /*  -- Kresimir Veselic of the Fernuniversitaet Hagen                  -- */
00116 /*  -- November 2008                                                   -- */
00117 
00118 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
00119 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
00120 
00121 /* This routine is also part of SIGMA (version 1.23, October 23. 2008.) */
00122 /* SIGMA is a library of algorithms for highly accurate algorithms for */
00123 /* computation of SVD, PSVD, QSVD, (H,K)-SVD, and for solution of the */
00124 /* eigenvalue problems Hx = lambda M x, H M x = lambda x with H, M > 0. */
00125 
00126 /*     -#- Scalar Arguments -#- */
00127 
00128 
00129 /*     -#- Array Arguments -#- */
00130 
00131 /*     .. */
00132 
00133 /*  Purpose */
00134 /*  ~~~~~~~ */
00135 /*  DGEJSV computes the singular value decomposition (SVD) of a real M-by-N */
00136 /*  matrix [A], where M >= N. The SVD of [A] is written as */
00137 
00138 /*               [A] = [U] * [SIGMA] * [V]^t, */
00139 
00140 /*  where [SIGMA] is an N-by-N (M-by-N) matrix which is zero except for its N */
00141 /*  diagonal elements, [U] is an M-by-N (or M-by-M) orthonormal matrix, and */
00142 /*  [V] is an N-by-N orthogonal matrix. The diagonal elements of [SIGMA] are */
00143 /*  the singular values of [A]. The columns of [U] and [V] are the left and */
00144 /*  the right singular vectors of [A], respectively. The matrices [U] and [V] */
00145 /*  are computed and stored in the arrays U and V, respectively. The diagonal */
00146 /*  of [SIGMA] is computed and stored in the array SVA. */
00147 
00148 /*  Further details */
00149 /*  ~~~~~~~~~~~~~~~ */
00150 /*  DGEJSV implements a preconditioned Jacobi SVD algorithm. It uses SGEQP3, */
00151 /*  SGEQRF, and SGELQF as preprocessors and preconditioners. Optionally, an */
00152 /*  additional row pivoting can be used as a preprocessor, which in some */
00153 /*  cases results in much higher accuracy. An example is matrix A with the */
00154 /*  structure A = D1 * C * D2, where D1, D2 are arbitrarily ill-conditioned */
00155 /*  diagonal matrices and C is well-conditioned matrix. In that case, complete */
00156 /*  pivoting in the first QR factorizations provides accuracy dependent on the */
00157 /*  condition number of C, and independent of D1, D2. Such higher accuracy is */
00158 /*  not completely understood theoretically, but it works well in practice. */
00159 /*  Further, if A can be written as A = B*D, with well-conditioned B and some */
00160 /*  diagonal D, then the high accuracy is guaranteed, both theoretically and */
00161 /*  in software, independent of D. For more details see [1], [2]. */
00162 /*     The computational range for the singular values can be the full range */
00163 /*  ( UNDERFLOW,OVERFLOW ), provided that the machine arithmetic and the BLAS */
00164 /*  & LAPACK routines called by DGEJSV are implemented to work in that range. */
00165 /*  If that is not the case, then the restriction for safe computation with */
00166 /*  the singular values in the range of normalized IEEE numbers is that the */
00167 /*  spectral condition number kappa(A)=sigma_max(A)/sigma_min(A) does not */
00168 /*  overflow. This code (DGEJSV) is best used in this restricted range, */
00169 /*  meaning that singular values of magnitude below ||A||_2 / SLAMCH('O') are */
00170 /*  returned as zeros. See JOBR for details on this. */
00171 /*     Further, this implementation is somewhat slower than the one described */
00172 /*  in [1,2] due to replacement of some non-LAPACK components, and because */
00173 /*  the choice of some tuning parameters in the iterative part (DGESVJ) is */
00174 /*  left to the implementer on a particular machine. */
00175 /*     The rank revealing QR factorization (in this code: SGEQP3) should be */
00176 /*  implemented as in [3]. We have a new version of SGEQP3 under development */
00177 /*  that is more robust than the current one in LAPACK, with a cleaner cut in */
00178 /*  rank defficient cases. It will be available in the SIGMA library [4]. */
00179 /*  If M is much larger than N, it is obvious that the inital QRF with */
00180 /*  column pivoting can be preprocessed by the QRF without pivoting. That */
00181 /*  well known trick is not used in DGEJSV because in some cases heavy row */
00182 /*  weighting can be treated with complete pivoting. The overhead in cases */
00183 /*  M much larger than N is then only due to pivoting, but the benefits in */
00184 /*  terms of accuracy have prevailed. The implementer/user can incorporate */
00185 /*  this extra QRF step easily. The implementer can also improve data movement */
00186 /*  (matrix transpose, matrix copy, matrix transposed copy) - this */
00187 /*  implementation of DGEJSV uses only the simplest, naive data movement. */
00188 
00189 /*  Contributors */
00190 /*  ~~~~~~~~~~~~ */
00191 /*  Zlatko Drmac (Zagreb, Croatia) and Kresimir Veselic (Hagen, Germany) */
00192 
00193 /*  References */
00194 /*  ~~~~~~~~~~ */
00195 /* [1] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I. */
00196 /*     SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342. */
00197 /*     LAPACK Working note 169. */
00198 /* [2] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II. */
00199 /*     SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362. */
00200 /*     LAPACK Working note 170. */
00201 /* [3] Z. Drmac and Z. Bujanovic: On the failure of rank-revealing QR */
00202 /*     factorization software - a case study. */
00203 /*     ACM Trans. Math. Softw. Vol. 35, No 2 (2008), pp. 1-28. */
00204 /*     LAPACK Working note 176. */
00205 /* [4] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV, */
00206 /*     QSVD, (H,K)-SVD computations. */
00207 /*     Department of Mathematics, University of Zagreb, 2008. */
00208 
00209 /*  Bugs, examples and comments */
00210 /*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
00211 /*  Please report all bugs and send interesting examples and/or comments to */
00212 /*  drmac@math.hr. Thank you. */
00213 
00214 /*  Arguments */
00215 /*  ~~~~~~~~~ */
00216 /* ............................................................................ */
00217 /* . JOBA   (input) CHARACTER*1 */
00218 /* .        Specifies the level of accuracy: */
00219 /* .      = 'C': This option works well (high relative accuracy) if A = B * D, */
00220 /* .             with well-conditioned B and arbitrary diagonal matrix D. */
00221 /* .             The accuracy cannot be spoiled by COLUMN scaling. The */
00222 /* .             accuracy of the computed output depends on the condition of */
00223 /* .             B, and the procedure aims at the best theoretical accuracy. */
00224 /* .             The relative error max_{i=1:N}|d sigma_i| / sigma_i is */
00225 /* .             bounded by f(M,N)*epsilon* cond(B), independent of D. */
00226 /* .             The input matrix is preprocessed with the QRF with column */
00227 /* .             pivoting. This initial preprocessing and preconditioning by */
00228 /* .             a rank revealing QR factorization is common for all values of */
00229 /* .             JOBA. Additional actions are specified as follows: */
00230 /* .      = 'E': Computation as with 'C' with an additional estimate of the */
00231 /* .             condition number of B. It provides a realistic error bound. */
00232 /* .      = 'F': If A = D1 * C * D2 with ill-conditioned diagonal scalings */
00233 /* .             D1, D2, and well-conditioned matrix C, this option gives */
00234 /* .             higher accuracy than the 'C' option. If the structure of the */
00235 /* .             input matrix is not known, and relative accuracy is */
00236 /* .             desirable, then this option is advisable. The input matrix A */
00237 /* .             is preprocessed with QR factorization with FULL (row and */
00238 /* .             column) pivoting. */
00239 /* .      = 'G'  Computation as with 'F' with an additional estimate of the */
00240 /* .             condition number of B, where A=D*B. If A has heavily weighted */
00241 /* .             rows, then using this condition number gives too pessimistic */
00242 /* .             error bound. */
00243 /* .      = 'A': Small singular values are the noise and the matrix is treated */
00244 /* .             as numerically rank defficient. The error in the computed */
00245 /* .             singular values is bounded by f(m,n)*epsilon*||A||. */
00246 /* .             The computed SVD A = U * S * V^t restores A up to */
00247 /* .             f(m,n)*epsilon*||A||. */
00248 /* .             This gives the procedure the licence to discard (set to zero) */
00249 /* .             all singular values below N*epsilon*||A||. */
00250 /* .      = 'R': Similar as in 'A'. Rank revealing property of the initial */
00251 /* .             QR factorization is used do reveal (using triangular factor) */
00252 /* .             a gap sigma_{r+1} < epsilon * sigma_r in which case the */
00253 /* .             numerical RANK is declared to be r. The SVD is computed with */
00254 /* .             absolute error bounds, but more accurately than with 'A'. */
00255 /* . */
00256 /* . JOBU   (input) CHARACTER*1 */
00257 /* .        Specifies whether to compute the columns of U: */
00258 /* .      = 'U': N columns of U are returned in the array U. */
00259 /* .      = 'F': full set of M left sing. vectors is returned in the array U. */
00260 /* .      = 'W': U may be used as workspace of length M*N. See the description */
00261 /* .             of U. */
00262 /* .      = 'N': U is not computed. */
00263 /* . */
00264 /* . JOBV   (input) CHARACTER*1 */
00265 /* .        Specifies whether to compute the matrix V: */
00266 /* .      = 'V': N columns of V are returned in the array V; Jacobi rotations */
00267 /* .             are not explicitly accumulated. */
00268 /* .      = 'J': N columns of V are returned in the array V, but they are */
00269 /* .             computed as the product of Jacobi rotations. This option is */
00270 /* .             allowed only if JOBU .NE. 'N', i.e. in computing the full SVD. */
00271 /* .      = 'W': V may be used as workspace of length N*N. See the description */
00272 /* .             of V. */
00273 /* .      = 'N': V is not computed. */
00274 /* . */
00275 /* . JOBR   (input) CHARACTER*1 */
00276 /* .        Specifies the RANGE for the singular values. Issues the licence to */
00277 /* .        set to zero small positive singular values if they are outside */
00278 /* .        specified range. If A .NE. 0 is scaled so that the largest singular */
00279 /* .        value of c*A is around DSQRT(BIG), BIG=SLAMCH('O'), then JOBR issues */
00280 /* .        the licence to kill columns of A whose norm in c*A is less than */
00281 /* .        DSQRT(SFMIN) (for JOBR.EQ.'R'), or less than SMALL=SFMIN/EPSLN, */
00282 /* .        where SFMIN=SLAMCH('S'), EPSLN=SLAMCH('E'). */
00283 /* .      = 'N': Do not kill small columns of c*A. This option assumes that */
00284 /* .             BLAS and QR factorizations and triangular solvers are */
00285 /* .             implemented to work in that range. If the condition of A */
00286 /* .             is greater than BIG, use DGESVJ. */
00287 /* .      = 'R': RESTRICTED range for sigma(c*A) is [DSQRT(SFMIN), DSQRT(BIG)] */
00288 /* .             (roughly, as described above). This option is recommended. */
00289 /* .                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
00290 /* .        For computing the singular values in the FULL range [SFMIN,BIG] */
00291 /* .        use DGESVJ. */
00292 /* . */
00293 /* . JOBT   (input) CHARACTER*1 */
00294 /* .        If the matrix is square then the procedure may determine to use */
00295 /* .        transposed A if A^t seems to be better with respect to convergence. */
00296 /* .        If the matrix is not square, JOBT is ignored. This is subject to */
00297 /* .        changes in the future. */
00298 /* .        The decision is based on two values of entropy over the adjoint */
00299 /* .        orbit of A^t * A. See the descriptions of WORK(6) and WORK(7). */
00300 /* .      = 'T': transpose if entropy test indicates possibly faster */
00301 /* .        convergence of Jacobi process if A^t is taken as input. If A is */
00302 /* .        replaced with A^t, then the row pivoting is included automatically. */
00303 /* .      = 'N': do not speculate. */
00304 /* .        This option can be used to compute only the singular values, or the */
00305 /* .        full SVD (U, SIGMA and V). For only one set of singular vectors */
00306 /* .        (U or V), the caller should provide both U and V, as one of the */
00307 /* .        matrices is used as workspace if the matrix A is transposed. */
00308 /* .        The implementer can easily remove this constraint and make the */
00309 /* .        code more complicated. See the descriptions of U and V. */
00310 /* . */
00311 /* . JOBP   (input) CHARACTER*1 */
00312 /* .        Issues the licence to introduce structured perturbations to drown */
00313 /* .        denormalized numbers. This licence should be active if the */
00314 /* .        denormals are poorly implemented, causing slow computation, */
00315 /* .        especially in cases of fast convergence (!). For details see [1,2]. */
00316 /* .        For the sake of simplicity, this perturbations are included only */
00317 /* .        when the full SVD or only the singular values are requested. The */
00318 /* .        implementer/user can easily add the perturbation for the cases of */
00319 /* .        computing one set of singular vectors. */
00320 /* .      = 'P': introduce perturbation */
00321 /* .      = 'N': do not perturb */
00322 /* ............................................................................ */
00323 
00324 /*  M      (input) INTEGER */
00325 /*         The number of rows of the input matrix A.  M >= 0. */
00326 
00327 /*  N      (input) INTEGER */
00328 /*         The number of columns of the input matrix A. M >= N >= 0. */
00329 
00330 /*  A       (input/workspace) REAL array, dimension (LDA,N) */
00331 /*          On entry, the M-by-N matrix A. */
00332 
00333 /*  LDA     (input) INTEGER */
00334 /*          The leading dimension of the array A.  LDA >= max(1,M). */
00335 
00336 /*  SVA     (workspace/output) REAL array, dimension (N) */
00337 /*          On exit, */
00338 /*          - For WORK(1)/WORK(2) = ONE: The singular values of A. During the */
00339 /*            computation SVA contains Euclidean column norms of the */
00340 /*            iterated matrices in the array A. */
00341 /*          - For WORK(1) .NE. WORK(2): The singular values of A are */
00342 /*            (WORK(1)/WORK(2)) * SVA(1:N). This factored form is used if */
00343 /*            sigma_max(A) overflows or if small singular values have been */
00344 /*            saved from underflow by scaling the input matrix A. */
00345 /*          - If JOBR='R' then some of the singular values may be returned */
00346 /*            as exact zeros obtained by "set to zero" because they are */
00347 /*            below the numerical rank threshold or are denormalized numbers. */
00348 
00349 /*  U       (workspace/output) REAL array, dimension ( LDU, N ) */
00350 /*          If JOBU = 'U', then U contains on exit the M-by-N matrix of */
00351 /*                         the left singular vectors. */
00352 /*          If JOBU = 'F', then U contains on exit the M-by-M matrix of */
00353 /*                         the left singular vectors, including an ONB */
00354 /*                         of the orthogonal complement of the Range(A). */
00355 /*          If JOBU = 'W'  .AND. (JOBV.EQ.'V' .AND. JOBT.EQ.'T' .AND. M.EQ.N), */
00356 /*                         then U is used as workspace if the procedure */
00357 /*                         replaces A with A^t. In that case, [V] is computed */
00358 /*                         in U as left singular vectors of A^t and then */
00359 /*                         copied back to the V array. This 'W' option is just */
00360 /*                         a reminder to the caller that in this case U is */
00361 /*                         reserved as workspace of length N*N. */
00362 /*          If JOBU = 'N'  U is not referenced. */
00363 
00364 /* LDU      (input) INTEGER */
00365 /*          The leading dimension of the array U,  LDU >= 1. */
00366 /*          IF  JOBU = 'U' or 'F' or 'W',  then LDU >= M. */
00367 
00368 /*  V       (workspace/output) REAL array, dimension ( LDV, N ) */
00369 /*          If JOBV = 'V', 'J' then V contains on exit the N-by-N matrix of */
00370 /*                         the right singular vectors; */
00371 /*          If JOBV = 'W', AND (JOBU.EQ.'U' AND JOBT.EQ.'T' AND M.EQ.N), */
00372 /*                         then V is used as workspace if the pprocedure */
00373 /*                         replaces A with A^t. In that case, [U] is computed */
00374 /*                         in V as right singular vectors of A^t and then */
00375 /*                         copied back to the U array. This 'W' option is just */
00376 /*                         a reminder to the caller that in this case V is */
00377 /*                         reserved as workspace of length N*N. */
00378 /*          If JOBV = 'N'  V is not referenced. */
00379 
00380 /*  LDV     (input) INTEGER */
00381 /*          The leading dimension of the array V,  LDV >= 1. */
00382 /*          If JOBV = 'V' or 'J' or 'W', then LDV >= N. */
00383 
00384 /*  WORK    (workspace/output) REAL array, dimension at least LWORK. */
00385 /*          On exit, */
00386 /*          WORK(1) = SCALE = WORK(2) / WORK(1) is the scaling factor such */
00387 /*                    that SCALE*SVA(1:N) are the computed singular values */
00388 /*                    of A. (See the description of SVA().) */
00389 /*          WORK(2) = See the description of WORK(1). */
00390 /*          WORK(3) = SCONDA is an estimate for the condition number of */
00391 /*                    column equilibrated A. (If JOBA .EQ. 'E' or 'G') */
00392 /*                    SCONDA is an estimate of DSQRT(||(R^t * R)^(-1)||_1). */
00393 /*                    It is computed using DPOCON. It holds */
00394 /*                    N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
00395 /*                    where R is the triangular factor from the QRF of A. */
00396 /*                    However, if R is truncated and the numerical rank is */
00397 /*                    determined to be strictly smaller than N, SCONDA is */
00398 /*                    returned as -1, thus indicating that the smallest */
00399 /*                    singular values might be lost. */
00400 
00401 /*          If full SVD is needed, the following two condition numbers are */
00402 /*          useful for the analysis of the algorithm. They are provied for */
00403 /*          a developer/implementer who is familiar with the details of */
00404 /*          the method. */
00405 
00406 /*          WORK(4) = an estimate of the scaled condition number of the */
00407 /*                    triangular factor in the first QR factorization. */
00408 /*          WORK(5) = an estimate of the scaled condition number of the */
00409 /*                    triangular factor in the second QR factorization. */
00410 /*          The following two parameters are computed if JOBT .EQ. 'T'. */
00411 /*          They are provided for a developer/implementer who is familiar */
00412 /*          with the details of the method. */
00413 
00414 /*          WORK(6) = the entropy of A^t*A :: this is the Shannon entropy */
00415 /*                    of diag(A^t*A) / Trace(A^t*A) taken as point in the */
00416 /*                    probability simplex. */
00417 /*          WORK(7) = the entropy of A*A^t. */
00418 
00419 /*  LWORK   (input) INTEGER */
00420 /*          Length of WORK to confirm proper allocation of work space. */
00421 /*          LWORK depends on the job: */
00422 
00423 /*          If only SIGMA is needed ( JOBU.EQ.'N', JOBV.EQ.'N' ) and */
00424 /*            -> .. no scaled condition estimate required ( JOBE.EQ.'N'): */
00425 /*               LWORK >= max(2*M+N,4*N+1,7). This is the minimal requirement. */
00426 /*               For optimal performance (blocked code) the optimal value */
00427 /*               is LWORK >= max(2*M+N,3*N+(N+1)*NB,7). Here NB is the optimal */
00428 /*               block size for xGEQP3/xGEQRF. */
00429 /*            -> .. an estimate of the scaled condition number of A is */
00430 /*               required (JOBA='E', 'G'). In this case, LWORK is the maximum */
00431 /*               of the above and N*N+4*N, i.e. LWORK >= max(2*M+N,N*N+4N,7). */
00432 
00433 /*          If SIGMA and the right singular vectors are needed (JOBV.EQ.'V'), */
00434 /*            -> the minimal requirement is LWORK >= max(2*N+M,7). */
00435 /*            -> For optimal performance, LWORK >= max(2*N+M,2*N+N*NB,7), */
00436 /*               where NB is the optimal block size. */
00437 
00438 /*          If SIGMA and the left singular vectors are needed */
00439 /*            -> the minimal requirement is LWORK >= max(2*N+M,7). */
00440 /*            -> For optimal performance, LWORK >= max(2*N+M,2*N+N*NB,7), */
00441 /*               where NB is the optimal block size. */
00442 
00443 /*          If full SVD is needed ( JOBU.EQ.'U' or 'F', JOBV.EQ.'V' ) and */
00444 /*            -> .. the singular vectors are computed without explicit */
00445 /*               accumulation of the Jacobi rotations, LWORK >= 6*N+2*N*N */
00446 /*            -> .. in the iterative part, the Jacobi rotations are */
00447 /*               explicitly accumulated (option, see the description of JOBV), */
00448 /*               then the minimal requirement is LWORK >= max(M+3*N+N*N,7). */
00449 /*               For better performance, if NB is the optimal block size, */
00450 /*               LWORK >= max(3*N+N*N+M,3*N+N*N+N*NB,7). */
00451 
00452 /*  IWORK   (workspace/output) INTEGER array, dimension M+3*N. */
00453 /*          On exit, */
00454 /*          IWORK(1) = the numerical rank determined after the initial */
00455 /*                     QR factorization with pivoting. See the descriptions */
00456 /*                     of JOBA and JOBR. */
00457 /*          IWORK(2) = the number of the computed nonzero singular values */
00458 /*          IWORK(3) = if nonzero, a warning message: */
00459 /*                     If IWORK(3).EQ.1 then some of the column norms of A */
00460 /*                     were denormalized floats. The requested high accuracy */
00461 /*                     is not warranted by the data. */
00462 
00463 /*  INFO    (output) INTEGER */
00464 /*           < 0  : if INFO = -i, then the i-th argument had an illegal value. */
00465 /*           = 0 :  successfull exit; */
00466 /*           > 0 :  DGEJSV  did not converge in the maximal allowed number */
00467 /*                  of sweeps. The computed values may be inaccurate. */
00468 
00469 /* ............................................................................ */
00470 
00471 /*     Local Parameters: */
00472 
00473 
00474 /*     Local Scalars: */
00475 
00476 
00477 /*     Intrinsic Functions: */
00478 
00479 
00480 /*     External Functions: */
00481 
00482 
00483 /*     External Subroutines ( BLAS, LAPACK ): */
00484 
00485 
00486 
00487 /* ............................................................................ */
00488 
00489 /*     Test the input arguments */
00490 
00491     /* Parameter adjustments */
00492     --sva;
00493     a_dim1 = *lda;
00494     a_offset = 1 + a_dim1;
00495     a -= a_offset;
00496     u_dim1 = *ldu;
00497     u_offset = 1 + u_dim1;
00498     u -= u_offset;
00499     v_dim1 = *ldv;
00500     v_offset = 1 + v_dim1;
00501     v -= v_offset;
00502     --work;
00503     --iwork;
00504 
00505     /* Function Body */
00506     lsvec = lsame_(jobu, "U") || lsame_(jobu, "F");
00507     jracc = lsame_(jobv, "J");
00508     rsvec = lsame_(jobv, "V") || jracc;
00509     rowpiv = lsame_(joba, "F") || lsame_(joba, "G");
00510     l2rank = lsame_(joba, "R");
00511     l2aber = lsame_(joba, "A");
00512     errest = lsame_(joba, "E") || lsame_(joba, "G");
00513     l2tran = lsame_(jobt, "T");
00514     l2kill = lsame_(jobr, "R");
00515     defr = lsame_(jobr, "N");
00516     l2pert = lsame_(jobp, "P");
00517 
00518     if (! (rowpiv || l2rank || l2aber || errest || lsame_(joba, "C"))) {
00519         *info = -1;
00520     } else if (! (lsvec || lsame_(jobu, "N") || lsame_(
00521             jobu, "W"))) {
00522         *info = -2;
00523     } else if (! (rsvec || lsame_(jobv, "N") || lsame_(
00524             jobv, "W")) || jracc && ! lsvec) {
00525         *info = -3;
00526     } else if (! (l2kill || defr)) {
00527         *info = -4;
00528     } else if (! (l2tran || lsame_(jobt, "N"))) {
00529         *info = -5;
00530     } else if (! (l2pert || lsame_(jobp, "N"))) {
00531         *info = -6;
00532     } else if (*m < 0) {
00533         *info = -7;
00534     } else if (*n < 0 || *n > *m) {
00535         *info = -8;
00536     } else if (*lda < *m) {
00537         *info = -10;
00538     } else if (lsvec && *ldu < *m) {
00539         *info = -13;
00540     } else if (rsvec && *ldv < *n) {
00541         *info = -14;
00542     } else /* if(complicated condition) */ {
00543 /* Computing MAX */
00544         i__1 = 7, i__2 = (*n << 2) + 1, i__1 = max(i__1,i__2), i__2 = (*m << 
00545                 1) + *n;
00546 /* Computing MAX */
00547         i__3 = 7, i__4 = (*n << 2) + *n * *n, i__3 = max(i__3,i__4), i__4 = (*
00548                 m << 1) + *n;
00549 /* Computing MAX */
00550         i__5 = 7, i__6 = (*n << 1) + *m;
00551 /* Computing MAX */
00552         i__7 = 7, i__8 = (*n << 1) + *m;
00553 /* Computing MAX */
00554         i__9 = 7, i__10 = *m + *n * 3 + *n * *n;
00555         if (! (lsvec || rsvec || errest) && *lwork < max(i__1,i__2) || ! (
00556                 lsvec || lsvec) && errest && *lwork < max(i__3,i__4) || lsvec 
00557                 && ! rsvec && *lwork < max(i__5,i__6) || rsvec && ! lsvec && *
00558                 lwork < max(i__7,i__8) || lsvec && rsvec && ! jracc && *lwork 
00559                 < *n * 6 + (*n << 1) * *n || lsvec && rsvec && jracc && *
00560                 lwork < max(i__9,i__10)) {
00561             *info = -17;
00562         } else {
00563 /*        #:) */
00564             *info = 0;
00565         }
00566     }
00567 
00568     if (*info != 0) {
00569 /*       #:( */
00570         i__1 = -(*info);
00571         xerbla_("DGEJSV", &i__1);
00572     }
00573 
00574 /*     Quick return for void matrix (Y3K safe) */
00575 /* #:) */
00576     if (*m == 0 || *n == 0) {
00577         return 0;
00578     }
00579 
00580 /*     Determine whether the matrix U should be M x N or M x M */
00581 
00582     if (lsvec) {
00583         n1 = *n;
00584         if (lsame_(jobu, "F")) {
00585             n1 = *m;
00586         }
00587     }
00588 
00589 /*     Set numerical parameters */
00590 
00591 /* !    NOTE: Make sure DLAMCH() does not fail on the target architecture. */
00592 
00593     epsln = dlamch_("Epsilon");
00594     sfmin = dlamch_("SafeMinimum");
00595     small = sfmin / epsln;
00596     big = dlamch_("O");
00597 /*     BIG   = ONE / SFMIN */
00598 
00599 /*     Initialize SVA(1:N) = diag( ||A e_i||_2 )_1^N */
00600 
00601 /* (!)  If necessary, scale SVA() to protect the largest norm from */
00602 /*     overflow. It is possible that this scaling pushes the smallest */
00603 /*     column norm left from the underflow threshold (extreme case). */
00604 
00605     scalem = 1. / sqrt((doublereal) (*m) * (doublereal) (*n));
00606     noscal = TRUE_;
00607     goscal = TRUE_;
00608     i__1 = *n;
00609     for (p = 1; p <= i__1; ++p) {
00610         aapp = 0.;
00611         aaqq = 0.;
00612         dlassq_(m, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
00613         if (aapp > big) {
00614             *info = -9;
00615             i__2 = -(*info);
00616             xerbla_("DGEJSV", &i__2);
00617             return 0;
00618         }
00619         aaqq = sqrt(aaqq);
00620         if (aapp < big / aaqq && noscal) {
00621             sva[p] = aapp * aaqq;
00622         } else {
00623             noscal = FALSE_;
00624             sva[p] = aapp * (aaqq * scalem);
00625             if (goscal) {
00626                 goscal = FALSE_;
00627                 i__2 = p - 1;
00628                 dscal_(&i__2, &scalem, &sva[1], &c__1);
00629             }
00630         }
00631 /* L1874: */
00632     }
00633 
00634     if (noscal) {
00635         scalem = 1.;
00636     }
00637 
00638     aapp = 0.;
00639     aaqq = big;
00640     i__1 = *n;
00641     for (p = 1; p <= i__1; ++p) {
00642 /* Computing MAX */
00643         d__1 = aapp, d__2 = sva[p];
00644         aapp = max(d__1,d__2);
00645         if (sva[p] != 0.) {
00646 /* Computing MIN */
00647             d__1 = aaqq, d__2 = sva[p];
00648             aaqq = min(d__1,d__2);
00649         }
00650 /* L4781: */
00651     }
00652 
00653 /*     Quick return for zero M x N matrix */
00654 /* #:) */
00655     if (aapp == 0.) {
00656         if (lsvec) {
00657             dlaset_("G", m, &n1, &c_b34, &c_b35, &u[u_offset], ldu)
00658                     ;
00659         }
00660         if (rsvec) {
00661             dlaset_("G", n, n, &c_b34, &c_b35, &v[v_offset], ldv);
00662         }
00663         work[1] = 1.;
00664         work[2] = 1.;
00665         if (errest) {
00666             work[3] = 1.;
00667         }
00668         if (lsvec && rsvec) {
00669             work[4] = 1.;
00670             work[5] = 1.;
00671         }
00672         if (l2tran) {
00673             work[6] = 0.;
00674             work[7] = 0.;
00675         }
00676         iwork[1] = 0;
00677         iwork[2] = 0;
00678         return 0;
00679     }
00680 
00681 /*     Issue warning if denormalized column norms detected. Override the */
00682 /*     high relative accuracy request. Issue licence to kill columns */
00683 /*     (set them to zero) whose norm is less than sigma_max / BIG (roughly). */
00684 /* #:( */
00685     warning = 0;
00686     if (aaqq <= sfmin) {
00687         l2rank = TRUE_;
00688         l2kill = TRUE_;
00689         warning = 1;
00690     }
00691 
00692 /*     Quick return for one-column matrix */
00693 /* #:) */
00694     if (*n == 1) {
00695 
00696         if (lsvec) {
00697             dlascl_("G", &c__0, &c__0, &sva[1], &scalem, m, &c__1, &a[a_dim1 
00698                     + 1], lda, &ierr);
00699             dlacpy_("A", m, &c__1, &a[a_offset], lda, &u[u_offset], ldu);
00700 /*           computing all M left singular vectors of the M x 1 matrix */
00701             if (n1 != *n) {
00702                 i__1 = *lwork - *n;
00703                 dgeqrf_(m, n, &u[u_offset], ldu, &work[1], &work[*n + 1], &
00704                         i__1, &ierr);
00705                 i__1 = *lwork - *n;
00706                 dorgqr_(m, &n1, &c__1, &u[u_offset], ldu, &work[1], &work[*n 
00707                         + 1], &i__1, &ierr);
00708                 dcopy_(m, &a[a_dim1 + 1], &c__1, &u[u_dim1 + 1], &c__1);
00709             }
00710         }
00711         if (rsvec) {
00712             v[v_dim1 + 1] = 1.;
00713         }
00714         if (sva[1] < big * scalem) {
00715             sva[1] /= scalem;
00716             scalem = 1.;
00717         }
00718         work[1] = 1. / scalem;
00719         work[2] = 1.;
00720         if (sva[1] != 0.) {
00721             iwork[1] = 1;
00722             if (sva[1] / scalem >= sfmin) {
00723                 iwork[2] = 1;
00724             } else {
00725                 iwork[2] = 0;
00726             }
00727         } else {
00728             iwork[1] = 0;
00729             iwork[2] = 0;
00730         }
00731         if (errest) {
00732             work[3] = 1.;
00733         }
00734         if (lsvec && rsvec) {
00735             work[4] = 1.;
00736             work[5] = 1.;
00737         }
00738         if (l2tran) {
00739             work[6] = 0.;
00740             work[7] = 0.;
00741         }
00742         return 0;
00743 
00744     }
00745 
00746     transp = FALSE_;
00747     l2tran = l2tran && *m == *n;
00748 
00749     aatmax = -1.;
00750     aatmin = big;
00751     if (rowpiv || l2tran) {
00752 
00753 /*     Compute the row norms, needed to determine row pivoting sequence */
00754 /*     (in the case of heavily row weighted A, row pivoting is strongly */
00755 /*     advised) and to collect information needed to compare the */
00756 /*     structures of A * A^t and A^t * A (in the case L2TRAN.EQ..TRUE.). */
00757 
00758         if (l2tran) {
00759             i__1 = *m;
00760             for (p = 1; p <= i__1; ++p) {
00761                 xsc = 0.;
00762                 temp1 = 0.;
00763                 dlassq_(n, &a[p + a_dim1], lda, &xsc, &temp1);
00764 /*              DLASSQ gets both the ell_2 and the ell_infinity norm */
00765 /*              in one pass through the vector */
00766                 work[*m + *n + p] = xsc * scalem;
00767                 work[*n + p] = xsc * (scalem * sqrt(temp1));
00768 /* Computing MAX */
00769                 d__1 = aatmax, d__2 = work[*n + p];
00770                 aatmax = max(d__1,d__2);
00771                 if (work[*n + p] != 0.) {
00772 /* Computing MIN */
00773                     d__1 = aatmin, d__2 = work[*n + p];
00774                     aatmin = min(d__1,d__2);
00775                 }
00776 /* L1950: */
00777             }
00778         } else {
00779             i__1 = *m;
00780             for (p = 1; p <= i__1; ++p) {
00781                 work[*m + *n + p] = scalem * (d__1 = a[p + idamax_(n, &a[p + 
00782                         a_dim1], lda) * a_dim1], abs(d__1));
00783 /* Computing MAX */
00784                 d__1 = aatmax, d__2 = work[*m + *n + p];
00785                 aatmax = max(d__1,d__2);
00786 /* Computing MIN */
00787                 d__1 = aatmin, d__2 = work[*m + *n + p];
00788                 aatmin = min(d__1,d__2);
00789 /* L1904: */
00790             }
00791         }
00792 
00793     }
00794 
00795 /*     For square matrix A try to determine whether A^t  would be  better */
00796 /*     input for the preconditioned Jacobi SVD, with faster convergence. */
00797 /*     The decision is based on an O(N) function of the vector of column */
00798 /*     and row norms of A, based on the Shannon entropy. This should give */
00799 /*     the right choice in most cases when the difference actually matters. */
00800 /*     It may fail and pick the slower converging side. */
00801 
00802     entra = 0.;
00803     entrat = 0.;
00804     if (l2tran) {
00805 
00806         xsc = 0.;
00807         temp1 = 0.;
00808         dlassq_(n, &sva[1], &c__1, &xsc, &temp1);
00809         temp1 = 1. / temp1;
00810 
00811         entra = 0.;
00812         i__1 = *n;
00813         for (p = 1; p <= i__1; ++p) {
00814 /* Computing 2nd power */
00815             d__1 = sva[p] / xsc;
00816             big1 = d__1 * d__1 * temp1;
00817             if (big1 != 0.) {
00818                 entra += big1 * log(big1);
00819             }
00820 /* L1113: */
00821         }
00822         entra = -entra / log((doublereal) (*n));
00823 
00824 /*        Now, SVA().^2/Trace(A^t * A) is a point in the probability simplex. */
00825 /*        It is derived from the diagonal of  A^t * A.  Do the same with the */
00826 /*        diagonal of A * A^t, compute the entropy of the corresponding */
00827 /*        probability distribution. Note that A * A^t and A^t * A have the */
00828 /*        same trace. */
00829 
00830         entrat = 0.;
00831         i__1 = *n + *m;
00832         for (p = *n + 1; p <= i__1; ++p) {
00833 /* Computing 2nd power */
00834             d__1 = work[p] / xsc;
00835             big1 = d__1 * d__1 * temp1;
00836             if (big1 != 0.) {
00837                 entrat += big1 * log(big1);
00838             }
00839 /* L1114: */
00840         }
00841         entrat = -entrat / log((doublereal) (*m));
00842 
00843 /*        Analyze the entropies and decide A or A^t. Smaller entropy */
00844 /*        usually means better input for the algorithm. */
00845 
00846         transp = entrat < entra;
00847 
00848 /*        If A^t is better than A, transpose A. */
00849 
00850         if (transp) {
00851 /*           In an optimal implementation, this trivial transpose */
00852 /*           should be replaced with faster transpose. */
00853             i__1 = *n - 1;
00854             for (p = 1; p <= i__1; ++p) {
00855                 i__2 = *n;
00856                 for (q = p + 1; q <= i__2; ++q) {
00857                     temp1 = a[q + p * a_dim1];
00858                     a[q + p * a_dim1] = a[p + q * a_dim1];
00859                     a[p + q * a_dim1] = temp1;
00860 /* L1116: */
00861                 }
00862 /* L1115: */
00863             }
00864             i__1 = *n;
00865             for (p = 1; p <= i__1; ++p) {
00866                 work[*m + *n + p] = sva[p];
00867                 sva[p] = work[*n + p];
00868 /* L1117: */
00869             }
00870             temp1 = aapp;
00871             aapp = aatmax;
00872             aatmax = temp1;
00873             temp1 = aaqq;
00874             aaqq = aatmin;
00875             aatmin = temp1;
00876             kill = lsvec;
00877             lsvec = rsvec;
00878             rsvec = kill;
00879 
00880             rowpiv = TRUE_;
00881         }
00882 
00883     }
00884 /*     END IF L2TRAN */
00885 
00886 /*     Scale the matrix so that its maximal singular value remains less */
00887 /*     than DSQRT(BIG) -- the matrix is scaled so that its maximal column */
00888 /*     has Euclidean norm equal to DSQRT(BIG/N). The only reason to keep */
00889 /*     DSQRT(BIG) instead of BIG is the fact that DGEJSV uses LAPACK and */
00890 /*     BLAS routines that, in some implementations, are not capable of */
00891 /*     working in the full interval [SFMIN,BIG] and that they may provoke */
00892 /*     overflows in the intermediate results. If the singular values spread */
00893 /*     from SFMIN to BIG, then DGESVJ will compute them. So, in that case, */
00894 /*     one should use DGESVJ instead of DGEJSV. */
00895 
00896     big1 = sqrt(big);
00897     temp1 = sqrt(big / (doublereal) (*n));
00898 
00899     dlascl_("G", &c__0, &c__0, &aapp, &temp1, n, &c__1, &sva[1], n, &ierr);
00900     if (aaqq > aapp * sfmin) {
00901         aaqq = aaqq / aapp * temp1;
00902     } else {
00903         aaqq = aaqq * temp1 / aapp;
00904     }
00905     temp1 *= scalem;
00906     dlascl_("G", &c__0, &c__0, &aapp, &temp1, m, n, &a[a_offset], lda, &ierr);
00907 
00908 /*     To undo scaling at the end of this procedure, multiply the */
00909 /*     computed singular values with USCAL2 / USCAL1. */
00910 
00911     uscal1 = temp1;
00912     uscal2 = aapp;
00913 
00914     if (l2kill) {
00915 /*        L2KILL enforces computation of nonzero singular values in */
00916 /*        the restricted range of condition number of the initial A, */
00917 /*        sigma_max(A) / sigma_min(A) approx. DSQRT(BIG)/DSQRT(SFMIN). */
00918         xsc = sqrt(sfmin);
00919     } else {
00920         xsc = small;
00921 
00922 /*        Now, if the condition number of A is too big, */
00923 /*        sigma_max(A) / sigma_min(A) .GT. DSQRT(BIG/N) * EPSLN / SFMIN, */
00924 /*        as a precaution measure, the full SVD is computed using DGESVJ */
00925 /*        with accumulated Jacobi rotations. This provides numerically */
00926 /*        more robust computation, at the cost of slightly increased run */
00927 /*        time. Depending on the concrete implementation of BLAS and LAPACK */
00928 /*        (i.e. how they behave in presence of extreme ill-conditioning) the */
00929 /*        implementor may decide to remove this switch. */
00930         if (aaqq < sqrt(sfmin) && lsvec && rsvec) {
00931             jracc = TRUE_;
00932         }
00933 
00934     }
00935     if (aaqq < xsc) {
00936         i__1 = *n;
00937         for (p = 1; p <= i__1; ++p) {
00938             if (sva[p] < xsc) {
00939                 dlaset_("A", m, &c__1, &c_b34, &c_b34, &a[p * a_dim1 + 1], 
00940                         lda);
00941                 sva[p] = 0.;
00942             }
00943 /* L700: */
00944         }
00945     }
00946 
00947 /*     Preconditioning using QR factorization with pivoting */
00948 
00949     if (rowpiv) {
00950 /*        Optional row permutation (Bjoerck row pivoting): */
00951 /*        A result by Cox and Higham shows that the Bjoerck's */
00952 /*        row pivoting combined with standard column pivoting */
00953 /*        has similar effect as Powell-Reid complete pivoting. */
00954 /*        The ell-infinity norms of A are made nonincreasing. */
00955         i__1 = *m - 1;
00956         for (p = 1; p <= i__1; ++p) {
00957             i__2 = *m - p + 1;
00958             q = idamax_(&i__2, &work[*m + *n + p], &c__1) + p - 1;
00959             iwork[(*n << 1) + p] = q;
00960             if (p != q) {
00961                 temp1 = work[*m + *n + p];
00962                 work[*m + *n + p] = work[*m + *n + q];
00963                 work[*m + *n + q] = temp1;
00964             }
00965 /* L1952: */
00966         }
00967         i__1 = *m - 1;
00968         dlaswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[(*n << 1) + 1], &
00969                 c__1);
00970     }
00971 
00972 /*     End of the preparation phase (scaling, optional sorting and */
00973 /*     transposing, optional flushing of small columns). */
00974 
00975 /*     Preconditioning */
00976 
00977 /*     If the full SVD is needed, the right singular vectors are computed */
00978 /*     from a matrix equation, and for that we need theoretical analysis */
00979 /*     of the Businger-Golub pivoting. So we use DGEQP3 as the first RR QRF. */
00980 /*     In all other cases the first RR QRF can be chosen by other criteria */
00981 /*     (eg speed by replacing global with restricted window pivoting, such */
00982 /*     as in SGEQPX from TOMS # 782). Good results will be obtained using */
00983 /*     SGEQPX with properly (!) chosen numerical parameters. */
00984 /*     Any improvement of DGEQP3 improves overal performance of DGEJSV. */
00985 
00986 /*     A * P1 = Q1 * [ R1^t 0]^t: */
00987     i__1 = *n;
00988     for (p = 1; p <= i__1; ++p) {
00989 /*        .. all columns are free columns */
00990         iwork[p] = 0;
00991 /* L1963: */
00992     }
00993     i__1 = *lwork - *n;
00994     dgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &work[1], &work[*n + 1], &
00995             i__1, &ierr);
00996 
00997 /*     The upper triangular matrix R1 from the first QRF is inspected for */
00998 /*     rank deficiency and possibilities for deflation, or possible */
00999 /*     ill-conditioning. Depending on the user specified flag L2RANK, */
01000 /*     the procedure explores possibilities to reduce the numerical */
01001 /*     rank by inspecting the computed upper triangular factor. If */
01002 /*     L2RANK or L2ABER are up, then DGEJSV will compute the SVD of */
01003 /*     A + dA, where ||dA|| <= f(M,N)*EPSLN. */
01004 
01005     nr = 1;
01006     if (l2aber) {
01007 /*        Standard absolute error bound suffices. All sigma_i with */
01008 /*        sigma_i < N*EPSLN*||A|| are flushed to zero. This is an */
01009 /*        agressive enforcement of lower numerical rank by introducing a */
01010 /*        backward error of the order of N*EPSLN*||A||. */
01011         temp1 = sqrt((doublereal) (*n)) * epsln;
01012         i__1 = *n;
01013         for (p = 2; p <= i__1; ++p) {
01014             if ((d__2 = a[p + p * a_dim1], abs(d__2)) >= temp1 * (d__1 = a[
01015                     a_dim1 + 1], abs(d__1))) {
01016                 ++nr;
01017             } else {
01018                 goto L3002;
01019             }
01020 /* L3001: */
01021         }
01022 L3002:
01023         ;
01024     } else if (l2rank) {
01025 /*        .. similarly as above, only slightly more gentle (less agressive). */
01026 /*        Sudden drop on the diagonal of R1 is used as the criterion for */
01027 /*        close-to-rank-defficient. */
01028         temp1 = sqrt(sfmin);
01029         i__1 = *n;
01030         for (p = 2; p <= i__1; ++p) {
01031             if ((d__2 = a[p + p * a_dim1], abs(d__2)) < epsln * (d__1 = a[p - 
01032                     1 + (p - 1) * a_dim1], abs(d__1)) || (d__3 = a[p + p * 
01033                     a_dim1], abs(d__3)) < small || l2kill && (d__4 = a[p + p *
01034                      a_dim1], abs(d__4)) < temp1) {
01035                 goto L3402;
01036             }
01037             ++nr;
01038 /* L3401: */
01039         }
01040 L3402:
01041 
01042         ;
01043     } else {
01044 /*        The goal is high relative accuracy. However, if the matrix */
01045 /*        has high scaled condition number the relative accuracy is in */
01046 /*        general not feasible. Later on, a condition number estimator */
01047 /*        will be deployed to estimate the scaled condition number. */
01048 /*        Here we just remove the underflowed part of the triangular */
01049 /*        factor. This prevents the situation in which the code is */
01050 /*        working hard to get the accuracy not warranted by the data. */
01051         temp1 = sqrt(sfmin);
01052         i__1 = *n;
01053         for (p = 2; p <= i__1; ++p) {
01054             if ((d__1 = a[p + p * a_dim1], abs(d__1)) < small || l2kill && (
01055                     d__2 = a[p + p * a_dim1], abs(d__2)) < temp1) {
01056                 goto L3302;
01057             }
01058             ++nr;
01059 /* L3301: */
01060         }
01061 L3302:
01062 
01063         ;
01064     }
01065 
01066     almort = FALSE_;
01067     if (nr == *n) {
01068         maxprj = 1.;
01069         i__1 = *n;
01070         for (p = 2; p <= i__1; ++p) {
01071             temp1 = (d__1 = a[p + p * a_dim1], abs(d__1)) / sva[iwork[p]];
01072             maxprj = min(maxprj,temp1);
01073 /* L3051: */
01074         }
01075 /* Computing 2nd power */
01076         d__1 = maxprj;
01077         if (d__1 * d__1 >= 1. - (doublereal) (*n) * epsln) {
01078             almort = TRUE_;
01079         }
01080     }
01081 
01082 
01083     sconda = -1.;
01084     condr1 = -1.;
01085     condr2 = -1.;
01086 
01087     if (errest) {
01088         if (*n == nr) {
01089             if (rsvec) {
01090 /*              .. V is available as workspace */
01091                 dlacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
01092                 i__1 = *n;
01093                 for (p = 1; p <= i__1; ++p) {
01094                     temp1 = sva[iwork[p]];
01095                     d__1 = 1. / temp1;
01096                     dscal_(&p, &d__1, &v[p * v_dim1 + 1], &c__1);
01097 /* L3053: */
01098                 }
01099                 dpocon_("U", n, &v[v_offset], ldv, &c_b35, &temp1, &work[*n + 
01100                         1], &iwork[(*n << 1) + *m + 1], &ierr);
01101             } else if (lsvec) {
01102 /*              .. U is available as workspace */
01103                 dlacpy_("U", n, n, &a[a_offset], lda, &u[u_offset], ldu);
01104                 i__1 = *n;
01105                 for (p = 1; p <= i__1; ++p) {
01106                     temp1 = sva[iwork[p]];
01107                     d__1 = 1. / temp1;
01108                     dscal_(&p, &d__1, &u[p * u_dim1 + 1], &c__1);
01109 /* L3054: */
01110                 }
01111                 dpocon_("U", n, &u[u_offset], ldu, &c_b35, &temp1, &work[*n + 
01112                         1], &iwork[(*n << 1) + *m + 1], &ierr);
01113             } else {
01114                 dlacpy_("U", n, n, &a[a_offset], lda, &work[*n + 1], n);
01115                 i__1 = *n;
01116                 for (p = 1; p <= i__1; ++p) {
01117                     temp1 = sva[iwork[p]];
01118                     d__1 = 1. / temp1;
01119                     dscal_(&p, &d__1, &work[*n + (p - 1) * *n + 1], &c__1);
01120 /* L3052: */
01121                 }
01122 /*           .. the columns of R are scaled to have unit Euclidean lengths. */
01123                 dpocon_("U", n, &work[*n + 1], n, &c_b35, &temp1, &work[*n + *
01124                         n * *n + 1], &iwork[(*n << 1) + *m + 1], &ierr);
01125             }
01126             sconda = 1. / sqrt(temp1);
01127 /*           SCONDA is an estimate of DSQRT(||(R^t * R)^(-1)||_1). */
01128 /*           N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
01129         } else {
01130             sconda = -1.;
01131         }
01132     }
01133 
01134     l2pert = l2pert && (d__1 = a[a_dim1 + 1] / a[nr + nr * a_dim1], abs(d__1))
01135              > sqrt(big1);
01136 /*     If there is no violent scaling, artificial perturbation is not needed. */
01137 
01138 /*     Phase 3: */
01139 
01140     if (! (rsvec || lsvec)) {
01141 
01142 /*         Singular Values only */
01143 
01144 /*         .. transpose A(1:NR,1:N) */
01145 /* Computing MIN */
01146         i__2 = *n - 1;
01147         i__1 = min(i__2,nr);
01148         for (p = 1; p <= i__1; ++p) {
01149             i__2 = *n - p;
01150             dcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p * 
01151                     a_dim1], &c__1);
01152 /* L1946: */
01153         }
01154 
01155 /*        The following two DO-loops introduce small relative perturbation */
01156 /*        into the strict upper triangle of the lower triangular matrix. */
01157 /*        Small entries below the main diagonal are also changed. */
01158 /*        This modification is useful if the computing environment does not */
01159 /*        provide/allow FLUSH TO ZERO underflow, for it prevents many */
01160 /*        annoying denormalized numbers in case of strongly scaled matrices. */
01161 /*        The perturbation is structured so that it does not introduce any */
01162 /*        new perturbation of the singular values, and it does not destroy */
01163 /*        the job done by the preconditioner. */
01164 /*        The licence for this perturbation is in the variable L2PERT, which */
01165 /*        should be .FALSE. if FLUSH TO ZERO underflow is active. */
01166 
01167         if (! almort) {
01168 
01169             if (l2pert) {
01170 /*              XSC = DSQRT(SMALL) */
01171                 xsc = epsln / (doublereal) (*n);
01172                 i__1 = nr;
01173                 for (q = 1; q <= i__1; ++q) {
01174                     temp1 = xsc * (d__1 = a[q + q * a_dim1], abs(d__1));
01175                     i__2 = *n;
01176                     for (p = 1; p <= i__2; ++p) {
01177                         if (p > q && (d__1 = a[p + q * a_dim1], abs(d__1)) <= 
01178                                 temp1 || p < q) {
01179                             a[p + q * a_dim1] = d_sign(&temp1, &a[p + q * 
01180                                     a_dim1]);
01181                         }
01182 /* L4949: */
01183                     }
01184 /* L4947: */
01185                 }
01186             } else {
01187                 i__1 = nr - 1;
01188                 i__2 = nr - 1;
01189                 dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &a[(a_dim1 << 1) + 
01190                         1], lda);
01191             }
01192 
01193 /*            .. second preconditioning using the QR factorization */
01194 
01195             i__1 = *lwork - *n;
01196             dgeqrf_(n, &nr, &a[a_offset], lda, &work[1], &work[*n + 1], &i__1, 
01197                      &ierr);
01198 
01199 /*           .. and transpose upper to lower triangular */
01200             i__1 = nr - 1;
01201             for (p = 1; p <= i__1; ++p) {
01202                 i__2 = nr - p;
01203                 dcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p * 
01204                         a_dim1], &c__1);
01205 /* L1948: */
01206             }
01207 
01208         }
01209 
01210 /*           Row-cyclic Jacobi SVD algorithm with column pivoting */
01211 
01212 /*           .. again some perturbation (a "background noise") is added */
01213 /*           to drown denormals */
01214         if (l2pert) {
01215 /*              XSC = DSQRT(SMALL) */
01216             xsc = epsln / (doublereal) (*n);
01217             i__1 = nr;
01218             for (q = 1; q <= i__1; ++q) {
01219                 temp1 = xsc * (d__1 = a[q + q * a_dim1], abs(d__1));
01220                 i__2 = nr;
01221                 for (p = 1; p <= i__2; ++p) {
01222                     if (p > q && (d__1 = a[p + q * a_dim1], abs(d__1)) <= 
01223                             temp1 || p < q) {
01224                         a[p + q * a_dim1] = d_sign(&temp1, &a[p + q * a_dim1])
01225                                 ;
01226                     }
01227 /* L1949: */
01228                 }
01229 /* L1947: */
01230             }
01231         } else {
01232             i__1 = nr - 1;
01233             i__2 = nr - 1;
01234             dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &a[(a_dim1 << 1) + 1], 
01235                     lda);
01236         }
01237 
01238 /*           .. and one-sided Jacobi rotations are started on a lower */
01239 /*           triangular matrix (plus perturbation which is ignored in */
01240 /*           the part which destroys triangular form (confusing?!)) */
01241 
01242         dgesvj_("L", "NoU", "NoV", &nr, &nr, &a[a_offset], lda, &sva[1], n, &
01243                 v[v_offset], ldv, &work[1], lwork, info);
01244 
01245         scalem = work[1];
01246         numrank = i_dnnt(&work[2]);
01247 
01248 
01249     } else if (rsvec && ! lsvec) {
01250 
01251 /*        -> Singular Values and Right Singular Vectors <- */
01252 
01253         if (almort) {
01254 
01255 /*           .. in this case NR equals N */
01256             i__1 = nr;
01257             for (p = 1; p <= i__1; ++p) {
01258                 i__2 = *n - p + 1;
01259                 dcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
01260                         c__1);
01261 /* L1998: */
01262             }
01263             i__1 = nr - 1;
01264             i__2 = nr - 1;
01265             dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) + 
01266                     1], ldv);
01267 
01268             dgesvj_("L", "U", "N", n, &nr, &v[v_offset], ldv, &sva[1], &nr, &
01269                     a[a_offset], lda, &work[1], lwork, info);
01270             scalem = work[1];
01271             numrank = i_dnnt(&work[2]);
01272         } else {
01273 
01274 /*        .. two more QR factorizations ( one QRF is not enough, two require */
01275 /*        accumulated product of Jacobi rotations, three are perfect ) */
01276 
01277             i__1 = nr - 1;
01278             i__2 = nr - 1;
01279             dlaset_("Lower", &i__1, &i__2, &c_b34, &c_b34, &a[a_dim1 + 2], 
01280                     lda);
01281             i__1 = *lwork - *n;
01282             dgelqf_(&nr, n, &a[a_offset], lda, &work[1], &work[*n + 1], &i__1, 
01283                      &ierr);
01284             dlacpy_("Lower", &nr, &nr, &a[a_offset], lda, &v[v_offset], ldv);
01285             i__1 = nr - 1;
01286             i__2 = nr - 1;
01287             dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) + 
01288                     1], ldv);
01289             i__1 = *lwork - (*n << 1);
01290             dgeqrf_(&nr, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*n << 
01291                     1) + 1], &i__1, &ierr);
01292             i__1 = nr;
01293             for (p = 1; p <= i__1; ++p) {
01294                 i__2 = nr - p + 1;
01295                 dcopy_(&i__2, &v[p + p * v_dim1], ldv, &v[p + p * v_dim1], &
01296                         c__1);
01297 /* L8998: */
01298             }
01299             i__1 = nr - 1;
01300             i__2 = nr - 1;
01301             dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) + 
01302                     1], ldv);
01303 
01304             dgesvj_("Lower", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[1], &
01305                     nr, &u[u_offset], ldu, &work[*n + 1], lwork, info);
01306             scalem = work[*n + 1];
01307             numrank = i_dnnt(&work[*n + 2]);
01308             if (nr < *n) {
01309                 i__1 = *n - nr;
01310                 dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + v_dim1], 
01311                         ldv);
01312                 i__1 = *n - nr;
01313                 dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) * v_dim1 
01314                         + 1], ldv);
01315                 i__1 = *n - nr;
01316                 i__2 = *n - nr;
01317                 dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 + (nr + 
01318                         1) * v_dim1], ldv);
01319             }
01320 
01321             i__1 = *lwork - *n;
01322             dormlq_("Left", "Transpose", n, n, &nr, &a[a_offset], lda, &work[
01323                     1], &v[v_offset], ldv, &work[*n + 1], &i__1, &ierr);
01324 
01325         }
01326 
01327         i__1 = *n;
01328         for (p = 1; p <= i__1; ++p) {
01329             dcopy_(n, &v[p + v_dim1], ldv, &a[iwork[p] + a_dim1], lda);
01330 /* L8991: */
01331         }
01332         dlacpy_("All", n, n, &a[a_offset], lda, &v[v_offset], ldv);
01333 
01334         if (transp) {
01335             dlacpy_("All", n, n, &v[v_offset], ldv, &u[u_offset], ldu);
01336         }
01337 
01338     } else if (lsvec && ! rsvec) {
01339 
01340 /*        -#- Singular Values and Left Singular Vectors                 -#- */
01341 
01342 /*        .. second preconditioning step to avoid need to accumulate */
01343 /*        Jacobi rotations in the Jacobi iterations. */
01344         i__1 = nr;
01345         for (p = 1; p <= i__1; ++p) {
01346             i__2 = *n - p + 1;
01347             dcopy_(&i__2, &a[p + p * a_dim1], lda, &u[p + p * u_dim1], &c__1);
01348 /* L1965: */
01349         }
01350         i__1 = nr - 1;
01351         i__2 = nr - 1;
01352         dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 1], 
01353                 ldu);
01354 
01355         i__1 = *lwork - (*n << 1);
01356         dgeqrf_(n, &nr, &u[u_offset], ldu, &work[*n + 1], &work[(*n << 1) + 1]
01357 , &i__1, &ierr);
01358 
01359         i__1 = nr - 1;
01360         for (p = 1; p <= i__1; ++p) {
01361             i__2 = nr - p;
01362             dcopy_(&i__2, &u[p + (p + 1) * u_dim1], ldu, &u[p + 1 + p * 
01363                     u_dim1], &c__1);
01364 /* L1967: */
01365         }
01366         i__1 = nr - 1;
01367         i__2 = nr - 1;
01368         dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 1], 
01369                 ldu);
01370 
01371         i__1 = *lwork - *n;
01372         dgesvj_("Lower", "U", "N", &nr, &nr, &u[u_offset], ldu, &sva[1], &nr, 
01373                 &a[a_offset], lda, &work[*n + 1], &i__1, info);
01374         scalem = work[*n + 1];
01375         numrank = i_dnnt(&work[*n + 2]);
01376 
01377         if (nr < *m) {
01378             i__1 = *m - nr;
01379             dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 + u_dim1], ldu);
01380             if (nr < n1) {
01381                 i__1 = n1 - nr;
01382                 dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) * u_dim1 
01383                         + 1], ldu);
01384                 i__1 = *m - nr;
01385                 i__2 = n1 - nr;
01386                 dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 + (nr + 
01387                         1) * u_dim1], ldu);
01388             }
01389         }
01390 
01391         i__1 = *lwork - *n;
01392         dormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[1], &u[
01393                 u_offset], ldu, &work[*n + 1], &i__1, &ierr);
01394 
01395         if (rowpiv) {
01396             i__1 = *m - 1;
01397             dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n << 1) + 
01398                     1], &c_n1);
01399         }
01400 
01401         i__1 = n1;
01402         for (p = 1; p <= i__1; ++p) {
01403             xsc = 1. / dnrm2_(m, &u[p * u_dim1 + 1], &c__1);
01404             dscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
01405 /* L1974: */
01406         }
01407 
01408         if (transp) {
01409             dlacpy_("All", n, n, &u[u_offset], ldu, &v[v_offset], ldv);
01410         }
01411 
01412     } else {
01413 
01414 /*        -#- Full SVD -#- */
01415 
01416         if (! jracc) {
01417 
01418             if (! almort) {
01419 
01420 /*           Second Preconditioning Step (QRF [with pivoting]) */
01421 /*           Note that the composition of TRANSPOSE, QRF and TRANSPOSE is */
01422 /*           equivalent to an LQF CALL. Since in many libraries the QRF */
01423 /*           seems to be better optimized than the LQF, we do explicit */
01424 /*           transpose and use the QRF. This is subject to changes in an */
01425 /*           optimized implementation of DGEJSV. */
01426 
01427                 i__1 = nr;
01428                 for (p = 1; p <= i__1; ++p) {
01429                     i__2 = *n - p + 1;
01430                     dcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], 
01431                              &c__1);
01432 /* L1968: */
01433                 }
01434 
01435 /*           .. the following two loops perturb small entries to avoid */
01436 /*           denormals in the second QR factorization, where they are */
01437 /*           as good as zeros. This is done to avoid painfully slow */
01438 /*           computation with denormals. The relative size of the perturbation */
01439 /*           is a parameter that can be changed by the implementer. */
01440 /*           This perturbation device will be obsolete on machines with */
01441 /*           properly implemented arithmetic. */
01442 /*           To switch it off, set L2PERT=.FALSE. To remove it from  the */
01443 /*           code, remove the action under L2PERT=.TRUE., leave the ELSE part. */
01444 /*           The following two loops should be blocked and fused with the */
01445 /*           transposed copy above. */
01446 
01447                 if (l2pert) {
01448                     xsc = sqrt(small);
01449                     i__1 = nr;
01450                     for (q = 1; q <= i__1; ++q) {
01451                         temp1 = xsc * (d__1 = v[q + q * v_dim1], abs(d__1));
01452                         i__2 = *n;
01453                         for (p = 1; p <= i__2; ++p) {
01454                             if (p > q && (d__1 = v[p + q * v_dim1], abs(d__1))
01455                                      <= temp1 || p < q) {
01456                                 v[p + q * v_dim1] = d_sign(&temp1, &v[p + q * 
01457                                         v_dim1]);
01458                             }
01459                             if (p < q) {
01460                                 v[p + q * v_dim1] = -v[p + q * v_dim1];
01461                             }
01462 /* L2968: */
01463                         }
01464 /* L2969: */
01465                     }
01466                 } else {
01467                     i__1 = nr - 1;
01468                     i__2 = nr - 1;
01469                     dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 
01470                             1) + 1], ldv);
01471                 }
01472 
01473 /*           Estimate the row scaled condition number of R1 */
01474 /*           (If R1 is rectangular, N > NR, then the condition number */
01475 /*           of the leading NR x NR submatrix is estimated.) */
01476 
01477                 dlacpy_("L", &nr, &nr, &v[v_offset], ldv, &work[(*n << 1) + 1]
01478 , &nr);
01479                 i__1 = nr;
01480                 for (p = 1; p <= i__1; ++p) {
01481                     i__2 = nr - p + 1;
01482                     temp1 = dnrm2_(&i__2, &work[(*n << 1) + (p - 1) * nr + p], 
01483                              &c__1);
01484                     i__2 = nr - p + 1;
01485                     d__1 = 1. / temp1;
01486                     dscal_(&i__2, &d__1, &work[(*n << 1) + (p - 1) * nr + p], 
01487                             &c__1);
01488 /* L3950: */
01489                 }
01490                 dpocon_("Lower", &nr, &work[(*n << 1) + 1], &nr, &c_b35, &
01491                         temp1, &work[(*n << 1) + nr * nr + 1], &iwork[*m + (*
01492                         n << 1) + 1], &ierr);
01493                 condr1 = 1. / sqrt(temp1);
01494 /*           .. here need a second oppinion on the condition number */
01495 /*           .. then assume worst case scenario */
01496 /*           R1 is OK for inverse <=> CONDR1 .LT. DBLE(N) */
01497 /*           more conservative    <=> CONDR1 .LT. DSQRT(DBLE(N)) */
01498 
01499                 cond_ok__ = sqrt((doublereal) nr);
01500 /* [TP]       COND_OK is a tuning parameter. */
01501                 if (condr1 < cond_ok__) {
01502 /*              .. the second QRF without pivoting. Note: in an optimized */
01503 /*              implementation, this QRF should be implemented as the QRF */
01504 /*              of a lower triangular matrix. */
01505 /*              R1^t = Q2 * R2 */
01506                     i__1 = *lwork - (*n << 1);
01507                     dgeqrf_(n, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*
01508                             n << 1) + 1], &i__1, &ierr);
01509 
01510                     if (l2pert) {
01511                         xsc = sqrt(small) / epsln;
01512                         i__1 = nr;
01513                         for (p = 2; p <= i__1; ++p) {
01514                             i__2 = p - 1;
01515                             for (q = 1; q <= i__2; ++q) {
01516 /* Computing MIN */
01517                                 d__3 = (d__1 = v[p + p * v_dim1], abs(d__1)), 
01518                                         d__4 = (d__2 = v[q + q * v_dim1], abs(
01519                                         d__2));
01520                                 temp1 = xsc * min(d__3,d__4);
01521                                 if ((d__1 = v[q + p * v_dim1], abs(d__1)) <= 
01522                                         temp1) {
01523                                     v[q + p * v_dim1] = d_sign(&temp1, &v[q + 
01524                                             p * v_dim1]);
01525                                 }
01526 /* L3958: */
01527                             }
01528 /* L3959: */
01529                         }
01530                     }
01531 
01532                     if (nr != *n) {
01533                         dlacpy_("A", n, &nr, &v[v_offset], ldv, &work[(*n << 
01534                                 1) + 1], n);
01535                     }
01536 /*              .. save ... */
01537 
01538 /*           .. this transposed copy should be better than naive */
01539                     i__1 = nr - 1;
01540                     for (p = 1; p <= i__1; ++p) {
01541                         i__2 = nr - p;
01542                         dcopy_(&i__2, &v[p + (p + 1) * v_dim1], ldv, &v[p + 1 
01543                                 + p * v_dim1], &c__1);
01544 /* L1969: */
01545                     }
01546 
01547                     condr2 = condr1;
01548 
01549                 } else {
01550 
01551 /*              .. ill-conditioned case: second QRF with pivoting */
01552 /*              Note that windowed pivoting would be equaly good */
01553 /*              numerically, and more run-time efficient. So, in */
01554 /*              an optimal implementation, the next call to DGEQP3 */
01555 /*              should be replaced with eg. CALL SGEQPX (ACM TOMS #782) */
01556 /*              with properly (carefully) chosen parameters. */
01557 
01558 /*              R1^t * P2 = Q2 * R2 */
01559                     i__1 = nr;
01560                     for (p = 1; p <= i__1; ++p) {
01561                         iwork[*n + p] = 0;
01562 /* L3003: */
01563                     }
01564                     i__1 = *lwork - (*n << 1);
01565                     dgeqp3_(n, &nr, &v[v_offset], ldv, &iwork[*n + 1], &work[*
01566                             n + 1], &work[(*n << 1) + 1], &i__1, &ierr);
01567 /* *               CALL DGEQRF( N, NR, V, LDV, WORK(N+1), WORK(2*N+1), */
01568 /* *     &              LWORK-2*N, IERR ) */
01569                     if (l2pert) {
01570                         xsc = sqrt(small);
01571                         i__1 = nr;
01572                         for (p = 2; p <= i__1; ++p) {
01573                             i__2 = p - 1;
01574                             for (q = 1; q <= i__2; ++q) {
01575 /* Computing MIN */
01576                                 d__3 = (d__1 = v[p + p * v_dim1], abs(d__1)), 
01577                                         d__4 = (d__2 = v[q + q * v_dim1], abs(
01578                                         d__2));
01579                                 temp1 = xsc * min(d__3,d__4);
01580                                 if ((d__1 = v[q + p * v_dim1], abs(d__1)) <= 
01581                                         temp1) {
01582                                     v[q + p * v_dim1] = d_sign(&temp1, &v[q + 
01583                                             p * v_dim1]);
01584                                 }
01585 /* L3968: */
01586                             }
01587 /* L3969: */
01588                         }
01589                     }
01590 
01591                     dlacpy_("A", n, &nr, &v[v_offset], ldv, &work[(*n << 1) + 
01592                             1], n);
01593 
01594                     if (l2pert) {
01595                         xsc = sqrt(small);
01596                         i__1 = nr;
01597                         for (p = 2; p <= i__1; ++p) {
01598                             i__2 = p - 1;
01599                             for (q = 1; q <= i__2; ++q) {
01600 /* Computing MIN */
01601                                 d__3 = (d__1 = v[p + p * v_dim1], abs(d__1)), 
01602                                         d__4 = (d__2 = v[q + q * v_dim1], abs(
01603                                         d__2));
01604                                 temp1 = xsc * min(d__3,d__4);
01605                                 v[p + q * v_dim1] = -d_sign(&temp1, &v[q + p *
01606                                          v_dim1]);
01607 /* L8971: */
01608                             }
01609 /* L8970: */
01610                         }
01611                     } else {
01612                         i__1 = nr - 1;
01613                         i__2 = nr - 1;
01614                         dlaset_("L", &i__1, &i__2, &c_b34, &c_b34, &v[v_dim1 
01615                                 + 2], ldv);
01616                     }
01617 /*              Now, compute R2 = L3 * Q3, the LQ factorization. */
01618                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
01619                     dgelqf_(&nr, &nr, &v[v_offset], ldv, &work[(*n << 1) + *n 
01620                             * nr + 1], &work[(*n << 1) + *n * nr + nr + 1], &
01621                             i__1, &ierr);
01622 /*              .. and estimate the condition number */
01623                     dlacpy_("L", &nr, &nr, &v[v_offset], ldv, &work[(*n << 1) 
01624                             + *n * nr + nr + 1], &nr);
01625                     i__1 = nr;
01626                     for (p = 1; p <= i__1; ++p) {
01627                         temp1 = dnrm2_(&p, &work[(*n << 1) + *n * nr + nr + p]
01628 , &nr);
01629                         d__1 = 1. / temp1;
01630                         dscal_(&p, &d__1, &work[(*n << 1) + *n * nr + nr + p], 
01631                                  &nr);
01632 /* L4950: */
01633                     }
01634                     dpocon_("L", &nr, &work[(*n << 1) + *n * nr + nr + 1], &
01635                             nr, &c_b35, &temp1, &work[(*n << 1) + *n * nr + 
01636                             nr + nr * nr + 1], &iwork[*m + (*n << 1) + 1], &
01637                             ierr);
01638                     condr2 = 1. / sqrt(temp1);
01639 
01640                     if (condr2 >= cond_ok__) {
01641 /*                 .. save the Householder vectors used for Q3 */
01642 /*                 (this overwrittes the copy of R2, as it will not be */
01643 /*                 needed in this branch, but it does not overwritte the */
01644 /*                 Huseholder vectors of Q2.). */
01645                         dlacpy_("U", &nr, &nr, &v[v_offset], ldv, &work[(*n <<
01646                                  1) + 1], n);
01647 /*                 .. and the rest of the information on Q3 is in */
01648 /*                 WORK(2*N+N*NR+1:2*N+N*NR+N) */
01649                     }
01650 
01651                 }
01652 
01653                 if (l2pert) {
01654                     xsc = sqrt(small);
01655                     i__1 = nr;
01656                     for (q = 2; q <= i__1; ++q) {
01657                         temp1 = xsc * v[q + q * v_dim1];
01658                         i__2 = q - 1;
01659                         for (p = 1; p <= i__2; ++p) {
01660 /*                    V(p,q) = - DSIGN( TEMP1, V(q,p) ) */
01661                             v[p + q * v_dim1] = -d_sign(&temp1, &v[p + q * 
01662                                     v_dim1]);
01663 /* L4969: */
01664                         }
01665 /* L4968: */
01666                     }
01667                 } else {
01668                     i__1 = nr - 1;
01669                     i__2 = nr - 1;
01670                     dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 
01671                             1) + 1], ldv);
01672                 }
01673 
01674 /*        Second preconditioning finished; continue with Jacobi SVD */
01675 /*        The input matrix is lower trinagular. */
01676 
01677 /*        Recover the right singular vectors as solution of a well */
01678 /*        conditioned triangular matrix equation. */
01679 
01680                 if (condr1 < cond_ok__) {
01681 
01682                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
01683                     dgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
01684                             1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
01685                              nr + nr + 1], &i__1, info);
01686                     scalem = work[(*n << 1) + *n * nr + nr + 1];
01687                     numrank = i_dnnt(&work[(*n << 1) + *n * nr + nr + 2]);
01688                     i__1 = nr;
01689                     for (p = 1; p <= i__1; ++p) {
01690                         dcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1 
01691                                 + 1], &c__1);
01692                         dscal_(&nr, &sva[p], &v[p * v_dim1 + 1], &c__1);
01693 /* L3970: */
01694                     }
01695 /*        .. pick the right matrix equation and solve it */
01696 
01697                     if (nr == *n) {
01698 /* :))             .. best case, R1 is inverted. The solution of this matrix */
01699 /*                 equation is Q2*V2 = the product of the Jacobi rotations */
01700 /*                 used in DGESVJ, premultiplied with the orthogonal matrix */
01701 /*                 from the second QR factorization. */
01702                         dtrsm_("L", "U", "N", "N", &nr, &nr, &c_b35, &a[
01703                                 a_offset], lda, &v[v_offset], ldv);
01704                     } else {
01705 /*                 .. R1 is well conditioned, but non-square. Transpose(R2) */
01706 /*                 is inverted to get the product of the Jacobi rotations */
01707 /*                 used in DGESVJ. The Q-factor from the second QR */
01708 /*                 factorization is then built in explicitly. */
01709                         dtrsm_("L", "U", "T", "N", &nr, &nr, &c_b35, &work[(*
01710                                 n << 1) + 1], n, &v[v_offset], ldv);
01711                         if (nr < *n) {
01712                             i__1 = *n - nr;
01713                             dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 
01714                                     1 + v_dim1], ldv);
01715                             i__1 = *n - nr;
01716                             dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 
01717                                     1) * v_dim1 + 1], ldv);
01718                             i__1 = *n - nr;
01719                             i__2 = *n - nr;
01720                             dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr 
01721                                     + 1 + (nr + 1) * v_dim1], ldv);
01722                         }
01723                         i__1 = *lwork - (*n << 1) - *n * nr - nr;
01724                         dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, 
01725                                 &work[*n + 1], &v[v_offset], ldv, &work[(*n <<
01726                                  1) + *n * nr + nr + 1], &i__1, &ierr);
01727                     }
01728 
01729                 } else if (condr2 < cond_ok__) {
01730 
01731 /* :)           .. the input matrix A is very likely a relative of */
01732 /*              the Kahan matrix :) */
01733 /*              The matrix R2 is inverted. The solution of the matrix equation */
01734 /*              is Q3^T*V3 = the product of the Jacobi rotations (appplied to */
01735 /*              the lower triangular L3 from the LQ factorization of */
01736 /*              R2=L3*Q3), pre-multiplied with the transposed Q3. */
01737                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
01738                     dgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
01739                             1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
01740                              nr + nr + 1], &i__1, info);
01741                     scalem = work[(*n << 1) + *n * nr + nr + 1];
01742                     numrank = i_dnnt(&work[(*n << 1) + *n * nr + nr + 2]);
01743                     i__1 = nr;
01744                     for (p = 1; p <= i__1; ++p) {
01745                         dcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1 
01746                                 + 1], &c__1);
01747                         dscal_(&nr, &sva[p], &u[p * u_dim1 + 1], &c__1);
01748 /* L3870: */
01749                     }
01750                     dtrsm_("L", "U", "N", "N", &nr, &nr, &c_b35, &work[(*n << 
01751                             1) + 1], n, &u[u_offset], ldu);
01752 /*              .. apply the permutation from the second QR factorization */
01753                     i__1 = nr;
01754                     for (q = 1; q <= i__1; ++q) {
01755                         i__2 = nr;
01756                         for (p = 1; p <= i__2; ++p) {
01757                             work[(*n << 1) + *n * nr + nr + iwork[*n + p]] = 
01758                                     u[p + q * u_dim1];
01759 /* L872: */
01760                         }
01761                         i__2 = nr;
01762                         for (p = 1; p <= i__2; ++p) {
01763                             u[p + q * u_dim1] = work[(*n << 1) + *n * nr + nr 
01764                                     + p];
01765 /* L874: */
01766                         }
01767 /* L873: */
01768                     }
01769                     if (nr < *n) {
01770                         i__1 = *n - nr;
01771                         dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + 
01772                                 v_dim1], ldv);
01773                         i__1 = *n - nr;
01774                         dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) *
01775                                  v_dim1 + 1], ldv);
01776                         i__1 = *n - nr;
01777                         i__2 = *n - nr;
01778                         dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 
01779                                 + (nr + 1) * v_dim1], ldv);
01780                     }
01781                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
01782                     dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &
01783                             work[*n + 1], &v[v_offset], ldv, &work[(*n << 1) 
01784                             + *n * nr + nr + 1], &i__1, &ierr);
01785                 } else {
01786 /*              Last line of defense. */
01787 /* #:(          This is a rather pathological case: no scaled condition */
01788 /*              improvement after two pivoted QR factorizations. Other */
01789 /*              possibility is that the rank revealing QR factorization */
01790 /*              or the condition estimator has failed, or the COND_OK */
01791 /*              is set very close to ONE (which is unnecessary). Normally, */
01792 /*              this branch should never be executed, but in rare cases of */
01793 /*              failure of the RRQR or condition estimator, the last line of */
01794 /*              defense ensures that DGEJSV completes the task. */
01795 /*              Compute the full SVD of L3 using DGESVJ with explicit */
01796 /*              accumulation of Jacobi rotations. */
01797                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
01798                     dgesvj_("L", "U", "V", &nr, &nr, &v[v_offset], ldv, &sva[
01799                             1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
01800                              nr + nr + 1], &i__1, info);
01801                     scalem = work[(*n << 1) + *n * nr + nr + 1];
01802                     numrank = i_dnnt(&work[(*n << 1) + *n * nr + nr + 2]);
01803                     if (nr < *n) {
01804                         i__1 = *n - nr;
01805                         dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + 
01806                                 v_dim1], ldv);
01807                         i__1 = *n - nr;
01808                         dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) *
01809                                  v_dim1 + 1], ldv);
01810                         i__1 = *n - nr;
01811                         i__2 = *n - nr;
01812                         dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 
01813                                 + (nr + 1) * v_dim1], ldv);
01814                     }
01815                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
01816                     dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &
01817                             work[*n + 1], &v[v_offset], ldv, &work[(*n << 1) 
01818                             + *n * nr + nr + 1], &i__1, &ierr);
01819 
01820                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
01821                     dormlq_("L", "T", &nr, &nr, &nr, &work[(*n << 1) + 1], n, 
01822                             &work[(*n << 1) + *n * nr + 1], &u[u_offset], ldu, 
01823                              &work[(*n << 1) + *n * nr + nr + 1], &i__1, &
01824                             ierr);
01825                     i__1 = nr;
01826                     for (q = 1; q <= i__1; ++q) {
01827                         i__2 = nr;
01828                         for (p = 1; p <= i__2; ++p) {
01829                             work[(*n << 1) + *n * nr + nr + iwork[*n + p]] = 
01830                                     u[p + q * u_dim1];
01831 /* L772: */
01832                         }
01833                         i__2 = nr;
01834                         for (p = 1; p <= i__2; ++p) {
01835                             u[p + q * u_dim1] = work[(*n << 1) + *n * nr + nr 
01836                                     + p];
01837 /* L774: */
01838                         }
01839 /* L773: */
01840                     }
01841 
01842                 }
01843 
01844 /*           Permute the rows of V using the (column) permutation from the */
01845 /*           first QRF. Also, scale the columns to make them unit in */
01846 /*           Euclidean norm. This applies to all cases. */
01847 
01848                 temp1 = sqrt((doublereal) (*n)) * epsln;
01849                 i__1 = *n;
01850                 for (q = 1; q <= i__1; ++q) {
01851                     i__2 = *n;
01852                     for (p = 1; p <= i__2; ++p) {
01853                         work[(*n << 1) + *n * nr + nr + iwork[p]] = v[p + q * 
01854                                 v_dim1];
01855 /* L972: */
01856                     }
01857                     i__2 = *n;
01858                     for (p = 1; p <= i__2; ++p) {
01859                         v[p + q * v_dim1] = work[(*n << 1) + *n * nr + nr + p]
01860                                 ;
01861 /* L973: */
01862                     }
01863                     xsc = 1. / dnrm2_(n, &v[q * v_dim1 + 1], &c__1);
01864                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
01865                         dscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
01866                     }
01867 /* L1972: */
01868                 }
01869 /*           At this moment, V contains the right singular vectors of A. */
01870 /*           Next, assemble the left singular vector matrix U (M x N). */
01871                 if (nr < *m) {
01872                     i__1 = *m - nr;
01873                     dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 + 
01874                             u_dim1], ldu);
01875                     if (nr < n1) {
01876                         i__1 = n1 - nr;
01877                         dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) *
01878                                  u_dim1 + 1], ldu);
01879                         i__1 = *m - nr;
01880                         i__2 = n1 - nr;
01881                         dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 
01882                                 + (nr + 1) * u_dim1], ldu);
01883                     }
01884                 }
01885 
01886 /*           The Q matrix from the first QRF is built into the left singular */
01887 /*           matrix U. This applies to all cases. */
01888 
01889                 i__1 = *lwork - *n;
01890                 dormqr_("Left", "No_Tr", m, &n1, n, &a[a_offset], lda, &work[
01891                         1], &u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
01892 /*           The columns of U are normalized. The cost is O(M*N) flops. */
01893                 temp1 = sqrt((doublereal) (*m)) * epsln;
01894                 i__1 = nr;
01895                 for (p = 1; p <= i__1; ++p) {
01896                     xsc = 1. / dnrm2_(m, &u[p * u_dim1 + 1], &c__1);
01897                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
01898                         dscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
01899                     }
01900 /* L1973: */
01901                 }
01902 
01903 /*           If the initial QRF is computed with row pivoting, the left */
01904 /*           singular vectors must be adjusted. */
01905 
01906                 if (rowpiv) {
01907                     i__1 = *m - 1;
01908                     dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n 
01909                             << 1) + 1], &c_n1);
01910                 }
01911 
01912             } else {
01913 
01914 /*        .. the initial matrix A has almost orthogonal columns and */
01915 /*        the second QRF is not needed */
01916 
01917                 dlacpy_("Upper", n, n, &a[a_offset], lda, &work[*n + 1], n);
01918                 if (l2pert) {
01919                     xsc = sqrt(small);
01920                     i__1 = *n;
01921                     for (p = 2; p <= i__1; ++p) {
01922                         temp1 = xsc * work[*n + (p - 1) * *n + p];
01923                         i__2 = p - 1;
01924                         for (q = 1; q <= i__2; ++q) {
01925                             work[*n + (q - 1) * *n + p] = -d_sign(&temp1, &
01926                                     work[*n + (p - 1) * *n + q]);
01927 /* L5971: */
01928                         }
01929 /* L5970: */
01930                     }
01931                 } else {
01932                     i__1 = *n - 1;
01933                     i__2 = *n - 1;
01934                     dlaset_("Lower", &i__1, &i__2, &c_b34, &c_b34, &work[*n + 
01935                             2], n);
01936                 }
01937 
01938                 i__1 = *lwork - *n - *n * *n;
01939                 dgesvj_("Upper", "U", "N", n, n, &work[*n + 1], n, &sva[1], n, 
01940                          &u[u_offset], ldu, &work[*n + *n * *n + 1], &i__1, 
01941                         info);
01942 
01943                 scalem = work[*n + *n * *n + 1];
01944                 numrank = i_dnnt(&work[*n + *n * *n + 2]);
01945                 i__1 = *n;
01946                 for (p = 1; p <= i__1; ++p) {
01947                     dcopy_(n, &work[*n + (p - 1) * *n + 1], &c__1, &u[p * 
01948                             u_dim1 + 1], &c__1);
01949                     dscal_(n, &sva[p], &work[*n + (p - 1) * *n + 1], &c__1);
01950 /* L6970: */
01951                 }
01952 
01953                 dtrsm_("Left", "Upper", "NoTrans", "No UD", n, n, &c_b35, &a[
01954                         a_offset], lda, &work[*n + 1], n);
01955                 i__1 = *n;
01956                 for (p = 1; p <= i__1; ++p) {
01957                     dcopy_(n, &work[*n + p], n, &v[iwork[p] + v_dim1], ldv);
01958 /* L6972: */
01959                 }
01960                 temp1 = sqrt((doublereal) (*n)) * epsln;
01961                 i__1 = *n;
01962                 for (p = 1; p <= i__1; ++p) {
01963                     xsc = 1. / dnrm2_(n, &v[p * v_dim1 + 1], &c__1);
01964                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
01965                         dscal_(n, &xsc, &v[p * v_dim1 + 1], &c__1);
01966                     }
01967 /* L6971: */
01968                 }
01969 
01970 /*           Assemble the left singular vector matrix U (M x N). */
01971 
01972                 if (*n < *m) {
01973                     i__1 = *m - *n;
01974                     dlaset_("A", &i__1, n, &c_b34, &c_b34, &u[nr + 1 + u_dim1]
01975 , ldu);
01976                     if (*n < n1) {
01977                         i__1 = n1 - *n;
01978                         dlaset_("A", n, &i__1, &c_b34, &c_b34, &u[(*n + 1) * 
01979                                 u_dim1 + 1], ldu);
01980                         i__1 = *m - *n;
01981                         i__2 = n1 - *n;
01982                         dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 
01983                                 + (*n + 1) * u_dim1], ldu);
01984                     }
01985                 }
01986                 i__1 = *lwork - *n;
01987                 dormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[
01988                         1], &u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
01989                 temp1 = sqrt((doublereal) (*m)) * epsln;
01990                 i__1 = n1;
01991                 for (p = 1; p <= i__1; ++p) {
01992                     xsc = 1. / dnrm2_(m, &u[p * u_dim1 + 1], &c__1);
01993                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
01994                         dscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
01995                     }
01996 /* L6973: */
01997                 }
01998 
01999                 if (rowpiv) {
02000                     i__1 = *m - 1;
02001                     dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n 
02002                             << 1) + 1], &c_n1);
02003                 }
02004 
02005             }
02006 
02007 /*        end of the  >> almost orthogonal case <<  in the full SVD */
02008 
02009         } else {
02010 
02011 /*        This branch deploys a preconditioned Jacobi SVD with explicitly */
02012 /*        accumulated rotations. It is included as optional, mainly for */
02013 /*        experimental purposes. It does perfom well, and can also be used. */
02014 /*        In this implementation, this branch will be automatically activated */
02015 /*        if the  condition number sigma_max(A) / sigma_min(A) is predicted */
02016 /*        to be greater than the overflow threshold. This is because the */
02017 /*        a posteriori computation of the singular vectors assumes robust */
02018 /*        implementation of BLAS and some LAPACK procedures, capable of working */
02019 /*        in presence of extreme values. Since that is not always the case, ... */
02020 
02021             i__1 = nr;
02022             for (p = 1; p <= i__1; ++p) {
02023                 i__2 = *n - p + 1;
02024                 dcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
02025                         c__1);
02026 /* L7968: */
02027             }
02028 
02029             if (l2pert) {
02030                 xsc = sqrt(small / epsln);
02031                 i__1 = nr;
02032                 for (q = 1; q <= i__1; ++q) {
02033                     temp1 = xsc * (d__1 = v[q + q * v_dim1], abs(d__1));
02034                     i__2 = *n;
02035                     for (p = 1; p <= i__2; ++p) {
02036                         if (p > q && (d__1 = v[p + q * v_dim1], abs(d__1)) <= 
02037                                 temp1 || p < q) {
02038                             v[p + q * v_dim1] = d_sign(&temp1, &v[p + q * 
02039                                     v_dim1]);
02040                         }
02041                         if (p < q) {
02042                             v[p + q * v_dim1] = -v[p + q * v_dim1];
02043                         }
02044 /* L5968: */
02045                     }
02046 /* L5969: */
02047                 }
02048             } else {
02049                 i__1 = nr - 1;
02050                 i__2 = nr - 1;
02051                 dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) + 
02052                         1], ldv);
02053             }
02054             i__1 = *lwork - (*n << 1);
02055             dgeqrf_(n, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*n << 1) 
02056                     + 1], &i__1, &ierr);
02057             dlacpy_("L", n, &nr, &v[v_offset], ldv, &work[(*n << 1) + 1], n);
02058 
02059             i__1 = nr;
02060             for (p = 1; p <= i__1; ++p) {
02061                 i__2 = nr - p + 1;
02062                 dcopy_(&i__2, &v[p + p * v_dim1], ldv, &u[p + p * u_dim1], &
02063                         c__1);
02064 /* L7969: */
02065             }
02066             if (l2pert) {
02067                 xsc = sqrt(small / epsln);
02068                 i__1 = nr;
02069                 for (q = 2; q <= i__1; ++q) {
02070                     i__2 = q - 1;
02071                     for (p = 1; p <= i__2; ++p) {
02072 /* Computing MIN */
02073                         d__3 = (d__1 = u[p + p * u_dim1], abs(d__1)), d__4 = (
02074                                 d__2 = u[q + q * u_dim1], abs(d__2));
02075                         temp1 = xsc * min(d__3,d__4);
02076                         u[p + q * u_dim1] = -d_sign(&temp1, &u[q + p * u_dim1]
02077                                 );
02078 /* L9971: */
02079                     }
02080 /* L9970: */
02081                 }
02082             } else {
02083                 i__1 = nr - 1;
02084                 i__2 = nr - 1;
02085                 dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 
02086                         1], ldu);
02087             }
02088             i__1 = *lwork - (*n << 1) - *n * nr;
02089             dgesvj_("G", "U", "V", &nr, &nr, &u[u_offset], ldu, &sva[1], n, &
02090                     v[v_offset], ldv, &work[(*n << 1) + *n * nr + 1], &i__1, 
02091                     info);
02092             scalem = work[(*n << 1) + *n * nr + 1];
02093             numrank = i_dnnt(&work[(*n << 1) + *n * nr + 2]);
02094             if (nr < *n) {
02095                 i__1 = *n - nr;
02096                 dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + v_dim1], 
02097                         ldv);
02098                 i__1 = *n - nr;
02099                 dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) * v_dim1 
02100                         + 1], ldv);
02101                 i__1 = *n - nr;
02102                 i__2 = *n - nr;
02103                 dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 + (nr + 
02104                         1) * v_dim1], ldv);
02105             }
02106             i__1 = *lwork - (*n << 1) - *n * nr - nr;
02107             dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &work[*n + 
02108                     1], &v[v_offset], ldv, &work[(*n << 1) + *n * nr + nr + 1]
02109 , &i__1, &ierr);
02110 
02111 /*           Permute the rows of V using the (column) permutation from the */
02112 /*           first QRF. Also, scale the columns to make them unit in */
02113 /*           Euclidean norm. This applies to all cases. */
02114 
02115             temp1 = sqrt((doublereal) (*n)) * epsln;
02116             i__1 = *n;
02117             for (q = 1; q <= i__1; ++q) {
02118                 i__2 = *n;
02119                 for (p = 1; p <= i__2; ++p) {
02120                     work[(*n << 1) + *n * nr + nr + iwork[p]] = v[p + q * 
02121                             v_dim1];
02122 /* L8972: */
02123                 }
02124                 i__2 = *n;
02125                 for (p = 1; p <= i__2; ++p) {
02126                     v[p + q * v_dim1] = work[(*n << 1) + *n * nr + nr + p];
02127 /* L8973: */
02128                 }
02129                 xsc = 1. / dnrm2_(n, &v[q * v_dim1 + 1], &c__1);
02130                 if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
02131                     dscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
02132                 }
02133 /* L7972: */
02134             }
02135 
02136 /*           At this moment, V contains the right singular vectors of A. */
02137 /*           Next, assemble the left singular vector matrix U (M x N). */
02138 
02139             if (*n < *m) {
02140                 i__1 = *m - *n;
02141                 dlaset_("A", &i__1, n, &c_b34, &c_b34, &u[nr + 1 + u_dim1], 
02142                         ldu);
02143                 if (*n < n1) {
02144                     i__1 = n1 - *n;
02145                     dlaset_("A", n, &i__1, &c_b34, &c_b34, &u[(*n + 1) * 
02146                             u_dim1 + 1], ldu);
02147                     i__1 = *m - *n;
02148                     i__2 = n1 - *n;
02149                     dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 + (*
02150                             n + 1) * u_dim1], ldu);
02151                 }
02152             }
02153 
02154             i__1 = *lwork - *n;
02155             dormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[1], &
02156                     u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
02157 
02158             if (rowpiv) {
02159                 i__1 = *m - 1;
02160                 dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n << 1)
02161                          + 1], &c_n1);
02162             }
02163 
02164 
02165         }
02166         if (transp) {
02167 /*           .. swap U and V because the procedure worked on A^t */
02168             i__1 = *n;
02169             for (p = 1; p <= i__1; ++p) {
02170                 dswap_(n, &u[p * u_dim1 + 1], &c__1, &v[p * v_dim1 + 1], &
02171                         c__1);
02172 /* L6974: */
02173             }
02174         }
02175 
02176     }
02177 /*     end of the full SVD */
02178 
02179 /*     Undo scaling, if necessary (and possible) */
02180 
02181     if (uscal2 <= big / sva[1] * uscal1) {
02182         dlascl_("G", &c__0, &c__0, &uscal1, &uscal2, &nr, &c__1, &sva[1], n, &
02183                 ierr);
02184         uscal1 = 1.;
02185         uscal2 = 1.;
02186     }
02187 
02188     if (nr < *n) {
02189         i__1 = *n;
02190         for (p = nr + 1; p <= i__1; ++p) {
02191             sva[p] = 0.;
02192 /* L3004: */
02193         }
02194     }
02195 
02196     work[1] = uscal2 * scalem;
02197     work[2] = uscal1;
02198     if (errest) {
02199         work[3] = sconda;
02200     }
02201     if (lsvec && rsvec) {
02202         work[4] = condr1;
02203         work[5] = condr2;
02204     }
02205     if (l2tran) {
02206         work[6] = entra;
02207         work[7] = entrat;
02208     }
02209 
02210     iwork[1] = nr;
02211     iwork[2] = numrank;
02212     iwork[3] = warning;
02213 
02214     return 0;
02215 /*     .. */
02216 /*     .. END OF DGEJSV */
02217 /*     .. */
02218 } /* dgejsv_ */


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