14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
22 typedef BLASLONG blasint;
24 #define blasabs(x) llabs(x)
26 #define blasabs(x) labs(x)
30 #define blasabs(x) abs(x)
33 typedef blasint integer;
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
63 /* Extern is for use with -E */
74 /*external read, write*/
83 /*internal read, write*/
113 /*rewind, backspace, endfile*/
125 ftnint *inex; /*parameters in standard's order*/
151 union Multitype { /* for multiple entry points */
162 typedef union Multitype Multitype;
164 struct Vardesc { /* for Namelist */
170 typedef struct Vardesc Vardesc;
177 typedef struct Namelist Namelist;
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b) ((a) >> (b) & 1)
186 #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) { ftnlen i, nc, ll; char *f__rp, *lp; ll = (llp); lp = (lpp); for(i=0; i < (int)*(np); ++i) { nc = ll; if((rnp)[i] < nc) nc = (rnp)[i]; ll -= nc; f__rp = (rpp)[i]; while(--nc >= 0) *lp++ = *(f__rp)++; } while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
261 /* procedure parameter types for -A and -C++ */
263 #define F2C_proc_par_types 1
265 typedef logical (*L_fp)(...);
267 typedef logical (*L_fp)();
270 static float spow_ui(float x, integer n) {
271 float pow=1.0; unsigned long int u;
273 if(n < 0) n = -n, x = 1/x;
282 static double dpow_ui(double x, integer n) {
283 double pow=1.0; unsigned long int u;
285 if(n < 0) n = -n, x = 1/x;
295 static _Fcomplex cpow_ui(complex x, integer n) {
296 complex pow={1.0,0.0}; unsigned long int u;
298 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
300 if(u & 01) pow.r *= x.r, pow.i *= x.i;
301 if(u >>= 1) x.r *= x.r, x.i *= x.i;
305 _Fcomplex p={pow.r, pow.i};
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310 _Complex float pow=1.0; unsigned long int u;
312 if(n < 0) n = -n, x = 1/x;
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324 _Dcomplex pow={1.0,0.0}; unsigned long int u;
326 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
328 if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329 if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
333 _Dcomplex p = {pow._Val[0], pow._Val[1]};
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338 _Complex double pow=1.0; unsigned long int u;
340 if(n < 0) n = -n, x = 1/x;
350 static integer pow_ii(integer x, integer n) {
351 integer pow; unsigned long int u;
353 if (n == 0 || x == 1) pow = 1;
354 else if (x != -1) pow = x == 0 ? 1/x : 0;
357 if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
369 double m; integer i, mi;
370 for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371 if (w[i-1]>m) mi=i ,m=w[i-1];
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
376 float m; integer i, mi;
377 for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378 if (w[i-1]>m) mi=i ,m=w[i-1];
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382 integer n = *n_, incx = *incx_, incy = *incy_, i;
384 _Fcomplex zdotc = {0.0, 0.0};
385 if (incx == 1 && incy == 1) {
386 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387 zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388 zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
391 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392 zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393 zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
399 _Complex float zdotc = 0.0;
400 if (incx == 1 && incy == 1) {
401 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402 zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
405 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406 zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413 integer n = *n_, incx = *incx_, incy = *incy_, i;
415 _Dcomplex zdotc = {0.0, 0.0};
416 if (incx == 1 && incy == 1) {
417 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418 zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419 zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
422 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423 zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424 zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
430 _Complex double zdotc = 0.0;
431 if (incx == 1 && incy == 1) {
432 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433 zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
436 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437 zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444 integer n = *n_, incx = *incx_, incy = *incy_, i;
446 _Fcomplex zdotc = {0.0, 0.0};
447 if (incx == 1 && incy == 1) {
448 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449 zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450 zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
453 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454 zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455 zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
461 _Complex float zdotc = 0.0;
462 if (incx == 1 && incy == 1) {
463 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464 zdotc += Cf(&x[i]) * Cf(&y[i]);
467 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468 zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475 integer n = *n_, incx = *incx_, incy = *incy_, i;
477 _Dcomplex zdotc = {0.0, 0.0};
478 if (incx == 1 && incy == 1) {
479 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480 zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481 zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
484 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485 zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486 zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
492 _Complex double zdotc = 0.0;
493 if (incx == 1 && incy == 1) {
494 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495 zdotc += Cd(&x[i]) * Cd(&y[i]);
498 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499 zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
505 /* -- translated by f2c (version 20000121).
506 You must link the resulting object file with the libraries:
507 -lf2c -lm (in that order)
513 /* Table of constant values */
515 static integer c__4 = 4;
516 static real c_b5 = 0.f;
517 static integer c__1 = 1;
518 static integer c__2 = 2;
519 static real c_b42 = 1.f;
520 static real c_b48 = -1.f;
521 static integer c__0 = 0;
523 /* > \brief \b STGEX2 swaps adjacent diagonal blocks in an upper (quasi) triangular matrix pair by an orthogon
524 al equivalence transformation. */
526 /* =========== DOCUMENTATION =========== */
528 /* Online html documentation available at */
529 /* http://www.netlib.org/lapack/explore-html/ */
532 /* > Download STGEX2 + dependencies */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/stgex2.
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/stgex2.
539 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/stgex2.
547 /* SUBROUTINE STGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z, */
548 /* LDZ, J1, N1, N2, WORK, LWORK, INFO ) */
550 /* LOGICAL WANTQ, WANTZ */
551 /* INTEGER INFO, J1, LDA, LDB, LDQ, LDZ, LWORK, N, N1, N2 */
552 /* REAL A( LDA, * ), B( LDB, * ), Q( LDQ, * ), */
553 /* $ WORK( * ), Z( LDZ, * ) */
556 /* > \par Purpose: */
561 /* > STGEX2 swaps adjacent diagonal blocks (A11, B11) and (A22, B22) */
562 /* > of size 1-by-1 or 2-by-2 in an upper (quasi) triangular matrix pair */
563 /* > (A, B) by an orthogonal equivalence transformation. */
565 /* > (A, B) must be in generalized real Schur canonical form (as returned */
566 /* > by SGGES), i.e. A is block upper triangular with 1-by-1 and 2-by-2 */
567 /* > diagonal blocks. B is upper triangular. */
569 /* > Optionally, the matrices Q and Z of generalized Schur vectors are */
572 /* > Q(in) * A(in) * Z(in)**T = Q(out) * A(out) * Z(out)**T */
573 /* > Q(in) * B(in) * Z(in)**T = Q(out) * B(out) * Z(out)**T */
580 /* > \param[in] WANTQ */
582 /* > WANTQ is LOGICAL */
583 /* > .TRUE. : update the left transformation matrix Q; */
584 /* > .FALSE.: do not update Q. */
587 /* > \param[in] WANTZ */
589 /* > WANTZ is LOGICAL */
590 /* > .TRUE. : update the right transformation matrix Z; */
591 /* > .FALSE.: do not update Z. */
597 /* > The order of the matrices A and B. N >= 0. */
600 /* > \param[in,out] A */
602 /* > A is REAL array, dimension (LDA,N) */
603 /* > On entry, the matrix A in the pair (A, B). */
604 /* > On exit, the updated matrix A. */
607 /* > \param[in] LDA */
609 /* > LDA is INTEGER */
610 /* > The leading dimension of the array A. LDA >= f2cmax(1,N). */
613 /* > \param[in,out] B */
615 /* > B is REAL array, dimension (LDB,N) */
616 /* > On entry, the matrix B in the pair (A, B). */
617 /* > On exit, the updated matrix B. */
620 /* > \param[in] LDB */
622 /* > LDB is INTEGER */
623 /* > The leading dimension of the array B. LDB >= f2cmax(1,N). */
626 /* > \param[in,out] Q */
628 /* > Q is REAL array, dimension (LDQ,N) */
629 /* > On entry, if WANTQ = .TRUE., the orthogonal matrix Q. */
630 /* > On exit, the updated matrix Q. */
631 /* > Not referenced if WANTQ = .FALSE.. */
634 /* > \param[in] LDQ */
636 /* > LDQ is INTEGER */
637 /* > The leading dimension of the array Q. LDQ >= 1. */
638 /* > If WANTQ = .TRUE., LDQ >= N. */
641 /* > \param[in,out] Z */
643 /* > Z is REAL array, dimension (LDZ,N) */
644 /* > On entry, if WANTZ =.TRUE., the orthogonal matrix Z. */
645 /* > On exit, the updated matrix Z. */
646 /* > Not referenced if WANTZ = .FALSE.. */
649 /* > \param[in] LDZ */
651 /* > LDZ is INTEGER */
652 /* > The leading dimension of the array Z. LDZ >= 1. */
653 /* > If WANTZ = .TRUE., LDZ >= N. */
656 /* > \param[in] J1 */
658 /* > J1 is INTEGER */
659 /* > The index to the first block (A11, B11). 1 <= J1 <= N. */
662 /* > \param[in] N1 */
664 /* > N1 is INTEGER */
665 /* > The order of the first block (A11, B11). N1 = 0, 1 or 2. */
668 /* > \param[in] N2 */
670 /* > N2 is INTEGER */
671 /* > The order of the second block (A22, B22). N2 = 0, 1 or 2. */
674 /* > \param[out] WORK */
676 /* > WORK is REAL array, dimension (MAX(1,LWORK)). */
679 /* > \param[in] LWORK */
681 /* > LWORK is INTEGER */
682 /* > The dimension of the array WORK. */
683 /* > LWORK >= MAX( N*(N2+N1), (N2+N1)*(N2+N1)*2 ) */
686 /* > \param[out] INFO */
688 /* > INFO is INTEGER */
689 /* > =0: Successful exit */
690 /* > >0: If INFO = 1, the transformed matrix (A, B) would be */
691 /* > too far from generalized Schur form; the blocks are */
692 /* > not swapped and (A, B) and (Q, Z) are unchanged. */
693 /* > The problem of swapping is too ill-conditioned. */
694 /* > <0: If INFO = -16: LWORK is too small. Appropriate value */
695 /* > for LWORK is returned in WORK(1). */
701 /* > \author Univ. of Tennessee */
702 /* > \author Univ. of California Berkeley */
703 /* > \author Univ. of Colorado Denver */
704 /* > \author NAG Ltd. */
706 /* > \date June 2017 */
708 /* > \ingroup realGEauxiliary */
710 /* > \par Further Details: */
711 /* ===================== */
713 /* > In the current code both weak and strong stability tests are */
714 /* > performed. The user can omit the strong stability test by changing */
715 /* > the internal logical parameter WANDS to .FALSE.. See ref. [2] for */
718 /* > \par Contributors: */
719 /* ================== */
721 /* > Bo Kagstrom and Peter Poromaa, Department of Computing Science, */
722 /* > Umea University, S-901 87 Umea, Sweden. */
724 /* > \par References: */
725 /* ================ */
729 /* > [1] B. Kagstrom; A Direct Method for Reordering Eigenvalues in the */
730 /* > Generalized Real Schur Form of a Regular Matrix Pair (A, B), in */
731 /* > M.S. Moonen et al (eds), Linear Algebra for Large Scale and */
732 /* > Real-Time Applications, Kluwer Academic Publ. 1993, pp 195-218. */
734 /* > [2] B. Kagstrom and P. Poromaa; Computing Eigenspaces with Specified */
735 /* > Eigenvalues of a Regular Matrix Pair (A, B) and Condition */
736 /* > Estimation: Theory, Algorithms and Software, */
737 /* > Report UMINF - 94.04, Department of Computing Science, Umea */
738 /* > University, S-901 87 Umea, Sweden, 1994. Also as LAPACK Working */
739 /* > Note 87. To appear in Numerical Algorithms, 1996. */
742 /* ===================================================================== */
743 /* Subroutine */ int stgex2_(logical *wantq, logical *wantz, integer *n, real
744 *a, integer *lda, real *b, integer *ldb, real *q, integer *ldq, real *
745 z__, integer *ldz, integer *j1, integer *n1, integer *n2, real *work,
746 integer *lwork, integer *info)
748 /* System generated locals */
749 integer a_dim1, a_offset, b_dim1, b_offset, q_dim1, q_offset, z_dim1,
750 z_offset, i__1, i__2;
753 /* Local variables */
757 real taul[4], dsum, taur[4], scpy[16] /* was [4][4] */, tcpy[16]
759 extern /* Subroutine */ int srot_(integer *, real *, integer *, real *,
760 integer *, real *, real *);
763 real s[16] /* was [4][4] */, t[16] /* was [4][4] */, scale, bqra21,
765 extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
766 real licop[16] /* was [4][4] */;
768 extern /* Subroutine */ int sgemm_(char *, char *, integer *, integer *,
769 integer *, real *, real *, integer *, real *, integer *, real *,
771 real ircop[16] /* was [4][4] */, dnorm;
773 extern /* Subroutine */ int slagv2_(real *, integer *, real *, integer *,
774 real *, real *, real *, real *, real *, real *, real *), sgeqr2_(
775 integer *, integer *, real *, integer *, real *, real *, integer *
776 ), sgerq2_(integer *, integer *, real *, integer *, real *, real *
779 extern /* Subroutine */ int sorg2r_(integer *, integer *, integer *, real
780 *, integer *, real *, real *, integer *), sorgr2_(integer *,
781 integer *, integer *, real *, integer *, real *, real *, integer *
783 real ar[2], sa, sb, li[16] /* was [4][4] */;
784 extern /* Subroutine */ int sorm2r_(char *, char *, integer *, integer *,
785 integer *, real *, integer *, real *, real *, integer *, real *,
786 integer *), sormr2_(char *, char *, integer *,
787 integer *, integer *, real *, integer *, real *, real *, integer *
788 , real *, integer *);
789 real dscale, ir[16] /* was [4][4] */;
790 extern /* Subroutine */ int stgsy2_(char *, integer *, integer *, integer
791 *, real *, integer *, real *, integer *, real *, integer *, real *
792 , integer *, real *, integer *, real *, integer *, real *, real *,
793 real *, integer *, integer *, integer *);
795 extern real slamch_(char *);
797 extern /* Subroutine */ int slacpy_(char *, integer *, integer *, real *,
798 integer *, real *, integer *), slartg_(real *, real *,
799 real *, real *, real *);
801 extern /* Subroutine */ int slaset_(char *, integer *, integer *, real *,
802 real *, real *, integer *), slassq_(integer *, real *,
803 integer *, real *, real *);
809 /* -- LAPACK auxiliary routine (version 3.7.1) -- */
810 /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
811 /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
815 /* ===================================================================== */
816 /* Replaced various illegal calls to SCOPY by calls to SLASET, or by DO */
817 /* loops. Sven Hammarling, 1/5/02. */
820 /* Parameter adjustments */
822 a_offset = 1 + a_dim1 * 1;
825 b_offset = 1 + b_dim1 * 1;
828 q_offset = 1 + q_dim1 * 1;
831 z_offset = 1 + z_dim1 * 1;
838 /* Quick return if possible */
840 if (*n <= 1 || *n1 <= 0 || *n2 <= 0) {
843 if (*n1 > *n || *j1 + *n1 > *n) {
848 i__1 = *n * m, i__2 = m * m << 1;
849 if (*lwork < f2cmax(i__1,i__2)) {
852 i__1 = *n * m, i__2 = m * m << 1;
853 work[1] = (real) f2cmax(i__1,i__2);
860 /* Make a local copy of selected block */
862 slaset_("Full", &c__4, &c__4, &c_b5, &c_b5, li, &c__4);
863 slaset_("Full", &c__4, &c__4, &c_b5, &c_b5, ir, &c__4);
864 slacpy_("Full", &m, &m, &a[*j1 + *j1 * a_dim1], lda, s, &c__4);
865 slacpy_("Full", &m, &m, &b[*j1 + *j1 * b_dim1], ldb, t, &c__4);
867 /* Compute threshold for testing acceptance of swapping. */
870 smlnum = slamch_("S") / eps;
873 slacpy_("Full", &m, &m, s, &c__4, &work[1], &m);
875 slassq_(&i__1, &work[1], &c__1, &dscale, &dsum);
876 slacpy_("Full", &m, &m, t, &c__4, &work[1], &m);
878 slassq_(&i__1, &work[1], &c__1, &dscale, &dsum);
879 dnorm = dscale * sqrt(dsum);
881 /* THRES has been changed from */
882 /* THRESH = MAX( TEN*EPS*SA, SMLNUM ) */
884 /* THRESH = MAX( TWENTY*EPS*SA, SMLNUM ) */
886 /* "Bug" reported by Ondra Kamenik, confirmed by Julie Langou, fixed by */
887 /* Jim Demmel and Guillaume Revy. See forum post 1783. */
890 r__1 = eps * 20.f * dnorm;
891 thresh = f2cmax(r__1,smlnum);
895 /* CASE 1: Swap 1-by-1 and 1-by-1 blocks. */
897 /* Compute orthogonal QL and RQ that swap 1-by-1 and 1-by-1 blocks */
898 /* using Givens rotations and perform the swap tentatively. */
900 f = s[5] * t[0] - t[5] * s[0];
901 g = s[5] * t[4] - t[5] * s[4];
904 slartg_(&f, &g, &ir[4], ir, &ddum);
907 srot_(&c__2, s, &c__1, &s[4], &c__1, ir, &ir[1]);
908 srot_(&c__2, t, &c__1, &t[4], &c__1, ir, &ir[1]);
910 slartg_(s, &s[1], li, &li[1], &ddum);
912 slartg_(t, &t[1], li, &li[1], &ddum);
914 srot_(&c__2, s, &c__4, &s[1], &c__4, li, &li[1]);
915 srot_(&c__2, t, &c__4, &t[1], &c__4, li, &li[1]);
919 /* Weak stability test: */
920 /* |S21| + |T21| <= O(EPS * F-norm((S, T))) */
922 ws = abs(s[1]) + abs(t[1]);
930 /* Strong stability test: */
931 /* F-norm((A-QL**T*S*QR, B-QL**T*T*QR)) <= O(EPS*F-norm((A, B))) */
933 slacpy_("Full", &m, &m, &a[*j1 + *j1 * a_dim1], lda, &work[m * m
935 sgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, s, &c__4, &c_b5, &
937 sgemm_("N", "T", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
938 c_b42, &work[m * m + 1], &m);
942 slassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
944 slacpy_("Full", &m, &m, &b[*j1 + *j1 * b_dim1], ldb, &work[m * m
946 sgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, t, &c__4, &c_b5, &
948 sgemm_("N", "T", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
949 c_b42, &work[m * m + 1], &m);
951 slassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
952 ss = dscale * sqrt(dsum);
953 strong = ss <= thresh;
959 /* Update (A(J1:J1+M-1, M+J1:N), B(J1:J1+M-1, M+J1:N)) and */
960 /* (A(1:J1-1, J1:J1+M), B(1:J1-1, J1:J1+M)). */
963 srot_(&i__1, &a[*j1 * a_dim1 + 1], &c__1, &a[(*j1 + 1) * a_dim1 + 1],
966 srot_(&i__1, &b[*j1 * b_dim1 + 1], &c__1, &b[(*j1 + 1) * b_dim1 + 1],
969 srot_(&i__1, &a[*j1 + *j1 * a_dim1], lda, &a[*j1 + 1 + *j1 * a_dim1],
972 srot_(&i__1, &b[*j1 + *j1 * b_dim1], ldb, &b[*j1 + 1 + *j1 * b_dim1],
975 /* Set N1-by-N2 (2,1) - blocks to ZERO. */
977 a[*j1 + 1 + *j1 * a_dim1] = 0.f;
978 b[*j1 + 1 + *j1 * b_dim1] = 0.f;
980 /* Accumulate transformations into Q and Z if requested. */
983 srot_(n, &z__[*j1 * z_dim1 + 1], &c__1, &z__[(*j1 + 1) * z_dim1 +
984 1], &c__1, ir, &ir[1]);
987 srot_(n, &q[*j1 * q_dim1 + 1], &c__1, &q[(*j1 + 1) * q_dim1 + 1],
991 /* Exit with INFO = 0 if swap was successfully performed. */
997 /* CASE 2: Swap 1-by-1 and 2-by-2 blocks, or 2-by-2 */
998 /* and 2-by-2 blocks. */
1000 /* Solve the generalized Sylvester equation */
1001 /* S11 * R - L * S22 = SCALE * S12 */
1002 /* T11 * R - L * T22 = SCALE * T12 */
1003 /* for R and L. Solutions in LI and IR. */
1005 slacpy_("Full", n1, n2, &t[(*n1 + 1 << 2) - 4], &c__4, li, &c__4);
1006 slacpy_("Full", n1, n2, &s[(*n1 + 1 << 2) - 4], &c__4, &ir[*n2 + 1 + (
1007 *n1 + 1 << 2) - 5], &c__4);
1008 stgsy2_("N", &c__0, n1, n2, s, &c__4, &s[*n1 + 1 + (*n1 + 1 << 2) - 5]
1009 , &c__4, &ir[*n2 + 1 + (*n1 + 1 << 2) - 5], &c__4, t, &c__4, &
1010 t[*n1 + 1 + (*n1 + 1 << 2) - 5], &c__4, li, &c__4, &scale, &
1011 dsum, &dscale, iwork, &idum, &linfo);
1013 /* Compute orthogonal matrix QL: */
1015 /* QL**T * LI = [ TL ] */
1019 /* [ SCALE * identity(N2) ] */
1022 for (i__ = 1; i__ <= i__1; ++i__) {
1023 sscal_(n1, &c_b48, &li[(i__ << 2) - 4], &c__1);
1024 li[*n1 + i__ + (i__ << 2) - 5] = scale;
1027 sgeqr2_(&m, n2, li, &c__4, taul, &work[1], &linfo);
1031 sorg2r_(&m, &m, n2, li, &c__4, taul, &work[1], &linfo);
1036 /* Compute orthogonal matrix RQ: */
1038 /* IR * RQ**T = [ 0 TR], */
1040 /* where IR = [ SCALE * identity(N1), R ] */
1043 for (i__ = 1; i__ <= i__1; ++i__) {
1044 ir[*n2 + i__ + (i__ << 2) - 5] = scale;
1047 sgerq2_(n1, &m, &ir[*n2], &c__4, taur, &work[1], &linfo);
1051 sorgr2_(&m, &m, n1, ir, &c__4, taur, &work[1], &linfo);
1056 /* Perform the swapping tentatively: */
1058 sgemm_("T", "N", &m, &m, &m, &c_b42, li, &c__4, s, &c__4, &c_b5, &
1060 sgemm_("N", "T", &m, &m, &m, &c_b42, &work[1], &m, ir, &c__4, &c_b5,
1062 sgemm_("T", "N", &m, &m, &m, &c_b42, li, &c__4, t, &c__4, &c_b5, &
1064 sgemm_("N", "T", &m, &m, &m, &c_b42, &work[1], &m, ir, &c__4, &c_b5,
1066 slacpy_("F", &m, &m, s, &c__4, scpy, &c__4);
1067 slacpy_("F", &m, &m, t, &c__4, tcpy, &c__4);
1068 slacpy_("F", &m, &m, ir, &c__4, ircop, &c__4);
1069 slacpy_("F", &m, &m, li, &c__4, licop, &c__4);
1071 /* Triangularize the B-part by an RQ factorization. */
1072 /* Apply transformation (from left) to A-part, giving S. */
1074 sgerq2_(&m, &m, t, &c__4, taur, &work[1], &linfo);
1078 sormr2_("R", "T", &m, &m, &m, t, &c__4, taur, s, &c__4, &work[1], &
1083 sormr2_("L", "N", &m, &m, &m, t, &c__4, taur, ir, &c__4, &work[1], &
1089 /* Compute F-norm(S21) in BRQA21. (T21 is 0.) */
1094 for (i__ = 1; i__ <= i__1; ++i__) {
1095 slassq_(n1, &s[*n2 + 1 + (i__ << 2) - 5], &c__1, &dscale, &dsum);
1098 brqa21 = dscale * sqrt(dsum);
1100 /* Triangularize the B-part by a QR factorization. */
1101 /* Apply transformation (from right) to A-part, giving S. */
1103 sgeqr2_(&m, &m, tcpy, &c__4, taul, &work[1], &linfo);
1107 sorm2r_("L", "T", &m, &m, &m, tcpy, &c__4, taul, scpy, &c__4, &work[1]
1109 sorm2r_("R", "N", &m, &m, &m, tcpy, &c__4, taul, licop, &c__4, &work[
1115 /* Compute F-norm(S21) in BQRA21. (T21 is 0.) */
1120 for (i__ = 1; i__ <= i__1; ++i__) {
1121 slassq_(n1, &scpy[*n2 + 1 + (i__ << 2) - 5], &c__1, &dscale, &
1125 bqra21 = dscale * sqrt(dsum);
1127 /* Decide which method to use. */
1128 /* Weak stability test: */
1129 /* F-norm(S21) <= O(EPS * F-norm((S, T))) */
1131 if (bqra21 <= brqa21 && bqra21 <= thresh) {
1132 slacpy_("F", &m, &m, scpy, &c__4, s, &c__4);
1133 slacpy_("F", &m, &m, tcpy, &c__4, t, &c__4);
1134 slacpy_("F", &m, &m, ircop, &c__4, ir, &c__4);
1135 slacpy_("F", &m, &m, licop, &c__4, li, &c__4);
1136 } else if (brqa21 >= thresh) {
1140 /* Set lower triangle of B-part to zero */
1144 slaset_("Lower", &i__1, &i__2, &c_b5, &c_b5, &t[1], &c__4);
1148 /* Strong stability test: */
1149 /* F-norm((A-QL*S*QR**T, B-QL*T*QR**T)) <= O(EPS*F-norm((A,B))) */
1151 slacpy_("Full", &m, &m, &a[*j1 + *j1 * a_dim1], lda, &work[m * m
1153 sgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, s, &c__4, &c_b5, &
1155 sgemm_("N", "N", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
1156 c_b42, &work[m * m + 1], &m);
1160 slassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
1162 slacpy_("Full", &m, &m, &b[*j1 + *j1 * b_dim1], ldb, &work[m * m
1164 sgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, t, &c__4, &c_b5, &
1166 sgemm_("N", "N", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
1167 c_b42, &work[m * m + 1], &m);
1169 slassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
1170 ss = dscale * sqrt(dsum);
1171 strong = ss <= thresh;
1178 /* If the swap is accepted ("weakly" and "strongly"), apply the */
1179 /* transformations and set N1-by-N2 (2,1)-block to zero. */
1181 slaset_("Full", n1, n2, &c_b5, &c_b5, &s[*n2], &c__4);
1183 /* copy back M-by-M diagonal block starting at index J1 of (A, B) */
1185 slacpy_("F", &m, &m, s, &c__4, &a[*j1 + *j1 * a_dim1], lda)
1187 slacpy_("F", &m, &m, t, &c__4, &b[*j1 + *j1 * b_dim1], ldb)
1189 slaset_("Full", &c__4, &c__4, &c_b5, &c_b5, t, &c__4);
1191 /* Standardize existing 2-by-2 blocks. */
1193 slaset_("Full", &m, &m, &c_b5, &c_b5, &work[1], &m);
1196 idum = *lwork - m * m - 2;
1198 slagv2_(&a[*j1 + *j1 * a_dim1], lda, &b[*j1 + *j1 * b_dim1], ldb,
1199 ar, ai, be, &work[1], &work[2], t, &t[1]);
1200 work[m + 1] = -work[2];
1201 work[m + 2] = work[1];
1202 t[*n2 + (*n2 << 2) - 5] = t[0];
1206 t[m + (m << 2) - 5] = 1.f;
1209 slagv2_(&a[*j1 + *n2 + (*j1 + *n2) * a_dim1], lda, &b[*j1 + *n2 +
1210 (*j1 + *n2) * b_dim1], ldb, taur, taul, &work[m * m + 1],
1211 &work[*n2 * m + *n2 + 1], &work[*n2 * m + *n2 + 2], &t[*
1212 n2 + 1 + (*n2 + 1 << 2) - 5], &t[m + (m - 1 << 2) - 5]);
1213 work[m * m] = work[*n2 * m + *n2 + 1];
1214 work[m * m - 1] = -work[*n2 * m + *n2 + 2];
1215 t[m + (m << 2) - 5] = t[*n2 + 1 + (*n2 + 1 << 2) - 5];
1216 t[m - 1 + (m << 2) - 5] = -t[m + (m - 1 << 2) - 5];
1218 sgemm_("T", "N", n2, n1, n2, &c_b42, &work[1], &m, &a[*j1 + (*j1 + *
1219 n2) * a_dim1], lda, &c_b5, &work[m * m + 1], n2);
1220 slacpy_("Full", n2, n1, &work[m * m + 1], n2, &a[*j1 + (*j1 + *n2) *
1222 sgemm_("T", "N", n2, n1, n2, &c_b42, &work[1], &m, &b[*j1 + (*j1 + *
1223 n2) * b_dim1], ldb, &c_b5, &work[m * m + 1], n2);
1224 slacpy_("Full", n2, n1, &work[m * m + 1], n2, &b[*j1 + (*j1 + *n2) *
1226 sgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, &work[1], &m, &c_b5, &
1227 work[m * m + 1], &m);
1228 slacpy_("Full", &m, &m, &work[m * m + 1], &m, li, &c__4);
1229 sgemm_("N", "N", n2, n1, n1, &c_b42, &a[*j1 + (*j1 + *n2) * a_dim1],
1230 lda, &t[*n2 + 1 + (*n2 + 1 << 2) - 5], &c__4, &c_b5, &work[1],
1232 slacpy_("Full", n2, n1, &work[1], n2, &a[*j1 + (*j1 + *n2) * a_dim1],
1234 sgemm_("N", "N", n2, n1, n1, &c_b42, &b[*j1 + (*j1 + *n2) * b_dim1],
1235 ldb, &t[*n2 + 1 + (*n2 + 1 << 2) - 5], &c__4, &c_b5, &work[1],
1237 slacpy_("Full", n2, n1, &work[1], n2, &b[*j1 + (*j1 + *n2) * b_dim1],
1239 sgemm_("T", "N", &m, &m, &m, &c_b42, ir, &c__4, t, &c__4, &c_b5, &
1241 slacpy_("Full", &m, &m, &work[1], &m, ir, &c__4);
1243 /* Accumulate transformations into Q and Z if requested. */
1246 sgemm_("N", "N", n, &m, &m, &c_b42, &q[*j1 * q_dim1 + 1], ldq, li,
1247 &c__4, &c_b5, &work[1], n);
1248 slacpy_("Full", n, &m, &work[1], n, &q[*j1 * q_dim1 + 1], ldq);
1253 sgemm_("N", "N", n, &m, &m, &c_b42, &z__[*j1 * z_dim1 + 1], ldz,
1254 ir, &c__4, &c_b5, &work[1], n);
1255 slacpy_("Full", n, &m, &work[1], n, &z__[*j1 * z_dim1 + 1], ldz);
1259 /* Update (A(J1:J1+M-1, M+J1:N), B(J1:J1+M-1, M+J1:N)) and */
1260 /* (A(1:J1-1, J1:J1+M), B(1:J1-1, J1:J1+M)). */
1264 i__1 = *n - i__ + 1;
1265 sgemm_("T", "N", &m, &i__1, &m, &c_b42, li, &c__4, &a[*j1 + i__ *
1266 a_dim1], lda, &c_b5, &work[1], &m);
1267 i__1 = *n - i__ + 1;
1268 slacpy_("Full", &m, &i__1, &work[1], &m, &a[*j1 + i__ * a_dim1],
1270 i__1 = *n - i__ + 1;
1271 sgemm_("T", "N", &m, &i__1, &m, &c_b42, li, &c__4, &b[*j1 + i__ *
1272 b_dim1], ldb, &c_b5, &work[1], &m);
1273 i__1 = *n - i__ + 1;
1274 slacpy_("Full", &m, &i__1, &work[1], &m, &b[*j1 + i__ * b_dim1],
1279 sgemm_("N", "N", &i__, &m, &m, &c_b42, &a[*j1 * a_dim1 + 1], lda,
1280 ir, &c__4, &c_b5, &work[1], &i__);
1281 slacpy_("Full", &i__, &m, &work[1], &i__, &a[*j1 * a_dim1 + 1],
1283 sgemm_("N", "N", &i__, &m, &m, &c_b42, &b[*j1 * b_dim1 + 1], ldb,
1284 ir, &c__4, &c_b5, &work[1], &i__);
1285 slacpy_("Full", &i__, &m, &work[1], &i__, &b[*j1 * b_dim1 + 1],
1289 /* Exit with INFO = 0 if swap was successfully performed. */
1295 /* Exit with INFO = 1 if swap was rejected. */