C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / zggesx.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static doublecomplex c_b1 = {0.,0.};
516 static doublecomplex c_b2 = {1.,0.};
517 static integer c__1 = 1;
518 static integer c__0 = 0;
519 static integer c_n1 = -1;
520
521 /* > \brief <b> ZGGESX computes the eigenvalues, the Schur form, and, optionally, the matrix of Schur vectors 
522 for GE matrices</b> */
523
524 /*  =========== DOCUMENTATION =========== */
525
526 /* Online html documentation available at */
527 /*            http://www.netlib.org/lapack/explore-html/ */
528
529 /* > \htmlonly */
530 /* > Download ZGGESX + dependencies */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zggesx.
532 f"> */
533 /* > [TGZ]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zggesx.
535 f"> */
536 /* > [ZIP]</a> */
537 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zggesx.
538 f"> */
539 /* > [TXT]</a> */
540 /* > \endhtmlonly */
541
542 /*  Definition: */
543 /*  =========== */
544
545 /*       SUBROUTINE ZGGESX( JOBVSL, JOBVSR, SORT, SELCTG, SENSE, N, A, LDA, */
546 /*                          B, LDB, SDIM, ALPHA, BETA, VSL, LDVSL, VSR, */
547 /*                          LDVSR, RCONDE, RCONDV, WORK, LWORK, RWORK, */
548 /*                          IWORK, LIWORK, BWORK, INFO ) */
549
550 /*       CHARACTER          JOBVSL, JOBVSR, SENSE, SORT */
551 /*       INTEGER            INFO, LDA, LDB, LDVSL, LDVSR, LIWORK, LWORK, N, */
552 /*      $                   SDIM */
553 /*       LOGICAL            BWORK( * ) */
554 /*       INTEGER            IWORK( * ) */
555 /*       DOUBLE PRECISION   RCONDE( 2 ), RCONDV( 2 ), RWORK( * ) */
556 /*       COMPLEX*16         A( LDA, * ), ALPHA( * ), B( LDB, * ), */
557 /*      $                   BETA( * ), VSL( LDVSL, * ), VSR( LDVSR, * ), */
558 /*      $                   WORK( * ) */
559 /*       LOGICAL            SELCTG */
560 /*       EXTERNAL           SELCTG */
561
562
563 /* > \par Purpose: */
564 /*  ============= */
565 /* > */
566 /* > \verbatim */
567 /* > */
568 /* > ZGGESX computes for a pair of N-by-N complex nonsymmetric matrices */
569 /* > (A,B), the generalized eigenvalues, the complex Schur form (S,T), */
570 /* > and, optionally, the left and/or right matrices of Schur vectors (VSL */
571 /* > and VSR).  This gives the generalized Schur factorization */
572 /* > */
573 /* >      (A,B) = ( (VSL) S (VSR)**H, (VSL) T (VSR)**H ) */
574 /* > */
575 /* > where (VSR)**H is the conjugate-transpose of VSR. */
576 /* > */
577 /* > Optionally, it also orders the eigenvalues so that a selected cluster */
578 /* > of eigenvalues appears in the leading diagonal blocks of the upper */
579 /* > triangular matrix S and the upper triangular matrix T; computes */
580 /* > a reciprocal condition number for the average of the selected */
581 /* > eigenvalues (RCONDE); and computes a reciprocal condition number for */
582 /* > the right and left deflating subspaces corresponding to the selected */
583 /* > eigenvalues (RCONDV). The leading columns of VSL and VSR then form */
584 /* > an orthonormal basis for the corresponding left and right eigenspaces */
585 /* > (deflating subspaces). */
586 /* > */
587 /* > A generalized eigenvalue for a pair of matrices (A,B) is a scalar w */
588 /* > or a ratio alpha/beta = w, such that  A - w*B is singular.  It is */
589 /* > usually represented as the pair (alpha,beta), as there is a */
590 /* > reasonable interpretation for beta=0 or for both being zero. */
591 /* > */
592 /* > A pair of matrices (S,T) is in generalized complex Schur form if T is */
593 /* > upper triangular with non-negative diagonal and S is upper */
594 /* > triangular. */
595 /* > \endverbatim */
596
597 /*  Arguments: */
598 /*  ========== */
599
600 /* > \param[in] JOBVSL */
601 /* > \verbatim */
602 /* >          JOBVSL is CHARACTER*1 */
603 /* >          = 'N':  do not compute the left Schur vectors; */
604 /* >          = 'V':  compute the left Schur vectors. */
605 /* > \endverbatim */
606 /* > */
607 /* > \param[in] JOBVSR */
608 /* > \verbatim */
609 /* >          JOBVSR is CHARACTER*1 */
610 /* >          = 'N':  do not compute the right Schur vectors; */
611 /* >          = 'V':  compute the right Schur vectors. */
612 /* > \endverbatim */
613 /* > */
614 /* > \param[in] SORT */
615 /* > \verbatim */
616 /* >          SORT is CHARACTER*1 */
617 /* >          Specifies whether or not to order the eigenvalues on the */
618 /* >          diagonal of the generalized Schur form. */
619 /* >          = 'N':  Eigenvalues are not ordered; */
620 /* >          = 'S':  Eigenvalues are ordered (see SELCTG). */
621 /* > \endverbatim */
622 /* > */
623 /* > \param[in] SELCTG */
624 /* > \verbatim */
625 /* >          SELCTG is a LOGICAL FUNCTION of two COMPLEX*16 arguments */
626 /* >          SELCTG must be declared EXTERNAL in the calling subroutine. */
627 /* >          If SORT = 'N', SELCTG is not referenced. */
628 /* >          If SORT = 'S', SELCTG is used to select eigenvalues to sort */
629 /* >          to the top left of the Schur form. */
630 /* >          Note that a selected complex eigenvalue may no longer satisfy */
631 /* >          SELCTG(ALPHA(j),BETA(j)) = .TRUE. after ordering, since */
632 /* >          ordering may change the value of complex eigenvalues */
633 /* >          (especially if the eigenvalue is ill-conditioned), in this */
634 /* >          case INFO is set to N+3 see INFO below). */
635 /* > \endverbatim */
636 /* > */
637 /* > \param[in] SENSE */
638 /* > \verbatim */
639 /* >          SENSE is CHARACTER*1 */
640 /* >          Determines which reciprocal condition numbers are computed. */
641 /* >          = 'N': None are computed; */
642 /* >          = 'E': Computed for average of selected eigenvalues only; */
643 /* >          = 'V': Computed for selected deflating subspaces only; */
644 /* >          = 'B': Computed for both. */
645 /* >          If SENSE = 'E', 'V', or 'B', SORT must equal 'S'. */
646 /* > \endverbatim */
647 /* > */
648 /* > \param[in] N */
649 /* > \verbatim */
650 /* >          N is INTEGER */
651 /* >          The order of the matrices A, B, VSL, and VSR.  N >= 0. */
652 /* > \endverbatim */
653 /* > */
654 /* > \param[in,out] A */
655 /* > \verbatim */
656 /* >          A is COMPLEX*16 array, dimension (LDA, N) */
657 /* >          On entry, the first of the pair of matrices. */
658 /* >          On exit, A has been overwritten by its generalized Schur */
659 /* >          form S. */
660 /* > \endverbatim */
661 /* > */
662 /* > \param[in] LDA */
663 /* > \verbatim */
664 /* >          LDA is INTEGER */
665 /* >          The leading dimension of A.  LDA >= f2cmax(1,N). */
666 /* > \endverbatim */
667 /* > */
668 /* > \param[in,out] B */
669 /* > \verbatim */
670 /* >          B is COMPLEX*16 array, dimension (LDB, N) */
671 /* >          On entry, the second of the pair of matrices. */
672 /* >          On exit, B has been overwritten by its generalized Schur */
673 /* >          form T. */
674 /* > \endverbatim */
675 /* > */
676 /* > \param[in] LDB */
677 /* > \verbatim */
678 /* >          LDB is INTEGER */
679 /* >          The leading dimension of B.  LDB >= f2cmax(1,N). */
680 /* > \endverbatim */
681 /* > */
682 /* > \param[out] SDIM */
683 /* > \verbatim */
684 /* >          SDIM is INTEGER */
685 /* >          If SORT = 'N', SDIM = 0. */
686 /* >          If SORT = 'S', SDIM = number of eigenvalues (after sorting) */
687 /* >          for which SELCTG is true. */
688 /* > \endverbatim */
689 /* > */
690 /* > \param[out] ALPHA */
691 /* > \verbatim */
692 /* >          ALPHA is COMPLEX*16 array, dimension (N) */
693 /* > \endverbatim */
694 /* > */
695 /* > \param[out] BETA */
696 /* > \verbatim */
697 /* >          BETA is COMPLEX*16 array, dimension (N) */
698 /* >          On exit, ALPHA(j)/BETA(j), j=1,...,N, will be the */
699 /* >          generalized eigenvalues.  ALPHA(j) and BETA(j),j=1,...,N  are */
700 /* >          the diagonals of the complex Schur form (S,T).  BETA(j) will */
701 /* >          be non-negative real. */
702 /* > */
703 /* >          Note: the quotients ALPHA(j)/BETA(j) may easily over- or */
704 /* >          underflow, and BETA(j) may even be zero.  Thus, the user */
705 /* >          should avoid naively computing the ratio alpha/beta. */
706 /* >          However, ALPHA will be always less than and usually */
707 /* >          comparable with norm(A) in magnitude, and BETA always less */
708 /* >          than and usually comparable with norm(B). */
709 /* > \endverbatim */
710 /* > */
711 /* > \param[out] VSL */
712 /* > \verbatim */
713 /* >          VSL is COMPLEX*16 array, dimension (LDVSL,N) */
714 /* >          If JOBVSL = 'V', VSL will contain the left Schur vectors. */
715 /* >          Not referenced if JOBVSL = 'N'. */
716 /* > \endverbatim */
717 /* > */
718 /* > \param[in] LDVSL */
719 /* > \verbatim */
720 /* >          LDVSL is INTEGER */
721 /* >          The leading dimension of the matrix VSL. LDVSL >=1, and */
722 /* >          if JOBVSL = 'V', LDVSL >= N. */
723 /* > \endverbatim */
724 /* > */
725 /* > \param[out] VSR */
726 /* > \verbatim */
727 /* >          VSR is COMPLEX*16 array, dimension (LDVSR,N) */
728 /* >          If JOBVSR = 'V', VSR will contain the right Schur vectors. */
729 /* >          Not referenced if JOBVSR = 'N'. */
730 /* > \endverbatim */
731 /* > */
732 /* > \param[in] LDVSR */
733 /* > \verbatim */
734 /* >          LDVSR is INTEGER */
735 /* >          The leading dimension of the matrix VSR. LDVSR >= 1, and */
736 /* >          if JOBVSR = 'V', LDVSR >= N. */
737 /* > \endverbatim */
738 /* > */
739 /* > \param[out] RCONDE */
740 /* > \verbatim */
741 /* >          RCONDE is DOUBLE PRECISION array, dimension ( 2 ) */
742 /* >          If SENSE = 'E' or 'B', RCONDE(1) and RCONDE(2) contain the */
743 /* >          reciprocal condition numbers for the average of the selected */
744 /* >          eigenvalues. */
745 /* >          Not referenced if SENSE = 'N' or 'V'. */
746 /* > \endverbatim */
747 /* > */
748 /* > \param[out] RCONDV */
749 /* > \verbatim */
750 /* >          RCONDV is DOUBLE PRECISION array, dimension ( 2 ) */
751 /* >          If SENSE = 'V' or 'B', RCONDV(1) and RCONDV(2) contain the */
752 /* >          reciprocal condition number for the selected deflating */
753 /* >          subspaces. */
754 /* >          Not referenced if SENSE = 'N' or 'E'. */
755 /* > \endverbatim */
756 /* > */
757 /* > \param[out] WORK */
758 /* > \verbatim */
759 /* >          WORK is COMPLEX*16 array, dimension (MAX(1,LWORK)) */
760 /* >          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
761 /* > \endverbatim */
762 /* > */
763 /* > \param[in] LWORK */
764 /* > \verbatim */
765 /* >          LWORK is INTEGER */
766 /* >          The dimension of the array WORK. */
767 /* >          If N = 0, LWORK >= 1, else if SENSE = 'E', 'V', or 'B', */
768 /* >          LWORK >= MAX(1,2*N,2*SDIM*(N-SDIM)), else */
769 /* >          LWORK >= MAX(1,2*N).  Note that 2*SDIM*(N-SDIM) <= N*N/2. */
770 /* >          Note also that an error is only returned if */
771 /* >          LWORK < MAX(1,2*N), but if SENSE = 'E' or 'V' or 'B' this may */
772 /* >          not be large enough. */
773 /* > */
774 /* >          If LWORK = -1, then a workspace query is assumed; the routine */
775 /* >          only calculates the bound on the optimal size of the WORK */
776 /* >          array and the minimum size of the IWORK array, returns these */
777 /* >          values as the first entries of the WORK and IWORK arrays, and */
778 /* >          no error message related to LWORK or LIWORK is issued by */
779 /* >          XERBLA. */
780 /* > \endverbatim */
781 /* > */
782 /* > \param[out] RWORK */
783 /* > \verbatim */
784 /* >          RWORK is DOUBLE PRECISION array, dimension ( 8*N ) */
785 /* >          Real workspace. */
786 /* > \endverbatim */
787 /* > */
788 /* > \param[out] IWORK */
789 /* > \verbatim */
790 /* >          IWORK is INTEGER array, dimension (MAX(1,LIWORK)) */
791 /* >          On exit, if INFO = 0, IWORK(1) returns the minimum LIWORK. */
792 /* > \endverbatim */
793 /* > */
794 /* > \param[in] LIWORK */
795 /* > \verbatim */
796 /* >          LIWORK is INTEGER */
797 /* >          The dimension of the array IWORK. */
798 /* >          If SENSE = 'N' or N = 0, LIWORK >= 1, otherwise */
799 /* >          LIWORK >= N+2. */
800 /* > */
801 /* >          If LIWORK = -1, then a workspace query is assumed; the */
802 /* >          routine only calculates the bound on the optimal size of the */
803 /* >          WORK array and the minimum size of the IWORK array, returns */
804 /* >          these values as the first entries of the WORK and IWORK */
805 /* >          arrays, and no error message related to LWORK or LIWORK is */
806 /* >          issued by XERBLA. */
807 /* > \endverbatim */
808 /* > */
809 /* > \param[out] BWORK */
810 /* > \verbatim */
811 /* >          BWORK is LOGICAL array, dimension (N) */
812 /* >          Not referenced if SORT = 'N'. */
813 /* > \endverbatim */
814 /* > */
815 /* > \param[out] INFO */
816 /* > \verbatim */
817 /* >          INFO is INTEGER */
818 /* >          = 0:  successful exit */
819 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value. */
820 /* >          = 1,...,N: */
821 /* >                The QZ iteration failed.  (A,B) are not in Schur */
822 /* >                form, but ALPHA(j) and BETA(j) should be correct for */
823 /* >                j=INFO+1,...,N. */
824 /* >          > N:  =N+1: other than QZ iteration failed in ZHGEQZ */
825 /* >                =N+2: after reordering, roundoff changed values of */
826 /* >                      some complex eigenvalues so that leading */
827 /* >                      eigenvalues in the Generalized Schur form no */
828 /* >                      longer satisfy SELCTG=.TRUE.  This could also */
829 /* >                      be caused due to scaling. */
830 /* >                =N+3: reordering failed in ZTGSEN. */
831 /* > \endverbatim */
832
833 /*  Authors: */
834 /*  ======== */
835
836 /* > \author Univ. of Tennessee */
837 /* > \author Univ. of California Berkeley */
838 /* > \author Univ. of Colorado Denver */
839 /* > \author NAG Ltd. */
840
841 /* > \date June 2017 */
842
843 /* > \ingroup complex16GEeigen */
844
845 /*  ===================================================================== */
846 /* Subroutine */ int zggesx_(char *jobvsl, char *jobvsr, char *sort, L_fp 
847         selctg, char *sense, integer *n, doublecomplex *a, integer *lda, 
848         doublecomplex *b, integer *ldb, integer *sdim, doublecomplex *alpha, 
849         doublecomplex *beta, doublecomplex *vsl, integer *ldvsl, 
850         doublecomplex *vsr, integer *ldvsr, doublereal *rconde, doublereal *
851         rcondv, doublecomplex *work, integer *lwork, doublereal *rwork, 
852         integer *iwork, integer *liwork, logical *bwork, integer *info)
853 {
854     /* System generated locals */
855     integer a_dim1, a_offset, b_dim1, b_offset, vsl_dim1, vsl_offset, 
856             vsr_dim1, vsr_offset, i__1, i__2;
857
858     /* Local variables */
859     integer ijob;
860     doublereal anrm, bnrm;
861     integer ierr, itau, iwrk, lwrk, i__;
862     extern logical lsame_(char *, char *);
863     integer ileft, icols;
864     logical cursl, ilvsl, ilvsr;
865     integer irwrk, irows;
866     extern /* Subroutine */ int dlabad_(doublereal *, doublereal *);
867     extern doublereal dlamch_(char *);
868     doublereal pl, pr;
869     extern /* Subroutine */ int zggbak_(char *, char *, integer *, integer *, 
870             integer *, doublereal *, doublereal *, integer *, doublecomplex *,
871              integer *, integer *), zggbal_(char *, integer *,
872              doublecomplex *, integer *, doublecomplex *, integer *, integer *
873             , integer *, doublereal *, doublereal *, doublereal *, integer *);
874     logical ilascl, ilbscl;
875     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
876     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
877             integer *, integer *, ftnlen, ftnlen);
878     extern doublereal zlange_(char *, integer *, integer *, doublecomplex *, 
879             integer *, doublereal *);
880     doublereal bignum;
881     integer ijobvl, iright;
882     extern /* Subroutine */ int zgghrd_(char *, char *, integer *, integer *, 
883             integer *, doublecomplex *, integer *, doublecomplex *, integer *,
884              doublecomplex *, integer *, doublecomplex *, integer *, integer *
885             ), zlascl_(char *, integer *, integer *, 
886             doublereal *, doublereal *, integer *, integer *, doublecomplex *,
887              integer *, integer *);
888     integer ijobvr;
889     logical wantsb;
890     integer liwmin;
891     logical wantse, lastsl;
892     doublereal anrmto, bnrmto;
893     extern /* Subroutine */ int zgeqrf_(integer *, integer *, doublecomplex *,
894              integer *, doublecomplex *, doublecomplex *, integer *, integer *
895             );
896     integer maxwrk;
897     logical wantsn;
898     integer minwrk;
899     doublereal smlnum;
900     extern /* Subroutine */ int zhgeqz_(char *, char *, char *, integer *, 
901             integer *, integer *, doublecomplex *, integer *, doublecomplex *,
902              integer *, doublecomplex *, doublecomplex *, doublecomplex *, 
903             integer *, doublecomplex *, integer *, doublecomplex *, integer *,
904              doublereal *, integer *), zlacpy_(char *,
905              integer *, integer *, doublecomplex *, integer *, doublecomplex *
906             , integer *), zlaset_(char *, integer *, integer *, 
907             doublecomplex *, doublecomplex *, doublecomplex *, integer *);
908     logical wantst, lquery, wantsv;
909     extern /* Subroutine */ int ztgsen_(integer *, logical *, logical *, 
910             logical *, integer *, doublecomplex *, integer *, doublecomplex *,
911              integer *, doublecomplex *, doublecomplex *, doublecomplex *, 
912             integer *, doublecomplex *, integer *, integer *, doublereal *, 
913             doublereal *, doublereal *, doublecomplex *, integer *, integer *,
914              integer *, integer *), zungqr_(integer *, integer *, integer *, 
915             doublecomplex *, integer *, doublecomplex *, doublecomplex *, 
916             integer *, integer *), zunmqr_(char *, char *, integer *, integer 
917             *, integer *, doublecomplex *, integer *, doublecomplex *, 
918             doublecomplex *, integer *, doublecomplex *, integer *, integer *);
919     doublereal dif[2];
920     integer ihi, ilo;
921     doublereal eps;
922
923
924 /*  -- LAPACK driver routine (version 3.7.1) -- */
925 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
926 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
927 /*     June 2017 */
928
929
930 /*  ===================================================================== */
931
932
933 /*     Decode the input arguments */
934
935     /* Parameter adjustments */
936     a_dim1 = *lda;
937     a_offset = 1 + a_dim1 * 1;
938     a -= a_offset;
939     b_dim1 = *ldb;
940     b_offset = 1 + b_dim1 * 1;
941     b -= b_offset;
942     --alpha;
943     --beta;
944     vsl_dim1 = *ldvsl;
945     vsl_offset = 1 + vsl_dim1 * 1;
946     vsl -= vsl_offset;
947     vsr_dim1 = *ldvsr;
948     vsr_offset = 1 + vsr_dim1 * 1;
949     vsr -= vsr_offset;
950     --rconde;
951     --rcondv;
952     --work;
953     --rwork;
954     --iwork;
955     --bwork;
956
957     /* Function Body */
958     if (lsame_(jobvsl, "N")) {
959         ijobvl = 1;
960         ilvsl = FALSE_;
961     } else if (lsame_(jobvsl, "V")) {
962         ijobvl = 2;
963         ilvsl = TRUE_;
964     } else {
965         ijobvl = -1;
966         ilvsl = FALSE_;
967     }
968
969     if (lsame_(jobvsr, "N")) {
970         ijobvr = 1;
971         ilvsr = FALSE_;
972     } else if (lsame_(jobvsr, "V")) {
973         ijobvr = 2;
974         ilvsr = TRUE_;
975     } else {
976         ijobvr = -1;
977         ilvsr = FALSE_;
978     }
979
980     wantst = lsame_(sort, "S");
981     wantsn = lsame_(sense, "N");
982     wantse = lsame_(sense, "E");
983     wantsv = lsame_(sense, "V");
984     wantsb = lsame_(sense, "B");
985     lquery = *lwork == -1 || *liwork == -1;
986     if (wantsn) {
987         ijob = 0;
988     } else if (wantse) {
989         ijob = 1;
990     } else if (wantsv) {
991         ijob = 2;
992     } else if (wantsb) {
993         ijob = 4;
994     }
995
996 /*     Test the input arguments */
997
998     *info = 0;
999     if (ijobvl <= 0) {
1000         *info = -1;
1001     } else if (ijobvr <= 0) {
1002         *info = -2;
1003     } else if (! wantst && ! lsame_(sort, "N")) {
1004         *info = -3;
1005     } else if (! (wantsn || wantse || wantsv || wantsb) || ! wantst && ! 
1006             wantsn) {
1007         *info = -5;
1008     } else if (*n < 0) {
1009         *info = -6;
1010     } else if (*lda < f2cmax(1,*n)) {
1011         *info = -8;
1012     } else if (*ldb < f2cmax(1,*n)) {
1013         *info = -10;
1014     } else if (*ldvsl < 1 || ilvsl && *ldvsl < *n) {
1015         *info = -15;
1016     } else if (*ldvsr < 1 || ilvsr && *ldvsr < *n) {
1017         *info = -17;
1018     }
1019
1020 /*     Compute workspace */
1021 /*      (Note: Comments in the code beginning "Workspace:" describe the */
1022 /*       minimal amount of workspace needed at that point in the code, */
1023 /*       as well as the preferred amount for good performance. */
1024 /*       NB refers to the optimal block size for the immediately */
1025 /*       following subroutine, as returned by ILAENV.) */
1026
1027     if (*info == 0) {
1028         if (*n > 0) {
1029             minwrk = *n << 1;
1030             maxwrk = *n * (ilaenv_(&c__1, "ZGEQRF", " ", n, &c__1, n, &c__0, (
1031                     ftnlen)6, (ftnlen)1) + 1);
1032 /* Computing MAX */
1033             i__1 = maxwrk, i__2 = *n * (ilaenv_(&c__1, "ZUNMQR", " ", n, &
1034                     c__1, n, &c_n1, (ftnlen)6, (ftnlen)1) + 1);
1035             maxwrk = f2cmax(i__1,i__2);
1036             if (ilvsl) {
1037 /* Computing MAX */
1038                 i__1 = maxwrk, i__2 = *n * (ilaenv_(&c__1, "ZUNGQR", " ", n, &
1039                         c__1, n, &c_n1, (ftnlen)6, (ftnlen)1) + 1);
1040                 maxwrk = f2cmax(i__1,i__2);
1041             }
1042             lwrk = maxwrk;
1043             if (ijob >= 1) {
1044 /* Computing MAX */
1045                 i__1 = lwrk, i__2 = *n * *n / 2;
1046                 lwrk = f2cmax(i__1,i__2);
1047             }
1048         } else {
1049             minwrk = 1;
1050             maxwrk = 1;
1051             lwrk = 1;
1052         }
1053         work[1].r = (doublereal) lwrk, work[1].i = 0.;
1054         if (wantsn || *n == 0) {
1055             liwmin = 1;
1056         } else {
1057             liwmin = *n + 2;
1058         }
1059         iwork[1] = liwmin;
1060
1061         if (*lwork < minwrk && ! lquery) {
1062             *info = -21;
1063         } else if (*liwork < liwmin && ! lquery) {
1064             *info = -24;
1065         }
1066     }
1067
1068     if (*info != 0) {
1069         i__1 = -(*info);
1070         xerbla_("ZGGESX", &i__1, (ftnlen)6);
1071         return 0;
1072     } else if (lquery) {
1073         return 0;
1074     }
1075
1076 /*     Quick return if possible */
1077
1078     if (*n == 0) {
1079         *sdim = 0;
1080         return 0;
1081     }
1082
1083 /*     Get machine constants */
1084
1085     eps = dlamch_("P");
1086     smlnum = dlamch_("S");
1087     bignum = 1. / smlnum;
1088     dlabad_(&smlnum, &bignum);
1089     smlnum = sqrt(smlnum) / eps;
1090     bignum = 1. / smlnum;
1091
1092 /*     Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
1093
1094     anrm = zlange_("M", n, n, &a[a_offset], lda, &rwork[1]);
1095     ilascl = FALSE_;
1096     if (anrm > 0. && anrm < smlnum) {
1097         anrmto = smlnum;
1098         ilascl = TRUE_;
1099     } else if (anrm > bignum) {
1100         anrmto = bignum;
1101         ilascl = TRUE_;
1102     }
1103     if (ilascl) {
1104         zlascl_("G", &c__0, &c__0, &anrm, &anrmto, n, n, &a[a_offset], lda, &
1105                 ierr);
1106     }
1107
1108 /*     Scale B if f2cmax element outside range [SMLNUM,BIGNUM] */
1109
1110     bnrm = zlange_("M", n, n, &b[b_offset], ldb, &rwork[1]);
1111     ilbscl = FALSE_;
1112     if (bnrm > 0. && bnrm < smlnum) {
1113         bnrmto = smlnum;
1114         ilbscl = TRUE_;
1115     } else if (bnrm > bignum) {
1116         bnrmto = bignum;
1117         ilbscl = TRUE_;
1118     }
1119     if (ilbscl) {
1120         zlascl_("G", &c__0, &c__0, &bnrm, &bnrmto, n, n, &b[b_offset], ldb, &
1121                 ierr);
1122     }
1123
1124 /*     Permute the matrix to make it more nearly triangular */
1125 /*     (Real Workspace: need 6*N) */
1126
1127     ileft = 1;
1128     iright = *n + 1;
1129     irwrk = iright + *n;
1130     zggbal_("P", n, &a[a_offset], lda, &b[b_offset], ldb, &ilo, &ihi, &rwork[
1131             ileft], &rwork[iright], &rwork[irwrk], &ierr);
1132
1133 /*     Reduce B to triangular form (QR decomposition of B) */
1134 /*     (Complex Workspace: need N, prefer N*NB) */
1135
1136     irows = ihi + 1 - ilo;
1137     icols = *n + 1 - ilo;
1138     itau = 1;
1139     iwrk = itau + irows;
1140     i__1 = *lwork + 1 - iwrk;
1141     zgeqrf_(&irows, &icols, &b[ilo + ilo * b_dim1], ldb, &work[itau], &work[
1142             iwrk], &i__1, &ierr);
1143
1144 /*     Apply the unitary transformation to matrix A */
1145 /*     (Complex Workspace: need N, prefer N*NB) */
1146
1147     i__1 = *lwork + 1 - iwrk;
1148     zunmqr_("L", "C", &irows, &icols, &irows, &b[ilo + ilo * b_dim1], ldb, &
1149             work[itau], &a[ilo + ilo * a_dim1], lda, &work[iwrk], &i__1, &
1150             ierr);
1151
1152 /*     Initialize VSL */
1153 /*     (Complex Workspace: need N, prefer N*NB) */
1154
1155     if (ilvsl) {
1156         zlaset_("Full", n, n, &c_b1, &c_b2, &vsl[vsl_offset], ldvsl);
1157         if (irows > 1) {
1158             i__1 = irows - 1;
1159             i__2 = irows - 1;
1160             zlacpy_("L", &i__1, &i__2, &b[ilo + 1 + ilo * b_dim1], ldb, &vsl[
1161                     ilo + 1 + ilo * vsl_dim1], ldvsl);
1162         }
1163         i__1 = *lwork + 1 - iwrk;
1164         zungqr_(&irows, &irows, &irows, &vsl[ilo + ilo * vsl_dim1], ldvsl, &
1165                 work[itau], &work[iwrk], &i__1, &ierr);
1166     }
1167
1168 /*     Initialize VSR */
1169
1170     if (ilvsr) {
1171         zlaset_("Full", n, n, &c_b1, &c_b2, &vsr[vsr_offset], ldvsr);
1172     }
1173
1174 /*     Reduce to generalized Hessenberg form */
1175 /*     (Workspace: none needed) */
1176
1177     zgghrd_(jobvsl, jobvsr, n, &ilo, &ihi, &a[a_offset], lda, &b[b_offset], 
1178             ldb, &vsl[vsl_offset], ldvsl, &vsr[vsr_offset], ldvsr, &ierr);
1179
1180     *sdim = 0;
1181
1182 /*     Perform QZ algorithm, computing Schur vectors if desired */
1183 /*     (Complex Workspace: need N) */
1184 /*     (Real Workspace:    need N) */
1185
1186     iwrk = itau;
1187     i__1 = *lwork + 1 - iwrk;
1188     zhgeqz_("S", jobvsl, jobvsr, n, &ilo, &ihi, &a[a_offset], lda, &b[
1189             b_offset], ldb, &alpha[1], &beta[1], &vsl[vsl_offset], ldvsl, &
1190             vsr[vsr_offset], ldvsr, &work[iwrk], &i__1, &rwork[irwrk], &ierr);
1191     if (ierr != 0) {
1192         if (ierr > 0 && ierr <= *n) {
1193             *info = ierr;
1194         } else if (ierr > *n && ierr <= *n << 1) {
1195             *info = ierr - *n;
1196         } else {
1197             *info = *n + 1;
1198         }
1199         goto L40;
1200     }
1201
1202 /*     Sort eigenvalues ALPHA/BETA and compute the reciprocal of */
1203 /*     condition number(s) */
1204
1205     if (wantst) {
1206
1207 /*        Undo scaling on eigenvalues before SELCTGing */
1208
1209         if (ilascl) {
1210             zlascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alpha[1], n,
1211                      &ierr);
1212         }
1213         if (ilbscl) {
1214             zlascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n, 
1215                     &ierr);
1216         }
1217
1218 /*        Select eigenvalues */
1219
1220         i__1 = *n;
1221         for (i__ = 1; i__ <= i__1; ++i__) {
1222             bwork[i__] = (*selctg)(&alpha[i__], &beta[i__]);
1223 /* L10: */
1224         }
1225
1226 /*        Reorder eigenvalues, transform Generalized Schur vectors, and */
1227 /*        compute reciprocal condition numbers */
1228 /*        (Complex Workspace: If IJOB >= 1, need MAX(1, 2*SDIM*(N-SDIM)) */
1229 /*                            otherwise, need 1 ) */
1230
1231         i__1 = *lwork - iwrk + 1;
1232         ztgsen_(&ijob, &ilvsl, &ilvsr, &bwork[1], n, &a[a_offset], lda, &b[
1233                 b_offset], ldb, &alpha[1], &beta[1], &vsl[vsl_offset], ldvsl, 
1234                 &vsr[vsr_offset], ldvsr, sdim, &pl, &pr, dif, &work[iwrk], &
1235                 i__1, &iwork[1], liwork, &ierr);
1236
1237         if (ijob >= 1) {
1238 /* Computing MAX */
1239             i__1 = maxwrk, i__2 = (*sdim << 1) * (*n - *sdim);
1240             maxwrk = f2cmax(i__1,i__2);
1241         }
1242         if (ierr == -21) {
1243
1244 /*            not enough complex workspace */
1245
1246             *info = -21;
1247         } else {
1248             if (ijob == 1 || ijob == 4) {
1249                 rconde[1] = pl;
1250                 rconde[2] = pr;
1251             }
1252             if (ijob == 2 || ijob == 4) {
1253                 rcondv[1] = dif[0];
1254                 rcondv[2] = dif[1];
1255             }
1256             if (ierr == 1) {
1257                 *info = *n + 3;
1258             }
1259         }
1260
1261     }
1262
1263 /*     Apply permutation to VSL and VSR */
1264 /*     (Workspace: none needed) */
1265
1266     if (ilvsl) {
1267         zggbak_("P", "L", n, &ilo, &ihi, &rwork[ileft], &rwork[iright], n, &
1268                 vsl[vsl_offset], ldvsl, &ierr);
1269     }
1270
1271     if (ilvsr) {
1272         zggbak_("P", "R", n, &ilo, &ihi, &rwork[ileft], &rwork[iright], n, &
1273                 vsr[vsr_offset], ldvsr, &ierr);
1274     }
1275
1276 /*     Undo scaling */
1277
1278     if (ilascl) {
1279         zlascl_("U", &c__0, &c__0, &anrmto, &anrm, n, n, &a[a_offset], lda, &
1280                 ierr);
1281         zlascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alpha[1], n, &
1282                 ierr);
1283     }
1284
1285     if (ilbscl) {
1286         zlascl_("U", &c__0, &c__0, &bnrmto, &bnrm, n, n, &b[b_offset], ldb, &
1287                 ierr);
1288         zlascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n, &
1289                 ierr);
1290     }
1291
1292     if (wantst) {
1293
1294 /*        Check if reordering is correct */
1295
1296         lastsl = TRUE_;
1297         *sdim = 0;
1298         i__1 = *n;
1299         for (i__ = 1; i__ <= i__1; ++i__) {
1300             cursl = (*selctg)(&alpha[i__], &beta[i__]);
1301             if (cursl) {
1302                 ++(*sdim);
1303             }
1304             if (cursl && ! lastsl) {
1305                 *info = *n + 2;
1306             }
1307             lastsl = cursl;
1308 /* L30: */
1309         }
1310
1311     }
1312
1313 L40:
1314
1315     work[1].r = (doublereal) maxwrk, work[1].i = 0.;
1316     iwork[1] = liwmin;
1317
1318     return 0;
1319
1320 /*     End of ZGGESX */
1321
1322 } /* zggesx_ */
1323