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