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