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.

sggevx.f 29 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. *> \brief <b> SGGEVX computes the eigenvalues and, optionally, the left and/or right eigenvectors 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 SGGEVX + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sggevx.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sggevx.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sggevx.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE SGGEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, B, LDB,
  22. * ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, ILO,
  23. * IHI, LSCALE, RSCALE, ABNRM, BBNRM, RCONDE,
  24. * RCONDV, WORK, LWORK, IWORK, BWORK, INFO )
  25. *
  26. * .. Scalar Arguments ..
  27. * CHARACTER BALANC, JOBVL, JOBVR, SENSE
  28. * INTEGER IHI, ILO, INFO, LDA, LDB, LDVL, LDVR, LWORK, N
  29. * REAL ABNRM, BBNRM
  30. * ..
  31. * .. Array Arguments ..
  32. * LOGICAL BWORK( * )
  33. * INTEGER IWORK( * )
  34. * REAL A( LDA, * ), ALPHAI( * ), ALPHAR( * ),
  35. * $ B( LDB, * ), BETA( * ), LSCALE( * ),
  36. * $ RCONDE( * ), RCONDV( * ), RSCALE( * ),
  37. * $ VL( LDVL, * ), VR( LDVR, * ), WORK( * )
  38. * ..
  39. *
  40. *
  41. *> \par Purpose:
  42. * =============
  43. *>
  44. *> \verbatim
  45. *>
  46. *> SGGEVX computes for a pair of N-by-N real nonsymmetric matrices (A,B)
  47. *> the generalized eigenvalues, and optionally, the left and/or right
  48. *> generalized eigenvectors.
  49. *>
  50. *> Optionally also, it computes a balancing transformation to improve
  51. *> the conditioning of the eigenvalues and eigenvectors (ILO, IHI,
  52. *> LSCALE, RSCALE, ABNRM, and BBNRM), reciprocal condition numbers for
  53. *> the eigenvalues (RCONDE), and reciprocal condition numbers for the
  54. *> right eigenvectors (RCONDV).
  55. *>
  56. *> A generalized eigenvalue for a pair of matrices (A,B) is a scalar
  57. *> lambda or a ratio alpha/beta = lambda, such that A - lambda*B is
  58. *> singular. It is usually represented as the pair (alpha,beta), as
  59. *> there is a reasonable interpretation for beta=0, and even for both
  60. *> being zero.
  61. *>
  62. *> The right eigenvector v(j) corresponding to the eigenvalue lambda(j)
  63. *> of (A,B) satisfies
  64. *>
  65. *> A * v(j) = lambda(j) * B * v(j) .
  66. *>
  67. *> The left eigenvector u(j) corresponding to the eigenvalue lambda(j)
  68. *> of (A,B) satisfies
  69. *>
  70. *> u(j)**H * A = lambda(j) * u(j)**H * B.
  71. *>
  72. *> where u(j)**H is the conjugate-transpose of u(j).
  73. *>
  74. *> \endverbatim
  75. *
  76. * Arguments:
  77. * ==========
  78. *
  79. *> \param[in] BALANC
  80. *> \verbatim
  81. *> BALANC is CHARACTER*1
  82. *> Specifies the balance option to be performed.
  83. *> = 'N': do not diagonally scale or permute;
  84. *> = 'P': permute only;
  85. *> = 'S': scale only;
  86. *> = 'B': both permute and scale.
  87. *> Computed reciprocal condition numbers will be for the
  88. *> matrices after permuting and/or balancing. Permuting does
  89. *> not change condition numbers (in exact arithmetic), but
  90. *> balancing does.
  91. *> \endverbatim
  92. *>
  93. *> \param[in] JOBVL
  94. *> \verbatim
  95. *> JOBVL is CHARACTER*1
  96. *> = 'N': do not compute the left generalized eigenvectors;
  97. *> = 'V': compute the left generalized eigenvectors.
  98. *> \endverbatim
  99. *>
  100. *> \param[in] JOBVR
  101. *> \verbatim
  102. *> JOBVR is CHARACTER*1
  103. *> = 'N': do not compute the right generalized eigenvectors;
  104. *> = 'V': compute the right generalized eigenvectors.
  105. *> \endverbatim
  106. *>
  107. *> \param[in] SENSE
  108. *> \verbatim
  109. *> SENSE is CHARACTER*1
  110. *> Determines which reciprocal condition numbers are computed.
  111. *> = 'N': none are computed;
  112. *> = 'E': computed for eigenvalues only;
  113. *> = 'V': computed for eigenvectors only;
  114. *> = 'B': computed for eigenvalues and eigenvectors.
  115. *> \endverbatim
  116. *>
  117. *> \param[in] N
  118. *> \verbatim
  119. *> N is INTEGER
  120. *> The order of the matrices A, B, VL, and VR. N >= 0.
  121. *> \endverbatim
  122. *>
  123. *> \param[in,out] A
  124. *> \verbatim
  125. *> A is REAL array, dimension (LDA, N)
  126. *> On entry, the matrix A in the pair (A,B).
  127. *> On exit, A has been overwritten. If JOBVL='V' or JOBVR='V'
  128. *> or both, then A contains the first part of the real Schur
  129. *> form of the "balanced" versions of the input A and B.
  130. *> \endverbatim
  131. *>
  132. *> \param[in] LDA
  133. *> \verbatim
  134. *> LDA is INTEGER
  135. *> The leading dimension of A. LDA >= max(1,N).
  136. *> \endverbatim
  137. *>
  138. *> \param[in,out] B
  139. *> \verbatim
  140. *> B is REAL array, dimension (LDB, N)
  141. *> On entry, the matrix B in the pair (A,B).
  142. *> On exit, B has been overwritten. If JOBVL='V' or JOBVR='V'
  143. *> or both, then B contains the second part of the real Schur
  144. *> form of the "balanced" versions of the input A and B.
  145. *> \endverbatim
  146. *>
  147. *> \param[in] LDB
  148. *> \verbatim
  149. *> LDB is INTEGER
  150. *> The leading dimension of B. LDB >= max(1,N).
  151. *> \endverbatim
  152. *>
  153. *> \param[out] ALPHAR
  154. *> \verbatim
  155. *> ALPHAR is REAL array, dimension (N)
  156. *> \endverbatim
  157. *>
  158. *> \param[out] ALPHAI
  159. *> \verbatim
  160. *> ALPHAI is REAL array, dimension (N)
  161. *> \endverbatim
  162. *>
  163. *> \param[out] BETA
  164. *> \verbatim
  165. *> BETA is REAL array, dimension (N)
  166. *> On exit, (ALPHAR(j) + ALPHAI(j)*i)/BETA(j), j=1,...,N, will
  167. *> be the generalized eigenvalues. If ALPHAI(j) is zero, then
  168. *> the j-th eigenvalue is real; if positive, then the j-th and
  169. *> (j+1)-st eigenvalues are a complex conjugate pair, with
  170. *> ALPHAI(j+1) negative.
  171. *>
  172. *> Note: the quotients ALPHAR(j)/BETA(j) and ALPHAI(j)/BETA(j)
  173. *> may easily over- or underflow, and BETA(j) may even be zero.
  174. *> Thus, the user should avoid naively computing the ratio
  175. *> ALPHA/BETA. However, ALPHAR and ALPHAI will be always less
  176. *> than and usually comparable with norm(A) in magnitude, and
  177. *> BETA always less than and usually comparable with norm(B).
  178. *> \endverbatim
  179. *>
  180. *> \param[out] VL
  181. *> \verbatim
  182. *> VL is REAL array, dimension (LDVL,N)
  183. *> If JOBVL = 'V', the left eigenvectors u(j) are stored one
  184. *> after another in the columns of VL, in the same order as
  185. *> their eigenvalues. If the j-th eigenvalue is real, then
  186. *> u(j) = VL(:,j), the j-th column of VL. If the j-th and
  187. *> (j+1)-th eigenvalues form a complex conjugate pair, then
  188. *> u(j) = VL(:,j)+i*VL(:,j+1) and u(j+1) = VL(:,j)-i*VL(:,j+1).
  189. *> Each eigenvector will be scaled so the largest component have
  190. *> abs(real part) + abs(imag. part) = 1.
  191. *> Not referenced if JOBVL = 'N'.
  192. *> \endverbatim
  193. *>
  194. *> \param[in] LDVL
  195. *> \verbatim
  196. *> LDVL is INTEGER
  197. *> The leading dimension of the matrix VL. LDVL >= 1, and
  198. *> if JOBVL = 'V', LDVL >= N.
  199. *> \endverbatim
  200. *>
  201. *> \param[out] VR
  202. *> \verbatim
  203. *> VR is REAL array, dimension (LDVR,N)
  204. *> If JOBVR = 'V', the right eigenvectors v(j) are stored one
  205. *> after another in the columns of VR, in the same order as
  206. *> their eigenvalues. If the j-th eigenvalue is real, then
  207. *> v(j) = VR(:,j), the j-th column of VR. If the j-th and
  208. *> (j+1)-th eigenvalues form a complex conjugate pair, then
  209. *> v(j) = VR(:,j)+i*VR(:,j+1) and v(j+1) = VR(:,j)-i*VR(:,j+1).
  210. *> Each eigenvector will be scaled so the largest component have
  211. *> abs(real part) + abs(imag. part) = 1.
  212. *> Not referenced if JOBVR = 'N'.
  213. *> \endverbatim
  214. *>
  215. *> \param[in] LDVR
  216. *> \verbatim
  217. *> LDVR is INTEGER
  218. *> The leading dimension of the matrix VR. LDVR >= 1, and
  219. *> if JOBVR = 'V', LDVR >= N.
  220. *> \endverbatim
  221. *>
  222. *> \param[out] ILO
  223. *> \verbatim
  224. *> ILO is INTEGER
  225. *> \endverbatim
  226. *>
  227. *> \param[out] IHI
  228. *> \verbatim
  229. *> IHI is INTEGER
  230. *> ILO and IHI are integer values such that on exit
  231. *> A(i,j) = 0 and B(i,j) = 0 if i > j and
  232. *> j = 1,...,ILO-1 or i = IHI+1,...,N.
  233. *> If BALANC = 'N' or 'S', ILO = 1 and IHI = N.
  234. *> \endverbatim
  235. *>
  236. *> \param[out] LSCALE
  237. *> \verbatim
  238. *> LSCALE is REAL array, dimension (N)
  239. *> Details of the permutations and scaling factors applied
  240. *> to the left side of A and B. If PL(j) is the index of the
  241. *> row interchanged with row j, and DL(j) is the scaling
  242. *> factor applied to row j, then
  243. *> LSCALE(j) = PL(j) for j = 1,...,ILO-1
  244. *> = DL(j) for j = ILO,...,IHI
  245. *> = PL(j) for j = IHI+1,...,N.
  246. *> The order in which the interchanges are made is N to IHI+1,
  247. *> then 1 to ILO-1.
  248. *> \endverbatim
  249. *>
  250. *> \param[out] RSCALE
  251. *> \verbatim
  252. *> RSCALE is REAL array, dimension (N)
  253. *> Details of the permutations and scaling factors applied
  254. *> to the right side of A and B. If PR(j) is the index of the
  255. *> column interchanged with column j, and DR(j) is the scaling
  256. *> factor applied to column j, then
  257. *> RSCALE(j) = PR(j) for j = 1,...,ILO-1
  258. *> = DR(j) for j = ILO,...,IHI
  259. *> = PR(j) for j = IHI+1,...,N
  260. *> The order in which the interchanges are made is N to IHI+1,
  261. *> then 1 to ILO-1.
  262. *> \endverbatim
  263. *>
  264. *> \param[out] ABNRM
  265. *> \verbatim
  266. *> ABNRM is REAL
  267. *> The one-norm of the balanced matrix A.
  268. *> \endverbatim
  269. *>
  270. *> \param[out] BBNRM
  271. *> \verbatim
  272. *> BBNRM is REAL
  273. *> The one-norm of the balanced matrix B.
  274. *> \endverbatim
  275. *>
  276. *> \param[out] RCONDE
  277. *> \verbatim
  278. *> RCONDE is REAL array, dimension (N)
  279. *> If SENSE = 'E' or 'B', the reciprocal condition numbers of
  280. *> the eigenvalues, stored in consecutive elements of the array.
  281. *> For a complex conjugate pair of eigenvalues two consecutive
  282. *> elements of RCONDE are set to the same value. Thus RCONDE(j),
  283. *> RCONDV(j), and the j-th columns of VL and VR all correspond
  284. *> to the j-th eigenpair.
  285. *> If SENSE = 'N' or 'V', RCONDE is not referenced.
  286. *> \endverbatim
  287. *>
  288. *> \param[out] RCONDV
  289. *> \verbatim
  290. *> RCONDV is REAL array, dimension (N)
  291. *> If SENSE = 'V' or 'B', the estimated reciprocal condition
  292. *> numbers of the eigenvectors, stored in consecutive elements
  293. *> of the array. For a complex eigenvector two consecutive
  294. *> elements of RCONDV are set to the same value. If the
  295. *> eigenvalues cannot be reordered to compute RCONDV(j),
  296. *> RCONDV(j) is set to 0; this can only occur when the true
  297. *> value would be very small anyway.
  298. *> If SENSE = 'N' or 'E', RCONDV is not referenced.
  299. *> \endverbatim
  300. *>
  301. *> \param[out] WORK
  302. *> \verbatim
  303. *> WORK is REAL array, dimension (MAX(1,LWORK))
  304. *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
  305. *> \endverbatim
  306. *>
  307. *> \param[in] LWORK
  308. *> \verbatim
  309. *> LWORK is INTEGER
  310. *> The dimension of the array WORK. LWORK >= max(1,2*N).
  311. *> If BALANC = 'S' or 'B', or JOBVL = 'V', or JOBVR = 'V',
  312. *> LWORK >= max(1,6*N).
  313. *> If SENSE = 'E', LWORK >= max(1,10*N).
  314. *> If SENSE = 'V' or 'B', LWORK >= 2*N*N+8*N+16.
  315. *>
  316. *> If LWORK = -1, then a workspace query is assumed; the routine
  317. *> only calculates the optimal size of the WORK array, returns
  318. *> this value as the first entry of the WORK array, and no error
  319. *> message related to LWORK is issued by XERBLA.
  320. *> \endverbatim
  321. *>
  322. *> \param[out] IWORK
  323. *> \verbatim
  324. *> IWORK is INTEGER array, dimension (N+6)
  325. *> If SENSE = 'E', IWORK is not referenced.
  326. *> \endverbatim
  327. *>
  328. *> \param[out] BWORK
  329. *> \verbatim
  330. *> BWORK is LOGICAL array, dimension (N)
  331. *> If SENSE = 'N', BWORK is not referenced.
  332. *> \endverbatim
  333. *>
  334. *> \param[out] INFO
  335. *> \verbatim
  336. *> INFO is INTEGER
  337. *> = 0: successful exit
  338. *> < 0: if INFO = -i, the i-th argument had an illegal value.
  339. *> = 1,...,N:
  340. *> The QZ iteration failed. No eigenvectors have been
  341. *> calculated, but ALPHAR(j), ALPHAI(j), and BETA(j)
  342. *> should be correct for j=INFO+1,...,N.
  343. *> > N: =N+1: other than QZ iteration failed in SHGEQZ.
  344. *> =N+2: error return from STGEVC.
  345. *> \endverbatim
  346. *
  347. * Authors:
  348. * ========
  349. *
  350. *> \author Univ. of Tennessee
  351. *> \author Univ. of California Berkeley
  352. *> \author Univ. of Colorado Denver
  353. *> \author NAG Ltd.
  354. *
  355. *> \ingroup ggevx
  356. *
  357. *> \par Further Details:
  358. * =====================
  359. *>
  360. *> \verbatim
  361. *>
  362. *> Balancing a matrix pair (A,B) includes, first, permuting rows and
  363. *> columns to isolate eigenvalues, second, applying diagonal similarity
  364. *> transformation to the rows and columns to make the rows and columns
  365. *> as close in norm as possible. The computed reciprocal condition
  366. *> numbers correspond to the balanced matrix. Permuting rows and columns
  367. *> will not change the condition numbers (in exact arithmetic) but
  368. *> diagonal scaling will. For further explanation of balancing, see
  369. *> section 4.11.1.2 of LAPACK Users' Guide.
  370. *>
  371. *> An approximate error bound on the chordal distance between the i-th
  372. *> computed generalized eigenvalue w and the corresponding exact
  373. *> eigenvalue lambda is
  374. *>
  375. *> chord(w, lambda) <= EPS * norm(ABNRM, BBNRM) / RCONDE(I)
  376. *>
  377. *> An approximate error bound for the angle between the i-th computed
  378. *> eigenvector VL(i) or VR(i) is given by
  379. *>
  380. *> EPS * norm(ABNRM, BBNRM) / DIF(i).
  381. *>
  382. *> For further explanation of the reciprocal condition numbers RCONDE
  383. *> and RCONDV, see section 4.11 of LAPACK User's Guide.
  384. *> \endverbatim
  385. *>
  386. * =====================================================================
  387. SUBROUTINE SGGEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, B, LDB,
  388. $ ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, ILO,
  389. $ IHI, LSCALE, RSCALE, ABNRM, BBNRM, RCONDE,
  390. $ RCONDV, WORK, LWORK, IWORK, BWORK, INFO )
  391. *
  392. * -- LAPACK driver routine --
  393. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  394. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  395. *
  396. * .. Scalar Arguments ..
  397. CHARACTER BALANC, JOBVL, JOBVR, SENSE
  398. INTEGER IHI, ILO, INFO, LDA, LDB, LDVL, LDVR, LWORK, N
  399. REAL ABNRM, BBNRM
  400. * ..
  401. * .. Array Arguments ..
  402. LOGICAL BWORK( * )
  403. INTEGER IWORK( * )
  404. REAL A( LDA, * ), ALPHAI( * ), ALPHAR( * ),
  405. $ B( LDB, * ), BETA( * ), LSCALE( * ),
  406. $ RCONDE( * ), RCONDV( * ), RSCALE( * ),
  407. $ VL( LDVL, * ), VR( LDVR, * ), WORK( * )
  408. * ..
  409. *
  410. * =====================================================================
  411. *
  412. * .. Parameters ..
  413. REAL ZERO, ONE
  414. PARAMETER ( ZERO = 0.0E+0, ONE = 1.0E+0 )
  415. * ..
  416. * .. Local Scalars ..
  417. LOGICAL ILASCL, ILBSCL, ILV, ILVL, ILVR, LQUERY, NOSCL,
  418. $ PAIR, WANTSB, WANTSE, WANTSN, WANTSV
  419. CHARACTER CHTEMP
  420. INTEGER I, ICOLS, IERR, IJOBVL, IJOBVR, IN, IROWS,
  421. $ ITAU, IWRK, IWRK1, J, JC, JR, M, MAXWRK,
  422. $ MINWRK, MM
  423. REAL ANRM, ANRMTO, BIGNUM, BNRM, BNRMTO, EPS,
  424. $ SMLNUM, TEMP
  425. * ..
  426. * .. Local Arrays ..
  427. LOGICAL LDUMMA( 1 )
  428. * ..
  429. * .. External Subroutines ..
  430. EXTERNAL SGEQRF, SGGBAK, SGGBAL, SGGHRD, SHGEQZ, SLACPY,
  431. $ SLASCL, SLASET, SORGQR, SORMQR, STGEVC, STGSNA,
  432. $ XERBLA
  433. * ..
  434. * .. External Functions ..
  435. LOGICAL LSAME
  436. INTEGER ILAENV
  437. REAL SLAMCH, SLANGE, SROUNDUP_LWORK
  438. EXTERNAL LSAME, ILAENV, SLAMCH, SLANGE, SROUNDUP_LWORK
  439. * ..
  440. * .. Intrinsic Functions ..
  441. INTRINSIC ABS, MAX, SQRT
  442. * ..
  443. * .. Executable Statements ..
  444. *
  445. * Decode the input arguments
  446. *
  447. IF( LSAME( JOBVL, 'N' ) ) THEN
  448. IJOBVL = 1
  449. ILVL = .FALSE.
  450. ELSE IF( LSAME( JOBVL, 'V' ) ) THEN
  451. IJOBVL = 2
  452. ILVL = .TRUE.
  453. ELSE
  454. IJOBVL = -1
  455. ILVL = .FALSE.
  456. END IF
  457. *
  458. IF( LSAME( JOBVR, 'N' ) ) THEN
  459. IJOBVR = 1
  460. ILVR = .FALSE.
  461. ELSE IF( LSAME( JOBVR, 'V' ) ) THEN
  462. IJOBVR = 2
  463. ILVR = .TRUE.
  464. ELSE
  465. IJOBVR = -1
  466. ILVR = .FALSE.
  467. END IF
  468. ILV = ILVL .OR. ILVR
  469. *
  470. NOSCL = LSAME( BALANC, 'N' ) .OR. LSAME( BALANC, 'P' )
  471. WANTSN = LSAME( SENSE, 'N' )
  472. WANTSE = LSAME( SENSE, 'E' )
  473. WANTSV = LSAME( SENSE, 'V' )
  474. WANTSB = LSAME( SENSE, 'B' )
  475. *
  476. * Test the input arguments
  477. *
  478. INFO = 0
  479. LQUERY = ( LWORK.EQ.-1 )
  480. IF( .NOT.( NOSCL .OR. LSAME( BALANC, 'S' ) .OR.
  481. $ LSAME( BALANC, 'B' ) ) ) THEN
  482. INFO = -1
  483. ELSE IF( IJOBVL.LE.0 ) THEN
  484. INFO = -2
  485. ELSE IF( IJOBVR.LE.0 ) THEN
  486. INFO = -3
  487. ELSE IF( .NOT.( WANTSN .OR. WANTSE .OR. WANTSB .OR. WANTSV ) )
  488. $ THEN
  489. INFO = -4
  490. ELSE IF( N.LT.0 ) THEN
  491. INFO = -5
  492. ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
  493. INFO = -7
  494. ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
  495. INFO = -9
  496. ELSE IF( LDVL.LT.1 .OR. ( ILVL .AND. LDVL.LT.N ) ) THEN
  497. INFO = -14
  498. ELSE IF( LDVR.LT.1 .OR. ( ILVR .AND. LDVR.LT.N ) ) THEN
  499. INFO = -16
  500. END IF
  501. *
  502. * Compute workspace
  503. * (Note: Comments in the code beginning "Workspace:" describe the
  504. * minimal amount of workspace needed at that point in the code,
  505. * as well as the preferred amount for good performance.
  506. * NB refers to the optimal block size for the immediately
  507. * following subroutine, as returned by ILAENV. The workspace is
  508. * computed assuming ILO = 1 and IHI = N, the worst case.)
  509. *
  510. IF( INFO.EQ.0 ) THEN
  511. IF( N.EQ.0 ) THEN
  512. MINWRK = 1
  513. MAXWRK = 1
  514. ELSE
  515. IF( NOSCL .AND. .NOT.ILV ) THEN
  516. MINWRK = 2*N
  517. ELSE
  518. MINWRK = 6*N
  519. END IF
  520. IF( WANTSE ) THEN
  521. MINWRK = 10*N
  522. ELSE IF( WANTSV .OR. WANTSB ) THEN
  523. MINWRK = 2*N*( N + 4 ) + 16
  524. END IF
  525. MAXWRK = MINWRK
  526. MAXWRK = MAX( MAXWRK,
  527. $ N + N*ILAENV( 1, 'SGEQRF', ' ', N, 1, N, 0 ) )
  528. MAXWRK = MAX( MAXWRK,
  529. $ N + N*ILAENV( 1, 'SORMQR', ' ', N, 1, N, 0 ) )
  530. IF( ILVL ) THEN
  531. MAXWRK = MAX( MAXWRK, N +
  532. $ N*ILAENV( 1, 'SORGQR', ' ', N, 1, N, 0 ) )
  533. END IF
  534. END IF
  535. WORK( 1 ) = SROUNDUP_LWORK(MAXWRK)
  536. *
  537. IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
  538. INFO = -26
  539. END IF
  540. END IF
  541. *
  542. IF( INFO.NE.0 ) THEN
  543. CALL XERBLA( 'SGGEVX', -INFO )
  544. RETURN
  545. ELSE IF( LQUERY ) THEN
  546. RETURN
  547. END IF
  548. *
  549. * Quick return if possible
  550. *
  551. IF( N.EQ.0 )
  552. $ RETURN
  553. *
  554. *
  555. * Get machine constants
  556. *
  557. EPS = SLAMCH( 'P' )
  558. SMLNUM = SLAMCH( 'S' )
  559. BIGNUM = ONE / SMLNUM
  560. SMLNUM = SQRT( SMLNUM ) / EPS
  561. BIGNUM = ONE / SMLNUM
  562. *
  563. * Scale A if max element outside range [SMLNUM,BIGNUM]
  564. *
  565. ANRM = SLANGE( 'M', N, N, A, LDA, WORK )
  566. ILASCL = .FALSE.
  567. IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
  568. ANRMTO = SMLNUM
  569. ILASCL = .TRUE.
  570. ELSE IF( ANRM.GT.BIGNUM ) THEN
  571. ANRMTO = BIGNUM
  572. ILASCL = .TRUE.
  573. END IF
  574. IF( ILASCL )
  575. $ CALL SLASCL( 'G', 0, 0, ANRM, ANRMTO, N, N, A, LDA, IERR )
  576. *
  577. * Scale B if max element outside range [SMLNUM,BIGNUM]
  578. *
  579. BNRM = SLANGE( 'M', N, N, B, LDB, WORK )
  580. ILBSCL = .FALSE.
  581. IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
  582. BNRMTO = SMLNUM
  583. ILBSCL = .TRUE.
  584. ELSE IF( BNRM.GT.BIGNUM ) THEN
  585. BNRMTO = BIGNUM
  586. ILBSCL = .TRUE.
  587. END IF
  588. IF( ILBSCL )
  589. $ CALL SLASCL( 'G', 0, 0, BNRM, BNRMTO, N, N, B, LDB, IERR )
  590. *
  591. * Permute and/or balance the matrix pair (A,B)
  592. * (Workspace: need 6*N if BALANC = 'S' or 'B', 1 otherwise)
  593. *
  594. CALL SGGBAL( BALANC, N, A, LDA, B, LDB, ILO, IHI, LSCALE, RSCALE,
  595. $ WORK, IERR )
  596. *
  597. * Compute ABNRM and BBNRM
  598. *
  599. ABNRM = SLANGE( '1', N, N, A, LDA, WORK( 1 ) )
  600. IF( ILASCL ) THEN
  601. WORK( 1 ) = ABNRM
  602. CALL SLASCL( 'G', 0, 0, ANRMTO, ANRM, 1, 1, WORK( 1 ), 1,
  603. $ IERR )
  604. ABNRM = WORK( 1 )
  605. END IF
  606. *
  607. BBNRM = SLANGE( '1', N, N, B, LDB, WORK( 1 ) )
  608. IF( ILBSCL ) THEN
  609. WORK( 1 ) = BBNRM
  610. CALL SLASCL( 'G', 0, 0, BNRMTO, BNRM, 1, 1, WORK( 1 ), 1,
  611. $ IERR )
  612. BBNRM = WORK( 1 )
  613. END IF
  614. *
  615. * Reduce B to triangular form (QR decomposition of B)
  616. * (Workspace: need N, prefer N*NB )
  617. *
  618. IROWS = IHI + 1 - ILO
  619. IF( ILV .OR. .NOT.WANTSN ) THEN
  620. ICOLS = N + 1 - ILO
  621. ELSE
  622. ICOLS = IROWS
  623. END IF
  624. ITAU = 1
  625. IWRK = ITAU + IROWS
  626. CALL SGEQRF( IROWS, ICOLS, B( ILO, ILO ), LDB, WORK( ITAU ),
  627. $ WORK( IWRK ), LWORK+1-IWRK, IERR )
  628. *
  629. * Apply the orthogonal transformation to A
  630. * (Workspace: need N, prefer N*NB)
  631. *
  632. CALL SORMQR( 'L', 'T', IROWS, ICOLS, IROWS, B( ILO, ILO ), LDB,
  633. $ WORK( ITAU ), A( ILO, ILO ), LDA, WORK( IWRK ),
  634. $ LWORK+1-IWRK, IERR )
  635. *
  636. * Initialize VL and/or VR
  637. * (Workspace: need N, prefer N*NB)
  638. *
  639. IF( ILVL ) THEN
  640. CALL SLASET( 'Full', N, N, ZERO, ONE, VL, LDVL )
  641. IF( IROWS.GT.1 ) THEN
  642. CALL SLACPY( 'L', IROWS-1, IROWS-1, B( ILO+1, ILO ), LDB,
  643. $ VL( ILO+1, ILO ), LDVL )
  644. END IF
  645. CALL SORGQR( IROWS, IROWS, IROWS, VL( ILO, ILO ), LDVL,
  646. $ WORK( ITAU ), WORK( IWRK ), LWORK+1-IWRK, IERR )
  647. END IF
  648. *
  649. IF( ILVR )
  650. $ CALL SLASET( 'Full', N, N, ZERO, ONE, VR, LDVR )
  651. *
  652. * Reduce to generalized Hessenberg form
  653. * (Workspace: none needed)
  654. *
  655. IF( ILV .OR. .NOT.WANTSN ) THEN
  656. *
  657. * Eigenvectors requested -- work on whole matrix.
  658. *
  659. CALL SGGHRD( JOBVL, JOBVR, N, ILO, IHI, A, LDA, B, LDB, VL,
  660. $ LDVL, VR, LDVR, IERR )
  661. ELSE
  662. CALL SGGHRD( 'N', 'N', IROWS, 1, IROWS, A( ILO, ILO ), LDA,
  663. $ B( ILO, ILO ), LDB, VL, LDVL, VR, LDVR, IERR )
  664. END IF
  665. *
  666. * Perform QZ algorithm (Compute eigenvalues, and optionally, the
  667. * Schur forms and Schur vectors)
  668. * (Workspace: need N)
  669. *
  670. IF( ILV .OR. .NOT.WANTSN ) THEN
  671. CHTEMP = 'S'
  672. ELSE
  673. CHTEMP = 'E'
  674. END IF
  675. *
  676. CALL SHGEQZ( CHTEMP, JOBVL, JOBVR, N, ILO, IHI, A, LDA, B, LDB,
  677. $ ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, WORK,
  678. $ LWORK, IERR )
  679. IF( IERR.NE.0 ) THEN
  680. IF( IERR.GT.0 .AND. IERR.LE.N ) THEN
  681. INFO = IERR
  682. ELSE IF( IERR.GT.N .AND. IERR.LE.2*N ) THEN
  683. INFO = IERR - N
  684. ELSE
  685. INFO = N + 1
  686. END IF
  687. GO TO 130
  688. END IF
  689. *
  690. * Compute Eigenvectors and estimate condition numbers if desired
  691. * (Workspace: STGEVC: need 6*N
  692. * STGSNA: need 2*N*(N+2)+16 if SENSE = 'V' or 'B',
  693. * need N otherwise )
  694. *
  695. IF( ILV .OR. .NOT.WANTSN ) THEN
  696. IF( ILV ) THEN
  697. IF( ILVL ) THEN
  698. IF( ILVR ) THEN
  699. CHTEMP = 'B'
  700. ELSE
  701. CHTEMP = 'L'
  702. END IF
  703. ELSE
  704. CHTEMP = 'R'
  705. END IF
  706. *
  707. CALL STGEVC( CHTEMP, 'B', LDUMMA, N, A, LDA, B, LDB, VL,
  708. $ LDVL, VR, LDVR, N, IN, WORK, IERR )
  709. IF( IERR.NE.0 ) THEN
  710. INFO = N + 2
  711. GO TO 130
  712. END IF
  713. END IF
  714. *
  715. IF( .NOT.WANTSN ) THEN
  716. *
  717. * compute eigenvectors (STGEVC) and estimate condition
  718. * numbers (STGSNA). Note that the definition of the condition
  719. * number is not invariant under transformation (u,v) to
  720. * (Q*u, Z*v), where (u,v) are eigenvectors of the generalized
  721. * Schur form (S,T), Q and Z are orthogonal matrices. In order
  722. * to avoid using extra 2*N*N workspace, we have to recalculate
  723. * eigenvectors and estimate one condition numbers at a time.
  724. *
  725. PAIR = .FALSE.
  726. DO 20 I = 1, N
  727. *
  728. IF( PAIR ) THEN
  729. PAIR = .FALSE.
  730. GO TO 20
  731. END IF
  732. MM = 1
  733. IF( I.LT.N ) THEN
  734. IF( A( I+1, I ).NE.ZERO ) THEN
  735. PAIR = .TRUE.
  736. MM = 2
  737. END IF
  738. END IF
  739. *
  740. DO 10 J = 1, N
  741. BWORK( J ) = .FALSE.
  742. 10 CONTINUE
  743. IF( MM.EQ.1 ) THEN
  744. BWORK( I ) = .TRUE.
  745. ELSE IF( MM.EQ.2 ) THEN
  746. BWORK( I ) = .TRUE.
  747. BWORK( I+1 ) = .TRUE.
  748. END IF
  749. *
  750. IWRK = MM*N + 1
  751. IWRK1 = IWRK + MM*N
  752. *
  753. * Compute a pair of left and right eigenvectors.
  754. * (compute workspace: need up to 4*N + 6*N)
  755. *
  756. IF( WANTSE .OR. WANTSB ) THEN
  757. CALL STGEVC( 'B', 'S', BWORK, N, A, LDA, B, LDB,
  758. $ WORK( 1 ), N, WORK( IWRK ), N, MM, M,
  759. $ WORK( IWRK1 ), IERR )
  760. IF( IERR.NE.0 ) THEN
  761. INFO = N + 2
  762. GO TO 130
  763. END IF
  764. END IF
  765. *
  766. CALL STGSNA( SENSE, 'S', BWORK, N, A, LDA, B, LDB,
  767. $ WORK( 1 ), N, WORK( IWRK ), N, RCONDE( I ),
  768. $ RCONDV( I ), MM, M, WORK( IWRK1 ),
  769. $ LWORK-IWRK1+1, IWORK, IERR )
  770. *
  771. 20 CONTINUE
  772. END IF
  773. END IF
  774. *
  775. * Undo balancing on VL and VR and normalization
  776. * (Workspace: none needed)
  777. *
  778. IF( ILVL ) THEN
  779. CALL SGGBAK( BALANC, 'L', N, ILO, IHI, LSCALE, RSCALE, N, VL,
  780. $ LDVL, IERR )
  781. *
  782. DO 70 JC = 1, N
  783. IF( ALPHAI( JC ).LT.ZERO )
  784. $ GO TO 70
  785. TEMP = ZERO
  786. IF( ALPHAI( JC ).EQ.ZERO ) THEN
  787. DO 30 JR = 1, N
  788. TEMP = MAX( TEMP, ABS( VL( JR, JC ) ) )
  789. 30 CONTINUE
  790. ELSE
  791. DO 40 JR = 1, N
  792. TEMP = MAX( TEMP, ABS( VL( JR, JC ) )+
  793. $ ABS( VL( JR, JC+1 ) ) )
  794. 40 CONTINUE
  795. END IF
  796. IF( TEMP.LT.SMLNUM )
  797. $ GO TO 70
  798. TEMP = ONE / TEMP
  799. IF( ALPHAI( JC ).EQ.ZERO ) THEN
  800. DO 50 JR = 1, N
  801. VL( JR, JC ) = VL( JR, JC )*TEMP
  802. 50 CONTINUE
  803. ELSE
  804. DO 60 JR = 1, N
  805. VL( JR, JC ) = VL( JR, JC )*TEMP
  806. VL( JR, JC+1 ) = VL( JR, JC+1 )*TEMP
  807. 60 CONTINUE
  808. END IF
  809. 70 CONTINUE
  810. END IF
  811. IF( ILVR ) THEN
  812. CALL SGGBAK( BALANC, 'R', N, ILO, IHI, LSCALE, RSCALE, N, VR,
  813. $ LDVR, IERR )
  814. DO 120 JC = 1, N
  815. IF( ALPHAI( JC ).LT.ZERO )
  816. $ GO TO 120
  817. TEMP = ZERO
  818. IF( ALPHAI( JC ).EQ.ZERO ) THEN
  819. DO 80 JR = 1, N
  820. TEMP = MAX( TEMP, ABS( VR( JR, JC ) ) )
  821. 80 CONTINUE
  822. ELSE
  823. DO 90 JR = 1, N
  824. TEMP = MAX( TEMP, ABS( VR( JR, JC ) )+
  825. $ ABS( VR( JR, JC+1 ) ) )
  826. 90 CONTINUE
  827. END IF
  828. IF( TEMP.LT.SMLNUM )
  829. $ GO TO 120
  830. TEMP = ONE / TEMP
  831. IF( ALPHAI( JC ).EQ.ZERO ) THEN
  832. DO 100 JR = 1, N
  833. VR( JR, JC ) = VR( JR, JC )*TEMP
  834. 100 CONTINUE
  835. ELSE
  836. DO 110 JR = 1, N
  837. VR( JR, JC ) = VR( JR, JC )*TEMP
  838. VR( JR, JC+1 ) = VR( JR, JC+1 )*TEMP
  839. 110 CONTINUE
  840. END IF
  841. 120 CONTINUE
  842. END IF
  843. *
  844. * Undo scaling if necessary
  845. *
  846. 130 CONTINUE
  847. *
  848. IF( ILASCL ) THEN
  849. CALL SLASCL( 'G', 0, 0, ANRMTO, ANRM, N, 1, ALPHAR, N, IERR )
  850. CALL SLASCL( 'G', 0, 0, ANRMTO, ANRM, N, 1, ALPHAI, N, IERR )
  851. END IF
  852. *
  853. IF( ILBSCL ) THEN
  854. CALL SLASCL( 'G', 0, 0, BNRMTO, BNRM, N, 1, BETA, N, IERR )
  855. END IF
  856. *
  857. WORK( 1 ) = SROUNDUP_LWORK(MAXWRK)
  858. RETURN
  859. *
  860. * End of SGGEVX
  861. *
  862. END