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.

sgges3.c 32 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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_n1 = -1;
  242. static integer c__1 = 1;
  243. static integer c__0 = 0;
  244. static real c_b36 = 0.f;
  245. static real c_b37 = 1.f;
  246. /* > \brief <b> SGGES3 computes the eigenvalues, the Schur form, and, optionally, the matrix of Schur vectors
  247. for GE matrices (blocked algorithm)</b> */
  248. /* =========== DOCUMENTATION =========== */
  249. /* Online html documentation available at */
  250. /* http://www.netlib.org/lapack/explore-html/ */
  251. /* > \htmlonly */
  252. /* > Download SGGES3 + dependencies */
  253. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgges3.
  254. f"> */
  255. /* > [TGZ]</a> */
  256. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgges3.
  257. f"> */
  258. /* > [ZIP]</a> */
  259. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgges3.
  260. f"> */
  261. /* > [TXT]</a> */
  262. /* > \endhtmlonly */
  263. /* Definition: */
  264. /* =========== */
  265. /* SUBROUTINE SGGES3( JOBVSL, JOBVSR, SORT, SELCTG, N, A, LDA, B, */
  266. /* $ LDB, SDIM, ALPHAR, ALPHAI, BETA, VSL, LDVSL, */
  267. /* $ VSR, LDVSR, WORK, LWORK, BWORK, INFO ) */
  268. /* CHARACTER JOBVSL, JOBVSR, SORT */
  269. /* INTEGER INFO, LDA, LDB, LDVSL, LDVSR, LWORK, N, SDIM */
  270. /* LOGICAL BWORK( * ) */
  271. /* REAL A( LDA, * ), ALPHAI( * ), ALPHAR( * ), */
  272. /* $ B( LDB, * ), BETA( * ), VSL( LDVSL, * ), */
  273. /* $ VSR( LDVSR, * ), WORK( * ) */
  274. /* LOGICAL SELCTG */
  275. /* EXTERNAL SELCTG */
  276. /* > \par Purpose: */
  277. /* ============= */
  278. /* > */
  279. /* > \verbatim */
  280. /* > */
  281. /* > SGGES3 computes for a pair of N-by-N real nonsymmetric matrices (A,B), */
  282. /* > the generalized eigenvalues, the generalized real Schur form (S,T), */
  283. /* > optionally, the left and/or right matrices of Schur vectors (VSL and */
  284. /* > VSR). This gives the generalized Schur factorization */
  285. /* > */
  286. /* > (A,B) = ( (VSL)*S*(VSR)**T, (VSL)*T*(VSR)**T ) */
  287. /* > */
  288. /* > Optionally, it also orders the eigenvalues so that a selected cluster */
  289. /* > of eigenvalues appears in the leading diagonal blocks of the upper */
  290. /* > quasi-triangular matrix S and the upper triangular matrix T.The */
  291. /* > leading columns of VSL and VSR then form an orthonormal basis for the */
  292. /* > corresponding left and right eigenspaces (deflating subspaces). */
  293. /* > */
  294. /* > (If only the generalized eigenvalues are needed, use the driver */
  295. /* > SGGEV instead, which is faster.) */
  296. /* > */
  297. /* > A generalized eigenvalue for a pair of matrices (A,B) is a scalar w */
  298. /* > or a ratio alpha/beta = w, such that A - w*B is singular. It is */
  299. /* > usually represented as the pair (alpha,beta), as there is a */
  300. /* > reasonable interpretation for beta=0 or both being zero. */
  301. /* > */
  302. /* > A pair of matrices (S,T) is in generalized real Schur form if T is */
  303. /* > upper triangular with non-negative diagonal and S is block upper */
  304. /* > triangular with 1-by-1 and 2-by-2 blocks. 1-by-1 blocks correspond */
  305. /* > to real generalized eigenvalues, while 2-by-2 blocks of S will be */
  306. /* > "standardized" by making the corresponding elements of T have the */
  307. /* > form: */
  308. /* > [ a 0 ] */
  309. /* > [ 0 b ] */
  310. /* > */
  311. /* > and the pair of corresponding 2-by-2 blocks in S and T will have a */
  312. /* > complex conjugate pair of generalized eigenvalues. */
  313. /* > */
  314. /* > \endverbatim */
  315. /* Arguments: */
  316. /* ========== */
  317. /* > \param[in] JOBVSL */
  318. /* > \verbatim */
  319. /* > JOBVSL is CHARACTER*1 */
  320. /* > = 'N': do not compute the left Schur vectors; */
  321. /* > = 'V': compute the left Schur vectors. */
  322. /* > \endverbatim */
  323. /* > */
  324. /* > \param[in] JOBVSR */
  325. /* > \verbatim */
  326. /* > JOBVSR is CHARACTER*1 */
  327. /* > = 'N': do not compute the right Schur vectors; */
  328. /* > = 'V': compute the right Schur vectors. */
  329. /* > \endverbatim */
  330. /* > */
  331. /* > \param[in] SORT */
  332. /* > \verbatim */
  333. /* > SORT is CHARACTER*1 */
  334. /* > Specifies whether or not to order the eigenvalues on the */
  335. /* > diagonal of the generalized Schur form. */
  336. /* > = 'N': Eigenvalues are not ordered; */
  337. /* > = 'S': Eigenvalues are ordered (see SELCTG); */
  338. /* > \endverbatim */
  339. /* > */
  340. /* > \param[in] SELCTG */
  341. /* > \verbatim */
  342. /* > SELCTG is a LOGICAL FUNCTION of three REAL arguments */
  343. /* > SELCTG must be declared EXTERNAL in the calling subroutine. */
  344. /* > If SORT = 'N', SELCTG is not referenced. */
  345. /* > If SORT = 'S', SELCTG is used to select eigenvalues to sort */
  346. /* > to the top left of the Schur form. */
  347. /* > An eigenvalue (ALPHAR(j)+ALPHAI(j))/BETA(j) is selected if */
  348. /* > SELCTG(ALPHAR(j),ALPHAI(j),BETA(j)) is true; i.e. if either */
  349. /* > one of a complex conjugate pair of eigenvalues is selected, */
  350. /* > then both complex eigenvalues are selected. */
  351. /* > */
  352. /* > Note that in the ill-conditioned case, a selected complex */
  353. /* > eigenvalue may no longer satisfy SELCTG(ALPHAR(j),ALPHAI(j), */
  354. /* > BETA(j)) = .TRUE. after ordering. INFO is to be set to N+2 */
  355. /* > in this case. */
  356. /* > \endverbatim */
  357. /* > */
  358. /* > \param[in] N */
  359. /* > \verbatim */
  360. /* > N is INTEGER */
  361. /* > The order of the matrices A, B, VSL, and VSR. N >= 0. */
  362. /* > \endverbatim */
  363. /* > */
  364. /* > \param[in,out] A */
  365. /* > \verbatim */
  366. /* > A is REAL array, dimension (LDA, N) */
  367. /* > On entry, the first of the pair of matrices. */
  368. /* > On exit, A has been overwritten by its generalized Schur */
  369. /* > form S. */
  370. /* > \endverbatim */
  371. /* > */
  372. /* > \param[in] LDA */
  373. /* > \verbatim */
  374. /* > LDA is INTEGER */
  375. /* > The leading dimension of A. LDA >= f2cmax(1,N). */
  376. /* > \endverbatim */
  377. /* > */
  378. /* > \param[in,out] B */
  379. /* > \verbatim */
  380. /* > B is REAL array, dimension (LDB, N) */
  381. /* > On entry, the second of the pair of matrices. */
  382. /* > On exit, B has been overwritten by its generalized Schur */
  383. /* > form T. */
  384. /* > \endverbatim */
  385. /* > */
  386. /* > \param[in] LDB */
  387. /* > \verbatim */
  388. /* > LDB is INTEGER */
  389. /* > The leading dimension of B. LDB >= f2cmax(1,N). */
  390. /* > \endverbatim */
  391. /* > */
  392. /* > \param[out] SDIM */
  393. /* > \verbatim */
  394. /* > SDIM is INTEGER */
  395. /* > If SORT = 'N', SDIM = 0. */
  396. /* > If SORT = 'S', SDIM = number of eigenvalues (after sorting) */
  397. /* > for which SELCTG is true. (Complex conjugate pairs for which */
  398. /* > SELCTG is true for either eigenvalue count as 2.) */
  399. /* > \endverbatim */
  400. /* > */
  401. /* > \param[out] ALPHAR */
  402. /* > \verbatim */
  403. /* > ALPHAR is REAL array, dimension (N) */
  404. /* > \endverbatim */
  405. /* > */
  406. /* > \param[out] ALPHAI */
  407. /* > \verbatim */
  408. /* > ALPHAI is REAL array, dimension (N) */
  409. /* > \endverbatim */
  410. /* > */
  411. /* > \param[out] BETA */
  412. /* > \verbatim */
  413. /* > BETA is REAL array, dimension (N) */
  414. /* > On exit, (ALPHAR(j) + ALPHAI(j)*i)/BETA(j), j=1,...,N, will */
  415. /* > be the generalized eigenvalues. ALPHAR(j) + ALPHAI(j)*i, */
  416. /* > and BETA(j),j=1,...,N are the diagonals of the complex Schur */
  417. /* > form (S,T) that would result if the 2-by-2 diagonal blocks of */
  418. /* > the real Schur form of (A,B) were further reduced to */
  419. /* > triangular form using 2-by-2 complex unitary transformations. */
  420. /* > If ALPHAI(j) is zero, then the j-th eigenvalue is real; if */
  421. /* > positive, then the j-th and (j+1)-st eigenvalues are a */
  422. /* > complex conjugate pair, with ALPHAI(j+1) negative. */
  423. /* > */
  424. /* > Note: the quotients ALPHAR(j)/BETA(j) and ALPHAI(j)/BETA(j) */
  425. /* > may easily over- or underflow, and BETA(j) may even be zero. */
  426. /* > Thus, the user should avoid naively computing the ratio. */
  427. /* > However, ALPHAR and ALPHAI will be always less than and */
  428. /* > usually comparable with norm(A) in magnitude, and BETA always */
  429. /* > less than and usually comparable with norm(B). */
  430. /* > \endverbatim */
  431. /* > */
  432. /* > \param[out] VSL */
  433. /* > \verbatim */
  434. /* > VSL is REAL array, dimension (LDVSL,N) */
  435. /* > If JOBVSL = 'V', VSL will contain the left Schur vectors. */
  436. /* > Not referenced if JOBVSL = 'N'. */
  437. /* > \endverbatim */
  438. /* > */
  439. /* > \param[in] LDVSL */
  440. /* > \verbatim */
  441. /* > LDVSL is INTEGER */
  442. /* > The leading dimension of the matrix VSL. LDVSL >=1, and */
  443. /* > if JOBVSL = 'V', LDVSL >= N. */
  444. /* > \endverbatim */
  445. /* > */
  446. /* > \param[out] VSR */
  447. /* > \verbatim */
  448. /* > VSR is REAL array, dimension (LDVSR,N) */
  449. /* > If JOBVSR = 'V', VSR will contain the right Schur vectors. */
  450. /* > Not referenced if JOBVSR = 'N'. */
  451. /* > \endverbatim */
  452. /* > */
  453. /* > \param[in] LDVSR */
  454. /* > \verbatim */
  455. /* > LDVSR is INTEGER */
  456. /* > The leading dimension of the matrix VSR. LDVSR >= 1, and */
  457. /* > if JOBVSR = 'V', LDVSR >= N. */
  458. /* > \endverbatim */
  459. /* > */
  460. /* > \param[out] WORK */
  461. /* > \verbatim */
  462. /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
  463. /* > On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  464. /* > \endverbatim */
  465. /* > */
  466. /* > \param[in] LWORK */
  467. /* > \verbatim */
  468. /* > LWORK is INTEGER */
  469. /* > The dimension of the array WORK. */
  470. /* > */
  471. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  472. /* > only calculates the optimal size of the WORK array, returns */
  473. /* > this value as the first entry of the WORK array, and no error */
  474. /* > message related to LWORK is issued by XERBLA. */
  475. /* > \endverbatim */
  476. /* > */
  477. /* > \param[out] BWORK */
  478. /* > \verbatim */
  479. /* > BWORK is LOGICAL array, dimension (N) */
  480. /* > Not referenced if SORT = 'N'. */
  481. /* > \endverbatim */
  482. /* > */
  483. /* > \param[out] INFO */
  484. /* > \verbatim */
  485. /* > INFO is INTEGER */
  486. /* > = 0: successful exit */
  487. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  488. /* > = 1,...,N: */
  489. /* > The QZ iteration failed. (A,B) are not in Schur */
  490. /* > form, but ALPHAR(j), ALPHAI(j), and BETA(j) should */
  491. /* > be correct for j=INFO+1,...,N. */
  492. /* > > N: =N+1: other than QZ iteration failed in SHGEQZ. */
  493. /* > =N+2: after reordering, roundoff changed values of */
  494. /* > some complex eigenvalues so that leading */
  495. /* > eigenvalues in the Generalized Schur form no */
  496. /* > longer satisfy SELCTG=.TRUE. This could also */
  497. /* > be caused due to scaling. */
  498. /* > =N+3: reordering failed in STGSEN. */
  499. /* > \endverbatim */
  500. /* Authors: */
  501. /* ======== */
  502. /* > \author Univ. of Tennessee */
  503. /* > \author Univ. of California Berkeley */
  504. /* > \author Univ. of Colorado Denver */
  505. /* > \author NAG Ltd. */
  506. /* > \date January 2015 */
  507. /* > \ingroup realGEeigen */
  508. /* ===================================================================== */
  509. /* Subroutine */ void sgges3_(char *jobvsl, char *jobvsr, char *sort, L_fp
  510. selctg, integer *n, real *a, integer *lda, real *b, integer *ldb,
  511. integer *sdim, real *alphar, real *alphai, real *beta, real *vsl,
  512. integer *ldvsl, real *vsr, integer *ldvsr, real *work, integer *lwork,
  513. logical *bwork, integer *info)
  514. {
  515. /* System generated locals */
  516. integer a_dim1, a_offset, b_dim1, b_offset, vsl_dim1, vsl_offset,
  517. vsr_dim1, vsr_offset, i__1, i__2;
  518. real r__1;
  519. /* Local variables */
  520. real anrm, bnrm;
  521. integer idum[1], ierr, itau, iwrk;
  522. real pvsl, pvsr;
  523. integer i__;
  524. extern logical lsame_(char *, char *);
  525. integer ileft, icols;
  526. logical cursl, ilvsl, ilvsr;
  527. integer irows;
  528. extern /* Subroutine */ void sgghd3_(char *, char *, integer *, integer *,
  529. integer *, real *, integer *, real *, integer *, real *, integer *
  530. , real *, integer *, real *, integer *, integer *)
  531. ;
  532. logical lst2sl;
  533. extern /* Subroutine */ void slabad_(real *, real *);
  534. integer ip;
  535. extern /* Subroutine */ void sggbak_(char *, char *, integer *, integer *,
  536. integer *, real *, real *, integer *, real *, integer *, integer *
  537. ), sggbal_(char *, integer *, real *, integer *,
  538. real *, integer *, integer *, integer *, real *, real *, real *,
  539. integer *);
  540. logical ilascl, ilbscl;
  541. extern real slamch_(char *), slange_(char *, integer *, integer *,
  542. real *, integer *, real *);
  543. real safmin;
  544. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  545. real safmax, bignum;
  546. extern /* Subroutine */ void slascl_(char *, integer *, integer *, real *,
  547. real *, integer *, integer *, real *, integer *, integer *);
  548. integer ijobvl, iright;
  549. extern /* Subroutine */ void sgeqrf_(integer *, integer *, real *, integer
  550. *, real *, real *, integer *, integer *);
  551. integer ijobvr;
  552. extern /* Subroutine */ void slacpy_(char *, integer *, integer *, real *,
  553. integer *, real *, integer *), slaset_(char *, integer *,
  554. integer *, real *, real *, real *, integer *);
  555. real anrmto, bnrmto;
  556. logical lastsl;
  557. extern /* Subroutine */ void shgeqz_(char *, char *, char *, integer *,
  558. integer *, integer *, real *, integer *, real *, integer *, real *
  559. , real *, real *, real *, integer *, real *, integer *, real *,
  560. integer *, integer *), stgsen_(integer *,
  561. logical *, logical *, logical *, integer *, real *, integer *,
  562. real *, integer *, real *, real *, real *, real *, integer *,
  563. real *, integer *, integer *, real *, real *, real *, real *,
  564. integer *, integer *, integer *, integer *);
  565. real smlnum;
  566. extern /* Subroutine */ void sorgqr_(integer *, integer *, integer *, real
  567. *, integer *, real *, real *, integer *, integer *);
  568. logical wantst, lquery;
  569. integer lwkopt;
  570. extern /* Subroutine */ void sormqr_(char *, char *, integer *, integer *,
  571. integer *, real *, integer *, real *, real *, integer *, real *,
  572. integer *, integer *);
  573. real dif[2];
  574. integer ihi, ilo;
  575. real eps;
  576. /* -- LAPACK driver routine (version 3.6.0) -- */
  577. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  578. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  579. /* January 2015 */
  580. /* ===================================================================== */
  581. /* Decode the input arguments */
  582. /* Parameter adjustments */
  583. a_dim1 = *lda;
  584. a_offset = 1 + a_dim1 * 1;
  585. a -= a_offset;
  586. b_dim1 = *ldb;
  587. b_offset = 1 + b_dim1 * 1;
  588. b -= b_offset;
  589. --alphar;
  590. --alphai;
  591. --beta;
  592. vsl_dim1 = *ldvsl;
  593. vsl_offset = 1 + vsl_dim1 * 1;
  594. vsl -= vsl_offset;
  595. vsr_dim1 = *ldvsr;
  596. vsr_offset = 1 + vsr_dim1 * 1;
  597. vsr -= vsr_offset;
  598. --work;
  599. --bwork;
  600. /* Function Body */
  601. if (lsame_(jobvsl, "N")) {
  602. ijobvl = 1;
  603. ilvsl = FALSE_;
  604. } else if (lsame_(jobvsl, "V")) {
  605. ijobvl = 2;
  606. ilvsl = TRUE_;
  607. } else {
  608. ijobvl = -1;
  609. ilvsl = FALSE_;
  610. }
  611. if (lsame_(jobvsr, "N")) {
  612. ijobvr = 1;
  613. ilvsr = FALSE_;
  614. } else if (lsame_(jobvsr, "V")) {
  615. ijobvr = 2;
  616. ilvsr = TRUE_;
  617. } else {
  618. ijobvr = -1;
  619. ilvsr = FALSE_;
  620. }
  621. wantst = lsame_(sort, "S");
  622. /* Test the input arguments */
  623. *info = 0;
  624. lquery = *lwork == -1;
  625. if (ijobvl <= 0) {
  626. *info = -1;
  627. } else if (ijobvr <= 0) {
  628. *info = -2;
  629. } else if (! wantst && ! lsame_(sort, "N")) {
  630. *info = -3;
  631. } else if (*n < 0) {
  632. *info = -5;
  633. } else if (*lda < f2cmax(1,*n)) {
  634. *info = -7;
  635. } else if (*ldb < f2cmax(1,*n)) {
  636. *info = -9;
  637. } else if (*ldvsl < 1 || ilvsl && *ldvsl < *n) {
  638. *info = -15;
  639. } else if (*ldvsr < 1 || ilvsr && *ldvsr < *n) {
  640. *info = -17;
  641. } else if (*lwork < *n * 6 + 16 && ! lquery) {
  642. *info = -19;
  643. }
  644. /* Compute workspace */
  645. if (*info == 0) {
  646. sgeqrf_(n, n, &b[b_offset], ldb, &work[1], &work[1], &c_n1, &ierr);
  647. /* Computing MAX */
  648. i__1 = *n * 6 + 16, i__2 = *n * 3 + (integer) work[1];
  649. lwkopt = f2cmax(i__1,i__2);
  650. sormqr_("L", "T", n, n, n, &b[b_offset], ldb, &work[1], &a[a_offset],
  651. lda, &work[1], &c_n1, &ierr);
  652. /* Computing MAX */
  653. i__1 = lwkopt, i__2 = *n * 3 + (integer) work[1];
  654. lwkopt = f2cmax(i__1,i__2);
  655. if (ilvsl) {
  656. sorgqr_(n, n, n, &vsl[vsl_offset], ldvsl, &work[1], &work[1], &
  657. c_n1, &ierr);
  658. /* Computing MAX */
  659. i__1 = lwkopt, i__2 = *n * 3 + (integer) work[1];
  660. lwkopt = f2cmax(i__1,i__2);
  661. }
  662. sgghd3_(jobvsl, jobvsr, n, &c__1, n, &a[a_offset], lda, &b[b_offset],
  663. ldb, &vsl[vsl_offset], ldvsl, &vsr[vsr_offset], ldvsr, &work[
  664. 1], &c_n1, &ierr);
  665. /* Computing MAX */
  666. i__1 = lwkopt, i__2 = *n * 3 + (integer) work[1];
  667. lwkopt = f2cmax(i__1,i__2);
  668. shgeqz_("S", jobvsl, jobvsr, n, &c__1, n, &a[a_offset], lda, &b[
  669. b_offset], ldb, &alphar[1], &alphai[1], &beta[1], &vsl[
  670. vsl_offset], ldvsl, &vsr[vsr_offset], ldvsr, &work[1], &c_n1,
  671. &ierr);
  672. /* Computing MAX */
  673. i__1 = lwkopt, i__2 = (*n << 1) + (integer) work[1];
  674. lwkopt = f2cmax(i__1,i__2);
  675. if (wantst) {
  676. stgsen_(&c__0, &ilvsl, &ilvsr, &bwork[1], n, &a[a_offset], lda, &
  677. b[b_offset], ldb, &alphar[1], &alphai[1], &beta[1], &vsl[
  678. vsl_offset], ldvsl, &vsr[vsr_offset], ldvsr, sdim, &pvsl,
  679. &pvsr, dif, &work[1], &c_n1, idum, &c__1, &ierr);
  680. /* Computing MAX */
  681. i__1 = lwkopt, i__2 = (*n << 1) + (integer) work[1];
  682. lwkopt = f2cmax(i__1,i__2);
  683. }
  684. work[1] = (real) lwkopt;
  685. }
  686. if (*info != 0) {
  687. i__1 = -(*info);
  688. xerbla_("SGGES3 ", &i__1, (ftnlen)6);
  689. return;
  690. } else if (lquery) {
  691. return;
  692. }
  693. /* Quick return if possible */
  694. if (*n == 0) {
  695. *sdim = 0;
  696. return;
  697. }
  698. /* Get machine constants */
  699. eps = slamch_("P");
  700. safmin = slamch_("S");
  701. safmax = 1.f / safmin;
  702. slabad_(&safmin, &safmax);
  703. smlnum = sqrt(safmin) / eps;
  704. bignum = 1.f / smlnum;
  705. /* Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
  706. anrm = slange_("M", n, n, &a[a_offset], lda, &work[1]);
  707. ilascl = FALSE_;
  708. if (anrm > 0.f && anrm < smlnum) {
  709. anrmto = smlnum;
  710. ilascl = TRUE_;
  711. } else if (anrm > bignum) {
  712. anrmto = bignum;
  713. ilascl = TRUE_;
  714. }
  715. if (ilascl) {
  716. slascl_("G", &c__0, &c__0, &anrm, &anrmto, n, n, &a[a_offset], lda, &
  717. ierr);
  718. }
  719. /* Scale B if f2cmax element outside range [SMLNUM,BIGNUM] */
  720. bnrm = slange_("M", n, n, &b[b_offset], ldb, &work[1]);
  721. ilbscl = FALSE_;
  722. if (bnrm > 0.f && bnrm < smlnum) {
  723. bnrmto = smlnum;
  724. ilbscl = TRUE_;
  725. } else if (bnrm > bignum) {
  726. bnrmto = bignum;
  727. ilbscl = TRUE_;
  728. }
  729. if (ilbscl) {
  730. slascl_("G", &c__0, &c__0, &bnrm, &bnrmto, n, n, &b[b_offset], ldb, &
  731. ierr);
  732. }
  733. /* Permute the matrix to make it more nearly triangular */
  734. ileft = 1;
  735. iright = *n + 1;
  736. iwrk = iright + *n;
  737. sggbal_("P", n, &a[a_offset], lda, &b[b_offset], ldb, &ilo, &ihi, &work[
  738. ileft], &work[iright], &work[iwrk], &ierr);
  739. /* Reduce B to triangular form (QR decomposition of B) */
  740. irows = ihi + 1 - ilo;
  741. icols = *n + 1 - ilo;
  742. itau = iwrk;
  743. iwrk = itau + irows;
  744. i__1 = *lwork + 1 - iwrk;
  745. sgeqrf_(&irows, &icols, &b[ilo + ilo * b_dim1], ldb, &work[itau], &work[
  746. iwrk], &i__1, &ierr);
  747. /* Apply the orthogonal transformation to matrix A */
  748. i__1 = *lwork + 1 - iwrk;
  749. sormqr_("L", "T", &irows, &icols, &irows, &b[ilo + ilo * b_dim1], ldb, &
  750. work[itau], &a[ilo + ilo * a_dim1], lda, &work[iwrk], &i__1, &
  751. ierr);
  752. /* Initialize VSL */
  753. if (ilvsl) {
  754. slaset_("Full", n, n, &c_b36, &c_b37, &vsl[vsl_offset], ldvsl);
  755. if (irows > 1) {
  756. i__1 = irows - 1;
  757. i__2 = irows - 1;
  758. slacpy_("L", &i__1, &i__2, &b[ilo + 1 + ilo * b_dim1], ldb, &vsl[
  759. ilo + 1 + ilo * vsl_dim1], ldvsl);
  760. }
  761. i__1 = *lwork + 1 - iwrk;
  762. sorgqr_(&irows, &irows, &irows, &vsl[ilo + ilo * vsl_dim1], ldvsl, &
  763. work[itau], &work[iwrk], &i__1, &ierr);
  764. }
  765. /* Initialize VSR */
  766. if (ilvsr) {
  767. slaset_("Full", n, n, &c_b36, &c_b37, &vsr[vsr_offset], ldvsr);
  768. }
  769. /* Reduce to generalized Hessenberg form */
  770. i__1 = *lwork + 1 - iwrk;
  771. sgghd3_(jobvsl, jobvsr, n, &ilo, &ihi, &a[a_offset], lda, &b[b_offset],
  772. ldb, &vsl[vsl_offset], ldvsl, &vsr[vsr_offset], ldvsr, &work[iwrk]
  773. , &i__1, &ierr);
  774. /* Perform QZ algorithm, computing Schur vectors if desired */
  775. iwrk = itau;
  776. i__1 = *lwork + 1 - iwrk;
  777. shgeqz_("S", jobvsl, jobvsr, n, &ilo, &ihi, &a[a_offset], lda, &b[
  778. b_offset], ldb, &alphar[1], &alphai[1], &beta[1], &vsl[vsl_offset]
  779. , ldvsl, &vsr[vsr_offset], ldvsr, &work[iwrk], &i__1, &ierr);
  780. if (ierr != 0) {
  781. if (ierr > 0 && ierr <= *n) {
  782. *info = ierr;
  783. } else if (ierr > *n && ierr <= *n << 1) {
  784. *info = ierr - *n;
  785. } else {
  786. *info = *n + 1;
  787. }
  788. goto L40;
  789. }
  790. /* Sort eigenvalues ALPHA/BETA if desired */
  791. *sdim = 0;
  792. if (wantst) {
  793. /* Undo scaling on eigenvalues before SELCTGing */
  794. if (ilascl) {
  795. slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphar[1],
  796. n, &ierr);
  797. slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphai[1],
  798. n, &ierr);
  799. }
  800. if (ilbscl) {
  801. slascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n,
  802. &ierr);
  803. }
  804. /* Select eigenvalues */
  805. i__1 = *n;
  806. for (i__ = 1; i__ <= i__1; ++i__) {
  807. bwork[i__] = (*selctg)(&alphar[i__], &alphai[i__], &beta[i__]);
  808. /* L10: */
  809. }
  810. i__1 = *lwork - iwrk + 1;
  811. stgsen_(&c__0, &ilvsl, &ilvsr, &bwork[1], n, &a[a_offset], lda, &b[
  812. b_offset], ldb, &alphar[1], &alphai[1], &beta[1], &vsl[
  813. vsl_offset], ldvsl, &vsr[vsr_offset], ldvsr, sdim, &pvsl, &
  814. pvsr, dif, &work[iwrk], &i__1, idum, &c__1, &ierr);
  815. if (ierr == 1) {
  816. *info = *n + 3;
  817. }
  818. }
  819. /* Apply back-permutation to VSL and VSR */
  820. if (ilvsl) {
  821. sggbak_("P", "L", n, &ilo, &ihi, &work[ileft], &work[iright], n, &vsl[
  822. vsl_offset], ldvsl, &ierr);
  823. }
  824. if (ilvsr) {
  825. sggbak_("P", "R", n, &ilo, &ihi, &work[ileft], &work[iright], n, &vsr[
  826. vsr_offset], ldvsr, &ierr);
  827. }
  828. /* Check if unscaling would cause over/underflow, if so, rescale */
  829. /* (ALPHAR(I),ALPHAI(I),BETA(I)) so BETA(I) is on the order of */
  830. /* B(I,I) and ALPHAR(I) and ALPHAI(I) are on the order of A(I,I) */
  831. if (ilascl) {
  832. i__1 = *n;
  833. for (i__ = 1; i__ <= i__1; ++i__) {
  834. if (alphai[i__] != 0.f) {
  835. if (alphar[i__] / safmax > anrmto / anrm || safmin / alphar[
  836. i__] > anrm / anrmto) {
  837. work[1] = (r__1 = a[i__ + i__ * a_dim1] / alphar[i__],
  838. abs(r__1));
  839. beta[i__] *= work[1];
  840. alphar[i__] *= work[1];
  841. alphai[i__] *= work[1];
  842. } else if (alphai[i__] / safmax > anrmto / anrm || safmin /
  843. alphai[i__] > anrm / anrmto) {
  844. work[1] = (r__1 = a[i__ + (i__ + 1) * a_dim1] / alphai[
  845. i__], abs(r__1));
  846. beta[i__] *= work[1];
  847. alphar[i__] *= work[1];
  848. alphai[i__] *= work[1];
  849. }
  850. }
  851. /* L50: */
  852. }
  853. }
  854. if (ilbscl) {
  855. i__1 = *n;
  856. for (i__ = 1; i__ <= i__1; ++i__) {
  857. if (alphai[i__] != 0.f) {
  858. if (beta[i__] / safmax > bnrmto / bnrm || safmin / beta[i__]
  859. > bnrm / bnrmto) {
  860. work[1] = (r__1 = b[i__ + i__ * b_dim1] / beta[i__], abs(
  861. r__1));
  862. beta[i__] *= work[1];
  863. alphar[i__] *= work[1];
  864. alphai[i__] *= work[1];
  865. }
  866. }
  867. /* L60: */
  868. }
  869. }
  870. /* Undo scaling */
  871. if (ilascl) {
  872. slascl_("H", &c__0, &c__0, &anrmto, &anrm, n, n, &a[a_offset], lda, &
  873. ierr);
  874. slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphar[1], n, &
  875. ierr);
  876. slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphai[1], n, &
  877. ierr);
  878. }
  879. if (ilbscl) {
  880. slascl_("U", &c__0, &c__0, &bnrmto, &bnrm, n, n, &b[b_offset], ldb, &
  881. ierr);
  882. slascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n, &
  883. ierr);
  884. }
  885. if (wantst) {
  886. /* Check if reordering is correct */
  887. lastsl = TRUE_;
  888. lst2sl = TRUE_;
  889. *sdim = 0;
  890. ip = 0;
  891. i__1 = *n;
  892. for (i__ = 1; i__ <= i__1; ++i__) {
  893. cursl = (*selctg)(&alphar[i__], &alphai[i__], &beta[i__]);
  894. if (alphai[i__] == 0.f) {
  895. if (cursl) {
  896. ++(*sdim);
  897. }
  898. ip = 0;
  899. if (cursl && ! lastsl) {
  900. *info = *n + 2;
  901. }
  902. } else {
  903. if (ip == 1) {
  904. /* Last eigenvalue of conjugate pair */
  905. cursl = cursl || lastsl;
  906. lastsl = cursl;
  907. if (cursl) {
  908. *sdim += 2;
  909. }
  910. ip = -1;
  911. if (cursl && ! lst2sl) {
  912. *info = *n + 2;
  913. }
  914. } else {
  915. /* First eigenvalue of conjugate pair */
  916. ip = 1;
  917. }
  918. }
  919. lst2sl = lastsl;
  920. lastsl = cursl;
  921. /* L30: */
  922. }
  923. }
  924. L40:
  925. work[1] = (real) lwkopt;
  926. return;
  927. /* End of SGGES3 */
  928. } /* sgges3_ */