1 /* ./src_f77/dtgsna.f -- translated by f2c (version 20030320).
2    You must link the resulting object file with the libraries:
3 	-lf2c -lm   (in that order)
4 */
5 
6 #include <punc/vf2c.h>
7 
8 /* Table of constant values */
9 
10 static integer c__1 = 1;
11 static doublereal c_b19 = 1.;
12 static doublereal c_b21 = 0.;
13 static integer c__2 = 2;
14 static logical c_false = FALSE_;
15 static integer c__3 = 3;
16 
dtgsna_(char * job,char * howmny,logical * select,integer * n,doublereal * a,integer * lda,doublereal * b,integer * ldb,doublereal * vl,integer * ldvl,doublereal * vr,integer * ldvr,doublereal * s,doublereal * dif,integer * mm,integer * m,doublereal * work,integer * lwork,integer * iwork,integer * info,ftnlen job_len,ftnlen howmny_len)17 /* Subroutine */ int dtgsna_(char *job, char *howmny, logical *select,
18 	integer *n, doublereal *a, integer *lda, doublereal *b, integer *ldb,
19 	doublereal *vl, integer *ldvl, doublereal *vr, integer *ldvr,
20 	doublereal *s, doublereal *dif, integer *mm, integer *m, doublereal *
21 	work, integer *lwork, integer *iwork, integer *info, ftnlen job_len,
22 	ftnlen howmny_len)
23 {
24     /* System generated locals */
25     integer a_dim1, a_offset, b_dim1, b_offset, vl_dim1, vl_offset, vr_dim1,
26 	    vr_offset, i__1, i__2;
27     doublereal d__1, d__2;
28 
29     /* Builtin functions */
30     double sqrt(doublereal);
31 
32     /* Local variables */
33     static integer i__, k;
34     static doublereal c1, c2;
35     static integer n1, n2, ks, iz;
36     static doublereal eps, beta, cond;
37     extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *,
38 	    integer *);
39     static logical pair;
40     static integer ierr;
41     static doublereal uhav, uhbv;
42     static integer ifst;
43     static doublereal lnrm;
44     static integer ilst;
45     static doublereal rnrm;
46     extern /* Subroutine */ int dlag2_(doublereal *, integer *, doublereal *,
47 	    integer *, doublereal *, doublereal *, doublereal *, doublereal *,
48 	     doublereal *, doublereal *);
49     extern doublereal dnrm2_(integer *, doublereal *, integer *);
50     static doublereal root1, root2, scale;
51     extern logical lsame_(char *, char *, ftnlen, ftnlen);
52     extern /* Subroutine */ int dgemv_(char *, integer *, integer *,
53 	    doublereal *, doublereal *, integer *, doublereal *, integer *,
54 	    doublereal *, doublereal *, integer *, ftnlen);
55     static doublereal uhavi, uhbvi, tmpii;
56     static integer lwmin;
57     static logical wants;
58     static doublereal tmpir, tmpri, dummy[1], tmprr;
59     extern doublereal dlapy2_(doublereal *, doublereal *);
60     static doublereal dummy1[1];
61     extern doublereal dlamch_(char *, ftnlen);
62     static doublereal alphai, alphar;
63     extern /* Subroutine */ int dlacpy_(char *, integer *, integer *,
64 	    doublereal *, integer *, doublereal *, integer *, ftnlen),
65 	    xerbla_(char *, integer *, ftnlen), dtgexc_(logical *, logical *,
66 	    integer *, doublereal *, integer *, doublereal *, integer *,
67 	    doublereal *, integer *, doublereal *, integer *, integer *,
68 	    integer *, doublereal *, integer *, integer *);
69     static logical wantbh, wantdf, somcon;
70     static doublereal alprqt;
71     extern /* Subroutine */ int dtgsyl_(char *, integer *, integer *, integer
72 	    *, doublereal *, integer *, doublereal *, integer *, doublereal *,
73 	     integer *, doublereal *, integer *, doublereal *, integer *,
74 	    doublereal *, integer *, doublereal *, doublereal *, doublereal *,
75 	     integer *, integer *, integer *, ftnlen);
76     static doublereal smlnum;
77     static logical lquery;
78 
79 
80 /*  -- LAPACK routine (version 3.0) -- */
81 /*     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., */
82 /*     Courant Institute, Argonne National Lab, and Rice University */
83 /*     June 30, 1999 */
84 
85 /*     .. Scalar Arguments .. */
86 /*     .. */
87 /*     .. Array Arguments .. */
88 /*     .. */
89 
90 /*  Purpose */
91 /*  ======= */
92 
93 /*  DTGSNA estimates reciprocal condition numbers for specified */
94 /*  eigenvalues and/or eigenvectors of a matrix pair (A, B) in */
95 /*  generalized real Schur canonical form (or of any matrix pair */
96 /*  (Q*A*Z', Q*B*Z') with orthogonal matrices Q and Z, where */
97 /*  Z' denotes the transpose of Z. */
98 
99 /*  (A, B) must be in generalized real Schur form (as returned by DGGES), */
100 /*  i.e. A is block upper triangular with 1-by-1 and 2-by-2 diagonal */
101 /*  blocks. B is upper triangular. */
102 
103 
104 /*  Arguments */
105 /*  ========= */
106 
107 /*  JOB     (input) CHARACTER*1 */
108 /*          Specifies whether condition numbers are required for */
109 /*          eigenvalues (S) or eigenvectors (DIF): */
110 /*          = 'E': for eigenvalues only (S); */
111 /*          = 'V': for eigenvectors only (DIF); */
112 /*          = 'B': for both eigenvalues and eigenvectors (S and DIF). */
113 
114 /*  HOWMNY  (input) CHARACTER*1 */
115 /*          = 'A': compute condition numbers for all eigenpairs; */
116 /*          = 'S': compute condition numbers for selected eigenpairs */
117 /*                 specified by the array SELECT. */
118 
119 /*  SELECT  (input) LOGICAL array, dimension (N) */
120 /*          If HOWMNY = 'S', SELECT specifies the eigenpairs for which */
121 /*          condition numbers are required. To select condition numbers */
122 /*          for the eigenpair corresponding to a real eigenvalue w(j), */
123 /*          SELECT(j) must be set to .TRUE.. To select condition numbers */
124 /*          corresponding to a complex conjugate pair of eigenvalues w(j) */
125 /*          and w(j+1), either SELECT(j) or SELECT(j+1) or both, must be */
126 /*          set to .TRUE.. */
127 /*          If HOWMNY = 'A', SELECT is not referenced. */
128 
129 /*  N       (input) INTEGER */
130 /*          The order of the square matrix pair (A, B). N >= 0. */
131 
132 /*  A       (input) DOUBLE PRECISION array, dimension (LDA,N) */
133 /*          The upper quasi-triangular matrix A in the pair (A,B). */
134 
135 /*  LDA     (input) INTEGER */
136 /*          The leading dimension of the array A. LDA >= max(1,N). */
137 
138 /*  B       (input) DOUBLE PRECISION array, dimension (LDB,N) */
139 /*          The upper triangular matrix B in the pair (A,B). */
140 
141 /*  LDB     (input) INTEGER */
142 /*          The leading dimension of the array B. LDB >= max(1,N). */
143 
144 /*  VL      (input) DOUBLE PRECISION array, dimension (LDVL,M) */
145 /*          If JOB = 'E' or 'B', VL must contain left eigenvectors of */
146 /*          (A, B), corresponding to the eigenpairs specified by HOWMNY */
147 /*          and SELECT. The eigenvectors must be stored in consecutive */
148 /*          columns of VL, as returned by DTGEVC. */
149 /*          If JOB = 'V', VL is not referenced. */
150 
151 /*  LDVL    (input) INTEGER */
152 /*          The leading dimension of the array VL. LDVL >= 1. */
153 /*          If JOB = 'E' or 'B', LDVL >= N. */
154 
155 /*  VR      (input) DOUBLE PRECISION array, dimension (LDVR,M) */
156 /*          If JOB = 'E' or 'B', VR must contain right eigenvectors of */
157 /*          (A, B), corresponding to the eigenpairs specified by HOWMNY */
158 /*          and SELECT. The eigenvectors must be stored in consecutive */
159 /*          columns ov VR, as returned by DTGEVC. */
160 /*          If JOB = 'V', VR is not referenced. */
161 
162 /*  LDVR    (input) INTEGER */
163 /*          The leading dimension of the array VR. LDVR >= 1. */
164 /*          If JOB = 'E' or 'B', LDVR >= N. */
165 
166 /*  S       (output) DOUBLE PRECISION array, dimension (MM) */
167 /*          If JOB = 'E' or 'B', the reciprocal condition numbers of the */
168 /*          selected eigenvalues, stored in consecutive elements of the */
169 /*          array. For a complex conjugate pair of eigenvalues two */
170 /*          consecutive elements of S are set to the same value. Thus */
171 /*          S(j), DIF(j), and the j-th columns of VL and VR all */
172 /*          correspond to the same eigenpair (but not in general the */
173 /*          j-th eigenpair, unless all eigenpairs are selected). */
174 /*          If JOB = 'V', S is not referenced. */
175 
176 /*  DIF     (output) DOUBLE PRECISION array, dimension (MM) */
177 /*          If JOB = 'V' or 'B', the estimated reciprocal condition */
178 /*          numbers of the selected eigenvectors, stored in consecutive */
179 /*          elements of the array. For a complex eigenvector two */
180 /*          consecutive elements of DIF are set to the same value. If */
181 /*          the eigenvalues cannot be reordered to compute DIF(j), DIF(j) */
182 /*          is set to 0; this can only occur when the true value would be */
183 /*          very small anyway. */
184 /*          If JOB = 'E', DIF is not referenced. */
185 
186 /*  MM      (input) INTEGER */
187 /*          The number of elements in the arrays S and DIF. MM >= M. */
188 
189 /*  M       (output) INTEGER */
190 /*          The number of elements of the arrays S and DIF used to store */
191 /*          the specified condition numbers; for each selected real */
192 /*          eigenvalue one element is used, and for each selected complex */
193 /*          conjugate pair of eigenvalues, two elements are used. */
194 /*          If HOWMNY = 'A', M is set to N. */
195 
196 /*  WORK    (workspace/output) DOUBLE PRECISION array, dimension (LWORK) */
197 /*          If JOB = 'E', WORK is not referenced.  Otherwise, */
198 /*          on exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
199 
200 /*  LWORK   (input) INTEGER */
201 /*          The dimension of the array WORK. LWORK >= N. */
202 /*          If JOB = 'V' or 'B' LWORK >= 2*N*(N+2)+16. */
203 
204 /*          If LWORK = -1, then a workspace query is assumed; the routine */
205 /*          only calculates the optimal size of the WORK array, returns */
206 /*          this value as the first entry of the WORK array, and no error */
207 /*          message related to LWORK is issued by XERBLA. */
208 
209 /*  IWORK   (workspace) INTEGER array, dimension (N + 6) */
210 /*          If JOB = 'E', IWORK is not referenced. */
211 
212 /*  INFO    (output) INTEGER */
213 /*          =0: Successful exit */
214 /*          <0: If INFO = -i, the i-th argument had an illegal value */
215 
216 
217 /*  Further Details */
218 /*  =============== */
219 
220 /*  The reciprocal of the condition number of a generalized eigenvalue */
221 /*  w = (a, b) is defined as */
222 
223 /*       S(w) = (|u'Av|**2 + |u'Bv|**2)**(1/2) / (norm(u)*norm(v)) */
224 
225 /*  where u and v are the left and right eigenvectors of (A, B) */
226 /*  corresponding to w; |z| denotes the absolute value of the complex */
227 /*  number, and norm(u) denotes the 2-norm of the vector u. */
228 /*  The pair (a, b) corresponds to an eigenvalue w = a/b (= u'Av/u'Bv) */
229 /*  of the matrix pair (A, B). If both a and b equal zero, then (A B) is */
230 /*  singular and S(I) = -1 is returned. */
231 
232 /*  An approximate error bound on the chordal distance between the i-th */
233 /*  computed generalized eigenvalue w and the corresponding exact */
234 /*  eigenvalue lambda is */
235 
236 /*       chord(w, lambda) <= EPS * norm(A, B) / S(I) */
237 
238 /*  where EPS is the machine precision. */
239 
240 /*  The reciprocal of the condition number DIF(i) of right eigenvector u */
241 /*  and left eigenvector v corresponding to the generalized eigenvalue w */
242 /*  is defined as follows: */
243 
244 /*  a) If the i-th eigenvalue w = (a,b) is real */
245 
246 /*     Suppose U and V are orthogonal transformations such that */
247 
248 /*                U'*(A, B)*V  = (S, T) = ( a   *  ) ( b  *  )  1 */
249 /*                                        ( 0  S22 ),( 0 T22 )  n-1 */
250 /*                                          1  n-1     1 n-1 */
251 
252 /*     Then the reciprocal condition number DIF(i) is */
253 
254 /*                Difl((a, b), (S22, T22)) = sigma-min( Zl ), */
255 
256 /*     where sigma-min(Zl) denotes the smallest singular value of the */
257 /*     2(n-1)-by-2(n-1) matrix */
258 
259 /*         Zl = [ kron(a, In-1)  -kron(1, S22) ] */
260 /*              [ kron(b, In-1)  -kron(1, T22) ] . */
261 
262 /*     Here In-1 is the identity matrix of size n-1. kron(X, Y) is the */
263 /*     Kronecker product between the matrices X and Y. */
264 
265 /*     Note that if the default method for computing DIF(i) is wanted */
266 /*     (see DLATDF), then the parameter DIFDRI (see below) should be */
267 /*     changed from 3 to 4 (routine DLATDF(IJOB = 2 will be used)). */
268 /*     See DTGSYL for more details. */
269 
270 /*  b) If the i-th and (i+1)-th eigenvalues are complex conjugate pair, */
271 
272 /*     Suppose U and V are orthogonal transformations such that */
273 
274 /*                U'*(A, B)*V = (S, T) = ( S11  *   ) ( T11  *  )  2 */
275 /*                                       ( 0    S22 ),( 0    T22) n-2 */
276 /*                                         2    n-2     2    n-2 */
277 
278 /*     and (S11, T11) corresponds to the complex conjugate eigenvalue */
279 /*     pair (w, conjg(w)). There exist unitary matrices U1 and V1 such */
280 /*     that */
281 
282 /*         U1'*S11*V1 = ( s11 s12 )   and U1'*T11*V1 = ( t11 t12 ) */
283 /*                      (  0  s22 )                    (  0  t22 ) */
284 
285 /*     where the generalized eigenvalues w = s11/t11 and */
286 /*     conjg(w) = s22/t22. */
287 
288 /*     Then the reciprocal condition number DIF(i) is bounded by */
289 
290 /*         min( d1, max( 1, |real(s11)/real(s22)| )*d2 ) */
291 
292 /*     where, d1 = Difl((s11, t11), (s22, t22)) = sigma-min(Z1), where */
293 /*     Z1 is the complex 2-by-2 matrix */
294 
295 /*              Z1 =  [ s11  -s22 ] */
296 /*                    [ t11  -t22 ], */
297 
298 /*     This is done by computing (using real arithmetic) the */
299 /*     roots of the characteristical polynomial det(Z1' * Z1 - lambda I), */
300 /*     where Z1' denotes the conjugate transpose of Z1 and det(X) denotes */
301 /*     the determinant of X. */
302 
303 /*     and d2 is an upper bound on Difl((S11, T11), (S22, T22)), i.e. an */
304 /*     upper bound on sigma-min(Z2), where Z2 is (2n-2)-by-(2n-2) */
305 
306 /*              Z2 = [ kron(S11', In-2)  -kron(I2, S22) ] */
307 /*                   [ kron(T11', In-2)  -kron(I2, T22) ] */
308 
309 /*     Note that if the default method for computing DIF is wanted (see */
310 /*     DLATDF), then the parameter DIFDRI (see below) should be changed */
311 /*     from 3 to 4 (routine DLATDF(IJOB = 2 will be used)). See DTGSYL */
312 /*     for more details. */
313 
314 /*  For each eigenvalue/vector specified by SELECT, DIF stores a */
315 /*  Frobenius norm-based estimate of Difl. */
316 
317 /*  An approximate error bound for the i-th computed eigenvector VL(i) or */
318 /*  VR(i) is given by */
319 
320 /*             EPS * norm(A, B) / DIF(i). */
321 
322 /*  See ref. [2-3] for more details and further references. */
323 
324 /*  Based on contributions by */
325 /*     Bo Kagstrom and Peter Poromaa, Department of Computing Science, */
326 /*     Umea University, S-901 87 Umea, Sweden. */
327 
328 /*  References */
329 /*  ========== */
330 
331 /*  [1] B. Kagstrom; A Direct Method for Reordering Eigenvalues in the */
332 /*      Generalized Real Schur Form of a Regular Matrix Pair (A, B), in */
333 /*      M.S. Moonen et al (eds), Linear Algebra for Large Scale and */
334 /*      Real-Time Applications, Kluwer Academic Publ. 1993, pp 195-218. */
335 
336 /*  [2] B. Kagstrom and P. Poromaa; Computing Eigenspaces with Specified */
337 /*      Eigenvalues of a Regular Matrix Pair (A, B) and Condition */
338 /*      Estimation: Theory, Algorithms and Software, */
339 /*      Report UMINF - 94.04, Department of Computing Science, Umea */
340 /*      University, S-901 87 Umea, Sweden, 1994. Also as LAPACK Working */
341 /*      Note 87. To appear in Numerical Algorithms, 1996. */
342 
343 /*  [3] B. Kagstrom and P. Poromaa, LAPACK-Style Algorithms and Software */
344 /*      for Solving the Generalized Sylvester Equation and Estimating the */
345 /*      Separation between Regular Matrix Pairs, Report UMINF - 93.23, */
346 /*      Department of Computing Science, Umea University, S-901 87 Umea, */
347 /*      Sweden, December 1993, Revised April 1994, Also as LAPACK Working */
348 /*      Note 75.  To appear in ACM Trans. on Math. Software, Vol 22, */
349 /*      No 1, 1996. */
350 
351 /*  ===================================================================== */
352 
353 /*     .. Parameters .. */
354 /*     .. */
355 /*     .. Local Scalars .. */
356 /*     .. */
357 /*     .. Local Arrays .. */
358 /*     .. */
359 /*     .. External Functions .. */
360 /*     .. */
361 /*     .. External Subroutines .. */
362 /*     .. */
363 /*     .. Intrinsic Functions .. */
364 /*     .. */
365 /*     .. Executable Statements .. */
366 
367 /*     Decode and test the input parameters */
368 
369     /* Parameter adjustments */
370     --select;
371     a_dim1 = *lda;
372     a_offset = 1 + a_dim1;
373     a -= a_offset;
374     b_dim1 = *ldb;
375     b_offset = 1 + b_dim1;
376     b -= b_offset;
377     vl_dim1 = *ldvl;
378     vl_offset = 1 + vl_dim1;
379     vl -= vl_offset;
380     vr_dim1 = *ldvr;
381     vr_offset = 1 + vr_dim1;
382     vr -= vr_offset;
383     --s;
384     --dif;
385     --work;
386     --iwork;
387 
388     /* Function Body */
389     wantbh = lsame_(job, "B", (ftnlen)1, (ftnlen)1);
390     wants = lsame_(job, "E", (ftnlen)1, (ftnlen)1) || wantbh;
391     wantdf = lsame_(job, "V", (ftnlen)1, (ftnlen)1) || wantbh;
392 
393     somcon = lsame_(howmny, "S", (ftnlen)1, (ftnlen)1);
394 
395     *info = 0;
396     lquery = *lwork == -1;
397 
398     if (lsame_(job, "V", (ftnlen)1, (ftnlen)1) || lsame_(job, "B", (ftnlen)1,
399 	    (ftnlen)1)) {
400 /* Computing MAX */
401 	i__1 = 1, i__2 = (*n << 1) * (*n + 2) + 16;
402 	lwmin = max(i__1,i__2);
403     } else {
404 	lwmin = 1;
405     }
406 
407     if (! wants && ! wantdf) {
408 	*info = -1;
409     } else if (! lsame_(howmny, "A", (ftnlen)1, (ftnlen)1) && ! somcon) {
410 	*info = -2;
411     } else if (*n < 0) {
412 	*info = -4;
413     } else if (*lda < max(1,*n)) {
414 	*info = -6;
415     } else if (*ldb < max(1,*n)) {
416 	*info = -8;
417     } else if (wants && *ldvl < *n) {
418 	*info = -10;
419     } else if (wants && *ldvr < *n) {
420 	*info = -12;
421     } else {
422 
423 /*        Set M to the number of eigenpairs for which condition numbers */
424 /*        are required, and test MM. */
425 
426 	if (somcon) {
427 	    *m = 0;
428 	    pair = FALSE_;
429 	    i__1 = *n;
430 	    for (k = 1; k <= i__1; ++k) {
431 		if (pair) {
432 		    pair = FALSE_;
433 		} else {
434 		    if (k < *n) {
435 			if (a[k + 1 + k * a_dim1] == 0.) {
436 			    if (select[k]) {
437 				++(*m);
438 			    }
439 			} else {
440 			    pair = TRUE_;
441 			    if (select[k] || select[k + 1]) {
442 				*m += 2;
443 			    }
444 			}
445 		    } else {
446 			if (select[*n]) {
447 			    ++(*m);
448 			}
449 		    }
450 		}
451 /* L10: */
452 	    }
453 	} else {
454 	    *m = *n;
455 	}
456 
457 	if (*mm < *m) {
458 	    *info = -15;
459 	} else if (*lwork < lwmin && ! lquery) {
460 	    *info = -18;
461 /*        ELSE IF( WANTDF .AND. LWORK.LT.2*N*( N+2 )+16 ) THEN */
462 /*           INFO = -18 */
463 	}
464     }
465 
466     if (*info == 0) {
467 	work[1] = (doublereal) lwmin;
468     }
469 
470     if (*info != 0) {
471 	i__1 = -(*info);
472 	xerbla_("DTGSNA", &i__1, (ftnlen)6);
473 	return 0;
474     } else if (lquery) {
475 	return 0;
476     }
477 
478 /*     Quick return if possible */
479 
480     if (*n == 0) {
481 	return 0;
482     }
483 
484 /*     Get machine constants */
485 
486     eps = dlamch_("P", (ftnlen)1);
487     smlnum = dlamch_("S", (ftnlen)1) / eps;
488     ks = 0;
489     pair = FALSE_;
490 
491     i__1 = *n;
492     for (k = 1; k <= i__1; ++k) {
493 
494 /*        Determine whether A(k,k) begins a 1-by-1 or 2-by-2 block. */
495 
496 	if (pair) {
497 	    pair = FALSE_;
498 	    goto L20;
499 	} else {
500 	    if (k < *n) {
501 		pair = a[k + 1 + k * a_dim1] != 0.;
502 	    }
503 	}
504 
505 /*        Determine whether condition numbers are required for the k-th */
506 /*        eigenpair. */
507 
508 	if (somcon) {
509 	    if (pair) {
510 		if (! select[k] && ! select[k + 1]) {
511 		    goto L20;
512 		}
513 	    } else {
514 		if (! select[k]) {
515 		    goto L20;
516 		}
517 	    }
518 	}
519 
520 	++ks;
521 
522 	if (wants) {
523 
524 /*           Compute the reciprocal condition number of the k-th */
525 /*           eigenvalue. */
526 
527 	    if (pair) {
528 
529 /*              Complex eigenvalue pair. */
530 
531 		d__1 = dnrm2_(n, &vr[ks * vr_dim1 + 1], &c__1);
532 		d__2 = dnrm2_(n, &vr[(ks + 1) * vr_dim1 + 1], &c__1);
533 		rnrm = dlapy2_(&d__1, &d__2);
534 		d__1 = dnrm2_(n, &vl[ks * vl_dim1 + 1], &c__1);
535 		d__2 = dnrm2_(n, &vl[(ks + 1) * vl_dim1 + 1], &c__1);
536 		lnrm = dlapy2_(&d__1, &d__2);
537 		dgemv_("N", n, n, &c_b19, &a[a_offset], lda, &vr[ks * vr_dim1
538 			+ 1], &c__1, &c_b21, &work[1], &c__1, (ftnlen)1);
539 		tmprr = ddot_(n, &work[1], &c__1, &vl[ks * vl_dim1 + 1], &
540 			c__1);
541 		tmpri = ddot_(n, &work[1], &c__1, &vl[(ks + 1) * vl_dim1 + 1],
542 			 &c__1);
543 		dgemv_("N", n, n, &c_b19, &a[a_offset], lda, &vr[(ks + 1) *
544 			vr_dim1 + 1], &c__1, &c_b21, &work[1], &c__1, (ftnlen)
545 			1);
546 		tmpii = ddot_(n, &work[1], &c__1, &vl[(ks + 1) * vl_dim1 + 1],
547 			 &c__1);
548 		tmpir = ddot_(n, &work[1], &c__1, &vl[ks * vl_dim1 + 1], &
549 			c__1);
550 		uhav = tmprr + tmpii;
551 		uhavi = tmpir - tmpri;
552 		dgemv_("N", n, n, &c_b19, &b[b_offset], ldb, &vr[ks * vr_dim1
553 			+ 1], &c__1, &c_b21, &work[1], &c__1, (ftnlen)1);
554 		tmprr = ddot_(n, &work[1], &c__1, &vl[ks * vl_dim1 + 1], &
555 			c__1);
556 		tmpri = ddot_(n, &work[1], &c__1, &vl[(ks + 1) * vl_dim1 + 1],
557 			 &c__1);
558 		dgemv_("N", n, n, &c_b19, &b[b_offset], ldb, &vr[(ks + 1) *
559 			vr_dim1 + 1], &c__1, &c_b21, &work[1], &c__1, (ftnlen)
560 			1);
561 		tmpii = ddot_(n, &work[1], &c__1, &vl[(ks + 1) * vl_dim1 + 1],
562 			 &c__1);
563 		tmpir = ddot_(n, &work[1], &c__1, &vl[ks * vl_dim1 + 1], &
564 			c__1);
565 		uhbv = tmprr + tmpii;
566 		uhbvi = tmpir - tmpri;
567 		uhav = dlapy2_(&uhav, &uhavi);
568 		uhbv = dlapy2_(&uhbv, &uhbvi);
569 		cond = dlapy2_(&uhav, &uhbv);
570 		s[ks] = cond / (rnrm * lnrm);
571 		s[ks + 1] = s[ks];
572 
573 	    } else {
574 
575 /*              Real eigenvalue. */
576 
577 		rnrm = dnrm2_(n, &vr[ks * vr_dim1 + 1], &c__1);
578 		lnrm = dnrm2_(n, &vl[ks * vl_dim1 + 1], &c__1);
579 		dgemv_("N", n, n, &c_b19, &a[a_offset], lda, &vr[ks * vr_dim1
580 			+ 1], &c__1, &c_b21, &work[1], &c__1, (ftnlen)1);
581 		uhav = ddot_(n, &work[1], &c__1, &vl[ks * vl_dim1 + 1], &c__1)
582 			;
583 		dgemv_("N", n, n, &c_b19, &b[b_offset], ldb, &vr[ks * vr_dim1
584 			+ 1], &c__1, &c_b21, &work[1], &c__1, (ftnlen)1);
585 		uhbv = ddot_(n, &work[1], &c__1, &vl[ks * vl_dim1 + 1], &c__1)
586 			;
587 		cond = dlapy2_(&uhav, &uhbv);
588 		if (cond == 0.) {
589 		    s[ks] = -1.;
590 		} else {
591 		    s[ks] = cond / (rnrm * lnrm);
592 		}
593 	    }
594 	}
595 
596 	if (wantdf) {
597 	    if (*n == 1) {
598 		dif[ks] = dlapy2_(&a[a_dim1 + 1], &b[b_dim1 + 1]);
599 		goto L20;
600 	    }
601 
602 /*           Estimate the reciprocal condition number of the k-th */
603 /*           eigenvectors. */
604 	    if (pair) {
605 
606 /*              Copy the  2-by 2 pencil beginning at (A(k,k), B(k, k)). */
607 /*              Compute the eigenvalue(s) at position K. */
608 
609 		work[1] = a[k + k * a_dim1];
610 		work[2] = a[k + 1 + k * a_dim1];
611 		work[3] = a[k + (k + 1) * a_dim1];
612 		work[4] = a[k + 1 + (k + 1) * a_dim1];
613 		work[5] = b[k + k * b_dim1];
614 		work[6] = b[k + 1 + k * b_dim1];
615 		work[7] = b[k + (k + 1) * b_dim1];
616 		work[8] = b[k + 1 + (k + 1) * b_dim1];
617 		d__1 = smlnum * eps;
618 		dlag2_(&work[1], &c__2, &work[5], &c__2, &d__1, &beta, dummy1,
619 			 &alphar, dummy, &alphai);
620 		alprqt = 1.;
621 		c1 = (alphar * alphar + alphai * alphai + beta * beta) * 2.;
622 		c2 = beta * 4. * beta * alphai * alphai;
623 		root1 = c1 + sqrt(c1 * c1 - c2 * 4.);
624 		root2 = c2 / root1;
625 		root1 /= 2.;
626 /* Computing MIN */
627 		d__1 = sqrt(root1), d__2 = sqrt(root2);
628 		cond = min(d__1,d__2);
629 	    }
630 
631 /*           Copy the matrix (A, B) to the array WORK and swap the */
632 /*           diagonal block beginning at A(k,k) to the (1,1) position. */
633 
634 	    dlacpy_("Full", n, n, &a[a_offset], lda, &work[1], n, (ftnlen)4);
635 	    dlacpy_("Full", n, n, &b[b_offset], ldb, &work[*n * *n + 1], n, (
636 		    ftnlen)4);
637 	    ifst = k;
638 	    ilst = 1;
639 
640 	    i__2 = *lwork - (*n << 1) * *n;
641 	    dtgexc_(&c_false, &c_false, n, &work[1], n, &work[*n * *n + 1], n,
642 		     dummy, &c__1, dummy1, &c__1, &ifst, &ilst, &work[(*n * *
643 		    n << 1) + 1], &i__2, &ierr);
644 
645 	    if (ierr > 0) {
646 
647 /*              Ill-conditioned problem - swap rejected. */
648 
649 		dif[ks] = 0.;
650 	    } else {
651 
652 /*              Reordering successful, solve generalized Sylvester */
653 /*              equation for R and L, */
654 /*                         A22 * R - L * A11 = A12 */
655 /*                         B22 * R - L * B11 = B12, */
656 /*              and compute estimate of Difl((A11,B11), (A22, B22)). */
657 
658 		n1 = 1;
659 		if (work[2] != 0.) {
660 		    n1 = 2;
661 		}
662 		n2 = *n - n1;
663 		if (n2 == 0) {
664 		    dif[ks] = cond;
665 		} else {
666 		    i__ = *n * *n + 1;
667 		    iz = (*n << 1) * *n + 1;
668 		    i__2 = *lwork - (*n << 1) * *n;
669 		    dtgsyl_("N", &c__3, &n2, &n1, &work[*n * n1 + n1 + 1], n,
670 			    &work[1], n, &work[n1 + 1], n, &work[*n * n1 + n1
671 			    + i__], n, &work[i__], n, &work[n1 + i__], n, &
672 			    scale, &dif[ks], &work[iz + 1], &i__2, &iwork[1],
673 			    &ierr, (ftnlen)1);
674 
675 		    if (pair) {
676 /* Computing MIN */
677 			d__1 = max(1.,alprqt) * dif[ks];
678 			dif[ks] = min(d__1,cond);
679 		    }
680 		}
681 	    }
682 	    if (pair) {
683 		dif[ks + 1] = dif[ks];
684 	    }
685 	}
686 	if (pair) {
687 	    ++ks;
688 	}
689 
690 L20:
691 	;
692     }
693     work[1] = (doublereal) lwmin;
694     return 0;
695 
696 /*     End of DTGSNA */
697 
698 } /* dtgsna_ */
699 
700