You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sgees.c 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. #if defined(_WIN64)
  13. typedef long long BLASLONG;
  14. typedef unsigned long long BLASULONG;
  15. #else
  16. typedef long BLASLONG;
  17. typedef unsigned long BLASULONG;
  18. #endif
  19. #ifdef LAPACK_ILP64
  20. typedef BLASLONG blasint;
  21. #if defined(_WIN64)
  22. #define blasabs(x) llabs(x)
  23. #else
  24. #define blasabs(x) labs(x)
  25. #endif
  26. #else
  27. typedef int blasint;
  28. #define blasabs(x) abs(x)
  29. #endif
  30. typedef blasint integer;
  31. typedef unsigned int uinteger;
  32. typedef char *address;
  33. typedef short int shortint;
  34. typedef float real;
  35. typedef double doublereal;
  36. typedef struct { real r, i; } complex;
  37. typedef struct { doublereal r, i; } doublecomplex;
  38. #ifdef _MSC_VER
  39. static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
  40. static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
  41. static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
  42. static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
  43. #else
  44. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  45. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  46. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  47. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  48. #endif
  49. #define pCf(z) (*_pCf(z))
  50. #define pCd(z) (*_pCd(z))
  51. typedef blasint logical;
  52. typedef char logical1;
  53. typedef char integer1;
  54. #define TRUE_ (1)
  55. #define FALSE_ (0)
  56. /* Extern is for use with -E */
  57. #ifndef Extern
  58. #define Extern extern
  59. #endif
  60. /* I/O stuff */
  61. typedef int flag;
  62. typedef int ftnlen;
  63. typedef int ftnint;
  64. /*external read, write*/
  65. typedef struct
  66. { flag cierr;
  67. ftnint ciunit;
  68. flag ciend;
  69. char *cifmt;
  70. ftnint cirec;
  71. } cilist;
  72. /*internal read, write*/
  73. typedef struct
  74. { flag icierr;
  75. char *iciunit;
  76. flag iciend;
  77. char *icifmt;
  78. ftnint icirlen;
  79. ftnint icirnum;
  80. } icilist;
  81. /*open*/
  82. typedef struct
  83. { flag oerr;
  84. ftnint ounit;
  85. char *ofnm;
  86. ftnlen ofnmlen;
  87. char *osta;
  88. char *oacc;
  89. char *ofm;
  90. ftnint orl;
  91. char *oblnk;
  92. } olist;
  93. /*close*/
  94. typedef struct
  95. { flag cerr;
  96. ftnint cunit;
  97. char *csta;
  98. } cllist;
  99. /*rewind, backspace, endfile*/
  100. typedef struct
  101. { flag aerr;
  102. ftnint aunit;
  103. } alist;
  104. /* inquire */
  105. typedef struct
  106. { flag inerr;
  107. ftnint inunit;
  108. char *infile;
  109. ftnlen infilen;
  110. ftnint *inex; /*parameters in standard's order*/
  111. ftnint *inopen;
  112. ftnint *innum;
  113. ftnint *innamed;
  114. char *inname;
  115. ftnlen innamlen;
  116. char *inacc;
  117. ftnlen inacclen;
  118. char *inseq;
  119. ftnlen inseqlen;
  120. char *indir;
  121. ftnlen indirlen;
  122. char *infmt;
  123. ftnlen infmtlen;
  124. char *inform;
  125. ftnint informlen;
  126. char *inunf;
  127. ftnlen inunflen;
  128. ftnint *inrecl;
  129. ftnint *innrec;
  130. char *inblank;
  131. ftnlen inblanklen;
  132. } inlist;
  133. #define VOID void
  134. union Multitype { /* for multiple entry points */
  135. integer1 g;
  136. shortint h;
  137. integer i;
  138. /* longint j; */
  139. real r;
  140. doublereal d;
  141. complex c;
  142. doublecomplex z;
  143. };
  144. typedef union Multitype Multitype;
  145. struct Vardesc { /* for Namelist */
  146. char *name;
  147. char *addr;
  148. ftnlen *dims;
  149. int type;
  150. };
  151. typedef struct Vardesc Vardesc;
  152. struct Namelist {
  153. char *name;
  154. Vardesc **vars;
  155. int nvars;
  156. };
  157. typedef struct Namelist Namelist;
  158. #define abs(x) ((x) >= 0 ? (x) : -(x))
  159. #define dabs(x) (fabs(x))
  160. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  161. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  162. #define dmin(a,b) (f2cmin(a,b))
  163. #define dmax(a,b) (f2cmax(a,b))
  164. #define bit_test(a,b) ((a) >> (b) & 1)
  165. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  166. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  167. #define abort_() { sig_die("Fortran abort routine called", 1); }
  168. #define c_abs(z) (cabsf(Cf(z)))
  169. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  170. #ifdef _MSC_VER
  171. #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]);}
  172. #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]);}
  173. #else
  174. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  175. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  176. #endif
  177. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  178. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  179. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  180. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  181. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  182. #define d_abs(x) (fabs(*(x)))
  183. #define d_acos(x) (acos(*(x)))
  184. #define d_asin(x) (asin(*(x)))
  185. #define d_atan(x) (atan(*(x)))
  186. #define d_atn2(x, y) (atan2(*(x),*(y)))
  187. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  188. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  189. #define d_cos(x) (cos(*(x)))
  190. #define d_cosh(x) (cosh(*(x)))
  191. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  192. #define d_exp(x) (exp(*(x)))
  193. #define d_imag(z) (cimag(Cd(z)))
  194. #define r_imag(z) (cimagf(Cf(z)))
  195. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  196. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  198. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define d_log(x) (log(*(x)))
  200. #define d_mod(x, y) (fmod(*(x), *(y)))
  201. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  202. #define d_nint(x) u_nint(*(x))
  203. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  204. #define d_sign(a,b) u_sign(*(a),*(b))
  205. #define r_sign(a,b) u_sign(*(a),*(b))
  206. #define d_sin(x) (sin(*(x)))
  207. #define d_sinh(x) (sinh(*(x)))
  208. #define d_sqrt(x) (sqrt(*(x)))
  209. #define d_tan(x) (tan(*(x)))
  210. #define d_tanh(x) (tanh(*(x)))
  211. #define i_abs(x) abs(*(x))
  212. #define i_dnnt(x) ((integer)u_nint(*(x)))
  213. #define i_len(s, n) (n)
  214. #define i_nint(x) ((integer)u_nint(*(x)))
  215. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  216. #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++ = ' '; }
  217. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  218. #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]; }
  219. #define sig_die(s, kill) { exit(1); }
  220. #define s_stop(s, n) {exit(0);}
  221. #define z_abs(z) (cabs(Cd(z)))
  222. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  223. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  224. #define myexit_() break;
  225. #define mycycle() continue;
  226. #define myceiling(w) {ceil(w)}
  227. #define myhuge(w) {HUGE_VAL}
  228. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  229. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  230. #ifdef __cplusplus
  231. typedef logical (*L_fp)(...);
  232. #else
  233. typedef logical (*L_fp)();
  234. #endif
  235. /* -- translated by f2c (version 20000121).
  236. You must link the resulting object file with the libraries:
  237. -lf2c -lm (in that order)
  238. */
  239. /* Table of constant values */
  240. static integer c__1 = 1;
  241. static integer c__0 = 0;
  242. static integer c_n1 = -1;
  243. /* > \brief <b> SGEES computes the eigenvalues, the Schur form, and, optionally, the matrix of Schur vectors f
  244. or GE matrices</b> */
  245. /* =========== DOCUMENTATION =========== */
  246. /* Online html documentation available at */
  247. /* http://www.netlib.org/lapack/explore-html/ */
  248. /* > \htmlonly */
  249. /* > Download SGEES + dependencies */
  250. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgees.f
  251. "> */
  252. /* > [TGZ]</a> */
  253. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgees.f
  254. "> */
  255. /* > [ZIP]</a> */
  256. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgees.f
  257. "> */
  258. /* > [TXT]</a> */
  259. /* > \endhtmlonly */
  260. /* Definition: */
  261. /* =========== */
  262. /* SUBROUTINE SGEES( JOBVS, SORT, SELECT, N, A, LDA, SDIM, WR, WI, */
  263. /* VS, LDVS, WORK, LWORK, BWORK, INFO ) */
  264. /* CHARACTER JOBVS, SORT */
  265. /* INTEGER INFO, LDA, LDVS, LWORK, N, SDIM */
  266. /* LOGICAL BWORK( * ) */
  267. /* REAL A( LDA, * ), VS( LDVS, * ), WI( * ), WORK( * ), */
  268. /* $ WR( * ) */
  269. /* LOGICAL SELECT */
  270. /* EXTERNAL SELECT */
  271. /* > \par Purpose: */
  272. /* ============= */
  273. /* > */
  274. /* > \verbatim */
  275. /* > */
  276. /* > SGEES computes for an N-by-N real nonsymmetric matrix A, the */
  277. /* > eigenvalues, the real Schur form T, and, optionally, the matrix of */
  278. /* > Schur vectors Z. This gives the Schur factorization A = Z*T*(Z**T). */
  279. /* > */
  280. /* > Optionally, it also orders the eigenvalues on the diagonal of the */
  281. /* > real Schur form so that selected eigenvalues are at the top left. */
  282. /* > The leading columns of Z then form an orthonormal basis for the */
  283. /* > invariant subspace corresponding to the selected eigenvalues. */
  284. /* > */
  285. /* > A matrix is in real Schur form if it is upper quasi-triangular with */
  286. /* > 1-by-1 and 2-by-2 blocks. 2-by-2 blocks will be standardized in the */
  287. /* > form */
  288. /* > [ a b ] */
  289. /* > [ c a ] */
  290. /* > */
  291. /* > where b*c < 0. The eigenvalues of such a block are a +- sqrt(bc). */
  292. /* > \endverbatim */
  293. /* Arguments: */
  294. /* ========== */
  295. /* > \param[in] JOBVS */
  296. /* > \verbatim */
  297. /* > JOBVS is CHARACTER*1 */
  298. /* > = 'N': Schur vectors are not computed; */
  299. /* > = 'V': Schur vectors are computed. */
  300. /* > \endverbatim */
  301. /* > */
  302. /* > \param[in] SORT */
  303. /* > \verbatim */
  304. /* > SORT is CHARACTER*1 */
  305. /* > Specifies whether or not to order the eigenvalues on the */
  306. /* > diagonal of the Schur form. */
  307. /* > = 'N': Eigenvalues are not ordered; */
  308. /* > = 'S': Eigenvalues are ordered (see SELECT). */
  309. /* > \endverbatim */
  310. /* > */
  311. /* > \param[in] SELECT */
  312. /* > \verbatim */
  313. /* > SELECT is a LOGICAL FUNCTION of two REAL arguments */
  314. /* > SELECT must be declared EXTERNAL in the calling subroutine. */
  315. /* > If SORT = 'S', SELECT is used to select eigenvalues to sort */
  316. /* > to the top left of the Schur form. */
  317. /* > If SORT = 'N', SELECT is not referenced. */
  318. /* > An eigenvalue WR(j)+sqrt(-1)*WI(j) is selected if */
  319. /* > SELECT(WR(j),WI(j)) is true; i.e., if either one of a complex */
  320. /* > conjugate pair of eigenvalues is selected, then both complex */
  321. /* > eigenvalues are selected. */
  322. /* > Note that a selected complex eigenvalue may no longer */
  323. /* > satisfy SELECT(WR(j),WI(j)) = .TRUE. after ordering, since */
  324. /* > ordering may change the value of complex eigenvalues */
  325. /* > (especially if the eigenvalue is ill-conditioned); in this */
  326. /* > case INFO is set to N+2 (see INFO below). */
  327. /* > \endverbatim */
  328. /* > */
  329. /* > \param[in] N */
  330. /* > \verbatim */
  331. /* > N is INTEGER */
  332. /* > The order of the matrix A. N >= 0. */
  333. /* > \endverbatim */
  334. /* > */
  335. /* > \param[in,out] A */
  336. /* > \verbatim */
  337. /* > A is REAL array, dimension (LDA,N) */
  338. /* > On entry, the N-by-N matrix A. */
  339. /* > On exit, A has been overwritten by its real Schur form T. */
  340. /* > \endverbatim */
  341. /* > */
  342. /* > \param[in] LDA */
  343. /* > \verbatim */
  344. /* > LDA is INTEGER */
  345. /* > The leading dimension of the array A. LDA >= f2cmax(1,N). */
  346. /* > \endverbatim */
  347. /* > */
  348. /* > \param[out] SDIM */
  349. /* > \verbatim */
  350. /* > SDIM is INTEGER */
  351. /* > If SORT = 'N', SDIM = 0. */
  352. /* > If SORT = 'S', SDIM = number of eigenvalues (after sorting) */
  353. /* > for which SELECT is true. (Complex conjugate */
  354. /* > pairs for which SELECT is true for either */
  355. /* > eigenvalue count as 2.) */
  356. /* > \endverbatim */
  357. /* > */
  358. /* > \param[out] WR */
  359. /* > \verbatim */
  360. /* > WR is REAL array, dimension (N) */
  361. /* > \endverbatim */
  362. /* > */
  363. /* > \param[out] WI */
  364. /* > \verbatim */
  365. /* > WI is REAL array, dimension (N) */
  366. /* > WR and WI contain the real and imaginary parts, */
  367. /* > respectively, of the computed eigenvalues in the same order */
  368. /* > that they appear on the diagonal of the output Schur form T. */
  369. /* > Complex conjugate pairs of eigenvalues will appear */
  370. /* > consecutively with the eigenvalue having the positive */
  371. /* > imaginary part first. */
  372. /* > \endverbatim */
  373. /* > */
  374. /* > \param[out] VS */
  375. /* > \verbatim */
  376. /* > VS is REAL array, dimension (LDVS,N) */
  377. /* > If JOBVS = 'V', VS contains the orthogonal matrix Z of Schur */
  378. /* > vectors. */
  379. /* > If JOBVS = 'N', VS is not referenced. */
  380. /* > \endverbatim */
  381. /* > */
  382. /* > \param[in] LDVS */
  383. /* > \verbatim */
  384. /* > LDVS is INTEGER */
  385. /* > The leading dimension of the array VS. LDVS >= 1; if */
  386. /* > JOBVS = 'V', LDVS >= N. */
  387. /* > \endverbatim */
  388. /* > */
  389. /* > \param[out] WORK */
  390. /* > \verbatim */
  391. /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
  392. /* > On exit, if INFO = 0, WORK(1) contains the optimal LWORK. */
  393. /* > \endverbatim */
  394. /* > */
  395. /* > \param[in] LWORK */
  396. /* > \verbatim */
  397. /* > LWORK is INTEGER */
  398. /* > The dimension of the array WORK. LWORK >= f2cmax(1,3*N). */
  399. /* > For good performance, LWORK must generally be larger. */
  400. /* > */
  401. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  402. /* > only calculates the optimal size of the WORK array, returns */
  403. /* > this value as the first entry of the WORK array, and no error */
  404. /* > message related to LWORK is issued by XERBLA. */
  405. /* > \endverbatim */
  406. /* > */
  407. /* > \param[out] BWORK */
  408. /* > \verbatim */
  409. /* > BWORK is LOGICAL array, dimension (N) */
  410. /* > Not referenced if SORT = 'N'. */
  411. /* > \endverbatim */
  412. /* > */
  413. /* > \param[out] INFO */
  414. /* > \verbatim */
  415. /* > INFO is INTEGER */
  416. /* > = 0: successful exit */
  417. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  418. /* > > 0: if INFO = i, and i is */
  419. /* > <= N: the QR algorithm failed to compute all the */
  420. /* > eigenvalues; elements 1:ILO-1 and i+1:N of WR and WI */
  421. /* > contain those eigenvalues which have converged; if */
  422. /* > JOBVS = 'V', VS contains the matrix which reduces A */
  423. /* > to its partially converged Schur form. */
  424. /* > = N+1: the eigenvalues could not be reordered because some */
  425. /* > eigenvalues were too close to separate (the problem */
  426. /* > is very ill-conditioned); */
  427. /* > = N+2: after reordering, roundoff changed values of some */
  428. /* > complex eigenvalues so that leading eigenvalues in */
  429. /* > the Schur form no longer satisfy SELECT=.TRUE. This */
  430. /* > could also be caused by underflow due to scaling. */
  431. /* > \endverbatim */
  432. /* Authors: */
  433. /* ======== */
  434. /* > \author Univ. of Tennessee */
  435. /* > \author Univ. of California Berkeley */
  436. /* > \author Univ. of Colorado Denver */
  437. /* > \author NAG Ltd. */
  438. /* > \date June 2017 */
  439. /* > \ingroup realGEeigen */
  440. /* ===================================================================== */
  441. /* Subroutine */ void sgees_(char *jobvs, char *sort, L_fp select, integer *n,
  442. real *a, integer *lda, integer *sdim, real *wr, real *wi, real *vs,
  443. integer *ldvs, real *work, integer *lwork, logical *bwork, integer *
  444. info)
  445. {
  446. /* System generated locals */
  447. integer a_dim1, a_offset, vs_dim1, vs_offset, i__1, i__2, i__3;
  448. /* Local variables */
  449. integer ibal;
  450. real anrm;
  451. integer idum[1], ierr, itau, iwrk, inxt, i__;
  452. real s;
  453. integer icond, ieval;
  454. extern logical lsame_(char *, char *);
  455. logical cursl;
  456. integer i1, i2;
  457. extern /* Subroutine */ void scopy_(integer *, real *, integer *, real *,
  458. integer *), sswap_(integer *, real *, integer *, real *, integer *
  459. );
  460. logical lst2sl;
  461. extern /* Subroutine */ void slabad_(real *, real *);
  462. logical scalea;
  463. integer ip;
  464. real cscale;
  465. extern /* Subroutine */ void sgebak_(char *, char *, integer *, integer *,
  466. integer *, real *, integer *, real *, integer *, integer *), sgebal_(char *, integer *, real *, integer *,
  467. integer *, integer *, real *, integer *);
  468. extern real slamch_(char *), slange_(char *, integer *, integer *,
  469. real *, integer *, real *);
  470. extern /* Subroutine */ void sgehrd_(integer *, integer *, integer *, real
  471. *, integer *, real *, real *, integer *, integer *);
  472. extern int xerbla_(char *, integer *, ftnlen);
  473. extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
  474. integer *, integer *, ftnlen, ftnlen);
  475. real bignum;
  476. extern /* Subroutine */ void slascl_(char *, integer *, integer *, real *,
  477. real *, integer *, integer *, real *, integer *, integer *), slacpy_(char *, integer *, integer *, real *, integer *,
  478. real *, integer *);
  479. logical lastsl;
  480. extern /* Subroutine */ void sorghr_(integer *, integer *, integer *, real
  481. *, integer *, real *, real *, integer *, integer *), shseqr_(char
  482. *, char *, integer *, integer *, integer *, real *, integer *,
  483. real *, real *, real *, integer *, real *, integer *, integer *);
  484. integer minwrk, maxwrk;
  485. real smlnum;
  486. integer hswork;
  487. extern /* Subroutine */ void strsen_(char *, char *, logical *, integer *,
  488. real *, integer *, real *, integer *, real *, real *, integer *,
  489. real *, real *, real *, integer *, integer *, integer *, integer *
  490. );
  491. logical wantst, lquery, wantvs;
  492. integer ihi, ilo;
  493. real dum[1], eps, sep;
  494. /* -- LAPACK driver routine (version 3.7.1) -- */
  495. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  496. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  497. /* June 2017 */
  498. /* ===================================================================== */
  499. /* Test the input arguments */
  500. /* Parameter adjustments */
  501. a_dim1 = *lda;
  502. a_offset = 1 + a_dim1 * 1;
  503. a -= a_offset;
  504. --wr;
  505. --wi;
  506. vs_dim1 = *ldvs;
  507. vs_offset = 1 + vs_dim1 * 1;
  508. vs -= vs_offset;
  509. --work;
  510. --bwork;
  511. /* Function Body */
  512. *info = 0;
  513. lquery = *lwork == -1;
  514. wantvs = lsame_(jobvs, "V");
  515. wantst = lsame_(sort, "S");
  516. if (! wantvs && ! lsame_(jobvs, "N")) {
  517. *info = -1;
  518. } else if (! wantst && ! lsame_(sort, "N")) {
  519. *info = -2;
  520. } else if (*n < 0) {
  521. *info = -4;
  522. } else if (*lda < f2cmax(1,*n)) {
  523. *info = -6;
  524. } else if (*ldvs < 1 || wantvs && *ldvs < *n) {
  525. *info = -11;
  526. }
  527. /* Compute workspace */
  528. /* (Note: Comments in the code beginning "Workspace:" describe the */
  529. /* minimal amount of workspace needed at that point in the code, */
  530. /* as well as the preferred amount for good performance. */
  531. /* NB refers to the optimal block size for the immediately */
  532. /* following subroutine, as returned by ILAENV. */
  533. /* HSWORK refers to the workspace preferred by SHSEQR, as */
  534. /* calculated below. HSWORK is computed assuming ILO=1 and IHI=N, */
  535. /* the worst case.) */
  536. if (*info == 0) {
  537. if (*n == 0) {
  538. minwrk = 1;
  539. maxwrk = 1;
  540. } else {
  541. maxwrk = (*n << 1) + *n * ilaenv_(&c__1, "SGEHRD", " ", n, &c__1,
  542. n, &c__0, (ftnlen)6, (ftnlen)1);
  543. minwrk = *n * 3;
  544. shseqr_("S", jobvs, n, &c__1, n, &a[a_offset], lda, &wr[1], &wi[1]
  545. , &vs[vs_offset], ldvs, &work[1], &c_n1, &ieval);
  546. hswork = work[1];
  547. if (! wantvs) {
  548. /* Computing MAX */
  549. i__1 = maxwrk, i__2 = *n + hswork;
  550. maxwrk = f2cmax(i__1,i__2);
  551. } else {
  552. /* Computing MAX */
  553. i__1 = maxwrk, i__2 = (*n << 1) + (*n - 1) * ilaenv_(&c__1,
  554. "SORGHR", " ", n, &c__1, n, &c_n1, (ftnlen)6, (ftnlen)
  555. 1);
  556. maxwrk = f2cmax(i__1,i__2);
  557. /* Computing MAX */
  558. i__1 = maxwrk, i__2 = *n + hswork;
  559. maxwrk = f2cmax(i__1,i__2);
  560. }
  561. }
  562. work[1] = (real) maxwrk;
  563. if (*lwork < minwrk && ! lquery) {
  564. *info = -13;
  565. }
  566. }
  567. if (*info != 0) {
  568. i__1 = -(*info);
  569. xerbla_("SGEES ", &i__1, (ftnlen)5);
  570. return;
  571. } else if (lquery) {
  572. return;
  573. }
  574. /* Quick return if possible */
  575. if (*n == 0) {
  576. *sdim = 0;
  577. return;
  578. }
  579. /* Get machine constants */
  580. eps = slamch_("P");
  581. smlnum = slamch_("S");
  582. bignum = 1.f / smlnum;
  583. slabad_(&smlnum, &bignum);
  584. smlnum = sqrt(smlnum) / eps;
  585. bignum = 1.f / smlnum;
  586. /* Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
  587. anrm = slange_("M", n, n, &a[a_offset], lda, dum);
  588. scalea = FALSE_;
  589. if (anrm > 0.f && anrm < smlnum) {
  590. scalea = TRUE_;
  591. cscale = smlnum;
  592. } else if (anrm > bignum) {
  593. scalea = TRUE_;
  594. cscale = bignum;
  595. }
  596. if (scalea) {
  597. slascl_("G", &c__0, &c__0, &anrm, &cscale, n, n, &a[a_offset], lda, &
  598. ierr);
  599. }
  600. /* Permute the matrix to make it more nearly triangular */
  601. /* (Workspace: need N) */
  602. ibal = 1;
  603. sgebal_("P", n, &a[a_offset], lda, &ilo, &ihi, &work[ibal], &ierr);
  604. /* Reduce to upper Hessenberg form */
  605. /* (Workspace: need 3*N, prefer 2*N+N*NB) */
  606. itau = *n + ibal;
  607. iwrk = *n + itau;
  608. i__1 = *lwork - iwrk + 1;
  609. sgehrd_(n, &ilo, &ihi, &a[a_offset], lda, &work[itau], &work[iwrk], &i__1,
  610. &ierr);
  611. if (wantvs) {
  612. /* Copy Householder vectors to VS */
  613. slacpy_("L", n, n, &a[a_offset], lda, &vs[vs_offset], ldvs)
  614. ;
  615. /* Generate orthogonal matrix in VS */
  616. /* (Workspace: need 3*N-1, prefer 2*N+(N-1)*NB) */
  617. i__1 = *lwork - iwrk + 1;
  618. sorghr_(n, &ilo, &ihi, &vs[vs_offset], ldvs, &work[itau], &work[iwrk],
  619. &i__1, &ierr);
  620. }
  621. *sdim = 0;
  622. /* Perform QR iteration, accumulating Schur vectors in VS if desired */
  623. /* (Workspace: need N+1, prefer N+HSWORK (see comments) ) */
  624. iwrk = itau;
  625. i__1 = *lwork - iwrk + 1;
  626. shseqr_("S", jobvs, n, &ilo, &ihi, &a[a_offset], lda, &wr[1], &wi[1], &vs[
  627. vs_offset], ldvs, &work[iwrk], &i__1, &ieval);
  628. if (ieval > 0) {
  629. *info = ieval;
  630. }
  631. /* Sort eigenvalues if desired */
  632. if (wantst && *info == 0) {
  633. if (scalea) {
  634. slascl_("G", &c__0, &c__0, &cscale, &anrm, n, &c__1, &wr[1], n, &
  635. ierr);
  636. slascl_("G", &c__0, &c__0, &cscale, &anrm, n, &c__1, &wi[1], n, &
  637. ierr);
  638. }
  639. i__1 = *n;
  640. for (i__ = 1; i__ <= i__1; ++i__) {
  641. bwork[i__] = (*select)(&wr[i__], &wi[i__]);
  642. /* L10: */
  643. }
  644. /* Reorder eigenvalues and transform Schur vectors */
  645. /* (Workspace: none needed) */
  646. i__1 = *lwork - iwrk + 1;
  647. strsen_("N", jobvs, &bwork[1], n, &a[a_offset], lda, &vs[vs_offset],
  648. ldvs, &wr[1], &wi[1], sdim, &s, &sep, &work[iwrk], &i__1,
  649. idum, &c__1, &icond);
  650. if (icond > 0) {
  651. *info = *n + icond;
  652. }
  653. }
  654. if (wantvs) {
  655. /* Undo balancing */
  656. /* (Workspace: need N) */
  657. sgebak_("P", "R", n, &ilo, &ihi, &work[ibal], n, &vs[vs_offset], ldvs,
  658. &ierr);
  659. }
  660. if (scalea) {
  661. /* Undo scaling for the Schur form of A */
  662. slascl_("H", &c__0, &c__0, &cscale, &anrm, n, n, &a[a_offset], lda, &
  663. ierr);
  664. i__1 = *lda + 1;
  665. scopy_(n, &a[a_offset], &i__1, &wr[1], &c__1);
  666. if (cscale == smlnum) {
  667. /* If scaling back towards underflow, adjust WI if an */
  668. /* offdiagonal element of a 2-by-2 block in the Schur form */
  669. /* underflows. */
  670. if (ieval > 0) {
  671. i1 = ieval + 1;
  672. i2 = ihi - 1;
  673. i__1 = ilo - 1;
  674. /* Computing MAX */
  675. i__3 = ilo - 1;
  676. i__2 = f2cmax(i__3,1);
  677. slascl_("G", &c__0, &c__0, &cscale, &anrm, &i__1, &c__1, &wi[
  678. 1], &i__2, &ierr);
  679. } else if (wantst) {
  680. i1 = 1;
  681. i2 = *n - 1;
  682. } else {
  683. i1 = ilo;
  684. i2 = ihi - 1;
  685. }
  686. inxt = i1 - 1;
  687. i__1 = i2;
  688. for (i__ = i1; i__ <= i__1; ++i__) {
  689. if (i__ < inxt) {
  690. goto L20;
  691. }
  692. if (wi[i__] == 0.f) {
  693. inxt = i__ + 1;
  694. } else {
  695. if (a[i__ + 1 + i__ * a_dim1] == 0.f) {
  696. wi[i__] = 0.f;
  697. wi[i__ + 1] = 0.f;
  698. } else if (a[i__ + 1 + i__ * a_dim1] != 0.f && a[i__ + (
  699. i__ + 1) * a_dim1] == 0.f) {
  700. wi[i__] = 0.f;
  701. wi[i__ + 1] = 0.f;
  702. if (i__ > 1) {
  703. i__2 = i__ - 1;
  704. sswap_(&i__2, &a[i__ * a_dim1 + 1], &c__1, &a[(
  705. i__ + 1) * a_dim1 + 1], &c__1);
  706. }
  707. if (*n > i__ + 1) {
  708. i__2 = *n - i__ - 1;
  709. sswap_(&i__2, &a[i__ + (i__ + 2) * a_dim1], lda, &
  710. a[i__ + 1 + (i__ + 2) * a_dim1], lda);
  711. }
  712. if (wantvs) {
  713. sswap_(n, &vs[i__ * vs_dim1 + 1], &c__1, &vs[(i__
  714. + 1) * vs_dim1 + 1], &c__1);
  715. }
  716. a[i__ + (i__ + 1) * a_dim1] = a[i__ + 1 + i__ *
  717. a_dim1];
  718. a[i__ + 1 + i__ * a_dim1] = 0.f;
  719. }
  720. inxt = i__ + 2;
  721. }
  722. L20:
  723. ;
  724. }
  725. }
  726. /* Undo scaling for the imaginary part of the eigenvalues */
  727. i__1 = *n - ieval;
  728. /* Computing MAX */
  729. i__3 = *n - ieval;
  730. i__2 = f2cmax(i__3,1);
  731. slascl_("G", &c__0, &c__0, &cscale, &anrm, &i__1, &c__1, &wi[ieval +
  732. 1], &i__2, &ierr);
  733. }
  734. if (wantst && *info == 0) {
  735. /* Check if reordering successful */
  736. lastsl = TRUE_;
  737. lst2sl = TRUE_;
  738. *sdim = 0;
  739. ip = 0;
  740. i__1 = *n;
  741. for (i__ = 1; i__ <= i__1; ++i__) {
  742. cursl = (*select)(&wr[i__], &wi[i__]);
  743. if (wi[i__] == 0.f) {
  744. if (cursl) {
  745. ++(*sdim);
  746. }
  747. ip = 0;
  748. if (cursl && ! lastsl) {
  749. *info = *n + 2;
  750. }
  751. } else {
  752. if (ip == 1) {
  753. /* Last eigenvalue of conjugate pair */
  754. cursl = cursl || lastsl;
  755. lastsl = cursl;
  756. if (cursl) {
  757. *sdim += 2;
  758. }
  759. ip = -1;
  760. if (cursl && ! lst2sl) {
  761. *info = *n + 2;
  762. }
  763. } else {
  764. /* First eigenvalue of conjugate pair */
  765. ip = 1;
  766. }
  767. }
  768. lst2sl = lastsl;
  769. lastsl = cursl;
  770. /* L30: */
  771. }
  772. }
  773. work[1] = (real) maxwrk;
  774. return;
  775. /* End of SGEES */
  776. } /* sgees_ */