C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / dgejsv.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static integer c__1 = 1;
516 static doublereal c_b34 = 0.;
517 static doublereal c_b35 = 1.;
518 static integer c__0 = 0;
519 static integer c_n1 = -1;
520
521 /* > \brief \b DGEJSV */
522
523 /*  =========== DOCUMENTATION =========== */
524
525 /* Online html documentation available at */
526 /*            http://www.netlib.org/lapack/explore-html/ */
527
528 /* > \htmlonly */
529 /* > Download DGEJSV + dependencies */
530 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dgejsv.
531 f"> */
532 /* > [TGZ]</a> */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dgejsv.
534 f"> */
535 /* > [ZIP]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dgejsv.
537 f"> */
538 /* > [TXT]</a> */
539 /* > \endhtmlonly */
540
541 /*  Definition: */
542 /*  =========== */
543
544 /*       SUBROUTINE DGEJSV( JOBA, JOBU, JOBV, JOBR, JOBT, JOBP, */
545 /*                          M, N, A, LDA, SVA, U, LDU, V, LDV, */
546 /*                          WORK, LWORK, IWORK, INFO ) */
547
548 /*       IMPLICIT    NONE */
549 /*       INTEGER     INFO, LDA, LDU, LDV, LWORK, M, N */
550 /*       DOUBLE PRECISION A( LDA, * ), SVA( N ), U( LDU, * ), V( LDV, * ), */
551 /*      $            WORK( LWORK ) */
552 /*       INTEGER     IWORK( * ) */
553 /*       CHARACTER*1 JOBA, JOBP, JOBR, JOBT, JOBU, JOBV */
554
555
556 /* > \par Purpose: */
557 /*  ============= */
558 /* > */
559 /* > \verbatim */
560 /* > */
561 /* > DGEJSV computes the singular value decomposition (SVD) of a real M-by-N */
562 /* > matrix [A], where M >= N. The SVD of [A] is written as */
563 /* > */
564 /* >              [A] = [U] * [SIGMA] * [V]^t, */
565 /* > */
566 /* > where [SIGMA] is an N-by-N (M-by-N) matrix which is zero except for its N */
567 /* > diagonal elements, [U] is an M-by-N (or M-by-M) orthonormal matrix, and */
568 /* > [V] is an N-by-N orthogonal matrix. The diagonal elements of [SIGMA] are */
569 /* > the singular values of [A]. The columns of [U] and [V] are the left and */
570 /* > the right singular vectors of [A], respectively. The matrices [U] and [V] */
571 /* > are computed and stored in the arrays U and V, respectively. The diagonal */
572 /* > of [SIGMA] is computed and stored in the array SVA. */
573 /* > DGEJSV can sometimes compute tiny singular values and their singular vectors much */
574 /* > more accurately than other SVD routines, see below under Further Details. */
575 /* > \endverbatim */
576
577 /*  Arguments: */
578 /*  ========== */
579
580 /* > \param[in] JOBA */
581 /* > \verbatim */
582 /* >          JOBA is CHARACTER*1 */
583 /* >        Specifies the level of accuracy: */
584 /* >       = 'C': This option works well (high relative accuracy) if A = B * D, */
585 /* >             with well-conditioned B and arbitrary diagonal matrix D. */
586 /* >             The accuracy cannot be spoiled by COLUMN scaling. The */
587 /* >             accuracy of the computed output depends on the condition of */
588 /* >             B, and the procedure aims at the best theoretical accuracy. */
589 /* >             The relative error max_{i=1:N}|d sigma_i| / sigma_i is */
590 /* >             bounded by f(M,N)*epsilon* cond(B), independent of D. */
591 /* >             The input matrix is preprocessed with the QRF with column */
592 /* >             pivoting. This initial preprocessing and preconditioning by */
593 /* >             a rank revealing QR factorization is common for all values of */
594 /* >             JOBA. Additional actions are specified as follows: */
595 /* >       = 'E': Computation as with 'C' with an additional estimate of the */
596 /* >             condition number of B. It provides a realistic error bound. */
597 /* >       = 'F': If A = D1 * C * D2 with ill-conditioned diagonal scalings */
598 /* >             D1, D2, and well-conditioned matrix C, this option gives */
599 /* >             higher accuracy than the 'C' option. If the structure of the */
600 /* >             input matrix is not known, and relative accuracy is */
601 /* >             desirable, then this option is advisable. The input matrix A */
602 /* >             is preprocessed with QR factorization with FULL (row and */
603 /* >             column) pivoting. */
604 /* >       = 'G': Computation as with 'F' with an additional estimate of the */
605 /* >             condition number of B, where A=D*B. If A has heavily weighted */
606 /* >             rows, then using this condition number gives too pessimistic */
607 /* >             error bound. */
608 /* >       = 'A': Small singular values are the noise and the matrix is treated */
609 /* >             as numerically rank deficient. The error in the computed */
610 /* >             singular values is bounded by f(m,n)*epsilon*||A||. */
611 /* >             The computed SVD A = U * S * V^t restores A up to */
612 /* >             f(m,n)*epsilon*||A||. */
613 /* >             This gives the procedure the licence to discard (set to zero) */
614 /* >             all singular values below N*epsilon*||A||. */
615 /* >       = 'R': Similar as in 'A'. Rank revealing property of the initial */
616 /* >             QR factorization is used do reveal (using triangular factor) */
617 /* >             a gap sigma_{r+1} < epsilon * sigma_r in which case the */
618 /* >             numerical RANK is declared to be r. The SVD is computed with */
619 /* >             absolute error bounds, but more accurately than with 'A'. */
620 /* > \endverbatim */
621 /* > */
622 /* > \param[in] JOBU */
623 /* > \verbatim */
624 /* >          JOBU is CHARACTER*1 */
625 /* >        Specifies whether to compute the columns of U: */
626 /* >       = 'U': N columns of U are returned in the array U. */
627 /* >       = 'F': full set of M left sing. vectors is returned in the array U. */
628 /* >       = 'W': U may be used as workspace of length M*N. See the description */
629 /* >             of U. */
630 /* >       = 'N': U is not computed. */
631 /* > \endverbatim */
632 /* > */
633 /* > \param[in] JOBV */
634 /* > \verbatim */
635 /* >          JOBV is CHARACTER*1 */
636 /* >        Specifies whether to compute the matrix V: */
637 /* >       = 'V': N columns of V are returned in the array V; Jacobi rotations */
638 /* >             are not explicitly accumulated. */
639 /* >       = 'J': N columns of V are returned in the array V, but they are */
640 /* >             computed as the product of Jacobi rotations. This option is */
641 /* >             allowed only if JOBU .NE. 'N', i.e. in computing the full SVD. */
642 /* >       = 'W': V may be used as workspace of length N*N. See the description */
643 /* >             of V. */
644 /* >       = 'N': V is not computed. */
645 /* > \endverbatim */
646 /* > */
647 /* > \param[in] JOBR */
648 /* > \verbatim */
649 /* >          JOBR is CHARACTER*1 */
650 /* >        Specifies the RANGE for the singular values. Issues the licence to */
651 /* >        set to zero small positive singular values if they are outside */
652 /* >        specified range. If A .NE. 0 is scaled so that the largest singular */
653 /* >        value of c*A is around DSQRT(BIG), BIG=SLAMCH('O'), then JOBR issues */
654 /* >        the licence to kill columns of A whose norm in c*A is less than */
655 /* >        DSQRT(SFMIN) (for JOBR = 'R'), or less than SMALL=SFMIN/EPSLN, */
656 /* >        where SFMIN=SLAMCH('S'), EPSLN=SLAMCH('E'). */
657 /* >       = 'N': Do not kill small columns of c*A. This option assumes that */
658 /* >             BLAS and QR factorizations and triangular solvers are */
659 /* >             implemented to work in that range. If the condition of A */
660 /* >             is greater than BIG, use DGESVJ. */
661 /* >       = 'R': RESTRICTED range for sigma(c*A) is [DSQRT(SFMIN), DSQRT(BIG)] */
662 /* >             (roughly, as described above). This option is recommended. */
663 /* >                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
664 /* >        For computing the singular values in the FULL range [SFMIN,BIG] */
665 /* >        use DGESVJ. */
666 /* > \endverbatim */
667 /* > */
668 /* > \param[in] JOBT */
669 /* > \verbatim */
670 /* >          JOBT is CHARACTER*1 */
671 /* >        If the matrix is square then the procedure may determine to use */
672 /* >        transposed A if A^t seems to be better with respect to convergence. */
673 /* >        If the matrix is not square, JOBT is ignored. This is subject to */
674 /* >        changes in the future. */
675 /* >        The decision is based on two values of entropy over the adjoint */
676 /* >        orbit of A^t * A. See the descriptions of WORK(6) and WORK(7). */
677 /* >       = 'T': transpose if entropy test indicates possibly faster */
678 /* >        convergence of Jacobi process if A^t is taken as input. If A is */
679 /* >        replaced with A^t, then the row pivoting is included automatically. */
680 /* >       = 'N': do not speculate. */
681 /* >        This option can be used to compute only the singular values, or the */
682 /* >        full SVD (U, SIGMA and V). For only one set of singular vectors */
683 /* >        (U or V), the caller should provide both U and V, as one of the */
684 /* >        matrices is used as workspace if the matrix A is transposed. */
685 /* >        The implementer can easily remove this constraint and make the */
686 /* >        code more complicated. See the descriptions of U and V. */
687 /* > \endverbatim */
688 /* > */
689 /* > \param[in] JOBP */
690 /* > \verbatim */
691 /* >          JOBP is CHARACTER*1 */
692 /* >        Issues the licence to introduce structured perturbations to drown */
693 /* >        denormalized numbers. This licence should be active if the */
694 /* >        denormals are poorly implemented, causing slow computation, */
695 /* >        especially in cases of fast convergence (!). For details see [1,2]. */
696 /* >        For the sake of simplicity, this perturbations are included only */
697 /* >        when the full SVD or only the singular values are requested. The */
698 /* >        implementer/user can easily add the perturbation for the cases of */
699 /* >        computing one set of singular vectors. */
700 /* >       = 'P': introduce perturbation */
701 /* >       = 'N': do not perturb */
702 /* > \endverbatim */
703 /* > */
704 /* > \param[in] M */
705 /* > \verbatim */
706 /* >          M is INTEGER */
707 /* >         The number of rows of the input matrix A.  M >= 0. */
708 /* > \endverbatim */
709 /* > */
710 /* > \param[in] N */
711 /* > \verbatim */
712 /* >          N is INTEGER */
713 /* >         The number of columns of the input matrix A. M >= N >= 0. */
714 /* > \endverbatim */
715 /* > */
716 /* > \param[in,out] A */
717 /* > \verbatim */
718 /* >          A is DOUBLE PRECISION array, dimension (LDA,N) */
719 /* >          On entry, the M-by-N matrix A. */
720 /* > \endverbatim */
721 /* > */
722 /* > \param[in] LDA */
723 /* > \verbatim */
724 /* >          LDA is INTEGER */
725 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,M). */
726 /* > \endverbatim */
727 /* > */
728 /* > \param[out] SVA */
729 /* > \verbatim */
730 /* >          SVA is DOUBLE PRECISION array, dimension (N) */
731 /* >          On exit, */
732 /* >          - For WORK(1)/WORK(2) = ONE: The singular values of A. During the */
733 /* >            computation SVA contains Euclidean column norms of the */
734 /* >            iterated matrices in the array A. */
735 /* >          - For WORK(1) .NE. WORK(2): The singular values of A are */
736 /* >            (WORK(1)/WORK(2)) * SVA(1:N). This factored form is used if */
737 /* >            sigma_max(A) overflows or if small singular values have been */
738 /* >            saved from underflow by scaling the input matrix A. */
739 /* >          - If JOBR='R' then some of the singular values may be returned */
740 /* >            as exact zeros obtained by "set to zero" because they are */
741 /* >            below the numerical rank threshold or are denormalized numbers. */
742 /* > \endverbatim */
743 /* > */
744 /* > \param[out] U */
745 /* > \verbatim */
746 /* >          U is DOUBLE PRECISION array, dimension ( LDU, N ) */
747 /* >          If JOBU = 'U', then U contains on exit the M-by-N matrix of */
748 /* >                         the left singular vectors. */
749 /* >          If JOBU = 'F', then U contains on exit the M-by-M matrix of */
750 /* >                         the left singular vectors, including an ONB */
751 /* >                         of the orthogonal complement of the Range(A). */
752 /* >          If JOBU = 'W'  .AND. (JOBV = 'V' .AND. JOBT = 'T' .AND. M = N), */
753 /* >                         then U is used as workspace if the procedure */
754 /* >                         replaces A with A^t. In that case, [V] is computed */
755 /* >                         in U as left singular vectors of A^t and then */
756 /* >                         copied back to the V array. This 'W' option is just */
757 /* >                         a reminder to the caller that in this case U is */
758 /* >                         reserved as workspace of length N*N. */
759 /* >          If JOBU = 'N'  U is not referenced, unless JOBT='T'. */
760 /* > \endverbatim */
761 /* > */
762 /* > \param[in] LDU */
763 /* > \verbatim */
764 /* >          LDU is INTEGER */
765 /* >          The leading dimension of the array U,  LDU >= 1. */
766 /* >          IF  JOBU = 'U' or 'F' or 'W',  then LDU >= M. */
767 /* > \endverbatim */
768 /* > */
769 /* > \param[out] V */
770 /* > \verbatim */
771 /* >          V is DOUBLE PRECISION array, dimension ( LDV, N ) */
772 /* >          If JOBV = 'V', 'J' then V contains on exit the N-by-N matrix of */
773 /* >                         the right singular vectors; */
774 /* >          If JOBV = 'W', AND (JOBU = 'U' AND JOBT = 'T' AND M = N), */
775 /* >                         then V is used as workspace if the pprocedure */
776 /* >                         replaces A with A^t. In that case, [U] is computed */
777 /* >                         in V as right singular vectors of A^t and then */
778 /* >                         copied back to the U array. This 'W' option is just */
779 /* >                         a reminder to the caller that in this case V is */
780 /* >                         reserved as workspace of length N*N. */
781 /* >          If JOBV = 'N'  V is not referenced, unless JOBT='T'. */
782 /* > \endverbatim */
783 /* > */
784 /* > \param[in] LDV */
785 /* > \verbatim */
786 /* >          LDV is INTEGER */
787 /* >          The leading dimension of the array V,  LDV >= 1. */
788 /* >          If JOBV = 'V' or 'J' or 'W', then LDV >= N. */
789 /* > \endverbatim */
790 /* > */
791 /* > \param[out] WORK */
792 /* > \verbatim */
793 /* >          WORK is DOUBLE PRECISION array, dimension (LWORK) */
794 /* >          On exit, if N > 0 .AND. M > 0 (else not referenced), */
795 /* >          WORK(1) = SCALE = WORK(2) / WORK(1) is the scaling factor such */
796 /* >                    that SCALE*SVA(1:N) are the computed singular values */
797 /* >                    of A. (See the description of SVA().) */
798 /* >          WORK(2) = See the description of WORK(1). */
799 /* >          WORK(3) = SCONDA is an estimate for the condition number of */
800 /* >                    column equilibrated A. (If JOBA = 'E' or 'G') */
801 /* >                    SCONDA is an estimate of DSQRT(||(R^t * R)^(-1)||_1). */
802 /* >                    It is computed using DPOCON. It holds */
803 /* >                    N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
804 /* >                    where R is the triangular factor from the QRF of A. */
805 /* >                    However, if R is truncated and the numerical rank is */
806 /* >                    determined to be strictly smaller than N, SCONDA is */
807 /* >                    returned as -1, thus indicating that the smallest */
808 /* >                    singular values might be lost. */
809 /* > */
810 /* >          If full SVD is needed, the following two condition numbers are */
811 /* >          useful for the analysis of the algorithm. They are provied for */
812 /* >          a developer/implementer who is familiar with the details of */
813 /* >          the method. */
814 /* > */
815 /* >          WORK(4) = an estimate of the scaled condition number of the */
816 /* >                    triangular factor in the first QR factorization. */
817 /* >          WORK(5) = an estimate of the scaled condition number of the */
818 /* >                    triangular factor in the second QR factorization. */
819 /* >          The following two parameters are computed if JOBT = 'T'. */
820 /* >          They are provided for a developer/implementer who is familiar */
821 /* >          with the details of the method. */
822 /* > */
823 /* >          WORK(6) = the entropy of A^t*A :: this is the Shannon entropy */
824 /* >                    of diag(A^t*A) / Trace(A^t*A) taken as point in the */
825 /* >                    probability simplex. */
826 /* >          WORK(7) = the entropy of A*A^t. */
827 /* > \endverbatim */
828 /* > */
829 /* > \param[in] LWORK */
830 /* > \verbatim */
831 /* >          LWORK is INTEGER */
832 /* >          Length of WORK to confirm proper allocation of work space. */
833 /* >          LWORK depends on the job: */
834 /* > */
835 /* >          If only SIGMA is needed (JOBU = 'N', JOBV = 'N') and */
836 /* >            -> .. no scaled condition estimate required (JOBE = 'N'): */
837 /* >               LWORK >= f2cmax(2*M+N,4*N+1,7). This is the minimal requirement. */
838 /* >               ->> For optimal performance (blocked code) the optimal value */
839 /* >               is LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB,7). Here NB is the optimal */
840 /* >               block size for DGEQP3 and DGEQRF. */
841 /* >               In general, optimal LWORK is computed as */
842 /* >               LWORK >= f2cmax(2*M+N,N+LWORK(DGEQP3),N+LWORK(DGEQRF), 7). */
843 /* >            -> .. an estimate of the scaled condition number of A is */
844 /* >               required (JOBA='E', 'G'). In this case, LWORK is the maximum */
845 /* >               of the above and N*N+4*N, i.e. LWORK >= f2cmax(2*M+N,N*N+4*N,7). */
846 /* >               ->> For optimal performance (blocked code) the optimal value */
847 /* >               is LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB, N*N+4*N, 7). */
848 /* >               In general, the optimal length LWORK is computed as */
849 /* >               LWORK >= f2cmax(2*M+N,N+LWORK(DGEQP3),N+LWORK(DGEQRF), */
850 /* >                                                     N+N*N+LWORK(DPOCON),7). */
851 /* > */
852 /* >          If SIGMA and the right singular vectors are needed (JOBV = 'V'), */
853 /* >            -> the minimal requirement is LWORK >= f2cmax(2*M+N,4*N+1,7). */
854 /* >            -> For optimal performance, LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB,7), */
855 /* >               where NB is the optimal block size for DGEQP3, DGEQRF, DGELQF, */
856 /* >               DORMLQ. In general, the optimal length LWORK is computed as */
857 /* >               LWORK >= f2cmax(2*M+N,N+LWORK(DGEQP3), N+LWORK(DPOCON), */
858 /* >                       N+LWORK(DGELQF), 2*N+LWORK(DGEQRF), N+LWORK(DORMLQ)). */
859 /* > */
860 /* >          If SIGMA and the left singular vectors are needed */
861 /* >            -> the minimal requirement is LWORK >= f2cmax(2*M+N,4*N+1,7). */
862 /* >            -> For optimal performance: */
863 /* >               if JOBU = 'U' :: LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB,7), */
864 /* >               if JOBU = 'F' :: LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB,N+M*NB,7), */
865 /* >               where NB is the optimal block size for DGEQP3, DGEQRF, DORMQR. */
866 /* >               In general, the optimal length LWORK is computed as */
867 /* >               LWORK >= f2cmax(2*M+N,N+LWORK(DGEQP3),N+LWORK(DPOCON), */
868 /* >                        2*N+LWORK(DGEQRF), N+LWORK(DORMQR)). */
869 /* >               Here LWORK(DORMQR) equals N*NB (for JOBU = 'U') or */
870 /* >               M*NB (for JOBU = 'F'). */
871 /* > */
872 /* >          If the full SVD is needed: (JOBU = 'U' or JOBU = 'F') and */
873 /* >            -> if JOBV = 'V' */
874 /* >               the minimal requirement is LWORK >= f2cmax(2*M+N,6*N+2*N*N). */
875 /* >            -> if JOBV = 'J' the minimal requirement is */
876 /* >               LWORK >= f2cmax(2*M+N, 4*N+N*N,2*N+N*N+6). */
877 /* >            -> For optimal performance, LWORK should be additionally */
878 /* >               larger than N+M*NB, where NB is the optimal block size */
879 /* >               for DORMQR. */
880 /* > \endverbatim */
881 /* > */
882 /* > \param[out] IWORK */
883 /* > \verbatim */
884 /* >          IWORK is INTEGER array, dimension (M+3*N). */
885 /* >          On exit, */
886 /* >          IWORK(1) = the numerical rank determined after the initial */
887 /* >                     QR factorization with pivoting. See the descriptions */
888 /* >                     of JOBA and JOBR. */
889 /* >          IWORK(2) = the number of the computed nonzero singular values */
890 /* >          IWORK(3) = if nonzero, a warning message: */
891 /* >                     If IWORK(3) = 1 then some of the column norms of A */
892 /* >                     were denormalized floats. The requested high accuracy */
893 /* >                     is not warranted by the data. */
894 /* > \endverbatim */
895 /* > */
896 /* > \param[out] INFO */
897 /* > \verbatim */
898 /* >          INFO is INTEGER */
899 /* >           < 0:  if INFO = -i, then the i-th argument had an illegal value. */
900 /* >           = 0:  successful exit; */
901 /* >           > 0:  DGEJSV  did not converge in the maximal allowed number */
902 /* >                 of sweeps. The computed values may be inaccurate. */
903 /* > \endverbatim */
904
905 /*  Authors: */
906 /*  ======== */
907
908 /* > \author Univ. of Tennessee */
909 /* > \author Univ. of California Berkeley */
910 /* > \author Univ. of Colorado Denver */
911 /* > \author NAG Ltd. */
912
913 /* > \date June 2016 */
914
915 /* > \ingroup doubleGEsing */
916
917 /* > \par Further Details: */
918 /*  ===================== */
919 /* > */
920 /* > \verbatim */
921 /* > */
922 /* >  DGEJSV implements a preconditioned Jacobi SVD algorithm. It uses DGEQP3, */
923 /* >  DGEQRF, and DGELQF as preprocessors and preconditioners. Optionally, an */
924 /* >  additional row pivoting can be used as a preprocessor, which in some */
925 /* >  cases results in much higher accuracy. An example is matrix A with the */
926 /* >  structure A = D1 * C * D2, where D1, D2 are arbitrarily ill-conditioned */
927 /* >  diagonal matrices and C is well-conditioned matrix. In that case, complete */
928 /* >  pivoting in the first QR factorizations provides accuracy dependent on the */
929 /* >  condition number of C, and independent of D1, D2. Such higher accuracy is */
930 /* >  not completely understood theoretically, but it works well in practice. */
931 /* >  Further, if A can be written as A = B*D, with well-conditioned B and some */
932 /* >  diagonal D, then the high accuracy is guaranteed, both theoretically and */
933 /* >  in software, independent of D. For more details see [1], [2]. */
934 /* >     The computational range for the singular values can be the full range */
935 /* >  ( UNDERFLOW,OVERFLOW ), provided that the machine arithmetic and the BLAS */
936 /* >  & LAPACK routines called by DGEJSV are implemented to work in that range. */
937 /* >  If that is not the case, then the restriction for safe computation with */
938 /* >  the singular values in the range of normalized IEEE numbers is that the */
939 /* >  spectral condition number kappa(A)=sigma_max(A)/sigma_min(A) does not */
940 /* >  overflow. This code (DGEJSV) is best used in this restricted range, */
941 /* >  meaning that singular values of magnitude below ||A||_2 / DLAMCH('O') are */
942 /* >  returned as zeros. See JOBR for details on this. */
943 /* >     Further, this implementation is somewhat slower than the one described */
944 /* >  in [1,2] due to replacement of some non-LAPACK components, and because */
945 /* >  the choice of some tuning parameters in the iterative part (DGESVJ) is */
946 /* >  left to the implementer on a particular machine. */
947 /* >     The rank revealing QR factorization (in this code: DGEQP3) should be */
948 /* >  implemented as in [3]. We have a new version of DGEQP3 under development */
949 /* >  that is more robust than the current one in LAPACK, with a cleaner cut in */
950 /* >  rank deficient cases. It will be available in the SIGMA library [4]. */
951 /* >  If M is much larger than N, it is obvious that the initial QRF with */
952 /* >  column pivoting can be preprocessed by the QRF without pivoting. That */
953 /* >  well known trick is not used in DGEJSV because in some cases heavy row */
954 /* >  weighting can be treated with complete pivoting. The overhead in cases */
955 /* >  M much larger than N is then only due to pivoting, but the benefits in */
956 /* >  terms of accuracy have prevailed. The implementer/user can incorporate */
957 /* >  this extra QRF step easily. The implementer can also improve data movement */
958 /* >  (matrix transpose, matrix copy, matrix transposed copy) - this */
959 /* >  implementation of DGEJSV uses only the simplest, naive data movement. */
960 /* > \endverbatim */
961
962 /* > \par Contributors: */
963 /*  ================== */
964 /* > */
965 /* >  Zlatko Drmac (Zagreb, Croatia) and Kresimir Veselic (Hagen, Germany) */
966
967 /* > \par References: */
968 /*  ================ */
969 /* > */
970 /* > \verbatim */
971 /* > */
972 /* > [1] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I. */
973 /* >     SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342. */
974 /* >     LAPACK Working note 169. */
975 /* > [2] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II. */
976 /* >     SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362. */
977 /* >     LAPACK Working note 170. */
978 /* > [3] Z. Drmac and Z. Bujanovic: On the failure of rank-revealing QR */
979 /* >     factorization software - a case study. */
980 /* >     ACM Trans. Math. Softw. Vol. 35, No 2 (2008), pp. 1-28. */
981 /* >     LAPACK Working note 176. */
982 /* > [4] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV, */
983 /* >     QSVD, (H,K)-SVD computations. */
984 /* >     Department of Mathematics, University of Zagreb, 2008. */
985 /* > \endverbatim */
986
987 /* >  \par Bugs, examples and comments: */
988 /*   ================================= */
989 /* > */
990 /* >  Please report all bugs and send interesting examples and/or comments to */
991 /* >  drmac@math.hr. Thank you. */
992 /* > */
993 /*  ===================================================================== */
994 /* Subroutine */ int dgejsv_(char *joba, char *jobu, char *jobv, char *jobr, 
995         char *jobt, char *jobp, integer *m, integer *n, doublereal *a, 
996         integer *lda, doublereal *sva, doublereal *u, integer *ldu, 
997         doublereal *v, integer *ldv, doublereal *work, integer *lwork, 
998         integer *iwork, integer *info)
999 {
1000     /* System generated locals */
1001     integer a_dim1, a_offset, u_dim1, u_offset, v_dim1, v_offset, i__1, i__2, 
1002             i__3, i__4, i__5, i__6, i__7, i__8, i__9, i__10, i__11, i__12;
1003     doublereal d__1, d__2, d__3, d__4;
1004
1005     /* Local variables */
1006     logical defr;
1007     doublereal aapp, aaqq;
1008     logical kill;
1009     integer ierr;
1010     extern doublereal dnrm2_(integer *, doublereal *, integer *);
1011     doublereal temp1;
1012     integer p, q;
1013     logical jracc;
1014     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
1015             integer *);
1016     extern logical lsame_(char *, char *);
1017     doublereal small, entra, sfmin;
1018     logical lsvec;
1019     extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
1020             doublereal *, integer *), dswap_(integer *, doublereal *, integer 
1021             *, doublereal *, integer *);
1022     doublereal epsln;
1023     logical rsvec;
1024     extern /* Subroutine */ int dtrsm_(char *, char *, char *, char *, 
1025             integer *, integer *, doublereal *, doublereal *, integer *, 
1026             doublereal *, integer *);
1027     integer n1;
1028     logical l2aber;
1029     extern /* Subroutine */ int dgeqp3_(integer *, integer *, doublereal *, 
1030             integer *, integer *, doublereal *, doublereal *, integer *, 
1031             integer *);
1032     doublereal condr1, condr2, uscal1, uscal2;
1033     logical l2kill, l2rank, l2tran, l2pert;
1034     extern doublereal dlamch_(char *);
1035     integer nr;
1036     extern /* Subroutine */ int dgelqf_(integer *, integer *, doublereal *, 
1037             integer *, doublereal *, doublereal *, integer *, integer *);
1038     extern integer idamax_(integer *, doublereal *, integer *);
1039     doublereal scalem;
1040     extern /* Subroutine */ int dlascl_(char *, integer *, integer *, 
1041             doublereal *, doublereal *, integer *, integer *, doublereal *, 
1042             integer *, integer *);
1043     doublereal sconda;
1044     logical goscal;
1045     doublereal aatmin;
1046     extern /* Subroutine */ int dgeqrf_(integer *, integer *, doublereal *, 
1047             integer *, doublereal *, doublereal *, integer *, integer *);
1048     doublereal aatmax;
1049     extern /* Subroutine */ int dlacpy_(char *, integer *, integer *, 
1050             doublereal *, integer *, doublereal *, integer *), 
1051             dlaset_(char *, integer *, integer *, doublereal *, doublereal *, 
1052             doublereal *, integer *), xerbla_(char *, integer *, ftnlen);
1053     logical noscal;
1054     extern /* Subroutine */ int dpocon_(char *, integer *, doublereal *, 
1055             integer *, doublereal *, doublereal *, doublereal *, integer *, 
1056             integer *), dgesvj_(char *, char *, char *, integer *, 
1057             integer *, doublereal *, integer *, doublereal *, integer *, 
1058             doublereal *, integer *, doublereal *, integer *, integer *), dlassq_(integer *, doublereal *, integer 
1059             *, doublereal *, doublereal *), dlaswp_(integer *, doublereal *, 
1060             integer *, integer *, integer *, integer *, integer *);
1061     doublereal entrat;
1062     logical almort;
1063     extern /* Subroutine */ int dorgqr_(integer *, integer *, integer *, 
1064             doublereal *, integer *, doublereal *, doublereal *, integer *, 
1065             integer *), dormlq_(char *, char *, integer *, integer *, integer 
1066             *, doublereal *, integer *, doublereal *, doublereal *, integer *,
1067              doublereal *, integer *, integer *);
1068     doublereal maxprj;
1069     logical errest;
1070     extern /* Subroutine */ int dormqr_(char *, char *, integer *, integer *, 
1071             integer *, doublereal *, integer *, doublereal *, doublereal *, 
1072             integer *, doublereal *, integer *, integer *);
1073     logical transp, rowpiv;
1074     doublereal big, cond_ok__, xsc, big1;
1075     integer warning, numrank;
1076
1077
1078 /*  -- LAPACK computational routine (version 3.7.1) -- */
1079 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
1080 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
1081 /*     June 2016 */
1082
1083
1084 /*  =========================================================================== */
1085
1086
1087
1088 /*     Test the input arguments */
1089
1090     /* Parameter adjustments */
1091     --sva;
1092     a_dim1 = *lda;
1093     a_offset = 1 + a_dim1 * 1;
1094     a -= a_offset;
1095     u_dim1 = *ldu;
1096     u_offset = 1 + u_dim1 * 1;
1097     u -= u_offset;
1098     v_dim1 = *ldv;
1099     v_offset = 1 + v_dim1 * 1;
1100     v -= v_offset;
1101     --work;
1102     --iwork;
1103
1104     /* Function Body */
1105     lsvec = lsame_(jobu, "U") || lsame_(jobu, "F");
1106     jracc = lsame_(jobv, "J");
1107     rsvec = lsame_(jobv, "V") || jracc;
1108     rowpiv = lsame_(joba, "F") || lsame_(joba, "G");
1109     l2rank = lsame_(joba, "R");
1110     l2aber = lsame_(joba, "A");
1111     errest = lsame_(joba, "E") || lsame_(joba, "G");
1112     l2tran = lsame_(jobt, "T");
1113     l2kill = lsame_(jobr, "R");
1114     defr = lsame_(jobr, "N");
1115     l2pert = lsame_(jobp, "P");
1116
1117     if (! (rowpiv || l2rank || l2aber || errest || lsame_(joba, "C"))) {
1118         *info = -1;
1119     } else if (! (lsvec || lsame_(jobu, "N") || lsame_(
1120             jobu, "W"))) {
1121         *info = -2;
1122     } else if (! (rsvec || lsame_(jobv, "N") || lsame_(
1123             jobv, "W")) || jracc && ! lsvec) {
1124         *info = -3;
1125     } else if (! (l2kill || defr)) {
1126         *info = -4;
1127     } else if (! (l2tran || lsame_(jobt, "N"))) {
1128         *info = -5;
1129     } else if (! (l2pert || lsame_(jobp, "N"))) {
1130         *info = -6;
1131     } else if (*m < 0) {
1132         *info = -7;
1133     } else if (*n < 0 || *n > *m) {
1134         *info = -8;
1135     } else if (*lda < *m) {
1136         *info = -10;
1137     } else if (lsvec && *ldu < *m) {
1138         *info = -13;
1139     } else if (rsvec && *ldv < *n) {
1140         *info = -15;
1141     } else /* if(complicated condition) */ {
1142 /* Computing MAX */
1143         i__1 = 7, i__2 = (*n << 2) + 1, i__1 = f2cmax(i__1,i__2), i__2 = (*m << 
1144                 1) + *n;
1145 /* Computing MAX */
1146         i__3 = 7, i__4 = (*n << 2) + *n * *n, i__3 = f2cmax(i__3,i__4), i__4 = (*
1147                 m << 1) + *n;
1148 /* Computing MAX */
1149         i__5 = 7, i__6 = (*m << 1) + *n, i__5 = f2cmax(i__5,i__6), i__6 = (*n << 
1150                 2) + 1;
1151 /* Computing MAX */
1152         i__7 = 7, i__8 = (*m << 1) + *n, i__7 = f2cmax(i__7,i__8), i__8 = (*n << 
1153                 2) + 1;
1154 /* Computing MAX */
1155         i__9 = (*m << 1) + *n, i__10 = *n * 6 + (*n << 1) * *n;
1156 /* Computing MAX */
1157         i__11 = (*m << 1) + *n, i__12 = (*n << 2) + *n * *n, i__11 = f2cmax(
1158                 i__11,i__12), i__12 = (*n << 1) + *n * *n + 6;
1159         if (! (lsvec || rsvec || errest) && *lwork < f2cmax(i__1,i__2) || ! (
1160                 lsvec || rsvec) && errest && *lwork < f2cmax(i__3,i__4) || lsvec 
1161                 && ! rsvec && *lwork < f2cmax(i__5,i__6) || rsvec && ! lsvec && *
1162                 lwork < f2cmax(i__7,i__8) || lsvec && rsvec && ! jracc && *lwork 
1163                 < f2cmax(i__9,i__10) || lsvec && rsvec && jracc && *lwork < f2cmax(
1164                 i__11,i__12)) {
1165             *info = -17;
1166         } else {
1167 /*        #:) */
1168             *info = 0;
1169         }
1170     }
1171
1172     if (*info != 0) {
1173 /*       #:( */
1174         i__1 = -(*info);
1175         xerbla_("DGEJSV", &i__1, (ftnlen)6);
1176         return 0;
1177     }
1178
1179 /*     Quick return for void matrix (Y3K safe) */
1180 /* #:) */
1181     if (*m == 0 || *n == 0) {
1182         iwork[1] = 0;
1183         iwork[2] = 0;
1184         iwork[3] = 0;
1185         work[1] = 0.;
1186         work[2] = 0.;
1187         work[3] = 0.;
1188         work[4] = 0.;
1189         work[5] = 0.;
1190         work[6] = 0.;
1191         work[7] = 0.;
1192         return 0;
1193     }
1194
1195 /*     Determine whether the matrix U should be M x N or M x M */
1196
1197     if (lsvec) {
1198         n1 = *n;
1199         if (lsame_(jobu, "F")) {
1200             n1 = *m;
1201         }
1202     }
1203
1204 /*     Set numerical parameters */
1205
1206 /* !    NOTE: Make sure DLAMCH() does not fail on the target architecture. */
1207
1208     epsln = dlamch_("Epsilon");
1209     sfmin = dlamch_("SafeMinimum");
1210     small = sfmin / epsln;
1211     big = dlamch_("O");
1212 /*     BIG   = ONE / SFMIN */
1213
1214 /*     Initialize SVA(1:N) = diag( ||A e_i||_2 )_1^N */
1215
1216 /* (!)  If necessary, scale SVA() to protect the largest norm from */
1217 /*     overflow. It is possible that this scaling pushes the smallest */
1218 /*     column norm left from the underflow threshold (extreme case). */
1219
1220     scalem = 1. / sqrt((doublereal) (*m) * (doublereal) (*n));
1221     noscal = TRUE_;
1222     goscal = TRUE_;
1223     i__1 = *n;
1224     for (p = 1; p <= i__1; ++p) {
1225         aapp = 0.;
1226         aaqq = 1.;
1227         dlassq_(m, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
1228         if (aapp > big) {
1229             *info = -9;
1230             i__2 = -(*info);
1231             xerbla_("DGEJSV", &i__2, (ftnlen)6);
1232             return 0;
1233         }
1234         aaqq = sqrt(aaqq);
1235         if (aapp < big / aaqq && noscal) {
1236             sva[p] = aapp * aaqq;
1237         } else {
1238             noscal = FALSE_;
1239             sva[p] = aapp * (aaqq * scalem);
1240             if (goscal) {
1241                 goscal = FALSE_;
1242                 i__2 = p - 1;
1243                 dscal_(&i__2, &scalem, &sva[1], &c__1);
1244             }
1245         }
1246 /* L1874: */
1247     }
1248
1249     if (noscal) {
1250         scalem = 1.;
1251     }
1252
1253     aapp = 0.;
1254     aaqq = big;
1255     i__1 = *n;
1256     for (p = 1; p <= i__1; ++p) {
1257 /* Computing MAX */
1258         d__1 = aapp, d__2 = sva[p];
1259         aapp = f2cmax(d__1,d__2);
1260         if (sva[p] != 0.) {
1261 /* Computing MIN */
1262             d__1 = aaqq, d__2 = sva[p];
1263             aaqq = f2cmin(d__1,d__2);
1264         }
1265 /* L4781: */
1266     }
1267
1268 /*     Quick return for zero M x N matrix */
1269 /* #:) */
1270     if (aapp == 0.) {
1271         if (lsvec) {
1272             dlaset_("G", m, &n1, &c_b34, &c_b35, &u[u_offset], ldu)
1273                     ;
1274         }
1275         if (rsvec) {
1276             dlaset_("G", n, n, &c_b34, &c_b35, &v[v_offset], ldv);
1277         }
1278         work[1] = 1.;
1279         work[2] = 1.;
1280         if (errest) {
1281             work[3] = 1.;
1282         }
1283         if (lsvec && rsvec) {
1284             work[4] = 1.;
1285             work[5] = 1.;
1286         }
1287         if (l2tran) {
1288             work[6] = 0.;
1289             work[7] = 0.;
1290         }
1291         iwork[1] = 0;
1292         iwork[2] = 0;
1293         iwork[3] = 0;
1294         return 0;
1295     }
1296
1297 /*     Issue warning if denormalized column norms detected. Override the */
1298 /*     high relative accuracy request. Issue licence to kill columns */
1299 /*     (set them to zero) whose norm is less than sigma_max / BIG (roughly). */
1300 /* #:( */
1301     warning = 0;
1302     if (aaqq <= sfmin) {
1303         l2rank = TRUE_;
1304         l2kill = TRUE_;
1305         warning = 1;
1306     }
1307
1308 /*     Quick return for one-column matrix */
1309 /* #:) */
1310     if (*n == 1) {
1311
1312         if (lsvec) {
1313             dlascl_("G", &c__0, &c__0, &sva[1], &scalem, m, &c__1, &a[a_dim1 
1314                     + 1], lda, &ierr);
1315             dlacpy_("A", m, &c__1, &a[a_offset], lda, &u[u_offset], ldu);
1316 /*           computing all M left singular vectors of the M x 1 matrix */
1317             if (n1 != *n) {
1318                 i__1 = *lwork - *n;
1319                 dgeqrf_(m, n, &u[u_offset], ldu, &work[1], &work[*n + 1], &
1320                         i__1, &ierr);
1321                 i__1 = *lwork - *n;
1322                 dorgqr_(m, &n1, &c__1, &u[u_offset], ldu, &work[1], &work[*n 
1323                         + 1], &i__1, &ierr);
1324                 dcopy_(m, &a[a_dim1 + 1], &c__1, &u[u_dim1 + 1], &c__1);
1325             }
1326         }
1327         if (rsvec) {
1328             v[v_dim1 + 1] = 1.;
1329         }
1330         if (sva[1] < big * scalem) {
1331             sva[1] /= scalem;
1332             scalem = 1.;
1333         }
1334         work[1] = 1. / scalem;
1335         work[2] = 1.;
1336         if (sva[1] != 0.) {
1337             iwork[1] = 1;
1338             if (sva[1] / scalem >= sfmin) {
1339                 iwork[2] = 1;
1340             } else {
1341                 iwork[2] = 0;
1342             }
1343         } else {
1344             iwork[1] = 0;
1345             iwork[2] = 0;
1346         }
1347         iwork[3] = 0;
1348         if (errest) {
1349             work[3] = 1.;
1350         }
1351         if (lsvec && rsvec) {
1352             work[4] = 1.;
1353             work[5] = 1.;
1354         }
1355         if (l2tran) {
1356             work[6] = 0.;
1357             work[7] = 0.;
1358         }
1359         return 0;
1360
1361     }
1362
1363     transp = FALSE_;
1364     l2tran = l2tran && *m == *n;
1365
1366     aatmax = -1.;
1367     aatmin = big;
1368     if (rowpiv || l2tran) {
1369
1370 /*     Compute the row norms, needed to determine row pivoting sequence */
1371 /*     (in the case of heavily row weighted A, row pivoting is strongly */
1372 /*     advised) and to collect information needed to compare the */
1373 /*     structures of A * A^t and A^t * A (in the case L2TRAN.EQ..TRUE.). */
1374
1375         if (l2tran) {
1376             i__1 = *m;
1377             for (p = 1; p <= i__1; ++p) {
1378                 xsc = 0.;
1379                 temp1 = 1.;
1380                 dlassq_(n, &a[p + a_dim1], lda, &xsc, &temp1);
1381 /*              DLASSQ gets both the ell_2 and the ell_infinity norm */
1382 /*              in one pass through the vector */
1383                 work[*m + *n + p] = xsc * scalem;
1384                 work[*n + p] = xsc * (scalem * sqrt(temp1));
1385 /* Computing MAX */
1386                 d__1 = aatmax, d__2 = work[*n + p];
1387                 aatmax = f2cmax(d__1,d__2);
1388                 if (work[*n + p] != 0.) {
1389 /* Computing MIN */
1390                     d__1 = aatmin, d__2 = work[*n + p];
1391                     aatmin = f2cmin(d__1,d__2);
1392                 }
1393 /* L1950: */
1394             }
1395         } else {
1396             i__1 = *m;
1397             for (p = 1; p <= i__1; ++p) {
1398                 work[*m + *n + p] = scalem * (d__1 = a[p + idamax_(n, &a[p + 
1399                         a_dim1], lda) * a_dim1], abs(d__1));
1400 /* Computing MAX */
1401                 d__1 = aatmax, d__2 = work[*m + *n + p];
1402                 aatmax = f2cmax(d__1,d__2);
1403 /* Computing MIN */
1404                 d__1 = aatmin, d__2 = work[*m + *n + p];
1405                 aatmin = f2cmin(d__1,d__2);
1406 /* L1904: */
1407             }
1408         }
1409
1410     }
1411
1412 /*     For square matrix A try to determine whether A^t  would be  better */
1413 /*     input for the preconditioned Jacobi SVD, with faster convergence. */
1414 /*     The decision is based on an O(N) function of the vector of column */
1415 /*     and row norms of A, based on the Shannon entropy. This should give */
1416 /*     the right choice in most cases when the difference actually matters. */
1417 /*     It may fail and pick the slower converging side. */
1418
1419     entra = 0.;
1420     entrat = 0.;
1421     if (l2tran) {
1422
1423         xsc = 0.;
1424         temp1 = 1.;
1425         dlassq_(n, &sva[1], &c__1, &xsc, &temp1);
1426         temp1 = 1. / temp1;
1427
1428         entra = 0.;
1429         i__1 = *n;
1430         for (p = 1; p <= i__1; ++p) {
1431 /* Computing 2nd power */
1432             d__1 = sva[p] / xsc;
1433             big1 = d__1 * d__1 * temp1;
1434             if (big1 != 0.) {
1435                 entra += big1 * log(big1);
1436             }
1437 /* L1113: */
1438         }
1439         entra = -entra / log((doublereal) (*n));
1440
1441 /*        Now, SVA().^2/Trace(A^t * A) is a point in the probability simplex. */
1442 /*        It is derived from the diagonal of  A^t * A.  Do the same with the */
1443 /*        diagonal of A * A^t, compute the entropy of the corresponding */
1444 /*        probability distribution. Note that A * A^t and A^t * A have the */
1445 /*        same trace. */
1446
1447         entrat = 0.;
1448         i__1 = *n + *m;
1449         for (p = *n + 1; p <= i__1; ++p) {
1450 /* Computing 2nd power */
1451             d__1 = work[p] / xsc;
1452             big1 = d__1 * d__1 * temp1;
1453             if (big1 != 0.) {
1454                 entrat += big1 * log(big1);
1455             }
1456 /* L1114: */
1457         }
1458         entrat = -entrat / log((doublereal) (*m));
1459
1460 /*        Analyze the entropies and decide A or A^t. Smaller entropy */
1461 /*        usually means better input for the algorithm. */
1462
1463         transp = entrat < entra;
1464
1465 /*        If A^t is better than A, transpose A. */
1466
1467         if (transp) {
1468 /*           In an optimal implementation, this trivial transpose */
1469 /*           should be replaced with faster transpose. */
1470             i__1 = *n - 1;
1471             for (p = 1; p <= i__1; ++p) {
1472                 i__2 = *n;
1473                 for (q = p + 1; q <= i__2; ++q) {
1474                     temp1 = a[q + p * a_dim1];
1475                     a[q + p * a_dim1] = a[p + q * a_dim1];
1476                     a[p + q * a_dim1] = temp1;
1477 /* L1116: */
1478                 }
1479 /* L1115: */
1480             }
1481             i__1 = *n;
1482             for (p = 1; p <= i__1; ++p) {
1483                 work[*m + *n + p] = sva[p];
1484                 sva[p] = work[*n + p];
1485 /* L1117: */
1486             }
1487             temp1 = aapp;
1488             aapp = aatmax;
1489             aatmax = temp1;
1490             temp1 = aaqq;
1491             aaqq = aatmin;
1492             aatmin = temp1;
1493             kill = lsvec;
1494             lsvec = rsvec;
1495             rsvec = kill;
1496             if (lsvec) {
1497                 n1 = *n;
1498             }
1499
1500             rowpiv = TRUE_;
1501         }
1502
1503     }
1504 /*     END IF L2TRAN */
1505
1506 /*     Scale the matrix so that its maximal singular value remains less */
1507 /*     than DSQRT(BIG) -- the matrix is scaled so that its maximal column */
1508 /*     has Euclidean norm equal to DSQRT(BIG/N). The only reason to keep */
1509 /*     DSQRT(BIG) instead of BIG is the fact that DGEJSV uses LAPACK and */
1510 /*     BLAS routines that, in some implementations, are not capable of */
1511 /*     working in the full interval [SFMIN,BIG] and that they may provoke */
1512 /*     overflows in the intermediate results. If the singular values spread */
1513 /*     from SFMIN to BIG, then DGESVJ will compute them. So, in that case, */
1514 /*     one should use DGESVJ instead of DGEJSV. */
1515
1516     big1 = sqrt(big);
1517     temp1 = sqrt(big / (doublereal) (*n));
1518
1519     dlascl_("G", &c__0, &c__0, &aapp, &temp1, n, &c__1, &sva[1], n, &ierr);
1520     if (aaqq > aapp * sfmin) {
1521         aaqq = aaqq / aapp * temp1;
1522     } else {
1523         aaqq = aaqq * temp1 / aapp;
1524     }
1525     temp1 *= scalem;
1526     dlascl_("G", &c__0, &c__0, &aapp, &temp1, m, n, &a[a_offset], lda, &ierr);
1527
1528 /*     To undo scaling at the end of this procedure, multiply the */
1529 /*     computed singular values with USCAL2 / USCAL1. */
1530
1531     uscal1 = temp1;
1532     uscal2 = aapp;
1533
1534     if (l2kill) {
1535 /*        L2KILL enforces computation of nonzero singular values in */
1536 /*        the restricted range of condition number of the initial A, */
1537 /*        sigma_max(A) / sigma_min(A) approx. DSQRT(BIG)/DSQRT(SFMIN). */
1538         xsc = sqrt(sfmin);
1539     } else {
1540         xsc = small;
1541
1542 /*        Now, if the condition number of A is too big, */
1543 /*        sigma_max(A) / sigma_min(A) .GT. DSQRT(BIG/N) * EPSLN / SFMIN, */
1544 /*        as a precaution measure, the full SVD is computed using DGESVJ */
1545 /*        with accumulated Jacobi rotations. This provides numerically */
1546 /*        more robust computation, at the cost of slightly increased run */
1547 /*        time. Depending on the concrete implementation of BLAS and LAPACK */
1548 /*        (i.e. how they behave in presence of extreme ill-conditioning) the */
1549 /*        implementor may decide to remove this switch. */
1550         if (aaqq < sqrt(sfmin) && lsvec && rsvec) {
1551             jracc = TRUE_;
1552         }
1553
1554     }
1555     if (aaqq < xsc) {
1556         i__1 = *n;
1557         for (p = 1; p <= i__1; ++p) {
1558             if (sva[p] < xsc) {
1559                 dlaset_("A", m, &c__1, &c_b34, &c_b34, &a[p * a_dim1 + 1], 
1560                         lda);
1561                 sva[p] = 0.;
1562             }
1563 /* L700: */
1564         }
1565     }
1566
1567 /*     Preconditioning using QR factorization with pivoting */
1568
1569     if (rowpiv) {
1570 /*        Optional row permutation (Bjoerck row pivoting): */
1571 /*        A result by Cox and Higham shows that the Bjoerck's */
1572 /*        row pivoting combined with standard column pivoting */
1573 /*        has similar effect as Powell-Reid complete pivoting. */
1574 /*        The ell-infinity norms of A are made nonincreasing. */
1575         i__1 = *m - 1;
1576         for (p = 1; p <= i__1; ++p) {
1577             i__2 = *m - p + 1;
1578             q = idamax_(&i__2, &work[*m + *n + p], &c__1) + p - 1;
1579             iwork[(*n << 1) + p] = q;
1580             if (p != q) {
1581                 temp1 = work[*m + *n + p];
1582                 work[*m + *n + p] = work[*m + *n + q];
1583                 work[*m + *n + q] = temp1;
1584             }
1585 /* L1952: */
1586         }
1587         i__1 = *m - 1;
1588         dlaswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[(*n << 1) + 1], &
1589                 c__1);
1590     }
1591
1592 /*     End of the preparation phase (scaling, optional sorting and */
1593 /*     transposing, optional flushing of small columns). */
1594
1595 /*     Preconditioning */
1596
1597 /*     If the full SVD is needed, the right singular vectors are computed */
1598 /*     from a matrix equation, and for that we need theoretical analysis */
1599 /*     of the Businger-Golub pivoting. So we use DGEQP3 as the first RR QRF. */
1600 /*     In all other cases the first RR QRF can be chosen by other criteria */
1601 /*     (eg speed by replacing global with restricted window pivoting, such */
1602 /*     as in SGEQPX from TOMS # 782). Good results will be obtained using */
1603 /*     SGEQPX with properly (!) chosen numerical parameters. */
1604 /*     Any improvement of DGEQP3 improves overal performance of DGEJSV. */
1605
1606 /*     A * P1 = Q1 * [ R1^t 0]^t: */
1607     i__1 = *n;
1608     for (p = 1; p <= i__1; ++p) {
1609         iwork[p] = 0;
1610 /* L1963: */
1611     }
1612     i__1 = *lwork - *n;
1613     dgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &work[1], &work[*n + 1], &
1614             i__1, &ierr);
1615
1616 /*     The upper triangular matrix R1 from the first QRF is inspected for */
1617 /*     rank deficiency and possibilities for deflation, or possible */
1618 /*     ill-conditioning. Depending on the user specified flag L2RANK, */
1619 /*     the procedure explores possibilities to reduce the numerical */
1620 /*     rank by inspecting the computed upper triangular factor. If */
1621 /*     L2RANK or L2ABER are up, then DGEJSV will compute the SVD of */
1622 /*     A + dA, where ||dA|| <= f(M,N)*EPSLN. */
1623
1624     nr = 1;
1625     if (l2aber) {
1626 /*        Standard absolute error bound suffices. All sigma_i with */
1627 /*        sigma_i < N*EPSLN*||A|| are flushed to zero. This is an */
1628 /*        aggressive enforcement of lower numerical rank by introducing a */
1629 /*        backward error of the order of N*EPSLN*||A||. */
1630         temp1 = sqrt((doublereal) (*n)) * epsln;
1631         i__1 = *n;
1632         for (p = 2; p <= i__1; ++p) {
1633             if ((d__2 = a[p + p * a_dim1], abs(d__2)) >= temp1 * (d__1 = a[
1634                     a_dim1 + 1], abs(d__1))) {
1635                 ++nr;
1636             } else {
1637                 goto L3002;
1638             }
1639 /* L3001: */
1640         }
1641 L3002:
1642         ;
1643     } else if (l2rank) {
1644 /*        Sudden drop on the diagonal of R1 is used as the criterion for */
1645 /*        close-to-rank-deficient. */
1646         temp1 = sqrt(sfmin);
1647         i__1 = *n;
1648         for (p = 2; p <= i__1; ++p) {
1649             if ((d__2 = a[p + p * a_dim1], abs(d__2)) < epsln * (d__1 = a[p - 
1650                     1 + (p - 1) * a_dim1], abs(d__1)) || (d__3 = a[p + p * 
1651                     a_dim1], abs(d__3)) < small || l2kill && (d__4 = a[p + p *
1652                      a_dim1], abs(d__4)) < temp1) {
1653                 goto L3402;
1654             }
1655             ++nr;
1656 /* L3401: */
1657         }
1658 L3402:
1659
1660         ;
1661     } else {
1662 /*        The goal is high relative accuracy. However, if the matrix */
1663 /*        has high scaled condition number the relative accuracy is in */
1664 /*        general not feasible. Later on, a condition number estimator */
1665 /*        will be deployed to estimate the scaled condition number. */
1666 /*        Here we just remove the underflowed part of the triangular */
1667 /*        factor. This prevents the situation in which the code is */
1668 /*        working hard to get the accuracy not warranted by the data. */
1669         temp1 = sqrt(sfmin);
1670         i__1 = *n;
1671         for (p = 2; p <= i__1; ++p) {
1672             if ((d__1 = a[p + p * a_dim1], abs(d__1)) < small || l2kill && (
1673                     d__2 = a[p + p * a_dim1], abs(d__2)) < temp1) {
1674                 goto L3302;
1675             }
1676             ++nr;
1677 /* L3301: */
1678         }
1679 L3302:
1680
1681         ;
1682     }
1683
1684     almort = FALSE_;
1685     if (nr == *n) {
1686         maxprj = 1.;
1687         i__1 = *n;
1688         for (p = 2; p <= i__1; ++p) {
1689             temp1 = (d__1 = a[p + p * a_dim1], abs(d__1)) / sva[iwork[p]];
1690             maxprj = f2cmin(maxprj,temp1);
1691 /* L3051: */
1692         }
1693 /* Computing 2nd power */
1694         d__1 = maxprj;
1695         if (d__1 * d__1 >= 1. - (doublereal) (*n) * epsln) {
1696             almort = TRUE_;
1697         }
1698     }
1699
1700
1701     sconda = -1.;
1702     condr1 = -1.;
1703     condr2 = -1.;
1704
1705     if (errest) {
1706         if (*n == nr) {
1707             if (rsvec) {
1708                 dlacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
1709                 i__1 = *n;
1710                 for (p = 1; p <= i__1; ++p) {
1711                     temp1 = sva[iwork[p]];
1712                     d__1 = 1. / temp1;
1713                     dscal_(&p, &d__1, &v[p * v_dim1 + 1], &c__1);
1714 /* L3053: */
1715                 }
1716                 dpocon_("U", n, &v[v_offset], ldv, &c_b35, &temp1, &work[*n + 
1717                         1], &iwork[(*n << 1) + *m + 1], &ierr);
1718             } else if (lsvec) {
1719                 dlacpy_("U", n, n, &a[a_offset], lda, &u[u_offset], ldu);
1720                 i__1 = *n;
1721                 for (p = 1; p <= i__1; ++p) {
1722                     temp1 = sva[iwork[p]];
1723                     d__1 = 1. / temp1;
1724                     dscal_(&p, &d__1, &u[p * u_dim1 + 1], &c__1);
1725 /* L3054: */
1726                 }
1727                 dpocon_("U", n, &u[u_offset], ldu, &c_b35, &temp1, &work[*n + 
1728                         1], &iwork[(*n << 1) + *m + 1], &ierr);
1729             } else {
1730                 dlacpy_("U", n, n, &a[a_offset], lda, &work[*n + 1], n);
1731                 i__1 = *n;
1732                 for (p = 1; p <= i__1; ++p) {
1733                     temp1 = sva[iwork[p]];
1734                     d__1 = 1. / temp1;
1735                     dscal_(&p, &d__1, &work[*n + (p - 1) * *n + 1], &c__1);
1736 /* L3052: */
1737                 }
1738                 dpocon_("U", n, &work[*n + 1], n, &c_b35, &temp1, &work[*n + *
1739                         n * *n + 1], &iwork[(*n << 1) + *m + 1], &ierr);
1740             }
1741             sconda = 1. / sqrt(temp1);
1742 /*           SCONDA is an estimate of DSQRT(||(R^t * R)^(-1)||_1). */
1743 /*           N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
1744         } else {
1745             sconda = -1.;
1746         }
1747     }
1748
1749     l2pert = l2pert && (d__1 = a[a_dim1 + 1] / a[nr + nr * a_dim1], abs(d__1))
1750              > sqrt(big1);
1751 /*     If there is no violent scaling, artificial perturbation is not needed. */
1752
1753 /*     Phase 3: */
1754
1755     if (! (rsvec || lsvec)) {
1756
1757 /*         Singular Values only */
1758
1759 /* Computing MIN */
1760         i__2 = *n - 1;
1761         i__1 = f2cmin(i__2,nr);
1762         for (p = 1; p <= i__1; ++p) {
1763             i__2 = *n - p;
1764             dcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p * 
1765                     a_dim1], &c__1);
1766 /* L1946: */
1767         }
1768
1769 /*        The following two DO-loops introduce small relative perturbation */
1770 /*        into the strict upper triangle of the lower triangular matrix. */
1771 /*        Small entries below the main diagonal are also changed. */
1772 /*        This modification is useful if the computing environment does not */
1773 /*        provide/allow FLUSH TO ZERO underflow, for it prevents many */
1774 /*        annoying denormalized numbers in case of strongly scaled matrices. */
1775 /*        The perturbation is structured so that it does not introduce any */
1776 /*        new perturbation of the singular values, and it does not destroy */
1777 /*        the job done by the preconditioner. */
1778 /*        The licence for this perturbation is in the variable L2PERT, which */
1779 /*        should be .FALSE. if FLUSH TO ZERO underflow is active. */
1780
1781         if (! almort) {
1782
1783             if (l2pert) {
1784 /*              XSC = DSQRT(SMALL) */
1785                 xsc = epsln / (doublereal) (*n);
1786                 i__1 = nr;
1787                 for (q = 1; q <= i__1; ++q) {
1788                     temp1 = xsc * (d__1 = a[q + q * a_dim1], abs(d__1));
1789                     i__2 = *n;
1790                     for (p = 1; p <= i__2; ++p) {
1791                         if (p > q && (d__1 = a[p + q * a_dim1], abs(d__1)) <= 
1792                                 temp1 || p < q) {
1793                             a[p + q * a_dim1] = d_sign(&temp1, &a[p + q * 
1794                                     a_dim1]);
1795                         }
1796 /* L4949: */
1797                     }
1798 /* L4947: */
1799                 }
1800             } else {
1801                 i__1 = nr - 1;
1802                 i__2 = nr - 1;
1803                 dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &a[(a_dim1 << 1) + 
1804                         1], lda);
1805             }
1806
1807
1808             i__1 = *lwork - *n;
1809             dgeqrf_(n, &nr, &a[a_offset], lda, &work[1], &work[*n + 1], &i__1,
1810                      &ierr);
1811
1812             i__1 = nr - 1;
1813             for (p = 1; p <= i__1; ++p) {
1814                 i__2 = nr - p;
1815                 dcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p * 
1816                         a_dim1], &c__1);
1817 /* L1948: */
1818             }
1819
1820         }
1821
1822 /*           Row-cyclic Jacobi SVD algorithm with column pivoting */
1823
1824 /*           to drown denormals */
1825         if (l2pert) {
1826 /*              XSC = DSQRT(SMALL) */
1827             xsc = epsln / (doublereal) (*n);
1828             i__1 = nr;
1829             for (q = 1; q <= i__1; ++q) {
1830                 temp1 = xsc * (d__1 = a[q + q * a_dim1], abs(d__1));
1831                 i__2 = nr;
1832                 for (p = 1; p <= i__2; ++p) {
1833                     if (p > q && (d__1 = a[p + q * a_dim1], abs(d__1)) <= 
1834                             temp1 || p < q) {
1835                         a[p + q * a_dim1] = d_sign(&temp1, &a[p + q * a_dim1])
1836                                 ;
1837                     }
1838 /* L1949: */
1839                 }
1840 /* L1947: */
1841             }
1842         } else {
1843             i__1 = nr - 1;
1844             i__2 = nr - 1;
1845             dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &a[(a_dim1 << 1) + 1], 
1846                     lda);
1847         }
1848
1849 /*           triangular matrix (plus perturbation which is ignored in */
1850 /*           the part which destroys triangular form (confusing?!)) */
1851
1852         dgesvj_("L", "NoU", "NoV", &nr, &nr, &a[a_offset], lda, &sva[1], n, &
1853                 v[v_offset], ldv, &work[1], lwork, info);
1854
1855         scalem = work[1];
1856         numrank = i_dnnt(&work[2]);
1857
1858
1859     } else if (rsvec && ! lsvec) {
1860
1861 /*        -> Singular Values and Right Singular Vectors <- */
1862
1863         if (almort) {
1864
1865             i__1 = nr;
1866             for (p = 1; p <= i__1; ++p) {
1867                 i__2 = *n - p + 1;
1868                 dcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
1869                         c__1);
1870 /* L1998: */
1871             }
1872             i__1 = nr - 1;
1873             i__2 = nr - 1;
1874             dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) + 
1875                     1], ldv);
1876
1877             dgesvj_("L", "U", "N", n, &nr, &v[v_offset], ldv, &sva[1], &nr, &
1878                     a[a_offset], lda, &work[1], lwork, info);
1879             scalem = work[1];
1880             numrank = i_dnnt(&work[2]);
1881         } else {
1882
1883 /*        accumulated product of Jacobi rotations, three are perfect ) */
1884
1885             i__1 = nr - 1;
1886             i__2 = nr - 1;
1887             dlaset_("Lower", &i__1, &i__2, &c_b34, &c_b34, &a[a_dim1 + 2], 
1888                     lda);
1889             i__1 = *lwork - *n;
1890             dgelqf_(&nr, n, &a[a_offset], lda, &work[1], &work[*n + 1], &i__1,
1891                      &ierr);
1892             dlacpy_("Lower", &nr, &nr, &a[a_offset], lda, &v[v_offset], ldv);
1893             i__1 = nr - 1;
1894             i__2 = nr - 1;
1895             dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) + 
1896                     1], ldv);
1897             i__1 = *lwork - (*n << 1);
1898             dgeqrf_(&nr, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*n << 
1899                     1) + 1], &i__1, &ierr);
1900             i__1 = nr;
1901             for (p = 1; p <= i__1; ++p) {
1902                 i__2 = nr - p + 1;
1903                 dcopy_(&i__2, &v[p + p * v_dim1], ldv, &v[p + p * v_dim1], &
1904                         c__1);
1905 /* L8998: */
1906             }
1907             i__1 = nr - 1;
1908             i__2 = nr - 1;
1909             dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) + 
1910                     1], ldv);
1911
1912             dgesvj_("Lower", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[1], &
1913                     nr, &u[u_offset], ldu, &work[*n + 1], lwork, info);
1914             scalem = work[*n + 1];
1915             numrank = i_dnnt(&work[*n + 2]);
1916             if (nr < *n) {
1917                 i__1 = *n - nr;
1918                 dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + v_dim1], 
1919                         ldv);
1920                 i__1 = *n - nr;
1921                 dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) * v_dim1 
1922                         + 1], ldv);
1923                 i__1 = *n - nr;
1924                 i__2 = *n - nr;
1925                 dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 + (nr + 
1926                         1) * v_dim1], ldv);
1927             }
1928
1929             i__1 = *lwork - *n;
1930             dormlq_("Left", "Transpose", n, n, &nr, &a[a_offset], lda, &work[
1931                     1], &v[v_offset], ldv, &work[*n + 1], &i__1, &ierr);
1932
1933         }
1934
1935         i__1 = *n;
1936         for (p = 1; p <= i__1; ++p) {
1937             dcopy_(n, &v[p + v_dim1], ldv, &a[iwork[p] + a_dim1], lda);
1938 /* L8991: */
1939         }
1940         dlacpy_("All", n, n, &a[a_offset], lda, &v[v_offset], ldv);
1941
1942         if (transp) {
1943             dlacpy_("All", n, n, &v[v_offset], ldv, &u[u_offset], ldu);
1944         }
1945
1946     } else if (lsvec && ! rsvec) {
1947
1948
1949 /*        Jacobi rotations in the Jacobi iterations. */
1950         i__1 = nr;
1951         for (p = 1; p <= i__1; ++p) {
1952             i__2 = *n - p + 1;
1953             dcopy_(&i__2, &a[p + p * a_dim1], lda, &u[p + p * u_dim1], &c__1);
1954 /* L1965: */
1955         }
1956         i__1 = nr - 1;
1957         i__2 = nr - 1;
1958         dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 1], 
1959                 ldu);
1960
1961         i__1 = *lwork - (*n << 1);
1962         dgeqrf_(n, &nr, &u[u_offset], ldu, &work[*n + 1], &work[(*n << 1) + 1]
1963                 , &i__1, &ierr);
1964
1965         i__1 = nr - 1;
1966         for (p = 1; p <= i__1; ++p) {
1967             i__2 = nr - p;
1968             dcopy_(&i__2, &u[p + (p + 1) * u_dim1], ldu, &u[p + 1 + p * 
1969                     u_dim1], &c__1);
1970 /* L1967: */
1971         }
1972         i__1 = nr - 1;
1973         i__2 = nr - 1;
1974         dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 1], 
1975                 ldu);
1976
1977         i__1 = *lwork - *n;
1978         dgesvj_("Lower", "U", "N", &nr, &nr, &u[u_offset], ldu, &sva[1], &nr, 
1979                 &a[a_offset], lda, &work[*n + 1], &i__1, info);
1980         scalem = work[*n + 1];
1981         numrank = i_dnnt(&work[*n + 2]);
1982
1983         if (nr < *m) {
1984             i__1 = *m - nr;
1985             dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 + u_dim1], ldu);
1986             if (nr < n1) {
1987                 i__1 = n1 - nr;
1988                 dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) * u_dim1 
1989                         + 1], ldu);
1990                 i__1 = *m - nr;
1991                 i__2 = n1 - nr;
1992                 dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 + (nr + 
1993                         1) * u_dim1], ldu);
1994             }
1995         }
1996
1997         i__1 = *lwork - *n;
1998         dormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[1], &u[
1999                 u_offset], ldu, &work[*n + 1], &i__1, &ierr);
2000
2001         if (rowpiv) {
2002             i__1 = *m - 1;
2003             dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n << 1) + 
2004                     1], &c_n1);
2005         }
2006
2007         i__1 = n1;
2008         for (p = 1; p <= i__1; ++p) {
2009             xsc = 1. / dnrm2_(m, &u[p * u_dim1 + 1], &c__1);
2010             dscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
2011 /* L1974: */
2012         }
2013
2014         if (transp) {
2015             dlacpy_("All", n, n, &u[u_offset], ldu, &v[v_offset], ldv);
2016         }
2017
2018     } else {
2019
2020
2021         if (! jracc) {
2022
2023             if (! almort) {
2024
2025 /*           Second Preconditioning Step (QRF [with pivoting]) */
2026 /*           Note that the composition of TRANSPOSE, QRF and TRANSPOSE is */
2027 /*           equivalent to an LQF CALL. Since in many libraries the QRF */
2028 /*           seems to be better optimized than the LQF, we do explicit */
2029 /*           transpose and use the QRF. This is subject to changes in an */
2030 /*           optimized implementation of DGEJSV. */
2031
2032                 i__1 = nr;
2033                 for (p = 1; p <= i__1; ++p) {
2034                     i__2 = *n - p + 1;
2035                     dcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1],
2036                              &c__1);
2037 /* L1968: */
2038                 }
2039
2040 /*           denormals in the second QR factorization, where they are */
2041 /*           as good as zeros. This is done to avoid painfully slow */
2042 /*           computation with denormals. The relative size of the perturbation */
2043 /*           is a parameter that can be changed by the implementer. */
2044 /*           This perturbation device will be obsolete on machines with */
2045 /*           properly implemented arithmetic. */
2046 /*           To switch it off, set L2PERT=.FALSE. To remove it from  the */
2047 /*           code, remove the action under L2PERT=.TRUE., leave the ELSE part. */
2048 /*           The following two loops should be blocked and fused with the */
2049 /*           transposed copy above. */
2050
2051                 if (l2pert) {
2052                     xsc = sqrt(small);
2053                     i__1 = nr;
2054                     for (q = 1; q <= i__1; ++q) {
2055                         temp1 = xsc * (d__1 = v[q + q * v_dim1], abs(d__1));
2056                         i__2 = *n;
2057                         for (p = 1; p <= i__2; ++p) {
2058                             if (p > q && (d__1 = v[p + q * v_dim1], abs(d__1))
2059                                      <= temp1 || p < q) {
2060                                 v[p + q * v_dim1] = d_sign(&temp1, &v[p + q * 
2061                                         v_dim1]);
2062                             }
2063                             if (p < q) {
2064                                 v[p + q * v_dim1] = -v[p + q * v_dim1];
2065                             }
2066 /* L2968: */
2067                         }
2068 /* L2969: */
2069                     }
2070                 } else {
2071                     i__1 = nr - 1;
2072                     i__2 = nr - 1;
2073                     dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 
2074                             1) + 1], ldv);
2075                 }
2076
2077 /*           Estimate the row scaled condition number of R1 */
2078 /*           (If R1 is rectangular, N > NR, then the condition number */
2079 /*           of the leading NR x NR submatrix is estimated.) */
2080
2081                 dlacpy_("L", &nr, &nr, &v[v_offset], ldv, &work[(*n << 1) + 1]
2082                         , &nr);
2083                 i__1 = nr;
2084                 for (p = 1; p <= i__1; ++p) {
2085                     i__2 = nr - p + 1;
2086                     temp1 = dnrm2_(&i__2, &work[(*n << 1) + (p - 1) * nr + p],
2087                              &c__1);
2088                     i__2 = nr - p + 1;
2089                     d__1 = 1. / temp1;
2090                     dscal_(&i__2, &d__1, &work[(*n << 1) + (p - 1) * nr + p], 
2091                             &c__1);
2092 /* L3950: */
2093                 }
2094                 dpocon_("Lower", &nr, &work[(*n << 1) + 1], &nr, &c_b35, &
2095                         temp1, &work[(*n << 1) + nr * nr + 1], &iwork[*m + (*
2096                         n << 1) + 1], &ierr);
2097                 condr1 = 1. / sqrt(temp1);
2098 /*           R1 is OK for inverse <=> CONDR1 .LT. DBLE(N) */
2099 /*           more conservative    <=> CONDR1 .LT. DSQRT(DBLE(N)) */
2100
2101                 cond_ok__ = sqrt((doublereal) nr);
2102 /* [TP]       COND_OK is a tuning parameter. */
2103                 if (condr1 < cond_ok__) {
2104 /*              implementation, this QRF should be implemented as the QRF */
2105 /*              of a lower triangular matrix. */
2106 /*              R1^t = Q2 * R2 */
2107                     i__1 = *lwork - (*n << 1);
2108                     dgeqrf_(n, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*
2109                             n << 1) + 1], &i__1, &ierr);
2110
2111                     if (l2pert) {
2112                         xsc = sqrt(small) / epsln;
2113                         i__1 = nr;
2114                         for (p = 2; p <= i__1; ++p) {
2115                             i__2 = p - 1;
2116                             for (q = 1; q <= i__2; ++q) {
2117 /* Computing MIN */
2118                                 d__3 = (d__1 = v[p + p * v_dim1], abs(d__1)), 
2119                                         d__4 = (d__2 = v[q + q * v_dim1], abs(
2120                                         d__2));
2121                                 temp1 = xsc * f2cmin(d__3,d__4);
2122                                 if ((d__1 = v[q + p * v_dim1], abs(d__1)) <= 
2123                                         temp1) {
2124                                     v[q + p * v_dim1] = d_sign(&temp1, &v[q + 
2125                                             p * v_dim1]);
2126                                 }
2127 /* L3958: */
2128                             }
2129 /* L3959: */
2130                         }
2131                     }
2132
2133                     if (nr != *n) {
2134                         dlacpy_("A", n, &nr, &v[v_offset], ldv, &work[(*n << 
2135                                 1) + 1], n);
2136                     }
2137
2138                     i__1 = nr - 1;
2139                     for (p = 1; p <= i__1; ++p) {
2140                         i__2 = nr - p;
2141                         dcopy_(&i__2, &v[p + (p + 1) * v_dim1], ldv, &v[p + 1 
2142                                 + p * v_dim1], &c__1);
2143 /* L1969: */
2144                     }
2145
2146                     condr2 = condr1;
2147
2148                 } else {
2149
2150 /*              Note that windowed pivoting would be equally good */
2151 /*              numerically, and more run-time efficient. So, in */
2152 /*              an optimal implementation, the next call to DGEQP3 */
2153 /*              should be replaced with eg. CALL SGEQPX (ACM TOMS #782) */
2154 /*              with properly (carefully) chosen parameters. */
2155
2156 /*              R1^t * P2 = Q2 * R2 */
2157                     i__1 = nr;
2158                     for (p = 1; p <= i__1; ++p) {
2159                         iwork[*n + p] = 0;
2160 /* L3003: */
2161                     }
2162                     i__1 = *lwork - (*n << 1);
2163                     dgeqp3_(n, &nr, &v[v_offset], ldv, &iwork[*n + 1], &work[*
2164                             n + 1], &work[(*n << 1) + 1], &i__1, &ierr);
2165 /* *               CALL DGEQRF( N, NR, V, LDV, WORK(N+1), WORK(2*N+1), */
2166 /* *     $              LWORK-2*N, IERR ) */
2167                     if (l2pert) {
2168                         xsc = sqrt(small);
2169                         i__1 = nr;
2170                         for (p = 2; p <= i__1; ++p) {
2171                             i__2 = p - 1;
2172                             for (q = 1; q <= i__2; ++q) {
2173 /* Computing MIN */
2174                                 d__3 = (d__1 = v[p + p * v_dim1], abs(d__1)), 
2175                                         d__4 = (d__2 = v[q + q * v_dim1], abs(
2176                                         d__2));
2177                                 temp1 = xsc * f2cmin(d__3,d__4);
2178                                 if ((d__1 = v[q + p * v_dim1], abs(d__1)) <= 
2179                                         temp1) {
2180                                     v[q + p * v_dim1] = d_sign(&temp1, &v[q + 
2181                                             p * v_dim1]);
2182                                 }
2183 /* L3968: */
2184                             }
2185 /* L3969: */
2186                         }
2187                     }
2188
2189                     dlacpy_("A", n, &nr, &v[v_offset], ldv, &work[(*n << 1) + 
2190                             1], n);
2191
2192                     if (l2pert) {
2193                         xsc = sqrt(small);
2194                         i__1 = nr;
2195                         for (p = 2; p <= i__1; ++p) {
2196                             i__2 = p - 1;
2197                             for (q = 1; q <= i__2; ++q) {
2198 /* Computing MIN */
2199                                 d__3 = (d__1 = v[p + p * v_dim1], abs(d__1)), 
2200                                         d__4 = (d__2 = v[q + q * v_dim1], abs(
2201                                         d__2));
2202                                 temp1 = xsc * f2cmin(d__3,d__4);
2203                                 v[p + q * v_dim1] = -d_sign(&temp1, &v[q + p *
2204                                          v_dim1]);
2205 /* L8971: */
2206                             }
2207 /* L8970: */
2208                         }
2209                     } else {
2210                         i__1 = nr - 1;
2211                         i__2 = nr - 1;
2212                         dlaset_("L", &i__1, &i__2, &c_b34, &c_b34, &v[v_dim1 
2213                                 + 2], ldv);
2214                     }
2215 /*              Now, compute R2 = L3 * Q3, the LQ factorization. */
2216                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
2217                     dgelqf_(&nr, &nr, &v[v_offset], ldv, &work[(*n << 1) + *n 
2218                             * nr + 1], &work[(*n << 1) + *n * nr + nr + 1], &
2219                             i__1, &ierr);
2220                     dlacpy_("L", &nr, &nr, &v[v_offset], ldv, &work[(*n << 1) 
2221                             + *n * nr + nr + 1], &nr);
2222                     i__1 = nr;
2223                     for (p = 1; p <= i__1; ++p) {
2224                         temp1 = dnrm2_(&p, &work[(*n << 1) + *n * nr + nr + p]
2225                                 , &nr);
2226                         d__1 = 1. / temp1;
2227                         dscal_(&p, &d__1, &work[(*n << 1) + *n * nr + nr + p],
2228                                  &nr);
2229 /* L4950: */
2230                     }
2231                     dpocon_("L", &nr, &work[(*n << 1) + *n * nr + nr + 1], &
2232                             nr, &c_b35, &temp1, &work[(*n << 1) + *n * nr + 
2233                             nr + nr * nr + 1], &iwork[*m + (*n << 1) + 1], &
2234                             ierr);
2235                     condr2 = 1. / sqrt(temp1);
2236
2237                     if (condr2 >= cond_ok__) {
2238 /*                 (this overwrites the copy of R2, as it will not be */
2239 /*                 needed in this branch, but it does not overwritte the */
2240 /*                 Huseholder vectors of Q2.). */
2241                         dlacpy_("U", &nr, &nr, &v[v_offset], ldv, &work[(*n <<
2242                                  1) + 1], n);
2243 /*                 WORK(2*N+N*NR+1:2*N+N*NR+N) */
2244                     }
2245
2246                 }
2247
2248                 if (l2pert) {
2249                     xsc = sqrt(small);
2250                     i__1 = nr;
2251                     for (q = 2; q <= i__1; ++q) {
2252                         temp1 = xsc * v[q + q * v_dim1];
2253                         i__2 = q - 1;
2254                         for (p = 1; p <= i__2; ++p) {
2255 /*                    V(p,q) = - DSIGN( TEMP1, V(q,p) ) */
2256                             v[p + q * v_dim1] = -d_sign(&temp1, &v[p + q * 
2257                                     v_dim1]);
2258 /* L4969: */
2259                         }
2260 /* L4968: */
2261                     }
2262                 } else {
2263                     i__1 = nr - 1;
2264                     i__2 = nr - 1;
2265                     dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 
2266                             1) + 1], ldv);
2267                 }
2268
2269 /*        Second preconditioning finished; continue with Jacobi SVD */
2270 /*        The input matrix is lower trinagular. */
2271
2272 /*        Recover the right singular vectors as solution of a well */
2273 /*        conditioned triangular matrix equation. */
2274
2275                 if (condr1 < cond_ok__) {
2276
2277                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
2278                     dgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
2279                             1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
2280                              nr + nr + 1], &i__1, info);
2281                     scalem = work[(*n << 1) + *n * nr + nr + 1];
2282                     numrank = i_dnnt(&work[(*n << 1) + *n * nr + nr + 2]);
2283                     i__1 = nr;
2284                     for (p = 1; p <= i__1; ++p) {
2285                         dcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1 
2286                                 + 1], &c__1);
2287                         dscal_(&nr, &sva[p], &v[p * v_dim1 + 1], &c__1);
2288 /* L3970: */
2289                     }
2290
2291                     if (nr == *n) {
2292 /* :))             .. best case, R1 is inverted. The solution of this matrix */
2293 /*                 equation is Q2*V2 = the product of the Jacobi rotations */
2294 /*                 used in DGESVJ, premultiplied with the orthogonal matrix */
2295 /*                 from the second QR factorization. */
2296                         dtrsm_("L", "U", "N", "N", &nr, &nr, &c_b35, &a[
2297                                 a_offset], lda, &v[v_offset], ldv);
2298                     } else {
2299 /*                 is inverted to get the product of the Jacobi rotations */
2300 /*                 used in DGESVJ. The Q-factor from the second QR */
2301 /*                 factorization is then built in explicitly. */
2302                         dtrsm_("L", "U", "T", "N", &nr, &nr, &c_b35, &work[(*
2303                                 n << 1) + 1], n, &v[v_offset], ldv);
2304                         if (nr < *n) {
2305                             i__1 = *n - nr;
2306                             dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 
2307                                     1 + v_dim1], ldv);
2308                             i__1 = *n - nr;
2309                             dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 
2310                                     1) * v_dim1 + 1], ldv);
2311                             i__1 = *n - nr;
2312                             i__2 = *n - nr;
2313                             dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr 
2314                                     + 1 + (nr + 1) * v_dim1], ldv);
2315                         }
2316                         i__1 = *lwork - (*n << 1) - *n * nr - nr;
2317                         dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, 
2318                                 &work[*n + 1], &v[v_offset], ldv, &work[(*n <<
2319                                  1) + *n * nr + nr + 1], &i__1, &ierr);
2320                     }
2321
2322                 } else if (condr2 < cond_ok__) {
2323
2324 /* :)           .. the input matrix A is very likely a relative of */
2325 /*              the Kahan matrix :) */
2326 /*              The matrix R2 is inverted. The solution of the matrix equation */
2327 /*              is Q3^T*V3 = the product of the Jacobi rotations (appplied to */
2328 /*              the lower triangular L3 from the LQ factorization of */
2329 /*              R2=L3*Q3), pre-multiplied with the transposed Q3. */
2330                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
2331                     dgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
2332                             1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
2333                              nr + nr + 1], &i__1, info);
2334                     scalem = work[(*n << 1) + *n * nr + nr + 1];
2335                     numrank = i_dnnt(&work[(*n << 1) + *n * nr + nr + 2]);
2336                     i__1 = nr;
2337                     for (p = 1; p <= i__1; ++p) {
2338                         dcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1 
2339                                 + 1], &c__1);
2340                         dscal_(&nr, &sva[p], &u[p * u_dim1 + 1], &c__1);
2341 /* L3870: */
2342                     }
2343                     dtrsm_("L", "U", "N", "N", &nr, &nr, &c_b35, &work[(*n << 
2344                             1) + 1], n, &u[u_offset], ldu);
2345                     i__1 = nr;
2346                     for (q = 1; q <= i__1; ++q) {
2347                         i__2 = nr;
2348                         for (p = 1; p <= i__2; ++p) {
2349                             work[(*n << 1) + *n * nr + nr + iwork[*n + p]] = 
2350                                     u[p + q * u_dim1];
2351 /* L872: */
2352                         }
2353                         i__2 = nr;
2354                         for (p = 1; p <= i__2; ++p) {
2355                             u[p + q * u_dim1] = work[(*n << 1) + *n * nr + nr 
2356                                     + p];
2357 /* L874: */
2358                         }
2359 /* L873: */
2360                     }
2361                     if (nr < *n) {
2362                         i__1 = *n - nr;
2363                         dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + 
2364                                 v_dim1], ldv);
2365                         i__1 = *n - nr;
2366                         dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) *
2367                                  v_dim1 + 1], ldv);
2368                         i__1 = *n - nr;
2369                         i__2 = *n - nr;
2370                         dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 
2371                                 + (nr + 1) * v_dim1], ldv);
2372                     }
2373                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
2374                     dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &
2375                             work[*n + 1], &v[v_offset], ldv, &work[(*n << 1) 
2376                             + *n * nr + nr + 1], &i__1, &ierr);
2377                 } else {
2378 /*              Last line of defense. */
2379 /* #:(          This is a rather pathological case: no scaled condition */
2380 /*              improvement after two pivoted QR factorizations. Other */
2381 /*              possibility is that the rank revealing QR factorization */
2382 /*              or the condition estimator has failed, or the COND_OK */
2383 /*              is set very close to ONE (which is unnecessary). Normally, */
2384 /*              this branch should never be executed, but in rare cases of */
2385 /*              failure of the RRQR or condition estimator, the last line of */
2386 /*              defense ensures that DGEJSV completes the task. */
2387 /*              Compute the full SVD of L3 using DGESVJ with explicit */
2388 /*              accumulation of Jacobi rotations. */
2389                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
2390                     dgesvj_("L", "U", "V", &nr, &nr, &v[v_offset], ldv, &sva[
2391                             1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
2392                              nr + nr + 1], &i__1, info);
2393                     scalem = work[(*n << 1) + *n * nr + nr + 1];
2394                     numrank = i_dnnt(&work[(*n << 1) + *n * nr + nr + 2]);
2395                     if (nr < *n) {
2396                         i__1 = *n - nr;
2397                         dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + 
2398                                 v_dim1], ldv);
2399                         i__1 = *n - nr;
2400                         dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) *
2401                                  v_dim1 + 1], ldv);
2402                         i__1 = *n - nr;
2403                         i__2 = *n - nr;
2404                         dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 
2405                                 + (nr + 1) * v_dim1], ldv);
2406                     }
2407                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
2408                     dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &
2409                             work[*n + 1], &v[v_offset], ldv, &work[(*n << 1) 
2410                             + *n * nr + nr + 1], &i__1, &ierr);
2411
2412                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
2413                     dormlq_("L", "T", &nr, &nr, &nr, &work[(*n << 1) + 1], n, 
2414                             &work[(*n << 1) + *n * nr + 1], &u[u_offset], ldu,
2415                              &work[(*n << 1) + *n * nr + nr + 1], &i__1, &
2416                             ierr);
2417                     i__1 = nr;
2418                     for (q = 1; q <= i__1; ++q) {
2419                         i__2 = nr;
2420                         for (p = 1; p <= i__2; ++p) {
2421                             work[(*n << 1) + *n * nr + nr + iwork[*n + p]] = 
2422                                     u[p + q * u_dim1];
2423 /* L772: */
2424                         }
2425                         i__2 = nr;
2426                         for (p = 1; p <= i__2; ++p) {
2427                             u[p + q * u_dim1] = work[(*n << 1) + *n * nr + nr 
2428                                     + p];
2429 /* L774: */
2430                         }
2431 /* L773: */
2432                     }
2433
2434                 }
2435
2436 /*           Permute the rows of V using the (column) permutation from the */
2437 /*           first QRF. Also, scale the columns to make them unit in */
2438 /*           Euclidean norm. This applies to all cases. */
2439
2440                 temp1 = sqrt((doublereal) (*n)) * epsln;
2441                 i__1 = *n;
2442                 for (q = 1; q <= i__1; ++q) {
2443                     i__2 = *n;
2444                     for (p = 1; p <= i__2; ++p) {
2445                         work[(*n << 1) + *n * nr + nr + iwork[p]] = v[p + q * 
2446                                 v_dim1];
2447 /* L972: */
2448                     }
2449                     i__2 = *n;
2450                     for (p = 1; p <= i__2; ++p) {
2451                         v[p + q * v_dim1] = work[(*n << 1) + *n * nr + nr + p]
2452                                 ;
2453 /* L973: */
2454                     }
2455                     xsc = 1. / dnrm2_(n, &v[q * v_dim1 + 1], &c__1);
2456                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
2457                         dscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
2458                     }
2459 /* L1972: */
2460                 }
2461 /*           At this moment, V contains the right singular vectors of A. */
2462 /*           Next, assemble the left singular vector matrix U (M x N). */
2463                 if (nr < *m) {
2464                     i__1 = *m - nr;
2465                     dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 + 
2466                             u_dim1], ldu);
2467                     if (nr < n1) {
2468                         i__1 = n1 - nr;
2469                         dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) *
2470                                  u_dim1 + 1], ldu);
2471                         i__1 = *m - nr;
2472                         i__2 = n1 - nr;
2473                         dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 
2474                                 + (nr + 1) * u_dim1], ldu);
2475                     }
2476                 }
2477
2478 /*           The Q matrix from the first QRF is built into the left singular */
2479 /*           matrix U. This applies to all cases. */
2480
2481                 i__1 = *lwork - *n;
2482                 dormqr_("Left", "No_Tr", m, &n1, n, &a[a_offset], lda, &work[
2483                         1], &u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
2484 /*           The columns of U are normalized. The cost is O(M*N) flops. */
2485                 temp1 = sqrt((doublereal) (*m)) * epsln;
2486                 i__1 = nr;
2487                 for (p = 1; p <= i__1; ++p) {
2488                     xsc = 1. / dnrm2_(m, &u[p * u_dim1 + 1], &c__1);
2489                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
2490                         dscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
2491                     }
2492 /* L1973: */
2493                 }
2494
2495 /*           If the initial QRF is computed with row pivoting, the left */
2496 /*           singular vectors must be adjusted. */
2497
2498                 if (rowpiv) {
2499                     i__1 = *m - 1;
2500                     dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n 
2501                             << 1) + 1], &c_n1);
2502                 }
2503
2504             } else {
2505
2506 /*        the second QRF is not needed */
2507
2508                 dlacpy_("Upper", n, n, &a[a_offset], lda, &work[*n + 1], n);
2509                 if (l2pert) {
2510                     xsc = sqrt(small);
2511                     i__1 = *n;
2512                     for (p = 2; p <= i__1; ++p) {
2513                         temp1 = xsc * work[*n + (p - 1) * *n + p];
2514                         i__2 = p - 1;
2515                         for (q = 1; q <= i__2; ++q) {
2516                             work[*n + (q - 1) * *n + p] = -d_sign(&temp1, &
2517                                     work[*n + (p - 1) * *n + q]);
2518 /* L5971: */
2519                         }
2520 /* L5970: */
2521                     }
2522                 } else {
2523                     i__1 = *n - 1;
2524                     i__2 = *n - 1;
2525                     dlaset_("Lower", &i__1, &i__2, &c_b34, &c_b34, &work[*n + 
2526                             2], n);
2527                 }
2528
2529                 i__1 = *lwork - *n - *n * *n;
2530                 dgesvj_("Upper", "U", "N", n, n, &work[*n + 1], n, &sva[1], n,
2531                          &u[u_offset], ldu, &work[*n + *n * *n + 1], &i__1, 
2532                         info);
2533
2534                 scalem = work[*n + *n * *n + 1];
2535                 numrank = i_dnnt(&work[*n + *n * *n + 2]);
2536                 i__1 = *n;
2537                 for (p = 1; p <= i__1; ++p) {
2538                     dcopy_(n, &work[*n + (p - 1) * *n + 1], &c__1, &u[p * 
2539                             u_dim1 + 1], &c__1);
2540                     dscal_(n, &sva[p], &work[*n + (p - 1) * *n + 1], &c__1);
2541 /* L6970: */
2542                 }
2543
2544                 dtrsm_("Left", "Upper", "NoTrans", "No UD", n, n, &c_b35, &a[
2545                         a_offset], lda, &work[*n + 1], n);
2546                 i__1 = *n;
2547                 for (p = 1; p <= i__1; ++p) {
2548                     dcopy_(n, &work[*n + p], n, &v[iwork[p] + v_dim1], ldv);
2549 /* L6972: */
2550                 }
2551                 temp1 = sqrt((doublereal) (*n)) * epsln;
2552                 i__1 = *n;
2553                 for (p = 1; p <= i__1; ++p) {
2554                     xsc = 1. / dnrm2_(n, &v[p * v_dim1 + 1], &c__1);
2555                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
2556                         dscal_(n, &xsc, &v[p * v_dim1 + 1], &c__1);
2557                     }
2558 /* L6971: */
2559                 }
2560
2561 /*           Assemble the left singular vector matrix U (M x N). */
2562
2563                 if (*n < *m) {
2564                     i__1 = *m - *n;
2565                     dlaset_("A", &i__1, n, &c_b34, &c_b34, &u[*n + 1 + u_dim1]
2566                             , ldu);
2567                     if (*n < n1) {
2568                         i__1 = n1 - *n;
2569                         dlaset_("A", n, &i__1, &c_b34, &c_b34, &u[(*n + 1) * 
2570                                 u_dim1 + 1], ldu);
2571                         i__1 = *m - *n;
2572                         i__2 = n1 - *n;
2573                         dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[*n + 1 
2574                                 + (*n + 1) * u_dim1], ldu);
2575                     }
2576                 }
2577                 i__1 = *lwork - *n;
2578                 dormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[
2579                         1], &u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
2580                 temp1 = sqrt((doublereal) (*m)) * epsln;
2581                 i__1 = n1;
2582                 for (p = 1; p <= i__1; ++p) {
2583                     xsc = 1. / dnrm2_(m, &u[p * u_dim1 + 1], &c__1);
2584                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
2585                         dscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
2586                     }
2587 /* L6973: */
2588                 }
2589
2590                 if (rowpiv) {
2591                     i__1 = *m - 1;
2592                     dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n 
2593                             << 1) + 1], &c_n1);
2594                 }
2595
2596             }
2597
2598 /*        end of the  >> almost orthogonal case <<  in the full SVD */
2599
2600         } else {
2601
2602 /*        This branch deploys a preconditioned Jacobi SVD with explicitly */
2603 /*        accumulated rotations. It is included as optional, mainly for */
2604 /*        experimental purposes. It does perform well, and can also be used. */
2605 /*        In this implementation, this branch will be automatically activated */
2606 /*        if the  condition number sigma_max(A) / sigma_min(A) is predicted */
2607 /*        to be greater than the overflow threshold. This is because the */
2608 /*        a posteriori computation of the singular vectors assumes robust */
2609 /*        implementation of BLAS and some LAPACK procedures, capable of working */
2610 /*        in presence of extreme values. Since that is not always the case, ... */
2611
2612             i__1 = nr;
2613             for (p = 1; p <= i__1; ++p) {
2614                 i__2 = *n - p + 1;
2615                 dcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
2616                         c__1);
2617 /* L7968: */
2618             }
2619
2620             if (l2pert) {
2621                 xsc = sqrt(small / epsln);
2622                 i__1 = nr;
2623                 for (q = 1; q <= i__1; ++q) {
2624                     temp1 = xsc * (d__1 = v[q + q * v_dim1], abs(d__1));
2625                     i__2 = *n;
2626                     for (p = 1; p <= i__2; ++p) {
2627                         if (p > q && (d__1 = v[p + q * v_dim1], abs(d__1)) <= 
2628                                 temp1 || p < q) {
2629                             v[p + q * v_dim1] = d_sign(&temp1, &v[p + q * 
2630                                     v_dim1]);
2631                         }
2632                         if (p < q) {
2633                             v[p + q * v_dim1] = -v[p + q * v_dim1];
2634                         }
2635 /* L5968: */
2636                     }
2637 /* L5969: */
2638                 }
2639             } else {
2640                 i__1 = nr - 1;
2641                 i__2 = nr - 1;
2642                 dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) + 
2643                         1], ldv);
2644             }
2645             i__1 = *lwork - (*n << 1);
2646             dgeqrf_(n, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*n << 1) 
2647                     + 1], &i__1, &ierr);
2648             dlacpy_("L", n, &nr, &v[v_offset], ldv, &work[(*n << 1) + 1], n);
2649
2650             i__1 = nr;
2651             for (p = 1; p <= i__1; ++p) {
2652                 i__2 = nr - p + 1;
2653                 dcopy_(&i__2, &v[p + p * v_dim1], ldv, &u[p + p * u_dim1], &
2654                         c__1);
2655 /* L7969: */
2656             }
2657             if (l2pert) {
2658                 xsc = sqrt(small / epsln);
2659                 i__1 = nr;
2660                 for (q = 2; q <= i__1; ++q) {
2661                     i__2 = q - 1;
2662                     for (p = 1; p <= i__2; ++p) {
2663 /* Computing MIN */
2664                         d__3 = (d__1 = u[p + p * u_dim1], abs(d__1)), d__4 = (
2665                                 d__2 = u[q + q * u_dim1], abs(d__2));
2666                         temp1 = xsc * f2cmin(d__3,d__4);
2667                         u[p + q * u_dim1] = -d_sign(&temp1, &u[q + p * u_dim1]
2668                                 );
2669 /* L9971: */
2670                     }
2671 /* L9970: */
2672                 }
2673             } else {
2674                 i__1 = nr - 1;
2675                 i__2 = nr - 1;
2676                 dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 
2677                         1], ldu);
2678             }
2679             i__1 = *lwork - (*n << 1) - *n * nr;
2680             dgesvj_("G", "U", "V", &nr, &nr, &u[u_offset], ldu, &sva[1], n, &
2681                     v[v_offset], ldv, &work[(*n << 1) + *n * nr + 1], &i__1, 
2682                     info);
2683             scalem = work[(*n << 1) + *n * nr + 1];
2684             numrank = i_dnnt(&work[(*n << 1) + *n * nr + 2]);
2685             if (nr < *n) {
2686                 i__1 = *n - nr;
2687                 dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + v_dim1], 
2688                         ldv);
2689                 i__1 = *n - nr;
2690                 dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) * v_dim1 
2691                         + 1], ldv);
2692                 i__1 = *n - nr;
2693                 i__2 = *n - nr;
2694                 dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 + (nr + 
2695                         1) * v_dim1], ldv);
2696             }
2697             i__1 = *lwork - (*n << 1) - *n * nr - nr;
2698             dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &work[*n + 
2699                     1], &v[v_offset], ldv, &work[(*n << 1) + *n * nr + nr + 1]
2700                     , &i__1, &ierr);
2701
2702 /*           Permute the rows of V using the (column) permutation from the */
2703 /*           first QRF. Also, scale the columns to make them unit in */
2704 /*           Euclidean norm. This applies to all cases. */
2705
2706             temp1 = sqrt((doublereal) (*n)) * epsln;
2707             i__1 = *n;
2708             for (q = 1; q <= i__1; ++q) {
2709                 i__2 = *n;
2710                 for (p = 1; p <= i__2; ++p) {
2711                     work[(*n << 1) + *n * nr + nr + iwork[p]] = v[p + q * 
2712                             v_dim1];
2713 /* L8972: */
2714                 }
2715                 i__2 = *n;
2716                 for (p = 1; p <= i__2; ++p) {
2717                     v[p + q * v_dim1] = work[(*n << 1) + *n * nr + nr + p];
2718 /* L8973: */
2719                 }
2720                 xsc = 1. / dnrm2_(n, &v[q * v_dim1 + 1], &c__1);
2721                 if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
2722                     dscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
2723                 }
2724 /* L7972: */
2725             }
2726
2727 /*           At this moment, V contains the right singular vectors of A. */
2728 /*           Next, assemble the left singular vector matrix U (M x N). */
2729
2730             if (nr < *m) {
2731                 i__1 = *m - nr;
2732                 dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 + u_dim1], 
2733                         ldu);
2734                 if (nr < n1) {
2735                     i__1 = n1 - nr;
2736                     dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) * 
2737                             u_dim1 + 1], ldu);
2738                     i__1 = *m - nr;
2739                     i__2 = n1 - nr;
2740                     dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 + (
2741                             nr + 1) * u_dim1], ldu);
2742                 }
2743             }
2744
2745             i__1 = *lwork - *n;
2746             dormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[1], &
2747                     u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
2748
2749             if (rowpiv) {
2750                 i__1 = *m - 1;
2751                 dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n << 1)
2752                          + 1], &c_n1);
2753             }
2754
2755
2756         }
2757         if (transp) {
2758             i__1 = *n;
2759             for (p = 1; p <= i__1; ++p) {
2760                 dswap_(n, &u[p * u_dim1 + 1], &c__1, &v[p * v_dim1 + 1], &
2761                         c__1);
2762 /* L6974: */
2763             }
2764         }
2765
2766     }
2767 /*     end of the full SVD */
2768
2769 /*     Undo scaling, if necessary (and possible) */
2770
2771     if (uscal2 <= big / sva[1] * uscal1) {
2772         dlascl_("G", &c__0, &c__0, &uscal1, &uscal2, &nr, &c__1, &sva[1], n, &
2773                 ierr);
2774         uscal1 = 1.;
2775         uscal2 = 1.;
2776     }
2777
2778     if (nr < *n) {
2779         i__1 = *n;
2780         for (p = nr + 1; p <= i__1; ++p) {
2781             sva[p] = 0.;
2782 /* L3004: */
2783         }
2784     }
2785
2786     work[1] = uscal2 * scalem;
2787     work[2] = uscal1;
2788     if (errest) {
2789         work[3] = sconda;
2790     }
2791     if (lsvec && rsvec) {
2792         work[4] = condr1;
2793         work[5] = condr2;
2794     }
2795     if (l2tran) {
2796         work[6] = entra;
2797         work[7] = entrat;
2798     }
2799
2800     iwork[1] = nr;
2801     iwork[2] = numrank;
2802     iwork[3] = warning;
2803
2804     return 0;
2805 } /* dgejsv_ */
2806