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.

sgges.c 32 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. static real c_b38 = 0.f;
  245. static real c_b39 = 1.f;
  246. /* > \brief <b> SGGES computes the eigenvalues, the Schur form, and, optionally, the matrix of Schur vectors f
  247. or GE matrices</b> */
  248. /* =========== DOCUMENTATION =========== */
  249. /* Online html documentation available at */
  250. /* http://www.netlib.org/lapack/explore-html/ */
  251. /* > \htmlonly */
  252. /* > Download SGGES + dependencies */
  253. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgges.f
  254. "> */
  255. /* > [TGZ]</a> */
  256. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgges.f
  257. "> */
  258. /* > [ZIP]</a> */
  259. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgges.f
  260. "> */
  261. /* > [TXT]</a> */
  262. /* > \endhtmlonly */
  263. /* Definition: */
  264. /* =========== */
  265. /* SUBROUTINE SGGES( JOBVSL, JOBVSR, SORT, SELCTG, N, A, LDA, B, LDB, */
  266. /* SDIM, ALPHAR, ALPHAI, BETA, VSL, LDVSL, VSR, */
  267. /* 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. /* > SGGES 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. /* > If N = 0, LWORK >= 1, else LWORK >= f2cmax(8*N,6*N+16). */
  471. /* > For good performance , LWORK must generally be larger. */
  472. /* > */
  473. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  474. /* > only calculates the optimal size of the WORK array, returns */
  475. /* > this value as the first entry of the WORK array, and no error */
  476. /* > message related to LWORK is issued by XERBLA. */
  477. /* > \endverbatim */
  478. /* > */
  479. /* > \param[out] BWORK */
  480. /* > \verbatim */
  481. /* > BWORK is LOGICAL array, dimension (N) */
  482. /* > Not referenced if SORT = 'N'. */
  483. /* > \endverbatim */
  484. /* > */
  485. /* > \param[out] INFO */
  486. /* > \verbatim */
  487. /* > INFO is INTEGER */
  488. /* > = 0: successful exit */
  489. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  490. /* > = 1,...,N: */
  491. /* > The QZ iteration failed. (A,B) are not in Schur */
  492. /* > form, but ALPHAR(j), ALPHAI(j), and BETA(j) should */
  493. /* > be correct for j=INFO+1,...,N. */
  494. /* > > N: =N+1: other than QZ iteration failed in SHGEQZ. */
  495. /* > =N+2: after reordering, roundoff changed values of */
  496. /* > some complex eigenvalues so that leading */
  497. /* > eigenvalues in the Generalized Schur form no */
  498. /* > longer satisfy SELCTG=.TRUE. This could also */
  499. /* > be caused due to scaling. */
  500. /* > =N+3: reordering failed in STGSEN. */
  501. /* > \endverbatim */
  502. /* Authors: */
  503. /* ======== */
  504. /* > \author Univ. of Tennessee */
  505. /* > \author Univ. of California Berkeley */
  506. /* > \author Univ. of Colorado Denver */
  507. /* > \author NAG Ltd. */
  508. /* > \date December 2016 */
  509. /* > \ingroup realGEeigen */
  510. /* ===================================================================== */
  511. /* Subroutine */ void sgges_(char *jobvsl, char *jobvsr, char *sort, logical
  512. (*selctg)(real*,real*,real*), integer *n, real *a, integer *lda, real *b, integer *ldb,
  513. integer *sdim, real *alphar, real *alphai, real *beta, real *vsl,
  514. integer *ldvsl, real *vsr, integer *ldvsr, real *work, integer *lwork,
  515. logical *bwork, integer *info)
  516. {
  517. /* System generated locals */
  518. integer a_dim1, a_offset, b_dim1, b_offset, vsl_dim1, vsl_offset,
  519. vsr_dim1, vsr_offset, i__1, i__2;
  520. real r__1;
  521. /* Local variables */
  522. real anrm, bnrm;
  523. integer idum[1], ierr, itau, iwrk;
  524. real pvsl, pvsr;
  525. integer i__;
  526. extern logical lsame_(char *, char *);
  527. integer ileft, icols;
  528. logical cursl, ilvsl, ilvsr;
  529. integer irows;
  530. logical lst2sl;
  531. extern /* Subroutine */ void slabad_(real *, real *);
  532. integer ip;
  533. extern /* Subroutine */ void sggbak_(char *, char *, integer *, integer *,
  534. integer *, real *, real *, integer *, real *, integer *, integer *
  535. ), sggbal_(char *, integer *, real *, integer *,
  536. real *, integer *, integer *, integer *, real *, real *, real *,
  537. integer *);
  538. logical ilascl, ilbscl;
  539. extern real slamch_(char *), slange_(char *, integer *, integer *,
  540. real *, integer *, real *);
  541. real safmin;
  542. extern /* Subroutine */ void sgghrd_(char *, char *, integer *, integer *,
  543. integer *, real *, integer *, real *, integer *, real *, integer *
  544. , real *, integer *, integer *);
  545. real safmax;
  546. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  547. real bignum;
  548. extern /* Subroutine */ void slascl_(char *, integer *, integer *, real *,
  549. real *, integer *, integer *, real *, integer *, integer *);
  550. extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
  551. integer *, integer *, ftnlen, ftnlen);
  552. integer ijobvl, iright;
  553. extern /* Subroutine */ void sgeqrf_(integer *, integer *, real *, integer
  554. *, real *, real *, integer *, integer *);
  555. integer ijobvr;
  556. extern /* Subroutine */ void slacpy_(char *, integer *, integer *, real *,
  557. integer *, real *, integer *), slaset_(char *, integer *,
  558. integer *, real *, real *, real *, integer *);
  559. real anrmto, bnrmto;
  560. logical lastsl;
  561. extern /* Subroutine */ void shgeqz_(char *, char *, char *, integer *,
  562. integer *, integer *, real *, integer *, real *, integer *, real *
  563. , real *, real *, real *, integer *, real *, integer *, real *,
  564. integer *, integer *), stgsen_(integer *,
  565. logical *, logical *, logical *, integer *, real *, integer *,
  566. real *, integer *, real *, real *, real *, real *, integer *,
  567. real *, integer *, integer *, real *, real *, real *, real *,
  568. integer *, integer *, integer *, integer *);
  569. integer minwrk, maxwrk;
  570. real smlnum;
  571. extern /* Subroutine */ void sorgqr_(integer *, integer *, integer *, real
  572. *, integer *, real *, real *, integer *, integer *);
  573. logical wantst, lquery;
  574. extern /* Subroutine */ void sormqr_(char *, char *, integer *, integer *,
  575. integer *, real *, integer *, real *, real *, integer *, real *,
  576. integer *, integer *);
  577. real dif[2];
  578. integer ihi, ilo;
  579. real eps;
  580. /* -- LAPACK driver routine (version 3.7.0) -- */
  581. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  582. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  583. /* December 2016 */
  584. /* ===================================================================== */
  585. /* Decode the input arguments */
  586. /* Parameter adjustments */
  587. a_dim1 = *lda;
  588. a_offset = 1 + a_dim1 * 1;
  589. a -= a_offset;
  590. b_dim1 = *ldb;
  591. b_offset = 1 + b_dim1 * 1;
  592. b -= b_offset;
  593. --alphar;
  594. --alphai;
  595. --beta;
  596. vsl_dim1 = *ldvsl;
  597. vsl_offset = 1 + vsl_dim1 * 1;
  598. vsl -= vsl_offset;
  599. vsr_dim1 = *ldvsr;
  600. vsr_offset = 1 + vsr_dim1 * 1;
  601. vsr -= vsr_offset;
  602. --work;
  603. --bwork;
  604. /* Function Body */
  605. if (lsame_(jobvsl, "N")) {
  606. ijobvl = 1;
  607. ilvsl = FALSE_;
  608. } else if (lsame_(jobvsl, "V")) {
  609. ijobvl = 2;
  610. ilvsl = TRUE_;
  611. } else {
  612. ijobvl = -1;
  613. ilvsl = FALSE_;
  614. }
  615. if (lsame_(jobvsr, "N")) {
  616. ijobvr = 1;
  617. ilvsr = FALSE_;
  618. } else if (lsame_(jobvsr, "V")) {
  619. ijobvr = 2;
  620. ilvsr = TRUE_;
  621. } else {
  622. ijobvr = -1;
  623. ilvsr = FALSE_;
  624. }
  625. wantst = lsame_(sort, "S");
  626. /* Test the input arguments */
  627. *info = 0;
  628. lquery = *lwork == -1;
  629. if (ijobvl <= 0) {
  630. *info = -1;
  631. } else if (ijobvr <= 0) {
  632. *info = -2;
  633. } else if (! wantst && ! lsame_(sort, "N")) {
  634. *info = -3;
  635. } else if (*n < 0) {
  636. *info = -5;
  637. } else if (*lda < f2cmax(1,*n)) {
  638. *info = -7;
  639. } else if (*ldb < f2cmax(1,*n)) {
  640. *info = -9;
  641. } else if (*ldvsl < 1 || ilvsl && *ldvsl < *n) {
  642. *info = -15;
  643. } else if (*ldvsr < 1 || ilvsr && *ldvsr < *n) {
  644. *info = -17;
  645. }
  646. /* Compute workspace */
  647. /* (Note: Comments in the code beginning "Workspace:" describe the */
  648. /* minimal amount of workspace needed at that point in the code, */
  649. /* as well as the preferred amount for good performance. */
  650. /* NB refers to the optimal block size for the immediately */
  651. /* following subroutine, as returned by ILAENV.) */
  652. if (*info == 0) {
  653. if (*n > 0) {
  654. /* Computing MAX */
  655. i__1 = *n << 3, i__2 = *n * 6 + 16;
  656. minwrk = f2cmax(i__1,i__2);
  657. maxwrk = minwrk - *n + *n * ilaenv_(&c__1, "SGEQRF", " ", n, &
  658. c__1, n, &c__0, (ftnlen)6, (ftnlen)1);
  659. /* Computing MAX */
  660. i__1 = maxwrk, i__2 = minwrk - *n + *n * ilaenv_(&c__1, "SORMQR",
  661. " ", n, &c__1, n, &c_n1, (ftnlen)6, (ftnlen)1);
  662. maxwrk = f2cmax(i__1,i__2);
  663. if (ilvsl) {
  664. /* Computing MAX */
  665. i__1 = maxwrk, i__2 = minwrk - *n + *n * ilaenv_(&c__1, "SOR"
  666. "GQR", " ", n, &c__1, n, &c_n1, (ftnlen)6, (ftnlen)1);
  667. maxwrk = f2cmax(i__1,i__2);
  668. }
  669. } else {
  670. minwrk = 1;
  671. maxwrk = 1;
  672. }
  673. work[1] = (real) maxwrk;
  674. if (*lwork < minwrk && ! lquery) {
  675. *info = -19;
  676. }
  677. }
  678. if (*info != 0) {
  679. i__1 = -(*info);
  680. xerbla_("SGGES ", &i__1, (ftnlen)5);
  681. return;
  682. } else if (lquery) {
  683. return;
  684. }
  685. /* Quick return if possible */
  686. if (*n == 0) {
  687. *sdim = 0;
  688. return;
  689. }
  690. /* Get machine constants */
  691. eps = slamch_("P");
  692. safmin = slamch_("S");
  693. safmax = 1.f / safmin;
  694. slabad_(&safmin, &safmax);
  695. smlnum = sqrt(safmin) / eps;
  696. bignum = 1.f / smlnum;
  697. /* Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
  698. anrm = slange_("M", n, n, &a[a_offset], lda, &work[1]);
  699. ilascl = FALSE_;
  700. if (anrm > 0.f && anrm < smlnum) {
  701. anrmto = smlnum;
  702. ilascl = TRUE_;
  703. } else if (anrm > bignum) {
  704. anrmto = bignum;
  705. ilascl = TRUE_;
  706. }
  707. if (ilascl) {
  708. slascl_("G", &c__0, &c__0, &anrm, &anrmto, n, n, &a[a_offset], lda, &
  709. ierr);
  710. }
  711. /* Scale B if f2cmax element outside range [SMLNUM,BIGNUM] */
  712. bnrm = slange_("M", n, n, &b[b_offset], ldb, &work[1]);
  713. ilbscl = FALSE_;
  714. if (bnrm > 0.f && bnrm < smlnum) {
  715. bnrmto = smlnum;
  716. ilbscl = TRUE_;
  717. } else if (bnrm > bignum) {
  718. bnrmto = bignum;
  719. ilbscl = TRUE_;
  720. }
  721. if (ilbscl) {
  722. slascl_("G", &c__0, &c__0, &bnrm, &bnrmto, n, n, &b[b_offset], ldb, &
  723. ierr);
  724. }
  725. /* Permute the matrix to make it more nearly triangular */
  726. /* (Workspace: need 6*N + 2*N space for storing balancing factors) */
  727. ileft = 1;
  728. iright = *n + 1;
  729. iwrk = iright + *n;
  730. sggbal_("P", n, &a[a_offset], lda, &b[b_offset], ldb, &ilo, &ihi, &work[
  731. ileft], &work[iright], &work[iwrk], &ierr);
  732. /* Reduce B to triangular form (QR decomposition of B) */
  733. /* (Workspace: need N, prefer N*NB) */
  734. irows = ihi + 1 - ilo;
  735. icols = *n + 1 - ilo;
  736. itau = iwrk;
  737. iwrk = itau + irows;
  738. i__1 = *lwork + 1 - iwrk;
  739. sgeqrf_(&irows, &icols, &b[ilo + ilo * b_dim1], ldb, &work[itau], &work[
  740. iwrk], &i__1, &ierr);
  741. /* Apply the orthogonal transformation to matrix A */
  742. /* (Workspace: need N, prefer N*NB) */
  743. i__1 = *lwork + 1 - iwrk;
  744. sormqr_("L", "T", &irows, &icols, &irows, &b[ilo + ilo * b_dim1], ldb, &
  745. work[itau], &a[ilo + ilo * a_dim1], lda, &work[iwrk], &i__1, &
  746. ierr);
  747. /* Initialize VSL */
  748. /* (Workspace: need N, prefer N*NB) */
  749. if (ilvsl) {
  750. slaset_("Full", n, n, &c_b38, &c_b39, &vsl[vsl_offset], ldvsl);
  751. if (irows > 1) {
  752. i__1 = irows - 1;
  753. i__2 = irows - 1;
  754. slacpy_("L", &i__1, &i__2, &b[ilo + 1 + ilo * b_dim1], ldb, &vsl[
  755. ilo + 1 + ilo * vsl_dim1], ldvsl);
  756. }
  757. i__1 = *lwork + 1 - iwrk;
  758. sorgqr_(&irows, &irows, &irows, &vsl[ilo + ilo * vsl_dim1], ldvsl, &
  759. work[itau], &work[iwrk], &i__1, &ierr);
  760. }
  761. /* Initialize VSR */
  762. if (ilvsr) {
  763. slaset_("Full", n, n, &c_b38, &c_b39, &vsr[vsr_offset], ldvsr);
  764. }
  765. /* Reduce to generalized Hessenberg form */
  766. /* (Workspace: none needed) */
  767. sgghrd_(jobvsl, jobvsr, n, &ilo, &ihi, &a[a_offset], lda, &b[b_offset],
  768. ldb, &vsl[vsl_offset], ldvsl, &vsr[vsr_offset], ldvsr, &ierr);
  769. /* Perform QZ algorithm, computing Schur vectors if desired */
  770. /* (Workspace: need N) */
  771. iwrk = itau;
  772. i__1 = *lwork + 1 - iwrk;
  773. shgeqz_("S", jobvsl, jobvsr, n, &ilo, &ihi, &a[a_offset], lda, &b[
  774. b_offset], ldb, &alphar[1], &alphai[1], &beta[1], &vsl[vsl_offset]
  775. , ldvsl, &vsr[vsr_offset], ldvsr, &work[iwrk], &i__1, &ierr);
  776. if (ierr != 0) {
  777. if (ierr > 0 && ierr <= *n) {
  778. *info = ierr;
  779. } else if (ierr > *n && ierr <= *n << 1) {
  780. *info = ierr - *n;
  781. } else {
  782. *info = *n + 1;
  783. }
  784. goto L40;
  785. }
  786. /* Sort eigenvalues ALPHA/BETA if desired */
  787. /* (Workspace: need 4*N+16 ) */
  788. *sdim = 0;
  789. if (wantst) {
  790. /* Undo scaling on eigenvalues before SELCTGing */
  791. if (ilascl) {
  792. slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphar[1],
  793. n, &ierr);
  794. slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphai[1],
  795. n, &ierr);
  796. }
  797. if (ilbscl) {
  798. slascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n,
  799. &ierr);
  800. }
  801. /* Select eigenvalues */
  802. i__1 = *n;
  803. for (i__ = 1; i__ <= i__1; ++i__) {
  804. bwork[i__] = (*selctg)(&alphar[i__], &alphai[i__], &beta[i__]);
  805. /* L10: */
  806. }
  807. i__1 = *lwork - iwrk + 1;
  808. stgsen_(&c__0, &ilvsl, &ilvsr, &bwork[1], n, &a[a_offset], lda, &b[
  809. b_offset], ldb, &alphar[1], &alphai[1], &beta[1], &vsl[
  810. vsl_offset], ldvsl, &vsr[vsr_offset], ldvsr, sdim, &pvsl, &
  811. pvsr, dif, &work[iwrk], &i__1, idum, &c__1, &ierr);
  812. if (ierr == 1) {
  813. *info = *n + 3;
  814. }
  815. }
  816. /* Apply back-permutation to VSL and VSR */
  817. /* (Workspace: none needed) */
  818. if (ilvsl) {
  819. sggbak_("P", "L", n, &ilo, &ihi, &work[ileft], &work[iright], n, &vsl[
  820. vsl_offset], ldvsl, &ierr);
  821. }
  822. if (ilvsr) {
  823. sggbak_("P", "R", n, &ilo, &ihi, &work[ileft], &work[iright], n, &vsr[
  824. vsr_offset], ldvsr, &ierr);
  825. }
  826. /* Check if unscaling would cause over/underflow, if so, rescale */
  827. /* (ALPHAR(I),ALPHAI(I),BETA(I)) so BETA(I) is on the order of */
  828. /* B(I,I) and ALPHAR(I) and ALPHAI(I) are on the order of A(I,I) */
  829. if (ilascl) {
  830. i__1 = *n;
  831. for (i__ = 1; i__ <= i__1; ++i__) {
  832. if (alphai[i__] != 0.f) {
  833. if (alphar[i__] / safmax > anrmto / anrm || safmin / alphar[
  834. i__] > anrm / anrmto) {
  835. work[1] = (r__1 = a[i__ + i__ * a_dim1] / alphar[i__],
  836. abs(r__1));
  837. beta[i__] *= work[1];
  838. alphar[i__] *= work[1];
  839. alphai[i__] *= work[1];
  840. } else if (alphai[i__] / safmax > anrmto / anrm || safmin /
  841. alphai[i__] > anrm / anrmto) {
  842. work[1] = (r__1 = a[i__ + (i__ + 1) * a_dim1] / alphai[
  843. i__], abs(r__1));
  844. beta[i__] *= work[1];
  845. alphar[i__] *= work[1];
  846. alphai[i__] *= work[1];
  847. }
  848. }
  849. /* L50: */
  850. }
  851. }
  852. if (ilbscl) {
  853. i__1 = *n;
  854. for (i__ = 1; i__ <= i__1; ++i__) {
  855. if (alphai[i__] != 0.f) {
  856. if (beta[i__] / safmax > bnrmto / bnrm || safmin / beta[i__]
  857. > bnrm / bnrmto) {
  858. work[1] = (r__1 = b[i__ + i__ * b_dim1] / beta[i__], abs(
  859. r__1));
  860. beta[i__] *= work[1];
  861. alphar[i__] *= work[1];
  862. alphai[i__] *= work[1];
  863. }
  864. }
  865. /* L60: */
  866. }
  867. }
  868. /* Undo scaling */
  869. if (ilascl) {
  870. slascl_("H", &c__0, &c__0, &anrmto, &anrm, n, n, &a[a_offset], lda, &
  871. ierr);
  872. slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphar[1], n, &
  873. ierr);
  874. slascl_("G", &c__0, &c__0, &anrmto, &anrm, n, &c__1, &alphai[1], n, &
  875. ierr);
  876. }
  877. if (ilbscl) {
  878. slascl_("U", &c__0, &c__0, &bnrmto, &bnrm, n, n, &b[b_offset], ldb, &
  879. ierr);
  880. slascl_("G", &c__0, &c__0, &bnrmto, &bnrm, n, &c__1, &beta[1], n, &
  881. ierr);
  882. }
  883. if (wantst) {
  884. /* Check if reordering is correct */
  885. lastsl = TRUE_;
  886. lst2sl = TRUE_;
  887. *sdim = 0;
  888. ip = 0;
  889. i__1 = *n;
  890. for (i__ = 1; i__ <= i__1; ++i__) {
  891. cursl = (*selctg)(&alphar[i__], &alphai[i__], &beta[i__]);
  892. if (alphai[i__] == 0.f) {
  893. if (cursl) {
  894. ++(*sdim);
  895. }
  896. ip = 0;
  897. if (cursl && ! lastsl) {
  898. *info = *n + 2;
  899. }
  900. } else {
  901. if (ip == 1) {
  902. /* Last eigenvalue of conjugate pair */
  903. cursl = cursl || lastsl;
  904. lastsl = cursl;
  905. if (cursl) {
  906. *sdim += 2;
  907. }
  908. ip = -1;
  909. if (cursl && ! lst2sl) {
  910. *info = *n + 2;
  911. }
  912. } else {
  913. /* First eigenvalue of conjugate pair */
  914. ip = 1;
  915. }
  916. }
  917. lst2sl = lastsl;
  918. lastsl = cursl;
  919. /* L30: */
  920. }
  921. }
  922. L40:
  923. work[1] = (real) maxwrk;
  924. return;
  925. /* End of SGGES */
  926. } /* sgges_ */