dhgeqz.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. /* dhgeqz.f -- translated by f2c (version 20061008).
  2. You must link the resulting object file with libf2c:
  3. on Microsoft Windows system, link with libf2c.lib;
  4. on Linux or Unix systems, link with .../path/to/libf2c.a -lm
  5. or, if you install libf2c.a in a standard place, with -lf2c -lm
  6. -- in that order, at the end of the command line, as in
  7. cc *.o -lf2c -lm
  8. Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
  9. http://www.netlib.org/f2c/libf2c.zip
  10. */
  11. #include "f2c.h"
  12. #include "blaswrap.h"
  13. /* Table of constant values */
  14. static doublereal c_b12 = 0.;
  15. static doublereal c_b13 = 1.;
  16. static integer c__1 = 1;
  17. static integer c__3 = 3;
  18. /* Subroutine */ int dhgeqz_(char *job, char *compq, char *compz, integer *n,
  19. integer *ilo, integer *ihi, doublereal *h__, integer *ldh, doublereal
  20. *t, integer *ldt, doublereal *alphar, doublereal *alphai, doublereal *
  21. beta, doublereal *q, integer *ldq, doublereal *z__, integer *ldz,
  22. doublereal *work, integer *lwork, integer *info)
  23. {
  24. /* System generated locals */
  25. integer h_dim1, h_offset, q_dim1, q_offset, t_dim1, t_offset, z_dim1,
  26. z_offset, i__1, i__2, i__3, i__4;
  27. doublereal d__1, d__2, d__3, d__4;
  28. /* Builtin functions */
  29. double sqrt(doublereal);
  30. /* Local variables */
  31. doublereal c__;
  32. integer j;
  33. doublereal s, v[3], s1, s2, t1, u1, u2, a11, a12, a21, a22, b11, b22, c12,
  34. c21;
  35. integer jc;
  36. doublereal an, bn, cl, cq, cr;
  37. integer in;
  38. doublereal u12, w11, w12, w21;
  39. integer jr;
  40. doublereal cz, w22, sl, wi, sr, vs, wr, b1a, b2a, a1i, a2i, b1i, b2i, a1r,
  41. a2r, b1r, b2r, wr2, ad11, ad12, ad21, ad22, c11i, c22i;
  42. integer jch;
  43. doublereal c11r, c22r;
  44. logical ilq;
  45. doublereal u12l, tau, sqi;
  46. logical ilz;
  47. doublereal ulp, sqr, szi, szr, ad11l, ad12l, ad21l, ad22l, ad32l, wabs,
  48. atol, btol, temp;
  49. extern /* Subroutine */ int drot_(integer *, doublereal *, integer *,
  50. doublereal *, integer *, doublereal *, doublereal *), dlag2_(
  51. doublereal *, integer *, doublereal *, integer *, doublereal *,
  52. doublereal *, doublereal *, doublereal *, doublereal *,
  53. doublereal *);
  54. doublereal temp2, s1inv, scale;
  55. extern logical lsame_(char *, char *);
  56. integer iiter, ilast, jiter;
  57. doublereal anorm, bnorm;
  58. integer maxit;
  59. doublereal tempi, tempr;
  60. extern doublereal dlapy2_(doublereal *, doublereal *), dlapy3_(doublereal
  61. *, doublereal *, doublereal *);
  62. extern /* Subroutine */ int dlasv2_(doublereal *, doublereal *,
  63. doublereal *, doublereal *, doublereal *, doublereal *,
  64. doublereal *, doublereal *, doublereal *);
  65. logical ilazr2;
  66. doublereal ascale, bscale;
  67. extern doublereal dlamch_(char *);
  68. extern /* Subroutine */ int dlarfg_(integer *, doublereal *, doublereal *,
  69. integer *, doublereal *);
  70. extern doublereal dlanhs_(char *, integer *, doublereal *, integer *,
  71. doublereal *);
  72. extern /* Subroutine */ int dlaset_(char *, integer *, integer *,
  73. doublereal *, doublereal *, doublereal *, integer *);
  74. doublereal safmin;
  75. extern /* Subroutine */ int dlartg_(doublereal *, doublereal *,
  76. doublereal *, doublereal *, doublereal *);
  77. doublereal safmax;
  78. extern /* Subroutine */ int xerbla_(char *, integer *);
  79. doublereal eshift;
  80. logical ilschr;
  81. integer icompq, ilastm, ischur;
  82. logical ilazro;
  83. integer icompz, ifirst, ifrstm, istart;
  84. logical ilpivt, lquery;
  85. /* -- LAPACK routine (version 3.2.1) -- */
  86. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  87. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  88. /* -- April 2009 -- */
  89. /* .. Scalar Arguments .. */
  90. /* .. */
  91. /* .. Array Arguments .. */
  92. /* .. */
  93. /* Purpose */
  94. /* ======= */
  95. /* DHGEQZ computes the eigenvalues of a real matrix pair (H,T), */
  96. /* where H is an upper Hessenberg matrix and T is upper triangular, */
  97. /* using the double-shift QZ method. */
  98. /* Matrix pairs of this type are produced by the reduction to */
  99. /* generalized upper Hessenberg form of a real matrix pair (A,B): */
  100. /* A = Q1*H*Z1**T, B = Q1*T*Z1**T, */
  101. /* as computed by DGGHRD. */
  102. /* If JOB='S', then the Hessenberg-triangular pair (H,T) is */
  103. /* also reduced to generalized Schur form, */
  104. /* H = Q*S*Z**T, T = Q*P*Z**T, */
  105. /* where Q and Z are orthogonal matrices, P is an upper triangular */
  106. /* matrix, and S is a quasi-triangular matrix with 1-by-1 and 2-by-2 */
  107. /* diagonal blocks. */
  108. /* The 1-by-1 blocks correspond to real eigenvalues of the matrix pair */
  109. /* (H,T) and the 2-by-2 blocks correspond to complex conjugate pairs of */
  110. /* eigenvalues. */
  111. /* Additionally, the 2-by-2 upper triangular diagonal blocks of P */
  112. /* corresponding to 2-by-2 blocks of S are reduced to positive diagonal */
  113. /* form, i.e., if S(j+1,j) is non-zero, then P(j+1,j) = P(j,j+1) = 0, */
  114. /* P(j,j) > 0, and P(j+1,j+1) > 0. */
  115. /* Optionally, the orthogonal matrix Q from the generalized Schur */
  116. /* factorization may be postmultiplied into an input matrix Q1, and the */
  117. /* orthogonal matrix Z may be postmultiplied into an input matrix Z1. */
  118. /* If Q1 and Z1 are the orthogonal matrices from DGGHRD that reduced */
  119. /* the matrix pair (A,B) to generalized upper Hessenberg form, then the */
  120. /* output matrices Q1*Q and Z1*Z are the orthogonal factors from the */
  121. /* generalized Schur factorization of (A,B): */
  122. /* A = (Q1*Q)*S*(Z1*Z)**T, B = (Q1*Q)*P*(Z1*Z)**T. */
  123. /* To avoid overflow, eigenvalues of the matrix pair (H,T) (equivalently, */
  124. /* of (A,B)) are computed as a pair of values (alpha,beta), where alpha is */
  125. /* complex and beta real. */
  126. /* If beta is nonzero, lambda = alpha / beta is an eigenvalue of the */
  127. /* generalized nonsymmetric eigenvalue problem (GNEP) */
  128. /* A*x = lambda*B*x */
  129. /* and if alpha is nonzero, mu = beta / alpha is an eigenvalue of the */
  130. /* alternate form of the GNEP */
  131. /* mu*A*y = B*y. */
  132. /* Real eigenvalues can be read directly from the generalized Schur */
  133. /* form: */
  134. /* alpha = S(i,i), beta = P(i,i). */
  135. /* Ref: C.B. Moler & G.W. Stewart, "An Algorithm for Generalized Matrix */
  136. /* Eigenvalue Problems", SIAM J. Numer. Anal., 10(1973), */
  137. /* pp. 241--256. */
  138. /* Arguments */
  139. /* ========= */
  140. /* JOB (input) CHARACTER*1 */
  141. /* = 'E': Compute eigenvalues only; */
  142. /* = 'S': Compute eigenvalues and the Schur form. */
  143. /* COMPQ (input) CHARACTER*1 */
  144. /* = 'N': Left Schur vectors (Q) are not computed; */
  145. /* = 'I': Q is initialized to the unit matrix and the matrix Q */
  146. /* of left Schur vectors of (H,T) is returned; */
  147. /* = 'V': Q must contain an orthogonal matrix Q1 on entry and */
  148. /* the product Q1*Q is returned. */
  149. /* COMPZ (input) CHARACTER*1 */
  150. /* = 'N': Right Schur vectors (Z) are not computed; */
  151. /* = 'I': Z is initialized to the unit matrix and the matrix Z */
  152. /* of right Schur vectors of (H,T) is returned; */
  153. /* = 'V': Z must contain an orthogonal matrix Z1 on entry and */
  154. /* the product Z1*Z is returned. */
  155. /* N (input) INTEGER */
  156. /* The order of the matrices H, T, Q, and Z. N >= 0. */
  157. /* ILO (input) INTEGER */
  158. /* IHI (input) INTEGER */
  159. /* ILO and IHI mark the rows and columns of H which are in */
  160. /* Hessenberg form. It is assumed that A is already upper */
  161. /* triangular in rows and columns 1:ILO-1 and IHI+1:N. */
  162. /* If N > 0, 1 <= ILO <= IHI <= N; if N = 0, ILO=1 and IHI=0. */
  163. /* H (input/output) DOUBLE PRECISION array, dimension (LDH, N) */
  164. /* On entry, the N-by-N upper Hessenberg matrix H. */
  165. /* On exit, if JOB = 'S', H contains the upper quasi-triangular */
  166. /* matrix S from the generalized Schur factorization; */
  167. /* 2-by-2 diagonal blocks (corresponding to complex conjugate */
  168. /* pairs of eigenvalues) are returned in standard form, with */
  169. /* H(i,i) = H(i+1,i+1) and H(i+1,i)*H(i,i+1) < 0. */
  170. /* If JOB = 'E', the diagonal blocks of H match those of S, but */
  171. /* the rest of H is unspecified. */
  172. /* LDH (input) INTEGER */
  173. /* The leading dimension of the array H. LDH >= max( 1, N ). */
  174. /* T (input/output) DOUBLE PRECISION array, dimension (LDT, N) */
  175. /* On entry, the N-by-N upper triangular matrix T. */
  176. /* On exit, if JOB = 'S', T contains the upper triangular */
  177. /* matrix P from the generalized Schur factorization; */
  178. /* 2-by-2 diagonal blocks of P corresponding to 2-by-2 blocks of S */
  179. /* are reduced to positive diagonal form, i.e., if H(j+1,j) is */
  180. /* non-zero, then T(j+1,j) = T(j,j+1) = 0, T(j,j) > 0, and */
  181. /* T(j+1,j+1) > 0. */
  182. /* If JOB = 'E', the diagonal blocks of T match those of P, but */
  183. /* the rest of T is unspecified. */
  184. /* LDT (input) INTEGER */
  185. /* The leading dimension of the array T. LDT >= max( 1, N ). */
  186. /* ALPHAR (output) DOUBLE PRECISION array, dimension (N) */
  187. /* The real parts of each scalar alpha defining an eigenvalue */
  188. /* of GNEP. */
  189. /* ALPHAI (output) DOUBLE PRECISION array, dimension (N) */
  190. /* The imaginary parts of each scalar alpha defining an */
  191. /* eigenvalue of GNEP. */
  192. /* If ALPHAI(j) is zero, then the j-th eigenvalue is real; if */
  193. /* positive, then the j-th and (j+1)-st eigenvalues are a */
  194. /* complex conjugate pair, with ALPHAI(j+1) = -ALPHAI(j). */
  195. /* BETA (output) DOUBLE PRECISION array, dimension (N) */
  196. /* The scalars beta that define the eigenvalues of GNEP. */
  197. /* Together, the quantities alpha = (ALPHAR(j),ALPHAI(j)) and */
  198. /* beta = BETA(j) represent the j-th eigenvalue of the matrix */
  199. /* pair (A,B), in one of the forms lambda = alpha/beta or */
  200. /* mu = beta/alpha. Since either lambda or mu may overflow, */
  201. /* they should not, in general, be computed. */
  202. /* Q (input/output) DOUBLE PRECISION array, dimension (LDQ, N) */
  203. /* On entry, if COMPZ = 'V', the orthogonal matrix Q1 used in */
  204. /* the reduction of (A,B) to generalized Hessenberg form. */
  205. /* On exit, if COMPZ = 'I', the orthogonal matrix of left Schur */
  206. /* vectors of (H,T), and if COMPZ = 'V', the orthogonal matrix */
  207. /* of left Schur vectors of (A,B). */
  208. /* Not referenced if COMPZ = 'N'. */
  209. /* LDQ (input) INTEGER */
  210. /* The leading dimension of the array Q. LDQ >= 1. */
  211. /* If COMPQ='V' or 'I', then LDQ >= N. */
  212. /* Z (input/output) DOUBLE PRECISION array, dimension (LDZ, N) */
  213. /* On entry, if COMPZ = 'V', the orthogonal matrix Z1 used in */
  214. /* the reduction of (A,B) to generalized Hessenberg form. */
  215. /* On exit, if COMPZ = 'I', the orthogonal matrix of */
  216. /* right Schur vectors of (H,T), and if COMPZ = 'V', the */
  217. /* orthogonal matrix of right Schur vectors of (A,B). */
  218. /* Not referenced if COMPZ = 'N'. */
  219. /* LDZ (input) INTEGER */
  220. /* The leading dimension of the array Z. LDZ >= 1. */
  221. /* If COMPZ='V' or 'I', then LDZ >= N. */
  222. /* WORK (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
  223. /* On exit, if INFO >= 0, WORK(1) returns the optimal LWORK. */
  224. /* LWORK (input) INTEGER */
  225. /* The dimension of the array WORK. LWORK >= max(1,N). */
  226. /* If LWORK = -1, then a workspace query is assumed; the routine */
  227. /* only calculates the optimal size of the WORK array, returns */
  228. /* this value as the first entry of the WORK array, and no error */
  229. /* message related to LWORK is issued by XERBLA. */
  230. /* INFO (output) INTEGER */
  231. /* = 0: successful exit */
  232. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  233. /* = 1,...,N: the QZ iteration did not converge. (H,T) is not */
  234. /* in Schur form, but ALPHAR(i), ALPHAI(i), and */
  235. /* BETA(i), i=INFO+1,...,N should be correct. */
  236. /* = N+1,...,2*N: the shift calculation failed. (H,T) is not */
  237. /* in Schur form, but ALPHAR(i), ALPHAI(i), and */
  238. /* BETA(i), i=INFO-N+1,...,N should be correct. */
  239. /* Further Details */
  240. /* =============== */
  241. /* Iteration counters: */
  242. /* JITER -- counts iterations. */
  243. /* IITER -- counts iterations run since ILAST was last */
  244. /* changed. This is therefore reset only when a 1-by-1 or */
  245. /* 2-by-2 block deflates off the bottom. */
  246. /* ===================================================================== */
  247. /* .. Parameters .. */
  248. /* $ SAFETY = 1.0E+0 ) */
  249. /* .. */
  250. /* .. Local Scalars .. */
  251. /* .. */
  252. /* .. Local Arrays .. */
  253. /* .. */
  254. /* .. External Functions .. */
  255. /* .. */
  256. /* .. External Subroutines .. */
  257. /* .. */
  258. /* .. Intrinsic Functions .. */
  259. /* .. */
  260. /* .. Executable Statements .. */
  261. /* Decode JOB, COMPQ, COMPZ */
  262. /* Parameter adjustments */
  263. h_dim1 = *ldh;
  264. h_offset = 1 + h_dim1;
  265. h__ -= h_offset;
  266. t_dim1 = *ldt;
  267. t_offset = 1 + t_dim1;
  268. t -= t_offset;
  269. --alphar;
  270. --alphai;
  271. --beta;
  272. q_dim1 = *ldq;
  273. q_offset = 1 + q_dim1;
  274. q -= q_offset;
  275. z_dim1 = *ldz;
  276. z_offset = 1 + z_dim1;
  277. z__ -= z_offset;
  278. --work;
  279. /* Function Body */
  280. if (lsame_(job, "E")) {
  281. ilschr = FALSE_;
  282. ischur = 1;
  283. } else if (lsame_(job, "S")) {
  284. ilschr = TRUE_;
  285. ischur = 2;
  286. } else {
  287. ischur = 0;
  288. }
  289. if (lsame_(compq, "N")) {
  290. ilq = FALSE_;
  291. icompq = 1;
  292. } else if (lsame_(compq, "V")) {
  293. ilq = TRUE_;
  294. icompq = 2;
  295. } else if (lsame_(compq, "I")) {
  296. ilq = TRUE_;
  297. icompq = 3;
  298. } else {
  299. icompq = 0;
  300. }
  301. if (lsame_(compz, "N")) {
  302. ilz = FALSE_;
  303. icompz = 1;
  304. } else if (lsame_(compz, "V")) {
  305. ilz = TRUE_;
  306. icompz = 2;
  307. } else if (lsame_(compz, "I")) {
  308. ilz = TRUE_;
  309. icompz = 3;
  310. } else {
  311. icompz = 0;
  312. }
  313. /* Check Argument Values */
  314. *info = 0;
  315. work[1] = (doublereal) max(1,*n);
  316. lquery = *lwork == -1;
  317. if (ischur == 0) {
  318. *info = -1;
  319. } else if (icompq == 0) {
  320. *info = -2;
  321. } else if (icompz == 0) {
  322. *info = -3;
  323. } else if (*n < 0) {
  324. *info = -4;
  325. } else if (*ilo < 1) {
  326. *info = -5;
  327. } else if (*ihi > *n || *ihi < *ilo - 1) {
  328. *info = -6;
  329. } else if (*ldh < *n) {
  330. *info = -8;
  331. } else if (*ldt < *n) {
  332. *info = -10;
  333. } else if (*ldq < 1 || ilq && *ldq < *n) {
  334. *info = -15;
  335. } else if (*ldz < 1 || ilz && *ldz < *n) {
  336. *info = -17;
  337. } else if (*lwork < max(1,*n) && ! lquery) {
  338. *info = -19;
  339. }
  340. if (*info != 0) {
  341. i__1 = -(*info);
  342. xerbla_("DHGEQZ", &i__1);
  343. return 0;
  344. } else if (lquery) {
  345. return 0;
  346. }
  347. /* Quick return if possible */
  348. if (*n <= 0) {
  349. work[1] = 1.;
  350. return 0;
  351. }
  352. /* Initialize Q and Z */
  353. if (icompq == 3) {
  354. dlaset_("Full", n, n, &c_b12, &c_b13, &q[q_offset], ldq);
  355. }
  356. if (icompz == 3) {
  357. dlaset_("Full", n, n, &c_b12, &c_b13, &z__[z_offset], ldz);
  358. }
  359. /* Machine Constants */
  360. in = *ihi + 1 - *ilo;
  361. safmin = dlamch_("S");
  362. safmax = 1. / safmin;
  363. ulp = dlamch_("E") * dlamch_("B");
  364. anorm = dlanhs_("F", &in, &h__[*ilo + *ilo * h_dim1], ldh, &work[1]);
  365. bnorm = dlanhs_("F", &in, &t[*ilo + *ilo * t_dim1], ldt, &work[1]);
  366. /* Computing MAX */
  367. d__1 = safmin, d__2 = ulp * anorm;
  368. atol = max(d__1,d__2);
  369. /* Computing MAX */
  370. d__1 = safmin, d__2 = ulp * bnorm;
  371. btol = max(d__1,d__2);
  372. ascale = 1. / max(safmin,anorm);
  373. bscale = 1. / max(safmin,bnorm);
  374. /* Set Eigenvalues IHI+1:N */
  375. i__1 = *n;
  376. for (j = *ihi + 1; j <= i__1; ++j) {
  377. if (t[j + j * t_dim1] < 0.) {
  378. if (ilschr) {
  379. i__2 = j;
  380. for (jr = 1; jr <= i__2; ++jr) {
  381. h__[jr + j * h_dim1] = -h__[jr + j * h_dim1];
  382. t[jr + j * t_dim1] = -t[jr + j * t_dim1];
  383. /* L10: */
  384. }
  385. } else {
  386. h__[j + j * h_dim1] = -h__[j + j * h_dim1];
  387. t[j + j * t_dim1] = -t[j + j * t_dim1];
  388. }
  389. if (ilz) {
  390. i__2 = *n;
  391. for (jr = 1; jr <= i__2; ++jr) {
  392. z__[jr + j * z_dim1] = -z__[jr + j * z_dim1];
  393. /* L20: */
  394. }
  395. }
  396. }
  397. alphar[j] = h__[j + j * h_dim1];
  398. alphai[j] = 0.;
  399. beta[j] = t[j + j * t_dim1];
  400. /* L30: */
  401. }
  402. /* If IHI < ILO, skip QZ steps */
  403. if (*ihi < *ilo) {
  404. goto L380;
  405. }
  406. /* MAIN QZ ITERATION LOOP */
  407. /* Initialize dynamic indices */
  408. /* Eigenvalues ILAST+1:N have been found. */
  409. /* Column operations modify rows IFRSTM:whatever. */
  410. /* Row operations modify columns whatever:ILASTM. */
  411. /* If only eigenvalues are being computed, then */
  412. /* IFRSTM is the row of the last splitting row above row ILAST; */
  413. /* this is always at least ILO. */
  414. /* IITER counts iterations since the last eigenvalue was found, */
  415. /* to tell when to use an extraordinary shift. */
  416. /* MAXIT is the maximum number of QZ sweeps allowed. */
  417. ilast = *ihi;
  418. if (ilschr) {
  419. ifrstm = 1;
  420. ilastm = *n;
  421. } else {
  422. ifrstm = *ilo;
  423. ilastm = *ihi;
  424. }
  425. iiter = 0;
  426. eshift = 0.;
  427. maxit = (*ihi - *ilo + 1) * 30;
  428. i__1 = maxit;
  429. for (jiter = 1; jiter <= i__1; ++jiter) {
  430. /* Split the matrix if possible. */
  431. /* Two tests: */
  432. /* 1: H(j,j-1)=0 or j=ILO */
  433. /* 2: T(j,j)=0 */
  434. if (ilast == *ilo) {
  435. /* Special case: j=ILAST */
  436. goto L80;
  437. } else {
  438. if ((d__1 = h__[ilast + (ilast - 1) * h_dim1], abs(d__1)) <= atol)
  439. {
  440. h__[ilast + (ilast - 1) * h_dim1] = 0.;
  441. goto L80;
  442. }
  443. }
  444. if ((d__1 = t[ilast + ilast * t_dim1], abs(d__1)) <= btol) {
  445. t[ilast + ilast * t_dim1] = 0.;
  446. goto L70;
  447. }
  448. /* General case: j<ILAST */
  449. i__2 = *ilo;
  450. for (j = ilast - 1; j >= i__2; --j) {
  451. /* Test 1: for H(j,j-1)=0 or j=ILO */
  452. if (j == *ilo) {
  453. ilazro = TRUE_;
  454. } else {
  455. if ((d__1 = h__[j + (j - 1) * h_dim1], abs(d__1)) <= atol) {
  456. h__[j + (j - 1) * h_dim1] = 0.;
  457. ilazro = TRUE_;
  458. } else {
  459. ilazro = FALSE_;
  460. }
  461. }
  462. /* Test 2: for T(j,j)=0 */
  463. if ((d__1 = t[j + j * t_dim1], abs(d__1)) < btol) {
  464. t[j + j * t_dim1] = 0.;
  465. /* Test 1a: Check for 2 consecutive small subdiagonals in A */
  466. ilazr2 = FALSE_;
  467. if (! ilazro) {
  468. temp = (d__1 = h__[j + (j - 1) * h_dim1], abs(d__1));
  469. temp2 = (d__1 = h__[j + j * h_dim1], abs(d__1));
  470. tempr = max(temp,temp2);
  471. if (tempr < 1. && tempr != 0.) {
  472. temp /= tempr;
  473. temp2 /= tempr;
  474. }
  475. if (temp * (ascale * (d__1 = h__[j + 1 + j * h_dim1], abs(
  476. d__1))) <= temp2 * (ascale * atol)) {
  477. ilazr2 = TRUE_;
  478. }
  479. }
  480. /* If both tests pass (1 & 2), i.e., the leading diagonal */
  481. /* element of B in the block is zero, split a 1x1 block off */
  482. /* at the top. (I.e., at the J-th row/column) The leading */
  483. /* diagonal element of the remainder can also be zero, so */
  484. /* this may have to be done repeatedly. */
  485. if (ilazro || ilazr2) {
  486. i__3 = ilast - 1;
  487. for (jch = j; jch <= i__3; ++jch) {
  488. temp = h__[jch + jch * h_dim1];
  489. dlartg_(&temp, &h__[jch + 1 + jch * h_dim1], &c__, &s,
  490. &h__[jch + jch * h_dim1]);
  491. h__[jch + 1 + jch * h_dim1] = 0.;
  492. i__4 = ilastm - jch;
  493. drot_(&i__4, &h__[jch + (jch + 1) * h_dim1], ldh, &
  494. h__[jch + 1 + (jch + 1) * h_dim1], ldh, &c__,
  495. &s);
  496. i__4 = ilastm - jch;
  497. drot_(&i__4, &t[jch + (jch + 1) * t_dim1], ldt, &t[
  498. jch + 1 + (jch + 1) * t_dim1], ldt, &c__, &s);
  499. if (ilq) {
  500. drot_(n, &q[jch * q_dim1 + 1], &c__1, &q[(jch + 1)
  501. * q_dim1 + 1], &c__1, &c__, &s);
  502. }
  503. if (ilazr2) {
  504. h__[jch + (jch - 1) * h_dim1] *= c__;
  505. }
  506. ilazr2 = FALSE_;
  507. if ((d__1 = t[jch + 1 + (jch + 1) * t_dim1], abs(d__1)
  508. ) >= btol) {
  509. if (jch + 1 >= ilast) {
  510. goto L80;
  511. } else {
  512. ifirst = jch + 1;
  513. goto L110;
  514. }
  515. }
  516. t[jch + 1 + (jch + 1) * t_dim1] = 0.;
  517. /* L40: */
  518. }
  519. goto L70;
  520. } else {
  521. /* Only test 2 passed -- chase the zero to T(ILAST,ILAST) */
  522. /* Then process as in the case T(ILAST,ILAST)=0 */
  523. i__3 = ilast - 1;
  524. for (jch = j; jch <= i__3; ++jch) {
  525. temp = t[jch + (jch + 1) * t_dim1];
  526. dlartg_(&temp, &t[jch + 1 + (jch + 1) * t_dim1], &c__,
  527. &s, &t[jch + (jch + 1) * t_dim1]);
  528. t[jch + 1 + (jch + 1) * t_dim1] = 0.;
  529. if (jch < ilastm - 1) {
  530. i__4 = ilastm - jch - 1;
  531. drot_(&i__4, &t[jch + (jch + 2) * t_dim1], ldt, &
  532. t[jch + 1 + (jch + 2) * t_dim1], ldt, &
  533. c__, &s);
  534. }
  535. i__4 = ilastm - jch + 2;
  536. drot_(&i__4, &h__[jch + (jch - 1) * h_dim1], ldh, &
  537. h__[jch + 1 + (jch - 1) * h_dim1], ldh, &c__,
  538. &s);
  539. if (ilq) {
  540. drot_(n, &q[jch * q_dim1 + 1], &c__1, &q[(jch + 1)
  541. * q_dim1 + 1], &c__1, &c__, &s);
  542. }
  543. temp = h__[jch + 1 + jch * h_dim1];
  544. dlartg_(&temp, &h__[jch + 1 + (jch - 1) * h_dim1], &
  545. c__, &s, &h__[jch + 1 + jch * h_dim1]);
  546. h__[jch + 1 + (jch - 1) * h_dim1] = 0.;
  547. i__4 = jch + 1 - ifrstm;
  548. drot_(&i__4, &h__[ifrstm + jch * h_dim1], &c__1, &h__[
  549. ifrstm + (jch - 1) * h_dim1], &c__1, &c__, &s)
  550. ;
  551. i__4 = jch - ifrstm;
  552. drot_(&i__4, &t[ifrstm + jch * t_dim1], &c__1, &t[
  553. ifrstm + (jch - 1) * t_dim1], &c__1, &c__, &s)
  554. ;
  555. if (ilz) {
  556. drot_(n, &z__[jch * z_dim1 + 1], &c__1, &z__[(jch
  557. - 1) * z_dim1 + 1], &c__1, &c__, &s);
  558. }
  559. /* L50: */
  560. }
  561. goto L70;
  562. }
  563. } else if (ilazro) {
  564. /* Only test 1 passed -- work on J:ILAST */
  565. ifirst = j;
  566. goto L110;
  567. }
  568. /* Neither test passed -- try next J */
  569. /* L60: */
  570. }
  571. /* (Drop-through is "impossible") */
  572. *info = *n + 1;
  573. goto L420;
  574. /* T(ILAST,ILAST)=0 -- clear H(ILAST,ILAST-1) to split off a */
  575. /* 1x1 block. */
  576. L70:
  577. temp = h__[ilast + ilast * h_dim1];
  578. dlartg_(&temp, &h__[ilast + (ilast - 1) * h_dim1], &c__, &s, &h__[
  579. ilast + ilast * h_dim1]);
  580. h__[ilast + (ilast - 1) * h_dim1] = 0.;
  581. i__2 = ilast - ifrstm;
  582. drot_(&i__2, &h__[ifrstm + ilast * h_dim1], &c__1, &h__[ifrstm + (
  583. ilast - 1) * h_dim1], &c__1, &c__, &s);
  584. i__2 = ilast - ifrstm;
  585. drot_(&i__2, &t[ifrstm + ilast * t_dim1], &c__1, &t[ifrstm + (ilast -
  586. 1) * t_dim1], &c__1, &c__, &s);
  587. if (ilz) {
  588. drot_(n, &z__[ilast * z_dim1 + 1], &c__1, &z__[(ilast - 1) *
  589. z_dim1 + 1], &c__1, &c__, &s);
  590. }
  591. /* H(ILAST,ILAST-1)=0 -- Standardize B, set ALPHAR, ALPHAI, */
  592. /* and BETA */
  593. L80:
  594. if (t[ilast + ilast * t_dim1] < 0.) {
  595. if (ilschr) {
  596. i__2 = ilast;
  597. for (j = ifrstm; j <= i__2; ++j) {
  598. h__[j + ilast * h_dim1] = -h__[j + ilast * h_dim1];
  599. t[j + ilast * t_dim1] = -t[j + ilast * t_dim1];
  600. /* L90: */
  601. }
  602. } else {
  603. h__[ilast + ilast * h_dim1] = -h__[ilast + ilast * h_dim1];
  604. t[ilast + ilast * t_dim1] = -t[ilast + ilast * t_dim1];
  605. }
  606. if (ilz) {
  607. i__2 = *n;
  608. for (j = 1; j <= i__2; ++j) {
  609. z__[j + ilast * z_dim1] = -z__[j + ilast * z_dim1];
  610. /* L100: */
  611. }
  612. }
  613. }
  614. alphar[ilast] = h__[ilast + ilast * h_dim1];
  615. alphai[ilast] = 0.;
  616. beta[ilast] = t[ilast + ilast * t_dim1];
  617. /* Go to next block -- exit if finished. */
  618. --ilast;
  619. if (ilast < *ilo) {
  620. goto L380;
  621. }
  622. /* Reset counters */
  623. iiter = 0;
  624. eshift = 0.;
  625. if (! ilschr) {
  626. ilastm = ilast;
  627. if (ifrstm > ilast) {
  628. ifrstm = *ilo;
  629. }
  630. }
  631. goto L350;
  632. /* QZ step */
  633. /* This iteration only involves rows/columns IFIRST:ILAST. We */
  634. /* assume IFIRST < ILAST, and that the diagonal of B is non-zero. */
  635. L110:
  636. ++iiter;
  637. if (! ilschr) {
  638. ifrstm = ifirst;
  639. }
  640. /* Compute single shifts. */
  641. /* At this point, IFIRST < ILAST, and the diagonal elements of */
  642. /* T(IFIRST:ILAST,IFIRST,ILAST) are larger than BTOL (in */
  643. /* magnitude) */
  644. if (iiter / 10 * 10 == iiter) {
  645. /* Exceptional shift. Chosen for no particularly good reason. */
  646. /* (Single shift only.) */
  647. if ((doublereal) maxit * safmin * (d__1 = h__[ilast - 1 + ilast *
  648. h_dim1], abs(d__1)) < (d__2 = t[ilast - 1 + (ilast - 1) *
  649. t_dim1], abs(d__2))) {
  650. eshift += h__[ilast - 1 + ilast * h_dim1] / t[ilast - 1 + (
  651. ilast - 1) * t_dim1];
  652. } else {
  653. eshift += 1. / (safmin * (doublereal) maxit);
  654. }
  655. s1 = 1.;
  656. wr = eshift;
  657. } else {
  658. /* Shifts based on the generalized eigenvalues of the */
  659. /* bottom-right 2x2 block of A and B. The first eigenvalue */
  660. /* returned by DLAG2 is the Wilkinson shift (AEP p.512), */
  661. d__1 = safmin * 100.;
  662. dlag2_(&h__[ilast - 1 + (ilast - 1) * h_dim1], ldh, &t[ilast - 1
  663. + (ilast - 1) * t_dim1], ldt, &d__1, &s1, &s2, &wr, &wr2,
  664. &wi);
  665. /* Computing MAX */
  666. /* Computing MAX */
  667. d__3 = 1., d__4 = abs(wr), d__3 = max(d__3,d__4), d__4 = abs(wi);
  668. d__1 = s1, d__2 = safmin * max(d__3,d__4);
  669. temp = max(d__1,d__2);
  670. if (wi != 0.) {
  671. goto L200;
  672. }
  673. }
  674. /* Fiddle with shift to avoid overflow */
  675. temp = min(ascale,1.) * (safmax * .5);
  676. if (s1 > temp) {
  677. scale = temp / s1;
  678. } else {
  679. scale = 1.;
  680. }
  681. temp = min(bscale,1.) * (safmax * .5);
  682. if (abs(wr) > temp) {
  683. /* Computing MIN */
  684. d__1 = scale, d__2 = temp / abs(wr);
  685. scale = min(d__1,d__2);
  686. }
  687. s1 = scale * s1;
  688. wr = scale * wr;
  689. /* Now check for two consecutive small subdiagonals. */
  690. i__2 = ifirst + 1;
  691. for (j = ilast - 1; j >= i__2; --j) {
  692. istart = j;
  693. temp = (d__1 = s1 * h__[j + (j - 1) * h_dim1], abs(d__1));
  694. temp2 = (d__1 = s1 * h__[j + j * h_dim1] - wr * t[j + j * t_dim1],
  695. abs(d__1));
  696. tempr = max(temp,temp2);
  697. if (tempr < 1. && tempr != 0.) {
  698. temp /= tempr;
  699. temp2 /= tempr;
  700. }
  701. if ((d__1 = ascale * h__[j + 1 + j * h_dim1] * temp, abs(d__1)) <=
  702. ascale * atol * temp2) {
  703. goto L130;
  704. }
  705. /* L120: */
  706. }
  707. istart = ifirst;
  708. L130:
  709. /* Do an implicit single-shift QZ sweep. */
  710. /* Initial Q */
  711. temp = s1 * h__[istart + istart * h_dim1] - wr * t[istart + istart *
  712. t_dim1];
  713. temp2 = s1 * h__[istart + 1 + istart * h_dim1];
  714. dlartg_(&temp, &temp2, &c__, &s, &tempr);
  715. /* Sweep */
  716. i__2 = ilast - 1;
  717. for (j = istart; j <= i__2; ++j) {
  718. if (j > istart) {
  719. temp = h__[j + (j - 1) * h_dim1];
  720. dlartg_(&temp, &h__[j + 1 + (j - 1) * h_dim1], &c__, &s, &h__[
  721. j + (j - 1) * h_dim1]);
  722. h__[j + 1 + (j - 1) * h_dim1] = 0.;
  723. }
  724. i__3 = ilastm;
  725. for (jc = j; jc <= i__3; ++jc) {
  726. temp = c__ * h__[j + jc * h_dim1] + s * h__[j + 1 + jc *
  727. h_dim1];
  728. h__[j + 1 + jc * h_dim1] = -s * h__[j + jc * h_dim1] + c__ *
  729. h__[j + 1 + jc * h_dim1];
  730. h__[j + jc * h_dim1] = temp;
  731. temp2 = c__ * t[j + jc * t_dim1] + s * t[j + 1 + jc * t_dim1];
  732. t[j + 1 + jc * t_dim1] = -s * t[j + jc * t_dim1] + c__ * t[j
  733. + 1 + jc * t_dim1];
  734. t[j + jc * t_dim1] = temp2;
  735. /* L140: */
  736. }
  737. if (ilq) {
  738. i__3 = *n;
  739. for (jr = 1; jr <= i__3; ++jr) {
  740. temp = c__ * q[jr + j * q_dim1] + s * q[jr + (j + 1) *
  741. q_dim1];
  742. q[jr + (j + 1) * q_dim1] = -s * q[jr + j * q_dim1] + c__ *
  743. q[jr + (j + 1) * q_dim1];
  744. q[jr + j * q_dim1] = temp;
  745. /* L150: */
  746. }
  747. }
  748. temp = t[j + 1 + (j + 1) * t_dim1];
  749. dlartg_(&temp, &t[j + 1 + j * t_dim1], &c__, &s, &t[j + 1 + (j +
  750. 1) * t_dim1]);
  751. t[j + 1 + j * t_dim1] = 0.;
  752. /* Computing MIN */
  753. i__4 = j + 2;
  754. i__3 = min(i__4,ilast);
  755. for (jr = ifrstm; jr <= i__3; ++jr) {
  756. temp = c__ * h__[jr + (j + 1) * h_dim1] + s * h__[jr + j *
  757. h_dim1];
  758. h__[jr + j * h_dim1] = -s * h__[jr + (j + 1) * h_dim1] + c__ *
  759. h__[jr + j * h_dim1];
  760. h__[jr + (j + 1) * h_dim1] = temp;
  761. /* L160: */
  762. }
  763. i__3 = j;
  764. for (jr = ifrstm; jr <= i__3; ++jr) {
  765. temp = c__ * t[jr + (j + 1) * t_dim1] + s * t[jr + j * t_dim1]
  766. ;
  767. t[jr + j * t_dim1] = -s * t[jr + (j + 1) * t_dim1] + c__ * t[
  768. jr + j * t_dim1];
  769. t[jr + (j + 1) * t_dim1] = temp;
  770. /* L170: */
  771. }
  772. if (ilz) {
  773. i__3 = *n;
  774. for (jr = 1; jr <= i__3; ++jr) {
  775. temp = c__ * z__[jr + (j + 1) * z_dim1] + s * z__[jr + j *
  776. z_dim1];
  777. z__[jr + j * z_dim1] = -s * z__[jr + (j + 1) * z_dim1] +
  778. c__ * z__[jr + j * z_dim1];
  779. z__[jr + (j + 1) * z_dim1] = temp;
  780. /* L180: */
  781. }
  782. }
  783. /* L190: */
  784. }
  785. goto L350;
  786. /* Use Francis double-shift */
  787. /* Note: the Francis double-shift should work with real shifts, */
  788. /* but only if the block is at least 3x3. */
  789. /* This code may break if this point is reached with */
  790. /* a 2x2 block with real eigenvalues. */
  791. L200:
  792. if (ifirst + 1 == ilast) {
  793. /* Special case -- 2x2 block with complex eigenvectors */
  794. /* Step 1: Standardize, that is, rotate so that */
  795. /* ( B11 0 ) */
  796. /* B = ( ) with B11 non-negative. */
  797. /* ( 0 B22 ) */
  798. dlasv2_(&t[ilast - 1 + (ilast - 1) * t_dim1], &t[ilast - 1 +
  799. ilast * t_dim1], &t[ilast + ilast * t_dim1], &b22, &b11, &
  800. sr, &cr, &sl, &cl);
  801. if (b11 < 0.) {
  802. cr = -cr;
  803. sr = -sr;
  804. b11 = -b11;
  805. b22 = -b22;
  806. }
  807. i__2 = ilastm + 1 - ifirst;
  808. drot_(&i__2, &h__[ilast - 1 + (ilast - 1) * h_dim1], ldh, &h__[
  809. ilast + (ilast - 1) * h_dim1], ldh, &cl, &sl);
  810. i__2 = ilast + 1 - ifrstm;
  811. drot_(&i__2, &h__[ifrstm + (ilast - 1) * h_dim1], &c__1, &h__[
  812. ifrstm + ilast * h_dim1], &c__1, &cr, &sr);
  813. if (ilast < ilastm) {
  814. i__2 = ilastm - ilast;
  815. drot_(&i__2, &t[ilast - 1 + (ilast + 1) * t_dim1], ldt, &t[
  816. ilast + (ilast + 1) * t_dim1], ldt, &cl, &sl);
  817. }
  818. if (ifrstm < ilast - 1) {
  819. i__2 = ifirst - ifrstm;
  820. drot_(&i__2, &t[ifrstm + (ilast - 1) * t_dim1], &c__1, &t[
  821. ifrstm + ilast * t_dim1], &c__1, &cr, &sr);
  822. }
  823. if (ilq) {
  824. drot_(n, &q[(ilast - 1) * q_dim1 + 1], &c__1, &q[ilast *
  825. q_dim1 + 1], &c__1, &cl, &sl);
  826. }
  827. if (ilz) {
  828. drot_(n, &z__[(ilast - 1) * z_dim1 + 1], &c__1, &z__[ilast *
  829. z_dim1 + 1], &c__1, &cr, &sr);
  830. }
  831. t[ilast - 1 + (ilast - 1) * t_dim1] = b11;
  832. t[ilast - 1 + ilast * t_dim1] = 0.;
  833. t[ilast + (ilast - 1) * t_dim1] = 0.;
  834. t[ilast + ilast * t_dim1] = b22;
  835. /* If B22 is negative, negate column ILAST */
  836. if (b22 < 0.) {
  837. i__2 = ilast;
  838. for (j = ifrstm; j <= i__2; ++j) {
  839. h__[j + ilast * h_dim1] = -h__[j + ilast * h_dim1];
  840. t[j + ilast * t_dim1] = -t[j + ilast * t_dim1];
  841. /* L210: */
  842. }
  843. if (ilz) {
  844. i__2 = *n;
  845. for (j = 1; j <= i__2; ++j) {
  846. z__[j + ilast * z_dim1] = -z__[j + ilast * z_dim1];
  847. /* L220: */
  848. }
  849. }
  850. }
  851. /* Step 2: Compute ALPHAR, ALPHAI, and BETA (see refs.) */
  852. /* Recompute shift */
  853. d__1 = safmin * 100.;
  854. dlag2_(&h__[ilast - 1 + (ilast - 1) * h_dim1], ldh, &t[ilast - 1
  855. + (ilast - 1) * t_dim1], ldt, &d__1, &s1, &temp, &wr, &
  856. temp2, &wi);
  857. /* If standardization has perturbed the shift onto real line, */
  858. /* do another (real single-shift) QR step. */
  859. if (wi == 0.) {
  860. goto L350;
  861. }
  862. s1inv = 1. / s1;
  863. /* Do EISPACK (QZVAL) computation of alpha and beta */
  864. a11 = h__[ilast - 1 + (ilast - 1) * h_dim1];
  865. a21 = h__[ilast + (ilast - 1) * h_dim1];
  866. a12 = h__[ilast - 1 + ilast * h_dim1];
  867. a22 = h__[ilast + ilast * h_dim1];
  868. /* Compute complex Givens rotation on right */
  869. /* (Assume some element of C = (sA - wB) > unfl ) */
  870. /* __ */
  871. /* (sA - wB) ( CZ -SZ ) */
  872. /* ( SZ CZ ) */
  873. c11r = s1 * a11 - wr * b11;
  874. c11i = -wi * b11;
  875. c12 = s1 * a12;
  876. c21 = s1 * a21;
  877. c22r = s1 * a22 - wr * b22;
  878. c22i = -wi * b22;
  879. if (abs(c11r) + abs(c11i) + abs(c12) > abs(c21) + abs(c22r) + abs(
  880. c22i)) {
  881. t1 = dlapy3_(&c12, &c11r, &c11i);
  882. cz = c12 / t1;
  883. szr = -c11r / t1;
  884. szi = -c11i / t1;
  885. } else {
  886. cz = dlapy2_(&c22r, &c22i);
  887. if (cz <= safmin) {
  888. cz = 0.;
  889. szr = 1.;
  890. szi = 0.;
  891. } else {
  892. tempr = c22r / cz;
  893. tempi = c22i / cz;
  894. t1 = dlapy2_(&cz, &c21);
  895. cz /= t1;
  896. szr = -c21 * tempr / t1;
  897. szi = c21 * tempi / t1;
  898. }
  899. }
  900. /* Compute Givens rotation on left */
  901. /* ( CQ SQ ) */
  902. /* ( __ ) A or B */
  903. /* ( -SQ CQ ) */
  904. an = abs(a11) + abs(a12) + abs(a21) + abs(a22);
  905. bn = abs(b11) + abs(b22);
  906. wabs = abs(wr) + abs(wi);
  907. if (s1 * an > wabs * bn) {
  908. cq = cz * b11;
  909. sqr = szr * b22;
  910. sqi = -szi * b22;
  911. } else {
  912. a1r = cz * a11 + szr * a12;
  913. a1i = szi * a12;
  914. a2r = cz * a21 + szr * a22;
  915. a2i = szi * a22;
  916. cq = dlapy2_(&a1r, &a1i);
  917. if (cq <= safmin) {
  918. cq = 0.;
  919. sqr = 1.;
  920. sqi = 0.;
  921. } else {
  922. tempr = a1r / cq;
  923. tempi = a1i / cq;
  924. sqr = tempr * a2r + tempi * a2i;
  925. sqi = tempi * a2r - tempr * a2i;
  926. }
  927. }
  928. t1 = dlapy3_(&cq, &sqr, &sqi);
  929. cq /= t1;
  930. sqr /= t1;
  931. sqi /= t1;
  932. /* Compute diagonal elements of QBZ */
  933. tempr = sqr * szr - sqi * szi;
  934. tempi = sqr * szi + sqi * szr;
  935. b1r = cq * cz * b11 + tempr * b22;
  936. b1i = tempi * b22;
  937. b1a = dlapy2_(&b1r, &b1i);
  938. b2r = cq * cz * b22 + tempr * b11;
  939. b2i = -tempi * b11;
  940. b2a = dlapy2_(&b2r, &b2i);
  941. /* Normalize so beta > 0, and Im( alpha1 ) > 0 */
  942. beta[ilast - 1] = b1a;
  943. beta[ilast] = b2a;
  944. alphar[ilast - 1] = wr * b1a * s1inv;
  945. alphai[ilast - 1] = wi * b1a * s1inv;
  946. alphar[ilast] = wr * b2a * s1inv;
  947. alphai[ilast] = -(wi * b2a) * s1inv;
  948. /* Step 3: Go to next block -- exit if finished. */
  949. ilast = ifirst - 1;
  950. if (ilast < *ilo) {
  951. goto L380;
  952. }
  953. /* Reset counters */
  954. iiter = 0;
  955. eshift = 0.;
  956. if (! ilschr) {
  957. ilastm = ilast;
  958. if (ifrstm > ilast) {
  959. ifrstm = *ilo;
  960. }
  961. }
  962. goto L350;
  963. } else {
  964. /* Usual case: 3x3 or larger block, using Francis implicit */
  965. /* double-shift */
  966. /* 2 */
  967. /* Eigenvalue equation is w - c w + d = 0, */
  968. /* -1 2 -1 */
  969. /* so compute 1st column of (A B ) - c A B + d */
  970. /* using the formula in QZIT (from EISPACK) */
  971. /* We assume that the block is at least 3x3 */
  972. ad11 = ascale * h__[ilast - 1 + (ilast - 1) * h_dim1] / (bscale *
  973. t[ilast - 1 + (ilast - 1) * t_dim1]);
  974. ad21 = ascale * h__[ilast + (ilast - 1) * h_dim1] / (bscale * t[
  975. ilast - 1 + (ilast - 1) * t_dim1]);
  976. ad12 = ascale * h__[ilast - 1 + ilast * h_dim1] / (bscale * t[
  977. ilast + ilast * t_dim1]);
  978. ad22 = ascale * h__[ilast + ilast * h_dim1] / (bscale * t[ilast +
  979. ilast * t_dim1]);
  980. u12 = t[ilast - 1 + ilast * t_dim1] / t[ilast + ilast * t_dim1];
  981. ad11l = ascale * h__[ifirst + ifirst * h_dim1] / (bscale * t[
  982. ifirst + ifirst * t_dim1]);
  983. ad21l = ascale * h__[ifirst + 1 + ifirst * h_dim1] / (bscale * t[
  984. ifirst + ifirst * t_dim1]);
  985. ad12l = ascale * h__[ifirst + (ifirst + 1) * h_dim1] / (bscale *
  986. t[ifirst + 1 + (ifirst + 1) * t_dim1]);
  987. ad22l = ascale * h__[ifirst + 1 + (ifirst + 1) * h_dim1] / (
  988. bscale * t[ifirst + 1 + (ifirst + 1) * t_dim1]);
  989. ad32l = ascale * h__[ifirst + 2 + (ifirst + 1) * h_dim1] / (
  990. bscale * t[ifirst + 1 + (ifirst + 1) * t_dim1]);
  991. u12l = t[ifirst + (ifirst + 1) * t_dim1] / t[ifirst + 1 + (ifirst
  992. + 1) * t_dim1];
  993. v[0] = (ad11 - ad11l) * (ad22 - ad11l) - ad12 * ad21 + ad21 * u12
  994. * ad11l + (ad12l - ad11l * u12l) * ad21l;
  995. v[1] = (ad22l - ad11l - ad21l * u12l - (ad11 - ad11l) - (ad22 -
  996. ad11l) + ad21 * u12) * ad21l;
  997. v[2] = ad32l * ad21l;
  998. istart = ifirst;
  999. dlarfg_(&c__3, v, &v[1], &c__1, &tau);
  1000. v[0] = 1.;
  1001. /* Sweep */
  1002. i__2 = ilast - 2;
  1003. for (j = istart; j <= i__2; ++j) {
  1004. /* All but last elements: use 3x3 Householder transforms. */
  1005. /* Zero (j-1)st column of A */
  1006. if (j > istart) {
  1007. v[0] = h__[j + (j - 1) * h_dim1];
  1008. v[1] = h__[j + 1 + (j - 1) * h_dim1];
  1009. v[2] = h__[j + 2 + (j - 1) * h_dim1];
  1010. dlarfg_(&c__3, &h__[j + (j - 1) * h_dim1], &v[1], &c__1, &
  1011. tau);
  1012. v[0] = 1.;
  1013. h__[j + 1 + (j - 1) * h_dim1] = 0.;
  1014. h__[j + 2 + (j - 1) * h_dim1] = 0.;
  1015. }
  1016. i__3 = ilastm;
  1017. for (jc = j; jc <= i__3; ++jc) {
  1018. temp = tau * (h__[j + jc * h_dim1] + v[1] * h__[j + 1 +
  1019. jc * h_dim1] + v[2] * h__[j + 2 + jc * h_dim1]);
  1020. h__[j + jc * h_dim1] -= temp;
  1021. h__[j + 1 + jc * h_dim1] -= temp * v[1];
  1022. h__[j + 2 + jc * h_dim1] -= temp * v[2];
  1023. temp2 = tau * (t[j + jc * t_dim1] + v[1] * t[j + 1 + jc *
  1024. t_dim1] + v[2] * t[j + 2 + jc * t_dim1]);
  1025. t[j + jc * t_dim1] -= temp2;
  1026. t[j + 1 + jc * t_dim1] -= temp2 * v[1];
  1027. t[j + 2 + jc * t_dim1] -= temp2 * v[2];
  1028. /* L230: */
  1029. }
  1030. if (ilq) {
  1031. i__3 = *n;
  1032. for (jr = 1; jr <= i__3; ++jr) {
  1033. temp = tau * (q[jr + j * q_dim1] + v[1] * q[jr + (j +
  1034. 1) * q_dim1] + v[2] * q[jr + (j + 2) * q_dim1]
  1035. );
  1036. q[jr + j * q_dim1] -= temp;
  1037. q[jr + (j + 1) * q_dim1] -= temp * v[1];
  1038. q[jr + (j + 2) * q_dim1] -= temp * v[2];
  1039. /* L240: */
  1040. }
  1041. }
  1042. /* Zero j-th column of B (see DLAGBC for details) */
  1043. /* Swap rows to pivot */
  1044. ilpivt = FALSE_;
  1045. /* Computing MAX */
  1046. d__3 = (d__1 = t[j + 1 + (j + 1) * t_dim1], abs(d__1)), d__4 =
  1047. (d__2 = t[j + 1 + (j + 2) * t_dim1], abs(d__2));
  1048. temp = max(d__3,d__4);
  1049. /* Computing MAX */
  1050. d__3 = (d__1 = t[j + 2 + (j + 1) * t_dim1], abs(d__1)), d__4 =
  1051. (d__2 = t[j + 2 + (j + 2) * t_dim1], abs(d__2));
  1052. temp2 = max(d__3,d__4);
  1053. if (max(temp,temp2) < safmin) {
  1054. scale = 0.;
  1055. u1 = 1.;
  1056. u2 = 0.;
  1057. goto L250;
  1058. } else if (temp >= temp2) {
  1059. w11 = t[j + 1 + (j + 1) * t_dim1];
  1060. w21 = t[j + 2 + (j + 1) * t_dim1];
  1061. w12 = t[j + 1 + (j + 2) * t_dim1];
  1062. w22 = t[j + 2 + (j + 2) * t_dim1];
  1063. u1 = t[j + 1 + j * t_dim1];
  1064. u2 = t[j + 2 + j * t_dim1];
  1065. } else {
  1066. w21 = t[j + 1 + (j + 1) * t_dim1];
  1067. w11 = t[j + 2 + (j + 1) * t_dim1];
  1068. w22 = t[j + 1 + (j + 2) * t_dim1];
  1069. w12 = t[j + 2 + (j + 2) * t_dim1];
  1070. u2 = t[j + 1 + j * t_dim1];
  1071. u1 = t[j + 2 + j * t_dim1];
  1072. }
  1073. /* Swap columns if nec. */
  1074. if (abs(w12) > abs(w11)) {
  1075. ilpivt = TRUE_;
  1076. temp = w12;
  1077. temp2 = w22;
  1078. w12 = w11;
  1079. w22 = w21;
  1080. w11 = temp;
  1081. w21 = temp2;
  1082. }
  1083. /* LU-factor */
  1084. temp = w21 / w11;
  1085. u2 -= temp * u1;
  1086. w22 -= temp * w12;
  1087. w21 = 0.;
  1088. /* Compute SCALE */
  1089. scale = 1.;
  1090. if (abs(w22) < safmin) {
  1091. scale = 0.;
  1092. u2 = 1.;
  1093. u1 = -w12 / w11;
  1094. goto L250;
  1095. }
  1096. if (abs(w22) < abs(u2)) {
  1097. scale = (d__1 = w22 / u2, abs(d__1));
  1098. }
  1099. if (abs(w11) < abs(u1)) {
  1100. /* Computing MIN */
  1101. d__2 = scale, d__3 = (d__1 = w11 / u1, abs(d__1));
  1102. scale = min(d__2,d__3);
  1103. }
  1104. /* Solve */
  1105. u2 = scale * u2 / w22;
  1106. u1 = (scale * u1 - w12 * u2) / w11;
  1107. L250:
  1108. if (ilpivt) {
  1109. temp = u2;
  1110. u2 = u1;
  1111. u1 = temp;
  1112. }
  1113. /* Compute Householder Vector */
  1114. /* Computing 2nd power */
  1115. d__1 = scale;
  1116. /* Computing 2nd power */
  1117. d__2 = u1;
  1118. /* Computing 2nd power */
  1119. d__3 = u2;
  1120. t1 = sqrt(d__1 * d__1 + d__2 * d__2 + d__3 * d__3);
  1121. tau = scale / t1 + 1.;
  1122. vs = -1. / (scale + t1);
  1123. v[0] = 1.;
  1124. v[1] = vs * u1;
  1125. v[2] = vs * u2;
  1126. /* Apply transformations from the right. */
  1127. /* Computing MIN */
  1128. i__4 = j + 3;
  1129. i__3 = min(i__4,ilast);
  1130. for (jr = ifrstm; jr <= i__3; ++jr) {
  1131. temp = tau * (h__[jr + j * h_dim1] + v[1] * h__[jr + (j +
  1132. 1) * h_dim1] + v[2] * h__[jr + (j + 2) * h_dim1]);
  1133. h__[jr + j * h_dim1] -= temp;
  1134. h__[jr + (j + 1) * h_dim1] -= temp * v[1];
  1135. h__[jr + (j + 2) * h_dim1] -= temp * v[2];
  1136. /* L260: */
  1137. }
  1138. i__3 = j + 2;
  1139. for (jr = ifrstm; jr <= i__3; ++jr) {
  1140. temp = tau * (t[jr + j * t_dim1] + v[1] * t[jr + (j + 1) *
  1141. t_dim1] + v[2] * t[jr + (j + 2) * t_dim1]);
  1142. t[jr + j * t_dim1] -= temp;
  1143. t[jr + (j + 1) * t_dim1] -= temp * v[1];
  1144. t[jr + (j + 2) * t_dim1] -= temp * v[2];
  1145. /* L270: */
  1146. }
  1147. if (ilz) {
  1148. i__3 = *n;
  1149. for (jr = 1; jr <= i__3; ++jr) {
  1150. temp = tau * (z__[jr + j * z_dim1] + v[1] * z__[jr + (
  1151. j + 1) * z_dim1] + v[2] * z__[jr + (j + 2) *
  1152. z_dim1]);
  1153. z__[jr + j * z_dim1] -= temp;
  1154. z__[jr + (j + 1) * z_dim1] -= temp * v[1];
  1155. z__[jr + (j + 2) * z_dim1] -= temp * v[2];
  1156. /* L280: */
  1157. }
  1158. }
  1159. t[j + 1 + j * t_dim1] = 0.;
  1160. t[j + 2 + j * t_dim1] = 0.;
  1161. /* L290: */
  1162. }
  1163. /* Last elements: Use Givens rotations */
  1164. /* Rotations from the left */
  1165. j = ilast - 1;
  1166. temp = h__[j + (j - 1) * h_dim1];
  1167. dlartg_(&temp, &h__[j + 1 + (j - 1) * h_dim1], &c__, &s, &h__[j +
  1168. (j - 1) * h_dim1]);
  1169. h__[j + 1 + (j - 1) * h_dim1] = 0.;
  1170. i__2 = ilastm;
  1171. for (jc = j; jc <= i__2; ++jc) {
  1172. temp = c__ * h__[j + jc * h_dim1] + s * h__[j + 1 + jc *
  1173. h_dim1];
  1174. h__[j + 1 + jc * h_dim1] = -s * h__[j + jc * h_dim1] + c__ *
  1175. h__[j + 1 + jc * h_dim1];
  1176. h__[j + jc * h_dim1] = temp;
  1177. temp2 = c__ * t[j + jc * t_dim1] + s * t[j + 1 + jc * t_dim1];
  1178. t[j + 1 + jc * t_dim1] = -s * t[j + jc * t_dim1] + c__ * t[j
  1179. + 1 + jc * t_dim1];
  1180. t[j + jc * t_dim1] = temp2;
  1181. /* L300: */
  1182. }
  1183. if (ilq) {
  1184. i__2 = *n;
  1185. for (jr = 1; jr <= i__2; ++jr) {
  1186. temp = c__ * q[jr + j * q_dim1] + s * q[jr + (j + 1) *
  1187. q_dim1];
  1188. q[jr + (j + 1) * q_dim1] = -s * q[jr + j * q_dim1] + c__ *
  1189. q[jr + (j + 1) * q_dim1];
  1190. q[jr + j * q_dim1] = temp;
  1191. /* L310: */
  1192. }
  1193. }
  1194. /* Rotations from the right. */
  1195. temp = t[j + 1 + (j + 1) * t_dim1];
  1196. dlartg_(&temp, &t[j + 1 + j * t_dim1], &c__, &s, &t[j + 1 + (j +
  1197. 1) * t_dim1]);
  1198. t[j + 1 + j * t_dim1] = 0.;
  1199. i__2 = ilast;
  1200. for (jr = ifrstm; jr <= i__2; ++jr) {
  1201. temp = c__ * h__[jr + (j + 1) * h_dim1] + s * h__[jr + j *
  1202. h_dim1];
  1203. h__[jr + j * h_dim1] = -s * h__[jr + (j + 1) * h_dim1] + c__ *
  1204. h__[jr + j * h_dim1];
  1205. h__[jr + (j + 1) * h_dim1] = temp;
  1206. /* L320: */
  1207. }
  1208. i__2 = ilast - 1;
  1209. for (jr = ifrstm; jr <= i__2; ++jr) {
  1210. temp = c__ * t[jr + (j + 1) * t_dim1] + s * t[jr + j * t_dim1]
  1211. ;
  1212. t[jr + j * t_dim1] = -s * t[jr + (j + 1) * t_dim1] + c__ * t[
  1213. jr + j * t_dim1];
  1214. t[jr + (j + 1) * t_dim1] = temp;
  1215. /* L330: */
  1216. }
  1217. if (ilz) {
  1218. i__2 = *n;
  1219. for (jr = 1; jr <= i__2; ++jr) {
  1220. temp = c__ * z__[jr + (j + 1) * z_dim1] + s * z__[jr + j *
  1221. z_dim1];
  1222. z__[jr + j * z_dim1] = -s * z__[jr + (j + 1) * z_dim1] +
  1223. c__ * z__[jr + j * z_dim1];
  1224. z__[jr + (j + 1) * z_dim1] = temp;
  1225. /* L340: */
  1226. }
  1227. }
  1228. /* End of Double-Shift code */
  1229. }
  1230. goto L350;
  1231. /* End of iteration loop */
  1232. L350:
  1233. /* L360: */
  1234. ;
  1235. }
  1236. /* Drop-through = non-convergence */
  1237. *info = ilast;
  1238. goto L420;
  1239. /* Successful completion of all QZ steps */
  1240. L380:
  1241. /* Set Eigenvalues 1:ILO-1 */
  1242. i__1 = *ilo - 1;
  1243. for (j = 1; j <= i__1; ++j) {
  1244. if (t[j + j * t_dim1] < 0.) {
  1245. if (ilschr) {
  1246. i__2 = j;
  1247. for (jr = 1; jr <= i__2; ++jr) {
  1248. h__[jr + j * h_dim1] = -h__[jr + j * h_dim1];
  1249. t[jr + j * t_dim1] = -t[jr + j * t_dim1];
  1250. /* L390: */
  1251. }
  1252. } else {
  1253. h__[j + j * h_dim1] = -h__[j + j * h_dim1];
  1254. t[j + j * t_dim1] = -t[j + j * t_dim1];
  1255. }
  1256. if (ilz) {
  1257. i__2 = *n;
  1258. for (jr = 1; jr <= i__2; ++jr) {
  1259. z__[jr + j * z_dim1] = -z__[jr + j * z_dim1];
  1260. /* L400: */
  1261. }
  1262. }
  1263. }
  1264. alphar[j] = h__[j + j * h_dim1];
  1265. alphai[j] = 0.;
  1266. beta[j] = t[j + j * t_dim1];
  1267. /* L410: */
  1268. }
  1269. /* Normal Termination */
  1270. *info = 0;
  1271. /* Exit (other than argument error) -- return optimal workspace size */
  1272. L420:
  1273. work[1] = (doublereal) (*n);
  1274. return 0;
  1275. /* End of DHGEQZ */
  1276. } /* dhgeqz_ */