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


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