C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / dggevx.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 doublereal c_b59 = 0.;
518 static doublereal c_b60 = 1.;
519
520 /* > \brief <b> DGGEVX computes the eigenvalues and, optionally, the left and/or right eigenvectors for GE mat
521 rices</b> */
522
523 /*  =========== DOCUMENTATION =========== */
524
525 /* Online html documentation available at */
526 /*            http://www.netlib.org/lapack/explore-html/ */
527
528 /* > \htmlonly */
529 /* > Download DGGEVX + dependencies */
530 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dggevx.
531 f"> */
532 /* > [TGZ]</a> */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dggevx.
534 f"> */
535 /* > [ZIP]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dggevx.
537 f"> */
538 /* > [TXT]</a> */
539 /* > \endhtmlonly */
540
541 /*  Definition: */
542 /*  =========== */
543
544 /*       SUBROUTINE DGGEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, B, LDB, */
545 /*                          ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, ILO, */
546 /*                          IHI, LSCALE, RSCALE, ABNRM, BBNRM, RCONDE, */
547 /*                          RCONDV, WORK, LWORK, IWORK, BWORK, INFO ) */
548
549 /*       CHARACTER          BALANC, JOBVL, JOBVR, SENSE */
550 /*       INTEGER            IHI, ILO, INFO, LDA, LDB, LDVL, LDVR, LWORK, N */
551 /*       DOUBLE PRECISION   ABNRM, BBNRM */
552 /*       LOGICAL            BWORK( * ) */
553 /*       INTEGER            IWORK( * ) */
554 /*       DOUBLE PRECISION   A( LDA, * ), ALPHAI( * ), ALPHAR( * ), */
555 /*      $                   B( LDB, * ), BETA( * ), LSCALE( * ), */
556 /*      $                   RCONDE( * ), RCONDV( * ), RSCALE( * ), */
557 /*      $                   VL( LDVL, * ), VR( LDVR, * ), WORK( * ) */
558
559
560 /* > \par Purpose: */
561 /*  ============= */
562 /* > */
563 /* > \verbatim */
564 /* > */
565 /* > DGGEVX computes for a pair of N-by-N real nonsymmetric matrices (A,B) */
566 /* > the generalized eigenvalues, and optionally, the left and/or right */
567 /* > generalized eigenvectors. */
568 /* > */
569 /* > Optionally also, it computes a balancing transformation to improve */
570 /* > the conditioning of the eigenvalues and eigenvectors (ILO, IHI, */
571 /* > LSCALE, RSCALE, ABNRM, and BBNRM), reciprocal condition numbers for */
572 /* > the eigenvalues (RCONDE), and reciprocal condition numbers for the */
573 /* > right eigenvectors (RCONDV). */
574 /* > */
575 /* > A generalized eigenvalue for a pair of matrices (A,B) is a scalar */
576 /* > lambda or a ratio alpha/beta = lambda, such that A - lambda*B is */
577 /* > singular. It is usually represented as the pair (alpha,beta), as */
578 /* > there is a reasonable interpretation for beta=0, and even for both */
579 /* > being zero. */
580 /* > */
581 /* > The right eigenvector v(j) corresponding to the eigenvalue lambda(j) */
582 /* > of (A,B) satisfies */
583 /* > */
584 /* >                  A * v(j) = lambda(j) * B * v(j) . */
585 /* > */
586 /* > The left eigenvector u(j) corresponding to the eigenvalue lambda(j) */
587 /* > of (A,B) satisfies */
588 /* > */
589 /* >                  u(j)**H * A  = lambda(j) * u(j)**H * B. */
590 /* > */
591 /* > where u(j)**H is the conjugate-transpose of u(j). */
592 /* > */
593 /* > \endverbatim */
594
595 /*  Arguments: */
596 /*  ========== */
597
598 /* > \param[in] BALANC */
599 /* > \verbatim */
600 /* >          BALANC is CHARACTER*1 */
601 /* >          Specifies the balance option to be performed. */
602 /* >          = 'N':  do not diagonally scale or permute; */
603 /* >          = 'P':  permute only; */
604 /* >          = 'S':  scale only; */
605 /* >          = 'B':  both permute and scale. */
606 /* >          Computed reciprocal condition numbers will be for the */
607 /* >          matrices after permuting and/or balancing. Permuting does */
608 /* >          not change condition numbers (in exact arithmetic), but */
609 /* >          balancing does. */
610 /* > \endverbatim */
611 /* > */
612 /* > \param[in] JOBVL */
613 /* > \verbatim */
614 /* >          JOBVL is CHARACTER*1 */
615 /* >          = 'N':  do not compute the left generalized eigenvectors; */
616 /* >          = 'V':  compute the left generalized eigenvectors. */
617 /* > \endverbatim */
618 /* > */
619 /* > \param[in] JOBVR */
620 /* > \verbatim */
621 /* >          JOBVR is CHARACTER*1 */
622 /* >          = 'N':  do not compute the right generalized eigenvectors; */
623 /* >          = 'V':  compute the right generalized eigenvectors. */
624 /* > \endverbatim */
625 /* > */
626 /* > \param[in] SENSE */
627 /* > \verbatim */
628 /* >          SENSE is CHARACTER*1 */
629 /* >          Determines which reciprocal condition numbers are computed. */
630 /* >          = 'N': none are computed; */
631 /* >          = 'E': computed for eigenvalues only; */
632 /* >          = 'V': computed for eigenvectors only; */
633 /* >          = 'B': computed for eigenvalues and eigenvectors. */
634 /* > \endverbatim */
635 /* > */
636 /* > \param[in] N */
637 /* > \verbatim */
638 /* >          N is INTEGER */
639 /* >          The order of the matrices A, B, VL, and VR.  N >= 0. */
640 /* > \endverbatim */
641 /* > */
642 /* > \param[in,out] A */
643 /* > \verbatim */
644 /* >          A is DOUBLE PRECISION array, dimension (LDA, N) */
645 /* >          On entry, the matrix A in the pair (A,B). */
646 /* >          On exit, A has been overwritten. If JOBVL='V' or JOBVR='V' */
647 /* >          or both, then A contains the first part of the real Schur */
648 /* >          form of the "balanced" versions of the input A and B. */
649 /* > \endverbatim */
650 /* > */
651 /* > \param[in] LDA */
652 /* > \verbatim */
653 /* >          LDA is INTEGER */
654 /* >          The leading dimension of A.  LDA >= f2cmax(1,N). */
655 /* > \endverbatim */
656 /* > */
657 /* > \param[in,out] B */
658 /* > \verbatim */
659 /* >          B is DOUBLE PRECISION array, dimension (LDB, N) */
660 /* >          On entry, the matrix B in the pair (A,B). */
661 /* >          On exit, B has been overwritten. If JOBVL='V' or JOBVR='V' */
662 /* >          or both, then B contains the second part of the real Schur */
663 /* >          form of the "balanced" versions of the input A and B. */
664 /* > \endverbatim */
665 /* > */
666 /* > \param[in] LDB */
667 /* > \verbatim */
668 /* >          LDB is INTEGER */
669 /* >          The leading dimension of B.  LDB >= f2cmax(1,N). */
670 /* > \endverbatim */
671 /* > */
672 /* > \param[out] ALPHAR */
673 /* > \verbatim */
674 /* >          ALPHAR is DOUBLE PRECISION array, dimension (N) */
675 /* > \endverbatim */
676 /* > */
677 /* > \param[out] ALPHAI */
678 /* > \verbatim */
679 /* >          ALPHAI is DOUBLE PRECISION array, dimension (N) */
680 /* > \endverbatim */
681 /* > */
682 /* > \param[out] BETA */
683 /* > \verbatim */
684 /* >          BETA is DOUBLE PRECISION array, dimension (N) */
685 /* >          On exit, (ALPHAR(j) + ALPHAI(j)*i)/BETA(j), j=1,...,N, will */
686 /* >          be the generalized eigenvalues.  If ALPHAI(j) is zero, then */
687 /* >          the j-th eigenvalue is real; if positive, then the j-th and */
688 /* >          (j+1)-st eigenvalues are a complex conjugate pair, with */
689 /* >          ALPHAI(j+1) negative. */
690 /* > */
691 /* >          Note: the quotients ALPHAR(j)/BETA(j) and ALPHAI(j)/BETA(j) */
692 /* >          may easily over- or underflow, and BETA(j) may even be zero. */
693 /* >          Thus, the user should avoid naively computing the ratio */
694 /* >          ALPHA/BETA. However, ALPHAR and ALPHAI will be always less */
695 /* >          than and usually comparable with norm(A) in magnitude, and */
696 /* >          BETA always less than and usually comparable with norm(B). */
697 /* > \endverbatim */
698 /* > */
699 /* > \param[out] VL */
700 /* > \verbatim */
701 /* >          VL is DOUBLE PRECISION array, dimension (LDVL,N) */
702 /* >          If JOBVL = 'V', the left eigenvectors u(j) are stored one */
703 /* >          after another in the columns of VL, in the same order as */
704 /* >          their eigenvalues. If the j-th eigenvalue is real, then */
705 /* >          u(j) = VL(:,j), the j-th column of VL. If the j-th and */
706 /* >          (j+1)-th eigenvalues form a complex conjugate pair, then */
707 /* >          u(j) = VL(:,j)+i*VL(:,j+1) and u(j+1) = VL(:,j)-i*VL(:,j+1). */
708 /* >          Each eigenvector will be scaled so the largest component have */
709 /* >          abs(real part) + abs(imag. part) = 1. */
710 /* >          Not referenced if JOBVL = 'N'. */
711 /* > \endverbatim */
712 /* > */
713 /* > \param[in] LDVL */
714 /* > \verbatim */
715 /* >          LDVL is INTEGER */
716 /* >          The leading dimension of the matrix VL. LDVL >= 1, and */
717 /* >          if JOBVL = 'V', LDVL >= N. */
718 /* > \endverbatim */
719 /* > */
720 /* > \param[out] VR */
721 /* > \verbatim */
722 /* >          VR is DOUBLE PRECISION array, dimension (LDVR,N) */
723 /* >          If JOBVR = 'V', the right eigenvectors v(j) are stored one */
724 /* >          after another in the columns of VR, in the same order as */
725 /* >          their eigenvalues. If the j-th eigenvalue is real, then */
726 /* >          v(j) = VR(:,j), the j-th column of VR. If the j-th and */
727 /* >          (j+1)-th eigenvalues form a complex conjugate pair, then */
728 /* >          v(j) = VR(:,j)+i*VR(:,j+1) and v(j+1) = VR(:,j)-i*VR(:,j+1). */
729 /* >          Each eigenvector will be scaled so the largest component have */
730 /* >          abs(real part) + abs(imag. part) = 1. */
731 /* >          Not referenced if JOBVR = 'N'. */
732 /* > \endverbatim */
733 /* > */
734 /* > \param[in] LDVR */
735 /* > \verbatim */
736 /* >          LDVR is INTEGER */
737 /* >          The leading dimension of the matrix VR. LDVR >= 1, and */
738 /* >          if JOBVR = 'V', LDVR >= N. */
739 /* > \endverbatim */
740 /* > */
741 /* > \param[out] ILO */
742 /* > \verbatim */
743 /* >          ILO is INTEGER */
744 /* > \endverbatim */
745 /* > */
746 /* > \param[out] IHI */
747 /* > \verbatim */
748 /* >          IHI is INTEGER */
749 /* >          ILO and IHI are integer values such that on exit */
750 /* >          A(i,j) = 0 and B(i,j) = 0 if i > j and */
751 /* >          j = 1,...,ILO-1 or i = IHI+1,...,N. */
752 /* >          If BALANC = 'N' or 'S', ILO = 1 and IHI = N. */
753 /* > \endverbatim */
754 /* > */
755 /* > \param[out] LSCALE */
756 /* > \verbatim */
757 /* >          LSCALE is DOUBLE PRECISION array, dimension (N) */
758 /* >          Details of the permutations and scaling factors applied */
759 /* >          to the left side of A and B.  If PL(j) is the index of the */
760 /* >          row interchanged with row j, and DL(j) is the scaling */
761 /* >          factor applied to row j, then */
762 /* >            LSCALE(j) = PL(j)  for j = 1,...,ILO-1 */
763 /* >                      = DL(j)  for j = ILO,...,IHI */
764 /* >                      = PL(j)  for j = IHI+1,...,N. */
765 /* >          The order in which the interchanges are made is N to IHI+1, */
766 /* >          then 1 to ILO-1. */
767 /* > \endverbatim */
768 /* > */
769 /* > \param[out] RSCALE */
770 /* > \verbatim */
771 /* >          RSCALE is DOUBLE PRECISION array, dimension (N) */
772 /* >          Details of the permutations and scaling factors applied */
773 /* >          to the right side of A and B.  If PR(j) is the index of the */
774 /* >          column interchanged with column j, and DR(j) is the scaling */
775 /* >          factor applied to column j, then */
776 /* >            RSCALE(j) = PR(j)  for j = 1,...,ILO-1 */
777 /* >                      = DR(j)  for j = ILO,...,IHI */
778 /* >                      = PR(j)  for j = IHI+1,...,N */
779 /* >          The order in which the interchanges are made is N to IHI+1, */
780 /* >          then 1 to ILO-1. */
781 /* > \endverbatim */
782 /* > */
783 /* > \param[out] ABNRM */
784 /* > \verbatim */
785 /* >          ABNRM is DOUBLE PRECISION */
786 /* >          The one-norm of the balanced matrix A. */
787 /* > \endverbatim */
788 /* > */
789 /* > \param[out] BBNRM */
790 /* > \verbatim */
791 /* >          BBNRM is DOUBLE PRECISION */
792 /* >          The one-norm of the balanced matrix B. */
793 /* > \endverbatim */
794 /* > */
795 /* > \param[out] RCONDE */
796 /* > \verbatim */
797 /* >          RCONDE is DOUBLE PRECISION array, dimension (N) */
798 /* >          If SENSE = 'E' or 'B', the reciprocal condition numbers of */
799 /* >          the eigenvalues, stored in consecutive elements of the array. */
800 /* >          For a complex conjugate pair of eigenvalues two consecutive */
801 /* >          elements of RCONDE are set to the same value. Thus RCONDE(j), */
802 /* >          RCONDV(j), and the j-th columns of VL and VR all correspond */
803 /* >          to the j-th eigenpair. */
804 /* >          If SENSE = 'N or 'V', RCONDE is not referenced. */
805 /* > \endverbatim */
806 /* > */
807 /* > \param[out] RCONDV */
808 /* > \verbatim */
809 /* >          RCONDV is DOUBLE PRECISION array, dimension (N) */
810 /* >          If SENSE = 'V' or 'B', the estimated reciprocal condition */
811 /* >          numbers of the eigenvectors, stored in consecutive elements */
812 /* >          of the array. For a complex eigenvector two consecutive */
813 /* >          elements of RCONDV are set to the same value. If the */
814 /* >          eigenvalues cannot be reordered to compute RCONDV(j), */
815 /* >          RCONDV(j) is set to 0; this can only occur when the true */
816 /* >          value would be very small anyway. */
817 /* >          If SENSE = 'N' or 'E', RCONDV is not referenced. */
818 /* > \endverbatim */
819 /* > */
820 /* > \param[out] WORK */
821 /* > \verbatim */
822 /* >          WORK is DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
823 /* >          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
824 /* > \endverbatim */
825 /* > */
826 /* > \param[in] LWORK */
827 /* > \verbatim */
828 /* >          LWORK is INTEGER */
829 /* >          The dimension of the array WORK. LWORK >= f2cmax(1,2*N). */
830 /* >          If BALANC = 'S' or 'B', or JOBVL = 'V', or JOBVR = 'V', */
831 /* >          LWORK >= f2cmax(1,6*N). */
832 /* >          If SENSE = 'E' or 'B', LWORK >= f2cmax(1,10*N). */
833 /* >          If SENSE = 'V' or 'B', LWORK >= 2*N*N+8*N+16. */
834 /* > */
835 /* >          If LWORK = -1, then a workspace query is assumed; the routine */
836 /* >          only calculates the optimal size of the WORK array, returns */
837 /* >          this value as the first entry of the WORK array, and no error */
838 /* >          message related to LWORK is issued by XERBLA. */
839 /* > \endverbatim */
840 /* > */
841 /* > \param[out] IWORK */
842 /* > \verbatim */
843 /* >          IWORK is INTEGER array, dimension (N+6) */
844 /* >          If SENSE = 'E', IWORK is not referenced. */
845 /* > \endverbatim */
846 /* > */
847 /* > \param[out] BWORK */
848 /* > \verbatim */
849 /* >          BWORK is LOGICAL array, dimension (N) */
850 /* >          If SENSE = 'N', BWORK is not referenced. */
851 /* > \endverbatim */
852 /* > */
853 /* > \param[out] INFO */
854 /* > \verbatim */
855 /* >          INFO is INTEGER */
856 /* >          = 0:  successful exit */
857 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value. */
858 /* >          = 1,...,N: */
859 /* >                The QZ iteration failed.  No eigenvectors have been */
860 /* >                calculated, but ALPHAR(j), ALPHAI(j), and BETA(j) */
861 /* >                should be correct for j=INFO+1,...,N. */
862 /* >          > N:  =N+1: other than QZ iteration failed in DHGEQZ. */
863 /* >                =N+2: error return from DTGEVC. */
864 /* > \endverbatim */
865
866 /*  Authors: */
867 /*  ======== */
868
869 /* > \author Univ. of Tennessee */
870 /* > \author Univ. of California Berkeley */
871 /* > \author Univ. of Colorado Denver */
872 /* > \author NAG Ltd. */
873
874 /* > \date April 2012 */
875
876 /* > \ingroup doubleGEeigen */
877
878 /* > \par Further Details: */
879 /*  ===================== */
880 /* > */
881 /* > \verbatim */
882 /* > */
883 /* >  Balancing a matrix pair (A,B) includes, first, permuting rows and */
884 /* >  columns to isolate eigenvalues, second, applying diagonal similarity */
885 /* >  transformation to the rows and columns to make the rows and columns */
886 /* >  as close in norm as possible. The computed reciprocal condition */
887 /* >  numbers correspond to the balanced matrix. Permuting rows and columns */
888 /* >  will not change the condition numbers (in exact arithmetic) but */
889 /* >  diagonal scaling will.  For further explanation of balancing, see */
890 /* >  section 4.11.1.2 of LAPACK Users' Guide. */
891 /* > */
892 /* >  An approximate error bound on the chordal distance between the i-th */
893 /* >  computed generalized eigenvalue w and the corresponding exact */
894 /* >  eigenvalue lambda is */
895 /* > */
896 /* >       chord(w, lambda) <= EPS * norm(ABNRM, BBNRM) / RCONDE(I) */
897 /* > */
898 /* >  An approximate error bound for the angle between the i-th computed */
899 /* >  eigenvector VL(i) or VR(i) is given by */
900 /* > */
901 /* >       EPS * norm(ABNRM, BBNRM) / DIF(i). */
902 /* > */
903 /* >  For further explanation of the reciprocal condition numbers RCONDE */
904 /* >  and RCONDV, see section 4.11 of LAPACK User's Guide. */
905 /* > \endverbatim */
906 /* > */
907 /*  ===================================================================== */
908 /* Subroutine */ int dggevx_(char *balanc, char *jobvl, char *jobvr, char *
909         sense, integer *n, doublereal *a, integer *lda, doublereal *b, 
910         integer *ldb, doublereal *alphar, doublereal *alphai, doublereal *
911         beta, doublereal *vl, integer *ldvl, doublereal *vr, integer *ldvr, 
912         integer *ilo, integer *ihi, doublereal *lscale, doublereal *rscale, 
913         doublereal *abnrm, doublereal *bbnrm, doublereal *rconde, doublereal *
914         rcondv, doublereal *work, integer *lwork, integer *iwork, logical *
915         bwork, integer *info)
916 {
917     /* System generated locals */
918     integer a_dim1, a_offset, b_dim1, b_offset, vl_dim1, vl_offset, vr_dim1, 
919             vr_offset, i__1, i__2;
920     doublereal d__1, d__2, d__3, d__4;
921
922     /* Local variables */
923     logical pair;
924     doublereal anrm, bnrm;
925     integer ierr, itau;
926     doublereal temp;
927     logical ilvl, ilvr;
928     integer iwrk, iwrk1, i__, j, m;
929     extern logical lsame_(char *, char *);
930     integer icols;
931     logical noscl;
932     integer irows;
933     extern /* Subroutine */ int dlabad_(doublereal *, doublereal *);
934     integer jc;
935     extern /* Subroutine */ int dggbak_(char *, char *, integer *, integer *, 
936             integer *, doublereal *, doublereal *, integer *, doublereal *, 
937             integer *, integer *), dggbal_(char *, integer *, 
938             doublereal *, integer *, doublereal *, integer *, integer *, 
939             integer *, doublereal *, doublereal *, doublereal *, integer *);
940     integer in;
941     extern doublereal dlamch_(char *);
942     integer mm;
943     extern doublereal dlange_(char *, integer *, integer *, doublereal *, 
944             integer *, doublereal *);
945     integer jr;
946     extern /* Subroutine */ int dgghrd_(char *, char *, integer *, integer *, 
947             integer *, doublereal *, integer *, doublereal *, integer *, 
948             doublereal *, integer *, doublereal *, integer *, integer *), dlascl_(char *, integer *, integer *, doublereal 
949             *, doublereal *, integer *, integer *, doublereal *, integer *, 
950             integer *);
951     logical ilascl, ilbscl;
952     extern /* Subroutine */ int dgeqrf_(integer *, integer *, doublereal *, 
953             integer *, doublereal *, doublereal *, integer *, integer *), 
954             dlacpy_(char *, integer *, integer *, doublereal *, integer *, 
955             doublereal *, integer *);
956     logical ldumma[1];
957     char chtemp[1];
958     doublereal bignum;
959     extern /* Subroutine */ int dhgeqz_(char *, char *, char *, integer *, 
960             integer *, integer *, doublereal *, integer *, doublereal *, 
961             integer *, doublereal *, doublereal *, doublereal *, doublereal *,
962              integer *, doublereal *, integer *, doublereal *, integer *, 
963             integer *), dlaset_(char *, integer *, 
964             integer *, doublereal *, doublereal *, doublereal *, integer *);
965     integer ijobvl;
966     extern /* Subroutine */ int dtgevc_(char *, char *, logical *, integer *, 
967             doublereal *, integer *, doublereal *, integer *, doublereal *, 
968             integer *, doublereal *, integer *, integer *, integer *, 
969             doublereal *, integer *), dtgsna_(char *, char *, 
970             logical *, integer *, doublereal *, integer *, doublereal *, 
971             integer *, doublereal *, integer *, doublereal *, integer *, 
972             doublereal *, doublereal *, integer *, integer *, doublereal *, 
973             integer *, integer *, integer *), xerbla_(char *, 
974             integer *, ftnlen);
975     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
976             integer *, integer *, ftnlen, ftnlen);
977     integer ijobvr;
978     logical wantsb;
979     extern /* Subroutine */ int dorgqr_(integer *, integer *, integer *, 
980             doublereal *, integer *, doublereal *, doublereal *, integer *, 
981             integer *);
982     doublereal anrmto;
983     logical wantse;
984     doublereal bnrmto;
985     extern /* Subroutine */ int dormqr_(char *, char *, integer *, integer *, 
986             integer *, doublereal *, integer *, doublereal *, doublereal *, 
987             integer *, doublereal *, integer *, integer *);
988     integer minwrk, maxwrk;
989     logical wantsn;
990     doublereal smlnum;
991     logical lquery, wantsv;
992     doublereal eps;
993     logical ilv;
994
995
996 /*  -- LAPACK driver routine (version 3.7.0) -- */
997 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
998 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
999 /*     April 2012 */
1000
1001
1002 /*  ===================================================================== */
1003
1004
1005 /*     Decode the input arguments */
1006
1007     /* Parameter adjustments */
1008     a_dim1 = *lda;
1009     a_offset = 1 + a_dim1 * 1;
1010     a -= a_offset;
1011     b_dim1 = *ldb;
1012     b_offset = 1 + b_dim1 * 1;
1013     b -= b_offset;
1014     --alphar;
1015     --alphai;
1016     --beta;
1017     vl_dim1 = *ldvl;
1018     vl_offset = 1 + vl_dim1 * 1;
1019     vl -= vl_offset;
1020     vr_dim1 = *ldvr;
1021     vr_offset = 1 + vr_dim1 * 1;
1022     vr -= vr_offset;
1023     --lscale;
1024     --rscale;
1025     --rconde;
1026     --rcondv;
1027     --work;
1028     --iwork;
1029     --bwork;
1030
1031     /* Function Body */
1032     if (lsame_(jobvl, "N")) {
1033         ijobvl = 1;
1034         ilvl = FALSE_;
1035     } else if (lsame_(jobvl, "V")) {
1036         ijobvl = 2;
1037         ilvl = TRUE_;
1038     } else {
1039         ijobvl = -1;
1040         ilvl = FALSE_;
1041     }
1042
1043     if (lsame_(jobvr, "N")) {
1044         ijobvr = 1;
1045         ilvr = FALSE_;
1046     } else if (lsame_(jobvr, "V")) {
1047         ijobvr = 2;
1048         ilvr = TRUE_;
1049     } else {
1050         ijobvr = -1;
1051         ilvr = FALSE_;
1052     }
1053     ilv = ilvl || ilvr;
1054
1055     noscl = lsame_(balanc, "N") || lsame_(balanc, "P");
1056     wantsn = lsame_(sense, "N");
1057     wantse = lsame_(sense, "E");
1058     wantsv = lsame_(sense, "V");
1059     wantsb = lsame_(sense, "B");
1060
1061 /*     Test the input arguments */
1062
1063     *info = 0;
1064     lquery = *lwork == -1;
1065     if (! (lsame_(balanc, "N") || lsame_(balanc, "S") || lsame_(balanc, "P") 
1066             || lsame_(balanc, "B"))) {
1067         *info = -1;
1068     } else if (ijobvl <= 0) {
1069         *info = -2;
1070     } else if (ijobvr <= 0) {
1071         *info = -3;
1072     } else if (! (wantsn || wantse || wantsb || wantsv)) {
1073         *info = -4;
1074     } else if (*n < 0) {
1075         *info = -5;
1076     } else if (*lda < f2cmax(1,*n)) {
1077         *info = -7;
1078     } else if (*ldb < f2cmax(1,*n)) {
1079         *info = -9;
1080     } else if (*ldvl < 1 || ilvl && *ldvl < *n) {
1081         *info = -14;
1082     } else if (*ldvr < 1 || ilvr && *ldvr < *n) {
1083         *info = -16;
1084     }
1085
1086 /*     Compute workspace */
1087 /*      (Note: Comments in the code beginning "Workspace:" describe the */
1088 /*       minimal amount of workspace needed at that point in the code, */
1089 /*       as well as the preferred amount for good performance. */
1090 /*       NB refers to the optimal block size for the immediately */
1091 /*       following subroutine, as returned by ILAENV. The workspace is */
1092 /*       computed assuming ILO = 1 and IHI = N, the worst case.) */
1093
1094     if (*info == 0) {
1095         if (*n == 0) {
1096             minwrk = 1;
1097             maxwrk = 1;
1098         } else {
1099             if (noscl && ! ilv) {
1100                 minwrk = *n << 1;
1101             } else {
1102                 minwrk = *n * 6;
1103             }
1104             if (wantse || wantsb) {
1105                 minwrk = *n * 10;
1106             }
1107             if (wantsv || wantsb) {
1108 /* Computing MAX */
1109                 i__1 = minwrk, i__2 = (*n << 1) * (*n + 4) + 16;
1110                 minwrk = f2cmax(i__1,i__2);
1111             }
1112             maxwrk = minwrk;
1113 /* Computing MAX */
1114             i__1 = maxwrk, i__2 = *n + *n * ilaenv_(&c__1, "DGEQRF", " ", n, &
1115                     c__1, n, &c__0, (ftnlen)6, (ftnlen)1);
1116             maxwrk = f2cmax(i__1,i__2);
1117 /* Computing MAX */
1118             i__1 = maxwrk, i__2 = *n + *n * ilaenv_(&c__1, "DORMQR", " ", n, &
1119                     c__1, n, &c__0, (ftnlen)6, (ftnlen)1);
1120             maxwrk = f2cmax(i__1,i__2);
1121             if (ilvl) {
1122 /* Computing MAX */
1123                 i__1 = maxwrk, i__2 = *n + *n * ilaenv_(&c__1, "DORGQR", 
1124                         " ", n, &c__1, n, &c__0, (ftnlen)6, (ftnlen)1);
1125                 maxwrk = f2cmax(i__1,i__2);
1126             }
1127         }
1128         work[1] = (doublereal) maxwrk;
1129
1130         if (*lwork < minwrk && ! lquery) {
1131             *info = -26;
1132         }
1133     }
1134
1135     if (*info != 0) {
1136         i__1 = -(*info);
1137         xerbla_("DGGEVX", &i__1, (ftnlen)6);
1138         return 0;
1139     } else if (lquery) {
1140         return 0;
1141     }
1142
1143 /*     Quick return if possible */
1144
1145     if (*n == 0) {
1146         return 0;
1147     }
1148
1149
1150 /*     Get machine constants */
1151
1152     eps = dlamch_("P");
1153     smlnum = dlamch_("S");
1154     bignum = 1. / smlnum;
1155     dlabad_(&smlnum, &bignum);
1156     smlnum = sqrt(smlnum) / eps;
1157     bignum = 1. / smlnum;
1158
1159 /*     Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
1160
1161     anrm = dlange_("M", n, n, &a[a_offset], lda, &work[1]);
1162     ilascl = FALSE_;
1163     if (anrm > 0. && anrm < smlnum) {
1164         anrmto = smlnum;
1165         ilascl = TRUE_;
1166     } else if (anrm > bignum) {
1167         anrmto = bignum;
1168         ilascl = TRUE_;
1169     }
1170     if (ilascl) {
1171         dlascl_("G", &c__0, &c__0, &anrm, &anrmto, n, n, &a[a_offset], lda, &
1172                 ierr);
1173     }
1174
1175 /*     Scale B if f2cmax element outside range [SMLNUM,BIGNUM] */
1176
1177     bnrm = dlange_("M", n, n, &b[b_offset], ldb, &work[1]);
1178     ilbscl = FALSE_;
1179     if (bnrm > 0. && bnrm < smlnum) {
1180         bnrmto = smlnum;
1181         ilbscl = TRUE_;
1182     } else if (bnrm > bignum) {
1183         bnrmto = bignum;
1184         ilbscl = TRUE_;
1185     }
1186     if (ilbscl) {
1187         dlascl_("G", &c__0, &c__0, &bnrm, &bnrmto, n, n, &b[b_offset], ldb, &
1188                 ierr);
1189     }
1190
1191 /*     Permute and/or balance the matrix pair (A,B) */
1192 /*     (Workspace: need 6*N if BALANC = 'S' or 'B', 1 otherwise) */
1193
1194     dggbal_(balanc, n, &a[a_offset], lda, &b[b_offset], ldb, ilo, ihi, &
1195             lscale[1], &rscale[1], &work[1], &ierr);
1196
1197 /*     Compute ABNRM and BBNRM */
1198
1199     *abnrm = dlange_("1", n, n, &a[a_offset], lda, &work[1]);
1200     if (ilascl) {
1201         work[1] = *abnrm;
1202         dlascl_("G", &c__0, &c__0, &anrmto, &anrm, &c__1, &c__1, &work[1], &
1203                 c__1, &ierr);
1204         *abnrm = work[1];
1205     }
1206
1207     *bbnrm = dlange_("1", n, n, &b[b_offset], ldb, &work[1]);
1208     if (ilbscl) {
1209         work[1] = *bbnrm;
1210         dlascl_("G", &c__0, &c__0, &bnrmto, &bnrm, &c__1, &c__1, &work[1], &
1211                 c__1, &ierr);
1212         *bbnrm = work[1];
1213     }
1214
1215 /*     Reduce B to triangular form (QR decomposition of B) */
1216 /*     (Workspace: need N, prefer N*NB ) */
1217
1218     irows = *ihi + 1 - *ilo;
1219     if (ilv || ! wantsn) {
1220         icols = *n + 1 - *ilo;
1221     } else {
1222         icols = irows;
1223     }
1224     itau = 1;
1225     iwrk = itau + irows;
1226     i__1 = *lwork + 1 - iwrk;
1227     dgeqrf_(&irows, &icols, &b[*ilo + *ilo * b_dim1], ldb, &work[itau], &work[
1228             iwrk], &i__1, &ierr);
1229
1230 /*     Apply the orthogonal transformation to A */
1231 /*     (Workspace: need N, prefer N*NB) */
1232
1233     i__1 = *lwork + 1 - iwrk;
1234     dormqr_("L", "T", &irows, &icols, &irows, &b[*ilo + *ilo * b_dim1], ldb, &
1235             work[itau], &a[*ilo + *ilo * a_dim1], lda, &work[iwrk], &i__1, &
1236             ierr);
1237
1238 /*     Initialize VL and/or VR */
1239 /*     (Workspace: need N, prefer N*NB) */
1240
1241     if (ilvl) {
1242         dlaset_("Full", n, n, &c_b59, &c_b60, &vl[vl_offset], ldvl)
1243                 ;
1244         if (irows > 1) {
1245             i__1 = irows - 1;
1246             i__2 = irows - 1;
1247             dlacpy_("L", &i__1, &i__2, &b[*ilo + 1 + *ilo * b_dim1], ldb, &vl[
1248                     *ilo + 1 + *ilo * vl_dim1], ldvl);
1249         }
1250         i__1 = *lwork + 1 - iwrk;
1251         dorgqr_(&irows, &irows, &irows, &vl[*ilo + *ilo * vl_dim1], ldvl, &
1252                 work[itau], &work[iwrk], &i__1, &ierr);
1253     }
1254
1255     if (ilvr) {
1256         dlaset_("Full", n, n, &c_b59, &c_b60, &vr[vr_offset], ldvr)
1257                 ;
1258     }
1259
1260 /*     Reduce to generalized Hessenberg form */
1261 /*     (Workspace: none needed) */
1262
1263     if (ilv || ! wantsn) {
1264
1265 /*        Eigenvectors requested -- work on whole matrix. */
1266
1267         dgghrd_(jobvl, jobvr, n, ilo, ihi, &a[a_offset], lda, &b[b_offset], 
1268                 ldb, &vl[vl_offset], ldvl, &vr[vr_offset], ldvr, &ierr);
1269     } else {
1270         dgghrd_("N", "N", &irows, &c__1, &irows, &a[*ilo + *ilo * a_dim1], 
1271                 lda, &b[*ilo + *ilo * b_dim1], ldb, &vl[vl_offset], ldvl, &vr[
1272                 vr_offset], ldvr, &ierr);
1273     }
1274
1275 /*     Perform QZ algorithm (Compute eigenvalues, and optionally, the */
1276 /*     Schur forms and Schur vectors) */
1277 /*     (Workspace: need N) */
1278
1279     if (ilv || ! wantsn) {
1280         *(unsigned char *)chtemp = 'S';
1281     } else {
1282         *(unsigned char *)chtemp = 'E';
1283     }
1284
1285     dhgeqz_(chtemp, jobvl, jobvr, n, ilo, ihi, &a[a_offset], lda, &b[b_offset]
1286             , ldb, &alphar[1], &alphai[1], &beta[1], &vl[vl_offset], ldvl, &
1287             vr[vr_offset], ldvr, &work[1], lwork, &ierr);
1288     if (ierr != 0) {
1289         if (ierr > 0 && ierr <= *n) {
1290             *info = ierr;
1291         } else if (ierr > *n && ierr <= *n << 1) {
1292             *info = ierr - *n;
1293         } else {
1294             *info = *n + 1;
1295         }
1296         goto L130;
1297     }
1298
1299 /*     Compute Eigenvectors and estimate condition numbers if desired */
1300 /*     (Workspace: DTGEVC: need 6*N */
1301 /*                 DTGSNA: need 2*N*(N+2)+16 if SENSE = 'V' or 'B', */
1302 /*                         need N otherwise ) */
1303
1304     if (ilv || ! wantsn) {
1305         if (ilv) {
1306             if (ilvl) {
1307                 if (ilvr) {
1308                     *(unsigned char *)chtemp = 'B';
1309                 } else {
1310                     *(unsigned char *)chtemp = 'L';
1311                 }
1312             } else {
1313                 *(unsigned char *)chtemp = 'R';
1314             }
1315
1316             dtgevc_(chtemp, "B", ldumma, n, &a[a_offset], lda, &b[b_offset], 
1317                     ldb, &vl[vl_offset], ldvl, &vr[vr_offset], ldvr, n, &in, &
1318                     work[1], &ierr);
1319             if (ierr != 0) {
1320                 *info = *n + 2;
1321                 goto L130;
1322             }
1323         }
1324
1325         if (! wantsn) {
1326
1327 /*           compute eigenvectors (DTGEVC) and estimate condition */
1328 /*           numbers (DTGSNA). Note that the definition of the condition */
1329 /*           number is not invariant under transformation (u,v) to */
1330 /*           (Q*u, Z*v), where (u,v) are eigenvectors of the generalized */
1331 /*           Schur form (S,T), Q and Z are orthogonal matrices. In order */
1332 /*           to avoid using extra 2*N*N workspace, we have to recalculate */
1333 /*           eigenvectors and estimate one condition numbers at a time. */
1334
1335             pair = FALSE_;
1336             i__1 = *n;
1337             for (i__ = 1; i__ <= i__1; ++i__) {
1338
1339                 if (pair) {
1340                     pair = FALSE_;
1341                     goto L20;
1342                 }
1343                 mm = 1;
1344                 if (i__ < *n) {
1345                     if (a[i__ + 1 + i__ * a_dim1] != 0.) {
1346                         pair = TRUE_;
1347                         mm = 2;
1348                     }
1349                 }
1350
1351                 i__2 = *n;
1352                 for (j = 1; j <= i__2; ++j) {
1353                     bwork[j] = FALSE_;
1354 /* L10: */
1355                 }
1356                 if (mm == 1) {
1357                     bwork[i__] = TRUE_;
1358                 } else if (mm == 2) {
1359                     bwork[i__] = TRUE_;
1360                     bwork[i__ + 1] = TRUE_;
1361                 }
1362
1363                 iwrk = mm * *n + 1;
1364                 iwrk1 = iwrk + mm * *n;
1365
1366 /*              Compute a pair of left and right eigenvectors. */
1367 /*              (compute workspace: need up to 4*N + 6*N) */
1368
1369                 if (wantse || wantsb) {
1370                     dtgevc_("B", "S", &bwork[1], n, &a[a_offset], lda, &b[
1371                             b_offset], ldb, &work[1], n, &work[iwrk], n, &mm, 
1372                             &m, &work[iwrk1], &ierr);
1373                     if (ierr != 0) {
1374                         *info = *n + 2;
1375                         goto L130;
1376                     }
1377                 }
1378
1379                 i__2 = *lwork - iwrk1 + 1;
1380                 dtgsna_(sense, "S", &bwork[1], n, &a[a_offset], lda, &b[
1381                         b_offset], ldb, &work[1], n, &work[iwrk], n, &rconde[
1382                         i__], &rcondv[i__], &mm, &m, &work[iwrk1], &i__2, &
1383                         iwork[1], &ierr);
1384
1385 L20:
1386                 ;
1387             }
1388         }
1389     }
1390
1391 /*     Undo balancing on VL and VR and normalization */
1392 /*     (Workspace: none needed) */
1393
1394     if (ilvl) {
1395         dggbak_(balanc, "L", n, ilo, ihi, &lscale[1], &rscale[1], n, &vl[
1396                 vl_offset], ldvl, &ierr);
1397
1398         i__1 = *n;
1399         for (jc = 1; jc <= i__1; ++jc) {
1400             if (alphai[jc] < 0.) {
1401                 goto L70;
1402             }
1403             temp = 0.;
1404             if (alphai[jc] == 0.) {
1405                 i__2 = *n;
1406                 for (jr = 1; jr <= i__2; ++jr) {
1407 /* Computing MAX */
1408                     d__2 = temp, d__3 = (d__1 = vl[jr + jc * vl_dim1], abs(
1409                             d__1));
1410                     temp = f2cmax(d__2,d__3);
1411 /* L30: */
1412                 }
1413             } else {
1414                 i__2 = *n;
1415                 for (jr = 1; jr <= i__2; ++jr) {
1416 /* Computing MAX */
1417                     d__3 = temp, d__4 = (d__1 = vl[jr + jc * vl_dim1], abs(
1418                             d__1)) + (d__2 = vl[jr + (jc + 1) * vl_dim1], abs(
1419                             d__2));
1420                     temp = f2cmax(d__3,d__4);
1421 /* L40: */
1422                 }
1423             }
1424             if (temp < smlnum) {
1425                 goto L70;
1426             }
1427             temp = 1. / temp;
1428             if (alphai[jc] == 0.) {
1429                 i__2 = *n;
1430                 for (jr = 1; jr <= i__2; ++jr) {
1431                     vl[jr + jc * vl_dim1] *= temp;
1432 /* L50: */
1433                 }
1434             } else {
1435                 i__2 = *n;
1436                 for (jr = 1; jr <= i__2; ++jr) {
1437                     vl[jr + jc * vl_dim1] *= temp;
1438                     vl[jr + (jc + 1) * vl_dim1] *= temp;
1439 /* L60: */
1440                 }
1441             }
1442 L70:
1443             ;
1444         }
1445     }
1446     if (ilvr) {
1447         dggbak_(balanc, "R", n, ilo, ihi, &lscale[1], &rscale[1], n, &vr[
1448                 vr_offset], ldvr, &ierr);
1449         i__1 = *n;
1450         for (jc = 1; jc <= i__1; ++jc) {
1451             if (alphai[jc] < 0.) {
1452                 goto L120;
1453             }
1454             temp = 0.;
1455             if (alphai[jc] == 0.) {
1456                 i__2 = *n;
1457                 for (jr = 1; jr <= i__2; ++jr) {
1458 /* Computing MAX */
1459                     d__2 = temp, d__3 = (d__1 = vr[jr + jc * vr_dim1], abs(
1460                             d__1));
1461                     temp = f2cmax(d__2,d__3);
1462 /* L80: */
1463                 }
1464             } else {
1465                 i__2 = *n;
1466                 for (jr = 1; jr <= i__2; ++jr) {
1467 /* Computing MAX */
1468                     d__3 = temp, d__4 = (d__1 = vr[jr + jc * vr_dim1], abs(
1469                             d__1)) + (d__2 = vr[jr + (jc + 1) * vr_dim1], abs(
1470                             d__2));
1471                     temp = f2cmax(d__3,d__4);
1472 /* L90: */
1473                 }
1474             }
1475             if (temp < smlnum) {
1476                 goto L120;
1477             }
1478             temp = 1. / temp;
1479             if (alphai[jc] == 0.) {
1480                 i__2 = *n;
1481                 for (jr = 1; jr <= i__2; ++jr) {
1482                     vr[jr + jc * vr_dim1] *= temp;
1483 /* L100: */
1484                 }
1485             } else {
1486                 i__2 = *n;
1487                 for (jr = 1; jr <= i__2; ++jr) {
1488                     vr[jr + jc * vr_dim1] *= temp;
1489                     vr[jr + (jc + 1) * vr_dim1] *= temp;
1490 /* L110: */
1491                 }
1492             }
1493 L120:
1494             ;
1495         }
1496     }
1497
1498 /*     Undo scaling if necessary */
1499
1500 L130:
1501
1502     if (ilascl) {
1503         dlascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphar[1], n, &
1504                 ierr);
1505         dlascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphai[1], n, &
1506                 ierr);
1507     }
1508
1509     if (ilbscl) {
1510         dlascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n, &
1511                 ierr);
1512     }
1513
1514     work[1] = (doublereal) maxwrk;
1515     return 0;
1516
1517 /*     End of DGGEVX */
1518
1519 } /* dggevx_ */
1520