C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / sggevx.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 real c_b57 = 0.f;
518 static real c_b58 = 1.f;
519
520 /* > \brief <b> SGGEVX 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 SGGEVX + dependencies */
530 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sggevx.
531 f"> */
532 /* > [TGZ]</a> */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sggevx.
534 f"> */
535 /* > [ZIP]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sggevx.
537 f"> */
538 /* > [TXT]</a> */
539 /* > \endhtmlonly */
540
541 /*  Definition: */
542 /*  =========== */
543
544 /*       SUBROUTINE SGGEVX( 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 /*       REAL               ABNRM, BBNRM */
552 /*       LOGICAL            BWORK( * ) */
553 /*       INTEGER            IWORK( * ) */
554 /*       REAL               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 /* > SGGEVX 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 REAL 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 REAL 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 REAL array, dimension (N) */
675 /* > \endverbatim */
676 /* > */
677 /* > \param[out] ALPHAI */
678 /* > \verbatim */
679 /* >          ALPHAI is REAL array, dimension (N) */
680 /* > \endverbatim */
681 /* > */
682 /* > \param[out] BETA */
683 /* > \verbatim */
684 /* >          BETA is REAL 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 REAL 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 REAL 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 REAL 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 REAL 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 REAL */
786 /* >          The one-norm of the balanced matrix A. */
787 /* > \endverbatim */
788 /* > */
789 /* > \param[out] BBNRM */
790 /* > \verbatim */
791 /* >          BBNRM is REAL */
792 /* >          The one-norm of the balanced matrix B. */
793 /* > \endverbatim */
794 /* > */
795 /* > \param[out] RCONDE */
796 /* > \verbatim */
797 /* >          RCONDE is REAL 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 REAL 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 REAL 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', 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 SHGEQZ. */
863 /* >                =N+2: error return from STGEVC. */
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 realGEeigen */
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 sggevx_(char *balanc, char *jobvl, char *jobvr, char *
909         sense, integer *n, real *a, integer *lda, real *b, integer *ldb, real 
910         *alphar, real *alphai, real *beta, real *vl, integer *ldvl, real *vr, 
911         integer *ldvr, integer *ilo, integer *ihi, real *lscale, real *rscale,
912          real *abnrm, real *bbnrm, real *rconde, real *rcondv, real *work, 
913         integer *lwork, integer *iwork, logical *bwork, integer *info)
914 {
915     /* System generated locals */
916     integer a_dim1, a_offset, b_dim1, b_offset, vl_dim1, vl_offset, vr_dim1, 
917             vr_offset, i__1, i__2;
918     real r__1, r__2, r__3, r__4;
919
920     /* Local variables */
921     logical pair;
922     real anrm, bnrm;
923     integer ierr, itau;
924     real temp;
925     logical ilvl, ilvr;
926     integer iwrk, iwrk1, i__, j, m;
927     extern logical lsame_(char *, char *);
928     integer icols;
929     logical noscl;
930     integer irows, jc;
931     extern /* Subroutine */ int slabad_(real *, real *);
932     integer in, mm, jr;
933     extern /* Subroutine */ int sggbak_(char *, char *, integer *, integer *, 
934             integer *, real *, real *, integer *, real *, integer *, integer *
935             ), sggbal_(char *, integer *, real *, integer *, 
936             real *, integer *, integer *, integer *, real *, real *, real *, 
937             integer *);
938     logical ilascl, ilbscl;
939     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen), sgghrd_(
940             char *, char *, integer *, integer *, integer *, real *, integer *
941             , real *, integer *, real *, integer *, real *, integer *, 
942             integer *);
943     logical ldumma[1];
944     char chtemp[1];
945     real bignum;
946     extern /* Subroutine */ int slascl_(char *, integer *, integer *, real *, 
947             real *, integer *, integer *, real *, integer *, integer *);
948     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
949             integer *, integer *, ftnlen, ftnlen);
950     extern real slamch_(char *);
951     integer ijobvl;
952     extern real slange_(char *, integer *, integer *, real *, integer *, real 
953             *);
954     extern /* Subroutine */ int sgeqrf_(integer *, integer *, real *, integer 
955             *, real *, real *, integer *, integer *);
956     integer ijobvr;
957     extern /* Subroutine */ int slacpy_(char *, integer *, integer *, real *, 
958             integer *, real *, integer *);
959     logical wantsb;
960     extern /* Subroutine */ int slaset_(char *, integer *, integer *, real *, 
961             real *, real *, integer *);
962     real anrmto;
963     logical wantse;
964     real bnrmto;
965     extern /* Subroutine */ int shgeqz_(char *, char *, char *, integer *, 
966             integer *, integer *, real *, integer *, real *, integer *, real *
967             , real *, real *, real *, integer *, real *, integer *, real *, 
968             integer *, integer *), stgevc_(char *, 
969             char *, logical *, integer *, real *, integer *, real *, integer *
970             , real *, integer *, real *, integer *, integer *, integer *, 
971             real *, integer *), stgsna_(char *, char *, 
972             logical *, integer *, real *, integer *, real *, integer *, real *
973             , integer *, real *, integer *, real *, real *, integer *, 
974             integer *, real *, integer *, integer *, integer *);
975     integer minwrk, maxwrk;
976     logical wantsn;
977     real smlnum;
978     extern /* Subroutine */ int sorgqr_(integer *, integer *, integer *, real 
979             *, integer *, real *, real *, integer *, integer *);
980     logical lquery, wantsv;
981     extern /* Subroutine */ int sormqr_(char *, char *, integer *, integer *, 
982             integer *, real *, integer *, real *, real *, integer *, real *, 
983             integer *, integer *);
984     real eps;
985     logical ilv;
986
987
988 /*  -- LAPACK driver routine (version 3.7.0) -- */
989 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
990 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
991 /*     April 2012 */
992
993
994 /*  ===================================================================== */
995
996
997 /*     Decode the input arguments */
998
999     /* Parameter adjustments */
1000     a_dim1 = *lda;
1001     a_offset = 1 + a_dim1 * 1;
1002     a -= a_offset;
1003     b_dim1 = *ldb;
1004     b_offset = 1 + b_dim1 * 1;
1005     b -= b_offset;
1006     --alphar;
1007     --alphai;
1008     --beta;
1009     vl_dim1 = *ldvl;
1010     vl_offset = 1 + vl_dim1 * 1;
1011     vl -= vl_offset;
1012     vr_dim1 = *ldvr;
1013     vr_offset = 1 + vr_dim1 * 1;
1014     vr -= vr_offset;
1015     --lscale;
1016     --rscale;
1017     --rconde;
1018     --rcondv;
1019     --work;
1020     --iwork;
1021     --bwork;
1022
1023     /* Function Body */
1024     if (lsame_(jobvl, "N")) {
1025         ijobvl = 1;
1026         ilvl = FALSE_;
1027     } else if (lsame_(jobvl, "V")) {
1028         ijobvl = 2;
1029         ilvl = TRUE_;
1030     } else {
1031         ijobvl = -1;
1032         ilvl = FALSE_;
1033     }
1034
1035     if (lsame_(jobvr, "N")) {
1036         ijobvr = 1;
1037         ilvr = FALSE_;
1038     } else if (lsame_(jobvr, "V")) {
1039         ijobvr = 2;
1040         ilvr = TRUE_;
1041     } else {
1042         ijobvr = -1;
1043         ilvr = FALSE_;
1044     }
1045     ilv = ilvl || ilvr;
1046
1047     noscl = lsame_(balanc, "N") || lsame_(balanc, "P");
1048     wantsn = lsame_(sense, "N");
1049     wantse = lsame_(sense, "E");
1050     wantsv = lsame_(sense, "V");
1051     wantsb = lsame_(sense, "B");
1052
1053 /*     Test the input arguments */
1054
1055     *info = 0;
1056     lquery = *lwork == -1;
1057     if (! (noscl || lsame_(balanc, "S") || lsame_(
1058             balanc, "B"))) {
1059         *info = -1;
1060     } else if (ijobvl <= 0) {
1061         *info = -2;
1062     } else if (ijobvr <= 0) {
1063         *info = -3;
1064     } else if (! (wantsn || wantse || wantsb || wantsv)) {
1065         *info = -4;
1066     } else if (*n < 0) {
1067         *info = -5;
1068     } else if (*lda < f2cmax(1,*n)) {
1069         *info = -7;
1070     } else if (*ldb < f2cmax(1,*n)) {
1071         *info = -9;
1072     } else if (*ldvl < 1 || ilvl && *ldvl < *n) {
1073         *info = -14;
1074     } else if (*ldvr < 1 || ilvr && *ldvr < *n) {
1075         *info = -16;
1076     }
1077
1078 /*     Compute workspace */
1079 /*      (Note: Comments in the code beginning "Workspace:" describe the */
1080 /*       minimal amount of workspace needed at that point in the code, */
1081 /*       as well as the preferred amount for good performance. */
1082 /*       NB refers to the optimal block size for the immediately */
1083 /*       following subroutine, as returned by ILAENV. The workspace is */
1084 /*       computed assuming ILO = 1 and IHI = N, the worst case.) */
1085
1086     if (*info == 0) {
1087         if (*n == 0) {
1088             minwrk = 1;
1089             maxwrk = 1;
1090         } else {
1091             if (noscl && ! ilv) {
1092                 minwrk = *n << 1;
1093             } else {
1094                 minwrk = *n * 6;
1095             }
1096             if (wantse) {
1097                 minwrk = *n * 10;
1098             } else if (wantsv || wantsb) {
1099                 minwrk = (*n << 1) * (*n + 4) + 16;
1100             }
1101             maxwrk = minwrk;
1102 /* Computing MAX */
1103             i__1 = maxwrk, i__2 = *n + *n * ilaenv_(&c__1, "SGEQRF", " ", n, &
1104                     c__1, n, &c__0, (ftnlen)6, (ftnlen)1);
1105             maxwrk = f2cmax(i__1,i__2);
1106 /* Computing MAX */
1107             i__1 = maxwrk, i__2 = *n + *n * ilaenv_(&c__1, "SORMQR", " ", n, &
1108                     c__1, n, &c__0, (ftnlen)6, (ftnlen)1);
1109             maxwrk = f2cmax(i__1,i__2);
1110             if (ilvl) {
1111 /* Computing MAX */
1112                 i__1 = maxwrk, i__2 = *n + *n * ilaenv_(&c__1, "SORGQR", 
1113                         " ", n, &c__1, n, &c__0, (ftnlen)6, (ftnlen)1);
1114                 maxwrk = f2cmax(i__1,i__2);
1115             }
1116         }
1117         work[1] = (real) maxwrk;
1118
1119         if (*lwork < minwrk && ! lquery) {
1120             *info = -26;
1121         }
1122     }
1123
1124     if (*info != 0) {
1125         i__1 = -(*info);
1126         xerbla_("SGGEVX", &i__1, (ftnlen)6);
1127         return 0;
1128     } else if (lquery) {
1129         return 0;
1130     }
1131
1132 /*     Quick return if possible */
1133
1134     if (*n == 0) {
1135         return 0;
1136     }
1137
1138
1139 /*     Get machine constants */
1140
1141     eps = slamch_("P");
1142     smlnum = slamch_("S");
1143     bignum = 1.f / smlnum;
1144     slabad_(&smlnum, &bignum);
1145     smlnum = sqrt(smlnum) / eps;
1146     bignum = 1.f / smlnum;
1147
1148 /*     Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
1149
1150     anrm = slange_("M", n, n, &a[a_offset], lda, &work[1]);
1151     ilascl = FALSE_;
1152     if (anrm > 0.f && anrm < smlnum) {
1153         anrmto = smlnum;
1154         ilascl = TRUE_;
1155     } else if (anrm > bignum) {
1156         anrmto = bignum;
1157         ilascl = TRUE_;
1158     }
1159     if (ilascl) {
1160         slascl_("G", &c__0, &c__0, &anrm, &anrmto, n, n, &a[a_offset], lda, &
1161                 ierr);
1162     }
1163
1164 /*     Scale B if f2cmax element outside range [SMLNUM,BIGNUM] */
1165
1166     bnrm = slange_("M", n, n, &b[b_offset], ldb, &work[1]);
1167     ilbscl = FALSE_;
1168     if (bnrm > 0.f && bnrm < smlnum) {
1169         bnrmto = smlnum;
1170         ilbscl = TRUE_;
1171     } else if (bnrm > bignum) {
1172         bnrmto = bignum;
1173         ilbscl = TRUE_;
1174     }
1175     if (ilbscl) {
1176         slascl_("G", &c__0, &c__0, &bnrm, &bnrmto, n, n, &b[b_offset], ldb, &
1177                 ierr);
1178     }
1179
1180 /*     Permute and/or balance the matrix pair (A,B) */
1181 /*     (Workspace: need 6*N if BALANC = 'S' or 'B', 1 otherwise) */
1182
1183     sggbal_(balanc, n, &a[a_offset], lda, &b[b_offset], ldb, ilo, ihi, &
1184             lscale[1], &rscale[1], &work[1], &ierr);
1185
1186 /*     Compute ABNRM and BBNRM */
1187
1188     *abnrm = slange_("1", n, n, &a[a_offset], lda, &work[1]);
1189     if (ilascl) {
1190         work[1] = *abnrm;
1191         slascl_("G", &c__0, &c__0, &anrmto, &anrm, &c__1, &c__1, &work[1], &
1192                 c__1, &ierr);
1193         *abnrm = work[1];
1194     }
1195
1196     *bbnrm = slange_("1", n, n, &b[b_offset], ldb, &work[1]);
1197     if (ilbscl) {
1198         work[1] = *bbnrm;
1199         slascl_("G", &c__0, &c__0, &bnrmto, &bnrm, &c__1, &c__1, &work[1], &
1200                 c__1, &ierr);
1201         *bbnrm = work[1];
1202     }
1203
1204 /*     Reduce B to triangular form (QR decomposition of B) */
1205 /*     (Workspace: need N, prefer N*NB ) */
1206
1207     irows = *ihi + 1 - *ilo;
1208     if (ilv || ! wantsn) {
1209         icols = *n + 1 - *ilo;
1210     } else {
1211         icols = irows;
1212     }
1213     itau = 1;
1214     iwrk = itau + irows;
1215     i__1 = *lwork + 1 - iwrk;
1216     sgeqrf_(&irows, &icols, &b[*ilo + *ilo * b_dim1], ldb, &work[itau], &work[
1217             iwrk], &i__1, &ierr);
1218
1219 /*     Apply the orthogonal transformation to A */
1220 /*     (Workspace: need N, prefer N*NB) */
1221
1222     i__1 = *lwork + 1 - iwrk;
1223     sormqr_("L", "T", &irows, &icols, &irows, &b[*ilo + *ilo * b_dim1], ldb, &
1224             work[itau], &a[*ilo + *ilo * a_dim1], lda, &work[iwrk], &i__1, &
1225             ierr);
1226
1227 /*     Initialize VL and/or VR */
1228 /*     (Workspace: need N, prefer N*NB) */
1229
1230     if (ilvl) {
1231         slaset_("Full", n, n, &c_b57, &c_b58, &vl[vl_offset], ldvl)
1232                 ;
1233         if (irows > 1) {
1234             i__1 = irows - 1;
1235             i__2 = irows - 1;
1236             slacpy_("L", &i__1, &i__2, &b[*ilo + 1 + *ilo * b_dim1], ldb, &vl[
1237                     *ilo + 1 + *ilo * vl_dim1], ldvl);
1238         }
1239         i__1 = *lwork + 1 - iwrk;
1240         sorgqr_(&irows, &irows, &irows, &vl[*ilo + *ilo * vl_dim1], ldvl, &
1241                 work[itau], &work[iwrk], &i__1, &ierr);
1242     }
1243
1244     if (ilvr) {
1245         slaset_("Full", n, n, &c_b57, &c_b58, &vr[vr_offset], ldvr)
1246                 ;
1247     }
1248
1249 /*     Reduce to generalized Hessenberg form */
1250 /*     (Workspace: none needed) */
1251
1252     if (ilv || ! wantsn) {
1253
1254 /*        Eigenvectors requested -- work on whole matrix. */
1255
1256         sgghrd_(jobvl, jobvr, n, ilo, ihi, &a[a_offset], lda, &b[b_offset], 
1257                 ldb, &vl[vl_offset], ldvl, &vr[vr_offset], ldvr, &ierr);
1258     } else {
1259         sgghrd_("N", "N", &irows, &c__1, &irows, &a[*ilo + *ilo * a_dim1], 
1260                 lda, &b[*ilo + *ilo * b_dim1], ldb, &vl[vl_offset], ldvl, &vr[
1261                 vr_offset], ldvr, &ierr);
1262     }
1263
1264 /*     Perform QZ algorithm (Compute eigenvalues, and optionally, the */
1265 /*     Schur forms and Schur vectors) */
1266 /*     (Workspace: need N) */
1267
1268     if (ilv || ! wantsn) {
1269         *(unsigned char *)chtemp = 'S';
1270     } else {
1271         *(unsigned char *)chtemp = 'E';
1272     }
1273
1274     shgeqz_(chtemp, jobvl, jobvr, n, ilo, ihi, &a[a_offset], lda, &b[b_offset]
1275             , ldb, &alphar[1], &alphai[1], &beta[1], &vl[vl_offset], ldvl, &
1276             vr[vr_offset], ldvr, &work[1], lwork, &ierr);
1277     if (ierr != 0) {
1278         if (ierr > 0 && ierr <= *n) {
1279             *info = ierr;
1280         } else if (ierr > *n && ierr <= *n << 1) {
1281             *info = ierr - *n;
1282         } else {
1283             *info = *n + 1;
1284         }
1285         goto L130;
1286     }
1287
1288 /*     Compute Eigenvectors and estimate condition numbers if desired */
1289 /*     (Workspace: STGEVC: need 6*N */
1290 /*                 STGSNA: need 2*N*(N+2)+16 if SENSE = 'V' or 'B', */
1291 /*                         need N otherwise ) */
1292
1293     if (ilv || ! wantsn) {
1294         if (ilv) {
1295             if (ilvl) {
1296                 if (ilvr) {
1297                     *(unsigned char *)chtemp = 'B';
1298                 } else {
1299                     *(unsigned char *)chtemp = 'L';
1300                 }
1301             } else {
1302                 *(unsigned char *)chtemp = 'R';
1303             }
1304
1305             stgevc_(chtemp, "B", ldumma, n, &a[a_offset], lda, &b[b_offset], 
1306                     ldb, &vl[vl_offset], ldvl, &vr[vr_offset], ldvr, n, &in, &
1307                     work[1], &ierr);
1308             if (ierr != 0) {
1309                 *info = *n + 2;
1310                 goto L130;
1311             }
1312         }
1313
1314         if (! wantsn) {
1315
1316 /*           compute eigenvectors (STGEVC) and estimate condition */
1317 /*           numbers (STGSNA). Note that the definition of the condition */
1318 /*           number is not invariant under transformation (u,v) to */
1319 /*           (Q*u, Z*v), where (u,v) are eigenvectors of the generalized */
1320 /*           Schur form (S,T), Q and Z are orthogonal matrices. In order */
1321 /*           to avoid using extra 2*N*N workspace, we have to recalculate */
1322 /*           eigenvectors and estimate one condition numbers at a time. */
1323
1324             pair = FALSE_;
1325             i__1 = *n;
1326             for (i__ = 1; i__ <= i__1; ++i__) {
1327
1328                 if (pair) {
1329                     pair = FALSE_;
1330                     goto L20;
1331                 }
1332                 mm = 1;
1333                 if (i__ < *n) {
1334                     if (a[i__ + 1 + i__ * a_dim1] != 0.f) {
1335                         pair = TRUE_;
1336                         mm = 2;
1337                     }
1338                 }
1339
1340                 i__2 = *n;
1341                 for (j = 1; j <= i__2; ++j) {
1342                     bwork[j] = FALSE_;
1343 /* L10: */
1344                 }
1345                 if (mm == 1) {
1346                     bwork[i__] = TRUE_;
1347                 } else if (mm == 2) {
1348                     bwork[i__] = TRUE_;
1349                     bwork[i__ + 1] = TRUE_;
1350                 }
1351
1352                 iwrk = mm * *n + 1;
1353                 iwrk1 = iwrk + mm * *n;
1354
1355 /*              Compute a pair of left and right eigenvectors. */
1356 /*              (compute workspace: need up to 4*N + 6*N) */
1357
1358                 if (wantse || wantsb) {
1359                     stgevc_("B", "S", &bwork[1], n, &a[a_offset], lda, &b[
1360                             b_offset], ldb, &work[1], n, &work[iwrk], n, &mm, 
1361                             &m, &work[iwrk1], &ierr);
1362                     if (ierr != 0) {
1363                         *info = *n + 2;
1364                         goto L130;
1365                     }
1366                 }
1367
1368                 i__2 = *lwork - iwrk1 + 1;
1369                 stgsna_(sense, "S", &bwork[1], n, &a[a_offset], lda, &b[
1370                         b_offset], ldb, &work[1], n, &work[iwrk], n, &rconde[
1371                         i__], &rcondv[i__], &mm, &m, &work[iwrk1], &i__2, &
1372                         iwork[1], &ierr);
1373
1374 L20:
1375                 ;
1376             }
1377         }
1378     }
1379
1380 /*     Undo balancing on VL and VR and normalization */
1381 /*     (Workspace: none needed) */
1382
1383     if (ilvl) {
1384         sggbak_(balanc, "L", n, ilo, ihi, &lscale[1], &rscale[1], n, &vl[
1385                 vl_offset], ldvl, &ierr);
1386
1387         i__1 = *n;
1388         for (jc = 1; jc <= i__1; ++jc) {
1389             if (alphai[jc] < 0.f) {
1390                 goto L70;
1391             }
1392             temp = 0.f;
1393             if (alphai[jc] == 0.f) {
1394                 i__2 = *n;
1395                 for (jr = 1; jr <= i__2; ++jr) {
1396 /* Computing MAX */
1397                     r__2 = temp, r__3 = (r__1 = vl[jr + jc * vl_dim1], abs(
1398                             r__1));
1399                     temp = f2cmax(r__2,r__3);
1400 /* L30: */
1401                 }
1402             } else {
1403                 i__2 = *n;
1404                 for (jr = 1; jr <= i__2; ++jr) {
1405 /* Computing MAX */
1406                     r__3 = temp, r__4 = (r__1 = vl[jr + jc * vl_dim1], abs(
1407                             r__1)) + (r__2 = vl[jr + (jc + 1) * vl_dim1], abs(
1408                             r__2));
1409                     temp = f2cmax(r__3,r__4);
1410 /* L40: */
1411                 }
1412             }
1413             if (temp < smlnum) {
1414                 goto L70;
1415             }
1416             temp = 1.f / temp;
1417             if (alphai[jc] == 0.f) {
1418                 i__2 = *n;
1419                 for (jr = 1; jr <= i__2; ++jr) {
1420                     vl[jr + jc * vl_dim1] *= temp;
1421 /* L50: */
1422                 }
1423             } else {
1424                 i__2 = *n;
1425                 for (jr = 1; jr <= i__2; ++jr) {
1426                     vl[jr + jc * vl_dim1] *= temp;
1427                     vl[jr + (jc + 1) * vl_dim1] *= temp;
1428 /* L60: */
1429                 }
1430             }
1431 L70:
1432             ;
1433         }
1434     }
1435     if (ilvr) {
1436         sggbak_(balanc, "R", n, ilo, ihi, &lscale[1], &rscale[1], n, &vr[
1437                 vr_offset], ldvr, &ierr);
1438         i__1 = *n;
1439         for (jc = 1; jc <= i__1; ++jc) {
1440             if (alphai[jc] < 0.f) {
1441                 goto L120;
1442             }
1443             temp = 0.f;
1444             if (alphai[jc] == 0.f) {
1445                 i__2 = *n;
1446                 for (jr = 1; jr <= i__2; ++jr) {
1447 /* Computing MAX */
1448                     r__2 = temp, r__3 = (r__1 = vr[jr + jc * vr_dim1], abs(
1449                             r__1));
1450                     temp = f2cmax(r__2,r__3);
1451 /* L80: */
1452                 }
1453             } else {
1454                 i__2 = *n;
1455                 for (jr = 1; jr <= i__2; ++jr) {
1456 /* Computing MAX */
1457                     r__3 = temp, r__4 = (r__1 = vr[jr + jc * vr_dim1], abs(
1458                             r__1)) + (r__2 = vr[jr + (jc + 1) * vr_dim1], abs(
1459                             r__2));
1460                     temp = f2cmax(r__3,r__4);
1461 /* L90: */
1462                 }
1463             }
1464             if (temp < smlnum) {
1465                 goto L120;
1466             }
1467             temp = 1.f / temp;
1468             if (alphai[jc] == 0.f) {
1469                 i__2 = *n;
1470                 for (jr = 1; jr <= i__2; ++jr) {
1471                     vr[jr + jc * vr_dim1] *= temp;
1472 /* L100: */
1473                 }
1474             } else {
1475                 i__2 = *n;
1476                 for (jr = 1; jr <= i__2; ++jr) {
1477                     vr[jr + jc * vr_dim1] *= temp;
1478                     vr[jr + (jc + 1) * vr_dim1] *= temp;
1479 /* L110: */
1480                 }
1481             }
1482 L120:
1483             ;
1484         }
1485     }
1486
1487 /*     Undo scaling if necessary */
1488
1489 L130:
1490
1491     if (ilascl) {
1492         slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphar[1], n, &
1493                 ierr);
1494         slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphai[1], n, &
1495                 ierr);
1496     }
1497
1498     if (ilbscl) {
1499         slascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n, &
1500                 ierr);
1501     }
1502
1503     work[1] = (real) maxwrk;
1504     return 0;
1505
1506 /*     End of SGGEVX */
1507
1508 } /* sggevx_ */
1509