C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / zgejsv.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]/Cd(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
514 /* Table of constant values */
515
516 static doublecomplex c_b1 = {0.,0.};
517 static doublecomplex c_b2 = {1.,0.};
518 static integer c_n1 = -1;
519 static integer c__1 = 1;
520 static integer c__0 = 0;
521 static doublereal c_b141 = 1.;
522 static logical c_false = FALSE_;
523
524 /* > \brief \b ZGEJSV */
525
526 /*  =========== DOCUMENTATION =========== */
527
528 /* Online html documentation available at */
529 /*            http://www.netlib.org/lapack/explore-html/ */
530
531 /* > \htmlonly */
532 /* > Download ZGEJSV + dependencies */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zgejsv.
534 f"> */
535 /* > [TGZ]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zgejsv.
537 f"> */
538 /* > [ZIP]</a> */
539 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zgejsv.
540 f"> */
541 /* > [TXT]</a> */
542 /* > \endhtmlonly */
543
544 /*  Definition: */
545 /*  =========== */
546
547 /*     SUBROUTINE ZGEJSV( JOBA, JOBU, JOBV, JOBR, JOBT, JOBP, */
548 /*                         M, N, A, LDA, SVA, U, LDU, V, LDV, */
549 /*                         CWORK, LWORK, RWORK, LRWORK, IWORK, INFO ) */
550
551 /*     IMPLICIT    NONE */
552 /*     INTEGER     INFO, LDA, LDU, LDV, LWORK, M, N */
553 /*     COMPLEX*16     A( LDA, * ),  U( LDU, * ), V( LDV, * ), CWORK( LWORK ) */
554 /*     DOUBLE PRECISION   SVA( N ), RWORK( LRWORK ) */
555 /*     INTEGER     IWORK( * ) */
556 /*     CHARACTER*1 JOBA, JOBP, JOBR, JOBT, JOBU, JOBV */
557
558
559 /* > \par Purpose: */
560 /*  ============= */
561 /* > */
562 /* > \verbatim */
563 /* > */
564 /* > ZGEJSV computes the singular value decomposition (SVD) of a complex M-by-N */
565 /* > matrix [A], where M >= N. The SVD of [A] is written as */
566 /* > */
567 /* >              [A] = [U] * [SIGMA] * [V]^*, */
568 /* > */
569 /* > where [SIGMA] is an N-by-N (M-by-N) matrix which is zero except for its N */
570 /* > diagonal elements, [U] is an M-by-N (or M-by-M) unitary matrix, and */
571 /* > [V] is an N-by-N unitary matrix. The diagonal elements of [SIGMA] are */
572 /* > the singular values of [A]. The columns of [U] and [V] are the left and */
573 /* > the right singular vectors of [A], respectively. The matrices [U] and [V] */
574 /* > are computed and stored in the arrays U and V, respectively. The diagonal */
575 /* > of [SIGMA] is computed and stored in the array SVA. */
576 /* > \endverbatim */
577 /* > */
578 /* >  Arguments: */
579 /* >  ========== */
580 /* > */
581 /* > \param[in] JOBA */
582 /* > \verbatim */
583 /* >          JOBA is CHARACTER*1 */
584 /* >         Specifies the level of accuracy: */
585 /* >       = 'C': This option works well (high relative accuracy) if A = B * D, */
586 /* >              with well-conditioned B and arbitrary diagonal matrix D. */
587 /* >              The accuracy cannot be spoiled by COLUMN scaling. The */
588 /* >              accuracy of the computed output depends on the condition of */
589 /* >              B, and the procedure aims at the best theoretical accuracy. */
590 /* >              The relative error max_{i=1:N}|d sigma_i| / sigma_i is */
591 /* >              bounded by f(M,N)*epsilon* cond(B), independent of D. */
592 /* >              The input matrix is preprocessed with the QRF with column */
593 /* >              pivoting. This initial preprocessing and preconditioning by */
594 /* >              a rank revealing QR factorization is common for all values of */
595 /* >              JOBA. Additional actions are specified as follows: */
596 /* >       = 'E': Computation as with 'C' with an additional estimate of the */
597 /* >              condition number of B. It provides a realistic error bound. */
598 /* >       = 'F': If A = D1 * C * D2 with ill-conditioned diagonal scalings */
599 /* >              D1, D2, and well-conditioned matrix C, this option gives */
600 /* >              higher accuracy than the 'C' option. If the structure of the */
601 /* >              input matrix is not known, and relative accuracy is */
602 /* >              desirable, then this option is advisable. The input matrix A */
603 /* >              is preprocessed with QR factorization with FULL (row and */
604 /* >              column) pivoting. */
605 /* >       = 'G': Computation as with 'F' with an additional estimate of the */
606 /* >              condition number of B, where A=B*D. If A has heavily weighted */
607 /* >              rows, then using this condition number gives too pessimistic */
608 /* >              error bound. */
609 /* >       = 'A': Small singular values are not well determined by the data */
610 /* >              and are considered as noisy; the matrix is treated as */
611 /* >              numerically rank deficient. The error in the computed */
612 /* >              singular values is bounded by f(m,n)*epsilon*||A||. */
613 /* >              The computed SVD A = U * S * V^* restores A up to */
614 /* >              f(m,n)*epsilon*||A||. */
615 /* >              This gives the procedure the licence to discard (set to zero) */
616 /* >              all singular values below N*epsilon*||A||. */
617 /* >       = 'R': Similar as in 'A'. Rank revealing property of the initial */
618 /* >              QR factorization is used do reveal (using triangular factor) */
619 /* >              a gap sigma_{r+1} < epsilon * sigma_r in which case the */
620 /* >              numerical RANK is declared to be r. The SVD is computed with */
621 /* >              absolute error bounds, but more accurately than with 'A'. */
622 /* > \endverbatim */
623 /* > */
624 /* > \param[in] JOBU */
625 /* > \verbatim */
626 /* >          JOBU is CHARACTER*1 */
627 /* >         Specifies whether to compute the columns of U: */
628 /* >       = 'U': N columns of U are returned in the array U. */
629 /* >       = 'F': full set of M left sing. vectors is returned in the array U. */
630 /* >       = 'W': U may be used as workspace of length M*N. See the description */
631 /* >              of U. */
632 /* >       = 'N': U is not computed. */
633 /* > \endverbatim */
634 /* > */
635 /* > \param[in] JOBV */
636 /* > \verbatim */
637 /* >          JOBV is CHARACTER*1 */
638 /* >         Specifies whether to compute the matrix V: */
639 /* >       = 'V': N columns of V are returned in the array V; Jacobi rotations */
640 /* >              are not explicitly accumulated. */
641 /* >       = 'J': N columns of V are returned in the array V, but they are */
642 /* >              computed as the product of Jacobi rotations, if JOBT = 'N'. */
643 /* >       = 'W': V may be used as workspace of length N*N. See the description */
644 /* >              of V. */
645 /* >       = 'N': V is not computed. */
646 /* > \endverbatim */
647 /* > */
648 /* > \param[in] JOBR */
649 /* > \verbatim */
650 /* >          JOBR is CHARACTER*1 */
651 /* >         Specifies the RANGE for the singular values. Issues the licence to */
652 /* >         set to zero small positive singular values if they are outside */
653 /* >         specified range. If A .NE. 0 is scaled so that the largest singular */
654 /* >         value of c*A is around SQRT(BIG), BIG=DLAMCH('O'), then JOBR issues */
655 /* >         the licence to kill columns of A whose norm in c*A is less than */
656 /* >         SQRT(SFMIN) (for JOBR = 'R'), or less than SMALL=SFMIN/EPSLN, */
657 /* >         where SFMIN=DLAMCH('S'), EPSLN=DLAMCH('E'). */
658 /* >       = 'N': Do not kill small columns of c*A. This option assumes that */
659 /* >              BLAS and QR factorizations and triangular solvers are */
660 /* >              implemented to work in that range. If the condition of A */
661 /* >              is greater than BIG, use ZGESVJ. */
662 /* >       = 'R': RESTRICTED range for sigma(c*A) is [SQRT(SFMIN), SQRT(BIG)] */
663 /* >              (roughly, as described above). This option is recommended. */
664 /* >                                             =========================== */
665 /* >         For computing the singular values in the FULL range [SFMIN,BIG] */
666 /* >         use ZGESVJ. */
667 /* > \endverbatim */
668 /* > */
669 /* > \param[in] JOBT */
670 /* > \verbatim */
671 /* >          JOBT is CHARACTER*1 */
672 /* >         If the matrix is square then the procedure may determine to use */
673 /* >         transposed A if A^* seems to be better with respect to convergence. */
674 /* >         If the matrix is not square, JOBT is ignored. */
675 /* >         The decision is based on two values of entropy over the adjoint */
676 /* >         orbit of A^* * 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^* is taken as input. If A is */
679 /* >         replaced with A^*, then the row pivoting is included automatically. */
680 /* >       = 'N': do not speculate. */
681 /* >         The option 'T' can be used to compute only the singular values, or */
682 /* >         the 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 /* >         In general, this option is considered experimental, and 'N'; should */
688 /* >         be preferred. This is subject to changes in the future. */
689 /* > \endverbatim */
690 /* > */
691 /* > \param[in] JOBP */
692 /* > \verbatim */
693 /* >          JOBP is CHARACTER*1 */
694 /* >         Issues the licence to introduce structured perturbations to drown */
695 /* >         denormalized numbers. This licence should be active if the */
696 /* >         denormals are poorly implemented, causing slow computation, */
697 /* >         especially in cases of fast convergence (!). For details see [1,2]. */
698 /* >         For the sake of simplicity, this perturbations are included only */
699 /* >         when the full SVD or only the singular values are requested. The */
700 /* >         implementer/user can easily add the perturbation for the cases of */
701 /* >         computing one set of singular vectors. */
702 /* >       = 'P': introduce perturbation */
703 /* >       = 'N': do not perturb */
704 /* > \endverbatim */
705 /* > */
706 /* > \param[in] M */
707 /* > \verbatim */
708 /* >          M is INTEGER */
709 /* >         The number of rows of the input matrix A.  M >= 0. */
710 /* > \endverbatim */
711 /* > */
712 /* > \param[in] N */
713 /* > \verbatim */
714 /* >          N is INTEGER */
715 /* >         The number of columns of the input matrix A. M >= N >= 0. */
716 /* > \endverbatim */
717 /* > */
718 /* > \param[in,out] A */
719 /* > \verbatim */
720 /* >          A is COMPLEX*16 array, dimension (LDA,N) */
721 /* >          On entry, the M-by-N matrix A. */
722 /* > \endverbatim */
723 /* > */
724 /* > \param[in] LDA */
725 /* > \verbatim */
726 /* >          LDA is INTEGER */
727 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,M). */
728 /* > \endverbatim */
729 /* > */
730 /* > \param[out] SVA */
731 /* > \verbatim */
732 /* >          SVA is DOUBLE PRECISION array, dimension (N) */
733 /* >          On exit, */
734 /* >          - For WORK(1)/WORK(2) = ONE: The singular values of A. During the */
735 /* >            computation SVA contains Euclidean column norms of the */
736 /* >            iterated matrices in the array A. */
737 /* >          - For WORK(1) .NE. WORK(2): The singular values of A are */
738 /* >            (WORK(1)/WORK(2)) * SVA(1:N). This factored form is used if */
739 /* >            sigma_max(A) overflows or if small singular values have been */
740 /* >            saved from underflow by scaling the input matrix A. */
741 /* >          - If JOBR='R' then some of the singular values may be returned */
742 /* >            as exact zeros obtained by "set to zero" because they are */
743 /* >            below the numerical rank threshold or are denormalized numbers. */
744 /* > \endverbatim */
745 /* > */
746 /* > \param[out] U */
747 /* > \verbatim */
748 /* >          U is COMPLEX*16 array, dimension ( LDU, N ) */
749 /* >          If JOBU = 'U', then U contains on exit the M-by-N matrix of */
750 /* >                         the left singular vectors. */
751 /* >          If JOBU = 'F', then U contains on exit the M-by-M matrix of */
752 /* >                         the left singular vectors, including an ONB */
753 /* >                         of the orthogonal complement of the Range(A). */
754 /* >          If JOBU = 'W'  .AND. (JOBV = 'V' .AND. JOBT = 'T' .AND. M = N), */
755 /* >                         then U is used as workspace if the procedure */
756 /* >                         replaces A with A^*. In that case, [V] is computed */
757 /* >                         in U as left singular vectors of A^* and then */
758 /* >                         copied back to the V array. This 'W' option is just */
759 /* >                         a reminder to the caller that in this case U is */
760 /* >                         reserved as workspace of length N*N. */
761 /* >          If JOBU = 'N'  U is not referenced, unless JOBT='T'. */
762 /* > \endverbatim */
763 /* > */
764 /* > \param[in] LDU */
765 /* > \verbatim */
766 /* >          LDU is INTEGER */
767 /* >          The leading dimension of the array U,  LDU >= 1. */
768 /* >          IF  JOBU = 'U' or 'F' or 'W',  then LDU >= M. */
769 /* > \endverbatim */
770 /* > */
771 /* > \param[out] V */
772 /* > \verbatim */
773 /* >          V is COMPLEX*16 array, dimension ( LDV, N ) */
774 /* >          If JOBV = 'V', 'J' then V contains on exit the N-by-N matrix of */
775 /* >                         the right singular vectors; */
776 /* >          If JOBV = 'W', AND (JOBU = 'U' AND JOBT = 'T' AND M = N), */
777 /* >                         then V is used as workspace if the pprocedure */
778 /* >                         replaces A with A^*. In that case, [U] is computed */
779 /* >                         in V as right singular vectors of A^* and then */
780 /* >                         copied back to the U array. This 'W' option is just */
781 /* >                         a reminder to the caller that in this case V is */
782 /* >                         reserved as workspace of length N*N. */
783 /* >          If JOBV = 'N'  V is not referenced, unless JOBT='T'. */
784 /* > \endverbatim */
785 /* > */
786 /* > \param[in] LDV */
787 /* > \verbatim */
788 /* >          LDV is INTEGER */
789 /* >          The leading dimension of the array V,  LDV >= 1. */
790 /* >          If JOBV = 'V' or 'J' or 'W', then LDV >= N. */
791 /* > \endverbatim */
792 /* > */
793 /* > \param[out] CWORK */
794 /* > \verbatim */
795 /* >          CWORK is COMPLEX*16 array, dimension (MAX(2,LWORK)) */
796 /* >          If the call to ZGEJSV is a workspace query (indicated by LWORK=-1 or */
797 /* >          LRWORK=-1), then on exit CWORK(1) contains the required length of */
798 /* >          CWORK for the job parameters used in the call. */
799 /* > \endverbatim */
800 /* > */
801 /* > \param[in] LWORK */
802 /* > \verbatim */
803 /* >          LWORK is INTEGER */
804 /* >          Length of CWORK to confirm proper allocation of workspace. */
805 /* >          LWORK depends on the job: */
806 /* > */
807 /* >          1. If only SIGMA is needed ( JOBU = 'N', JOBV = 'N' ) and */
808 /* >            1.1 .. no scaled condition estimate required (JOBA.NE.'E'.AND.JOBA.NE.'G'): */
809 /* >               LWORK >= 2*N+1. This is the minimal requirement. */
810 /* >               ->> For optimal performance (blocked code) the optimal value */
811 /* >               is LWORK >= N + (N+1)*NB. Here NB is the optimal */
812 /* >               block size for ZGEQP3 and ZGEQRF. */
813 /* >               In general, optimal LWORK is computed as */
814 /* >               LWORK >= f2cmax(N+LWORK(ZGEQP3),N+LWORK(ZGEQRF), LWORK(ZGESVJ)). */
815 /* >            1.2. .. an estimate of the scaled condition number of A is */
816 /* >               required (JOBA='E', or 'G'). In this case, LWORK the minimal */
817 /* >               requirement is LWORK >= N*N + 2*N. */
818 /* >               ->> For optimal performance (blocked code) the optimal value */
819 /* >               is LWORK >= f2cmax(N+(N+1)*NB, N*N+2*N)=N**2+2*N. */
820 /* >               In general, the optimal length LWORK is computed as */
821 /* >               LWORK >= f2cmax(N+LWORK(ZGEQP3),N+LWORK(ZGEQRF), LWORK(ZGESVJ), */
822 /* >                            N*N+LWORK(ZPOCON)). */
823 /* >          2. If SIGMA and the right singular vectors are needed (JOBV = 'V'), */
824 /* >             (JOBU = 'N') */
825 /* >            2.1   .. no scaled condition estimate requested (JOBE = 'N'): */
826 /* >            -> the minimal requirement is LWORK >= 3*N. */
827 /* >            -> For optimal performance, */
828 /* >               LWORK >= f2cmax(N+(N+1)*NB, 2*N+N*NB)=2*N+N*NB, */
829 /* >               where NB is the optimal block size for ZGEQP3, ZGEQRF, ZGELQ, */
830 /* >               ZUNMLQ. In general, the optimal length LWORK is computed as */
831 /* >               LWORK >= f2cmax(N+LWORK(ZGEQP3), N+LWORK(ZGESVJ), */
832 /* >                       N+LWORK(ZGELQF), 2*N+LWORK(ZGEQRF), N+LWORK(ZUNMLQ)). */
833 /* >            2.2 .. an estimate of the scaled condition number of A is */
834 /* >               required (JOBA='E', or 'G'). */
835 /* >            -> the minimal requirement is LWORK >= 3*N. */
836 /* >            -> For optimal performance, */
837 /* >               LWORK >= f2cmax(N+(N+1)*NB, 2*N,2*N+N*NB)=2*N+N*NB, */
838 /* >               where NB is the optimal block size for ZGEQP3, ZGEQRF, ZGELQ, */
839 /* >               ZUNMLQ. In general, the optimal length LWORK is computed as */
840 /* >               LWORK >= f2cmax(N+LWORK(ZGEQP3), LWORK(ZPOCON), N+LWORK(ZGESVJ), */
841 /* >                       N+LWORK(ZGELQF), 2*N+LWORK(ZGEQRF), N+LWORK(ZUNMLQ)). */
842 /* >          3. If SIGMA and the left singular vectors are needed */
843 /* >            3.1  .. no scaled condition estimate requested (JOBE = 'N'): */
844 /* >            -> the minimal requirement is LWORK >= 3*N. */
845 /* >            -> For optimal performance: */
846 /* >               if JOBU = 'U' :: LWORK >= f2cmax(3*N, N+(N+1)*NB, 2*N+N*NB)=2*N+N*NB, */
847 /* >               where NB is the optimal block size for ZGEQP3, ZGEQRF, ZUNMQR. */
848 /* >               In general, the optimal length LWORK is computed as */
849 /* >               LWORK >= f2cmax(N+LWORK(ZGEQP3), 2*N+LWORK(ZGEQRF), N+LWORK(ZUNMQR)). */
850 /* >            3.2  .. an estimate of the scaled condition number of A is */
851 /* >               required (JOBA='E', or 'G'). */
852 /* >            -> the minimal requirement is LWORK >= 3*N. */
853 /* >            -> For optimal performance: */
854 /* >               if JOBU = 'U' :: LWORK >= f2cmax(3*N, N+(N+1)*NB, 2*N+N*NB)=2*N+N*NB, */
855 /* >               where NB is the optimal block size for ZGEQP3, ZGEQRF, ZUNMQR. */
856 /* >               In general, the optimal length LWORK is computed as */
857 /* >               LWORK >= f2cmax(N+LWORK(ZGEQP3),N+LWORK(ZPOCON), */
858 /* >                        2*N+LWORK(ZGEQRF), N+LWORK(ZUNMQR)). */
859 /* >          4. If the full SVD is needed: (JOBU = 'U' or JOBU = 'F') and */
860 /* >            4.1. if JOBV = 'V' */
861 /* >               the minimal requirement is LWORK >= 5*N+2*N*N. */
862 /* >            4.2. if JOBV = 'J' the minimal requirement is */
863 /* >               LWORK >= 4*N+N*N. */
864 /* >            In both cases, the allocated CWORK can accommodate blocked runs */
865 /* >            of ZGEQP3, ZGEQRF, ZGELQF, SUNMQR, ZUNMLQ. */
866 /* > */
867 /* >          If the call to ZGEJSV is a workspace query (indicated by LWORK=-1 or */
868 /* >          LRWORK=-1), then on exit CWORK(1) contains the optimal and CWORK(2) contains the */
869 /* >          minimal length of CWORK for the job parameters used in the call. */
870 /* > \endverbatim */
871 /* > */
872 /* > \param[out] RWORK */
873 /* > \verbatim */
874 /* >          RWORK is DOUBLE PRECISION array, dimension (MAX(7,LWORK)) */
875 /* >          On exit, */
876 /* >          RWORK(1) = Determines the scaling factor SCALE = RWORK(2) / RWORK(1) */
877 /* >                    such that SCALE*SVA(1:N) are the computed singular values */
878 /* >                    of A. (See the description of SVA().) */
879 /* >          RWORK(2) = See the description of RWORK(1). */
880 /* >          RWORK(3) = SCONDA is an estimate for the condition number of */
881 /* >                    column equilibrated A. (If JOBA = 'E' or 'G') */
882 /* >                    SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1). */
883 /* >                    It is computed using SPOCON. It holds */
884 /* >                    N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
885 /* >                    where R is the triangular factor from the QRF of A. */
886 /* >                    However, if R is truncated and the numerical rank is */
887 /* >                    determined to be strictly smaller than N, SCONDA is */
888 /* >                    returned as -1, thus indicating that the smallest */
889 /* >                    singular values might be lost. */
890 /* > */
891 /* >          If full SVD is needed, the following two condition numbers are */
892 /* >          useful for the analysis of the algorithm. They are provied for */
893 /* >          a developer/implementer who is familiar with the details of */
894 /* >          the method. */
895 /* > */
896 /* >          RWORK(4) = an estimate of the scaled condition number of the */
897 /* >                    triangular factor in the first QR factorization. */
898 /* >          RWORK(5) = an estimate of the scaled condition number of the */
899 /* >                    triangular factor in the second QR factorization. */
900 /* >          The following two parameters are computed if JOBT = 'T'. */
901 /* >          They are provided for a developer/implementer who is familiar */
902 /* >          with the details of the method. */
903 /* >          RWORK(6) = the entropy of A^* * A :: this is the Shannon entropy */
904 /* >                    of diag(A^* * A) / Trace(A^* * A) taken as point in the */
905 /* >                    probability simplex. */
906 /* >          RWORK(7) = the entropy of A * A^*. (See the description of RWORK(6).) */
907 /* >          If the call to ZGEJSV is a workspace query (indicated by LWORK=-1 or */
908 /* >          LRWORK=-1), then on exit RWORK(1) contains the required length of */
909 /* >          RWORK for the job parameters used in the call. */
910 /* > \endverbatim */
911 /* > */
912 /* > \param[in] LRWORK */
913 /* > \verbatim */
914 /* >          LRWORK is INTEGER */
915 /* >          Length of RWORK to confirm proper allocation of workspace. */
916 /* >          LRWORK depends on the job: */
917 /* > */
918 /* >       1. If only the singular values are requested i.e. if */
919 /* >          LSAME(JOBU,'N') .AND. LSAME(JOBV,'N') */
920 /* >          then: */
921 /* >          1.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
922 /* >               then: LRWORK = f2cmax( 7, 2 * M ). */
923 /* >          1.2. Otherwise, LRWORK  = f2cmax( 7,  N ). */
924 /* >       2. If singular values with the right singular vectors are requested */
925 /* >          i.e. if */
926 /* >          (LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) .AND. */
927 /* >          .NOT.(LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) */
928 /* >          then: */
929 /* >          2.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
930 /* >          then LRWORK = f2cmax( 7, 2 * M ). */
931 /* >          2.2. Otherwise, LRWORK  = f2cmax( 7,  N ). */
932 /* >       3. If singular values with the left singular vectors are requested, i.e. if */
933 /* >          (LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) .AND. */
934 /* >          .NOT.(LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) */
935 /* >          then: */
936 /* >          3.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
937 /* >          then LRWORK = f2cmax( 7, 2 * M ). */
938 /* >          3.2. Otherwise, LRWORK  = f2cmax( 7,  N ). */
939 /* >       4. If singular values with both the left and the right singular vectors */
940 /* >          are requested, i.e. if */
941 /* >          (LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) .AND. */
942 /* >          (LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) */
943 /* >          then: */
944 /* >          4.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
945 /* >          then LRWORK = f2cmax( 7, 2 * M ). */
946 /* >          4.2. Otherwise, LRWORK  = f2cmax( 7, N ). */
947 /* > */
948 /* >          If, on entry, LRWORK = -1 or LWORK=-1, a workspace query is assumed and */
949 /* >          the length of RWORK is returned in RWORK(1). */
950 /* > \endverbatim */
951 /* > */
952 /* > \param[out] IWORK */
953 /* > \verbatim */
954 /* >          IWORK is INTEGER array, of dimension at least 4, that further depends */
955 /* >          on the job: */
956 /* > */
957 /* >          1. If only the singular values are requested then: */
958 /* >             If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
959 /* >             then the length of IWORK is N+M; otherwise the length of IWORK is N. */
960 /* >          2. If the singular values and the right singular vectors are requested then: */
961 /* >             If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
962 /* >             then the length of IWORK is N+M; otherwise the length of IWORK is N. */
963 /* >          3. If the singular values and the left singular vectors are requested then: */
964 /* >             If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
965 /* >             then the length of IWORK is N+M; otherwise the length of IWORK is N. */
966 /* >          4. If the singular values with both the left and the right singular vectors */
967 /* >             are requested, then: */
968 /* >             4.1. If LSAME(JOBV,'J') the length of IWORK is determined as follows: */
969 /* >                  If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
970 /* >                  then the length of IWORK is N+M; otherwise the length of IWORK is N. */
971 /* >             4.2. If LSAME(JOBV,'V') the length of IWORK is determined as follows: */
972 /* >                  If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
973 /* >                  then the length of IWORK is 2*N+M; otherwise the length of IWORK is 2*N. */
974 /* > */
975 /* >          On exit, */
976 /* >          IWORK(1) = the numerical rank determined after the initial */
977 /* >                     QR factorization with pivoting. See the descriptions */
978 /* >                     of JOBA and JOBR. */
979 /* >          IWORK(2) = the number of the computed nonzero singular values */
980 /* >          IWORK(3) = if nonzero, a warning message: */
981 /* >                     If IWORK(3) = 1 then some of the column norms of A */
982 /* >                     were denormalized floats. The requested high accuracy */
983 /* >                     is not warranted by the data. */
984 /* >          IWORK(4) = 1 or -1. If IWORK(4) = 1, then the procedure used A^* to */
985 /* >                     do the job as specified by the JOB parameters. */
986 /* >          If the call to ZGEJSV is a workspace query (indicated by LWORK = -1 or */
987 /* >          LRWORK = -1), then on exit IWORK(1) contains the required length of */
988 /* >          IWORK for the job parameters used in the call. */
989 /* > \endverbatim */
990 /* > */
991 /* > \param[out] INFO */
992 /* > \verbatim */
993 /* >          INFO is INTEGER */
994 /* >           < 0:  if INFO = -i, then the i-th argument had an illegal value. */
995 /* >           = 0:  successful exit; */
996 /* >           > 0:  ZGEJSV  did not converge in the maximal allowed number */
997 /* >                 of sweeps. The computed values may be inaccurate. */
998 /* > \endverbatim */
999
1000 /*  Authors: */
1001 /*  ======== */
1002
1003 /* > \author Univ. of Tennessee */
1004 /* > \author Univ. of California Berkeley */
1005 /* > \author Univ. of Colorado Denver */
1006 /* > \author NAG Ltd. */
1007
1008 /* > \date June 2016 */
1009
1010 /* > \ingroup complex16GEsing */
1011
1012 /* > \par Further Details: */
1013 /*  ===================== */
1014 /* > */
1015 /* > \verbatim */
1016 /* > */
1017 /* >  ZGEJSV implements a preconditioned Jacobi SVD algorithm. It uses ZGEQP3, */
1018 /* >  ZGEQRF, and ZGELQF as preprocessors and preconditioners. Optionally, an */
1019 /* >  additional row pivoting can be used as a preprocessor, which in some */
1020 /* >  cases results in much higher accuracy. An example is matrix A with the */
1021 /* >  structure A = D1 * C * D2, where D1, D2 are arbitrarily ill-conditioned */
1022 /* >  diagonal matrices and C is well-conditioned matrix. In that case, complete */
1023 /* >  pivoting in the first QR factorizations provides accuracy dependent on the */
1024 /* >  condition number of C, and independent of D1, D2. Such higher accuracy is */
1025 /* >  not completely understood theoretically, but it works well in practice. */
1026 /* >  Further, if A can be written as A = B*D, with well-conditioned B and some */
1027 /* >  diagonal D, then the high accuracy is guaranteed, both theoretically and */
1028 /* >  in software, independent of D. For more details see [1], [2]. */
1029 /* >     The computational range for the singular values can be the full range */
1030 /* >  ( UNDERFLOW,OVERFLOW ), provided that the machine arithmetic and the BLAS */
1031 /* >  & LAPACK routines called by ZGEJSV are implemented to work in that range. */
1032 /* >  If that is not the case, then the restriction for safe computation with */
1033 /* >  the singular values in the range of normalized IEEE numbers is that the */
1034 /* >  spectral condition number kappa(A)=sigma_max(A)/sigma_min(A) does not */
1035 /* >  overflow. This code (ZGEJSV) is best used in this restricted range, */
1036 /* >  meaning that singular values of magnitude below ||A||_2 / DLAMCH('O') are */
1037 /* >  returned as zeros. See JOBR for details on this. */
1038 /* >     Further, this implementation is somewhat slower than the one described */
1039 /* >  in [1,2] due to replacement of some non-LAPACK components, and because */
1040 /* >  the choice of some tuning parameters in the iterative part (ZGESVJ) is */
1041 /* >  left to the implementer on a particular machine. */
1042 /* >     The rank revealing QR factorization (in this code: ZGEQP3) should be */
1043 /* >  implemented as in [3]. We have a new version of ZGEQP3 under development */
1044 /* >  that is more robust than the current one in LAPACK, with a cleaner cut in */
1045 /* >  rank deficient cases. It will be available in the SIGMA library [4]. */
1046 /* >  If M is much larger than N, it is obvious that the initial QRF with */
1047 /* >  column pivoting can be preprocessed by the QRF without pivoting. That */
1048 /* >  well known trick is not used in ZGEJSV because in some cases heavy row */
1049 /* >  weighting can be treated with complete pivoting. The overhead in cases */
1050 /* >  M much larger than N is then only due to pivoting, but the benefits in */
1051 /* >  terms of accuracy have prevailed. The implementer/user can incorporate */
1052 /* >  this extra QRF step easily. The implementer can also improve data movement */
1053 /* >  (matrix transpose, matrix copy, matrix transposed copy) - this */
1054 /* >  implementation of ZGEJSV uses only the simplest, naive data movement. */
1055 /* > \endverbatim */
1056
1057 /* > \par Contributor: */
1058 /*  ================== */
1059 /* > */
1060 /* >  Zlatko Drmac, Department of Mathematics, Faculty of Science, */
1061 /* >  University of Zagreb (Zagreb, Croatia); drmac@math.hr */
1062
1063 /* > \par References: */
1064 /*  ================ */
1065 /* > */
1066 /* > \verbatim */
1067 /* > */
1068 /* > [1] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I. */
1069 /* >     SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342. */
1070 /* >     LAPACK Working note 169. */
1071 /* > [2] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II. */
1072 /* >     SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362. */
1073 /* >     LAPACK Working note 170. */
1074 /* > [3] Z. Drmac and Z. Bujanovic: On the failure of rank-revealing QR */
1075 /* >     factorization software - a case study. */
1076 /* >     ACM Trans. Math. Softw. Vol. 35, No 2 (2008), pp. 1-28. */
1077 /* >     LAPACK Working note 176. */
1078 /* > [4] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV, */
1079 /* >     QSVD, (H,K)-SVD computations. */
1080 /* >     Department of Mathematics, University of Zagreb, 2008, 2016. */
1081 /* > \endverbatim */
1082
1083 /* >  \par Bugs, examples and comments: */
1084 /*   ================================= */
1085 /* > */
1086 /* >  Please report all bugs and send interesting examples and/or comments to */
1087 /* >  drmac@math.hr. Thank you. */
1088 /* > */
1089 /*  ===================================================================== */
1090 /* Subroutine */ int zgejsv_(char *joba, char *jobu, char *jobv, char *jobr, 
1091         char *jobt, char *jobp, integer *m, integer *n, doublecomplex *a, 
1092         integer *lda, doublereal *sva, doublecomplex *u, integer *ldu, 
1093         doublecomplex *v, integer *ldv, doublecomplex *cwork, integer *lwork, 
1094         doublereal *rwork, integer *lrwork, integer *iwork, integer *info)
1095 {
1096     /* System generated locals */
1097     integer a_dim1, a_offset, u_dim1, u_offset, v_dim1, v_offset, i__1, i__2, 
1098             i__3, i__4, i__5, i__6, i__7, i__8, i__9, i__10, i__11;
1099     doublereal d__1, d__2, d__3;
1100     doublecomplex z__1;
1101
1102     /* Local variables */
1103     integer lwrk_zgesvj__;
1104     logical defr;
1105     doublereal aapp, aaqq;
1106     integer lwrk_zunmlq__, lwrk_zunmqr__;
1107     logical kill;
1108     integer ierr, lwrk_zgeqp3n__;
1109     doublereal temp1;
1110     integer lwunmqrm, lwqp3, p, q;
1111     logical jracc;
1112     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
1113             integer *);
1114     integer lwrk_zgesvju__, lwrk_zgesvjv__;
1115     extern logical lsame_(char *, char *);
1116     integer lwrk_zunmqrm__;
1117     doublecomplex ctemp;
1118     doublereal entra, small;
1119     integer iwoff;
1120     doublereal sfmin;
1121     logical lsvec;
1122     doublereal epsln;
1123     logical rsvec;
1124     integer lwcon, lwlqf, lwqrf, n1;
1125     extern /* Subroutine */ int zcopy_(integer *, doublecomplex *, integer *, 
1126             doublecomplex *, integer *), zswap_(integer *, doublecomplex *, 
1127             integer *, doublecomplex *, integer *);
1128     logical l2aber;
1129     extern /* Subroutine */ int ztrsm_(char *, char *, char *, char *, 
1130             integer *, integer *, doublecomplex *, doublecomplex *, integer *,
1131              doublecomplex *, integer *);
1132     doublereal condr1, condr2, uscal1, uscal2;
1133     logical l2kill, l2rank, l2tran, l2pert;
1134     extern /* Subroutine */ int zgeqp3_(integer *, integer *, doublecomplex *,
1135              integer *, integer *, doublecomplex *, doublecomplex *, integer *
1136             , doublereal *, integer *);
1137     extern doublereal dznrm2_(integer *, doublecomplex *, integer *);
1138     integer lrwqp3;
1139     extern doublereal dlamch_(char *);
1140     integer nr;
1141     extern /* Subroutine */ int dlascl_(char *, integer *, integer *, 
1142             doublereal *, doublereal *, integer *, integer *, doublereal *, 
1143             integer *, integer *);
1144     extern integer idamax_(integer *, doublereal *, integer *);
1145     doublereal scalem, sconda;
1146     logical goscal;
1147     doublereal aatmin, aatmax;
1148     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
1149     logical noscal;
1150     extern /* Subroutine */ int zdscal_(integer *, doublereal *, 
1151             doublecomplex *, integer *), zlacgv_(integer *, doublecomplex *, 
1152             integer *), dlassq_(integer *, doublereal *, integer *, 
1153             doublereal *, doublereal *);
1154     extern integer izamax_(integer *, doublecomplex *, integer *);
1155     extern /* Subroutine */ int zgelqf_(integer *, integer *, doublecomplex *,
1156              integer *, doublecomplex *, doublecomplex *, integer *, integer *
1157             ), zlascl_(char *, integer *, integer *, doublereal *, doublereal 
1158             *, integer *, integer *, doublecomplex *, integer *, integer *);
1159     doublereal entrat;
1160     logical almort;
1161     doublecomplex cdummy[1];
1162     extern /* Subroutine */ int zgeqrf_(integer *, integer *, doublecomplex *,
1163              integer *, doublecomplex *, doublecomplex *, integer *, integer *
1164             );
1165     doublereal maxprj;
1166     extern /* Subroutine */ int zlacpy_(char *, integer *, integer *, 
1167             doublecomplex *, integer *, doublecomplex *, integer *), 
1168             zlaset_(char *, integer *, integer *, doublecomplex *, 
1169             doublecomplex *, doublecomplex *, integer *);
1170     logical errest;
1171     integer lrwcon;
1172     extern /* Subroutine */ int zlapmr_(logical *, integer *, integer *, 
1173             doublecomplex *, integer *, integer *);
1174     logical transp;
1175     integer minwrk, lwsvdj;
1176     extern /* Subroutine */ int zpocon_(char *, integer *, doublecomplex *, 
1177             integer *, doublereal *, doublereal *, doublecomplex *, 
1178             doublereal *, integer *), zgesvj_(char *, char *, char *, 
1179             integer *, integer *, doublecomplex *, integer *, doublereal *, 
1180             integer *, doublecomplex *, integer *, doublecomplex *, integer *,
1181              doublereal *, integer *, integer *);
1182     doublereal rdummy[1];
1183     extern /* Subroutine */ int zlassq_(integer *, doublecomplex *, integer *,
1184              doublereal *, doublereal *);
1185     logical lquery;
1186     extern /* Subroutine */ int zlaswp_(integer *, doublecomplex *, integer *,
1187              integer *, integer *, integer *, integer *);
1188     logical rowpiv;
1189     integer optwrk;
1190     extern /* Subroutine */ int zungqr_(integer *, integer *, integer *, 
1191             doublecomplex *, integer *, doublecomplex *, doublecomplex *, 
1192             integer *, integer *), zunmlq_(char *, char *, integer *, integer 
1193             *, integer *, doublecomplex *, integer *, doublecomplex *, 
1194             doublecomplex *, integer *, doublecomplex *, integer *, integer *), zunmqr_(char *, char *, integer *, integer *, 
1195             integer *, doublecomplex *, integer *, doublecomplex *, 
1196             doublecomplex *, integer *, doublecomplex *, integer *, integer *);
1197     doublereal big, cond_ok__, xsc;
1198     integer lwrk_zgeqp3__;
1199     doublereal big1;
1200     integer warning, numrank, miniwrk, minrwrk, lrwsvdj, lwunmlq, lwsvdjv, 
1201             lwunmqr, lwrk_zgelqf__, lwrk_zgeqrf__;
1202
1203
1204 /*  -- LAPACK computational routine (version 3.7.1) -- */
1205 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
1206 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
1207 /*     June 2017 */
1208
1209
1210 /*  =========================================================================== */
1211
1212
1213
1214
1215
1216 /*     Test the input arguments */
1217
1218     /* Parameter adjustments */
1219     --sva;
1220     a_dim1 = *lda;
1221     a_offset = 1 + a_dim1 * 1;
1222     a -= a_offset;
1223     u_dim1 = *ldu;
1224     u_offset = 1 + u_dim1 * 1;
1225     u -= u_offset;
1226     v_dim1 = *ldv;
1227     v_offset = 1 + v_dim1 * 1;
1228     v -= v_offset;
1229     --cwork;
1230     --rwork;
1231     --iwork;
1232
1233     /* Function Body */
1234     lsvec = lsame_(jobu, "U") || lsame_(jobu, "F");
1235     jracc = lsame_(jobv, "J");
1236     rsvec = lsame_(jobv, "V") || jracc;
1237     rowpiv = lsame_(joba, "F") || lsame_(joba, "G");
1238     l2rank = lsame_(joba, "R");
1239     l2aber = lsame_(joba, "A");
1240     errest = lsame_(joba, "E") || lsame_(joba, "G");
1241     l2tran = lsame_(jobt, "T") && *m == *n;
1242     l2kill = lsame_(jobr, "R");
1243     defr = lsame_(jobr, "N");
1244     l2pert = lsame_(jobp, "P");
1245
1246     lquery = *lwork == -1 || *lrwork == -1;
1247
1248     if (! (rowpiv || l2rank || l2aber || errest || lsame_(joba, "C"))) {
1249         *info = -1;
1250     } else if (! (lsvec || lsame_(jobu, "N") || lsame_(
1251             jobu, "W") && rsvec && l2tran)) {
1252         *info = -2;
1253     } else if (! (rsvec || lsame_(jobv, "N") || lsame_(
1254             jobv, "W") && lsvec && l2tran)) {
1255         *info = -3;
1256     } else if (! (l2kill || defr)) {
1257         *info = -4;
1258     } else if (! (lsame_(jobt, "T") || lsame_(jobt, 
1259             "N"))) {
1260         *info = -5;
1261     } else if (! (l2pert || lsame_(jobp, "N"))) {
1262         *info = -6;
1263     } else if (*m < 0) {
1264         *info = -7;
1265     } else if (*n < 0 || *n > *m) {
1266         *info = -8;
1267     } else if (*lda < *m) {
1268         *info = -10;
1269     } else if (lsvec && *ldu < *m) {
1270         *info = -13;
1271     } else if (rsvec && *ldv < *n) {
1272         *info = -15;
1273     } else {
1274 /*        #:) */
1275         *info = 0;
1276     }
1277
1278     if (*info == 0) {
1279 /*         [[The expressions for computing the minimal and the optimal */
1280 /*         values of LCWORK, LRWORK are written with a lot of redundancy and */
1281 /*         can be simplified. However, this verbose form is useful for */
1282 /*         maintenance and modifications of the code.]] */
1283
1284 /*         ZGEQRF of an N x N matrix, ZGELQF of an N x N matrix, */
1285 /*         ZUNMLQ for computing N x N matrix, ZUNMQR for computing N x N */
1286 /*         matrix, ZUNMQR for computing M x N matrix, respectively. */
1287         lwqp3 = *n + 1;
1288         lwqrf = f2cmax(1,*n);
1289         lwlqf = f2cmax(1,*n);
1290         lwunmlq = f2cmax(1,*n);
1291         lwunmqr = f2cmax(1,*n);
1292         lwunmqrm = f2cmax(1,*m);
1293         lwcon = *n << 1;
1294 /*         without and with explicit accumulation of Jacobi rotations */
1295 /* Computing MAX */
1296         i__1 = *n << 1;
1297         lwsvdj = f2cmax(i__1,1);
1298 /* Computing MAX */
1299         i__1 = *n << 1;
1300         lwsvdjv = f2cmax(i__1,1);
1301         lrwqp3 = *n << 1;
1302         lrwcon = *n;
1303         lrwsvdj = *n;
1304         if (lquery) {
1305             zgeqp3_(m, n, &a[a_offset], lda, &iwork[1], cdummy, cdummy, &c_n1,
1306                      rdummy, &ierr);
1307             lwrk_zgeqp3__ = (integer) cdummy[0].r;
1308             zgeqrf_(n, n, &a[a_offset], lda, cdummy, cdummy, &c_n1, &ierr);
1309             lwrk_zgeqrf__ = (integer) cdummy[0].r;
1310             zgelqf_(n, n, &a[a_offset], lda, cdummy, cdummy, &c_n1, &ierr);
1311             lwrk_zgelqf__ = (integer) cdummy[0].r;
1312         }
1313         minwrk = 2;
1314         optwrk = 2;
1315         miniwrk = *n;
1316         if (! (lsvec || rsvec)) {
1317 /*             only the singular values are requested */
1318             if (errest) {
1319 /* Computing MAX */
1320 /* Computing 2nd power */
1321                 i__3 = *n;
1322                 i__1 = *n + lwqp3, i__2 = i__3 * i__3 + lwcon, i__1 = f2cmax(
1323                         i__1,i__2), i__2 = *n + lwqrf, i__1 = f2cmax(i__1,i__2);
1324                 minwrk = f2cmax(i__1,lwsvdj);
1325             } else {
1326 /* Computing MAX */
1327                 i__1 = *n + lwqp3, i__2 = *n + lwqrf, i__1 = f2cmax(i__1,i__2);
1328                 minwrk = f2cmax(i__1,lwsvdj);
1329             }
1330             if (lquery) {
1331                 zgesvj_("L", "N", "N", n, n, &a[a_offset], lda, &sva[1], n, &
1332                         v[v_offset], ldv, cdummy, &c_n1, rdummy, &c_n1, &ierr);
1333                 lwrk_zgesvj__ = (integer) cdummy[0].r;
1334                 if (errest) {
1335 /* Computing MAX */
1336 /* Computing 2nd power */
1337                     i__3 = *n;
1338                     i__1 = *n + lwrk_zgeqp3__, i__2 = i__3 * i__3 + lwcon, 
1339                             i__1 = f2cmax(i__1,i__2), i__2 = *n + lwrk_zgeqrf__, 
1340                             i__1 = f2cmax(i__1,i__2);
1341                     optwrk = f2cmax(i__1,lwrk_zgesvj__);
1342                 } else {
1343 /* Computing MAX */
1344                     i__1 = *n + lwrk_zgeqp3__, i__2 = *n + lwrk_zgeqrf__, 
1345                             i__1 = f2cmax(i__1,i__2);
1346                     optwrk = f2cmax(i__1,lwrk_zgesvj__);
1347                 }
1348             }
1349             if (l2tran || rowpiv) {
1350                 if (errest) {
1351 /* Computing MAX */
1352                     i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 = 
1353                             f2cmax(i__1,lrwqp3), i__1 = f2cmax(i__1,lrwcon);
1354                     minrwrk = f2cmax(i__1,lrwsvdj);
1355                 } else {
1356 /* Computing MAX */
1357                     i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 = 
1358                             f2cmax(i__1,lrwqp3);
1359                     minrwrk = f2cmax(i__1,lrwsvdj);
1360                 }
1361             } else {
1362                 if (errest) {
1363 /* Computing MAX */
1364                     i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwcon);
1365                     minrwrk = f2cmax(i__1,lrwsvdj);
1366                 } else {
1367 /* Computing MAX */
1368                     i__1 = f2cmax(7,lrwqp3);
1369                     minrwrk = f2cmax(i__1,lrwsvdj);
1370                 }
1371             }
1372             if (rowpiv || l2tran) {
1373                 miniwrk += *m;
1374             }
1375         } else if (rsvec && ! lsvec) {
1376 /*            singular values and the right singular vectors are requested */
1377             if (errest) {
1378 /* Computing MAX */
1379                 i__1 = *n + lwqp3, i__1 = f2cmax(i__1,lwcon), i__1 = f2cmax(i__1,
1380                         lwsvdj), i__2 = *n + lwlqf, i__1 = f2cmax(i__1,i__2), 
1381                         i__2 = (*n << 1) + lwqrf, i__1 = f2cmax(i__1,i__2), i__2 
1382                         = *n + lwsvdj, i__1 = f2cmax(i__1,i__2), i__2 = *n + 
1383                         lwunmlq;
1384                 minwrk = f2cmax(i__1,i__2);
1385             } else {
1386 /* Computing MAX */
1387                 i__1 = *n + lwqp3, i__1 = f2cmax(i__1,lwsvdj), i__2 = *n + lwlqf,
1388                          i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqrf, 
1389                         i__1 = f2cmax(i__1,i__2), i__2 = *n + lwsvdj, i__1 = f2cmax(
1390                         i__1,i__2), i__2 = *n + lwunmlq;
1391                 minwrk = f2cmax(i__1,i__2);
1392             }
1393             if (lquery) {
1394                 zgesvj_("L", "U", "N", n, n, &u[u_offset], ldu, &sva[1], n, &
1395                         a[a_offset], lda, cdummy, &c_n1, rdummy, &c_n1, &ierr);
1396                 lwrk_zgesvj__ = (integer) cdummy[0].r;
1397                 zunmlq_("L", "C", n, n, n, &a[a_offset], lda, cdummy, &v[
1398                         v_offset], ldv, cdummy, &c_n1, &ierr);
1399                 lwrk_zunmlq__ = (integer) cdummy[0].r;
1400                 if (errest) {
1401 /* Computing MAX */
1402                     i__1 = *n + lwrk_zgeqp3__, i__1 = f2cmax(i__1,lwcon), i__1 = 
1403                             f2cmax(i__1,lwrk_zgesvj__), i__2 = *n + 
1404                             lwrk_zgelqf__, i__1 = f2cmax(i__1,i__2), i__2 = (*n 
1405                             << 1) + lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2), 
1406                             i__2 = *n + lwrk_zgesvj__, i__1 = f2cmax(i__1,i__2), 
1407                             i__2 = *n + lwrk_zunmlq__;
1408                     optwrk = f2cmax(i__1,i__2);
1409                 } else {
1410 /* Computing MAX */
1411                     i__1 = *n + lwrk_zgeqp3__, i__1 = f2cmax(i__1,lwrk_zgesvj__),
1412                              i__2 = *n + lwrk_zgelqf__, i__1 = f2cmax(i__1,i__2),
1413                              i__2 = (*n << 1) + lwrk_zgeqrf__, i__1 = f2cmax(
1414                             i__1,i__2), i__2 = *n + lwrk_zgesvj__, i__1 = f2cmax(
1415                             i__1,i__2), i__2 = *n + lwrk_zunmlq__;
1416                     optwrk = f2cmax(i__1,i__2);
1417                 }
1418             }
1419             if (l2tran || rowpiv) {
1420                 if (errest) {
1421 /* Computing MAX */
1422                     i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 = 
1423                             f2cmax(i__1,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
1424                     minrwrk = f2cmax(i__1,lrwcon);
1425                 } else {
1426 /* Computing MAX */
1427                     i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 = 
1428                             f2cmax(i__1,lrwqp3);
1429                     minrwrk = f2cmax(i__1,lrwsvdj);
1430                 }
1431             } else {
1432                 if (errest) {
1433 /* Computing MAX */
1434                     i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
1435                     minrwrk = f2cmax(i__1,lrwcon);
1436                 } else {
1437 /* Computing MAX */
1438                     i__1 = f2cmax(7,lrwqp3);
1439                     minrwrk = f2cmax(i__1,lrwsvdj);
1440                 }
1441             }
1442             if (rowpiv || l2tran) {
1443                 miniwrk += *m;
1444             }
1445         } else if (lsvec && ! rsvec) {
1446 /*            singular values and the left singular vectors are requested */
1447             if (errest) {
1448 /* Computing MAX */
1449                 i__1 = f2cmax(lwqp3,lwcon), i__2 = *n + lwqrf, i__1 = f2cmax(i__1,
1450                         i__2), i__1 = f2cmax(i__1,lwsvdj);
1451                 minwrk = *n + f2cmax(i__1,lwunmqrm);
1452             } else {
1453 /* Computing MAX */
1454                 i__1 = lwqp3, i__2 = *n + lwqrf, i__1 = f2cmax(i__1,i__2), i__1 =
1455                          f2cmax(i__1,lwsvdj);
1456                 minwrk = *n + f2cmax(i__1,lwunmqrm);
1457             }
1458             if (lquery) {
1459                 zgesvj_("L", "U", "N", n, n, &u[u_offset], ldu, &sva[1], n, &
1460                         a[a_offset], lda, cdummy, &c_n1, rdummy, &c_n1, &ierr);
1461                 lwrk_zgesvj__ = (integer) cdummy[0].r;
1462                 zunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
1463                         u_offset], ldu, cdummy, &c_n1, &ierr);
1464                 lwrk_zunmqrm__ = (integer) cdummy[0].r;
1465                 if (errest) {
1466 /* Computing MAX */
1467                     i__1 = f2cmax(lwrk_zgeqp3__,lwcon), i__2 = *n + 
1468                             lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2), i__1 = f2cmax(
1469                             i__1,lwrk_zgesvj__);
1470                     optwrk = *n + f2cmax(i__1,lwrk_zunmqrm__);
1471                 } else {
1472 /* Computing MAX */
1473                     i__1 = lwrk_zgeqp3__, i__2 = *n + lwrk_zgeqrf__, i__1 = 
1474                             f2cmax(i__1,i__2), i__1 = f2cmax(i__1,lwrk_zgesvj__);
1475                     optwrk = *n + f2cmax(i__1,lwrk_zunmqrm__);
1476                 }
1477             }
1478             if (l2tran || rowpiv) {
1479                 if (errest) {
1480 /* Computing MAX */
1481                     i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 = 
1482                             f2cmax(i__1,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
1483                     minrwrk = f2cmax(i__1,lrwcon);
1484                 } else {
1485 /* Computing MAX */
1486                     i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 = 
1487                             f2cmax(i__1,lrwqp3);
1488                     minrwrk = f2cmax(i__1,lrwsvdj);
1489                 }
1490             } else {
1491                 if (errest) {
1492 /* Computing MAX */
1493                     i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
1494                     minrwrk = f2cmax(i__1,lrwcon);
1495                 } else {
1496 /* Computing MAX */
1497                     i__1 = f2cmax(7,lrwqp3);
1498                     minrwrk = f2cmax(i__1,lrwsvdj);
1499                 }
1500             }
1501             if (rowpiv || l2tran) {
1502                 miniwrk += *m;
1503             }
1504         } else {
1505 /*            full SVD is requested */
1506             if (! jracc) {
1507                 if (errest) {
1508 /* Computing MAX */
1509 /* Computing 2nd power */
1510                     i__3 = *n;
1511 /* Computing 2nd power */
1512                     i__4 = *n;
1513 /* Computing 2nd power */
1514                     i__5 = *n;
1515 /* Computing 2nd power */
1516                     i__6 = *n;
1517 /* Computing 2nd power */
1518                     i__7 = *n;
1519 /* Computing 2nd power */
1520                     i__8 = *n;
1521 /* Computing 2nd power */
1522                     i__9 = *n;
1523 /* Computing 2nd power */
1524                     i__10 = *n;
1525 /* Computing 2nd power */
1526                     i__11 = *n;
1527                     i__1 = *n + lwqp3, i__2 = *n + lwcon, i__1 = f2cmax(i__1,
1528                             i__2), i__2 = (*n << 1) + i__3 * i__3 + lwcon, 
1529                             i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqrf, 
1530                             i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqp3, 
1531                             i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 * 
1532                             i__4 + *n + lwlqf, i__1 = f2cmax(i__1,i__2), i__2 = (
1533                             *n << 1) + i__5 * i__5 + *n + i__6 * i__6 + lwcon,
1534                              i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__7 * 
1535                             i__7 + *n + lwsvdj, i__1 = f2cmax(i__1,i__2), i__2 = 
1536                             (*n << 1) + i__8 * i__8 + *n + lwsvdjv, i__1 = 
1537                             f2cmax(i__1,i__2), i__2 = (*n << 1) + i__9 * i__9 + *
1538                             n + lwunmqr, i__1 = f2cmax(i__1,i__2), i__2 = (*n << 
1539                             1) + i__10 * i__10 + *n + lwunmlq, i__1 = f2cmax(
1540                             i__1,i__2), i__2 = *n + i__11 * i__11 + lwsvdj, 
1541                             i__1 = f2cmax(i__1,i__2), i__2 = *n + lwunmqrm;
1542                     minwrk = f2cmax(i__1,i__2);
1543                 } else {
1544 /* Computing MAX */
1545 /* Computing 2nd power */
1546                     i__3 = *n;
1547 /* Computing 2nd power */
1548                     i__4 = *n;
1549 /* Computing 2nd power */
1550                     i__5 = *n;
1551 /* Computing 2nd power */
1552                     i__6 = *n;
1553 /* Computing 2nd power */
1554                     i__7 = *n;
1555 /* Computing 2nd power */
1556                     i__8 = *n;
1557 /* Computing 2nd power */
1558                     i__9 = *n;
1559 /* Computing 2nd power */
1560                     i__10 = *n;
1561 /* Computing 2nd power */
1562                     i__11 = *n;
1563                     i__1 = *n + lwqp3, i__2 = (*n << 1) + i__3 * i__3 + lwcon,
1564                              i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqrf, 
1565                             i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqp3, 
1566                             i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 * 
1567                             i__4 + *n + lwlqf, i__1 = f2cmax(i__1,i__2), i__2 = (
1568                             *n << 1) + i__5 * i__5 + *n + i__6 * i__6 + lwcon,
1569                              i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__7 * 
1570                             i__7 + *n + lwsvdj, i__1 = f2cmax(i__1,i__2), i__2 = 
1571                             (*n << 1) + i__8 * i__8 + *n + lwsvdjv, i__1 = 
1572                             f2cmax(i__1,i__2), i__2 = (*n << 1) + i__9 * i__9 + *
1573                             n + lwunmqr, i__1 = f2cmax(i__1,i__2), i__2 = (*n << 
1574                             1) + i__10 * i__10 + *n + lwunmlq, i__1 = f2cmax(
1575                             i__1,i__2), i__2 = *n + i__11 * i__11 + lwsvdj, 
1576                             i__1 = f2cmax(i__1,i__2), i__2 = *n + lwunmqrm;
1577                     minwrk = f2cmax(i__1,i__2);
1578                 }
1579                 miniwrk += *n;
1580                 if (rowpiv || l2tran) {
1581                     miniwrk += *m;
1582                 }
1583             } else {
1584                 if (errest) {
1585 /* Computing MAX */
1586 /* Computing 2nd power */
1587                     i__3 = *n;
1588 /* Computing 2nd power */
1589                     i__4 = *n;
1590                     i__1 = *n + lwqp3, i__2 = *n + lwcon, i__1 = f2cmax(i__1,
1591                             i__2), i__2 = (*n << 1) + lwqrf, i__1 = f2cmax(i__1,
1592                             i__2), i__2 = (*n << 1) + i__3 * i__3 + lwsvdjv, 
1593                             i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 * 
1594                             i__4 + *n + lwunmqr, i__1 = f2cmax(i__1,i__2), i__2 =
1595                              *n + lwunmqrm;
1596                     minwrk = f2cmax(i__1,i__2);
1597                 } else {
1598 /* Computing MAX */
1599 /* Computing 2nd power */
1600                     i__3 = *n;
1601 /* Computing 2nd power */
1602                     i__4 = *n;
1603                     i__1 = *n + lwqp3, i__2 = (*n << 1) + lwqrf, i__1 = f2cmax(
1604                             i__1,i__2), i__2 = (*n << 1) + i__3 * i__3 + 
1605                             lwsvdjv, i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) 
1606                             + i__4 * i__4 + *n + lwunmqr, i__1 = f2cmax(i__1,
1607                             i__2), i__2 = *n + lwunmqrm;
1608                     minwrk = f2cmax(i__1,i__2);
1609                 }
1610                 if (rowpiv || l2tran) {
1611                     miniwrk += *m;
1612                 }
1613             }
1614             if (lquery) {
1615                 zunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
1616                         u_offset], ldu, cdummy, &c_n1, &ierr);
1617                 lwrk_zunmqrm__ = (integer) cdummy[0].r;
1618                 zunmqr_("L", "N", n, n, n, &a[a_offset], lda, cdummy, &u[
1619                         u_offset], ldu, cdummy, &c_n1, &ierr);
1620                 lwrk_zunmqr__ = (integer) cdummy[0].r;
1621                 if (! jracc) {
1622                     zgeqp3_(n, n, &a[a_offset], lda, &iwork[1], cdummy, 
1623                             cdummy, &c_n1, rdummy, &ierr);
1624                     lwrk_zgeqp3n__ = (integer) cdummy[0].r;
1625                     zgesvj_("L", "U", "N", n, n, &u[u_offset], ldu, &sva[1], 
1626                             n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
1627                             c_n1, &ierr);
1628                     lwrk_zgesvj__ = (integer) cdummy[0].r;
1629                     zgesvj_("U", "U", "N", n, n, &u[u_offset], ldu, &sva[1], 
1630                             n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
1631                             c_n1, &ierr);
1632                     lwrk_zgesvju__ = (integer) cdummy[0].r;
1633                     zgesvj_("L", "U", "V", n, n, &u[u_offset], ldu, &sva[1], 
1634                             n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
1635                             c_n1, &ierr);
1636                     lwrk_zgesvjv__ = (integer) cdummy[0].r;
1637                     zunmlq_("L", "C", n, n, n, &a[a_offset], lda, cdummy, &v[
1638                             v_offset], ldv, cdummy, &c_n1, &ierr);
1639                     lwrk_zunmlq__ = (integer) cdummy[0].r;
1640                     if (errest) {
1641 /* Computing MAX */
1642 /* Computing 2nd power */
1643                         i__3 = *n;
1644 /* Computing 2nd power */
1645                         i__4 = *n;
1646 /* Computing 2nd power */
1647                         i__5 = *n;
1648 /* Computing 2nd power */
1649                         i__6 = *n;
1650 /* Computing 2nd power */
1651                         i__7 = *n;
1652 /* Computing 2nd power */
1653                         i__8 = *n;
1654 /* Computing 2nd power */
1655                         i__9 = *n;
1656 /* Computing 2nd power */
1657                         i__10 = *n;
1658 /* Computing 2nd power */
1659                         i__11 = *n;
1660                         i__1 = *n + lwrk_zgeqp3__, i__2 = *n + lwcon, i__1 = 
1661                                 f2cmax(i__1,i__2), i__2 = (*n << 1) + i__3 * 
1662                                 i__3 + lwcon, i__1 = f2cmax(i__1,i__2), i__2 = (*
1663                                 n << 1) + lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2)
1664                                 , i__2 = (*n << 1) + lwrk_zgeqp3n__, i__1 = 
1665                                 f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 * 
1666                                 i__4 + *n + lwrk_zgelqf__, i__1 = f2cmax(i__1,
1667                                 i__2), i__2 = (*n << 1) + i__5 * i__5 + *n + 
1668                                 i__6 * i__6 + lwcon, i__1 = f2cmax(i__1,i__2), 
1669                                 i__2 = (*n << 1) + i__7 * i__7 + *n + 
1670                                 lwrk_zgesvj__, i__1 = f2cmax(i__1,i__2), i__2 = (
1671                                 *n << 1) + i__8 * i__8 + *n + lwrk_zgesvjv__, 
1672                                 i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + 
1673                                 i__9 * i__9 + *n + lwrk_zunmqr__, i__1 = f2cmax(
1674                                 i__1,i__2), i__2 = (*n << 1) + i__10 * i__10 
1675                                 + *n + lwrk_zunmlq__, i__1 = f2cmax(i__1,i__2), 
1676                                 i__2 = *n + i__11 * i__11 + lwrk_zgesvju__, 
1677                                 i__1 = f2cmax(i__1,i__2), i__2 = *n + 
1678                                 lwrk_zunmqrm__;
1679                         optwrk = f2cmax(i__1,i__2);
1680                     } else {
1681 /* Computing MAX */
1682 /* Computing 2nd power */
1683                         i__3 = *n;
1684 /* Computing 2nd power */
1685                         i__4 = *n;
1686 /* Computing 2nd power */
1687                         i__5 = *n;
1688 /* Computing 2nd power */
1689                         i__6 = *n;
1690 /* Computing 2nd power */
1691                         i__7 = *n;
1692 /* Computing 2nd power */
1693                         i__8 = *n;
1694 /* Computing 2nd power */
1695                         i__9 = *n;
1696 /* Computing 2nd power */
1697                         i__10 = *n;
1698 /* Computing 2nd power */
1699                         i__11 = *n;
1700                         i__1 = *n + lwrk_zgeqp3__, i__2 = (*n << 1) + i__3 * 
1701                                 i__3 + lwcon, i__1 = f2cmax(i__1,i__2), i__2 = (*
1702                                 n << 1) + lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2)
1703                                 , i__2 = (*n << 1) + lwrk_zgeqp3n__, i__1 = 
1704                                 f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 * 
1705                                 i__4 + *n + lwrk_zgelqf__, i__1 = f2cmax(i__1,
1706                                 i__2), i__2 = (*n << 1) + i__5 * i__5 + *n + 
1707                                 i__6 * i__6 + lwcon, i__1 = f2cmax(i__1,i__2), 
1708                                 i__2 = (*n << 1) + i__7 * i__7 + *n + 
1709                                 lwrk_zgesvj__, i__1 = f2cmax(i__1,i__2), i__2 = (
1710                                 *n << 1) + i__8 * i__8 + *n + lwrk_zgesvjv__, 
1711                                 i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + 
1712                                 i__9 * i__9 + *n + lwrk_zunmqr__, i__1 = f2cmax(
1713                                 i__1,i__2), i__2 = (*n << 1) + i__10 * i__10 
1714                                 + *n + lwrk_zunmlq__, i__1 = f2cmax(i__1,i__2), 
1715                                 i__2 = *n + i__11 * i__11 + lwrk_zgesvju__, 
1716                                 i__1 = f2cmax(i__1,i__2), i__2 = *n + 
1717                                 lwrk_zunmqrm__;
1718                         optwrk = f2cmax(i__1,i__2);
1719                     }
1720                 } else {
1721                     zgesvj_("L", "U", "V", n, n, &u[u_offset], ldu, &sva[1], 
1722                             n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
1723                             c_n1, &ierr);
1724                     lwrk_zgesvjv__ = (integer) cdummy[0].r;
1725                     zunmqr_("L", "N", n, n, n, cdummy, n, cdummy, &v[v_offset]
1726                             , ldv, cdummy, &c_n1, &ierr)
1727                             ;
1728                     lwrk_zunmqr__ = (integer) cdummy[0].r;
1729                     zunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
1730                             u_offset], ldu, cdummy, &c_n1, &ierr);
1731                     lwrk_zunmqrm__ = (integer) cdummy[0].r;
1732                     if (errest) {
1733 /* Computing MAX */
1734 /* Computing 2nd power */
1735                         i__3 = *n;
1736 /* Computing 2nd power */
1737                         i__4 = *n;
1738 /* Computing 2nd power */
1739                         i__5 = *n;
1740                         i__1 = *n + lwrk_zgeqp3__, i__2 = *n + lwcon, i__1 = 
1741                                 f2cmax(i__1,i__2), i__2 = (*n << 1) + 
1742                                 lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2), i__2 = (
1743                                 *n << 1) + i__3 * i__3, i__1 = f2cmax(i__1,i__2),
1744                                  i__2 = (*n << 1) + i__4 * i__4 + 
1745                                 lwrk_zgesvjv__, i__1 = f2cmax(i__1,i__2), i__2 = 
1746                                 (*n << 1) + i__5 * i__5 + *n + lwrk_zunmqr__, 
1747                                 i__1 = f2cmax(i__1,i__2), i__2 = *n + 
1748                                 lwrk_zunmqrm__;
1749                         optwrk = f2cmax(i__1,i__2);
1750                     } else {
1751 /* Computing MAX */
1752 /* Computing 2nd power */
1753                         i__3 = *n;
1754 /* Computing 2nd power */
1755                         i__4 = *n;
1756 /* Computing 2nd power */
1757                         i__5 = *n;
1758                         i__1 = *n + lwrk_zgeqp3__, i__2 = (*n << 1) + 
1759                                 lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2), i__2 = (
1760                                 *n << 1) + i__3 * i__3, i__1 = f2cmax(i__1,i__2),
1761                                  i__2 = (*n << 1) + i__4 * i__4 + 
1762                                 lwrk_zgesvjv__, i__1 = f2cmax(i__1,i__2), i__2 = 
1763                                 (*n << 1) + i__5 * i__5 + *n + lwrk_zunmqr__, 
1764                                 i__1 = f2cmax(i__1,i__2), i__2 = *n + 
1765                                 lwrk_zunmqrm__;
1766                         optwrk = f2cmax(i__1,i__2);
1767                     }
1768                 }
1769             }
1770             if (l2tran || rowpiv) {
1771 /* Computing MAX */
1772                 i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 = f2cmax(
1773                         i__1,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
1774                 minrwrk = f2cmax(i__1,lrwcon);
1775             } else {
1776 /* Computing MAX */
1777                 i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
1778                 minrwrk = f2cmax(i__1,lrwcon);
1779             }
1780         }
1781         minwrk = f2cmax(2,minwrk);
1782         optwrk = f2cmax(minwrk,optwrk);
1783         if (*lwork < minwrk && ! lquery) {
1784             *info = -17;
1785         }
1786         if (*lrwork < minrwrk && ! lquery) {
1787             *info = -19;
1788         }
1789     }
1790
1791     if (*info != 0) {
1792 /*       #:( */
1793         i__1 = -(*info);
1794         xerbla_("ZGEJSV", &i__1, (ftnlen)6);
1795         return 0;
1796     } else if (lquery) {
1797         cwork[1].r = (doublereal) optwrk, cwork[1].i = 0.;
1798         cwork[2].r = (doublereal) minwrk, cwork[2].i = 0.;
1799         rwork[1] = (doublereal) minrwrk;
1800         iwork[1] = f2cmax(4,miniwrk);
1801         return 0;
1802     }
1803
1804 /*     Quick return for void matrix (Y3K safe) */
1805 /* #:) */
1806     if (*m == 0 || *n == 0) {
1807         iwork[1] = 0;
1808         iwork[2] = 0;
1809         iwork[3] = 0;
1810         iwork[4] = 0;
1811         rwork[1] = 0.;
1812         rwork[2] = 0.;
1813         rwork[3] = 0.;
1814         rwork[4] = 0.;
1815         rwork[5] = 0.;
1816         rwork[6] = 0.;
1817         rwork[7] = 0.;
1818         return 0;
1819     }
1820
1821 /*     Determine whether the matrix U should be M x N or M x M */
1822
1823     if (lsvec) {
1824         n1 = *n;
1825         if (lsame_(jobu, "F")) {
1826             n1 = *m;
1827         }
1828     }
1829
1830 /*     Set numerical parameters */
1831
1832 /* !    NOTE: Make sure DLAMCH() does not fail on the target architecture. */
1833
1834     epsln = dlamch_("Epsilon");
1835     sfmin = dlamch_("SafeMinimum");
1836     small = sfmin / epsln;
1837     big = dlamch_("O");
1838 /*     BIG   = ONE / SFMIN */
1839
1840 /*     Initialize SVA(1:N) = diag( ||A e_i||_2 )_1^N */
1841
1842 /* (!)  If necessary, scale SVA() to protect the largest norm from */
1843 /*     overflow. It is possible that this scaling pushes the smallest */
1844 /*     column norm left from the underflow threshold (extreme case). */
1845
1846     scalem = 1. / sqrt((doublereal) (*m) * (doublereal) (*n));
1847     noscal = TRUE_;
1848     goscal = TRUE_;
1849     i__1 = *n;
1850     for (p = 1; p <= i__1; ++p) {
1851         aapp = 0.;
1852         aaqq = 1.;
1853         zlassq_(m, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
1854         if (aapp > big) {
1855             *info = -9;
1856             i__2 = -(*info);
1857             xerbla_("ZGEJSV", &i__2, (ftnlen)6);
1858             return 0;
1859         }
1860         aaqq = sqrt(aaqq);
1861         if (aapp < big / aaqq && noscal) {
1862             sva[p] = aapp * aaqq;
1863         } else {
1864             noscal = FALSE_;
1865             sva[p] = aapp * (aaqq * scalem);
1866             if (goscal) {
1867                 goscal = FALSE_;
1868                 i__2 = p - 1;
1869                 dscal_(&i__2, &scalem, &sva[1], &c__1);
1870             }
1871         }
1872 /* L1874: */
1873     }
1874
1875     if (noscal) {
1876         scalem = 1.;
1877     }
1878
1879     aapp = 0.;
1880     aaqq = big;
1881     i__1 = *n;
1882     for (p = 1; p <= i__1; ++p) {
1883 /* Computing MAX */
1884         d__1 = aapp, d__2 = sva[p];
1885         aapp = f2cmax(d__1,d__2);
1886         if (sva[p] != 0.) {
1887 /* Computing MIN */
1888             d__1 = aaqq, d__2 = sva[p];
1889             aaqq = f2cmin(d__1,d__2);
1890         }
1891 /* L4781: */
1892     }
1893
1894 /*     Quick return for zero M x N matrix */
1895 /* #:) */
1896     if (aapp == 0.) {
1897         if (lsvec) {
1898             zlaset_("G", m, &n1, &c_b1, &c_b2, &u[u_offset], ldu);
1899         }
1900         if (rsvec) {
1901             zlaset_("G", n, n, &c_b1, &c_b2, &v[v_offset], ldv);
1902         }
1903         rwork[1] = 1.;
1904         rwork[2] = 1.;
1905         if (errest) {
1906             rwork[3] = 1.;
1907         }
1908         if (lsvec && rsvec) {
1909             rwork[4] = 1.;
1910             rwork[5] = 1.;
1911         }
1912         if (l2tran) {
1913             rwork[6] = 0.;
1914             rwork[7] = 0.;
1915         }
1916         iwork[1] = 0;
1917         iwork[2] = 0;
1918         iwork[3] = 0;
1919         iwork[4] = -1;
1920         return 0;
1921     }
1922
1923 /*     Issue warning if denormalized column norms detected. Override the */
1924 /*     high relative accuracy request. Issue licence to kill nonzero columns */
1925 /*     (set them to zero) whose norm is less than sigma_max / BIG (roughly). */
1926 /* #:( */
1927     warning = 0;
1928     if (aaqq <= sfmin) {
1929         l2rank = TRUE_;
1930         l2kill = TRUE_;
1931         warning = 1;
1932     }
1933
1934 /*     Quick return for one-column matrix */
1935 /* #:) */
1936     if (*n == 1) {
1937
1938         if (lsvec) {
1939             zlascl_("G", &c__0, &c__0, &sva[1], &scalem, m, &c__1, &a[a_dim1 
1940                     + 1], lda, &ierr);
1941             zlacpy_("A", m, &c__1, &a[a_offset], lda, &u[u_offset], ldu);
1942 /*           computing all M left singular vectors of the M x 1 matrix */
1943             if (n1 != *n) {
1944                 i__1 = *lwork - *n;
1945                 zgeqrf_(m, n, &u[u_offset], ldu, &cwork[1], &cwork[*n + 1], &
1946                         i__1, &ierr);
1947                 i__1 = *lwork - *n;
1948                 zungqr_(m, &n1, &c__1, &u[u_offset], ldu, &cwork[1], &cwork[*
1949                         n + 1], &i__1, &ierr);
1950                 zcopy_(m, &a[a_dim1 + 1], &c__1, &u[u_dim1 + 1], &c__1);
1951             }
1952         }
1953         if (rsvec) {
1954             i__1 = v_dim1 + 1;
1955             v[i__1].r = 1., v[i__1].i = 0.;
1956         }
1957         if (sva[1] < big * scalem) {
1958             sva[1] /= scalem;
1959             scalem = 1.;
1960         }
1961         rwork[1] = 1. / scalem;
1962         rwork[2] = 1.;
1963         if (sva[1] != 0.) {
1964             iwork[1] = 1;
1965             if (sva[1] / scalem >= sfmin) {
1966                 iwork[2] = 1;
1967             } else {
1968                 iwork[2] = 0;
1969             }
1970         } else {
1971             iwork[1] = 0;
1972             iwork[2] = 0;
1973         }
1974         iwork[3] = 0;
1975         iwork[4] = -1;
1976         if (errest) {
1977             rwork[3] = 1.;
1978         }
1979         if (lsvec && rsvec) {
1980             rwork[4] = 1.;
1981             rwork[5] = 1.;
1982         }
1983         if (l2tran) {
1984             rwork[6] = 0.;
1985             rwork[7] = 0.;
1986         }
1987         return 0;
1988
1989     }
1990
1991     transp = FALSE_;
1992
1993     aatmax = -1.;
1994     aatmin = big;
1995     if (rowpiv || l2tran) {
1996
1997 /*     Compute the row norms, needed to determine row pivoting sequence */
1998 /*     (in the case of heavily row weighted A, row pivoting is strongly */
1999 /*     advised) and to collect information needed to compare the */
2000 /*     structures of A * A^* and A^* * A (in the case L2TRAN.EQ..TRUE.). */
2001
2002         if (l2tran) {
2003             i__1 = *m;
2004             for (p = 1; p <= i__1; ++p) {
2005                 xsc = 0.;
2006                 temp1 = 1.;
2007                 zlassq_(n, &a[p + a_dim1], lda, &xsc, &temp1);
2008 /*              ZLASSQ gets both the ell_2 and the ell_infinity norm */
2009 /*              in one pass through the vector */
2010                 rwork[*m + p] = xsc * scalem;
2011                 rwork[p] = xsc * (scalem * sqrt(temp1));
2012 /* Computing MAX */
2013                 d__1 = aatmax, d__2 = rwork[p];
2014                 aatmax = f2cmax(d__1,d__2);
2015                 if (rwork[p] != 0.) {
2016 /* Computing MIN */
2017                     d__1 = aatmin, d__2 = rwork[p];
2018                     aatmin = f2cmin(d__1,d__2);
2019                 }
2020 /* L1950: */
2021             }
2022         } else {
2023             i__1 = *m;
2024             for (p = 1; p <= i__1; ++p) {
2025                 rwork[*m + p] = scalem * z_abs(&a[p + izamax_(n, &a[p + 
2026                         a_dim1], lda) * a_dim1]);
2027 /* Computing MAX */
2028                 d__1 = aatmax, d__2 = rwork[*m + p];
2029                 aatmax = f2cmax(d__1,d__2);
2030 /* Computing MIN */
2031                 d__1 = aatmin, d__2 = rwork[*m + p];
2032                 aatmin = f2cmin(d__1,d__2);
2033 /* L1904: */
2034             }
2035         }
2036
2037     }
2038
2039 /*     For square matrix A try to determine whether A^*  would be better */
2040 /*     input for the preconditioned Jacobi SVD, with faster convergence. */
2041 /*     The decision is based on an O(N) function of the vector of column */
2042 /*     and row norms of A, based on the Shannon entropy. This should give */
2043 /*     the right choice in most cases when the difference actually matters. */
2044 /*     It may fail and pick the slower converging side. */
2045
2046     entra = 0.;
2047     entrat = 0.;
2048     if (l2tran) {
2049
2050         xsc = 0.;
2051         temp1 = 1.;
2052         dlassq_(n, &sva[1], &c__1, &xsc, &temp1);
2053         temp1 = 1. / temp1;
2054
2055         entra = 0.;
2056         i__1 = *n;
2057         for (p = 1; p <= i__1; ++p) {
2058 /* Computing 2nd power */
2059             d__1 = sva[p] / xsc;
2060             big1 = d__1 * d__1 * temp1;
2061             if (big1 != 0.) {
2062                 entra += big1 * log(big1);
2063             }
2064 /* L1113: */
2065         }
2066         entra = -entra / log((doublereal) (*n));
2067
2068 /*        Now, SVA().^2/Trace(A^* * A) is a point in the probability simplex. */
2069 /*        It is derived from the diagonal of  A^* * A.  Do the same with the */
2070 /*        diagonal of A * A^*, compute the entropy of the corresponding */
2071 /*        probability distribution. Note that A * A^* and A^* * A have the */
2072 /*        same trace. */
2073
2074         entrat = 0.;
2075         i__1 = *m;
2076         for (p = 1; p <= i__1; ++p) {
2077 /* Computing 2nd power */
2078             d__1 = rwork[p] / xsc;
2079             big1 = d__1 * d__1 * temp1;
2080             if (big1 != 0.) {
2081                 entrat += big1 * log(big1);
2082             }
2083 /* L1114: */
2084         }
2085         entrat = -entrat / log((doublereal) (*m));
2086
2087 /*        Analyze the entropies and decide A or A^*. Smaller entropy */
2088 /*        usually means better input for the algorithm. */
2089
2090         transp = entrat < entra;
2091
2092 /*        If A^* is better than A, take the adjoint of A. This is allowed */
2093 /*        only for square matrices, M=N. */
2094         if (transp) {
2095 /*           In an optimal implementation, this trivial transpose */
2096 /*           should be replaced with faster transpose. */
2097             i__1 = *n - 1;
2098             for (p = 1; p <= i__1; ++p) {
2099                 i__2 = p + p * a_dim1;
2100                 d_cnjg(&z__1, &a[p + p * a_dim1]);
2101                 a[i__2].r = z__1.r, a[i__2].i = z__1.i;
2102                 i__2 = *n;
2103                 for (q = p + 1; q <= i__2; ++q) {
2104                     d_cnjg(&z__1, &a[q + p * a_dim1]);
2105                     ctemp.r = z__1.r, ctemp.i = z__1.i;
2106                     i__3 = q + p * a_dim1;
2107                     d_cnjg(&z__1, &a[p + q * a_dim1]);
2108                     a[i__3].r = z__1.r, a[i__3].i = z__1.i;
2109                     i__3 = p + q * a_dim1;
2110                     a[i__3].r = ctemp.r, a[i__3].i = ctemp.i;
2111 /* L1116: */
2112                 }
2113 /* L1115: */
2114             }
2115             i__1 = *n + *n * a_dim1;
2116             d_cnjg(&z__1, &a[*n + *n * a_dim1]);
2117             a[i__1].r = z__1.r, a[i__1].i = z__1.i;
2118             i__1 = *n;
2119             for (p = 1; p <= i__1; ++p) {
2120                 rwork[*m + p] = sva[p];
2121                 sva[p] = rwork[p];
2122 /*              previously computed row 2-norms are now column 2-norms */
2123 /*              of the transposed matrix */
2124 /* L1117: */
2125             }
2126             temp1 = aapp;
2127             aapp = aatmax;
2128             aatmax = temp1;
2129             temp1 = aaqq;
2130             aaqq = aatmin;
2131             aatmin = temp1;
2132             kill = lsvec;
2133             lsvec = rsvec;
2134             rsvec = kill;
2135             if (lsvec) {
2136                 n1 = *n;
2137             }
2138
2139             rowpiv = TRUE_;
2140         }
2141
2142     }
2143 /*     END IF L2TRAN */
2144
2145 /*     Scale the matrix so that its maximal singular value remains less */
2146 /*     than SQRT(BIG) -- the matrix is scaled so that its maximal column */
2147 /*     has Euclidean norm equal to SQRT(BIG/N). The only reason to keep */
2148 /*     SQRT(BIG) instead of BIG is the fact that ZGEJSV uses LAPACK and */
2149 /*     BLAS routines that, in some implementations, are not capable of */
2150 /*     working in the full interval [SFMIN,BIG] and that they may provoke */
2151 /*     overflows in the intermediate results. If the singular values spread */
2152 /*     from SFMIN to BIG, then ZGESVJ will compute them. So, in that case, */
2153 /*     one should use ZGESVJ instead of ZGEJSV. */
2154 /*     >> change in the April 2016 update: allow bigger range, i.e. the */
2155 /*     largest column is allowed up to BIG/N and ZGESVJ will do the rest. */
2156     big1 = sqrt(big);
2157     temp1 = sqrt(big / (doublereal) (*n));
2158 /*      TEMP1  = BIG/DBLE(N) */
2159
2160     dlascl_("G", &c__0, &c__0, &aapp, &temp1, n, &c__1, &sva[1], n, &ierr);
2161     if (aaqq > aapp * sfmin) {
2162         aaqq = aaqq / aapp * temp1;
2163     } else {
2164         aaqq = aaqq * temp1 / aapp;
2165     }
2166     temp1 *= scalem;
2167     zlascl_("G", &c__0, &c__0, &aapp, &temp1, m, n, &a[a_offset], lda, &ierr);
2168
2169 /*     To undo scaling at the end of this procedure, multiply the */
2170 /*     computed singular values with USCAL2 / USCAL1. */
2171
2172     uscal1 = temp1;
2173     uscal2 = aapp;
2174
2175     if (l2kill) {
2176 /*        L2KILL enforces computation of nonzero singular values in */
2177 /*        the restricted range of condition number of the initial A, */
2178 /*        sigma_max(A) / sigma_min(A) approx. SQRT(BIG)/SQRT(SFMIN). */
2179         xsc = sqrt(sfmin);
2180     } else {
2181         xsc = small;
2182
2183 /*        Now, if the condition number of A is too big, */
2184 /*        sigma_max(A) / sigma_min(A) .GT. SQRT(BIG/N) * EPSLN / SFMIN, */
2185 /*        as a precaution measure, the full SVD is computed using ZGESVJ */
2186 /*        with accumulated Jacobi rotations. This provides numerically */
2187 /*        more robust computation, at the cost of slightly increased run */
2188 /*        time. Depending on the concrete implementation of BLAS and LAPACK */
2189 /*        (i.e. how they behave in presence of extreme ill-conditioning) the */
2190 /*        implementor may decide to remove this switch. */
2191         if (aaqq < sqrt(sfmin) && lsvec && rsvec) {
2192             jracc = TRUE_;
2193         }
2194
2195     }
2196     if (aaqq < xsc) {
2197         i__1 = *n;
2198         for (p = 1; p <= i__1; ++p) {
2199             if (sva[p] < xsc) {
2200                 zlaset_("A", m, &c__1, &c_b1, &c_b1, &a[p * a_dim1 + 1], lda);
2201                 sva[p] = 0.;
2202             }
2203 /* L700: */
2204         }
2205     }
2206
2207 /*     Preconditioning using QR factorization with pivoting */
2208
2209     if (rowpiv) {
2210 /*        Optional row permutation (Bjoerck row pivoting): */
2211 /*        A result by Cox and Higham shows that the Bjoerck's */
2212 /*        row pivoting combined with standard column pivoting */
2213 /*        has similar effect as Powell-Reid complete pivoting. */
2214 /*        The ell-infinity norms of A are made nonincreasing. */
2215         if (lsvec && rsvec && ! jracc) {
2216             iwoff = *n << 1;
2217         } else {
2218             iwoff = *n;
2219         }
2220         i__1 = *m - 1;
2221         for (p = 1; p <= i__1; ++p) {
2222             i__2 = *m - p + 1;
2223             q = idamax_(&i__2, &rwork[*m + p], &c__1) + p - 1;
2224             iwork[iwoff + p] = q;
2225             if (p != q) {
2226                 temp1 = rwork[*m + p];
2227                 rwork[*m + p] = rwork[*m + q];
2228                 rwork[*m + q] = temp1;
2229             }
2230 /* L1952: */
2231         }
2232         i__1 = *m - 1;
2233         zlaswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[iwoff + 1], &c__1);
2234     }
2235
2236 /*     End of the preparation phase (scaling, optional sorting and */
2237 /*     transposing, optional flushing of small columns). */
2238
2239 /*     Preconditioning */
2240
2241 /*     If the full SVD is needed, the right singular vectors are computed */
2242 /*     from a matrix equation, and for that we need theoretical analysis */
2243 /*     of the Businger-Golub pivoting. So we use ZGEQP3 as the first RR QRF. */
2244 /*     In all other cases the first RR QRF can be chosen by other criteria */
2245 /*     (eg speed by replacing global with restricted window pivoting, such */
2246 /*     as in xGEQPX from TOMS # 782). Good results will be obtained using */
2247 /*     xGEQPX with properly (!) chosen numerical parameters. */
2248 /*     Any improvement of ZGEQP3 improves overal performance of ZGEJSV. */
2249
2250 /*     A * P1 = Q1 * [ R1^* 0]^*: */
2251     i__1 = *n;
2252     for (p = 1; p <= i__1; ++p) {
2253         iwork[p] = 0;
2254 /* L1963: */
2255     }
2256     i__1 = *lwork - *n;
2257     zgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &cwork[1], &cwork[*n + 1], &
2258             i__1, &rwork[1], &ierr);
2259
2260 /*     The upper triangular matrix R1 from the first QRF is inspected for */
2261 /*     rank deficiency and possibilities for deflation, or possible */
2262 /*     ill-conditioning. Depending on the user specified flag L2RANK, */
2263 /*     the procedure explores possibilities to reduce the numerical */
2264 /*     rank by inspecting the computed upper triangular factor. If */
2265 /*     L2RANK or L2ABER are up, then ZGEJSV will compute the SVD of */
2266 /*     A + dA, where ||dA|| <= f(M,N)*EPSLN. */
2267
2268     nr = 1;
2269     if (l2aber) {
2270 /*        Standard absolute error bound suffices. All sigma_i with */
2271 /*        sigma_i < N*EPSLN*||A|| are flushed to zero. This is an */
2272 /*        aggressive enforcement of lower numerical rank by introducing a */
2273 /*        backward error of the order of N*EPSLN*||A||. */
2274         temp1 = sqrt((doublereal) (*n)) * epsln;
2275         i__1 = *n;
2276         for (p = 2; p <= i__1; ++p) {
2277             if (z_abs(&a[p + p * a_dim1]) >= temp1 * z_abs(&a[a_dim1 + 1])) {
2278                 ++nr;
2279             } else {
2280                 goto L3002;
2281             }
2282 /* L3001: */
2283         }
2284 L3002:
2285         ;
2286     } else if (l2rank) {
2287 /*        Sudden drop on the diagonal of R1 is used as the criterion for */
2288 /*        close-to-rank-deficient. */
2289         temp1 = sqrt(sfmin);
2290         i__1 = *n;
2291         for (p = 2; p <= i__1; ++p) {
2292             if (z_abs(&a[p + p * a_dim1]) < epsln * z_abs(&a[p - 1 + (p - 1) *
2293                      a_dim1]) || z_abs(&a[p + p * a_dim1]) < small || l2kill 
2294                     && z_abs(&a[p + p * a_dim1]) < temp1) {
2295                 goto L3402;
2296             }
2297             ++nr;
2298 /* L3401: */
2299         }
2300 L3402:
2301
2302         ;
2303     } else {
2304 /*        The goal is high relative accuracy. However, if the matrix */
2305 /*        has high scaled condition number the relative accuracy is in */
2306 /*        general not feasible. Later on, a condition number estimator */
2307 /*        will be deployed to estimate the scaled condition number. */
2308 /*        Here we just remove the underflowed part of the triangular */
2309 /*        factor. This prevents the situation in which the code is */
2310 /*        working hard to get the accuracy not warranted by the data. */
2311         temp1 = sqrt(sfmin);
2312         i__1 = *n;
2313         for (p = 2; p <= i__1; ++p) {
2314             if (z_abs(&a[p + p * a_dim1]) < small || l2kill && z_abs(&a[p + p 
2315                     * a_dim1]) < temp1) {
2316                 goto L3302;
2317             }
2318             ++nr;
2319 /* L3301: */
2320         }
2321 L3302:
2322
2323         ;
2324     }
2325
2326     almort = FALSE_;
2327     if (nr == *n) {
2328         maxprj = 1.;
2329         i__1 = *n;
2330         for (p = 2; p <= i__1; ++p) {
2331             temp1 = z_abs(&a[p + p * a_dim1]) / sva[iwork[p]];
2332             maxprj = f2cmin(maxprj,temp1);
2333 /* L3051: */
2334         }
2335 /* Computing 2nd power */
2336         d__1 = maxprj;
2337         if (d__1 * d__1 >= 1. - (doublereal) (*n) * epsln) {
2338             almort = TRUE_;
2339         }
2340     }
2341
2342
2343     sconda = -1.;
2344     condr1 = -1.;
2345     condr2 = -1.;
2346
2347     if (errest) {
2348         if (*n == nr) {
2349             if (rsvec) {
2350                 zlacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
2351                 i__1 = *n;
2352                 for (p = 1; p <= i__1; ++p) {
2353                     temp1 = sva[iwork[p]];
2354                     d__1 = 1. / temp1;
2355                     zdscal_(&p, &d__1, &v[p * v_dim1 + 1], &c__1);
2356 /* L3053: */
2357                 }
2358                 if (lsvec) {
2359                     zpocon_("U", n, &v[v_offset], ldv, &c_b141, &temp1, &
2360                             cwork[*n + 1], &rwork[1], &ierr);
2361                 } else {
2362                     zpocon_("U", n, &v[v_offset], ldv, &c_b141, &temp1, &
2363                             cwork[1], &rwork[1], &ierr);
2364                 }
2365
2366             } else if (lsvec) {
2367                 zlacpy_("U", n, n, &a[a_offset], lda, &u[u_offset], ldu);
2368                 i__1 = *n;
2369                 for (p = 1; p <= i__1; ++p) {
2370                     temp1 = sva[iwork[p]];
2371                     d__1 = 1. / temp1;
2372                     zdscal_(&p, &d__1, &u[p * u_dim1 + 1], &c__1);
2373 /* L3054: */
2374                 }
2375                 zpocon_("U", n, &u[u_offset], ldu, &c_b141, &temp1, &cwork[*n 
2376                         + 1], &rwork[1], &ierr);
2377             } else {
2378                 zlacpy_("U", n, n, &a[a_offset], lda, &cwork[1], n)
2379                         ;
2380 /* []            CALL ZLACPY( 'U', N, N, A, LDA, CWORK(N+1), N ) */
2381 /*              Change: here index shifted by N to the left, CWORK(1:N) */
2382 /*              not needed for SIGMA only computation */
2383                 i__1 = *n;
2384                 for (p = 1; p <= i__1; ++p) {
2385                     temp1 = sva[iwork[p]];
2386 /* []               CALL ZDSCAL( p, ONE/TEMP1, CWORK(N+(p-1)*N+1), 1 ) */
2387                     d__1 = 1. / temp1;
2388                     zdscal_(&p, &d__1, &cwork[(p - 1) * *n + 1], &c__1);
2389 /* L3052: */
2390                 }
2391 /* []               CALL ZPOCON( 'U', N, CWORK(N+1), N, ONE, TEMP1, */
2392 /* []     $              CWORK(N+N*N+1), RWORK, IERR ) */
2393                 zpocon_("U", n, &cwork[1], n, &c_b141, &temp1, &cwork[*n * *n 
2394                         + 1], &rwork[1], &ierr);
2395
2396             }
2397             if (temp1 != 0.) {
2398                 sconda = 1. / sqrt(temp1);
2399             } else {
2400                 sconda = -1.;
2401             }
2402 /*           SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1). */
2403 /*           N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
2404         } else {
2405             sconda = -1.;
2406         }
2407     }
2408
2409     z_div(&z__1, &a[a_dim1 + 1], &a[nr + nr * a_dim1]);
2410     l2pert = l2pert && z_abs(&z__1) > sqrt(big1);
2411 /*     If there is no violent scaling, artificial perturbation is not needed. */
2412
2413 /*     Phase 3: */
2414
2415     if (! (rsvec || lsvec)) {
2416
2417 /*         Singular Values only */
2418
2419 /* Computing MIN */
2420         i__2 = *n - 1;
2421         i__1 = f2cmin(i__2,nr);
2422         for (p = 1; p <= i__1; ++p) {
2423             i__2 = *n - p;
2424             zcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p * 
2425                     a_dim1], &c__1);
2426             i__2 = *n - p + 1;
2427             zlacgv_(&i__2, &a[p + p * a_dim1], &c__1);
2428 /* L1946: */
2429         }
2430         if (nr == *n) {
2431             i__1 = *n + *n * a_dim1;
2432             d_cnjg(&z__1, &a[*n + *n * a_dim1]);
2433             a[i__1].r = z__1.r, a[i__1].i = z__1.i;
2434         }
2435
2436 /*        The following two DO-loops introduce small relative perturbation */
2437 /*        into the strict upper triangle of the lower triangular matrix. */
2438 /*        Small entries below the main diagonal are also changed. */
2439 /*        This modification is useful if the computing environment does not */
2440 /*        provide/allow FLUSH TO ZERO underflow, for it prevents many */
2441 /*        annoying denormalized numbers in case of strongly scaled matrices. */
2442 /*        The perturbation is structured so that it does not introduce any */
2443 /*        new perturbation of the singular values, and it does not destroy */
2444 /*        the job done by the preconditioner. */
2445 /*        The licence for this perturbation is in the variable L2PERT, which */
2446 /*        should be .FALSE. if FLUSH TO ZERO underflow is active. */
2447
2448         if (! almort) {
2449
2450             if (l2pert) {
2451 /*              XSC = SQRT(SMALL) */
2452                 xsc = epsln / (doublereal) (*n);
2453                 i__1 = nr;
2454                 for (q = 1; q <= i__1; ++q) {
2455                     d__1 = xsc * z_abs(&a[q + q * a_dim1]);
2456                     z__1.r = d__1, z__1.i = 0.;
2457                     ctemp.r = z__1.r, ctemp.i = z__1.i;
2458                     i__2 = *n;
2459                     for (p = 1; p <= i__2; ++p) {
2460                         if (p > q && z_abs(&a[p + q * a_dim1]) <= temp1 || p <
2461                                  q) {
2462                             i__3 = p + q * a_dim1;
2463                             a[i__3].r = ctemp.r, a[i__3].i = ctemp.i;
2464                         }
2465 /*     $                     A(p,q) = TEMP1 * ( A(p,q) / ABS(A(p,q)) ) */
2466 /* L4949: */
2467                     }
2468 /* L4947: */
2469                 }
2470             } else {
2471                 i__1 = nr - 1;
2472                 i__2 = nr - 1;
2473                 zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &a[(a_dim1 << 1) + 1]
2474                         , lda);
2475             }
2476
2477
2478             i__1 = *lwork - *n;
2479             zgeqrf_(n, &nr, &a[a_offset], lda, &cwork[1], &cwork[*n + 1], &
2480                     i__1, &ierr);
2481
2482             i__1 = nr - 1;
2483             for (p = 1; p <= i__1; ++p) {
2484                 i__2 = nr - p;
2485                 zcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p * 
2486                         a_dim1], &c__1);
2487                 i__2 = nr - p + 1;
2488                 zlacgv_(&i__2, &a[p + p * a_dim1], &c__1);
2489 /* L1948: */
2490             }
2491
2492         }
2493
2494 /*           Row-cyclic Jacobi SVD algorithm with column pivoting */
2495
2496 /*           to drown denormals */
2497         if (l2pert) {
2498 /*              XSC = SQRT(SMALL) */
2499             xsc = epsln / (doublereal) (*n);
2500             i__1 = nr;
2501             for (q = 1; q <= i__1; ++q) {
2502                 d__1 = xsc * z_abs(&a[q + q * a_dim1]);
2503                 z__1.r = d__1, z__1.i = 0.;
2504                 ctemp.r = z__1.r, ctemp.i = z__1.i;
2505                 i__2 = nr;
2506                 for (p = 1; p <= i__2; ++p) {
2507                     if (p > q && z_abs(&a[p + q * a_dim1]) <= temp1 || p < q) 
2508                             {
2509                         i__3 = p + q * a_dim1;
2510                         a[i__3].r = ctemp.r, a[i__3].i = ctemp.i;
2511                     }
2512 /*     $                   A(p,q) = TEMP1 * ( A(p,q) / ABS(A(p,q)) ) */
2513 /* L1949: */
2514                 }
2515 /* L1947: */
2516             }
2517         } else {
2518             i__1 = nr - 1;
2519             i__2 = nr - 1;
2520             zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &a[(a_dim1 << 1) + 1], 
2521                     lda);
2522         }
2523
2524 /*           triangular matrix (plus perturbation which is ignored in */
2525 /*           the part which destroys triangular form (confusing?!)) */
2526
2527         zgesvj_("L", "N", "N", &nr, &nr, &a[a_offset], lda, &sva[1], n, &v[
2528                 v_offset], ldv, &cwork[1], lwork, &rwork[1], lrwork, info);
2529
2530         scalem = rwork[1];
2531         numrank = i_dnnt(&rwork[2]);
2532
2533
2534     } else if (rsvec && ! lsvec && ! jracc || jracc && ! lsvec && nr != *n) {
2535
2536 /*        -> Singular Values and Right Singular Vectors <- */
2537
2538         if (almort) {
2539
2540             i__1 = nr;
2541             for (p = 1; p <= i__1; ++p) {
2542                 i__2 = *n - p + 1;
2543                 zcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
2544                         c__1);
2545                 i__2 = *n - p + 1;
2546                 zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
2547 /* L1998: */
2548             }
2549             i__1 = nr - 1;
2550             i__2 = nr - 1;
2551             zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1], 
2552                     ldv);
2553
2554             zgesvj_("L", "U", "N", n, &nr, &v[v_offset], ldv, &sva[1], &nr, &
2555                     a[a_offset], lda, &cwork[1], lwork, &rwork[1], lrwork, 
2556                     info);
2557             scalem = rwork[1];
2558             numrank = i_dnnt(&rwork[2]);
2559         } else {
2560
2561 /*        accumulated product of Jacobi rotations, three are perfect ) */
2562
2563             i__1 = nr - 1;
2564             i__2 = nr - 1;
2565             zlaset_("L", &i__1, &i__2, &c_b1, &c_b1, &a[a_dim1 + 2], lda);
2566             i__1 = *lwork - *n;
2567             zgelqf_(&nr, n, &a[a_offset], lda, &cwork[1], &cwork[*n + 1], &
2568                     i__1, &ierr);
2569             zlacpy_("L", &nr, &nr, &a[a_offset], lda, &v[v_offset], ldv);
2570             i__1 = nr - 1;
2571             i__2 = nr - 1;
2572             zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1], 
2573                     ldv);
2574             i__1 = *lwork - (*n << 1);
2575             zgeqrf_(&nr, &nr, &v[v_offset], ldv, &cwork[*n + 1], &cwork[(*n <<
2576                      1) + 1], &i__1, &ierr);
2577             i__1 = nr;
2578             for (p = 1; p <= i__1; ++p) {
2579                 i__2 = nr - p + 1;
2580                 zcopy_(&i__2, &v[p + p * v_dim1], ldv, &v[p + p * v_dim1], &
2581                         c__1);
2582                 i__2 = nr - p + 1;
2583                 zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
2584 /* L8998: */
2585             }
2586             i__1 = nr - 1;
2587             i__2 = nr - 1;
2588             zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1], 
2589                     ldv);
2590
2591             i__1 = *lwork - *n;
2592             zgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[1], &nr, 
2593                     &u[u_offset], ldu, &cwork[*n + 1], &i__1, &rwork[1], 
2594                     lrwork, info);
2595             scalem = rwork[1];
2596             numrank = i_dnnt(&rwork[2]);
2597             if (nr < *n) {
2598                 i__1 = *n - nr;
2599                 zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + v_dim1], 
2600                         ldv);
2601                 i__1 = *n - nr;
2602                 zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * v_dim1 + 
2603                         1], ldv);
2604                 i__1 = *n - nr;
2605                 i__2 = *n - nr;
2606                 zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (nr + 1) 
2607                         * v_dim1], ldv);
2608             }
2609
2610             i__1 = *lwork - *n;
2611             zunmlq_("L", "C", n, n, &nr, &a[a_offset], lda, &cwork[1], &v[
2612                     v_offset], ldv, &cwork[*n + 1], &i__1, &ierr);
2613
2614         }
2615 /*         DO 8991 p = 1, N */
2616 /*            CALL ZCOPY( N, V(p,1), LDV, A(IWORK(p),1), LDA ) */
2617 /* 8991    CONTINUE */
2618 /*         CALL ZLACPY( 'All', N, N, A, LDA, V, LDV ) */
2619         zlapmr_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
2620
2621         if (transp) {
2622             zlacpy_("A", n, n, &v[v_offset], ldv, &u[u_offset], ldu);
2623         }
2624
2625     } else if (jracc && ! lsvec && nr == *n) {
2626
2627         i__1 = *n - 1;
2628         i__2 = *n - 1;
2629         zlaset_("L", &i__1, &i__2, &c_b1, &c_b1, &a[a_dim1 + 2], lda);
2630
2631         zgesvj_("U", "N", "V", n, n, &a[a_offset], lda, &sva[1], n, &v[
2632                 v_offset], ldv, &cwork[1], lwork, &rwork[1], lrwork, info);
2633         scalem = rwork[1];
2634         numrank = i_dnnt(&rwork[2]);
2635         zlapmr_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
2636
2637     } else if (lsvec && ! rsvec) {
2638
2639
2640 /*        Jacobi rotations in the Jacobi iterations. */
2641         i__1 = nr;
2642         for (p = 1; p <= i__1; ++p) {
2643             i__2 = *n - p + 1;
2644             zcopy_(&i__2, &a[p + p * a_dim1], lda, &u[p + p * u_dim1], &c__1);
2645             i__2 = *n - p + 1;
2646             zlacgv_(&i__2, &u[p + p * u_dim1], &c__1);
2647 /* L1965: */
2648         }
2649         i__1 = nr - 1;
2650         i__2 = nr - 1;
2651         zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1], ldu);
2652
2653         i__1 = *lwork - (*n << 1);
2654         zgeqrf_(n, &nr, &u[u_offset], ldu, &cwork[*n + 1], &cwork[(*n << 1) + 
2655                 1], &i__1, &ierr);
2656
2657         i__1 = nr - 1;
2658         for (p = 1; p <= i__1; ++p) {
2659             i__2 = nr - p;
2660             zcopy_(&i__2, &u[p + (p + 1) * u_dim1], ldu, &u[p + 1 + p * 
2661                     u_dim1], &c__1);
2662             i__2 = *n - p + 1;
2663             zlacgv_(&i__2, &u[p + p * u_dim1], &c__1);
2664 /* L1967: */
2665         }
2666         i__1 = nr - 1;
2667         i__2 = nr - 1;
2668         zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1], ldu);
2669
2670         i__1 = *lwork - *n;
2671         zgesvj_("L", "U", "N", &nr, &nr, &u[u_offset], ldu, &sva[1], &nr, &a[
2672                 a_offset], lda, &cwork[*n + 1], &i__1, &rwork[1], lrwork, 
2673                 info);
2674         scalem = rwork[1];
2675         numrank = i_dnnt(&rwork[2]);
2676
2677         if (nr < *m) {
2678             i__1 = *m - nr;
2679             zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1], ldu);
2680             if (nr < n1) {
2681                 i__1 = n1 - nr;
2682                 zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) * u_dim1 + 
2683                         1], ldu);
2684                 i__1 = *m - nr;
2685                 i__2 = n1 - nr;
2686                 zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (nr + 1) 
2687                         * u_dim1], ldu);
2688             }
2689         }
2690
2691         i__1 = *lwork - *n;
2692         zunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
2693                 u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
2694
2695         if (rowpiv) {
2696             i__1 = *m - 1;
2697             zlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[iwoff + 1], &
2698                     c_n1);
2699         }
2700
2701         i__1 = n1;
2702         for (p = 1; p <= i__1; ++p) {
2703             xsc = 1. / dznrm2_(m, &u[p * u_dim1 + 1], &c__1);
2704             zdscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
2705 /* L1974: */
2706         }
2707
2708         if (transp) {
2709             zlacpy_("A", n, n, &u[u_offset], ldu, &v[v_offset], ldv);
2710         }
2711
2712     } else {
2713
2714
2715         if (! jracc) {
2716
2717             if (! almort) {
2718
2719 /*           Second Preconditioning Step (QRF [with pivoting]) */
2720 /*           Note that the composition of TRANSPOSE, QRF and TRANSPOSE is */
2721 /*           equivalent to an LQF CALL. Since in many libraries the QRF */
2722 /*           seems to be better optimized than the LQF, we do explicit */
2723 /*           transpose and use the QRF. This is subject to changes in an */
2724 /*           optimized implementation of ZGEJSV. */
2725
2726                 i__1 = nr;
2727                 for (p = 1; p <= i__1; ++p) {
2728                     i__2 = *n - p + 1;
2729                     zcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1],
2730                              &c__1);
2731                     i__2 = *n - p + 1;
2732                     zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
2733 /* L1968: */
2734                 }
2735
2736 /*           denormals in the second QR factorization, where they are */
2737 /*           as good as zeros. This is done to avoid painfully slow */
2738 /*           computation with denormals. The relative size of the perturbation */
2739 /*           is a parameter that can be changed by the implementer. */
2740 /*           This perturbation device will be obsolete on machines with */
2741 /*           properly implemented arithmetic. */
2742 /*           To switch it off, set L2PERT=.FALSE. To remove it from  the */
2743 /*           code, remove the action under L2PERT=.TRUE., leave the ELSE part. */
2744 /*           The following two loops should be blocked and fused with the */
2745 /*           transposed copy above. */
2746
2747                 if (l2pert) {
2748                     xsc = sqrt(small);
2749                     i__1 = nr;
2750                     for (q = 1; q <= i__1; ++q) {
2751                         d__1 = xsc * z_abs(&v[q + q * v_dim1]);
2752                         z__1.r = d__1, z__1.i = 0.;
2753                         ctemp.r = z__1.r, ctemp.i = z__1.i;
2754                         i__2 = *n;
2755                         for (p = 1; p <= i__2; ++p) {
2756                             if (p > q && z_abs(&v[p + q * v_dim1]) <= temp1 ||
2757                                      p < q) {
2758                                 i__3 = p + q * v_dim1;
2759                                 v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
2760                             }
2761 /*     $                   V(p,q) = TEMP1 * ( V(p,q) / ABS(V(p,q)) ) */
2762                             if (p < q) {
2763                                 i__3 = p + q * v_dim1;
2764                                 i__4 = p + q * v_dim1;
2765                                 z__1.r = -v[i__4].r, z__1.i = -v[i__4].i;
2766                                 v[i__3].r = z__1.r, v[i__3].i = z__1.i;
2767                             }
2768 /* L2968: */
2769                         }
2770 /* L2969: */
2771                     }
2772                 } else {
2773                     i__1 = nr - 1;
2774                     i__2 = nr - 1;
2775                     zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) 
2776                             + 1], ldv);
2777                 }
2778
2779 /*           Estimate the row scaled condition number of R1 */
2780 /*           (If R1 is rectangular, N > NR, then the condition number */
2781 /*           of the leading NR x NR submatrix is estimated.) */
2782
2783                 zlacpy_("L", &nr, &nr, &v[v_offset], ldv, &cwork[(*n << 1) + 
2784                         1], &nr);
2785                 i__1 = nr;
2786                 for (p = 1; p <= i__1; ++p) {
2787                     i__2 = nr - p + 1;
2788                     temp1 = dznrm2_(&i__2, &cwork[(*n << 1) + (p - 1) * nr + 
2789                             p], &c__1);
2790                     i__2 = nr - p + 1;
2791                     d__1 = 1. / temp1;
2792                     zdscal_(&i__2, &d__1, &cwork[(*n << 1) + (p - 1) * nr + p]
2793                             , &c__1);
2794 /* L3950: */
2795                 }
2796                 zpocon_("L", &nr, &cwork[(*n << 1) + 1], &nr, &c_b141, &temp1,
2797                          &cwork[(*n << 1) + nr * nr + 1], &rwork[1], &ierr);
2798                 condr1 = 1. / sqrt(temp1);
2799 /*           R1 is OK for inverse <=> CONDR1 .LT. DBLE(N) */
2800 /*           more conservative    <=> CONDR1 .LT. SQRT(DBLE(N)) */
2801
2802                 cond_ok__ = sqrt(sqrt((doublereal) nr));
2803 /* [TP]       COND_OK is a tuning parameter. */
2804
2805                 if (condr1 < cond_ok__) {
2806 /*              implementation, this QRF should be implemented as the QRF */
2807 /*              of a lower triangular matrix. */
2808 /*              R1^* = Q2 * R2 */
2809                     i__1 = *lwork - (*n << 1);
2810                     zgeqrf_(n, &nr, &v[v_offset], ldv, &cwork[*n + 1], &cwork[
2811                             (*n << 1) + 1], &i__1, &ierr);
2812
2813                     if (l2pert) {
2814                         xsc = sqrt(small) / epsln;
2815                         i__1 = nr;
2816                         for (p = 2; p <= i__1; ++p) {
2817                             i__2 = p - 1;
2818                             for (q = 1; q <= i__2; ++q) {
2819 /* Computing MIN */
2820                                 d__2 = z_abs(&v[p + p * v_dim1]), d__3 = 
2821                                         z_abs(&v[q + q * v_dim1]);
2822                                 d__1 = xsc * f2cmin(d__2,d__3);
2823                                 z__1.r = d__1, z__1.i = 0.;
2824                                 ctemp.r = z__1.r, ctemp.i = z__1.i;
2825                                 if (z_abs(&v[q + p * v_dim1]) <= temp1) {
2826                                     i__3 = q + p * v_dim1;
2827                                     v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
2828                                 }
2829 /*     $                     V(q,p) = TEMP1 * ( V(q,p) / ABS(V(q,p)) ) */
2830 /* L3958: */
2831                             }
2832 /* L3959: */
2833                         }
2834                     }
2835
2836                     if (nr != *n) {
2837                         zlacpy_("A", n, &nr, &v[v_offset], ldv, &cwork[(*n << 
2838                                 1) + 1], n);
2839                     }
2840
2841                     i__1 = nr - 1;
2842                     for (p = 1; p <= i__1; ++p) {
2843                         i__2 = nr - p;
2844                         zcopy_(&i__2, &v[p + (p + 1) * v_dim1], ldv, &v[p + 1 
2845                                 + p * v_dim1], &c__1);
2846                         i__2 = nr - p + 1;
2847                         zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
2848 /* L1969: */
2849                     }
2850                     i__1 = nr + nr * v_dim1;
2851                     d_cnjg(&z__1, &v[nr + nr * v_dim1]);
2852                     v[i__1].r = z__1.r, v[i__1].i = z__1.i;
2853
2854                     condr2 = condr1;
2855
2856                 } else {
2857
2858 /*              Note that windowed pivoting would be equally good */
2859 /*              numerically, and more run-time efficient. So, in */
2860 /*              an optimal implementation, the next call to ZGEQP3 */
2861 /*              should be replaced with eg. CALL ZGEQPX (ACM TOMS #782) */
2862 /*              with properly (carefully) chosen parameters. */
2863
2864 /*              R1^* * P2 = Q2 * R2 */
2865                     i__1 = nr;
2866                     for (p = 1; p <= i__1; ++p) {
2867                         iwork[*n + p] = 0;
2868 /* L3003: */
2869                     }
2870                     i__1 = *lwork - (*n << 1);
2871                     zgeqp3_(n, &nr, &v[v_offset], ldv, &iwork[*n + 1], &cwork[
2872                             *n + 1], &cwork[(*n << 1) + 1], &i__1, &rwork[1], 
2873                             &ierr);
2874 /* *               CALL ZGEQRF( N, NR, V, LDV, CWORK(N+1), CWORK(2*N+1), */
2875 /* *     $              LWORK-2*N, IERR ) */
2876                     if (l2pert) {
2877                         xsc = sqrt(small);
2878                         i__1 = nr;
2879                         for (p = 2; p <= i__1; ++p) {
2880                             i__2 = p - 1;
2881                             for (q = 1; q <= i__2; ++q) {
2882 /* Computing MIN */
2883                                 d__2 = z_abs(&v[p + p * v_dim1]), d__3 = 
2884                                         z_abs(&v[q + q * v_dim1]);
2885                                 d__1 = xsc * f2cmin(d__2,d__3);
2886                                 z__1.r = d__1, z__1.i = 0.;
2887                                 ctemp.r = z__1.r, ctemp.i = z__1.i;
2888                                 if (z_abs(&v[q + p * v_dim1]) <= temp1) {
2889                                     i__3 = q + p * v_dim1;
2890                                     v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
2891                                 }
2892 /*     $                     V(q,p) = TEMP1 * ( V(q,p) / ABS(V(q,p)) ) */
2893 /* L3968: */
2894                             }
2895 /* L3969: */
2896                         }
2897                     }
2898
2899                     zlacpy_("A", n, &nr, &v[v_offset], ldv, &cwork[(*n << 1) 
2900                             + 1], n);
2901
2902                     if (l2pert) {
2903                         xsc = sqrt(small);
2904                         i__1 = nr;
2905                         for (p = 2; p <= i__1; ++p) {
2906                             i__2 = p - 1;
2907                             for (q = 1; q <= i__2; ++q) {
2908 /* Computing MIN */
2909                                 d__2 = z_abs(&v[p + p * v_dim1]), d__3 = 
2910                                         z_abs(&v[q + q * v_dim1]);
2911                                 d__1 = xsc * f2cmin(d__2,d__3);
2912                                 z__1.r = d__1, z__1.i = 0.;
2913                                 ctemp.r = z__1.r, ctemp.i = z__1.i;
2914 /*                        V(p,q) = - TEMP1*( V(q,p) / ABS(V(q,p)) ) */
2915                                 i__3 = p + q * v_dim1;
2916                                 z__1.r = -ctemp.r, z__1.i = -ctemp.i;
2917                                 v[i__3].r = z__1.r, v[i__3].i = z__1.i;
2918 /* L8971: */
2919                             }
2920 /* L8970: */
2921                         }
2922                     } else {
2923                         i__1 = nr - 1;
2924                         i__2 = nr - 1;
2925                         zlaset_("L", &i__1, &i__2, &c_b1, &c_b1, &v[v_dim1 + 
2926                                 2], ldv);
2927                     }
2928 /*              Now, compute R2 = L3 * Q3, the LQ factorization. */
2929                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
2930                     zgelqf_(&nr, &nr, &v[v_offset], ldv, &cwork[(*n << 1) + *
2931                             n * nr + 1], &cwork[(*n << 1) + *n * nr + nr + 1],
2932                              &i__1, &ierr);
2933                     zlacpy_("L", &nr, &nr, &v[v_offset], ldv, &cwork[(*n << 1)
2934                              + *n * nr + nr + 1], &nr);
2935                     i__1 = nr;
2936                     for (p = 1; p <= i__1; ++p) {
2937                         temp1 = dznrm2_(&p, &cwork[(*n << 1) + *n * nr + nr + 
2938                                 p], &nr);
2939                         d__1 = 1. / temp1;
2940                         zdscal_(&p, &d__1, &cwork[(*n << 1) + *n * nr + nr + 
2941                                 p], &nr);
2942 /* L4950: */
2943                     }
2944                     zpocon_("L", &nr, &cwork[(*n << 1) + *n * nr + nr + 1], &
2945                             nr, &c_b141, &temp1, &cwork[(*n << 1) + *n * nr + 
2946                             nr + nr * nr + 1], &rwork[1], &ierr);
2947                     condr2 = 1. / sqrt(temp1);
2948
2949
2950                     if (condr2 >= cond_ok__) {
2951 /*                 (this overwrites the copy of R2, as it will not be */
2952 /*                 needed in this branch, but it does not overwritte the */
2953 /*                 Huseholder vectors of Q2.). */
2954                         zlacpy_("U", &nr, &nr, &v[v_offset], ldv, &cwork[(*n 
2955                                 << 1) + 1], n);
2956 /*                 WORK(2*N+N*NR+1:2*N+N*NR+N) */
2957                     }
2958
2959                 }
2960
2961                 if (l2pert) {
2962                     xsc = sqrt(small);
2963                     i__1 = nr;
2964                     for (q = 2; q <= i__1; ++q) {
2965                         i__2 = q + q * v_dim1;
2966                         z__1.r = xsc * v[i__2].r, z__1.i = xsc * v[i__2].i;
2967                         ctemp.r = z__1.r, ctemp.i = z__1.i;
2968                         i__2 = q - 1;
2969                         for (p = 1; p <= i__2; ++p) {
2970 /*                     V(p,q) = - TEMP1*( V(p,q) / ABS(V(p,q)) ) */
2971                             i__3 = p + q * v_dim1;
2972                             z__1.r = -ctemp.r, z__1.i = -ctemp.i;
2973                             v[i__3].r = z__1.r, v[i__3].i = z__1.i;
2974 /* L4969: */
2975                         }
2976 /* L4968: */
2977                     }
2978                 } else {
2979                     i__1 = nr - 1;
2980                     i__2 = nr - 1;
2981                     zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) 
2982                             + 1], ldv);
2983                 }
2984
2985 /*        Second preconditioning finished; continue with Jacobi SVD */
2986 /*        The input matrix is lower trinagular. */
2987
2988 /*        Recover the right singular vectors as solution of a well */
2989 /*        conditioned triangular matrix equation. */
2990
2991                 if (condr1 < cond_ok__) {
2992
2993                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
2994                     zgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
2995                             1], &nr, &u[u_offset], ldu, &cwork[(*n << 1) + *n 
2996                             * nr + nr + 1], &i__1, &rwork[1], lrwork, info);
2997                     scalem = rwork[1];
2998                     numrank = i_dnnt(&rwork[2]);
2999                     i__1 = nr;
3000                     for (p = 1; p <= i__1; ++p) {
3001                         zcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1 
3002                                 + 1], &c__1);
3003                         zdscal_(&nr, &sva[p], &v[p * v_dim1 + 1], &c__1);
3004 /* L3970: */
3005                     }
3006
3007                     if (nr == *n) {
3008 /* :))             .. best case, R1 is inverted. The solution of this matrix */
3009 /*                 equation is Q2*V2 = the product of the Jacobi rotations */
3010 /*                 used in ZGESVJ, premultiplied with the orthogonal matrix */
3011 /*                 from the second QR factorization. */
3012                         ztrsm_("L", "U", "N", "N", &nr, &nr, &c_b2, &a[
3013                                 a_offset], lda, &v[v_offset], ldv);
3014                     } else {
3015 /*                 is inverted to get the product of the Jacobi rotations */
3016 /*                 used in ZGESVJ. The Q-factor from the second QR */
3017 /*                 factorization is then built in explicitly. */
3018                         ztrsm_("L", "U", "C", "N", &nr, &nr, &c_b2, &cwork[(*
3019                                 n << 1) + 1], n, &v[v_offset], ldv);
3020                         if (nr < *n) {
3021                             i__1 = *n - nr;
3022                             zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 
3023                                     + v_dim1], ldv);
3024                             i__1 = *n - nr;
3025                             zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1)
3026                                      * v_dim1 + 1], ldv);
3027                             i__1 = *n - nr;
3028                             i__2 = *n - nr;
3029                             zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 
3030                                     1 + (nr + 1) * v_dim1], ldv);
3031                         }
3032                         i__1 = *lwork - (*n << 1) - *n * nr - nr;
3033                         zunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n,
3034                                  &cwork[*n + 1], &v[v_offset], ldv, &cwork[(*
3035                                 n << 1) + *n * nr + nr + 1], &i__1, &ierr);
3036                     }
3037
3038                 } else if (condr2 < cond_ok__) {
3039
3040 /*              The matrix R2 is inverted. The solution of the matrix equation */
3041 /*              is Q3^* * V3 = the product of the Jacobi rotations (appplied to */
3042 /*              the lower triangular L3 from the LQ factorization of */
3043 /*              R2=L3*Q3), pre-multiplied with the transposed Q3. */
3044                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
3045                     zgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
3046                             1], &nr, &u[u_offset], ldu, &cwork[(*n << 1) + *n 
3047                             * nr + nr + 1], &i__1, &rwork[1], lrwork, info);
3048                     scalem = rwork[1];
3049                     numrank = i_dnnt(&rwork[2]);
3050                     i__1 = nr;
3051                     for (p = 1; p <= i__1; ++p) {
3052                         zcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1 
3053                                 + 1], &c__1);
3054                         zdscal_(&nr, &sva[p], &u[p * u_dim1 + 1], &c__1);
3055 /* L3870: */
3056                     }
3057                     ztrsm_("L", "U", "N", "N", &nr, &nr, &c_b2, &cwork[(*n << 
3058                             1) + 1], n, &u[u_offset], ldu);
3059                     i__1 = nr;
3060                     for (q = 1; q <= i__1; ++q) {
3061                         i__2 = nr;
3062                         for (p = 1; p <= i__2; ++p) {
3063                             i__3 = (*n << 1) + *n * nr + nr + iwork[*n + p];
3064                             i__4 = p + q * u_dim1;
3065                             cwork[i__3].r = u[i__4].r, cwork[i__3].i = u[i__4]
3066                                     .i;
3067 /* L872: */
3068                         }
3069                         i__2 = nr;
3070                         for (p = 1; p <= i__2; ++p) {
3071                             i__3 = p + q * u_dim1;
3072                             i__4 = (*n << 1) + *n * nr + nr + p;
3073                             u[i__3].r = cwork[i__4].r, u[i__3].i = cwork[i__4]
3074                                     .i;
3075 /* L874: */
3076                         }
3077 /* L873: */
3078                     }
3079                     if (nr < *n) {
3080                         i__1 = *n - nr;
3081                         zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + 
3082                                 v_dim1], ldv);
3083                         i__1 = *n - nr;
3084                         zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * 
3085                                 v_dim1 + 1], ldv);
3086                         i__1 = *n - nr;
3087                         i__2 = *n - nr;
3088                         zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (
3089                                 nr + 1) * v_dim1], ldv);
3090                     }
3091                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
3092                     zunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n, &
3093                             cwork[*n + 1], &v[v_offset], ldv, &cwork[(*n << 1)
3094                              + *n * nr + nr + 1], &i__1, &ierr);
3095                 } else {
3096 /*              Last line of defense. */
3097 /* #:(          This is a rather pathological case: no scaled condition */
3098 /*              improvement after two pivoted QR factorizations. Other */
3099 /*              possibility is that the rank revealing QR factorization */
3100 /*              or the condition estimator has failed, or the COND_OK */
3101 /*              is set very close to ONE (which is unnecessary). Normally, */
3102 /*              this branch should never be executed, but in rare cases of */
3103 /*              failure of the RRQR or condition estimator, the last line of */
3104 /*              defense ensures that ZGEJSV completes the task. */
3105 /*              Compute the full SVD of L3 using ZGESVJ with explicit */
3106 /*              accumulation of Jacobi rotations. */
3107                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
3108                     zgesvj_("L", "U", "V", &nr, &nr, &v[v_offset], ldv, &sva[
3109                             1], &nr, &u[u_offset], ldu, &cwork[(*n << 1) + *n 
3110                             * nr + nr + 1], &i__1, &rwork[1], lrwork, info);
3111                     scalem = rwork[1];
3112                     numrank = i_dnnt(&rwork[2]);
3113                     if (nr < *n) {
3114                         i__1 = *n - nr;
3115                         zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + 
3116                                 v_dim1], ldv);
3117                         i__1 = *n - nr;
3118                         zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * 
3119                                 v_dim1 + 1], ldv);
3120                         i__1 = *n - nr;
3121                         i__2 = *n - nr;
3122                         zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (
3123                                 nr + 1) * v_dim1], ldv);
3124                     }
3125                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
3126                     zunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n, &
3127                             cwork[*n + 1], &v[v_offset], ldv, &cwork[(*n << 1)
3128                              + *n * nr + nr + 1], &i__1, &ierr);
3129
3130                     i__1 = *lwork - (*n << 1) - *n * nr - nr;
3131                     zunmlq_("L", "C", &nr, &nr, &nr, &cwork[(*n << 1) + 1], n,
3132                              &cwork[(*n << 1) + *n * nr + 1], &u[u_offset], 
3133                             ldu, &cwork[(*n << 1) + *n * nr + nr + 1], &i__1, 
3134                             &ierr);
3135                     i__1 = nr;
3136                     for (q = 1; q <= i__1; ++q) {
3137                         i__2 = nr;
3138                         for (p = 1; p <= i__2; ++p) {
3139                             i__3 = (*n << 1) + *n * nr + nr + iwork[*n + p];
3140                             i__4 = p + q * u_dim1;
3141                             cwork[i__3].r = u[i__4].r, cwork[i__3].i = u[i__4]
3142                                     .i;
3143 /* L772: */
3144                         }
3145                         i__2 = nr;
3146                         for (p = 1; p <= i__2; ++p) {
3147                             i__3 = p + q * u_dim1;
3148                             i__4 = (*n << 1) + *n * nr + nr + p;
3149                             u[i__3].r = cwork[i__4].r, u[i__3].i = cwork[i__4]
3150                                     .i;
3151 /* L774: */
3152                         }
3153 /* L773: */
3154                     }
3155
3156                 }
3157
3158 /*           Permute the rows of V using the (column) permutation from the */
3159 /*           first QRF. Also, scale the columns to make them unit in */
3160 /*           Euclidean norm. This applies to all cases. */
3161
3162                 temp1 = sqrt((doublereal) (*n)) * epsln;
3163                 i__1 = *n;
3164                 for (q = 1; q <= i__1; ++q) {
3165                     i__2 = *n;
3166                     for (p = 1; p <= i__2; ++p) {
3167                         i__3 = (*n << 1) + *n * nr + nr + iwork[p];
3168                         i__4 = p + q * v_dim1;
3169                         cwork[i__3].r = v[i__4].r, cwork[i__3].i = v[i__4].i;
3170 /* L972: */
3171                     }
3172                     i__2 = *n;
3173                     for (p = 1; p <= i__2; ++p) {
3174                         i__3 = p + q * v_dim1;
3175                         i__4 = (*n << 1) + *n * nr + nr + p;
3176                         v[i__3].r = cwork[i__4].r, v[i__3].i = cwork[i__4].i;
3177 /* L973: */
3178                     }
3179                     xsc = 1. / dznrm2_(n, &v[q * v_dim1 + 1], &c__1);
3180                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
3181                         zdscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
3182                     }
3183 /* L1972: */
3184                 }
3185 /*           At this moment, V contains the right singular vectors of A. */
3186 /*           Next, assemble the left singular vector matrix U (M x N). */
3187                 if (nr < *m) {
3188                     i__1 = *m - nr;
3189                     zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1]
3190                             , ldu);
3191                     if (nr < n1) {
3192                         i__1 = n1 - nr;
3193                         zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) * 
3194                                 u_dim1 + 1], ldu);
3195                         i__1 = *m - nr;
3196                         i__2 = n1 - nr;
3197                         zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (
3198                                 nr + 1) * u_dim1], ldu);
3199                     }
3200                 }
3201
3202 /*           The Q matrix from the first QRF is built into the left singular */
3203 /*           matrix U. This applies to all cases. */
3204
3205                 i__1 = *lwork - *n;
3206                 zunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
3207                         u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
3208 /*           The columns of U are normalized. The cost is O(M*N) flops. */
3209                 temp1 = sqrt((doublereal) (*m)) * epsln;
3210                 i__1 = nr;
3211                 for (p = 1; p <= i__1; ++p) {
3212                     xsc = 1. / dznrm2_(m, &u[p * u_dim1 + 1], &c__1);
3213                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
3214                         zdscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
3215                     }
3216 /* L1973: */
3217                 }
3218
3219 /*           If the initial QRF is computed with row pivoting, the left */
3220 /*           singular vectors must be adjusted. */
3221
3222                 if (rowpiv) {
3223                     i__1 = *m - 1;
3224                     zlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[
3225                             iwoff + 1], &c_n1);
3226                 }
3227
3228             } else {
3229
3230 /*        the second QRF is not needed */
3231
3232                 zlacpy_("U", n, n, &a[a_offset], lda, &cwork[*n + 1], n);
3233                 if (l2pert) {
3234                     xsc = sqrt(small);
3235                     i__1 = *n;
3236                     for (p = 2; p <= i__1; ++p) {
3237                         i__2 = *n + (p - 1) * *n + p;
3238                         z__1.r = xsc * cwork[i__2].r, z__1.i = xsc * cwork[
3239                                 i__2].i;
3240                         ctemp.r = z__1.r, ctemp.i = z__1.i;
3241                         i__2 = p - 1;
3242                         for (q = 1; q <= i__2; ++q) {
3243 /*                     CWORK(N+(q-1)*N+p)=-TEMP1 * ( CWORK(N+(p-1)*N+q) / */
3244 /*     $                                        ABS(CWORK(N+(p-1)*N+q)) ) */
3245                             i__3 = *n + (q - 1) * *n + p;
3246                             z__1.r = -ctemp.r, z__1.i = -ctemp.i;
3247                             cwork[i__3].r = z__1.r, cwork[i__3].i = z__1.i;
3248 /* L5971: */
3249                         }
3250 /* L5970: */
3251                     }
3252                 } else {
3253                     i__1 = *n - 1;
3254                     i__2 = *n - 1;
3255                     zlaset_("L", &i__1, &i__2, &c_b1, &c_b1, &cwork[*n + 2], 
3256                             n);
3257                 }
3258
3259                 i__1 = *lwork - *n - *n * *n;
3260                 zgesvj_("U", "U", "N", n, n, &cwork[*n + 1], n, &sva[1], n, &
3261                         u[u_offset], ldu, &cwork[*n + *n * *n + 1], &i__1, &
3262                         rwork[1], lrwork, info);
3263
3264                 scalem = rwork[1];
3265                 numrank = i_dnnt(&rwork[2]);
3266                 i__1 = *n;
3267                 for (p = 1; p <= i__1; ++p) {
3268                     zcopy_(n, &cwork[*n + (p - 1) * *n + 1], &c__1, &u[p * 
3269                             u_dim1 + 1], &c__1);
3270                     zdscal_(n, &sva[p], &cwork[*n + (p - 1) * *n + 1], &c__1);
3271 /* L6970: */
3272                 }
3273
3274                 ztrsm_("L", "U", "N", "N", n, n, &c_b2, &a[a_offset], lda, &
3275                         cwork[*n + 1], n);
3276                 i__1 = *n;
3277                 for (p = 1; p <= i__1; ++p) {
3278                     zcopy_(n, &cwork[*n + p], n, &v[iwork[p] + v_dim1], ldv);
3279 /* L6972: */
3280                 }
3281                 temp1 = sqrt((doublereal) (*n)) * epsln;
3282                 i__1 = *n;
3283                 for (p = 1; p <= i__1; ++p) {
3284                     xsc = 1. / dznrm2_(n, &v[p * v_dim1 + 1], &c__1);
3285                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
3286                         zdscal_(n, &xsc, &v[p * v_dim1 + 1], &c__1);
3287                     }
3288 /* L6971: */
3289                 }
3290
3291 /*           Assemble the left singular vector matrix U (M x N). */
3292
3293                 if (*n < *m) {
3294                     i__1 = *m - *n;
3295                     zlaset_("A", &i__1, n, &c_b1, &c_b1, &u[*n + 1 + u_dim1], 
3296                             ldu);
3297                     if (*n < n1) {
3298                         i__1 = n1 - *n;
3299                         zlaset_("A", n, &i__1, &c_b1, &c_b1, &u[(*n + 1) * 
3300                                 u_dim1 + 1], ldu);
3301                         i__1 = *m - *n;
3302                         i__2 = n1 - *n;
3303                         zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[*n + 1 + (
3304                                 *n + 1) * u_dim1], ldu);
3305                     }
3306                 }
3307                 i__1 = *lwork - *n;
3308                 zunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
3309                         u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
3310                 temp1 = sqrt((doublereal) (*m)) * epsln;
3311                 i__1 = n1;
3312                 for (p = 1; p <= i__1; ++p) {
3313                     xsc = 1. / dznrm2_(m, &u[p * u_dim1 + 1], &c__1);
3314                     if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
3315                         zdscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
3316                     }
3317 /* L6973: */
3318                 }
3319
3320                 if (rowpiv) {
3321                     i__1 = *m - 1;
3322                     zlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[
3323                             iwoff + 1], &c_n1);
3324                 }
3325
3326             }
3327
3328 /*        end of the  >> almost orthogonal case <<  in the full SVD */
3329
3330         } else {
3331
3332 /*        This branch deploys a preconditioned Jacobi SVD with explicitly */
3333 /*        accumulated rotations. It is included as optional, mainly for */
3334 /*        experimental purposes. It does perform well, and can also be used. */
3335 /*        In this implementation, this branch will be automatically activated */
3336 /*        if the  condition number sigma_max(A) / sigma_min(A) is predicted */
3337 /*        to be greater than the overflow threshold. This is because the */
3338 /*        a posteriori computation of the singular vectors assumes robust */
3339 /*        implementation of BLAS and some LAPACK procedures, capable of working */
3340 /*        in presence of extreme values, e.g. when the singular values spread from */
3341 /*        the underflow to the overflow threshold. */
3342
3343             i__1 = nr;
3344             for (p = 1; p <= i__1; ++p) {
3345                 i__2 = *n - p + 1;
3346                 zcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
3347                         c__1);
3348                 i__2 = *n - p + 1;
3349                 zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
3350 /* L7968: */
3351             }
3352
3353             if (l2pert) {
3354                 xsc = sqrt(small / epsln);
3355                 i__1 = nr;
3356                 for (q = 1; q <= i__1; ++q) {
3357                     d__1 = xsc * z_abs(&v[q + q * v_dim1]);
3358                     z__1.r = d__1, z__1.i = 0.;
3359                     ctemp.r = z__1.r, ctemp.i = z__1.i;
3360                     i__2 = *n;
3361                     for (p = 1; p <= i__2; ++p) {
3362                         if (p > q && z_abs(&v[p + q * v_dim1]) <= temp1 || p <
3363                                  q) {
3364                             i__3 = p + q * v_dim1;
3365                             v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
3366                         }
3367 /*     $                V(p,q) = TEMP1 * ( V(p,q) / ABS(V(p,q)) ) */
3368                         if (p < q) {
3369                             i__3 = p + q * v_dim1;
3370                             i__4 = p + q * v_dim1;
3371                             z__1.r = -v[i__4].r, z__1.i = -v[i__4].i;
3372                             v[i__3].r = z__1.r, v[i__3].i = z__1.i;
3373                         }
3374 /* L5968: */
3375                     }
3376 /* L5969: */
3377                 }
3378             } else {
3379                 i__1 = nr - 1;
3380                 i__2 = nr - 1;
3381                 zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1]
3382                         , ldv);
3383             }
3384             i__1 = *lwork - (*n << 1);
3385             zgeqrf_(n, &nr, &v[v_offset], ldv, &cwork[*n + 1], &cwork[(*n << 
3386                     1) + 1], &i__1, &ierr);
3387             zlacpy_("L", n, &nr, &v[v_offset], ldv, &cwork[(*n << 1) + 1], n);
3388
3389             i__1 = nr;
3390             for (p = 1; p <= i__1; ++p) {
3391                 i__2 = nr - p + 1;
3392                 zcopy_(&i__2, &v[p + p * v_dim1], ldv, &u[p + p * u_dim1], &
3393                         c__1);
3394                 i__2 = nr - p + 1;
3395                 zlacgv_(&i__2, &u[p + p * u_dim1], &c__1);
3396 /* L7969: */
3397             }
3398             if (l2pert) {
3399                 xsc = sqrt(small / epsln);
3400                 i__1 = nr;
3401                 for (q = 2; q <= i__1; ++q) {
3402                     i__2 = q - 1;
3403                     for (p = 1; p <= i__2; ++p) {
3404 /* Computing MIN */
3405                         d__2 = z_abs(&u[p + p * u_dim1]), d__3 = z_abs(&u[q + 
3406                                 q * u_dim1]);
3407                         d__1 = xsc * f2cmin(d__2,d__3);
3408                         z__1.r = d__1, z__1.i = 0.;
3409                         ctemp.r = z__1.r, ctemp.i = z__1.i;
3410 /*                  U(p,q) = - TEMP1 * ( U(q,p) / ABS(U(q,p)) ) */
3411                         i__3 = p + q * u_dim1;
3412                         z__1.r = -ctemp.r, z__1.i = -ctemp.i;
3413                         u[i__3].r = z__1.r, u[i__3].i = z__1.i;
3414 /* L9971: */
3415                     }
3416 /* L9970: */
3417                 }
3418             } else {
3419                 i__1 = nr - 1;
3420                 i__2 = nr - 1;
3421                 zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1]
3422                         , ldu);
3423             }
3424             i__1 = *lwork - (*n << 1) - *n * nr;
3425             zgesvj_("L", "U", "V", &nr, &nr, &u[u_offset], ldu, &sva[1], n, &
3426                     v[v_offset], ldv, &cwork[(*n << 1) + *n * nr + 1], &i__1, 
3427                     &rwork[1], lrwork, info);
3428             scalem = rwork[1];
3429             numrank = i_dnnt(&rwork[2]);
3430             if (nr < *n) {
3431                 i__1 = *n - nr;
3432                 zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + v_dim1], 
3433                         ldv);
3434                 i__1 = *n - nr;
3435                 zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * v_dim1 + 
3436                         1], ldv);
3437                 i__1 = *n - nr;
3438                 i__2 = *n - nr;
3439                 zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (nr + 1) 
3440                         * v_dim1], ldv);
3441             }
3442             i__1 = *lwork - (*n << 1) - *n * nr - nr;
3443             zunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n, &cwork[*n 
3444                     + 1], &v[v_offset], ldv, &cwork[(*n << 1) + *n * nr + nr 
3445                     + 1], &i__1, &ierr);
3446
3447 /*           Permute the rows of V using the (column) permutation from the */
3448 /*           first QRF. Also, scale the columns to make them unit in */
3449 /*           Euclidean norm. This applies to all cases. */
3450
3451             temp1 = sqrt((doublereal) (*n)) * epsln;
3452             i__1 = *n;
3453             for (q = 1; q <= i__1; ++q) {
3454                 i__2 = *n;
3455                 for (p = 1; p <= i__2; ++p) {
3456                     i__3 = (*n << 1) + *n * nr + nr + iwork[p];
3457                     i__4 = p + q * v_dim1;
3458                     cwork[i__3].r = v[i__4].r, cwork[i__3].i = v[i__4].i;
3459 /* L8972: */
3460                 }
3461                 i__2 = *n;
3462                 for (p = 1; p <= i__2; ++p) {
3463                     i__3 = p + q * v_dim1;
3464                     i__4 = (*n << 1) + *n * nr + nr + p;
3465                     v[i__3].r = cwork[i__4].r, v[i__3].i = cwork[i__4].i;
3466 /* L8973: */
3467                 }
3468                 xsc = 1. / dznrm2_(n, &v[q * v_dim1 + 1], &c__1);
3469                 if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
3470                     zdscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
3471                 }
3472 /* L7972: */
3473             }
3474
3475 /*           At this moment, V contains the right singular vectors of A. */
3476 /*           Next, assemble the left singular vector matrix U (M x N). */
3477
3478             if (nr < *m) {
3479                 i__1 = *m - nr;
3480                 zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1], 
3481                         ldu);
3482                 if (nr < n1) {
3483                     i__1 = n1 - nr;
3484                     zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) * 
3485                             u_dim1 + 1], ldu);
3486                     i__1 = *m - nr;
3487                     i__2 = n1 - nr;
3488                     zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (nr 
3489                             + 1) * u_dim1], ldu);
3490                 }
3491             }
3492
3493             i__1 = *lwork - *n;
3494             zunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
3495                     u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
3496
3497             if (rowpiv) {
3498                 i__1 = *m - 1;
3499                 zlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[iwoff + 
3500                         1], &c_n1);
3501             }
3502
3503
3504         }
3505         if (transp) {
3506             i__1 = *n;
3507             for (p = 1; p <= i__1; ++p) {
3508                 zswap_(n, &u[p * u_dim1 + 1], &c__1, &v[p * v_dim1 + 1], &
3509                         c__1);
3510 /* L6974: */
3511             }
3512         }
3513
3514     }
3515 /*     end of the full SVD */
3516
3517 /*     Undo scaling, if necessary (and possible) */
3518
3519     if (uscal2 <= big / sva[1] * uscal1) {
3520         dlascl_("G", &c__0, &c__0, &uscal1, &uscal2, &nr, &c__1, &sva[1], n, &
3521                 ierr);
3522         uscal1 = 1.;
3523         uscal2 = 1.;
3524     }
3525
3526     if (nr < *n) {
3527         i__1 = *n;
3528         for (p = nr + 1; p <= i__1; ++p) {
3529             sva[p] = 0.;
3530 /* L3004: */
3531         }
3532     }
3533
3534     rwork[1] = uscal2 * scalem;
3535     rwork[2] = uscal1;
3536     if (errest) {
3537         rwork[3] = sconda;
3538     }
3539     if (lsvec && rsvec) {
3540         rwork[4] = condr1;
3541         rwork[5] = condr2;
3542     }
3543     if (l2tran) {
3544         rwork[6] = entra;
3545         rwork[7] = entrat;
3546     }
3547
3548     iwork[1] = nr;
3549     iwork[2] = numrank;
3550     iwork[3] = warning;
3551     if (transp) {
3552         iwork[4] = 1;
3553     } else {
3554         iwork[4] = -1;
3555     }
3556
3557     return 0;
3558 } /* zgejsv_ */
3559