C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / cggesx.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 complex c_b1 = {0.f,0.f};
516 static complex c_b2 = {1.f,0.f};
517 static integer c__1 = 1;
518 static integer c__0 = 0;
519 static integer c_n1 = -1;
520
521 /* > \brief <b> CGGESX 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 CGGESX + dependencies */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cggesx.
532 f"> */
533 /* > [TGZ]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cggesx.
535 f"> */
536 /* > [ZIP]</a> */
537 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cggesx.
538 f"> */
539 /* > [TXT]</a> */
540 /* > \endhtmlonly */
541
542 /*  Definition: */
543 /*  =========== */
544
545 /*       SUBROUTINE CGGESX( 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 /*       REAL               RCONDE( 2 ), RCONDV( 2 ), RWORK( * ) */
556 /*       COMPLEX            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 /* > CGGESX 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 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 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 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 array, dimension (N) */
693 /* > \endverbatim */
694 /* > */
695 /* > \param[out] BETA */
696 /* > \verbatim */
697 /* >          BETA is COMPLEX 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 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 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 REAL 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 REAL 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 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 REAL 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 WORK. */
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 CHGEQZ */
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 CTGSEN. */
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 complexGEeigen */
844
845 /*  ===================================================================== */
846 /* Subroutine */ int cggesx_(char *jobvsl, char *jobvsr, char *sort, L_fp 
847         selctg, char *sense, integer *n, complex *a, integer *lda, complex *b,
848          integer *ldb, integer *sdim, complex *alpha, complex *beta, complex *
849         vsl, integer *ldvsl, complex *vsr, integer *ldvsr, real *rconde, real 
850         *rcondv, complex *work, integer *lwork, real *rwork, integer *iwork, 
851         integer *liwork, logical *bwork, integer *info)
852 {
853     /* System generated locals */
854     integer a_dim1, a_offset, b_dim1, b_offset, vsl_dim1, vsl_offset, 
855             vsr_dim1, vsr_offset, i__1, i__2;
856
857     /* Local variables */
858     integer ijob;
859     real anrm, bnrm;
860     integer ierr, itau, iwrk, lwrk, i__;
861     extern logical lsame_(char *, char *);
862     integer ileft, icols;
863     logical cursl, ilvsl, ilvsr;
864     integer irwrk, irows;
865     extern /* Subroutine */ int cggbak_(char *, char *, integer *, integer *, 
866             integer *, real *, real *, integer *, complex *, integer *, 
867             integer *), cggbal_(char *, integer *, complex *, 
868             integer *, complex *, integer *, integer *, integer *, real *, 
869             real *, real *, integer *), slabad_(real *, real *);
870     extern real clange_(char *, integer *, integer *, complex *, integer *, 
871             real *);
872     real pl;
873     extern /* Subroutine */ int cgghrd_(char *, char *, integer *, integer *, 
874             integer *, complex *, integer *, complex *, integer *, complex *, 
875             integer *, complex *, integer *, integer *), 
876             clascl_(char *, integer *, integer *, real *, real *, integer *, 
877             integer *, complex *, integer *, integer *);
878     real pr;
879     logical ilascl, ilbscl;
880     extern /* Subroutine */ int cgeqrf_(integer *, integer *, complex *, 
881             integer *, complex *, complex *, integer *, integer *), clacpy_(
882             char *, integer *, integer *, complex *, integer *, complex *, 
883             integer *), claset_(char *, integer *, integer *, complex 
884             *, complex *, complex *, integer *), xerbla_(char *, 
885             integer *, ftnlen);
886     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
887             integer *, integer *, ftnlen, ftnlen);
888     extern real slamch_(char *);
889     real bignum;
890     extern /* Subroutine */ int chgeqz_(char *, char *, char *, integer *, 
891             integer *, integer *, complex *, integer *, complex *, integer *, 
892             complex *, complex *, complex *, integer *, complex *, integer *, 
893             complex *, integer *, real *, integer *), 
894             ctgsen_(integer *, logical *, logical *, logical *, integer *, 
895             complex *, integer *, complex *, integer *, complex *, complex *, 
896             complex *, integer *, complex *, integer *, integer *, real *, 
897             real *, real *, complex *, integer *, integer *, integer *, 
898             integer *);
899     integer ijobvl, iright, ijobvr;
900     logical wantsb;
901     integer liwmin;
902     logical wantse, lastsl;
903     real anrmto, bnrmto;
904     extern /* Subroutine */ int cungqr_(integer *, integer *, integer *, 
905             complex *, integer *, complex *, complex *, integer *, integer *);
906     integer minwrk, maxwrk;
907     logical wantsn;
908     real smlnum;
909     extern /* Subroutine */ int cunmqr_(char *, char *, integer *, integer *, 
910             integer *, complex *, integer *, complex *, complex *, integer *, 
911             complex *, integer *, integer *);
912     logical wantst, lquery, wantsv;
913     real dif[2];
914     integer ihi, ilo;
915     real eps;
916
917
918 /*  -- LAPACK driver routine (version 3.7.1) -- */
919 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
920 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
921 /*     June 2017 */
922
923
924 /*  ===================================================================== */
925
926
927 /*     Decode the input arguments */
928
929     /* Parameter adjustments */
930     a_dim1 = *lda;
931     a_offset = 1 + a_dim1 * 1;
932     a -= a_offset;
933     b_dim1 = *ldb;
934     b_offset = 1 + b_dim1 * 1;
935     b -= b_offset;
936     --alpha;
937     --beta;
938     vsl_dim1 = *ldvsl;
939     vsl_offset = 1 + vsl_dim1 * 1;
940     vsl -= vsl_offset;
941     vsr_dim1 = *ldvsr;
942     vsr_offset = 1 + vsr_dim1 * 1;
943     vsr -= vsr_offset;
944     --rconde;
945     --rcondv;
946     --work;
947     --rwork;
948     --iwork;
949     --bwork;
950
951     /* Function Body */
952     if (lsame_(jobvsl, "N")) {
953         ijobvl = 1;
954         ilvsl = FALSE_;
955     } else if (lsame_(jobvsl, "V")) {
956         ijobvl = 2;
957         ilvsl = TRUE_;
958     } else {
959         ijobvl = -1;
960         ilvsl = FALSE_;
961     }
962
963     if (lsame_(jobvsr, "N")) {
964         ijobvr = 1;
965         ilvsr = FALSE_;
966     } else if (lsame_(jobvsr, "V")) {
967         ijobvr = 2;
968         ilvsr = TRUE_;
969     } else {
970         ijobvr = -1;
971         ilvsr = FALSE_;
972     }
973
974     wantst = lsame_(sort, "S");
975     wantsn = lsame_(sense, "N");
976     wantse = lsame_(sense, "E");
977     wantsv = lsame_(sense, "V");
978     wantsb = lsame_(sense, "B");
979     lquery = *lwork == -1 || *liwork == -1;
980     if (wantsn) {
981         ijob = 0;
982     } else if (wantse) {
983         ijob = 1;
984     } else if (wantsv) {
985         ijob = 2;
986     } else if (wantsb) {
987         ijob = 4;
988     }
989
990 /*     Test the input arguments */
991
992     *info = 0;
993     if (ijobvl <= 0) {
994         *info = -1;
995     } else if (ijobvr <= 0) {
996         *info = -2;
997     } else if (! wantst && ! lsame_(sort, "N")) {
998         *info = -3;
999     } else if (! (wantsn || wantse || wantsv || wantsb) || ! wantst && ! 
1000             wantsn) {
1001         *info = -5;
1002     } else if (*n < 0) {
1003         *info = -6;
1004     } else if (*lda < f2cmax(1,*n)) {
1005         *info = -8;
1006     } else if (*ldb < f2cmax(1,*n)) {
1007         *info = -10;
1008     } else if (*ldvsl < 1 || ilvsl && *ldvsl < *n) {
1009         *info = -15;
1010     } else if (*ldvsr < 1 || ilvsr && *ldvsr < *n) {
1011         *info = -17;
1012     }
1013
1014 /*     Compute workspace */
1015 /*      (Note: Comments in the code beginning "Workspace:" describe the */
1016 /*       minimal amount of workspace needed at that point in the code, */
1017 /*       as well as the preferred amount for good performance. */
1018 /*       NB refers to the optimal block size for the immediately */
1019 /*       following subroutine, as returned by ILAENV.) */
1020
1021     if (*info == 0) {
1022         if (*n > 0) {
1023             minwrk = *n << 1;
1024             maxwrk = *n * (ilaenv_(&c__1, "CGEQRF", " ", n, &c__1, n, &c__0, (
1025                     ftnlen)6, (ftnlen)1) + 1);
1026 /* Computing MAX */
1027             i__1 = maxwrk, i__2 = *n * (ilaenv_(&c__1, "CUNMQR", " ", n, &
1028                     c__1, n, &c_n1, (ftnlen)6, (ftnlen)1) + 1);
1029             maxwrk = f2cmax(i__1,i__2);
1030             if (ilvsl) {
1031 /* Computing MAX */
1032                 i__1 = maxwrk, i__2 = *n * (ilaenv_(&c__1, "CUNGQR", " ", n, &
1033                         c__1, n, &c_n1, (ftnlen)6, (ftnlen)1) + 1);
1034                 maxwrk = f2cmax(i__1,i__2);
1035             }
1036             lwrk = maxwrk;
1037             if (ijob >= 1) {
1038 /* Computing MAX */
1039                 i__1 = lwrk, i__2 = *n * *n / 2;
1040                 lwrk = f2cmax(i__1,i__2);
1041             }
1042         } else {
1043             minwrk = 1;
1044             maxwrk = 1;
1045             lwrk = 1;
1046         }
1047         work[1].r = (real) lwrk, work[1].i = 0.f;
1048         if (wantsn || *n == 0) {
1049             liwmin = 1;
1050         } else {
1051             liwmin = *n + 2;
1052         }
1053         iwork[1] = liwmin;
1054
1055         if (*lwork < minwrk && ! lquery) {
1056             *info = -21;
1057         } else if (*liwork < liwmin && ! lquery) {
1058             *info = -24;
1059         }
1060     }
1061
1062     if (*info != 0) {
1063         i__1 = -(*info);
1064         xerbla_("CGGESX", &i__1, (ftnlen)6);
1065         return 0;
1066     } else if (lquery) {
1067         return 0;
1068     }
1069
1070 /*     Quick return if possible */
1071
1072     if (*n == 0) {
1073         *sdim = 0;
1074         return 0;
1075     }
1076
1077 /*     Get machine constants */
1078
1079     eps = slamch_("P");
1080     smlnum = slamch_("S");
1081     bignum = 1.f / smlnum;
1082     slabad_(&smlnum, &bignum);
1083     smlnum = sqrt(smlnum) / eps;
1084     bignum = 1.f / smlnum;
1085
1086 /*     Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
1087
1088     anrm = clange_("M", n, n, &a[a_offset], lda, &rwork[1]);
1089     ilascl = FALSE_;
1090     if (anrm > 0.f && anrm < smlnum) {
1091         anrmto = smlnum;
1092         ilascl = TRUE_;
1093     } else if (anrm > bignum) {
1094         anrmto = bignum;
1095         ilascl = TRUE_;
1096     }
1097     if (ilascl) {
1098         clascl_("G", &c__0, &c__0, &anrm, &anrmto, n, n, &a[a_offset], lda, &
1099                 ierr);
1100     }
1101
1102 /*     Scale B if f2cmax element outside range [SMLNUM,BIGNUM] */
1103
1104     bnrm = clange_("M", n, n, &b[b_offset], ldb, &rwork[1]);
1105     ilbscl = FALSE_;
1106     if (bnrm > 0.f && bnrm < smlnum) {
1107         bnrmto = smlnum;
1108         ilbscl = TRUE_;
1109     } else if (bnrm > bignum) {
1110         bnrmto = bignum;
1111         ilbscl = TRUE_;
1112     }
1113     if (ilbscl) {
1114         clascl_("G", &c__0, &c__0, &bnrm, &bnrmto, n, n, &b[b_offset], ldb, &
1115                 ierr);
1116     }
1117
1118 /*     Permute the matrix to make it more nearly triangular */
1119 /*     (Real Workspace: need 6*N) */
1120
1121     ileft = 1;
1122     iright = *n + 1;
1123     irwrk = iright + *n;
1124     cggbal_("P", n, &a[a_offset], lda, &b[b_offset], ldb, &ilo, &ihi, &rwork[
1125             ileft], &rwork[iright], &rwork[irwrk], &ierr);
1126
1127 /*     Reduce B to triangular form (QR decomposition of B) */
1128 /*     (Complex Workspace: need N, prefer N*NB) */
1129
1130     irows = ihi + 1 - ilo;
1131     icols = *n + 1 - ilo;
1132     itau = 1;
1133     iwrk = itau + irows;
1134     i__1 = *lwork + 1 - iwrk;
1135     cgeqrf_(&irows, &icols, &b[ilo + ilo * b_dim1], ldb, &work[itau], &work[
1136             iwrk], &i__1, &ierr);
1137
1138 /*     Apply the unitary transformation to matrix A */
1139 /*     (Complex Workspace: need N, prefer N*NB) */
1140
1141     i__1 = *lwork + 1 - iwrk;
1142     cunmqr_("L", "C", &irows, &icols, &irows, &b[ilo + ilo * b_dim1], ldb, &
1143             work[itau], &a[ilo + ilo * a_dim1], lda, &work[iwrk], &i__1, &
1144             ierr);
1145
1146 /*     Initialize VSL */
1147 /*     (Complex Workspace: need N, prefer N*NB) */
1148
1149     if (ilvsl) {
1150         claset_("Full", n, n, &c_b1, &c_b2, &vsl[vsl_offset], ldvsl);
1151         if (irows > 1) {
1152             i__1 = irows - 1;
1153             i__2 = irows - 1;
1154             clacpy_("L", &i__1, &i__2, &b[ilo + 1 + ilo * b_dim1], ldb, &vsl[
1155                     ilo + 1 + ilo * vsl_dim1], ldvsl);
1156         }
1157         i__1 = *lwork + 1 - iwrk;
1158         cungqr_(&irows, &irows, &irows, &vsl[ilo + ilo * vsl_dim1], ldvsl, &
1159                 work[itau], &work[iwrk], &i__1, &ierr);
1160     }
1161
1162 /*     Initialize VSR */
1163
1164     if (ilvsr) {
1165         claset_("Full", n, n, &c_b1, &c_b2, &vsr[vsr_offset], ldvsr);
1166     }
1167
1168 /*     Reduce to generalized Hessenberg form */
1169 /*     (Workspace: none needed) */
1170
1171     cgghrd_(jobvsl, jobvsr, n, &ilo, &ihi, &a[a_offset], lda, &b[b_offset], 
1172             ldb, &vsl[vsl_offset], ldvsl, &vsr[vsr_offset], ldvsr, &ierr);
1173
1174     *sdim = 0;
1175
1176 /*     Perform QZ algorithm, computing Schur vectors if desired */
1177 /*     (Complex Workspace: need N) */
1178 /*     (Real Workspace:    need N) */
1179
1180     iwrk = itau;
1181     i__1 = *lwork + 1 - iwrk;
1182     chgeqz_("S", jobvsl, jobvsr, n, &ilo, &ihi, &a[a_offset], lda, &b[
1183             b_offset], ldb, &alpha[1], &beta[1], &vsl[vsl_offset], ldvsl, &
1184             vsr[vsr_offset], ldvsr, &work[iwrk], &i__1, &rwork[irwrk], &ierr);
1185     if (ierr != 0) {
1186         if (ierr > 0 && ierr <= *n) {
1187             *info = ierr;
1188         } else if (ierr > *n && ierr <= *n << 1) {
1189             *info = ierr - *n;
1190         } else {
1191             *info = *n + 1;
1192         }
1193         goto L40;
1194     }
1195
1196 /*     Sort eigenvalues ALPHA/BETA and compute the reciprocal of */
1197 /*     condition number(s) */
1198
1199     if (wantst) {
1200
1201 /*        Undo scaling on eigenvalues before SELCTGing */
1202
1203         if (ilascl) {
1204             clascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alpha[1], n,
1205                      &ierr);
1206         }
1207         if (ilbscl) {
1208             clascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n, 
1209                     &ierr);
1210         }
1211
1212 /*        Select eigenvalues */
1213
1214         i__1 = *n;
1215         for (i__ = 1; i__ <= i__1; ++i__) {
1216             bwork[i__] = (*selctg)(&alpha[i__], &beta[i__]);
1217 /* L10: */
1218         }
1219
1220 /*        Reorder eigenvalues, transform Generalized Schur vectors, and */
1221 /*        compute reciprocal condition numbers */
1222 /*        (Complex Workspace: If IJOB >= 1, need MAX(1, 2*SDIM*(N-SDIM)) */
1223 /*                            otherwise, need 1 ) */
1224
1225         i__1 = *lwork - iwrk + 1;
1226         ctgsen_(&ijob, &ilvsl, &ilvsr, &bwork[1], n, &a[a_offset], lda, &b[
1227                 b_offset], ldb, &alpha[1], &beta[1], &vsl[vsl_offset], ldvsl, 
1228                 &vsr[vsr_offset], ldvsr, sdim, &pl, &pr, dif, &work[iwrk], &
1229                 i__1, &iwork[1], liwork, &ierr);
1230
1231         if (ijob >= 1) {
1232 /* Computing MAX */
1233             i__1 = maxwrk, i__2 = (*sdim << 1) * (*n - *sdim);
1234             maxwrk = f2cmax(i__1,i__2);
1235         }
1236         if (ierr == -21) {
1237
1238 /*            not enough complex workspace */
1239
1240             *info = -21;
1241         } else {
1242             if (ijob == 1 || ijob == 4) {
1243                 rconde[1] = pl;
1244                 rconde[2] = pr;
1245             }
1246             if (ijob == 2 || ijob == 4) {
1247                 rcondv[1] = dif[0];
1248                 rcondv[2] = dif[1];
1249             }
1250             if (ierr == 1) {
1251                 *info = *n + 3;
1252             }
1253         }
1254
1255     }
1256
1257 /*     Apply permutation to VSL and VSR */
1258 /*     (Workspace: none needed) */
1259
1260     if (ilvsl) {
1261         cggbak_("P", "L", n, &ilo, &ihi, &rwork[ileft], &rwork[iright], n, &
1262                 vsl[vsl_offset], ldvsl, &ierr);
1263     }
1264
1265     if (ilvsr) {
1266         cggbak_("P", "R", n, &ilo, &ihi, &rwork[ileft], &rwork[iright], n, &
1267                 vsr[vsr_offset], ldvsr, &ierr);
1268     }
1269
1270 /*     Undo scaling */
1271
1272     if (ilascl) {
1273         clascl_("U", &c__0, &c__0, &anrmto, &anrm, n, n, &a[a_offset], lda, &
1274                 ierr);
1275         clascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alpha[1], n, &
1276                 ierr);
1277     }
1278
1279     if (ilbscl) {
1280         clascl_("U", &c__0, &c__0, &bnrmto, &bnrm, n, n, &b[b_offset], ldb, &
1281                 ierr);
1282         clascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n, &
1283                 ierr);
1284     }
1285
1286     if (wantst) {
1287
1288 /*        Check if reordering is correct */
1289
1290         lastsl = TRUE_;
1291         *sdim = 0;
1292         i__1 = *n;
1293         for (i__ = 1; i__ <= i__1; ++i__) {
1294             cursl = (*selctg)(&alpha[i__], &beta[i__]);
1295             if (cursl) {
1296                 ++(*sdim);
1297             }
1298             if (cursl && ! lastsl) {
1299                 *info = *n + 2;
1300             }
1301             lastsl = cursl;
1302 /* L30: */
1303         }
1304
1305     }
1306
1307 L40:
1308
1309     work[1].r = (real) maxwrk, work[1].i = 0.f;
1310     iwork[1] = liwmin;
1311
1312     return 0;
1313
1314 /*     End of CGGESX */
1315
1316 } /* cggesx_ */
1317