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.

cggesx.f 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. *> \brief <b> CGGESX computes the eigenvalues, the Schur form, and, optionally, the matrix of Schur vectors for GE matrices</b>
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download CGGESX + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cggesx.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cggesx.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cggesx.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE CGGESX( JOBVSL, JOBVSR, SORT, SELCTG, SENSE, N, A, LDA,
  22. * B, LDB, SDIM, ALPHA, BETA, VSL, LDVSL, VSR,
  23. * LDVSR, RCONDE, RCONDV, WORK, LWORK, RWORK,
  24. * IWORK, LIWORK, BWORK, INFO )
  25. *
  26. * .. Scalar Arguments ..
  27. * CHARACTER JOBVSL, JOBVSR, SENSE, SORT
  28. * INTEGER INFO, LDA, LDB, LDVSL, LDVSR, LIWORK, LWORK, N,
  29. * $ SDIM
  30. * ..
  31. * .. Array Arguments ..
  32. * LOGICAL BWORK( * )
  33. * INTEGER IWORK( * )
  34. * REAL RCONDE( 2 ), RCONDV( 2 ), RWORK( * )
  35. * COMPLEX A( LDA, * ), ALPHA( * ), B( LDB, * ),
  36. * $ BETA( * ), VSL( LDVSL, * ), VSR( LDVSR, * ),
  37. * $ WORK( * )
  38. * ..
  39. * .. Function Arguments ..
  40. * LOGICAL SELCTG
  41. * EXTERNAL SELCTG
  42. * ..
  43. *
  44. *
  45. *> \par Purpose:
  46. * =============
  47. *>
  48. *> \verbatim
  49. *>
  50. *> CGGESX computes for a pair of N-by-N complex nonsymmetric matrices
  51. *> (A,B), the generalized eigenvalues, the complex Schur form (S,T),
  52. *> and, optionally, the left and/or right matrices of Schur vectors (VSL
  53. *> and VSR). This gives the generalized Schur factorization
  54. *>
  55. *> (A,B) = ( (VSL) S (VSR)**H, (VSL) T (VSR)**H )
  56. *>
  57. *> where (VSR)**H is the conjugate-transpose of VSR.
  58. *>
  59. *> Optionally, it also orders the eigenvalues so that a selected cluster
  60. *> of eigenvalues appears in the leading diagonal blocks of the upper
  61. *> triangular matrix S and the upper triangular matrix T; computes
  62. *> a reciprocal condition number for the average of the selected
  63. *> eigenvalues (RCONDE); and computes a reciprocal condition number for
  64. *> the right and left deflating subspaces corresponding to the selected
  65. *> eigenvalues (RCONDV). The leading columns of VSL and VSR then form
  66. *> an orthonormal basis for the corresponding left and right eigenspaces
  67. *> (deflating subspaces).
  68. *>
  69. *> A generalized eigenvalue for a pair of matrices (A,B) is a scalar w
  70. *> or a ratio alpha/beta = w, such that A - w*B is singular. It is
  71. *> usually represented as the pair (alpha,beta), as there is a
  72. *> reasonable interpretation for beta=0 or for both being zero.
  73. *>
  74. *> A pair of matrices (S,T) is in generalized complex Schur form if T is
  75. *> upper triangular with non-negative diagonal and S is upper
  76. *> triangular.
  77. *> \endverbatim
  78. *
  79. * Arguments:
  80. * ==========
  81. *
  82. *> \param[in] JOBVSL
  83. *> \verbatim
  84. *> JOBVSL is CHARACTER*1
  85. *> = 'N': do not compute the left Schur vectors;
  86. *> = 'V': compute the left Schur vectors.
  87. *> \endverbatim
  88. *>
  89. *> \param[in] JOBVSR
  90. *> \verbatim
  91. *> JOBVSR is CHARACTER*1
  92. *> = 'N': do not compute the right Schur vectors;
  93. *> = 'V': compute the right Schur vectors.
  94. *> \endverbatim
  95. *>
  96. *> \param[in] SORT
  97. *> \verbatim
  98. *> SORT is CHARACTER*1
  99. *> Specifies whether or not to order the eigenvalues on the
  100. *> diagonal of the generalized Schur form.
  101. *> = 'N': Eigenvalues are not ordered;
  102. *> = 'S': Eigenvalues are ordered (see SELCTG).
  103. *> \endverbatim
  104. *>
  105. *> \param[in] SELCTG
  106. *> \verbatim
  107. *> SELCTG is a LOGICAL FUNCTION of two COMPLEX arguments
  108. *> SELCTG must be declared EXTERNAL in the calling subroutine.
  109. *> If SORT = 'N', SELCTG is not referenced.
  110. *> If SORT = 'S', SELCTG is used to select eigenvalues to sort
  111. *> to the top left of the Schur form.
  112. *> Note that a selected complex eigenvalue may no longer satisfy
  113. *> SELCTG(ALPHA(j),BETA(j)) = .TRUE. after ordering, since
  114. *> ordering may change the value of complex eigenvalues
  115. *> (especially if the eigenvalue is ill-conditioned), in this
  116. *> case INFO is set to N+3 see INFO below).
  117. *> \endverbatim
  118. *>
  119. *> \param[in] SENSE
  120. *> \verbatim
  121. *> SENSE is CHARACTER*1
  122. *> Determines which reciprocal condition numbers are computed.
  123. *> = 'N': None are computed;
  124. *> = 'E': Computed for average of selected eigenvalues only;
  125. *> = 'V': Computed for selected deflating subspaces only;
  126. *> = 'B': Computed for both.
  127. *> If SENSE = 'E', 'V', or 'B', SORT must equal 'S'.
  128. *> \endverbatim
  129. *>
  130. *> \param[in] N
  131. *> \verbatim
  132. *> N is INTEGER
  133. *> The order of the matrices A, B, VSL, and VSR. N >= 0.
  134. *> \endverbatim
  135. *>
  136. *> \param[in,out] A
  137. *> \verbatim
  138. *> A is COMPLEX array, dimension (LDA, N)
  139. *> On entry, the first of the pair of matrices.
  140. *> On exit, A has been overwritten by its generalized Schur
  141. *> form S.
  142. *> \endverbatim
  143. *>
  144. *> \param[in] LDA
  145. *> \verbatim
  146. *> LDA is INTEGER
  147. *> The leading dimension of A. LDA >= max(1,N).
  148. *> \endverbatim
  149. *>
  150. *> \param[in,out] B
  151. *> \verbatim
  152. *> B is COMPLEX array, dimension (LDB, N)
  153. *> On entry, the second of the pair of matrices.
  154. *> On exit, B has been overwritten by its generalized Schur
  155. *> form T.
  156. *> \endverbatim
  157. *>
  158. *> \param[in] LDB
  159. *> \verbatim
  160. *> LDB is INTEGER
  161. *> The leading dimension of B. LDB >= max(1,N).
  162. *> \endverbatim
  163. *>
  164. *> \param[out] SDIM
  165. *> \verbatim
  166. *> SDIM is INTEGER
  167. *> If SORT = 'N', SDIM = 0.
  168. *> If SORT = 'S', SDIM = number of eigenvalues (after sorting)
  169. *> for which SELCTG is true.
  170. *> \endverbatim
  171. *>
  172. *> \param[out] ALPHA
  173. *> \verbatim
  174. *> ALPHA is COMPLEX array, dimension (N)
  175. *> \endverbatim
  176. *>
  177. *> \param[out] BETA
  178. *> \verbatim
  179. *> BETA is COMPLEX array, dimension (N)
  180. *> On exit, ALPHA(j)/BETA(j), j=1,...,N, will be the
  181. *> generalized eigenvalues. ALPHA(j) and BETA(j),j=1,...,N are
  182. *> the diagonals of the complex Schur form (S,T). BETA(j) will
  183. *> be non-negative real.
  184. *>
  185. *> Note: the quotients ALPHA(j)/BETA(j) may easily over- or
  186. *> underflow, and BETA(j) may even be zero. Thus, the user
  187. *> should avoid naively computing the ratio alpha/beta.
  188. *> However, ALPHA will be always less than and usually
  189. *> comparable with norm(A) in magnitude, and BETA always less
  190. *> than and usually comparable with norm(B).
  191. *> \endverbatim
  192. *>
  193. *> \param[out] VSL
  194. *> \verbatim
  195. *> VSL is COMPLEX array, dimension (LDVSL,N)
  196. *> If JOBVSL = 'V', VSL will contain the left Schur vectors.
  197. *> Not referenced if JOBVSL = 'N'.
  198. *> \endverbatim
  199. *>
  200. *> \param[in] LDVSL
  201. *> \verbatim
  202. *> LDVSL is INTEGER
  203. *> The leading dimension of the matrix VSL. LDVSL >=1, and
  204. *> if JOBVSL = 'V', LDVSL >= N.
  205. *> \endverbatim
  206. *>
  207. *> \param[out] VSR
  208. *> \verbatim
  209. *> VSR is COMPLEX array, dimension (LDVSR,N)
  210. *> If JOBVSR = 'V', VSR will contain the right Schur vectors.
  211. *> Not referenced if JOBVSR = 'N'.
  212. *> \endverbatim
  213. *>
  214. *> \param[in] LDVSR
  215. *> \verbatim
  216. *> LDVSR is INTEGER
  217. *> The leading dimension of the matrix VSR. LDVSR >= 1, and
  218. *> if JOBVSR = 'V', LDVSR >= N.
  219. *> \endverbatim
  220. *>
  221. *> \param[out] RCONDE
  222. *> \verbatim
  223. *> RCONDE is REAL array, dimension ( 2 )
  224. *> If SENSE = 'E' or 'B', RCONDE(1) and RCONDE(2) contain the
  225. *> reciprocal condition numbers for the average of the selected
  226. *> eigenvalues.
  227. *> Not referenced if SENSE = 'N' or 'V'.
  228. *> \endverbatim
  229. *>
  230. *> \param[out] RCONDV
  231. *> \verbatim
  232. *> RCONDV is REAL array, dimension ( 2 )
  233. *> If SENSE = 'V' or 'B', RCONDV(1) and RCONDV(2) contain the
  234. *> reciprocal condition number for the selected deflating
  235. *> subspaces.
  236. *> Not referenced if SENSE = 'N' or 'E'.
  237. *> \endverbatim
  238. *>
  239. *> \param[out] WORK
  240. *> \verbatim
  241. *> WORK is COMPLEX array, dimension (MAX(1,LWORK))
  242. *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
  243. *> \endverbatim
  244. *>
  245. *> \param[in] LWORK
  246. *> \verbatim
  247. *> LWORK is INTEGER
  248. *> The dimension of the array WORK.
  249. *> If N = 0, LWORK >= 1, else if SENSE = 'E', 'V', or 'B',
  250. *> LWORK >= MAX(1,2*N,2*SDIM*(N-SDIM)), else
  251. *> LWORK >= MAX(1,2*N). Note that 2*SDIM*(N-SDIM) <= N*N/2.
  252. *> Note also that an error is only returned if
  253. *> LWORK < MAX(1,2*N), but if SENSE = 'E' or 'V' or 'B' this may
  254. *> not be large enough.
  255. *>
  256. *> If LWORK = -1, then a workspace query is assumed; the routine
  257. *> only calculates the bound on the optimal size of the WORK
  258. *> array and the minimum size of the IWORK array, returns these
  259. *> values as the first entries of the WORK and IWORK arrays, and
  260. *> no error message related to LWORK or LIWORK is issued by
  261. *> XERBLA.
  262. *> \endverbatim
  263. *>
  264. *> \param[out] RWORK
  265. *> \verbatim
  266. *> RWORK is REAL array, dimension ( 8*N )
  267. *> Real workspace.
  268. *> \endverbatim
  269. *>
  270. *> \param[out] IWORK
  271. *> \verbatim
  272. *> IWORK is INTEGER array, dimension (MAX(1,LIWORK))
  273. *> On exit, if INFO = 0, IWORK(1) returns the minimum LIWORK.
  274. *> \endverbatim
  275. *>
  276. *> \param[in] LIWORK
  277. *> \verbatim
  278. *> LIWORK is INTEGER
  279. *> The dimension of the array WORK.
  280. *> If SENSE = 'N' or N = 0, LIWORK >= 1, otherwise
  281. *> LIWORK >= N+2.
  282. *>
  283. *> If LIWORK = -1, then a workspace query is assumed; the
  284. *> routine only calculates the bound on the optimal size of the
  285. *> WORK array and the minimum size of the IWORK array, returns
  286. *> these values as the first entries of the WORK and IWORK
  287. *> arrays, and no error message related to LWORK or LIWORK is
  288. *> issued by XERBLA.
  289. *> \endverbatim
  290. *>
  291. *> \param[out] BWORK
  292. *> \verbatim
  293. *> BWORK is LOGICAL array, dimension (N)
  294. *> Not referenced if SORT = 'N'.
  295. *> \endverbatim
  296. *>
  297. *> \param[out] INFO
  298. *> \verbatim
  299. *> INFO is INTEGER
  300. *> = 0: successful exit
  301. *> < 0: if INFO = -i, the i-th argument had an illegal value.
  302. *> = 1,...,N:
  303. *> The QZ iteration failed. (A,B) are not in Schur
  304. *> form, but ALPHA(j) and BETA(j) should be correct for
  305. *> j=INFO+1,...,N.
  306. *> > N: =N+1: other than QZ iteration failed in CHGEQZ
  307. *> =N+2: after reordering, roundoff changed values of
  308. *> some complex eigenvalues so that leading
  309. *> eigenvalues in the Generalized Schur form no
  310. *> longer satisfy SELCTG=.TRUE. This could also
  311. *> be caused due to scaling.
  312. *> =N+3: reordering failed in CTGSEN.
  313. *> \endverbatim
  314. *
  315. * Authors:
  316. * ========
  317. *
  318. *> \author Univ. of Tennessee
  319. *> \author Univ. of California Berkeley
  320. *> \author Univ. of Colorado Denver
  321. *> \author NAG Ltd.
  322. *
  323. *> \ingroup ggesx
  324. *
  325. * =====================================================================
  326. SUBROUTINE CGGESX( JOBVSL, JOBVSR, SORT, SELCTG, SENSE, N, A, LDA,
  327. $ B, LDB, SDIM, ALPHA, BETA, VSL, LDVSL, VSR,
  328. $ LDVSR, RCONDE, RCONDV, WORK, LWORK, RWORK,
  329. $ IWORK, LIWORK, BWORK, INFO )
  330. *
  331. * -- LAPACK driver routine --
  332. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  333. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  334. *
  335. * .. Scalar Arguments ..
  336. CHARACTER JOBVSL, JOBVSR, SENSE, SORT
  337. INTEGER INFO, LDA, LDB, LDVSL, LDVSR, LIWORK, LWORK, N,
  338. $ SDIM
  339. * ..
  340. * .. Array Arguments ..
  341. LOGICAL BWORK( * )
  342. INTEGER IWORK( * )
  343. REAL RCONDE( 2 ), RCONDV( 2 ), RWORK( * )
  344. COMPLEX A( LDA, * ), ALPHA( * ), B( LDB, * ),
  345. $ BETA( * ), VSL( LDVSL, * ), VSR( LDVSR, * ),
  346. $ WORK( * )
  347. * ..
  348. * .. Function Arguments ..
  349. LOGICAL SELCTG
  350. EXTERNAL SELCTG
  351. * ..
  352. *
  353. * =====================================================================
  354. *
  355. * .. Parameters ..
  356. REAL ZERO, ONE
  357. PARAMETER ( ZERO = 0.0E+0, ONE = 1.0E+0 )
  358. COMPLEX CZERO, CONE
  359. PARAMETER ( CZERO = ( 0.0E+0, 0.0E+0 ),
  360. $ CONE = ( 1.0E+0, 0.0E+0 ) )
  361. * ..
  362. * .. Local Scalars ..
  363. LOGICAL CURSL, ILASCL, ILBSCL, ILVSL, ILVSR, LASTSL,
  364. $ LQUERY, WANTSB, WANTSE, WANTSN, WANTST, WANTSV
  365. INTEGER I, ICOLS, IERR, IHI, IJOB, IJOBVL, IJOBVR,
  366. $ ILEFT, ILO, IRIGHT, IROWS, IRWRK, ITAU, IWRK,
  367. $ LIWMIN, LWRK, MAXWRK, MINWRK
  368. REAL ANRM, ANRMTO, BIGNUM, BNRM, BNRMTO, EPS, PL,
  369. $ PR, SMLNUM
  370. * ..
  371. * .. Local Arrays ..
  372. REAL DIF( 2 )
  373. * ..
  374. * .. External Subroutines ..
  375. EXTERNAL CGEQRF, CGGBAK, CGGBAL, CGGHRD, CHGEQZ, CLACPY,
  376. $ CLASCL, CLASET, CTGSEN, CUNGQR, CUNMQR, XERBLA
  377. * ..
  378. * .. External Functions ..
  379. LOGICAL LSAME
  380. INTEGER ILAENV
  381. REAL CLANGE, SLAMCH, SROUNDUP_LWORK
  382. EXTERNAL LSAME, ILAENV, CLANGE, SLAMCH, SROUNDUP_LWORK
  383. * ..
  384. * .. Intrinsic Functions ..
  385. INTRINSIC MAX, SQRT
  386. * ..
  387. * .. Executable Statements ..
  388. *
  389. * Decode the input arguments
  390. *
  391. IF( LSAME( JOBVSL, 'N' ) ) THEN
  392. IJOBVL = 1
  393. ILVSL = .FALSE.
  394. ELSE IF( LSAME( JOBVSL, 'V' ) ) THEN
  395. IJOBVL = 2
  396. ILVSL = .TRUE.
  397. ELSE
  398. IJOBVL = -1
  399. ILVSL = .FALSE.
  400. END IF
  401. *
  402. IF( LSAME( JOBVSR, 'N' ) ) THEN
  403. IJOBVR = 1
  404. ILVSR = .FALSE.
  405. ELSE IF( LSAME( JOBVSR, 'V' ) ) THEN
  406. IJOBVR = 2
  407. ILVSR = .TRUE.
  408. ELSE
  409. IJOBVR = -1
  410. ILVSR = .FALSE.
  411. END IF
  412. *
  413. WANTST = LSAME( SORT, 'S' )
  414. WANTSN = LSAME( SENSE, 'N' )
  415. WANTSE = LSAME( SENSE, 'E' )
  416. WANTSV = LSAME( SENSE, 'V' )
  417. WANTSB = LSAME( SENSE, 'B' )
  418. LQUERY = ( LWORK.EQ.-1 .OR. LIWORK.EQ.-1 )
  419. IF( WANTSN ) THEN
  420. IJOB = 0
  421. ELSE IF( WANTSE ) THEN
  422. IJOB = 1
  423. ELSE IF( WANTSV ) THEN
  424. IJOB = 2
  425. ELSE IF( WANTSB ) THEN
  426. IJOB = 4
  427. END IF
  428. *
  429. * Test the input arguments
  430. *
  431. INFO = 0
  432. IF( IJOBVL.LE.0 ) THEN
  433. INFO = -1
  434. ELSE IF( IJOBVR.LE.0 ) THEN
  435. INFO = -2
  436. ELSE IF( ( .NOT.WANTST ) .AND. ( .NOT.LSAME( SORT, 'N' ) ) ) THEN
  437. INFO = -3
  438. ELSE IF( .NOT.( WANTSN .OR. WANTSE .OR. WANTSV .OR. WANTSB ) .OR.
  439. $ ( .NOT.WANTST .AND. .NOT.WANTSN ) ) THEN
  440. INFO = -5
  441. ELSE IF( N.LT.0 ) THEN
  442. INFO = -6
  443. ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
  444. INFO = -8
  445. ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
  446. INFO = -10
  447. ELSE IF( LDVSL.LT.1 .OR. ( ILVSL .AND. LDVSL.LT.N ) ) THEN
  448. INFO = -15
  449. ELSE IF( LDVSR.LT.1 .OR. ( ILVSR .AND. LDVSR.LT.N ) ) THEN
  450. INFO = -17
  451. END IF
  452. *
  453. * Compute workspace
  454. * (Note: Comments in the code beginning "Workspace:" describe the
  455. * minimal amount of workspace needed at that point in the code,
  456. * as well as the preferred amount for good performance.
  457. * NB refers to the optimal block size for the immediately
  458. * following subroutine, as returned by ILAENV.)
  459. *
  460. IF( INFO.EQ.0 ) THEN
  461. IF( N.GT.0) THEN
  462. MINWRK = 2*N
  463. MAXWRK = N*(1 + ILAENV( 1, 'CGEQRF', ' ', N, 1, N, 0 ) )
  464. MAXWRK = MAX( MAXWRK, N*( 1 +
  465. $ ILAENV( 1, 'CUNMQR', ' ', N, 1, N, -1 ) ) )
  466. IF( ILVSL ) THEN
  467. MAXWRK = MAX( MAXWRK, N*( 1 +
  468. $ ILAENV( 1, 'CUNGQR', ' ', N, 1, N, -1 ) ) )
  469. END IF
  470. LWRK = MAXWRK
  471. IF( IJOB.GE.1 )
  472. $ LWRK = MAX( LWRK, N*N/2 )
  473. ELSE
  474. MINWRK = 1
  475. MAXWRK = 1
  476. LWRK = 1
  477. END IF
  478. WORK( 1 ) = SROUNDUP_LWORK(LWRK)
  479. IF( WANTSN .OR. N.EQ.0 ) THEN
  480. LIWMIN = 1
  481. ELSE
  482. LIWMIN = N + 2
  483. END IF
  484. IWORK( 1 ) = LIWMIN
  485. *
  486. IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
  487. INFO = -21
  488. ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY) THEN
  489. INFO = -24
  490. END IF
  491. END IF
  492. *
  493. IF( INFO.NE.0 ) THEN
  494. CALL XERBLA( 'CGGESX', -INFO )
  495. RETURN
  496. ELSE IF (LQUERY) THEN
  497. RETURN
  498. END IF
  499. *
  500. * Quick return if possible
  501. *
  502. IF( N.EQ.0 ) THEN
  503. SDIM = 0
  504. RETURN
  505. END IF
  506. *
  507. * Get machine constants
  508. *
  509. EPS = SLAMCH( 'P' )
  510. SMLNUM = SLAMCH( 'S' )
  511. BIGNUM = ONE / SMLNUM
  512. SMLNUM = SQRT( SMLNUM ) / EPS
  513. BIGNUM = ONE / SMLNUM
  514. *
  515. * Scale A if max element outside range [SMLNUM,BIGNUM]
  516. *
  517. ANRM = CLANGE( 'M', N, N, A, LDA, RWORK )
  518. ILASCL = .FALSE.
  519. IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
  520. ANRMTO = SMLNUM
  521. ILASCL = .TRUE.
  522. ELSE IF( ANRM.GT.BIGNUM ) THEN
  523. ANRMTO = BIGNUM
  524. ILASCL = .TRUE.
  525. END IF
  526. IF( ILASCL )
  527. $ CALL CLASCL( 'G', 0, 0, ANRM, ANRMTO, N, N, A, LDA, IERR )
  528. *
  529. * Scale B if max element outside range [SMLNUM,BIGNUM]
  530. *
  531. BNRM = CLANGE( 'M', N, N, B, LDB, RWORK )
  532. ILBSCL = .FALSE.
  533. IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
  534. BNRMTO = SMLNUM
  535. ILBSCL = .TRUE.
  536. ELSE IF( BNRM.GT.BIGNUM ) THEN
  537. BNRMTO = BIGNUM
  538. ILBSCL = .TRUE.
  539. END IF
  540. IF( ILBSCL )
  541. $ CALL CLASCL( 'G', 0, 0, BNRM, BNRMTO, N, N, B, LDB, IERR )
  542. *
  543. * Permute the matrix to make it more nearly triangular
  544. * (Real Workspace: need 6*N)
  545. *
  546. ILEFT = 1
  547. IRIGHT = N + 1
  548. IRWRK = IRIGHT + N
  549. CALL CGGBAL( 'P', N, A, LDA, B, LDB, ILO, IHI, RWORK( ILEFT ),
  550. $ RWORK( IRIGHT ), RWORK( IRWRK ), IERR )
  551. *
  552. * Reduce B to triangular form (QR decomposition of B)
  553. * (Complex Workspace: need N, prefer N*NB)
  554. *
  555. IROWS = IHI + 1 - ILO
  556. ICOLS = N + 1 - ILO
  557. ITAU = 1
  558. IWRK = ITAU + IROWS
  559. CALL CGEQRF( IROWS, ICOLS, B( ILO, ILO ), LDB, WORK( ITAU ),
  560. $ WORK( IWRK ), LWORK+1-IWRK, IERR )
  561. *
  562. * Apply the unitary transformation to matrix A
  563. * (Complex Workspace: need N, prefer N*NB)
  564. *
  565. CALL CUNMQR( 'L', 'C', IROWS, ICOLS, IROWS, B( ILO, ILO ), LDB,
  566. $ WORK( ITAU ), A( ILO, ILO ), LDA, WORK( IWRK ),
  567. $ LWORK+1-IWRK, IERR )
  568. *
  569. * Initialize VSL
  570. * (Complex Workspace: need N, prefer N*NB)
  571. *
  572. IF( ILVSL ) THEN
  573. CALL CLASET( 'Full', N, N, CZERO, CONE, VSL, LDVSL )
  574. IF( IROWS.GT.1 ) THEN
  575. CALL CLACPY( 'L', IROWS-1, IROWS-1, B( ILO+1, ILO ), LDB,
  576. $ VSL( ILO+1, ILO ), LDVSL )
  577. END IF
  578. CALL CUNGQR( IROWS, IROWS, IROWS, VSL( ILO, ILO ), LDVSL,
  579. $ WORK( ITAU ), WORK( IWRK ), LWORK+1-IWRK, IERR )
  580. END IF
  581. *
  582. * Initialize VSR
  583. *
  584. IF( ILVSR )
  585. $ CALL CLASET( 'Full', N, N, CZERO, CONE, VSR, LDVSR )
  586. *
  587. * Reduce to generalized Hessenberg form
  588. * (Workspace: none needed)
  589. *
  590. CALL CGGHRD( JOBVSL, JOBVSR, N, ILO, IHI, A, LDA, B, LDB, VSL,
  591. $ LDVSL, VSR, LDVSR, IERR )
  592. *
  593. SDIM = 0
  594. *
  595. * Perform QZ algorithm, computing Schur vectors if desired
  596. * (Complex Workspace: need N)
  597. * (Real Workspace: need N)
  598. *
  599. IWRK = ITAU
  600. CALL CHGEQZ( 'S', JOBVSL, JOBVSR, N, ILO, IHI, A, LDA, B, LDB,
  601. $ ALPHA, BETA, VSL, LDVSL, VSR, LDVSR, WORK( IWRK ),
  602. $ LWORK+1-IWRK, RWORK( IRWRK ), IERR )
  603. IF( IERR.NE.0 ) THEN
  604. IF( IERR.GT.0 .AND. IERR.LE.N ) THEN
  605. INFO = IERR
  606. ELSE IF( IERR.GT.N .AND. IERR.LE.2*N ) THEN
  607. INFO = IERR - N
  608. ELSE
  609. INFO = N + 1
  610. END IF
  611. GO TO 40
  612. END IF
  613. *
  614. * Sort eigenvalues ALPHA/BETA and compute the reciprocal of
  615. * condition number(s)
  616. *
  617. IF( WANTST ) THEN
  618. *
  619. * Undo scaling on eigenvalues before SELCTGing
  620. *
  621. IF( ILASCL )
  622. $ CALL CLASCL( 'G', 0, 0, ANRMTO, ANRM, N, 1, ALPHA, N, IERR )
  623. IF( ILBSCL )
  624. $ CALL CLASCL( 'G', 0, 0, BNRMTO, BNRM, N, 1, BETA, N, IERR )
  625. *
  626. * Select eigenvalues
  627. *
  628. DO 10 I = 1, N
  629. BWORK( I ) = SELCTG( ALPHA( I ), BETA( I ) )
  630. 10 CONTINUE
  631. *
  632. * Reorder eigenvalues, transform Generalized Schur vectors, and
  633. * compute reciprocal condition numbers
  634. * (Complex Workspace: If IJOB >= 1, need MAX(1, 2*SDIM*(N-SDIM))
  635. * otherwise, need 1 )
  636. *
  637. CALL CTGSEN( IJOB, ILVSL, ILVSR, BWORK, N, A, LDA, B, LDB,
  638. $ ALPHA, BETA, VSL, LDVSL, VSR, LDVSR, SDIM, PL, PR,
  639. $ DIF, WORK( IWRK ), LWORK-IWRK+1, IWORK, LIWORK,
  640. $ IERR )
  641. *
  642. IF( IJOB.GE.1 )
  643. $ MAXWRK = MAX( MAXWRK, 2*SDIM*( N-SDIM ) )
  644. IF( IERR.EQ.-21 ) THEN
  645. *
  646. * not enough complex workspace
  647. *
  648. INFO = -21
  649. ELSE
  650. IF( IJOB.EQ.1 .OR. IJOB.EQ.4 ) THEN
  651. RCONDE( 1 ) = PL
  652. RCONDE( 2 ) = PR
  653. END IF
  654. IF( IJOB.EQ.2 .OR. IJOB.EQ.4 ) THEN
  655. RCONDV( 1 ) = DIF( 1 )
  656. RCONDV( 2 ) = DIF( 2 )
  657. END IF
  658. IF( IERR.EQ.1 )
  659. $ INFO = N + 3
  660. END IF
  661. *
  662. END IF
  663. *
  664. * Apply permutation to VSL and VSR
  665. * (Workspace: none needed)
  666. *
  667. IF( ILVSL )
  668. $ CALL CGGBAK( 'P', 'L', N, ILO, IHI, RWORK( ILEFT ),
  669. $ RWORK( IRIGHT ), N, VSL, LDVSL, IERR )
  670. *
  671. IF( ILVSR )
  672. $ CALL CGGBAK( 'P', 'R', N, ILO, IHI, RWORK( ILEFT ),
  673. $ RWORK( IRIGHT ), N, VSR, LDVSR, IERR )
  674. *
  675. * Undo scaling
  676. *
  677. IF( ILASCL ) THEN
  678. CALL CLASCL( 'U', 0, 0, ANRMTO, ANRM, N, N, A, LDA, IERR )
  679. CALL CLASCL( 'G', 0, 0, ANRMTO, ANRM, N, 1, ALPHA, N, IERR )
  680. END IF
  681. *
  682. IF( ILBSCL ) THEN
  683. CALL CLASCL( 'U', 0, 0, BNRMTO, BNRM, N, N, B, LDB, IERR )
  684. CALL CLASCL( 'G', 0, 0, BNRMTO, BNRM, N, 1, BETA, N, IERR )
  685. END IF
  686. *
  687. IF( WANTST ) THEN
  688. *
  689. * Check if reordering is correct
  690. *
  691. LASTSL = .TRUE.
  692. SDIM = 0
  693. DO 30 I = 1, N
  694. CURSL = SELCTG( ALPHA( I ), BETA( I ) )
  695. IF( CURSL )
  696. $ SDIM = SDIM + 1
  697. IF( CURSL .AND. .NOT.LASTSL )
  698. $ INFO = N + 2
  699. LASTSL = CURSL
  700. 30 CONTINUE
  701. *
  702. END IF
  703. *
  704. 40 CONTINUE
  705. *
  706. WORK( 1 ) = SROUNDUP_LWORK(MAXWRK)
  707. IWORK( 1 ) = LIWMIN
  708. *
  709. RETURN
  710. *
  711. * End of CGGESX
  712. *
  713. END