C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / zlaqr4.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]/Cd(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__13 = 13;
516 static integer c__15 = 15;
517 static integer c_n1 = -1;
518 static integer c__12 = 12;
519 static integer c__14 = 14;
520 static integer c__16 = 16;
521 static logical c_false = FALSE_;
522 static integer c__1 = 1;
523 static integer c__3 = 3;
524
525 /* > \brief \b ZLAQR4 computes the eigenvalues of a Hessenberg matrix, and optionally the matrices from the Sc
526 hur decomposition. */
527
528 /*  =========== DOCUMENTATION =========== */
529
530 /* Online html documentation available at */
531 /*            http://www.netlib.org/lapack/explore-html/ */
532
533 /* > \htmlonly */
534 /* > Download ZLAQR4 + dependencies */
535 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zlaqr4.
536 f"> */
537 /* > [TGZ]</a> */
538 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zlaqr4.
539 f"> */
540 /* > [ZIP]</a> */
541 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zlaqr4.
542 f"> */
543 /* > [TXT]</a> */
544 /* > \endhtmlonly */
545
546 /*  Definition: */
547 /*  =========== */
548
549 /*       SUBROUTINE ZLAQR4( WANTT, WANTZ, N, ILO, IHI, H, LDH, W, ILOZ, */
550 /*                          IHIZ, Z, LDZ, WORK, LWORK, INFO ) */
551
552 /*       INTEGER            IHI, IHIZ, ILO, ILOZ, INFO, LDH, LDZ, LWORK, N */
553 /*       LOGICAL            WANTT, WANTZ */
554 /*       COMPLEX*16         H( LDH, * ), W( * ), WORK( * ), Z( LDZ, * ) */
555
556
557 /* > \par Purpose: */
558 /*  ============= */
559 /* > */
560 /* > \verbatim */
561 /* > */
562 /* >    ZLAQR4 implements one level of recursion for ZLAQR0. */
563 /* >    It is a complete implementation of the small bulge multi-shift */
564 /* >    QR algorithm.  It may be called by ZLAQR0 and, for large enough */
565 /* >    deflation window size, it may be called by ZLAQR3.  This */
566 /* >    subroutine is identical to ZLAQR0 except that it calls ZLAQR2 */
567 /* >    instead of ZLAQR3. */
568 /* > */
569 /* >    ZLAQR4 computes the eigenvalues of a Hessenberg matrix H */
570 /* >    and, optionally, the matrices T and Z from the Schur decomposition */
571 /* >    H = Z T Z**H, where T is an upper triangular matrix (the */
572 /* >    Schur form), and Z is the unitary matrix of Schur vectors. */
573 /* > */
574 /* >    Optionally Z may be postmultiplied into an input unitary */
575 /* >    matrix Q so that this routine can give the Schur factorization */
576 /* >    of a matrix A which has been reduced to the Hessenberg form H */
577 /* >    by the unitary matrix Q:  A = Q*H*Q**H = (QZ)*H*(QZ)**H. */
578 /* > \endverbatim */
579
580 /*  Arguments: */
581 /*  ========== */
582
583 /* > \param[in] WANTT */
584 /* > \verbatim */
585 /* >          WANTT is LOGICAL */
586 /* >          = .TRUE. : the full Schur form T is required; */
587 /* >          = .FALSE.: only eigenvalues are required. */
588 /* > \endverbatim */
589 /* > */
590 /* > \param[in] WANTZ */
591 /* > \verbatim */
592 /* >          WANTZ is LOGICAL */
593 /* >          = .TRUE. : the matrix of Schur vectors Z is required; */
594 /* >          = .FALSE.: Schur vectors are not required. */
595 /* > \endverbatim */
596 /* > */
597 /* > \param[in] N */
598 /* > \verbatim */
599 /* >          N is INTEGER */
600 /* >           The order of the matrix H.  N >= 0. */
601 /* > \endverbatim */
602 /* > */
603 /* > \param[in] ILO */
604 /* > \verbatim */
605 /* >          ILO is INTEGER */
606 /* > \endverbatim */
607 /* > */
608 /* > \param[in] IHI */
609 /* > \verbatim */
610 /* >          IHI is INTEGER */
611 /* >           It is assumed that H is already upper triangular in rows */
612 /* >           and columns 1:ILO-1 and IHI+1:N and, if ILO > 1, */
613 /* >           H(ILO,ILO-1) is zero. ILO and IHI are normally set by a */
614 /* >           previous call to ZGEBAL, and then passed to ZGEHRD when the */
615 /* >           matrix output by ZGEBAL is reduced to Hessenberg form. */
616 /* >           Otherwise, ILO and IHI should be set to 1 and N, */
617 /* >           respectively.  If N > 0, then 1 <= ILO <= IHI <= N. */
618 /* >           If N = 0, then ILO = 1 and IHI = 0. */
619 /* > \endverbatim */
620 /* > */
621 /* > \param[in,out] H */
622 /* > \verbatim */
623 /* >          H is COMPLEX*16 array, dimension (LDH,N) */
624 /* >           On entry, the upper Hessenberg matrix H. */
625 /* >           On exit, if INFO = 0 and WANTT is .TRUE., then H */
626 /* >           contains the upper triangular matrix T from the Schur */
627 /* >           decomposition (the Schur form). If INFO = 0 and WANT is */
628 /* >           .FALSE., then the contents of H are unspecified on exit. */
629 /* >           (The output value of H when INFO > 0 is given under the */
630 /* >           description of INFO below.) */
631 /* > */
632 /* >           This subroutine may explicitly set H(i,j) = 0 for i > j and */
633 /* >           j = 1, 2, ... ILO-1 or j = IHI+1, IHI+2, ... N. */
634 /* > \endverbatim */
635 /* > */
636 /* > \param[in] LDH */
637 /* > \verbatim */
638 /* >          LDH is INTEGER */
639 /* >           The leading dimension of the array H. LDH >= f2cmax(1,N). */
640 /* > \endverbatim */
641 /* > */
642 /* > \param[out] W */
643 /* > \verbatim */
644 /* >          W is COMPLEX*16 array, dimension (N) */
645 /* >           The computed eigenvalues of H(ILO:IHI,ILO:IHI) are stored */
646 /* >           in W(ILO:IHI). If WANTT is .TRUE., then the eigenvalues are */
647 /* >           stored in the same order as on the diagonal of the Schur */
648 /* >           form returned in H, with W(i) = H(i,i). */
649 /* > \endverbatim */
650 /* > */
651 /* > \param[in] ILOZ */
652 /* > \verbatim */
653 /* >          ILOZ is INTEGER */
654 /* > \endverbatim */
655 /* > */
656 /* > \param[in] IHIZ */
657 /* > \verbatim */
658 /* >          IHIZ is INTEGER */
659 /* >           Specify the rows of Z to which transformations must be */
660 /* >           applied if WANTZ is .TRUE.. */
661 /* >           1 <= ILOZ <= ILO; IHI <= IHIZ <= N. */
662 /* > \endverbatim */
663 /* > */
664 /* > \param[in,out] Z */
665 /* > \verbatim */
666 /* >          Z is COMPLEX*16 array, dimension (LDZ,IHI) */
667 /* >           If WANTZ is .FALSE., then Z is not referenced. */
668 /* >           If WANTZ is .TRUE., then Z(ILO:IHI,ILOZ:IHIZ) is */
669 /* >           replaced by Z(ILO:IHI,ILOZ:IHIZ)*U where U is the */
670 /* >           orthogonal Schur factor of H(ILO:IHI,ILO:IHI). */
671 /* >           (The output value of Z when INFO > 0 is given under */
672 /* >           the description of INFO below.) */
673 /* > \endverbatim */
674 /* > */
675 /* > \param[in] LDZ */
676 /* > \verbatim */
677 /* >          LDZ is INTEGER */
678 /* >           The leading dimension of the array Z.  if WANTZ is .TRUE. */
679 /* >           then LDZ >= MAX(1,IHIZ).  Otherwise, LDZ >= 1. */
680 /* > \endverbatim */
681 /* > */
682 /* > \param[out] WORK */
683 /* > \verbatim */
684 /* >          WORK is COMPLEX*16 array, dimension LWORK */
685 /* >           On exit, if LWORK = -1, WORK(1) returns an estimate of */
686 /* >           the optimal value for LWORK. */
687 /* > \endverbatim */
688 /* > */
689 /* > \param[in] LWORK */
690 /* > \verbatim */
691 /* >          LWORK is INTEGER */
692 /* >           The dimension of the array WORK.  LWORK >= f2cmax(1,N) */
693 /* >           is sufficient, but LWORK typically as large as 6*N may */
694 /* >           be required for optimal performance.  A workspace query */
695 /* >           to determine the optimal workspace size is recommended. */
696 /* > */
697 /* >           If LWORK = -1, then ZLAQR4 does a workspace query. */
698 /* >           In this case, ZLAQR4 checks the input parameters and */
699 /* >           estimates the optimal workspace size for the given */
700 /* >           values of N, ILO and IHI.  The estimate is returned */
701 /* >           in WORK(1).  No error message related to LWORK is */
702 /* >           issued by XERBLA.  Neither H nor Z are accessed. */
703 /* > \endverbatim */
704 /* > */
705 /* > \param[out] INFO */
706 /* > \verbatim */
707 /* >          INFO is INTEGER */
708 /* >             =  0:  successful exit */
709 /* >             > 0:  if INFO = i, ZLAQR4 failed to compute all of */
710 /* >                the eigenvalues.  Elements 1:ilo-1 and i+1:n of WR */
711 /* >                and WI contain those eigenvalues which have been */
712 /* >                successfully computed.  (Failures are rare.) */
713 /* > */
714 /* >                If INFO > 0 and WANT is .FALSE., then on exit, */
715 /* >                the remaining unconverged eigenvalues are the eigen- */
716 /* >                values of the upper Hessenberg matrix rows and */
717 /* >                columns ILO through INFO of the final, output */
718 /* >                value of H. */
719 /* > */
720 /* >                If INFO > 0 and WANTT is .TRUE., then on exit */
721 /* > */
722 /* >           (*)  (initial value of H)*U  = U*(final value of H) */
723 /* > */
724 /* >                where U is a unitary matrix.  The final */
725 /* >                value of  H is upper Hessenberg and triangular in */
726 /* >                rows and columns INFO+1 through IHI. */
727 /* > */
728 /* >                If INFO > 0 and WANTZ is .TRUE., then on exit */
729 /* > */
730 /* >                  (final value of Z(ILO:IHI,ILOZ:IHIZ) */
731 /* >                   =  (initial value of Z(ILO:IHI,ILOZ:IHIZ)*U */
732 /* > */
733 /* >                where U is the unitary matrix in (*) (regard- */
734 /* >                less of the value of WANTT.) */
735 /* > */
736 /* >                If INFO > 0 and WANTZ is .FALSE., then Z is not */
737 /* >                accessed. */
738 /* > \endverbatim */
739
740 /*  Authors: */
741 /*  ======== */
742
743 /* > \author Univ. of Tennessee */
744 /* > \author Univ. of California Berkeley */
745 /* > \author Univ. of Colorado Denver */
746 /* > \author NAG Ltd. */
747
748 /* > \date December 2016 */
749
750 /* > \ingroup complex16OTHERauxiliary */
751
752 /* > \par Contributors: */
753 /*  ================== */
754 /* > */
755 /* >       Karen Braman and Ralph Byers, Department of Mathematics, */
756 /* >       University of Kansas, USA */
757
758 /* > \par References: */
759 /*  ================ */
760 /* > */
761 /* >       K. Braman, R. Byers and R. Mathias, The Multi-Shift QR */
762 /* >       Algorithm Part I: Maintaining Well Focused Shifts, and Level 3 */
763 /* >       Performance, SIAM Journal of Matrix Analysis, volume 23, pages */
764 /* >       929--947, 2002. */
765 /* > \n */
766 /* >       K. Braman, R. Byers and R. Mathias, The Multi-Shift QR */
767 /* >       Algorithm Part II: Aggressive Early Deflation, SIAM Journal */
768 /* >       of Matrix Analysis, volume 23, pages 948--973, 2002. */
769 /* > */
770 /*  ===================================================================== */
771 /* Subroutine */ int zlaqr4_(logical *wantt, logical *wantz, integer *n, 
772         integer *ilo, integer *ihi, doublecomplex *h__, integer *ldh, 
773         doublecomplex *w, integer *iloz, integer *ihiz, doublecomplex *z__, 
774         integer *ldz, doublecomplex *work, integer *lwork, integer *info)
775 {
776     /* System generated locals */
777     integer h_dim1, h_offset, z_dim1, z_offset, i__1, i__2, i__3, i__4, i__5;
778     doublereal d__1, d__2, d__3, d__4, d__5, d__6, d__7, d__8;
779     doublecomplex z__1, z__2, z__3, z__4, z__5;
780
781     /* Local variables */
782     integer ndec, ndfl, kbot, nmin;
783     doublecomplex swap;
784     integer ktop;
785     doublecomplex zdum[1]       /* was [1][1] */;
786     integer kacc22, i__, k;
787     doublereal s;
788     integer itmax, nsmax, nwmax, kwtop;
789     doublecomplex aa, bb, cc, dd;
790     extern /* Subroutine */ int zlaqr2_(logical *, logical *, integer *, 
791             integer *, integer *, integer *, doublecomplex *, integer *, 
792             integer *, integer *, doublecomplex *, integer *, integer *, 
793             integer *, doublecomplex *, doublecomplex *, integer *, integer *,
794              doublecomplex *, integer *, integer *, doublecomplex *, integer *
795             , doublecomplex *, integer *), zlaqr5_(logical *, logical *, 
796             integer *, integer *, integer *, integer *, integer *, 
797             doublecomplex *, doublecomplex *, integer *, integer *, integer *,
798              doublecomplex *, integer *, doublecomplex *, integer *, 
799             doublecomplex *, integer *, integer *, doublecomplex *, integer *,
800              integer *, doublecomplex *, integer *);
801     integer ld, nh, nibble, it, ks, kt, ku, kv, ls, ns, nw;
802     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
803             integer *, integer *, ftnlen, ftnlen);
804     char jbcmpz[2];
805     doublecomplex rtdisc;
806     integer nwupbd;
807     logical sorted;
808     extern /* Subroutine */ int zlahqr_(logical *, logical *, integer *, 
809             integer *, integer *, doublecomplex *, integer *, doublecomplex *,
810              integer *, integer *, doublecomplex *, integer *, integer *), 
811             zlacpy_(char *, integer *, integer *, doublecomplex *, integer *, 
812             doublecomplex *, integer *);
813     integer lwkopt;
814     doublecomplex tr2, det;
815     integer inf, kdu, nho, nve, kwh, nsr, nwr, kwv;
816
817
818 /*  -- LAPACK auxiliary routine (version 3.7.0) -- */
819 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
820 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
821 /*     December 2016 */
822
823
824 /*  ================================================================ */
825
826
827 /*     ==== Matrices of order NTINY or smaller must be processed by */
828 /*     .    ZLAHQR because of insufficient subdiagonal scratch space. */
829 /*     .    (This is a hard limit.) ==== */
830
831 /*     ==== Exceptional deflation windows:  try to cure rare */
832 /*     .    slow convergence by varying the size of the */
833 /*     .    deflation window after KEXNW iterations. ==== */
834
835 /*     ==== Exceptional shifts: try to cure rare slow convergence */
836 /*     .    with ad-hoc exceptional shifts every KEXSH iterations. */
837 /*     .    ==== */
838
839 /*     ==== The constant WILK1 is used to form the exceptional */
840 /*     .    shifts. ==== */
841     /* Parameter adjustments */
842     h_dim1 = *ldh;
843     h_offset = 1 + h_dim1 * 1;
844     h__ -= h_offset;
845     --w;
846     z_dim1 = *ldz;
847     z_offset = 1 + z_dim1 * 1;
848     z__ -= z_offset;
849     --work;
850
851     /* Function Body */
852     *info = 0;
853
854 /*     ==== Quick return for N = 0: nothing to do. ==== */
855
856     if (*n == 0) {
857         work[1].r = 1., work[1].i = 0.;
858         return 0;
859     }
860
861     if (*n <= 15) {
862
863 /*        ==== Tiny matrices must use ZLAHQR. ==== */
864
865         lwkopt = 1;
866         if (*lwork != -1) {
867             zlahqr_(wantt, wantz, n, ilo, ihi, &h__[h_offset], ldh, &w[1], 
868                     iloz, ihiz, &z__[z_offset], ldz, info);
869         }
870     } else {
871
872 /*        ==== Use small bulge multi-shift QR with aggressive early */
873 /*        .    deflation on larger-than-tiny matrices. ==== */
874
875 /*        ==== Hope for the best. ==== */
876
877         *info = 0;
878
879 /*        ==== Set up job flags for ILAENV. ==== */
880
881         if (*wantt) {
882             *(unsigned char *)jbcmpz = 'S';
883         } else {
884             *(unsigned char *)jbcmpz = 'E';
885         }
886         if (*wantz) {
887             *(unsigned char *)&jbcmpz[1] = 'V';
888         } else {
889             *(unsigned char *)&jbcmpz[1] = 'N';
890         }
891
892 /*        ==== NWR = recommended deflation window size.  At this */
893 /*        .    point,  N .GT. NTINY = 15, so there is enough */
894 /*        .    subdiagonal workspace for NWR.GE.2 as required. */
895 /*        .    (In fact, there is enough subdiagonal space for */
896 /*        .    NWR.GE.4.) ==== */
897
898         nwr = ilaenv_(&c__13, "ZLAQR4", jbcmpz, n, ilo, ihi, lwork, (ftnlen)6,
899                  (ftnlen)2);
900         nwr = f2cmax(2,nwr);
901 /* Computing MIN */
902         i__1 = *ihi - *ilo + 1, i__2 = (*n - 1) / 3, i__1 = f2cmin(i__1,i__2);
903         nwr = f2cmin(i__1,nwr);
904
905 /*        ==== NSR = recommended number of simultaneous shifts. */
906 /*        .    At this point N .GT. NTINY = 15, so there is at */
907 /*        .    enough subdiagonal workspace for NSR to be even */
908 /*        .    and greater than or equal to two as required. ==== */
909
910         nsr = ilaenv_(&c__15, "ZLAQR4", jbcmpz, n, ilo, ihi, lwork, (ftnlen)6,
911                  (ftnlen)2);
912 /* Computing MIN */
913         i__1 = nsr, i__2 = (*n - 3) / 6, i__1 = f2cmin(i__1,i__2), i__2 = *ihi - 
914                 *ilo;
915         nsr = f2cmin(i__1,i__2);
916 /* Computing MAX */
917         i__1 = 2, i__2 = nsr - nsr % 2;
918         nsr = f2cmax(i__1,i__2);
919
920 /*        ==== Estimate optimal workspace ==== */
921
922 /*        ==== Workspace query call to ZLAQR2 ==== */
923
924         i__1 = nwr + 1;
925         zlaqr2_(wantt, wantz, n, ilo, ihi, &i__1, &h__[h_offset], ldh, iloz, 
926                 ihiz, &z__[z_offset], ldz, &ls, &ld, &w[1], &h__[h_offset], 
927                 ldh, n, &h__[h_offset], ldh, n, &h__[h_offset], ldh, &work[1],
928                  &c_n1);
929
930 /*        ==== Optimal workspace = MAX(ZLAQR5, ZLAQR2) ==== */
931
932 /* Computing MAX */
933         i__1 = nsr * 3 / 2, i__2 = (integer) work[1].r;
934         lwkopt = f2cmax(i__1,i__2);
935
936 /*        ==== Quick return in case of workspace query. ==== */
937
938         if (*lwork == -1) {
939             d__1 = (doublereal) lwkopt;
940             z__1.r = d__1, z__1.i = 0.;
941             work[1].r = z__1.r, work[1].i = z__1.i;
942             return 0;
943         }
944
945 /*        ==== ZLAHQR/ZLAQR0 crossover point ==== */
946
947         nmin = ilaenv_(&c__12, "ZLAQR4", jbcmpz, n, ilo, ihi, lwork, (ftnlen)
948                 6, (ftnlen)2);
949         nmin = f2cmax(15,nmin);
950
951 /*        ==== Nibble crossover point ==== */
952
953         nibble = ilaenv_(&c__14, "ZLAQR4", jbcmpz, n, ilo, ihi, lwork, (
954                 ftnlen)6, (ftnlen)2);
955         nibble = f2cmax(0,nibble);
956
957 /*        ==== Accumulate reflections during ttswp?  Use block */
958 /*        .    2-by-2 structure during matrix-matrix multiply? ==== */
959
960         kacc22 = ilaenv_(&c__16, "ZLAQR4", jbcmpz, n, ilo, ihi, lwork, (
961                 ftnlen)6, (ftnlen)2);
962         kacc22 = f2cmax(0,kacc22);
963         kacc22 = f2cmin(2,kacc22);
964
965 /*        ==== NWMAX = the largest possible deflation window for */
966 /*        .    which there is sufficient workspace. ==== */
967
968 /* Computing MIN */
969         i__1 = (*n - 1) / 3, i__2 = *lwork / 2;
970         nwmax = f2cmin(i__1,i__2);
971         nw = nwmax;
972
973 /*        ==== NSMAX = the Largest number of simultaneous shifts */
974 /*        .    for which there is sufficient workspace. ==== */
975
976 /* Computing MIN */
977         i__1 = (*n - 3) / 6, i__2 = (*lwork << 1) / 3;
978         nsmax = f2cmin(i__1,i__2);
979         nsmax -= nsmax % 2;
980
981 /*        ==== NDFL: an iteration count restarted at deflation. ==== */
982
983         ndfl = 1;
984
985 /*        ==== ITMAX = iteration limit ==== */
986
987 /* Computing MAX */
988         i__1 = 10, i__2 = *ihi - *ilo + 1;
989         itmax = 30 * f2cmax(i__1,i__2);
990
991 /*        ==== Last row and column in the active block ==== */
992
993         kbot = *ihi;
994
995 /*        ==== Main Loop ==== */
996
997         i__1 = itmax;
998         for (it = 1; it <= i__1; ++it) {
999
1000 /*           ==== Done when KBOT falls below ILO ==== */
1001
1002             if (kbot < *ilo) {
1003                 goto L80;
1004             }
1005
1006 /*           ==== Locate active block ==== */
1007
1008             i__2 = *ilo + 1;
1009             for (k = kbot; k >= i__2; --k) {
1010                 i__3 = k + (k - 1) * h_dim1;
1011                 if (h__[i__3].r == 0. && h__[i__3].i == 0.) {
1012                     goto L20;
1013                 }
1014 /* L10: */
1015             }
1016             k = *ilo;
1017 L20:
1018             ktop = k;
1019
1020 /*           ==== Select deflation window size: */
1021 /*           .    Typical Case: */
1022 /*           .      If possible and advisable, nibble the entire */
1023 /*           .      active block.  If not, use size MIN(NWR,NWMAX) */
1024 /*           .      or MIN(NWR+1,NWMAX) depending upon which has */
1025 /*           .      the smaller corresponding subdiagonal entry */
1026 /*           .      (a heuristic). */
1027 /*           . */
1028 /*           .    Exceptional Case: */
1029 /*           .      If there have been no deflations in KEXNW or */
1030 /*           .      more iterations, then vary the deflation window */
1031 /*           .      size.   At first, because, larger windows are, */
1032 /*           .      in general, more powerful than smaller ones, */
1033 /*           .      rapidly increase the window to the maximum possible. */
1034 /*           .      Then, gradually reduce the window size. ==== */
1035
1036             nh = kbot - ktop + 1;
1037             nwupbd = f2cmin(nh,nwmax);
1038             if (ndfl < 5) {
1039                 nw = f2cmin(nwupbd,nwr);
1040             } else {
1041 /* Computing MIN */
1042                 i__2 = nwupbd, i__3 = nw << 1;
1043                 nw = f2cmin(i__2,i__3);
1044             }
1045             if (nw < nwmax) {
1046                 if (nw >= nh - 1) {
1047                     nw = nh;
1048                 } else {
1049                     kwtop = kbot - nw + 1;
1050                     i__2 = kwtop + (kwtop - 1) * h_dim1;
1051                     i__3 = kwtop - 1 + (kwtop - 2) * h_dim1;
1052                     if ((d__1 = h__[i__2].r, abs(d__1)) + (d__2 = d_imag(&h__[
1053                             kwtop + (kwtop - 1) * h_dim1]), abs(d__2)) > (
1054                             d__3 = h__[i__3].r, abs(d__3)) + (d__4 = d_imag(&
1055                             h__[kwtop - 1 + (kwtop - 2) * h_dim1]), abs(d__4))
1056                             ) {
1057                         ++nw;
1058                     }
1059                 }
1060             }
1061             if (ndfl < 5) {
1062                 ndec = -1;
1063             } else if (ndec >= 0 || nw >= nwupbd) {
1064                 ++ndec;
1065                 if (nw - ndec < 2) {
1066                     ndec = 0;
1067                 }
1068                 nw -= ndec;
1069             }
1070
1071 /*           ==== Aggressive early deflation: */
1072 /*           .    split workspace under the subdiagonal into */
1073 /*           .      - an nw-by-nw work array V in the lower */
1074 /*           .        left-hand-corner, */
1075 /*           .      - an NW-by-at-least-NW-but-more-is-better */
1076 /*           .        (NW-by-NHO) horizontal work array along */
1077 /*           .        the bottom edge, */
1078 /*           .      - an at-least-NW-but-more-is-better (NHV-by-NW) */
1079 /*           .        vertical work array along the left-hand-edge. */
1080 /*           .        ==== */
1081
1082             kv = *n - nw + 1;
1083             kt = nw + 1;
1084             nho = *n - nw - 1 - kt + 1;
1085             kwv = nw + 2;
1086             nve = *n - nw - kwv + 1;
1087
1088 /*           ==== Aggressive early deflation ==== */
1089
1090             zlaqr2_(wantt, wantz, n, &ktop, &kbot, &nw, &h__[h_offset], ldh, 
1091                     iloz, ihiz, &z__[z_offset], ldz, &ls, &ld, &w[1], &h__[kv 
1092                     + h_dim1], ldh, &nho, &h__[kv + kt * h_dim1], ldh, &nve, &
1093                     h__[kwv + h_dim1], ldh, &work[1], lwork);
1094
1095 /*           ==== Adjust KBOT accounting for new deflations. ==== */
1096
1097             kbot -= ld;
1098
1099 /*           ==== KS points to the shifts. ==== */
1100
1101             ks = kbot - ls + 1;
1102
1103 /*           ==== Skip an expensive QR sweep if there is a (partly */
1104 /*           .    heuristic) reason to expect that many eigenvalues */
1105 /*           .    will deflate without it.  Here, the QR sweep is */
1106 /*           .    skipped if many eigenvalues have just been deflated */
1107 /*           .    or if the remaining active block is small. */
1108
1109             if (ld == 0 || ld * 100 <= nw * nibble && kbot - ktop + 1 > f2cmin(
1110                     nmin,nwmax)) {
1111
1112 /*              ==== NS = nominal number of simultaneous shifts. */
1113 /*              .    This may be lowered (slightly) if ZLAQR2 */
1114 /*              .    did not provide that many shifts. ==== */
1115
1116 /* Computing MIN */
1117 /* Computing MAX */
1118                 i__4 = 2, i__5 = kbot - ktop;
1119                 i__2 = f2cmin(nsmax,nsr), i__3 = f2cmax(i__4,i__5);
1120                 ns = f2cmin(i__2,i__3);
1121                 ns -= ns % 2;
1122
1123 /*              ==== If there have been no deflations */
1124 /*              .    in a multiple of KEXSH iterations, */
1125 /*              .    then try exceptional shifts. */
1126 /*              .    Otherwise use shifts provided by */
1127 /*              .    ZLAQR2 above or from the eigenvalues */
1128 /*              .    of a trailing principal submatrix. ==== */
1129
1130                 if (ndfl % 6 == 0) {
1131                     ks = kbot - ns + 1;
1132                     i__2 = ks + 1;
1133                     for (i__ = kbot; i__ >= i__2; i__ += -2) {
1134                         i__3 = i__;
1135                         i__4 = i__ + i__ * h_dim1;
1136                         i__5 = i__ + (i__ - 1) * h_dim1;
1137                         d__3 = ((d__1 = h__[i__5].r, abs(d__1)) + (d__2 = 
1138                                 d_imag(&h__[i__ + (i__ - 1) * h_dim1]), abs(
1139                                 d__2))) * .75;
1140                         z__1.r = h__[i__4].r + d__3, z__1.i = h__[i__4].i;
1141                         w[i__3].r = z__1.r, w[i__3].i = z__1.i;
1142                         i__3 = i__ - 1;
1143                         i__4 = i__;
1144                         w[i__3].r = w[i__4].r, w[i__3].i = w[i__4].i;
1145 /* L30: */
1146                     }
1147                 } else {
1148
1149 /*                 ==== Got NS/2 or fewer shifts? Use ZLAHQR */
1150 /*                 .    on a trailing principal submatrix to */
1151 /*                 .    get more. (Since NS.LE.NSMAX.LE.(N-3)/6, */
1152 /*                 .    there is enough space below the subdiagonal */
1153 /*                 .    to fit an NS-by-NS scratch array.) ==== */
1154
1155                     if (kbot - ks + 1 <= ns / 2) {
1156                         ks = kbot - ns + 1;
1157                         kt = *n - ns + 1;
1158                         zlacpy_("A", &ns, &ns, &h__[ks + ks * h_dim1], ldh, &
1159                                 h__[kt + h_dim1], ldh);
1160                         zlahqr_(&c_false, &c_false, &ns, &c__1, &ns, &h__[kt 
1161                                 + h_dim1], ldh, &w[ks], &c__1, &c__1, zdum, &
1162                                 c__1, &inf);
1163                         ks += inf;
1164
1165 /*                    ==== In case of a rare QR failure use */
1166 /*                    .    eigenvalues of the trailing 2-by-2 */
1167 /*                    .    principal submatrix.  Scale to avoid */
1168 /*                    .    overflows, underflows and subnormals. */
1169 /*                    .    (The scale factor S can not be zero, */
1170 /*                    .    because H(KBOT,KBOT-1) is nonzero.) ==== */
1171
1172                         if (ks >= kbot) {
1173                             i__2 = kbot - 1 + (kbot - 1) * h_dim1;
1174                             i__3 = kbot + (kbot - 1) * h_dim1;
1175                             i__4 = kbot - 1 + kbot * h_dim1;
1176                             i__5 = kbot + kbot * h_dim1;
1177                             s = (d__1 = h__[i__2].r, abs(d__1)) + (d__2 = 
1178                                     d_imag(&h__[kbot - 1 + (kbot - 1) * 
1179                                     h_dim1]), abs(d__2)) + ((d__3 = h__[i__3]
1180                                     .r, abs(d__3)) + (d__4 = d_imag(&h__[kbot 
1181                                     + (kbot - 1) * h_dim1]), abs(d__4))) + ((
1182                                     d__5 = h__[i__4].r, abs(d__5)) + (d__6 = 
1183                                     d_imag(&h__[kbot - 1 + kbot * h_dim1]), 
1184                                     abs(d__6))) + ((d__7 = h__[i__5].r, abs(
1185                                     d__7)) + (d__8 = d_imag(&h__[kbot + kbot *
1186                                      h_dim1]), abs(d__8)));
1187                             i__2 = kbot - 1 + (kbot - 1) * h_dim1;
1188                             z__1.r = h__[i__2].r / s, z__1.i = h__[i__2].i / 
1189                                     s;
1190                             aa.r = z__1.r, aa.i = z__1.i;
1191                             i__2 = kbot + (kbot - 1) * h_dim1;
1192                             z__1.r = h__[i__2].r / s, z__1.i = h__[i__2].i / 
1193                                     s;
1194                             cc.r = z__1.r, cc.i = z__1.i;
1195                             i__2 = kbot - 1 + kbot * h_dim1;
1196                             z__1.r = h__[i__2].r / s, z__1.i = h__[i__2].i / 
1197                                     s;
1198                             bb.r = z__1.r, bb.i = z__1.i;
1199                             i__2 = kbot + kbot * h_dim1;
1200                             z__1.r = h__[i__2].r / s, z__1.i = h__[i__2].i / 
1201                                     s;
1202                             dd.r = z__1.r, dd.i = z__1.i;
1203                             z__2.r = aa.r + dd.r, z__2.i = aa.i + dd.i;
1204                             z__1.r = z__2.r / 2., z__1.i = z__2.i / 2.;
1205                             tr2.r = z__1.r, tr2.i = z__1.i;
1206                             z__3.r = aa.r - tr2.r, z__3.i = aa.i - tr2.i;
1207                             z__4.r = dd.r - tr2.r, z__4.i = dd.i - tr2.i;
1208                             z__2.r = z__3.r * z__4.r - z__3.i * z__4.i, 
1209                                     z__2.i = z__3.r * z__4.i + z__3.i * 
1210                                     z__4.r;
1211                             z__5.r = bb.r * cc.r - bb.i * cc.i, z__5.i = bb.r 
1212                                     * cc.i + bb.i * cc.r;
1213                             z__1.r = z__2.r - z__5.r, z__1.i = z__2.i - 
1214                                     z__5.i;
1215                             det.r = z__1.r, det.i = z__1.i;
1216                             z__2.r = -det.r, z__2.i = -det.i;
1217                             z_sqrt(&z__1, &z__2);
1218                             rtdisc.r = z__1.r, rtdisc.i = z__1.i;
1219                             i__2 = kbot - 1;
1220                             z__2.r = tr2.r + rtdisc.r, z__2.i = tr2.i + 
1221                                     rtdisc.i;
1222                             z__1.r = s * z__2.r, z__1.i = s * z__2.i;
1223                             w[i__2].r = z__1.r, w[i__2].i = z__1.i;
1224                             i__2 = kbot;
1225                             z__2.r = tr2.r - rtdisc.r, z__2.i = tr2.i - 
1226                                     rtdisc.i;
1227                             z__1.r = s * z__2.r, z__1.i = s * z__2.i;
1228                             w[i__2].r = z__1.r, w[i__2].i = z__1.i;
1229
1230                             ks = kbot - 1;
1231                         }
1232                     }
1233
1234                     if (kbot - ks + 1 > ns) {
1235
1236 /*                    ==== Sort the shifts (Helps a little) ==== */
1237
1238                         sorted = FALSE_;
1239                         i__2 = ks + 1;
1240                         for (k = kbot; k >= i__2; --k) {
1241                             if (sorted) {
1242                                 goto L60;
1243                             }
1244                             sorted = TRUE_;
1245                             i__3 = k - 1;
1246                             for (i__ = ks; i__ <= i__3; ++i__) {
1247                                 i__4 = i__;
1248                                 i__5 = i__ + 1;
1249                                 if ((d__1 = w[i__4].r, abs(d__1)) + (d__2 = 
1250                                         d_imag(&w[i__]), abs(d__2)) < (d__3 = 
1251                                         w[i__5].r, abs(d__3)) + (d__4 = 
1252                                         d_imag(&w[i__ + 1]), abs(d__4))) {
1253                                     sorted = FALSE_;
1254                                     i__4 = i__;
1255                                     swap.r = w[i__4].r, swap.i = w[i__4].i;
1256                                     i__4 = i__;
1257                                     i__5 = i__ + 1;
1258                                     w[i__4].r = w[i__5].r, w[i__4].i = w[i__5]
1259                                             .i;
1260                                     i__4 = i__ + 1;
1261                                     w[i__4].r = swap.r, w[i__4].i = swap.i;
1262                                 }
1263 /* L40: */
1264                             }
1265 /* L50: */
1266                         }
1267 L60:
1268                         ;
1269                     }
1270                 }
1271
1272 /*              ==== If there are only two shifts, then use */
1273 /*              .    only one.  ==== */
1274
1275                 if (kbot - ks + 1 == 2) {
1276                     i__2 = kbot;
1277                     i__3 = kbot + kbot * h_dim1;
1278                     z__2.r = w[i__2].r - h__[i__3].r, z__2.i = w[i__2].i - 
1279                             h__[i__3].i;
1280                     z__1.r = z__2.r, z__1.i = z__2.i;
1281                     i__4 = kbot - 1;
1282                     i__5 = kbot + kbot * h_dim1;
1283                     z__4.r = w[i__4].r - h__[i__5].r, z__4.i = w[i__4].i - 
1284                             h__[i__5].i;
1285                     z__3.r = z__4.r, z__3.i = z__4.i;
1286                     if ((d__1 = z__1.r, abs(d__1)) + (d__2 = d_imag(&z__1), 
1287                             abs(d__2)) < (d__3 = z__3.r, abs(d__3)) + (d__4 = 
1288                             d_imag(&z__3), abs(d__4))) {
1289                         i__2 = kbot - 1;
1290                         i__3 = kbot;
1291                         w[i__2].r = w[i__3].r, w[i__2].i = w[i__3].i;
1292                     } else {
1293                         i__2 = kbot;
1294                         i__3 = kbot - 1;
1295                         w[i__2].r = w[i__3].r, w[i__2].i = w[i__3].i;
1296                     }
1297                 }
1298
1299 /*              ==== Use up to NS of the the smallest magnitude */
1300 /*              .    shifts.  If there aren't NS shifts available, */
1301 /*              .    then use them all, possibly dropping one to */
1302 /*              .    make the number of shifts even. ==== */
1303
1304 /* Computing MIN */
1305                 i__2 = ns, i__3 = kbot - ks + 1;
1306                 ns = f2cmin(i__2,i__3);
1307                 ns -= ns % 2;
1308                 ks = kbot - ns + 1;
1309
1310 /*              ==== Small-bulge multi-shift QR sweep: */
1311 /*              .    split workspace under the subdiagonal into */
1312 /*              .    - a KDU-by-KDU work array U in the lower */
1313 /*              .      left-hand-corner, */
1314 /*              .    - a KDU-by-at-least-KDU-but-more-is-better */
1315 /*              .      (KDU-by-NHo) horizontal work array WH along */
1316 /*              .      the bottom edge, */
1317 /*              .    - and an at-least-KDU-but-more-is-better-by-KDU */
1318 /*              .      (NVE-by-KDU) vertical work WV arrow along */
1319 /*              .      the left-hand-edge. ==== */
1320
1321                 kdu = ns << 1;
1322                 ku = *n - kdu + 1;
1323                 kwh = kdu + 1;
1324                 nho = *n - kdu - 3 - (kdu + 1) + 1;
1325                 kwv = kdu + 4;
1326                 nve = *n - kdu - kwv + 1;
1327
1328 /*              ==== Small-bulge multi-shift QR sweep ==== */
1329
1330                 zlaqr5_(wantt, wantz, &kacc22, n, &ktop, &kbot, &ns, &w[ks], &
1331                         h__[h_offset], ldh, iloz, ihiz, &z__[z_offset], ldz, &
1332                         work[1], &c__3, &h__[ku + h_dim1], ldh, &nve, &h__[
1333                         kwv + h_dim1], ldh, &nho, &h__[ku + kwh * h_dim1], 
1334                         ldh);
1335             }
1336
1337 /*           ==== Note progress (or the lack of it). ==== */
1338
1339             if (ld > 0) {
1340                 ndfl = 1;
1341             } else {
1342                 ++ndfl;
1343             }
1344
1345 /*           ==== End of main loop ==== */
1346 /* L70: */
1347         }
1348
1349 /*        ==== Iteration limit exceeded.  Set INFO to show where */
1350 /*        .    the problem occurred and exit. ==== */
1351
1352         *info = kbot;
1353 L80:
1354         ;
1355     }
1356
1357 /*     ==== Return the optimal value of LWORK. ==== */
1358
1359     d__1 = (doublereal) lwkopt;
1360     z__1.r = d__1, z__1.i = 0.;
1361     work[1].r = z__1.r, work[1].i = z__1.i;
1362
1363 /*     ==== End of ZLAQR4 ==== */
1364
1365     return 0;
1366 } /* zlaqr4_ */
1367