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.

stgexc.f 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. *> \brief \b STGEXC
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download STGEXC + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/stgexc.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/stgexc.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/stgexc.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE STGEXC( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
  22. * LDZ, IFST, ILST, WORK, LWORK, INFO )
  23. *
  24. * .. Scalar Arguments ..
  25. * LOGICAL WANTQ, WANTZ
  26. * INTEGER IFST, ILST, INFO, LDA, LDB, LDQ, LDZ, LWORK, N
  27. * ..
  28. * .. Array Arguments ..
  29. * REAL A( LDA, * ), B( LDB, * ), Q( LDQ, * ),
  30. * $ WORK( * ), Z( LDZ, * )
  31. * ..
  32. *
  33. *
  34. *> \par Purpose:
  35. * =============
  36. *>
  37. *> \verbatim
  38. *>
  39. *> STGEXC reorders the generalized real Schur decomposition of a real
  40. *> matrix pair (A,B) using an orthogonal equivalence transformation
  41. *>
  42. *> (A, B) = Q * (A, B) * Z**T,
  43. *>
  44. *> so that the diagonal block of (A, B) with row index IFST is moved
  45. *> to row ILST.
  46. *>
  47. *> (A, B) must be in generalized real Schur canonical form (as returned
  48. *> by SGGES), i.e. A is block upper triangular with 1-by-1 and 2-by-2
  49. *> diagonal blocks. B is upper triangular.
  50. *>
  51. *> Optionally, the matrices Q and Z of generalized Schur vectors are
  52. *> updated.
  53. *>
  54. *> Q(in) * A(in) * Z(in)**T = Q(out) * A(out) * Z(out)**T
  55. *> Q(in) * B(in) * Z(in)**T = Q(out) * B(out) * Z(out)**T
  56. *>
  57. *> \endverbatim
  58. *
  59. * Arguments:
  60. * ==========
  61. *
  62. *> \param[in] WANTQ
  63. *> \verbatim
  64. *> WANTQ is LOGICAL
  65. *> .TRUE. : update the left transformation matrix Q;
  66. *> .FALSE.: do not update Q.
  67. *> \endverbatim
  68. *>
  69. *> \param[in] WANTZ
  70. *> \verbatim
  71. *> WANTZ is LOGICAL
  72. *> .TRUE. : update the right transformation matrix Z;
  73. *> .FALSE.: do not update Z.
  74. *> \endverbatim
  75. *>
  76. *> \param[in] N
  77. *> \verbatim
  78. *> N is INTEGER
  79. *> The order of the matrices A and B. N >= 0.
  80. *> \endverbatim
  81. *>
  82. *> \param[in,out] A
  83. *> \verbatim
  84. *> A is REAL array, dimension (LDA,N)
  85. *> On entry, the matrix A in generalized real Schur canonical
  86. *> form.
  87. *> On exit, the updated matrix A, again in generalized
  88. *> real Schur canonical form.
  89. *> \endverbatim
  90. *>
  91. *> \param[in] LDA
  92. *> \verbatim
  93. *> LDA is INTEGER
  94. *> The leading dimension of the array A. LDA >= max(1,N).
  95. *> \endverbatim
  96. *>
  97. *> \param[in,out] B
  98. *> \verbatim
  99. *> B is REAL array, dimension (LDB,N)
  100. *> On entry, the matrix B in generalized real Schur canonical
  101. *> form (A,B).
  102. *> On exit, the updated matrix B, again in generalized
  103. *> real Schur canonical form (A,B).
  104. *> \endverbatim
  105. *>
  106. *> \param[in] LDB
  107. *> \verbatim
  108. *> LDB is INTEGER
  109. *> The leading dimension of the array B. LDB >= max(1,N).
  110. *> \endverbatim
  111. *>
  112. *> \param[in,out] Q
  113. *> \verbatim
  114. *> Q is REAL array, dimension (LDQ,N)
  115. *> On entry, if WANTQ = .TRUE., the orthogonal matrix Q.
  116. *> On exit, the updated matrix Q.
  117. *> If WANTQ = .FALSE., Q is not referenced.
  118. *> \endverbatim
  119. *>
  120. *> \param[in] LDQ
  121. *> \verbatim
  122. *> LDQ is INTEGER
  123. *> The leading dimension of the array Q. LDQ >= 1.
  124. *> If WANTQ = .TRUE., LDQ >= N.
  125. *> \endverbatim
  126. *>
  127. *> \param[in,out] Z
  128. *> \verbatim
  129. *> Z is REAL array, dimension (LDZ,N)
  130. *> On entry, if WANTZ = .TRUE., the orthogonal matrix Z.
  131. *> On exit, the updated matrix Z.
  132. *> If WANTZ = .FALSE., Z is not referenced.
  133. *> \endverbatim
  134. *>
  135. *> \param[in] LDZ
  136. *> \verbatim
  137. *> LDZ is INTEGER
  138. *> The leading dimension of the array Z. LDZ >= 1.
  139. *> If WANTZ = .TRUE., LDZ >= N.
  140. *> \endverbatim
  141. *>
  142. *> \param[in,out] IFST
  143. *> \verbatim
  144. *> IFST is INTEGER
  145. *> \endverbatim
  146. *>
  147. *> \param[in,out] ILST
  148. *> \verbatim
  149. *> ILST is INTEGER
  150. *> Specify the reordering of the diagonal blocks of (A, B).
  151. *> The block with row index IFST is moved to row ILST, by a
  152. *> sequence of swapping between adjacent blocks.
  153. *> On exit, if IFST pointed on entry to the second row of
  154. *> a 2-by-2 block, it is changed to point to the first row;
  155. *> ILST always points to the first row of the block in its
  156. *> final position (which may differ from its input value by
  157. *> +1 or -1). 1 <= IFST, ILST <= N.
  158. *> \endverbatim
  159. *>
  160. *> \param[out] WORK
  161. *> \verbatim
  162. *> WORK is REAL array, dimension (MAX(1,LWORK))
  163. *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
  164. *> \endverbatim
  165. *>
  166. *> \param[in] LWORK
  167. *> \verbatim
  168. *> LWORK is INTEGER
  169. *> The dimension of the array WORK.
  170. *> LWORK >= 1 when N <= 1, otherwise LWORK >= 4*N + 16.
  171. *>
  172. *> If LWORK = -1, then a workspace query is assumed; the routine
  173. *> only calculates the optimal size of the WORK array, returns
  174. *> this value as the first entry of the WORK array, and no error
  175. *> message related to LWORK is issued by XERBLA.
  176. *> \endverbatim
  177. *>
  178. *> \param[out] INFO
  179. *> \verbatim
  180. *> INFO is INTEGER
  181. *> =0: successful exit.
  182. *> <0: if INFO = -i, the i-th argument had an illegal value.
  183. *> =1: The transformed matrix pair (A, B) would be too far
  184. *> from generalized Schur form; the problem is ill-
  185. *> conditioned. (A, B) may have been partially reordered,
  186. *> and ILST points to the first row of the current
  187. *> position of the block being moved.
  188. *> \endverbatim
  189. *
  190. * Authors:
  191. * ========
  192. *
  193. *> \author Univ. of Tennessee
  194. *> \author Univ. of California Berkeley
  195. *> \author Univ. of Colorado Denver
  196. *> \author NAG Ltd.
  197. *
  198. *> \ingroup tgexc
  199. *
  200. *> \par Contributors:
  201. * ==================
  202. *>
  203. *> Bo Kagstrom and Peter Poromaa, Department of Computing Science,
  204. *> Umea University, S-901 87 Umea, Sweden.
  205. *
  206. *> \par References:
  207. * ================
  208. *>
  209. *> \verbatim
  210. *>
  211. *> [1] B. Kagstrom; A Direct Method for Reordering Eigenvalues in the
  212. *> Generalized Real Schur Form of a Regular Matrix Pair (A, B), in
  213. *> M.S. Moonen et al (eds), Linear Algebra for Large Scale and
  214. *> Real-Time Applications, Kluwer Academic Publ. 1993, pp 195-218.
  215. *> \endverbatim
  216. *>
  217. * =====================================================================
  218. SUBROUTINE STGEXC( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
  219. $ LDZ, IFST, ILST, WORK, LWORK, INFO )
  220. *
  221. * -- LAPACK computational routine --
  222. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  223. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  224. *
  225. * .. Scalar Arguments ..
  226. LOGICAL WANTQ, WANTZ
  227. INTEGER IFST, ILST, INFO, LDA, LDB, LDQ, LDZ, LWORK, N
  228. * ..
  229. * .. Array Arguments ..
  230. REAL A( LDA, * ), B( LDB, * ), Q( LDQ, * ),
  231. $ WORK( * ), Z( LDZ, * )
  232. * ..
  233. *
  234. * =====================================================================
  235. *
  236. * .. Parameters ..
  237. REAL ZERO
  238. PARAMETER ( ZERO = 0.0E+0 )
  239. * ..
  240. * .. Local Scalars ..
  241. LOGICAL LQUERY
  242. INTEGER HERE, LWMIN, NBF, NBL, NBNEXT
  243. * ..
  244. * .. External Functions ..
  245. REAL SROUNDUP_LWORK
  246. EXTERNAL SROUNDUP_LWORK
  247. * ..
  248. * .. External Subroutines ..
  249. EXTERNAL STGEX2, XERBLA
  250. * ..
  251. * .. Intrinsic Functions ..
  252. INTRINSIC MAX
  253. * ..
  254. * .. Executable Statements ..
  255. *
  256. * Decode and test input arguments.
  257. *
  258. INFO = 0
  259. LQUERY = ( LWORK.EQ.-1 )
  260. IF( N.LT.0 ) THEN
  261. INFO = -3
  262. ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
  263. INFO = -5
  264. ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
  265. INFO = -7
  266. ELSE IF( LDQ.LT.1 .OR. WANTQ .AND. ( LDQ.LT.MAX( 1, N ) ) ) THEN
  267. INFO = -9
  268. ELSE IF( LDZ.LT.1 .OR. WANTZ .AND. ( LDZ.LT.MAX( 1, N ) ) ) THEN
  269. INFO = -11
  270. ELSE IF( IFST.LT.1 .OR. IFST.GT.N ) THEN
  271. INFO = -12
  272. ELSE IF( ILST.LT.1 .OR. ILST.GT.N ) THEN
  273. INFO = -13
  274. END IF
  275. *
  276. IF( INFO.EQ.0 ) THEN
  277. IF( N.LE.1 ) THEN
  278. LWMIN = 1
  279. ELSE
  280. LWMIN = 4*N + 16
  281. END IF
  282. WORK(1) = LWMIN
  283. *
  284. IF (LWORK.LT.LWMIN .AND. .NOT.LQUERY) THEN
  285. INFO = -15
  286. END IF
  287. END IF
  288. *
  289. IF( INFO.NE.0 ) THEN
  290. CALL XERBLA( 'STGEXC', -INFO )
  291. RETURN
  292. ELSE IF( LQUERY ) THEN
  293. RETURN
  294. END IF
  295. *
  296. * Quick return if possible
  297. *
  298. IF( N.LE.1 )
  299. $ RETURN
  300. *
  301. * Determine the first row of the specified block and find out
  302. * if it is 1-by-1 or 2-by-2.
  303. *
  304. IF( IFST.GT.1 ) THEN
  305. IF( A( IFST, IFST-1 ).NE.ZERO )
  306. $ IFST = IFST - 1
  307. END IF
  308. NBF = 1
  309. IF( IFST.LT.N ) THEN
  310. IF( A( IFST+1, IFST ).NE.ZERO )
  311. $ NBF = 2
  312. END IF
  313. *
  314. * Determine the first row of the final block
  315. * and find out if it is 1-by-1 or 2-by-2.
  316. *
  317. IF( ILST.GT.1 ) THEN
  318. IF( A( ILST, ILST-1 ).NE.ZERO )
  319. $ ILST = ILST - 1
  320. END IF
  321. NBL = 1
  322. IF( ILST.LT.N ) THEN
  323. IF( A( ILST+1, ILST ).NE.ZERO )
  324. $ NBL = 2
  325. END IF
  326. IF( IFST.EQ.ILST )
  327. $ RETURN
  328. *
  329. IF( IFST.LT.ILST ) THEN
  330. *
  331. * Update ILST.
  332. *
  333. IF( NBF.EQ.2 .AND. NBL.EQ.1 )
  334. $ ILST = ILST - 1
  335. IF( NBF.EQ.1 .AND. NBL.EQ.2 )
  336. $ ILST = ILST + 1
  337. *
  338. HERE = IFST
  339. *
  340. 10 CONTINUE
  341. *
  342. * Swap with next one below.
  343. *
  344. IF( NBF.EQ.1 .OR. NBF.EQ.2 ) THEN
  345. *
  346. * Current block either 1-by-1 or 2-by-2.
  347. *
  348. NBNEXT = 1
  349. IF( HERE+NBF+1.LE.N ) THEN
  350. IF( A( HERE+NBF+1, HERE+NBF ).NE.ZERO )
  351. $ NBNEXT = 2
  352. END IF
  353. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
  354. $ LDZ, HERE, NBF, NBNEXT, WORK, LWORK, INFO )
  355. IF( INFO.NE.0 ) THEN
  356. ILST = HERE
  357. RETURN
  358. END IF
  359. HERE = HERE + NBNEXT
  360. *
  361. * Test if 2-by-2 block breaks into two 1-by-1 blocks.
  362. *
  363. IF( NBF.EQ.2 ) THEN
  364. IF( A( HERE+1, HERE ).EQ.ZERO )
  365. $ NBF = 3
  366. END IF
  367. *
  368. ELSE
  369. *
  370. * Current block consists of two 1-by-1 blocks, each of which
  371. * must be swapped individually.
  372. *
  373. NBNEXT = 1
  374. IF( HERE+3.LE.N ) THEN
  375. IF( A( HERE+3, HERE+2 ).NE.ZERO )
  376. $ NBNEXT = 2
  377. END IF
  378. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
  379. $ LDZ, HERE+1, 1, NBNEXT, WORK, LWORK, INFO )
  380. IF( INFO.NE.0 ) THEN
  381. ILST = HERE
  382. RETURN
  383. END IF
  384. IF( NBNEXT.EQ.1 ) THEN
  385. *
  386. * Swap two 1-by-1 blocks.
  387. *
  388. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
  389. $ LDZ, HERE, 1, 1, WORK, LWORK, INFO )
  390. IF( INFO.NE.0 ) THEN
  391. ILST = HERE
  392. RETURN
  393. END IF
  394. HERE = HERE + 1
  395. *
  396. ELSE
  397. *
  398. * Recompute NBNEXT in case of 2-by-2 split.
  399. *
  400. IF( A( HERE+2, HERE+1 ).EQ.ZERO )
  401. $ NBNEXT = 1
  402. IF( NBNEXT.EQ.2 ) THEN
  403. *
  404. * 2-by-2 block did not split.
  405. *
  406. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ,
  407. $ Z, LDZ, HERE, 1, NBNEXT, WORK, LWORK,
  408. $ INFO )
  409. IF( INFO.NE.0 ) THEN
  410. ILST = HERE
  411. RETURN
  412. END IF
  413. HERE = HERE + 2
  414. ELSE
  415. *
  416. * 2-by-2 block did split.
  417. *
  418. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ,
  419. $ Z, LDZ, HERE, 1, 1, WORK, LWORK, INFO )
  420. IF( INFO.NE.0 ) THEN
  421. ILST = HERE
  422. RETURN
  423. END IF
  424. HERE = HERE + 1
  425. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ,
  426. $ Z, LDZ, HERE, 1, 1, WORK, LWORK, INFO )
  427. IF( INFO.NE.0 ) THEN
  428. ILST = HERE
  429. RETURN
  430. END IF
  431. HERE = HERE + 1
  432. END IF
  433. *
  434. END IF
  435. END IF
  436. IF( HERE.LT.ILST )
  437. $ GO TO 10
  438. ELSE
  439. HERE = IFST
  440. *
  441. 20 CONTINUE
  442. *
  443. * Swap with next one below.
  444. *
  445. IF( NBF.EQ.1 .OR. NBF.EQ.2 ) THEN
  446. *
  447. * Current block either 1-by-1 or 2-by-2.
  448. *
  449. NBNEXT = 1
  450. IF( HERE.GE.3 ) THEN
  451. IF( A( HERE-1, HERE-2 ).NE.ZERO )
  452. $ NBNEXT = 2
  453. END IF
  454. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
  455. $ LDZ, HERE-NBNEXT, NBNEXT, NBF, WORK, LWORK,
  456. $ INFO )
  457. IF( INFO.NE.0 ) THEN
  458. ILST = HERE
  459. RETURN
  460. END IF
  461. HERE = HERE - NBNEXT
  462. *
  463. * Test if 2-by-2 block breaks into two 1-by-1 blocks.
  464. *
  465. IF( NBF.EQ.2 ) THEN
  466. IF( A( HERE+1, HERE ).EQ.ZERO )
  467. $ NBF = 3
  468. END IF
  469. *
  470. ELSE
  471. *
  472. * Current block consists of two 1-by-1 blocks, each of which
  473. * must be swapped individually.
  474. *
  475. NBNEXT = 1
  476. IF( HERE.GE.3 ) THEN
  477. IF( A( HERE-1, HERE-2 ).NE.ZERO )
  478. $ NBNEXT = 2
  479. END IF
  480. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
  481. $ LDZ, HERE-NBNEXT, NBNEXT, 1, WORK, LWORK,
  482. $ INFO )
  483. IF( INFO.NE.0 ) THEN
  484. ILST = HERE
  485. RETURN
  486. END IF
  487. IF( NBNEXT.EQ.1 ) THEN
  488. *
  489. * Swap two 1-by-1 blocks.
  490. *
  491. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
  492. $ LDZ, HERE, NBNEXT, 1, WORK, LWORK, INFO )
  493. IF( INFO.NE.0 ) THEN
  494. ILST = HERE
  495. RETURN
  496. END IF
  497. HERE = HERE - 1
  498. ELSE
  499. *
  500. * Recompute NBNEXT in case of 2-by-2 split.
  501. *
  502. IF( A( HERE, HERE-1 ).EQ.ZERO )
  503. $ NBNEXT = 1
  504. IF( NBNEXT.EQ.2 ) THEN
  505. *
  506. * 2-by-2 block did not split.
  507. *
  508. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ,
  509. $ Z, LDZ, HERE-1, 2, 1, WORK, LWORK, INFO )
  510. IF( INFO.NE.0 ) THEN
  511. ILST = HERE
  512. RETURN
  513. END IF
  514. HERE = HERE - 2
  515. ELSE
  516. *
  517. * 2-by-2 block did split.
  518. *
  519. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ,
  520. $ Z, LDZ, HERE, 1, 1, WORK, LWORK, INFO )
  521. IF( INFO.NE.0 ) THEN
  522. ILST = HERE
  523. RETURN
  524. END IF
  525. HERE = HERE - 1
  526. CALL STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ,
  527. $ Z, LDZ, HERE, 1, 1, WORK, LWORK, INFO )
  528. IF( INFO.NE.0 ) THEN
  529. ILST = HERE
  530. RETURN
  531. END IF
  532. HERE = HERE - 1
  533. END IF
  534. END IF
  535. END IF
  536. IF( HERE.GT.ILST )
  537. $ GO TO 20
  538. END IF
  539. ILST = HERE
  540. WORK( 1 ) = SROUNDUP_LWORK(LWMIN)
  541. RETURN
  542. *
  543. * End of STGEXC
  544. *
  545. END