dtgex2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /* dtgex2.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 integer c__4 = 4;
  15. static doublereal c_b5 = 0.;
  16. static integer c__1 = 1;
  17. static integer c__2 = 2;
  18. static doublereal c_b42 = 1.;
  19. static doublereal c_b48 = -1.;
  20. static integer c__0 = 0;
  21. /* Subroutine */ int dtgex2_(logical *wantq, logical *wantz, integer *n,
  22. doublereal *a, integer *lda, doublereal *b, integer *ldb, doublereal *
  23. q, integer *ldq, doublereal *z__, integer *ldz, integer *j1, integer *
  24. n1, integer *n2, doublereal *work, integer *lwork, integer *info)
  25. {
  26. /* System generated locals */
  27. integer a_dim1, a_offset, b_dim1, b_offset, q_dim1, q_offset, z_dim1,
  28. z_offset, i__1, i__2;
  29. doublereal d__1;
  30. /* Builtin functions */
  31. double sqrt(doublereal);
  32. /* Local variables */
  33. doublereal f, g;
  34. integer i__, m;
  35. doublereal s[16] /* was [4][4] */, t[16] /* was [4][4] */, be[2], ai[2]
  36. , ar[2], sa, sb, li[16] /* was [4][4] */, ir[16] /*
  37. was [4][4] */, ss, ws, eps;
  38. logical weak;
  39. doublereal ddum;
  40. integer idum;
  41. doublereal taul[4], dsum;
  42. extern /* Subroutine */ int drot_(integer *, doublereal *, integer *,
  43. doublereal *, integer *, doublereal *, doublereal *);
  44. doublereal taur[4], scpy[16] /* was [4][4] */, tcpy[16] /*
  45. was [4][4] */;
  46. extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *,
  47. integer *);
  48. doublereal scale, bqra21, brqa21;
  49. extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *,
  50. integer *, doublereal *, doublereal *, integer *, doublereal *,
  51. integer *, doublereal *, doublereal *, integer *);
  52. doublereal licop[16] /* was [4][4] */;
  53. integer linfo;
  54. doublereal ircop[16] /* was [4][4] */, dnorm;
  55. integer iwork[4];
  56. extern /* Subroutine */ int dlagv2_(doublereal *, integer *, doublereal *,
  57. integer *, doublereal *, doublereal *, doublereal *, doublereal *
  58. , doublereal *, doublereal *, doublereal *), dgeqr2_(integer *,
  59. integer *, doublereal *, integer *, doublereal *, doublereal *,
  60. integer *), dgerq2_(integer *, integer *, doublereal *, integer *,
  61. doublereal *, doublereal *, integer *), dorg2r_(integer *,
  62. integer *, integer *, doublereal *, integer *, doublereal *,
  63. doublereal *, integer *), dorgr2_(integer *, integer *, integer *,
  64. doublereal *, integer *, doublereal *, doublereal *, integer *),
  65. dorm2r_(char *, char *, integer *, integer *, integer *,
  66. doublereal *, integer *, doublereal *, doublereal *, integer *,
  67. doublereal *, integer *), dormr2_(char *, char *,
  68. integer *, integer *, integer *, doublereal *, integer *,
  69. doublereal *, doublereal *, integer *, doublereal *, integer *), dtgsy2_(char *, integer *, integer *, integer *,
  70. doublereal *, integer *, doublereal *, integer *, doublereal *,
  71. integer *, doublereal *, integer *, doublereal *, integer *,
  72. doublereal *, integer *, doublereal *, doublereal *, doublereal *,
  73. integer *, integer *, integer *);
  74. extern doublereal dlamch_(char *);
  75. doublereal dscale;
  76. extern /* Subroutine */ int dlacpy_(char *, integer *, integer *,
  77. doublereal *, integer *, doublereal *, integer *),
  78. dlartg_(doublereal *, doublereal *, doublereal *, doublereal *,
  79. doublereal *), dlaset_(char *, integer *, integer *, doublereal *,
  80. doublereal *, doublereal *, integer *), dlassq_(integer *
  81. , doublereal *, integer *, doublereal *, doublereal *);
  82. logical dtrong;
  83. doublereal thresh, smlnum;
  84. /* -- LAPACK auxiliary routine (version 3.2) -- */
  85. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  86. /* November 2006 */
  87. /* .. Scalar Arguments .. */
  88. /* .. */
  89. /* .. Array Arguments .. */
  90. /* .. */
  91. /* Purpose */
  92. /* ======= */
  93. /* DTGEX2 swaps adjacent diagonal blocks (A11, B11) and (A22, B22) */
  94. /* of size 1-by-1 or 2-by-2 in an upper (quasi) triangular matrix pair */
  95. /* (A, B) by an orthogonal equivalence transformation. */
  96. /* (A, B) must be in generalized real Schur canonical form (as returned */
  97. /* by DGGES), i.e. A is block upper triangular with 1-by-1 and 2-by-2 */
  98. /* diagonal blocks. B is upper triangular. */
  99. /* Optionally, the matrices Q and Z of generalized Schur vectors are */
  100. /* updated. */
  101. /* Q(in) * A(in) * Z(in)' = Q(out) * A(out) * Z(out)' */
  102. /* Q(in) * B(in) * Z(in)' = Q(out) * B(out) * Z(out)' */
  103. /* Arguments */
  104. /* ========= */
  105. /* WANTQ (input) LOGICAL */
  106. /* .TRUE. : update the left transformation matrix Q; */
  107. /* .FALSE.: do not update Q. */
  108. /* WANTZ (input) LOGICAL */
  109. /* .TRUE. : update the right transformation matrix Z; */
  110. /* .FALSE.: do not update Z. */
  111. /* N (input) INTEGER */
  112. /* The order of the matrices A and B. N >= 0. */
  113. /* A (input/output) DOUBLE PRECISION arrays, dimensions (LDA,N) */
  114. /* On entry, the matrix A in the pair (A, B). */
  115. /* On exit, the updated matrix A. */
  116. /* LDA (input) INTEGER */
  117. /* The leading dimension of the array A. LDA >= max(1,N). */
  118. /* B (input/output) DOUBLE PRECISION arrays, dimensions (LDB,N) */
  119. /* On entry, the matrix B in the pair (A, B). */
  120. /* On exit, the updated matrix B. */
  121. /* LDB (input) INTEGER */
  122. /* The leading dimension of the array B. LDB >= max(1,N). */
  123. /* Q (input/output) DOUBLE PRECISION array, dimension (LDZ,N) */
  124. /* On entry, if WANTQ = .TRUE., the orthogonal matrix Q. */
  125. /* On exit, the updated matrix Q. */
  126. /* Not referenced if WANTQ = .FALSE.. */
  127. /* LDQ (input) INTEGER */
  128. /* The leading dimension of the array Q. LDQ >= 1. */
  129. /* If WANTQ = .TRUE., LDQ >= N. */
  130. /* Z (input/output) DOUBLE PRECISION array, dimension (LDZ,N) */
  131. /* On entry, if WANTZ =.TRUE., the orthogonal matrix Z. */
  132. /* On exit, the updated matrix Z. */
  133. /* Not referenced if WANTZ = .FALSE.. */
  134. /* LDZ (input) INTEGER */
  135. /* The leading dimension of the array Z. LDZ >= 1. */
  136. /* If WANTZ = .TRUE., LDZ >= N. */
  137. /* J1 (input) INTEGER */
  138. /* The index to the first block (A11, B11). 1 <= J1 <= N. */
  139. /* N1 (input) INTEGER */
  140. /* The order of the first block (A11, B11). N1 = 0, 1 or 2. */
  141. /* N2 (input) INTEGER */
  142. /* The order of the second block (A22, B22). N2 = 0, 1 or 2. */
  143. /* WORK (workspace) DOUBLE PRECISION array, dimension (MAX(1,LWORK)). */
  144. /* LWORK (input) INTEGER */
  145. /* The dimension of the array WORK. */
  146. /* LWORK >= MAX( 1, N*(N2+N1), (N2+N1)*(N2+N1)*2 ) */
  147. /* INFO (output) INTEGER */
  148. /* =0: Successful exit */
  149. /* >0: If INFO = 1, the transformed matrix (A, B) would be */
  150. /* too far from generalized Schur form; the blocks are */
  151. /* not swapped and (A, B) and (Q, Z) are unchanged. */
  152. /* The problem of swapping is too ill-conditioned. */
  153. /* <0: If INFO = -16: LWORK is too small. Appropriate value */
  154. /* for LWORK is returned in WORK(1). */
  155. /* Further Details */
  156. /* =============== */
  157. /* Based on contributions by */
  158. /* Bo Kagstrom and Peter Poromaa, Department of Computing Science, */
  159. /* Umea University, S-901 87 Umea, Sweden. */
  160. /* In the current code both weak and strong stability tests are */
  161. /* performed. The user can omit the strong stability test by changing */
  162. /* the internal logical parameter WANDS to .FALSE.. See ref. [2] for */
  163. /* details. */
  164. /* [1] B. Kagstrom; A Direct Method for Reordering Eigenvalues in the */
  165. /* Generalized Real Schur Form of a Regular Matrix Pair (A, B), in */
  166. /* M.S. Moonen et al (eds), Linear Algebra for Large Scale and */
  167. /* Real-Time Applications, Kluwer Academic Publ. 1993, pp 195-218. */
  168. /* [2] B. Kagstrom and P. Poromaa; Computing Eigenspaces with Specified */
  169. /* Eigenvalues of a Regular Matrix Pair (A, B) and Condition */
  170. /* Estimation: Theory, Algorithms and Software, */
  171. /* Report UMINF - 94.04, Department of Computing Science, Umea */
  172. /* University, S-901 87 Umea, Sweden, 1994. Also as LAPACK Working */
  173. /* Note 87. To appear in Numerical Algorithms, 1996. */
  174. /* ===================================================================== */
  175. /* Replaced various illegal calls to DCOPY by calls to DLASET, or by DO */
  176. /* loops. Sven Hammarling, 1/5/02. */
  177. /* .. Parameters .. */
  178. /* .. */
  179. /* .. Local Scalars .. */
  180. /* .. */
  181. /* .. Local Arrays .. */
  182. /* .. */
  183. /* .. External Functions .. */
  184. /* .. */
  185. /* .. External Subroutines .. */
  186. /* .. */
  187. /* .. Intrinsic Functions .. */
  188. /* .. */
  189. /* .. Executable Statements .. */
  190. /* Parameter adjustments */
  191. a_dim1 = *lda;
  192. a_offset = 1 + a_dim1;
  193. a -= a_offset;
  194. b_dim1 = *ldb;
  195. b_offset = 1 + b_dim1;
  196. b -= b_offset;
  197. q_dim1 = *ldq;
  198. q_offset = 1 + q_dim1;
  199. q -= q_offset;
  200. z_dim1 = *ldz;
  201. z_offset = 1 + z_dim1;
  202. z__ -= z_offset;
  203. --work;
  204. /* Function Body */
  205. *info = 0;
  206. /* Quick return if possible */
  207. if (*n <= 1 || *n1 <= 0 || *n2 <= 0) {
  208. return 0;
  209. }
  210. if (*n1 > *n || *j1 + *n1 > *n) {
  211. return 0;
  212. }
  213. m = *n1 + *n2;
  214. /* Computing MAX */
  215. i__1 = 1, i__2 = *n * m, i__1 = max(i__1,i__2), i__2 = m * m << 1;
  216. if (*lwork < max(i__1,i__2)) {
  217. *info = -16;
  218. /* Computing MAX */
  219. i__1 = 1, i__2 = *n * m, i__1 = max(i__1,i__2), i__2 = m * m << 1;
  220. work[1] = (doublereal) max(i__1,i__2);
  221. return 0;
  222. }
  223. weak = FALSE_;
  224. dtrong = FALSE_;
  225. /* Make a local copy of selected block */
  226. dlaset_("Full", &c__4, &c__4, &c_b5, &c_b5, li, &c__4);
  227. dlaset_("Full", &c__4, &c__4, &c_b5, &c_b5, ir, &c__4);
  228. dlacpy_("Full", &m, &m, &a[*j1 + *j1 * a_dim1], lda, s, &c__4);
  229. dlacpy_("Full", &m, &m, &b[*j1 + *j1 * b_dim1], ldb, t, &c__4);
  230. /* Compute threshold for testing acceptance of swapping. */
  231. eps = dlamch_("P");
  232. smlnum = dlamch_("S") / eps;
  233. dscale = 0.;
  234. dsum = 1.;
  235. dlacpy_("Full", &m, &m, s, &c__4, &work[1], &m);
  236. i__1 = m * m;
  237. dlassq_(&i__1, &work[1], &c__1, &dscale, &dsum);
  238. dlacpy_("Full", &m, &m, t, &c__4, &work[1], &m);
  239. i__1 = m * m;
  240. dlassq_(&i__1, &work[1], &c__1, &dscale, &dsum);
  241. dnorm = dscale * sqrt(dsum);
  242. /* Computing MAX */
  243. d__1 = eps * 10. * dnorm;
  244. thresh = max(d__1,smlnum);
  245. if (m == 2) {
  246. /* CASE 1: Swap 1-by-1 and 1-by-1 blocks. */
  247. /* Compute orthogonal QL and RQ that swap 1-by-1 and 1-by-1 blocks */
  248. /* using Givens rotations and perform the swap tentatively. */
  249. f = s[5] * t[0] - t[5] * s[0];
  250. g = s[5] * t[4] - t[5] * s[4];
  251. sb = abs(t[5]);
  252. sa = abs(s[5]);
  253. dlartg_(&f, &g, &ir[4], ir, &ddum);
  254. ir[1] = -ir[4];
  255. ir[5] = ir[0];
  256. drot_(&c__2, s, &c__1, &s[4], &c__1, ir, &ir[1]);
  257. drot_(&c__2, t, &c__1, &t[4], &c__1, ir, &ir[1]);
  258. if (sa >= sb) {
  259. dlartg_(s, &s[1], li, &li[1], &ddum);
  260. } else {
  261. dlartg_(t, &t[1], li, &li[1], &ddum);
  262. }
  263. drot_(&c__2, s, &c__4, &s[1], &c__4, li, &li[1]);
  264. drot_(&c__2, t, &c__4, &t[1], &c__4, li, &li[1]);
  265. li[5] = li[0];
  266. li[4] = -li[1];
  267. /* Weak stability test: */
  268. /* |S21| + |T21| <= O(EPS * F-norm((S, T))) */
  269. ws = abs(s[1]) + abs(t[1]);
  270. weak = ws <= thresh;
  271. if (! weak) {
  272. goto L70;
  273. }
  274. if (TRUE_) {
  275. /* Strong stability test: */
  276. /* F-norm((A-QL'*S*QR, B-QL'*T*QR)) <= O(EPS*F-norm((A,B))) */
  277. dlacpy_("Full", &m, &m, &a[*j1 + *j1 * a_dim1], lda, &work[m * m
  278. + 1], &m);
  279. dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, s, &c__4, &c_b5, &
  280. work[1], &m);
  281. dgemm_("N", "T", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
  282. c_b42, &work[m * m + 1], &m);
  283. dscale = 0.;
  284. dsum = 1.;
  285. i__1 = m * m;
  286. dlassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
  287. dlacpy_("Full", &m, &m, &b[*j1 + *j1 * b_dim1], ldb, &work[m * m
  288. + 1], &m);
  289. dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, t, &c__4, &c_b5, &
  290. work[1], &m);
  291. dgemm_("N", "T", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
  292. c_b42, &work[m * m + 1], &m);
  293. i__1 = m * m;
  294. dlassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
  295. ss = dscale * sqrt(dsum);
  296. dtrong = ss <= thresh;
  297. if (! dtrong) {
  298. goto L70;
  299. }
  300. }
  301. /* Update (A(J1:J1+M-1, M+J1:N), B(J1:J1+M-1, M+J1:N)) and */
  302. /* (A(1:J1-1, J1:J1+M), B(1:J1-1, J1:J1+M)). */
  303. i__1 = *j1 + 1;
  304. drot_(&i__1, &a[*j1 * a_dim1 + 1], &c__1, &a[(*j1 + 1) * a_dim1 + 1],
  305. &c__1, ir, &ir[1]);
  306. i__1 = *j1 + 1;
  307. drot_(&i__1, &b[*j1 * b_dim1 + 1], &c__1, &b[(*j1 + 1) * b_dim1 + 1],
  308. &c__1, ir, &ir[1]);
  309. i__1 = *n - *j1 + 1;
  310. drot_(&i__1, &a[*j1 + *j1 * a_dim1], lda, &a[*j1 + 1 + *j1 * a_dim1],
  311. lda, li, &li[1]);
  312. i__1 = *n - *j1 + 1;
  313. drot_(&i__1, &b[*j1 + *j1 * b_dim1], ldb, &b[*j1 + 1 + *j1 * b_dim1],
  314. ldb, li, &li[1]);
  315. /* Set N1-by-N2 (2,1) - blocks to ZERO. */
  316. a[*j1 + 1 + *j1 * a_dim1] = 0.;
  317. b[*j1 + 1 + *j1 * b_dim1] = 0.;
  318. /* Accumulate transformations into Q and Z if requested. */
  319. if (*wantz) {
  320. drot_(n, &z__[*j1 * z_dim1 + 1], &c__1, &z__[(*j1 + 1) * z_dim1 +
  321. 1], &c__1, ir, &ir[1]);
  322. }
  323. if (*wantq) {
  324. drot_(n, &q[*j1 * q_dim1 + 1], &c__1, &q[(*j1 + 1) * q_dim1 + 1],
  325. &c__1, li, &li[1]);
  326. }
  327. /* Exit with INFO = 0 if swap was successfully performed. */
  328. return 0;
  329. } else {
  330. /* CASE 2: Swap 1-by-1 and 2-by-2 blocks, or 2-by-2 */
  331. /* and 2-by-2 blocks. */
  332. /* Solve the generalized Sylvester equation */
  333. /* S11 * R - L * S22 = SCALE * S12 */
  334. /* T11 * R - L * T22 = SCALE * T12 */
  335. /* for R and L. Solutions in LI and IR. */
  336. dlacpy_("Full", n1, n2, &t[(*n1 + 1 << 2) - 4], &c__4, li, &c__4);
  337. dlacpy_("Full", n1, n2, &s[(*n1 + 1 << 2) - 4], &c__4, &ir[*n2 + 1 + (
  338. *n1 + 1 << 2) - 5], &c__4);
  339. dtgsy2_("N", &c__0, n1, n2, s, &c__4, &s[*n1 + 1 + (*n1 + 1 << 2) - 5]
  340. , &c__4, &ir[*n2 + 1 + (*n1 + 1 << 2) - 5], &c__4, t, &c__4, &
  341. t[*n1 + 1 + (*n1 + 1 << 2) - 5], &c__4, li, &c__4, &scale, &
  342. dsum, &dscale, iwork, &idum, &linfo);
  343. /* Compute orthogonal matrix QL: */
  344. /* QL' * LI = [ TL ] */
  345. /* [ 0 ] */
  346. /* where */
  347. /* LI = [ -L ] */
  348. /* [ SCALE * identity(N2) ] */
  349. i__1 = *n2;
  350. for (i__ = 1; i__ <= i__1; ++i__) {
  351. dscal_(n1, &c_b48, &li[(i__ << 2) - 4], &c__1);
  352. li[*n1 + i__ + (i__ << 2) - 5] = scale;
  353. /* L10: */
  354. }
  355. dgeqr2_(&m, n2, li, &c__4, taul, &work[1], &linfo);
  356. if (linfo != 0) {
  357. goto L70;
  358. }
  359. dorg2r_(&m, &m, n2, li, &c__4, taul, &work[1], &linfo);
  360. if (linfo != 0) {
  361. goto L70;
  362. }
  363. /* Compute orthogonal matrix RQ: */
  364. /* IR * RQ' = [ 0 TR], */
  365. /* where IR = [ SCALE * identity(N1), R ] */
  366. i__1 = *n1;
  367. for (i__ = 1; i__ <= i__1; ++i__) {
  368. ir[*n2 + i__ + (i__ << 2) - 5] = scale;
  369. /* L20: */
  370. }
  371. dgerq2_(n1, &m, &ir[*n2], &c__4, taur, &work[1], &linfo);
  372. if (linfo != 0) {
  373. goto L70;
  374. }
  375. dorgr2_(&m, &m, n1, ir, &c__4, taur, &work[1], &linfo);
  376. if (linfo != 0) {
  377. goto L70;
  378. }
  379. /* Perform the swapping tentatively: */
  380. dgemm_("T", "N", &m, &m, &m, &c_b42, li, &c__4, s, &c__4, &c_b5, &
  381. work[1], &m);
  382. dgemm_("N", "T", &m, &m, &m, &c_b42, &work[1], &m, ir, &c__4, &c_b5,
  383. s, &c__4);
  384. dgemm_("T", "N", &m, &m, &m, &c_b42, li, &c__4, t, &c__4, &c_b5, &
  385. work[1], &m);
  386. dgemm_("N", "T", &m, &m, &m, &c_b42, &work[1], &m, ir, &c__4, &c_b5,
  387. t, &c__4);
  388. dlacpy_("F", &m, &m, s, &c__4, scpy, &c__4);
  389. dlacpy_("F", &m, &m, t, &c__4, tcpy, &c__4);
  390. dlacpy_("F", &m, &m, ir, &c__4, ircop, &c__4);
  391. dlacpy_("F", &m, &m, li, &c__4, licop, &c__4);
  392. /* Triangularize the B-part by an RQ factorization. */
  393. /* Apply transformation (from left) to A-part, giving S. */
  394. dgerq2_(&m, &m, t, &c__4, taur, &work[1], &linfo);
  395. if (linfo != 0) {
  396. goto L70;
  397. }
  398. dormr2_("R", "T", &m, &m, &m, t, &c__4, taur, s, &c__4, &work[1], &
  399. linfo);
  400. if (linfo != 0) {
  401. goto L70;
  402. }
  403. dormr2_("L", "N", &m, &m, &m, t, &c__4, taur, ir, &c__4, &work[1], &
  404. linfo);
  405. if (linfo != 0) {
  406. goto L70;
  407. }
  408. /* Compute F-norm(S21) in BRQA21. (T21 is 0.) */
  409. dscale = 0.;
  410. dsum = 1.;
  411. i__1 = *n2;
  412. for (i__ = 1; i__ <= i__1; ++i__) {
  413. dlassq_(n1, &s[*n2 + 1 + (i__ << 2) - 5], &c__1, &dscale, &dsum);
  414. /* L30: */
  415. }
  416. brqa21 = dscale * sqrt(dsum);
  417. /* Triangularize the B-part by a QR factorization. */
  418. /* Apply transformation (from right) to A-part, giving S. */
  419. dgeqr2_(&m, &m, tcpy, &c__4, taul, &work[1], &linfo);
  420. if (linfo != 0) {
  421. goto L70;
  422. }
  423. dorm2r_("L", "T", &m, &m, &m, tcpy, &c__4, taul, scpy, &c__4, &work[1]
  424. , info);
  425. dorm2r_("R", "N", &m, &m, &m, tcpy, &c__4, taul, licop, &c__4, &work[
  426. 1], info);
  427. if (linfo != 0) {
  428. goto L70;
  429. }
  430. /* Compute F-norm(S21) in BQRA21. (T21 is 0.) */
  431. dscale = 0.;
  432. dsum = 1.;
  433. i__1 = *n2;
  434. for (i__ = 1; i__ <= i__1; ++i__) {
  435. dlassq_(n1, &scpy[*n2 + 1 + (i__ << 2) - 5], &c__1, &dscale, &
  436. dsum);
  437. /* L40: */
  438. }
  439. bqra21 = dscale * sqrt(dsum);
  440. /* Decide which method to use. */
  441. /* Weak stability test: */
  442. /* F-norm(S21) <= O(EPS * F-norm((S, T))) */
  443. if (bqra21 <= brqa21 && bqra21 <= thresh) {
  444. dlacpy_("F", &m, &m, scpy, &c__4, s, &c__4);
  445. dlacpy_("F", &m, &m, tcpy, &c__4, t, &c__4);
  446. dlacpy_("F", &m, &m, ircop, &c__4, ir, &c__4);
  447. dlacpy_("F", &m, &m, licop, &c__4, li, &c__4);
  448. } else if (brqa21 >= thresh) {
  449. goto L70;
  450. }
  451. /* Set lower triangle of B-part to zero */
  452. i__1 = m - 1;
  453. i__2 = m - 1;
  454. dlaset_("Lower", &i__1, &i__2, &c_b5, &c_b5, &t[1], &c__4);
  455. if (TRUE_) {
  456. /* Strong stability test: */
  457. /* F-norm((A-QL*S*QR', B-QL*T*QR')) <= O(EPS*F-norm((A,B))) */
  458. dlacpy_("Full", &m, &m, &a[*j1 + *j1 * a_dim1], lda, &work[m * m
  459. + 1], &m);
  460. dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, s, &c__4, &c_b5, &
  461. work[1], &m);
  462. dgemm_("N", "N", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
  463. c_b42, &work[m * m + 1], &m);
  464. dscale = 0.;
  465. dsum = 1.;
  466. i__1 = m * m;
  467. dlassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
  468. dlacpy_("Full", &m, &m, &b[*j1 + *j1 * b_dim1], ldb, &work[m * m
  469. + 1], &m);
  470. dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, t, &c__4, &c_b5, &
  471. work[1], &m);
  472. dgemm_("N", "N", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
  473. c_b42, &work[m * m + 1], &m);
  474. i__1 = m * m;
  475. dlassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
  476. ss = dscale * sqrt(dsum);
  477. dtrong = ss <= thresh;
  478. if (! dtrong) {
  479. goto L70;
  480. }
  481. }
  482. /* If the swap is accepted ("weakly" and "strongly"), apply the */
  483. /* transformations and set N1-by-N2 (2,1)-block to zero. */
  484. dlaset_("Full", n1, n2, &c_b5, &c_b5, &s[*n2], &c__4);
  485. /* copy back M-by-M diagonal block starting at index J1 of (A, B) */
  486. dlacpy_("F", &m, &m, s, &c__4, &a[*j1 + *j1 * a_dim1], lda)
  487. ;
  488. dlacpy_("F", &m, &m, t, &c__4, &b[*j1 + *j1 * b_dim1], ldb)
  489. ;
  490. dlaset_("Full", &c__4, &c__4, &c_b5, &c_b5, t, &c__4);
  491. /* Standardize existing 2-by-2 blocks. */
  492. i__1 = m * m;
  493. for (i__ = 1; i__ <= i__1; ++i__) {
  494. work[i__] = 0.;
  495. /* L50: */
  496. }
  497. work[1] = 1.;
  498. t[0] = 1.;
  499. idum = *lwork - m * m - 2;
  500. if (*n2 > 1) {
  501. dlagv2_(&a[*j1 + *j1 * a_dim1], lda, &b[*j1 + *j1 * b_dim1], ldb,
  502. ar, ai, be, &work[1], &work[2], t, &t[1]);
  503. work[m + 1] = -work[2];
  504. work[m + 2] = work[1];
  505. t[*n2 + (*n2 << 2) - 5] = t[0];
  506. t[4] = -t[1];
  507. }
  508. work[m * m] = 1.;
  509. t[m + (m << 2) - 5] = 1.;
  510. if (*n1 > 1) {
  511. dlagv2_(&a[*j1 + *n2 + (*j1 + *n2) * a_dim1], lda, &b[*j1 + *n2 +
  512. (*j1 + *n2) * b_dim1], ldb, taur, taul, &work[m * m + 1],
  513. &work[*n2 * m + *n2 + 1], &work[*n2 * m + *n2 + 2], &t[*
  514. n2 + 1 + (*n2 + 1 << 2) - 5], &t[m + (m - 1 << 2) - 5]);
  515. work[m * m] = work[*n2 * m + *n2 + 1];
  516. work[m * m - 1] = -work[*n2 * m + *n2 + 2];
  517. t[m + (m << 2) - 5] = t[*n2 + 1 + (*n2 + 1 << 2) - 5];
  518. t[m - 1 + (m << 2) - 5] = -t[m + (m - 1 << 2) - 5];
  519. }
  520. dgemm_("T", "N", n2, n1, n2, &c_b42, &work[1], &m, &a[*j1 + (*j1 + *
  521. n2) * a_dim1], lda, &c_b5, &work[m * m + 1], n2);
  522. dlacpy_("Full", n2, n1, &work[m * m + 1], n2, &a[*j1 + (*j1 + *n2) *
  523. a_dim1], lda);
  524. dgemm_("T", "N", n2, n1, n2, &c_b42, &work[1], &m, &b[*j1 + (*j1 + *
  525. n2) * b_dim1], ldb, &c_b5, &work[m * m + 1], n2);
  526. dlacpy_("Full", n2, n1, &work[m * m + 1], n2, &b[*j1 + (*j1 + *n2) *
  527. b_dim1], ldb);
  528. dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, &work[1], &m, &c_b5, &
  529. work[m * m + 1], &m);
  530. dlacpy_("Full", &m, &m, &work[m * m + 1], &m, li, &c__4);
  531. dgemm_("N", "N", n2, n1, n1, &c_b42, &a[*j1 + (*j1 + *n2) * a_dim1],
  532. lda, &t[*n2 + 1 + (*n2 + 1 << 2) - 5], &c__4, &c_b5, &work[1],
  533. n2);
  534. dlacpy_("Full", n2, n1, &work[1], n2, &a[*j1 + (*j1 + *n2) * a_dim1],
  535. lda);
  536. dgemm_("N", "N", n2, n1, n1, &c_b42, &b[*j1 + (*j1 + *n2) * b_dim1],
  537. ldb, &t[*n2 + 1 + (*n2 + 1 << 2) - 5], &c__4, &c_b5, &work[1],
  538. n2);
  539. dlacpy_("Full", n2, n1, &work[1], n2, &b[*j1 + (*j1 + *n2) * b_dim1],
  540. ldb);
  541. dgemm_("T", "N", &m, &m, &m, &c_b42, ir, &c__4, t, &c__4, &c_b5, &
  542. work[1], &m);
  543. dlacpy_("Full", &m, &m, &work[1], &m, ir, &c__4);
  544. /* Accumulate transformations into Q and Z if requested. */
  545. if (*wantq) {
  546. dgemm_("N", "N", n, &m, &m, &c_b42, &q[*j1 * q_dim1 + 1], ldq, li,
  547. &c__4, &c_b5, &work[1], n);
  548. dlacpy_("Full", n, &m, &work[1], n, &q[*j1 * q_dim1 + 1], ldq);
  549. }
  550. if (*wantz) {
  551. dgemm_("N", "N", n, &m, &m, &c_b42, &z__[*j1 * z_dim1 + 1], ldz,
  552. ir, &c__4, &c_b5, &work[1], n);
  553. dlacpy_("Full", n, &m, &work[1], n, &z__[*j1 * z_dim1 + 1], ldz);
  554. }
  555. /* Update (A(J1:J1+M-1, M+J1:N), B(J1:J1+M-1, M+J1:N)) and */
  556. /* (A(1:J1-1, J1:J1+M), B(1:J1-1, J1:J1+M)). */
  557. i__ = *j1 + m;
  558. if (i__ <= *n) {
  559. i__1 = *n - i__ + 1;
  560. dgemm_("T", "N", &m, &i__1, &m, &c_b42, li, &c__4, &a[*j1 + i__ *
  561. a_dim1], lda, &c_b5, &work[1], &m);
  562. i__1 = *n - i__ + 1;
  563. dlacpy_("Full", &m, &i__1, &work[1], &m, &a[*j1 + i__ * a_dim1],
  564. lda);
  565. i__1 = *n - i__ + 1;
  566. dgemm_("T", "N", &m, &i__1, &m, &c_b42, li, &c__4, &b[*j1 + i__ *
  567. b_dim1], lda, &c_b5, &work[1], &m);
  568. i__1 = *n - i__ + 1;
  569. dlacpy_("Full", &m, &i__1, &work[1], &m, &b[*j1 + i__ * b_dim1],
  570. ldb);
  571. }
  572. i__ = *j1 - 1;
  573. if (i__ > 0) {
  574. dgemm_("N", "N", &i__, &m, &m, &c_b42, &a[*j1 * a_dim1 + 1], lda,
  575. ir, &c__4, &c_b5, &work[1], &i__);
  576. dlacpy_("Full", &i__, &m, &work[1], &i__, &a[*j1 * a_dim1 + 1],
  577. lda);
  578. dgemm_("N", "N", &i__, &m, &m, &c_b42, &b[*j1 * b_dim1 + 1], ldb,
  579. ir, &c__4, &c_b5, &work[1], &i__);
  580. dlacpy_("Full", &i__, &m, &work[1], &i__, &b[*j1 * b_dim1 + 1],
  581. ldb);
  582. }
  583. /* Exit with INFO = 0 if swap was successfully performed. */
  584. return 0;
  585. }
  586. /* Exit with INFO = 1 if swap was rejected. */
  587. L70:
  588. *info = 1;
  589. return 0;
  590. /* End of DTGEX2 */
  591. } /* dtgex2_ */