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