C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / zsteqr.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/Cd(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle_() continue;
256 #define myceiling_(w) {ceil(w)}
257 #define myhuge_(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc_(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static doublecomplex c_b1 = {0.,0.};
516 static doublecomplex c_b2 = {1.,0.};
517 static integer c__0 = 0;
518 static integer c__1 = 1;
519 static integer c__2 = 2;
520 static doublereal c_b41 = 1.;
521
522 /* > \brief \b ZSTEQR */
523
524 /*  =========== DOCUMENTATION =========== */
525
526 /* Online html documentation available at */
527 /*            http://www.netlib.org/lapack/explore-html/ */
528
529 /* > \htmlonly */
530 /* > Download ZSTEQR + dependencies */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zsteqr.
532 f"> */
533 /* > [TGZ]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zsteqr.
535 f"> */
536 /* > [ZIP]</a> */
537 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zsteqr.
538 f"> */
539 /* > [TXT]</a> */
540 /* > \endhtmlonly */
541
542 /*  Definition: */
543 /*  =========== */
544
545 /*       SUBROUTINE ZSTEQR( COMPZ, N, D, E, Z, LDZ, WORK, INFO ) */
546
547 /*       CHARACTER          COMPZ */
548 /*       INTEGER            INFO, LDZ, N */
549 /*       DOUBLE PRECISION   D( * ), E( * ), WORK( * ) */
550 /*       COMPLEX*16         Z( LDZ, * ) */
551
552
553 /* > \par Purpose: */
554 /*  ============= */
555 /* > */
556 /* > \verbatim */
557 /* > */
558 /* > ZSTEQR computes all eigenvalues and, optionally, eigenvectors of a */
559 /* > symmetric tridiagonal matrix using the implicit QL or QR method. */
560 /* > The eigenvectors of a full or band complex Hermitian matrix can also */
561 /* > be found if ZHETRD or ZHPTRD or ZHBTRD has been used to reduce this */
562 /* > matrix to tridiagonal form. */
563 /* > \endverbatim */
564
565 /*  Arguments: */
566 /*  ========== */
567
568 /* > \param[in] COMPZ */
569 /* > \verbatim */
570 /* >          COMPZ is CHARACTER*1 */
571 /* >          = 'N':  Compute eigenvalues only. */
572 /* >          = 'V':  Compute eigenvalues and eigenvectors of the original */
573 /* >                  Hermitian matrix.  On entry, Z must contain the */
574 /* >                  unitary matrix used to reduce the original matrix */
575 /* >                  to tridiagonal form. */
576 /* >          = 'I':  Compute eigenvalues and eigenvectors of the */
577 /* >                  tridiagonal matrix.  Z is initialized to the identity */
578 /* >                  matrix. */
579 /* > \endverbatim */
580 /* > */
581 /* > \param[in] N */
582 /* > \verbatim */
583 /* >          N is INTEGER */
584 /* >          The order of the matrix.  N >= 0. */
585 /* > \endverbatim */
586 /* > */
587 /* > \param[in,out] D */
588 /* > \verbatim */
589 /* >          D is DOUBLE PRECISION array, dimension (N) */
590 /* >          On entry, the diagonal elements of the tridiagonal matrix. */
591 /* >          On exit, if INFO = 0, the eigenvalues in ascending order. */
592 /* > \endverbatim */
593 /* > */
594 /* > \param[in,out] E */
595 /* > \verbatim */
596 /* >          E is DOUBLE PRECISION array, dimension (N-1) */
597 /* >          On entry, the (n-1) subdiagonal elements of the tridiagonal */
598 /* >          matrix. */
599 /* >          On exit, E has been destroyed. */
600 /* > \endverbatim */
601 /* > */
602 /* > \param[in,out] Z */
603 /* > \verbatim */
604 /* >          Z is COMPLEX*16 array, dimension (LDZ, N) */
605 /* >          On entry, if  COMPZ = 'V', then Z contains the unitary */
606 /* >          matrix used in the reduction to tridiagonal form. */
607 /* >          On exit, if INFO = 0, then if COMPZ = 'V', Z contains the */
608 /* >          orthonormal eigenvectors of the original Hermitian matrix, */
609 /* >          and if COMPZ = 'I', Z contains the orthonormal eigenvectors */
610 /* >          of the symmetric tridiagonal matrix. */
611 /* >          If COMPZ = 'N', then Z is not referenced. */
612 /* > \endverbatim */
613 /* > */
614 /* > \param[in] LDZ */
615 /* > \verbatim */
616 /* >          LDZ is INTEGER */
617 /* >          The leading dimension of the array Z.  LDZ >= 1, and if */
618 /* >          eigenvectors are desired, then  LDZ >= f2cmax(1,N). */
619 /* > \endverbatim */
620 /* > */
621 /* > \param[out] WORK */
622 /* > \verbatim */
623 /* >          WORK is DOUBLE PRECISION array, dimension (f2cmax(1,2*N-2)) */
624 /* >          If COMPZ = 'N', then WORK is not referenced. */
625 /* > \endverbatim */
626 /* > */
627 /* > \param[out] INFO */
628 /* > \verbatim */
629 /* >          INFO is INTEGER */
630 /* >          = 0:  successful exit */
631 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value */
632 /* >          > 0:  the algorithm has failed to find all the eigenvalues in */
633 /* >                a total of 30*N iterations; if INFO = i, then i */
634 /* >                elements of E have not converged to zero; on exit, D */
635 /* >                and E contain the elements of a symmetric tridiagonal */
636 /* >                matrix which is unitarily similar to the original */
637 /* >                matrix. */
638 /* > \endverbatim */
639
640 /*  Authors: */
641 /*  ======== */
642
643 /* > \author Univ. of Tennessee */
644 /* > \author Univ. of California Berkeley */
645 /* > \author Univ. of Colorado Denver */
646 /* > \author NAG Ltd. */
647
648 /* > \date December 2016 */
649
650 /* > \ingroup complex16OTHERcomputational */
651
652 /*  ===================================================================== */
653 /* Subroutine */ int zsteqr_(char *compz, integer *n, doublereal *d__, 
654         doublereal *e, doublecomplex *z__, integer *ldz, doublereal *work, 
655         integer *info)
656 {
657     /* System generated locals */
658     integer z_dim1, z_offset, i__1, i__2;
659     doublereal d__1, d__2;
660
661     /* Local variables */
662     integer lend, jtot;
663     extern /* Subroutine */ int dlae2_(doublereal *, doublereal *, doublereal 
664             *, doublereal *, doublereal *);
665     doublereal b, c__, f, g;
666     integer i__, j, k, l, m;
667     doublereal p, r__, s;
668     extern logical lsame_(char *, char *);
669     doublereal anorm;
670     extern /* Subroutine */ int zlasr_(char *, char *, char *, integer *, 
671             integer *, doublereal *, doublereal *, doublecomplex *, integer *);
672     integer l1;
673     extern /* Subroutine */ int zswap_(integer *, doublecomplex *, integer *, 
674             doublecomplex *, integer *), dlaev2_(doublereal *, doublereal *, 
675             doublereal *, doublereal *, doublereal *, doublereal *, 
676             doublereal *);
677     integer lendm1, lendp1;
678     extern doublereal dlapy2_(doublereal *, doublereal *);
679     integer ii;
680     extern doublereal dlamch_(char *);
681     integer mm, iscale;
682     extern /* Subroutine */ int dlascl_(char *, integer *, integer *, 
683             doublereal *, doublereal *, integer *, integer *, doublereal *, 
684             integer *, integer *);
685     doublereal safmin;
686     extern /* Subroutine */ int dlartg_(doublereal *, doublereal *, 
687             doublereal *, doublereal *, doublereal *);
688     doublereal safmax;
689     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
690     extern doublereal dlanst_(char *, integer *, doublereal *, doublereal *);
691     extern /* Subroutine */ int dlasrt_(char *, integer *, doublereal *, 
692             integer *);
693     integer lendsv;
694     doublereal ssfmin;
695     integer nmaxit, icompz;
696     doublereal ssfmax;
697     extern /* Subroutine */ int zlaset_(char *, integer *, integer *, 
698             doublecomplex *, doublecomplex *, doublecomplex *, integer *);
699     integer lm1, mm1, nm1;
700     doublereal rt1, rt2, eps;
701     integer lsv;
702     doublereal tst, eps2;
703
704
705 /*  -- LAPACK computational routine (version 3.7.0) -- */
706 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
707 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
708 /*     December 2016 */
709
710
711 /*  ===================================================================== */
712
713
714 /*     Test the input parameters. */
715
716     /* Parameter adjustments */
717     --d__;
718     --e;
719     z_dim1 = *ldz;
720     z_offset = 1 + z_dim1 * 1;
721     z__ -= z_offset;
722     --work;
723
724     /* Function Body */
725     *info = 0;
726
727     if (lsame_(compz, "N")) {
728         icompz = 0;
729     } else if (lsame_(compz, "V")) {
730         icompz = 1;
731     } else if (lsame_(compz, "I")) {
732         icompz = 2;
733     } else {
734         icompz = -1;
735     }
736     if (icompz < 0) {
737         *info = -1;
738     } else if (*n < 0) {
739         *info = -2;
740     } else if (*ldz < 1 || icompz > 0 && *ldz < f2cmax(1,*n)) {
741         *info = -6;
742     }
743     if (*info != 0) {
744         i__1 = -(*info);
745         xerbla_("ZSTEQR", &i__1, (ftnlen)6);
746         return 0;
747     }
748
749 /*     Quick return if possible */
750
751     if (*n == 0) {
752         return 0;
753     }
754
755     if (*n == 1) {
756         if (icompz == 2) {
757             i__1 = z_dim1 + 1;
758             z__[i__1].r = 1., z__[i__1].i = 0.;
759         }
760         return 0;
761     }
762
763 /*     Determine the unit roundoff and over/underflow thresholds. */
764
765     eps = dlamch_("E");
766 /* Computing 2nd power */
767     d__1 = eps;
768     eps2 = d__1 * d__1;
769     safmin = dlamch_("S");
770     safmax = 1. / safmin;
771     ssfmax = sqrt(safmax) / 3.;
772     ssfmin = sqrt(safmin) / eps2;
773
774 /*     Compute the eigenvalues and eigenvectors of the tridiagonal */
775 /*     matrix. */
776
777     if (icompz == 2) {
778         zlaset_("Full", n, n, &c_b1, &c_b2, &z__[z_offset], ldz);
779     }
780
781     nmaxit = *n * 30;
782     jtot = 0;
783
784 /*     Determine where the matrix splits and choose QL or QR iteration */
785 /*     for each block, according to whether top or bottom diagonal */
786 /*     element is smaller. */
787
788     l1 = 1;
789     nm1 = *n - 1;
790
791 L10:
792     if (l1 > *n) {
793         goto L160;
794     }
795     if (l1 > 1) {
796         e[l1 - 1] = 0.;
797     }
798     if (l1 <= nm1) {
799         i__1 = nm1;
800         for (m = l1; m <= i__1; ++m) {
801             tst = (d__1 = e[m], abs(d__1));
802             if (tst == 0.) {
803                 goto L30;
804             }
805             if (tst <= sqrt((d__1 = d__[m], abs(d__1))) * sqrt((d__2 = d__[m 
806                     + 1], abs(d__2))) * eps) {
807                 e[m] = 0.;
808                 goto L30;
809             }
810 /* L20: */
811         }
812     }
813     m = *n;
814
815 L30:
816     l = l1;
817     lsv = l;
818     lend = m;
819     lendsv = lend;
820     l1 = m + 1;
821     if (lend == l) {
822         goto L10;
823     }
824
825 /*     Scale submatrix in rows and columns L to LEND */
826
827     i__1 = lend - l + 1;
828     anorm = dlanst_("I", &i__1, &d__[l], &e[l]);
829     iscale = 0;
830     if (anorm == 0.) {
831         goto L10;
832     }
833     if (anorm > ssfmax) {
834         iscale = 1;
835         i__1 = lend - l + 1;
836         dlascl_("G", &c__0, &c__0, &anorm, &ssfmax, &i__1, &c__1, &d__[l], n, 
837                 info);
838         i__1 = lend - l;
839         dlascl_("G", &c__0, &c__0, &anorm, &ssfmax, &i__1, &c__1, &e[l], n, 
840                 info);
841     } else if (anorm < ssfmin) {
842         iscale = 2;
843         i__1 = lend - l + 1;
844         dlascl_("G", &c__0, &c__0, &anorm, &ssfmin, &i__1, &c__1, &d__[l], n, 
845                 info);
846         i__1 = lend - l;
847         dlascl_("G", &c__0, &c__0, &anorm, &ssfmin, &i__1, &c__1, &e[l], n, 
848                 info);
849     }
850
851 /*     Choose between QL and QR iteration */
852
853     if ((d__1 = d__[lend], abs(d__1)) < (d__2 = d__[l], abs(d__2))) {
854         lend = lsv;
855         l = lendsv;
856     }
857
858     if (lend > l) {
859
860 /*        QL Iteration */
861
862 /*        Look for small subdiagonal element. */
863
864 L40:
865         if (l != lend) {
866             lendm1 = lend - 1;
867             i__1 = lendm1;
868             for (m = l; m <= i__1; ++m) {
869 /* Computing 2nd power */
870                 d__2 = (d__1 = e[m], abs(d__1));
871                 tst = d__2 * d__2;
872                 if (tst <= eps2 * (d__1 = d__[m], abs(d__1)) * (d__2 = d__[m 
873                         + 1], abs(d__2)) + safmin) {
874                     goto L60;
875                 }
876 /* L50: */
877             }
878         }
879
880         m = lend;
881
882 L60:
883         if (m < lend) {
884             e[m] = 0.;
885         }
886         p = d__[l];
887         if (m == l) {
888             goto L80;
889         }
890
891 /*        If remaining matrix is 2-by-2, use DLAE2 or SLAEV2 */
892 /*        to compute its eigensystem. */
893
894         if (m == l + 1) {
895             if (icompz > 0) {
896                 dlaev2_(&d__[l], &e[l], &d__[l + 1], &rt1, &rt2, &c__, &s);
897                 work[l] = c__;
898                 work[*n - 1 + l] = s;
899                 zlasr_("R", "V", "B", n, &c__2, &work[l], &work[*n - 1 + l], &
900                         z__[l * z_dim1 + 1], ldz);
901             } else {
902                 dlae2_(&d__[l], &e[l], &d__[l + 1], &rt1, &rt2);
903             }
904             d__[l] = rt1;
905             d__[l + 1] = rt2;
906             e[l] = 0.;
907             l += 2;
908             if (l <= lend) {
909                 goto L40;
910             }
911             goto L140;
912         }
913
914         if (jtot == nmaxit) {
915             goto L140;
916         }
917         ++jtot;
918
919 /*        Form shift. */
920
921         g = (d__[l + 1] - p) / (e[l] * 2.);
922         r__ = dlapy2_(&g, &c_b41);
923         g = d__[m] - p + e[l] / (g + d_sign(&r__, &g));
924
925         s = 1.;
926         c__ = 1.;
927         p = 0.;
928
929 /*        Inner loop */
930
931         mm1 = m - 1;
932         i__1 = l;
933         for (i__ = mm1; i__ >= i__1; --i__) {
934             f = s * e[i__];
935             b = c__ * e[i__];
936             dlartg_(&g, &f, &c__, &s, &r__);
937             if (i__ != m - 1) {
938                 e[i__ + 1] = r__;
939             }
940             g = d__[i__ + 1] - p;
941             r__ = (d__[i__] - g) * s + c__ * 2. * b;
942             p = s * r__;
943             d__[i__ + 1] = g + p;
944             g = c__ * r__ - b;
945
946 /*           If eigenvectors are desired, then save rotations. */
947
948             if (icompz > 0) {
949                 work[i__] = c__;
950                 work[*n - 1 + i__] = -s;
951             }
952
953 /* L70: */
954         }
955
956 /*        If eigenvectors are desired, then apply saved rotations. */
957
958         if (icompz > 0) {
959             mm = m - l + 1;
960             zlasr_("R", "V", "B", n, &mm, &work[l], &work[*n - 1 + l], &z__[l 
961                     * z_dim1 + 1], ldz);
962         }
963
964         d__[l] -= p;
965         e[l] = g;
966         goto L40;
967
968 /*        Eigenvalue found. */
969
970 L80:
971         d__[l] = p;
972
973         ++l;
974         if (l <= lend) {
975             goto L40;
976         }
977         goto L140;
978
979     } else {
980
981 /*        QR Iteration */
982
983 /*        Look for small superdiagonal element. */
984
985 L90:
986         if (l != lend) {
987             lendp1 = lend + 1;
988             i__1 = lendp1;
989             for (m = l; m >= i__1; --m) {
990 /* Computing 2nd power */
991                 d__2 = (d__1 = e[m - 1], abs(d__1));
992                 tst = d__2 * d__2;
993                 if (tst <= eps2 * (d__1 = d__[m], abs(d__1)) * (d__2 = d__[m 
994                         - 1], abs(d__2)) + safmin) {
995                     goto L110;
996                 }
997 /* L100: */
998             }
999         }
1000
1001         m = lend;
1002
1003 L110:
1004         if (m > lend) {
1005             e[m - 1] = 0.;
1006         }
1007         p = d__[l];
1008         if (m == l) {
1009             goto L130;
1010         }
1011
1012 /*        If remaining matrix is 2-by-2, use DLAE2 or SLAEV2 */
1013 /*        to compute its eigensystem. */
1014
1015         if (m == l - 1) {
1016             if (icompz > 0) {
1017                 dlaev2_(&d__[l - 1], &e[l - 1], &d__[l], &rt1, &rt2, &c__, &s)
1018                         ;
1019                 work[m] = c__;
1020                 work[*n - 1 + m] = s;
1021                 zlasr_("R", "V", "F", n, &c__2, &work[m], &work[*n - 1 + m], &
1022                         z__[(l - 1) * z_dim1 + 1], ldz);
1023             } else {
1024                 dlae2_(&d__[l - 1], &e[l - 1], &d__[l], &rt1, &rt2);
1025             }
1026             d__[l - 1] = rt1;
1027             d__[l] = rt2;
1028             e[l - 1] = 0.;
1029             l += -2;
1030             if (l >= lend) {
1031                 goto L90;
1032             }
1033             goto L140;
1034         }
1035
1036         if (jtot == nmaxit) {
1037             goto L140;
1038         }
1039         ++jtot;
1040
1041 /*        Form shift. */
1042
1043         g = (d__[l - 1] - p) / (e[l - 1] * 2.);
1044         r__ = dlapy2_(&g, &c_b41);
1045         g = d__[m] - p + e[l - 1] / (g + d_sign(&r__, &g));
1046
1047         s = 1.;
1048         c__ = 1.;
1049         p = 0.;
1050
1051 /*        Inner loop */
1052
1053         lm1 = l - 1;
1054         i__1 = lm1;
1055         for (i__ = m; i__ <= i__1; ++i__) {
1056             f = s * e[i__];
1057             b = c__ * e[i__];
1058             dlartg_(&g, &f, &c__, &s, &r__);
1059             if (i__ != m) {
1060                 e[i__ - 1] = r__;
1061             }
1062             g = d__[i__] - p;
1063             r__ = (d__[i__ + 1] - g) * s + c__ * 2. * b;
1064             p = s * r__;
1065             d__[i__] = g + p;
1066             g = c__ * r__ - b;
1067
1068 /*           If eigenvectors are desired, then save rotations. */
1069
1070             if (icompz > 0) {
1071                 work[i__] = c__;
1072                 work[*n - 1 + i__] = s;
1073             }
1074
1075 /* L120: */
1076         }
1077
1078 /*        If eigenvectors are desired, then apply saved rotations. */
1079
1080         if (icompz > 0) {
1081             mm = l - m + 1;
1082             zlasr_("R", "V", "F", n, &mm, &work[m], &work[*n - 1 + m], &z__[m 
1083                     * z_dim1 + 1], ldz);
1084         }
1085
1086         d__[l] -= p;
1087         e[lm1] = g;
1088         goto L90;
1089
1090 /*        Eigenvalue found. */
1091
1092 L130:
1093         d__[l] = p;
1094
1095         --l;
1096         if (l >= lend) {
1097             goto L90;
1098         }
1099         goto L140;
1100
1101     }
1102
1103 /*     Undo scaling if necessary */
1104
1105 L140:
1106     if (iscale == 1) {
1107         i__1 = lendsv - lsv + 1;
1108         dlascl_("G", &c__0, &c__0, &ssfmax, &anorm, &i__1, &c__1, &d__[lsv], 
1109                 n, info);
1110         i__1 = lendsv - lsv;
1111         dlascl_("G", &c__0, &c__0, &ssfmax, &anorm, &i__1, &c__1, &e[lsv], n, 
1112                 info);
1113     } else if (iscale == 2) {
1114         i__1 = lendsv - lsv + 1;
1115         dlascl_("G", &c__0, &c__0, &ssfmin, &anorm, &i__1, &c__1, &d__[lsv], 
1116                 n, info);
1117         i__1 = lendsv - lsv;
1118         dlascl_("G", &c__0, &c__0, &ssfmin, &anorm, &i__1, &c__1, &e[lsv], n, 
1119                 info);
1120     }
1121
1122 /*     Check for no convergence to an eigenvalue after a total */
1123 /*     of N*MAXIT iterations. */
1124
1125     if (jtot == nmaxit) {
1126         i__1 = *n - 1;
1127         for (i__ = 1; i__ <= i__1; ++i__) {
1128             if (e[i__] != 0.) {
1129                 ++(*info);
1130             }
1131 /* L150: */
1132         }
1133         return 0;
1134     }
1135     goto L10;
1136
1137 /*     Order eigenvalues and eigenvectors. */
1138
1139 L160:
1140     if (icompz == 0) {
1141
1142 /*        Use Quick Sort */
1143
1144         dlasrt_("I", n, &d__[1], info);
1145
1146     } else {
1147
1148 /*        Use Selection Sort to minimize swaps of eigenvectors */
1149
1150         i__1 = *n;
1151         for (ii = 2; ii <= i__1; ++ii) {
1152             i__ = ii - 1;
1153             k = i__;
1154             p = d__[i__];
1155             i__2 = *n;
1156             for (j = ii; j <= i__2; ++j) {
1157                 if (d__[j] < p) {
1158                     k = j;
1159                     p = d__[j];
1160                 }
1161 /* L170: */
1162             }
1163             if (k != i__) {
1164                 d__[k] = d__[i__];
1165                 d__[i__] = p;
1166                 zswap_(n, &z__[i__ * z_dim1 + 1], &c__1, &z__[k * z_dim1 + 1],
1167                          &c__1);
1168             }
1169 /* L180: */
1170         }
1171     }
1172     return 0;
1173
1174 /*     End of ZSTEQR */
1175
1176 } /* zsteqr_ */
1177