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