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.

sgeesx.c 31 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. /* procedure parameter types for -A and -C++ */
  231. #ifdef __cplusplus
  232. typedef logical (*L_fp)(...);
  233. #else
  234. typedef logical (*L_fp)();
  235. #endif
  236. /* -- translated by f2c (version 20000121).
  237. You must link the resulting object file with the libraries:
  238. -lf2c -lm (in that order)
  239. */
  240. /* Table of constant values */
  241. static integer c__1 = 1;
  242. static integer c__0 = 0;
  243. static integer c_n1 = -1;
  244. /* > \brief <b> SGEESX computes the eigenvalues, the Schur form, and, optionally, the matrix of Schur vectors
  245. for GE matrices</b> */
  246. /* =========== DOCUMENTATION =========== */
  247. /* Online html documentation available at */
  248. /* http://www.netlib.org/lapack/explore-html/ */
  249. /* > \htmlonly */
  250. /* > Download SGEESX + dependencies */
  251. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgeesx.
  252. f"> */
  253. /* > [TGZ]</a> */
  254. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgeesx.
  255. f"> */
  256. /* > [ZIP]</a> */
  257. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgeesx.
  258. f"> */
  259. /* > [TXT]</a> */
  260. /* > \endhtmlonly */
  261. /* Definition: */
  262. /* =========== */
  263. /* SUBROUTINE SGEESX( JOBVS, SORT, SELECT, SENSE, N, A, LDA, SDIM, */
  264. /* WR, WI, VS, LDVS, RCONDE, RCONDV, WORK, LWORK, */
  265. /* IWORK, LIWORK, BWORK, INFO ) */
  266. /* CHARACTER JOBVS, SENSE, SORT */
  267. /* INTEGER INFO, LDA, LDVS, LIWORK, LWORK, N, SDIM */
  268. /* REAL RCONDE, RCONDV */
  269. /* LOGICAL BWORK( * ) */
  270. /* INTEGER IWORK( * ) */
  271. /* REAL A( LDA, * ), VS( LDVS, * ), WI( * ), WORK( * ), */
  272. /* $ WR( * ) */
  273. /* LOGICAL SELECT */
  274. /* EXTERNAL SELECT */
  275. /* > \par Purpose: */
  276. /* ============= */
  277. /* > */
  278. /* > \verbatim */
  279. /* > */
  280. /* > SGEESX computes for an N-by-N real nonsymmetric matrix A, the */
  281. /* > eigenvalues, the real Schur form T, and, optionally, the matrix of */
  282. /* > Schur vectors Z. This gives the Schur factorization A = Z*T*(Z**T). */
  283. /* > */
  284. /* > Optionally, it also orders the eigenvalues on the diagonal of the */
  285. /* > real Schur form so that selected eigenvalues are at the top left; */
  286. /* > computes a reciprocal condition number for the average of the */
  287. /* > selected eigenvalues (RCONDE); and computes a reciprocal condition */
  288. /* > number for the right invariant subspace corresponding to the */
  289. /* > selected eigenvalues (RCONDV). The leading columns of Z form an */
  290. /* > orthonormal basis for this invariant subspace. */
  291. /* > */
  292. /* > For further explanation of the reciprocal condition numbers RCONDE */
  293. /* > and RCONDV, see Section 4.10 of the LAPACK Users' Guide (where */
  294. /* > these quantities are called s and sep respectively). */
  295. /* > */
  296. /* > A real matrix is in real Schur form if it is upper quasi-triangular */
  297. /* > with 1-by-1 and 2-by-2 blocks. 2-by-2 blocks will be standardized in */
  298. /* > the form */
  299. /* > [ a b ] */
  300. /* > [ c a ] */
  301. /* > */
  302. /* > where b*c < 0. The eigenvalues of such a block are a +- sqrt(bc). */
  303. /* > \endverbatim */
  304. /* Arguments: */
  305. /* ========== */
  306. /* > \param[in] JOBVS */
  307. /* > \verbatim */
  308. /* > JOBVS is CHARACTER*1 */
  309. /* > = 'N': Schur vectors are not computed; */
  310. /* > = 'V': Schur vectors are computed. */
  311. /* > \endverbatim */
  312. /* > */
  313. /* > \param[in] SORT */
  314. /* > \verbatim */
  315. /* > SORT is CHARACTER*1 */
  316. /* > Specifies whether or not to order the eigenvalues on the */
  317. /* > diagonal of the Schur form. */
  318. /* > = 'N': Eigenvalues are not ordered; */
  319. /* > = 'S': Eigenvalues are ordered (see SELECT). */
  320. /* > \endverbatim */
  321. /* > */
  322. /* > \param[in] SELECT */
  323. /* > \verbatim */
  324. /* > SELECT is a LOGICAL FUNCTION of two REAL arguments */
  325. /* > SELECT must be declared EXTERNAL in the calling subroutine. */
  326. /* > If SORT = 'S', SELECT is used to select eigenvalues to sort */
  327. /* > to the top left of the Schur form. */
  328. /* > If SORT = 'N', SELECT is not referenced. */
  329. /* > An eigenvalue WR(j)+sqrt(-1)*WI(j) is selected if */
  330. /* > SELECT(WR(j),WI(j)) is true; i.e., if either one of a */
  331. /* > complex conjugate pair of eigenvalues is selected, then both */
  332. /* > are. Note that a selected complex eigenvalue may no longer */
  333. /* > satisfy SELECT(WR(j),WI(j)) = .TRUE. after ordering, since */
  334. /* > ordering may change the value of complex eigenvalues */
  335. /* > (especially if the eigenvalue is ill-conditioned); in this */
  336. /* > case INFO may be set to N+3 (see INFO below). */
  337. /* > \endverbatim */
  338. /* > */
  339. /* > \param[in] SENSE */
  340. /* > \verbatim */
  341. /* > SENSE is CHARACTER*1 */
  342. /* > Determines which reciprocal condition numbers are computed. */
  343. /* > = 'N': None are computed; */
  344. /* > = 'E': Computed for average of selected eigenvalues only; */
  345. /* > = 'V': Computed for selected right invariant subspace only; */
  346. /* > = 'B': Computed for both. */
  347. /* > If SENSE = 'E', 'V' or 'B', SORT must equal 'S'. */
  348. /* > \endverbatim */
  349. /* > */
  350. /* > \param[in] N */
  351. /* > \verbatim */
  352. /* > N is INTEGER */
  353. /* > The order of the matrix A. N >= 0. */
  354. /* > \endverbatim */
  355. /* > */
  356. /* > \param[in,out] A */
  357. /* > \verbatim */
  358. /* > A is REAL array, dimension (LDA, N) */
  359. /* > On entry, the N-by-N matrix A. */
  360. /* > On exit, A is overwritten by its real Schur form T. */
  361. /* > \endverbatim */
  362. /* > */
  363. /* > \param[in] LDA */
  364. /* > \verbatim */
  365. /* > LDA is INTEGER */
  366. /* > The leading dimension of the array A. LDA >= f2cmax(1,N). */
  367. /* > \endverbatim */
  368. /* > */
  369. /* > \param[out] SDIM */
  370. /* > \verbatim */
  371. /* > SDIM is INTEGER */
  372. /* > If SORT = 'N', SDIM = 0. */
  373. /* > If SORT = 'S', SDIM = number of eigenvalues (after sorting) */
  374. /* > for which SELECT is true. (Complex conjugate */
  375. /* > pairs for which SELECT is true for either */
  376. /* > eigenvalue count as 2.) */
  377. /* > \endverbatim */
  378. /* > */
  379. /* > \param[out] WR */
  380. /* > \verbatim */
  381. /* > WR is REAL array, dimension (N) */
  382. /* > \endverbatim */
  383. /* > */
  384. /* > \param[out] WI */
  385. /* > \verbatim */
  386. /* > WI is REAL array, dimension (N) */
  387. /* > WR and WI contain the real and imaginary parts, respectively, */
  388. /* > of the computed eigenvalues, in the same order that they */
  389. /* > appear on the diagonal of the output Schur form T. Complex */
  390. /* > conjugate pairs of eigenvalues appear consecutively with the */
  391. /* > eigenvalue having the positive imaginary part first. */
  392. /* > \endverbatim */
  393. /* > */
  394. /* > \param[out] VS */
  395. /* > \verbatim */
  396. /* > VS is REAL array, dimension (LDVS,N) */
  397. /* > If JOBVS = 'V', VS contains the orthogonal matrix Z of Schur */
  398. /* > vectors. */
  399. /* > If JOBVS = 'N', VS is not referenced. */
  400. /* > \endverbatim */
  401. /* > */
  402. /* > \param[in] LDVS */
  403. /* > \verbatim */
  404. /* > LDVS is INTEGER */
  405. /* > The leading dimension of the array VS. LDVS >= 1, and if */
  406. /* > JOBVS = 'V', LDVS >= N. */
  407. /* > \endverbatim */
  408. /* > */
  409. /* > \param[out] RCONDE */
  410. /* > \verbatim */
  411. /* > RCONDE is REAL */
  412. /* > If SENSE = 'E' or 'B', RCONDE contains the reciprocal */
  413. /* > condition number for the average of the selected eigenvalues. */
  414. /* > Not referenced if SENSE = 'N' or 'V'. */
  415. /* > \endverbatim */
  416. /* > */
  417. /* > \param[out] RCONDV */
  418. /* > \verbatim */
  419. /* > RCONDV is REAL */
  420. /* > If SENSE = 'V' or 'B', RCONDV contains the reciprocal */
  421. /* > condition number for the selected right invariant subspace. */
  422. /* > Not referenced if SENSE = 'N' or 'E'. */
  423. /* > \endverbatim */
  424. /* > */
  425. /* > \param[out] WORK */
  426. /* > \verbatim */
  427. /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
  428. /* > On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  429. /* > \endverbatim */
  430. /* > */
  431. /* > \param[in] LWORK */
  432. /* > \verbatim */
  433. /* > LWORK is INTEGER */
  434. /* > The dimension of the array WORK. LWORK >= f2cmax(1,3*N). */
  435. /* > Also, if SENSE = 'E' or 'V' or 'B', */
  436. /* > LWORK >= N+2*SDIM*(N-SDIM), where SDIM is the number of */
  437. /* > selected eigenvalues computed by this routine. Note that */
  438. /* > N+2*SDIM*(N-SDIM) <= N+N*N/2. Note also that an error is only */
  439. /* > returned if LWORK < f2cmax(1,3*N), but if SENSE = 'E' or 'V' or */
  440. /* > 'B' this may not be large enough. */
  441. /* > For good performance, LWORK must generally be larger. */
  442. /* > */
  443. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  444. /* > only calculates upper bounds on the optimal sizes of the */
  445. /* > arrays WORK and IWORK, returns these values as the first */
  446. /* > entries of the WORK and IWORK arrays, and no error messages */
  447. /* > related to LWORK or LIWORK are issued by XERBLA. */
  448. /* > \endverbatim */
  449. /* > */
  450. /* > \param[out] IWORK */
  451. /* > \verbatim */
  452. /* > IWORK is INTEGER array, dimension (MAX(1,LIWORK)) */
  453. /* > On exit, if INFO = 0, IWORK(1) returns the optimal LIWORK. */
  454. /* > \endverbatim */
  455. /* > */
  456. /* > \param[in] LIWORK */
  457. /* > \verbatim */
  458. /* > LIWORK is INTEGER */
  459. /* > The dimension of the array IWORK. */
  460. /* > LIWORK >= 1; if SENSE = 'V' or 'B', LIWORK >= SDIM*(N-SDIM). */
  461. /* > Note that SDIM*(N-SDIM) <= N*N/4. Note also that an error is */
  462. /* > only returned if LIWORK < 1, but if SENSE = 'V' or 'B' this */
  463. /* > may not be large enough. */
  464. /* > */
  465. /* > If LIWORK = -1, then a workspace query is assumed; the */
  466. /* > routine only calculates upper bounds on the optimal sizes of */
  467. /* > the arrays WORK and IWORK, returns these values as the first */
  468. /* > entries of the WORK and IWORK arrays, and no error messages */
  469. /* > related to LWORK or LIWORK are issued by XERBLA. */
  470. /* > \endverbatim */
  471. /* > */
  472. /* > \param[out] BWORK */
  473. /* > \verbatim */
  474. /* > BWORK is LOGICAL array, dimension (N) */
  475. /* > Not referenced if SORT = 'N'. */
  476. /* > \endverbatim */
  477. /* > */
  478. /* > \param[out] INFO */
  479. /* > \verbatim */
  480. /* > INFO is INTEGER */
  481. /* > = 0: successful exit */
  482. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  483. /* > > 0: if INFO = i, and i is */
  484. /* > <= N: the QR algorithm failed to compute all the */
  485. /* > eigenvalues; elements 1:ILO-1 and i+1:N of WR and WI */
  486. /* > contain those eigenvalues which have converged; if */
  487. /* > JOBVS = 'V', VS contains the transformation which */
  488. /* > reduces A to its partially converged Schur form. */
  489. /* > = N+1: the eigenvalues could not be reordered because some */
  490. /* > eigenvalues were too close to separate (the problem */
  491. /* > is very ill-conditioned); */
  492. /* > = N+2: after reordering, roundoff changed values of some */
  493. /* > complex eigenvalues so that leading eigenvalues in */
  494. /* > the Schur form no longer satisfy SELECT=.TRUE. This */
  495. /* > could also be caused by underflow due to scaling. */
  496. /* > \endverbatim */
  497. /* Authors: */
  498. /* ======== */
  499. /* > \author Univ. of Tennessee */
  500. /* > \author Univ. of California Berkeley */
  501. /* > \author Univ. of Colorado Denver */
  502. /* > \author NAG Ltd. */
  503. /* > \date June 2016 */
  504. /* > \ingroup realGEeigen */
  505. /* ===================================================================== */
  506. /* Subroutine */ void sgeesx_(char *jobvs, char *sort, L_fp select, char *
  507. sense, integer *n, real *a, integer *lda, integer *sdim, real *wr,
  508. real *wi, real *vs, integer *ldvs, real *rconde, real *rcondv, real *
  509. work, integer *lwork, integer *iwork, integer *liwork, logical *bwork,
  510. integer *info)
  511. {
  512. /* System generated locals */
  513. integer a_dim1, a_offset, vs_dim1, vs_offset, i__1, i__2, i__3;
  514. /* Local variables */
  515. integer ibal;
  516. real anrm;
  517. integer ierr, itau, iwrk, lwrk, inxt, i__, icond, ieval;
  518. extern logical lsame_(char *, char *);
  519. logical cursl;
  520. integer liwrk, i1, i2;
  521. extern /* Subroutine */ void scopy_(integer *, real *, integer *, real *,
  522. integer *), sswap_(integer *, real *, integer *, real *, integer *
  523. );
  524. logical lst2sl;
  525. extern /* Subroutine */ void slabad_(real *, real *);
  526. logical scalea;
  527. integer ip;
  528. real cscale;
  529. extern /* Subroutine */ void sgebak_(char *, char *, integer *, integer *,
  530. integer *, real *, integer *, real *, integer *, integer *), sgebal_(char *, integer *, real *, integer *,
  531. integer *, integer *, real *, integer *);
  532. extern real slamch_(char *);
  533. extern /* Subroutine */ void sgehrd_(integer *, integer *, integer *, real
  534. *, integer *, real *, real *, integer *, integer *);
  535. extern int xerbla_(char *, integer *, ftnlen);
  536. extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
  537. integer *, integer *, ftnlen, ftnlen);
  538. extern real slange_(char *, integer *, integer *, real *, integer *, real
  539. *);
  540. real bignum;
  541. extern /* Subroutine */ void slascl_(char *, integer *, integer *, real *,
  542. real *, integer *, integer *, real *, integer *, integer *), slacpy_(char *, integer *, integer *, real *, integer *,
  543. real *, integer *);
  544. logical wantsb, wantse, lastsl;
  545. extern /* Subroutine */ void sorghr_(integer *, integer *, integer *, real
  546. *, integer *, real *, real *, integer *, integer *), shseqr_(char
  547. *, char *, integer *, integer *, integer *, real *, integer *,
  548. real *, real *, real *, integer *, real *, integer *, integer *);
  549. integer minwrk, maxwrk;
  550. logical wantsn;
  551. real smlnum;
  552. integer hswork;
  553. extern /* Subroutine */ void strsen_(char *, char *, logical *, integer *,
  554. real *, integer *, real *, integer *, real *, real *, integer *,
  555. real *, real *, real *, integer *, integer *, integer *, integer *
  556. );
  557. logical wantst, lquery, wantsv, wantvs;
  558. integer ihi, ilo;
  559. real dum[1], eps;
  560. /* -- LAPACK driver routine (version 3.7.0) -- */
  561. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  562. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  563. /* June 2016 */
  564. /* ===================================================================== */
  565. /* Test the input arguments */
  566. /* Parameter adjustments */
  567. a_dim1 = *lda;
  568. a_offset = 1 + a_dim1 * 1;
  569. a -= a_offset;
  570. --wr;
  571. --wi;
  572. vs_dim1 = *ldvs;
  573. vs_offset = 1 + vs_dim1 * 1;
  574. vs -= vs_offset;
  575. --work;
  576. --iwork;
  577. --bwork;
  578. /* Function Body */
  579. *info = 0;
  580. wantvs = lsame_(jobvs, "V");
  581. wantst = lsame_(sort, "S");
  582. wantsn = lsame_(sense, "N");
  583. wantse = lsame_(sense, "E");
  584. wantsv = lsame_(sense, "V");
  585. wantsb = lsame_(sense, "B");
  586. lquery = *lwork == -1 || *liwork == -1;
  587. if (! wantvs && ! lsame_(jobvs, "N")) {
  588. *info = -1;
  589. } else if (! wantst && ! lsame_(sort, "N")) {
  590. *info = -2;
  591. } else if (! (wantsn || wantse || wantsv || wantsb) || ! wantst && !
  592. wantsn) {
  593. *info = -4;
  594. } else if (*n < 0) {
  595. *info = -5;
  596. } else if (*lda < f2cmax(1,*n)) {
  597. *info = -7;
  598. } else if (*ldvs < 1 || wantvs && *ldvs < *n) {
  599. *info = -12;
  600. }
  601. /* Compute workspace */
  602. /* (Note: Comments in the code beginning "RWorkspace:" describe the */
  603. /* minimal amount of real workspace needed at that point in the */
  604. /* code, as well as the preferred amount for good performance. */
  605. /* IWorkspace refers to integer workspace. */
  606. /* NB refers to the optimal block size for the immediately */
  607. /* following subroutine, as returned by ILAENV. */
  608. /* HSWORK refers to the workspace preferred by SHSEQR, as */
  609. /* calculated below. HSWORK is computed assuming ILO=1 and IHI=N, */
  610. /* the worst case. */
  611. /* If SENSE = 'E', 'V' or 'B', then the amount of workspace needed */
  612. /* depends on SDIM, which is computed by the routine STRSEN later */
  613. /* in the code.) */
  614. if (*info == 0) {
  615. liwrk = 1;
  616. if (*n == 0) {
  617. minwrk = 1;
  618. lwrk = 1;
  619. } else {
  620. maxwrk = (*n << 1) + *n * ilaenv_(&c__1, "SGEHRD", " ", n, &c__1,
  621. n, &c__0, (ftnlen)6, (ftnlen)1);
  622. minwrk = *n * 3;
  623. shseqr_("S", jobvs, n, &c__1, n, &a[a_offset], lda, &wr[1], &wi[1]
  624. , &vs[vs_offset], ldvs, &work[1], &c_n1, &ieval);
  625. hswork = work[1];
  626. if (! wantvs) {
  627. /* Computing MAX */
  628. i__1 = maxwrk, i__2 = *n + hswork;
  629. maxwrk = f2cmax(i__1,i__2);
  630. } else {
  631. /* Computing MAX */
  632. i__1 = maxwrk, i__2 = (*n << 1) + (*n - 1) * ilaenv_(&c__1,
  633. "SORGHR", " ", n, &c__1, n, &c_n1, (ftnlen)6, (ftnlen)
  634. 1);
  635. maxwrk = f2cmax(i__1,i__2);
  636. /* Computing MAX */
  637. i__1 = maxwrk, i__2 = *n + hswork;
  638. maxwrk = f2cmax(i__1,i__2);
  639. }
  640. lwrk = maxwrk;
  641. if (! wantsn) {
  642. /* Computing MAX */
  643. i__1 = lwrk, i__2 = *n + *n * *n / 2;
  644. lwrk = f2cmax(i__1,i__2);
  645. }
  646. if (wantsv || wantsb) {
  647. liwrk = *n * *n / 4;
  648. }
  649. }
  650. iwork[1] = liwrk;
  651. work[1] = (real) lwrk;
  652. if (*lwork < minwrk && ! lquery) {
  653. *info = -16;
  654. } else if (*liwork < 1 && ! lquery) {
  655. *info = -18;
  656. }
  657. }
  658. if (*info != 0) {
  659. i__1 = -(*info);
  660. xerbla_("SGEESX", &i__1, (ftnlen)6);
  661. return;
  662. } else if (lquery) {
  663. return;
  664. }
  665. /* Quick return if possible */
  666. if (*n == 0) {
  667. *sdim = 0;
  668. return;
  669. }
  670. /* Get machine constants */
  671. eps = slamch_("P");
  672. smlnum = slamch_("S");
  673. bignum = 1.f / smlnum;
  674. slabad_(&smlnum, &bignum);
  675. smlnum = sqrt(smlnum) / eps;
  676. bignum = 1.f / smlnum;
  677. /* Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
  678. anrm = slange_("M", n, n, &a[a_offset], lda, dum);
  679. scalea = FALSE_;
  680. if (anrm > 0.f && anrm < smlnum) {
  681. scalea = TRUE_;
  682. cscale = smlnum;
  683. } else if (anrm > bignum) {
  684. scalea = TRUE_;
  685. cscale = bignum;
  686. }
  687. if (scalea) {
  688. slascl_("G", &c__0, &c__0, &anrm, &cscale, n, n, &a[a_offset], lda, &
  689. ierr);
  690. }
  691. /* Permute the matrix to make it more nearly triangular */
  692. /* (RWorkspace: need N) */
  693. ibal = 1;
  694. sgebal_("P", n, &a[a_offset], lda, &ilo, &ihi, &work[ibal], &ierr);
  695. /* Reduce to upper Hessenberg form */
  696. /* (RWorkspace: need 3*N, prefer 2*N+N*NB) */
  697. itau = *n + ibal;
  698. iwrk = *n + itau;
  699. i__1 = *lwork - iwrk + 1;
  700. sgehrd_(n, &ilo, &ihi, &a[a_offset], lda, &work[itau], &work[iwrk], &i__1,
  701. &ierr);
  702. if (wantvs) {
  703. /* Copy Householder vectors to VS */
  704. slacpy_("L", n, n, &a[a_offset], lda, &vs[vs_offset], ldvs)
  705. ;
  706. /* Generate orthogonal matrix in VS */
  707. /* (RWorkspace: need 3*N-1, prefer 2*N+(N-1)*NB) */
  708. i__1 = *lwork - iwrk + 1;
  709. sorghr_(n, &ilo, &ihi, &vs[vs_offset], ldvs, &work[itau], &work[iwrk],
  710. &i__1, &ierr);
  711. }
  712. *sdim = 0;
  713. /* Perform QR iteration, accumulating Schur vectors in VS if desired */
  714. /* (RWorkspace: need N+1, prefer N+HSWORK (see comments) ) */
  715. iwrk = itau;
  716. i__1 = *lwork - iwrk + 1;
  717. shseqr_("S", jobvs, n, &ilo, &ihi, &a[a_offset], lda, &wr[1], &wi[1], &vs[
  718. vs_offset], ldvs, &work[iwrk], &i__1, &ieval);
  719. if (ieval > 0) {
  720. *info = ieval;
  721. }
  722. /* Sort eigenvalues if desired */
  723. if (wantst && *info == 0) {
  724. if (scalea) {
  725. slascl_("G", &c__0, &c__0, &cscale, &anrm, n, &c__1, &wr[1], n, &
  726. ierr);
  727. slascl_("G", &c__0, &c__0, &cscale, &anrm, n, &c__1, &wi[1], n, &
  728. ierr);
  729. }
  730. i__1 = *n;
  731. for (i__ = 1; i__ <= i__1; ++i__) {
  732. bwork[i__] = (*select)(&wr[i__], &wi[i__]);
  733. /* L10: */
  734. }
  735. /* Reorder eigenvalues, transform Schur vectors, and compute */
  736. /* reciprocal condition numbers */
  737. /* (RWorkspace: if SENSE is not 'N', need N+2*SDIM*(N-SDIM) */
  738. /* otherwise, need N ) */
  739. /* (IWorkspace: if SENSE is 'V' or 'B', need SDIM*(N-SDIM) */
  740. /* otherwise, need 0 ) */
  741. i__1 = *lwork - iwrk + 1;
  742. strsen_(sense, jobvs, &bwork[1], n, &a[a_offset], lda, &vs[vs_offset],
  743. ldvs, &wr[1], &wi[1], sdim, rconde, rcondv, &work[iwrk], &
  744. i__1, &iwork[1], liwork, &icond);
  745. if (! wantsn) {
  746. /* Computing MAX */
  747. i__1 = maxwrk, i__2 = *n + (*sdim << 1) * (*n - *sdim);
  748. maxwrk = f2cmax(i__1,i__2);
  749. }
  750. if (icond == -15) {
  751. /* Not enough real workspace */
  752. *info = -16;
  753. } else if (icond == -17) {
  754. /* Not enough integer workspace */
  755. *info = -18;
  756. } else if (icond > 0) {
  757. /* STRSEN failed to reorder or to restore standard Schur form */
  758. *info = icond + *n;
  759. }
  760. }
  761. if (wantvs) {
  762. /* Undo balancing */
  763. /* (RWorkspace: need N) */
  764. sgebak_("P", "R", n, &ilo, &ihi, &work[ibal], n, &vs[vs_offset], ldvs,
  765. &ierr);
  766. }
  767. if (scalea) {
  768. /* Undo scaling for the Schur form of A */
  769. slascl_("H", &c__0, &c__0, &cscale, &anrm, n, n, &a[a_offset], lda, &
  770. ierr);
  771. i__1 = *lda + 1;
  772. scopy_(n, &a[a_offset], &i__1, &wr[1], &c__1);
  773. if ((wantsv || wantsb) && *info == 0) {
  774. dum[0] = *rcondv;
  775. slascl_("G", &c__0, &c__0, &cscale, &anrm, &c__1, &c__1, dum, &
  776. c__1, &ierr);
  777. *rcondv = dum[0];
  778. }
  779. if (cscale == smlnum) {
  780. /* If scaling back towards underflow, adjust WI if an */
  781. /* offdiagonal element of a 2-by-2 block in the Schur form */
  782. /* underflows. */
  783. if (ieval > 0) {
  784. i1 = ieval + 1;
  785. i2 = ihi - 1;
  786. i__1 = ilo - 1;
  787. slascl_("G", &c__0, &c__0, &cscale, &anrm, &i__1, &c__1, &wi[
  788. 1], n, &ierr);
  789. } else if (wantst) {
  790. i1 = 1;
  791. i2 = *n - 1;
  792. } else {
  793. i1 = ilo;
  794. i2 = ihi - 1;
  795. }
  796. inxt = i1 - 1;
  797. i__1 = i2;
  798. for (i__ = i1; i__ <= i__1; ++i__) {
  799. if (i__ < inxt) {
  800. goto L20;
  801. }
  802. if (wi[i__] == 0.f) {
  803. inxt = i__ + 1;
  804. } else {
  805. if (a[i__ + 1 + i__ * a_dim1] == 0.f) {
  806. wi[i__] = 0.f;
  807. wi[i__ + 1] = 0.f;
  808. } else if (a[i__ + 1 + i__ * a_dim1] != 0.f && a[i__ + (
  809. i__ + 1) * a_dim1] == 0.f) {
  810. wi[i__] = 0.f;
  811. wi[i__ + 1] = 0.f;
  812. if (i__ > 1) {
  813. i__2 = i__ - 1;
  814. sswap_(&i__2, &a[i__ * a_dim1 + 1], &c__1, &a[(
  815. i__ + 1) * a_dim1 + 1], &c__1);
  816. }
  817. if (*n > i__ + 1) {
  818. i__2 = *n - i__ - 1;
  819. sswap_(&i__2, &a[i__ + (i__ + 2) * a_dim1], lda, &
  820. a[i__ + 1 + (i__ + 2) * a_dim1], lda);
  821. }
  822. if (wantvs) {
  823. sswap_(n, &vs[i__ * vs_dim1 + 1], &c__1, &vs[(i__
  824. + 1) * vs_dim1 + 1], &c__1);
  825. }
  826. a[i__ + (i__ + 1) * a_dim1] = a[i__ + 1 + i__ *
  827. a_dim1];
  828. a[i__ + 1 + i__ * a_dim1] = 0.f;
  829. }
  830. inxt = i__ + 2;
  831. }
  832. L20:
  833. ;
  834. }
  835. }
  836. i__1 = *n - ieval;
  837. /* Computing MAX */
  838. i__3 = *n - ieval;
  839. i__2 = f2cmax(i__3,1);
  840. slascl_("G", &c__0, &c__0, &cscale, &anrm, &i__1, &c__1, &wi[ieval +
  841. 1], &i__2, &ierr);
  842. }
  843. if (wantst && *info == 0) {
  844. /* Check if reordering successful */
  845. lastsl = TRUE_;
  846. lst2sl = TRUE_;
  847. *sdim = 0;
  848. ip = 0;
  849. i__1 = *n;
  850. for (i__ = 1; i__ <= i__1; ++i__) {
  851. cursl = (*select)(&wr[i__], &wi[i__]);
  852. if (wi[i__] == 0.f) {
  853. if (cursl) {
  854. ++(*sdim);
  855. }
  856. ip = 0;
  857. if (cursl && ! lastsl) {
  858. *info = *n + 2;
  859. }
  860. } else {
  861. if (ip == 1) {
  862. /* Last eigenvalue of conjugate pair */
  863. cursl = cursl || lastsl;
  864. lastsl = cursl;
  865. if (cursl) {
  866. *sdim += 2;
  867. }
  868. ip = -1;
  869. if (cursl && ! lst2sl) {
  870. *info = *n + 2;
  871. }
  872. } else {
  873. /* First eigenvalue of conjugate pair */
  874. ip = 1;
  875. }
  876. }
  877. lst2sl = lastsl;
  878. lastsl = cursl;
  879. /* L30: */
  880. }
  881. }
  882. work[1] = (real) maxwrk;
  883. if (wantsv || wantsb) {
  884. iwork[1] = *sdim * (*n - *sdim);
  885. } else {
  886. iwork[1] = 1;
  887. }
  888. return;
  889. /* End of SGEESX */
  890. } /* sgeesx_ */