ad5b35696db696679c1fd6637b6c160dc18996bd
[platform/upstream/lapack.git] / SRC / sggevx.f
1 *> \brief <b> SGGEVX computes the eigenvalues and, optionally, the left and/or right eigenvectors for GE matrices</b>
2 *
3 *  =========== DOCUMENTATION ===========
4 *
5 * Online html documentation available at 
6 *            http://www.netlib.org/lapack/explore-html/ 
7 *
8 *> \htmlonly
9 *> Download SGGEVX + dependencies 
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sggevx.f"> 
11 *> [TGZ]</a> 
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sggevx.f"> 
13 *> [ZIP]</a> 
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sggevx.f"> 
15 *> [TXT]</a>
16 *> \endhtmlonly 
17 *
18 *  Definition:
19 *  ===========
20 *
21 *       SUBROUTINE SGGEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, B, LDB,
22 *                          ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, ILO,
23 *                          IHI, LSCALE, RSCALE, ABNRM, BBNRM, RCONDE,
24 *                          RCONDV, WORK, LWORK, IWORK, BWORK, INFO )
25
26 *       .. Scalar Arguments ..
27 *       CHARACTER          BALANC, JOBVL, JOBVR, SENSE
28 *       INTEGER            IHI, ILO, INFO, LDA, LDB, LDVL, LDVR, LWORK, N
29 *       REAL               ABNRM, BBNRM
30 *       ..
31 *       .. Array Arguments ..
32 *       LOGICAL            BWORK( * )
33 *       INTEGER            IWORK( * )
34 *       REAL               A( LDA, * ), ALPHAI( * ), ALPHAR( * ),
35 *      $                   B( LDB, * ), BETA( * ), LSCALE( * ),
36 *      $                   RCONDE( * ), RCONDV( * ), RSCALE( * ),
37 *      $                   VL( LDVL, * ), VR( LDVR, * ), WORK( * )
38 *       ..
39 *  
40 *
41 *> \par Purpose:
42 *  =============
43 *>
44 *> \verbatim
45 *>
46 *> SGGEVX computes for a pair of N-by-N real nonsymmetric matrices (A,B)
47 *> the generalized eigenvalues, and optionally, the left and/or right
48 *> generalized eigenvectors.
49 *>
50 *> Optionally also, it computes a balancing transformation to improve
51 *> the conditioning of the eigenvalues and eigenvectors (ILO, IHI,
52 *> LSCALE, RSCALE, ABNRM, and BBNRM), reciprocal condition numbers for
53 *> the eigenvalues (RCONDE), and reciprocal condition numbers for the
54 *> right eigenvectors (RCONDV).
55 *>
56 *> A generalized eigenvalue for a pair of matrices (A,B) is a scalar
57 *> lambda or a ratio alpha/beta = lambda, such that A - lambda*B is
58 *> singular. It is usually represented as the pair (alpha,beta), as
59 *> there is a reasonable interpretation for beta=0, and even for both
60 *> being zero.
61 *>
62 *> The right eigenvector v(j) corresponding to the eigenvalue lambda(j)
63 *> of (A,B) satisfies
64 *>
65 *>                  A * v(j) = lambda(j) * B * v(j) .
66 *>
67 *> The left eigenvector u(j) corresponding to the eigenvalue lambda(j)
68 *> of (A,B) satisfies
69 *>
70 *>                  u(j)**H * A  = lambda(j) * u(j)**H * B.
71 *>
72 *> where u(j)**H is the conjugate-transpose of u(j).
73 *>
74 *> \endverbatim
75 *
76 *  Arguments:
77 *  ==========
78 *
79 *> \param[in] BALANC
80 *> \verbatim
81 *>          BALANC is CHARACTER*1
82 *>          Specifies the balance option to be performed.
83 *>          = 'N':  do not diagonally scale or permute;
84 *>          = 'P':  permute only;
85 *>          = 'S':  scale only;
86 *>          = 'B':  both permute and scale.
87 *>          Computed reciprocal condition numbers will be for the
88 *>          matrices after permuting and/or balancing. Permuting does
89 *>          not change condition numbers (in exact arithmetic), but
90 *>          balancing does.
91 *> \endverbatim
92 *>
93 *> \param[in] JOBVL
94 *> \verbatim
95 *>          JOBVL is CHARACTER*1
96 *>          = 'N':  do not compute the left generalized eigenvectors;
97 *>          = 'V':  compute the left generalized eigenvectors.
98 *> \endverbatim
99 *>
100 *> \param[in] JOBVR
101 *> \verbatim
102 *>          JOBVR is CHARACTER*1
103 *>          = 'N':  do not compute the right generalized eigenvectors;
104 *>          = 'V':  compute the right generalized eigenvectors.
105 *> \endverbatim
106 *>
107 *> \param[in] SENSE
108 *> \verbatim
109 *>          SENSE is CHARACTER*1
110 *>          Determines which reciprocal condition numbers are computed.
111 *>          = 'N': none are computed;
112 *>          = 'E': computed for eigenvalues only;
113 *>          = 'V': computed for eigenvectors only;
114 *>          = 'B': computed for eigenvalues and eigenvectors.
115 *> \endverbatim
116 *>
117 *> \param[in] N
118 *> \verbatim
119 *>          N is INTEGER
120 *>          The order of the matrices A, B, VL, and VR.  N >= 0.
121 *> \endverbatim
122 *>
123 *> \param[in,out] A
124 *> \verbatim
125 *>          A is REAL array, dimension (LDA, N)
126 *>          On entry, the matrix A in the pair (A,B).
127 *>          On exit, A has been overwritten. If JOBVL='V' or JOBVR='V'
128 *>          or both, then A contains the first part of the real Schur
129 *>          form of the "balanced" versions of the input A and B.
130 *> \endverbatim
131 *>
132 *> \param[in] LDA
133 *> \verbatim
134 *>          LDA is INTEGER
135 *>          The leading dimension of A.  LDA >= max(1,N).
136 *> \endverbatim
137 *>
138 *> \param[in,out] B
139 *> \verbatim
140 *>          B is REAL array, dimension (LDB, N)
141 *>          On entry, the matrix B in the pair (A,B).
142 *>          On exit, B has been overwritten. If JOBVL='V' or JOBVR='V'
143 *>          or both, then B contains the second part of the real Schur
144 *>          form of the "balanced" versions of the input A and B.
145 *> \endverbatim
146 *>
147 *> \param[in] LDB
148 *> \verbatim
149 *>          LDB is INTEGER
150 *>          The leading dimension of B.  LDB >= max(1,N).
151 *> \endverbatim
152 *>
153 *> \param[out] ALPHAR
154 *> \verbatim
155 *>          ALPHAR is REAL array, dimension (N)
156 *> \endverbatim
157 *>
158 *> \param[out] ALPHAI
159 *> \verbatim
160 *>          ALPHAI is REAL array, dimension (N)
161 *> \endverbatim
162 *>
163 *> \param[out] BETA
164 *> \verbatim
165 *>          BETA is REAL array, dimension (N)
166 *>          On exit, (ALPHAR(j) + ALPHAI(j)*i)/BETA(j), j=1,...,N, will
167 *>          be the generalized eigenvalues.  If ALPHAI(j) is zero, then
168 *>          the j-th eigenvalue is real; if positive, then the j-th and
169 *>          (j+1)-st eigenvalues are a complex conjugate pair, with
170 *>          ALPHAI(j+1) negative.
171 *>
172 *>          Note: the quotients ALPHAR(j)/BETA(j) and ALPHAI(j)/BETA(j)
173 *>          may easily over- or underflow, and BETA(j) may even be zero.
174 *>          Thus, the user should avoid naively computing the ratio
175 *>          ALPHA/BETA. However, ALPHAR and ALPHAI will be always less
176 *>          than and usually comparable with norm(A) in magnitude, and
177 *>          BETA always less than and usually comparable with norm(B).
178 *> \endverbatim
179 *>
180 *> \param[out] VL
181 *> \verbatim
182 *>          VL is REAL array, dimension (LDVL,N)
183 *>          If JOBVL = 'V', the left eigenvectors u(j) are stored one
184 *>          after another in the columns of VL, in the same order as
185 *>          their eigenvalues. If the j-th eigenvalue is real, then
186 *>          u(j) = VL(:,j), the j-th column of VL. If the j-th and
187 *>          (j+1)-th eigenvalues form a complex conjugate pair, then
188 *>          u(j) = VL(:,j)+i*VL(:,j+1) and u(j+1) = VL(:,j)-i*VL(:,j+1).
189 *>          Each eigenvector will be scaled so the largest component have
190 *>          abs(real part) + abs(imag. part) = 1.
191 *>          Not referenced if JOBVL = 'N'.
192 *> \endverbatim
193 *>
194 *> \param[in] LDVL
195 *> \verbatim
196 *>          LDVL is INTEGER
197 *>          The leading dimension of the matrix VL. LDVL >= 1, and
198 *>          if JOBVL = 'V', LDVL >= N.
199 *> \endverbatim
200 *>
201 *> \param[out] VR
202 *> \verbatim
203 *>          VR is REAL array, dimension (LDVR,N)
204 *>          If JOBVR = 'V', the right eigenvectors v(j) are stored one
205 *>          after another in the columns of VR, in the same order as
206 *>          their eigenvalues. If the j-th eigenvalue is real, then
207 *>          v(j) = VR(:,j), the j-th column of VR. If the j-th and
208 *>          (j+1)-th eigenvalues form a complex conjugate pair, then
209 *>          v(j) = VR(:,j)+i*VR(:,j+1) and v(j+1) = VR(:,j)-i*VR(:,j+1).
210 *>          Each eigenvector will be scaled so the largest component have
211 *>          abs(real part) + abs(imag. part) = 1.
212 *>          Not referenced if JOBVR = 'N'.
213 *> \endverbatim
214 *>
215 *> \param[in] LDVR
216 *> \verbatim
217 *>          LDVR is INTEGER
218 *>          The leading dimension of the matrix VR. LDVR >= 1, and
219 *>          if JOBVR = 'V', LDVR >= N.
220 *> \endverbatim
221 *>
222 *> \param[out] ILO
223 *> \verbatim
224 *>          ILO is INTEGER
225 *> \endverbatim
226 *>
227 *> \param[out] IHI
228 *> \verbatim
229 *>          IHI is INTEGER
230 *>          ILO and IHI are integer values such that on exit
231 *>          A(i,j) = 0 and B(i,j) = 0 if i > j and
232 *>          j = 1,...,ILO-1 or i = IHI+1,...,N.
233 *>          If BALANC = 'N' or 'S', ILO = 1 and IHI = N.
234 *> \endverbatim
235 *>
236 *> \param[out] LSCALE
237 *> \verbatim
238 *>          LSCALE is REAL array, dimension (N)
239 *>          Details of the permutations and scaling factors applied
240 *>          to the left side of A and B.  If PL(j) is the index of the
241 *>          row interchanged with row j, and DL(j) is the scaling
242 *>          factor applied to row j, then
243 *>            LSCALE(j) = PL(j)  for j = 1,...,ILO-1
244 *>                      = DL(j)  for j = ILO,...,IHI
245 *>                      = PL(j)  for j = IHI+1,...,N.
246 *>          The order in which the interchanges are made is N to IHI+1,
247 *>          then 1 to ILO-1.
248 *> \endverbatim
249 *>
250 *> \param[out] RSCALE
251 *> \verbatim
252 *>          RSCALE is REAL array, dimension (N)
253 *>          Details of the permutations and scaling factors applied
254 *>          to the right side of A and B.  If PR(j) is the index of the
255 *>          column interchanged with column j, and DR(j) is the scaling
256 *>          factor applied to column j, then
257 *>            RSCALE(j) = PR(j)  for j = 1,...,ILO-1
258 *>                      = DR(j)  for j = ILO,...,IHI
259 *>                      = PR(j)  for j = IHI+1,...,N
260 *>          The order in which the interchanges are made is N to IHI+1,
261 *>          then 1 to ILO-1.
262 *> \endverbatim
263 *>
264 *> \param[out] ABNRM
265 *> \verbatim
266 *>          ABNRM is REAL
267 *>          The one-norm of the balanced matrix A.
268 *> \endverbatim
269 *>
270 *> \param[out] BBNRM
271 *> \verbatim
272 *>          BBNRM is REAL
273 *>          The one-norm of the balanced matrix B.
274 *> \endverbatim
275 *>
276 *> \param[out] RCONDE
277 *> \verbatim
278 *>          RCONDE is REAL array, dimension (N)
279 *>          If SENSE = 'E' or 'B', the reciprocal condition numbers of
280 *>          the eigenvalues, stored in consecutive elements of the array.
281 *>          For a complex conjugate pair of eigenvalues two consecutive
282 *>          elements of RCONDE are set to the same value. Thus RCONDE(j),
283 *>          RCONDV(j), and the j-th columns of VL and VR all correspond
284 *>          to the j-th eigenpair.
285 *>          If SENSE = 'N' or 'V', RCONDE is not referenced.
286 *> \endverbatim
287 *>
288 *> \param[out] RCONDV
289 *> \verbatim
290 *>          RCONDV is REAL array, dimension (N)
291 *>          If SENSE = 'V' or 'B', the estimated reciprocal condition
292 *>          numbers of the eigenvectors, stored in consecutive elements
293 *>          of the array. For a complex eigenvector two consecutive
294 *>          elements of RCONDV are set to the same value. If the
295 *>          eigenvalues cannot be reordered to compute RCONDV(j),
296 *>          RCONDV(j) is set to 0; this can only occur when the true
297 *>          value would be very small anyway.
298 *>          If SENSE = 'N' or 'E', RCONDV is not referenced.
299 *> \endverbatim
300 *>
301 *> \param[out] WORK
302 *> \verbatim
303 *>          WORK is REAL array, dimension (MAX(1,LWORK))
304 *>          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
305 *> \endverbatim
306 *>
307 *> \param[in] LWORK
308 *> \verbatim
309 *>          LWORK is INTEGER
310 *>          The dimension of the array WORK. LWORK >= max(1,2*N).
311 *>          If BALANC = 'S' or 'B', or JOBVL = 'V', or JOBVR = 'V',
312 *>          LWORK >= max(1,6*N).
313 *>          If SENSE = 'E', LWORK >= max(1,10*N).
314 *>          If SENSE = 'V' or 'B', LWORK >= 2*N*N+8*N+16.
315 *>
316 *>          If LWORK = -1, then a workspace query is assumed; the routine
317 *>          only calculates the optimal size of the WORK array, returns
318 *>          this value as the first entry of the WORK array, and no error
319 *>          message related to LWORK is issued by XERBLA.
320 *> \endverbatim
321 *>
322 *> \param[out] IWORK
323 *> \verbatim
324 *>          IWORK is INTEGER array, dimension (N+6)
325 *>          If SENSE = 'E', IWORK is not referenced.
326 *> \endverbatim
327 *>
328 *> \param[out] BWORK
329 *> \verbatim
330 *>          BWORK is LOGICAL array, dimension (N)
331 *>          If SENSE = 'N', BWORK is not referenced.
332 *> \endverbatim
333 *>
334 *> \param[out] INFO
335 *> \verbatim
336 *>          INFO is INTEGER
337 *>          = 0:  successful exit
338 *>          < 0:  if INFO = -i, the i-th argument had an illegal value.
339 *>          = 1,...,N:
340 *>                The QZ iteration failed.  No eigenvectors have been
341 *>                calculated, but ALPHAR(j), ALPHAI(j), and BETA(j)
342 *>                should be correct for j=INFO+1,...,N.
343 *>          > N:  =N+1: other than QZ iteration failed in SHGEQZ.
344 *>                =N+2: error return from STGEVC.
345 *> \endverbatim
346 *
347 *  Authors:
348 *  ========
349 *
350 *> \author Univ. of Tennessee 
351 *> \author Univ. of California Berkeley 
352 *> \author Univ. of Colorado Denver 
353 *> \author NAG Ltd. 
354 *
355 *> \date April 2012
356 *
357 *> \ingroup realGEeigen
358 *
359 *> \par Further Details:
360 *  =====================
361 *>
362 *> \verbatim
363 *>
364 *>  Balancing a matrix pair (A,B) includes, first, permuting rows and
365 *>  columns to isolate eigenvalues, second, applying diagonal similarity
366 *>  transformation to the rows and columns to make the rows and columns
367 *>  as close in norm as possible. The computed reciprocal condition
368 *>  numbers correspond to the balanced matrix. Permuting rows and columns
369 *>  will not change the condition numbers (in exact arithmetic) but
370 *>  diagonal scaling will.  For further explanation of balancing, see
371 *>  section 4.11.1.2 of LAPACK Users' Guide.
372 *>
373 *>  An approximate error bound on the chordal distance between the i-th
374 *>  computed generalized eigenvalue w and the corresponding exact
375 *>  eigenvalue lambda is
376 *>
377 *>       chord(w, lambda) <= EPS * norm(ABNRM, BBNRM) / RCONDE(I)
378 *>
379 *>  An approximate error bound for the angle between the i-th computed
380 *>  eigenvector VL(i) or VR(i) is given by
381 *>
382 *>       EPS * norm(ABNRM, BBNRM) / DIF(i).
383 *>
384 *>  For further explanation of the reciprocal condition numbers RCONDE
385 *>  and RCONDV, see section 4.11 of LAPACK User's Guide.
386 *> \endverbatim
387 *>
388 *  =====================================================================
389       SUBROUTINE SGGEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, B, LDB,
390      $                   ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, ILO,
391      $                   IHI, LSCALE, RSCALE, ABNRM, BBNRM, RCONDE,
392      $                   RCONDV, WORK, LWORK, IWORK, BWORK, INFO )
393 *
394 *  -- LAPACK driver routine (version 3.4.1) --
395 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
396 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
397 *     April 2012
398 *
399 *     .. Scalar Arguments ..
400       CHARACTER          BALANC, JOBVL, JOBVR, SENSE
401       INTEGER            IHI, ILO, INFO, LDA, LDB, LDVL, LDVR, LWORK, N
402       REAL               ABNRM, BBNRM
403 *     ..
404 *     .. Array Arguments ..
405       LOGICAL            BWORK( * )
406       INTEGER            IWORK( * )
407       REAL               A( LDA, * ), ALPHAI( * ), ALPHAR( * ),
408      $                   B( LDB, * ), BETA( * ), LSCALE( * ),
409      $                   RCONDE( * ), RCONDV( * ), RSCALE( * ),
410      $                   VL( LDVL, * ), VR( LDVR, * ), WORK( * )
411 *     ..
412 *
413 *  =====================================================================
414 *
415 *     .. Parameters ..
416       REAL               ZERO, ONE
417       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
418 *     ..
419 *     .. Local Scalars ..
420       LOGICAL            ILASCL, ILBSCL, ILV, ILVL, ILVR, LQUERY, NOSCL,
421      $                   PAIR, WANTSB, WANTSE, WANTSN, WANTSV
422       CHARACTER          CHTEMP
423       INTEGER            I, ICOLS, IERR, IJOBVL, IJOBVR, IN, IROWS,
424      $                   ITAU, IWRK, IWRK1, J, JC, JR, M, MAXWRK,
425      $                   MINWRK, MM
426       REAL               ANRM, ANRMTO, BIGNUM, BNRM, BNRMTO, EPS,
427      $                   SMLNUM, TEMP
428 *     ..
429 *     .. Local Arrays ..
430       LOGICAL            LDUMMA( 1 )
431 *     ..
432 *     .. External Subroutines ..
433       EXTERNAL           SGEQRF, SGGBAK, SGGBAL, SGGHRD, SHGEQZ, SLABAD,
434      $                   SLACPY, SLASCL, SLASET, SORGQR, SORMQR, STGEVC,
435      $                   STGSNA, XERBLA
436 *     ..
437 *     .. External Functions ..
438       LOGICAL            LSAME
439       INTEGER            ILAENV
440       REAL               SLAMCH, SLANGE
441       EXTERNAL           LSAME, ILAENV, SLAMCH, SLANGE
442 *     ..
443 *     .. Intrinsic Functions ..
444       INTRINSIC          ABS, MAX, SQRT
445 *     ..
446 *     .. Executable Statements ..
447 *
448 *     Decode the input arguments
449 *
450       IF( LSAME( JOBVL, 'N' ) ) THEN
451          IJOBVL = 1
452          ILVL = .FALSE.
453       ELSE IF( LSAME( JOBVL, 'V' ) ) THEN
454          IJOBVL = 2
455          ILVL = .TRUE.
456       ELSE
457          IJOBVL = -1
458          ILVL = .FALSE.
459       END IF
460 *
461       IF( LSAME( JOBVR, 'N' ) ) THEN
462          IJOBVR = 1
463          ILVR = .FALSE.
464       ELSE IF( LSAME( JOBVR, 'V' ) ) THEN
465          IJOBVR = 2
466          ILVR = .TRUE.
467       ELSE
468          IJOBVR = -1
469          ILVR = .FALSE.
470       END IF
471       ILV = ILVL .OR. ILVR
472 *
473       NOSCL  = LSAME( BALANC, 'N' ) .OR. LSAME( BALANC, 'P' )
474       WANTSN = LSAME( SENSE, 'N' )
475       WANTSE = LSAME( SENSE, 'E' )
476       WANTSV = LSAME( SENSE, 'V' )
477       WANTSB = LSAME( SENSE, 'B' )
478 *
479 *     Test the input arguments
480 *
481       INFO = 0
482       LQUERY = ( LWORK.EQ.-1 )
483       IF( .NOT.( NOSCL .OR. LSAME( BALANC, 'S' ) .OR.
484      $    LSAME( BALANC, 'B' ) ) ) THEN
485          INFO = -1
486       ELSE IF( IJOBVL.LE.0 ) THEN
487          INFO = -2
488       ELSE IF( IJOBVR.LE.0 ) THEN
489          INFO = -3
490       ELSE IF( .NOT.( WANTSN .OR. WANTSE .OR. WANTSB .OR. WANTSV ) )
491      $          THEN
492          INFO = -4
493       ELSE IF( N.LT.0 ) THEN
494          INFO = -5
495       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
496          INFO = -7
497       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
498          INFO = -9
499       ELSE IF( LDVL.LT.1 .OR. ( ILVL .AND. LDVL.LT.N ) ) THEN
500          INFO = -14
501       ELSE IF( LDVR.LT.1 .OR. ( ILVR .AND. LDVR.LT.N ) ) THEN
502          INFO = -16
503       END IF
504 *
505 *     Compute workspace
506 *      (Note: Comments in the code beginning "Workspace:" describe the
507 *       minimal amount of workspace needed at that point in the code,
508 *       as well as the preferred amount for good performance.
509 *       NB refers to the optimal block size for the immediately
510 *       following subroutine, as returned by ILAENV. The workspace is
511 *       computed assuming ILO = 1 and IHI = N, the worst case.)
512 *
513       IF( INFO.EQ.0 ) THEN
514          IF( N.EQ.0 ) THEN
515             MINWRK = 1
516             MAXWRK = 1
517          ELSE
518             IF( NOSCL .AND. .NOT.ILV ) THEN
519                MINWRK = 2*N
520             ELSE
521                MINWRK = 6*N
522             END IF
523             IF( WANTSE ) THEN
524                MINWRK = 10*N
525             ELSE IF( WANTSV .OR. WANTSB ) THEN
526                MINWRK = 2*N*( N + 4 ) + 16
527             END IF
528             MAXWRK = MINWRK
529             MAXWRK = MAX( MAXWRK,
530      $                    N + N*ILAENV( 1, 'SGEQRF', ' ', N, 1, N, 0 ) )
531             MAXWRK = MAX( MAXWRK,
532      $                    N + N*ILAENV( 1, 'SORMQR', ' ', N, 1, N, 0 ) )
533             IF( ILVL ) THEN
534                MAXWRK = MAX( MAXWRK, N +
535      $                       N*ILAENV( 1, 'SORGQR', ' ', N, 1, N, 0 ) )
536             END IF
537          END IF
538          WORK( 1 ) = MAXWRK
539 *
540          IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
541             INFO = -26
542          END IF
543       END IF
544 *
545       IF( INFO.NE.0 ) THEN
546          CALL XERBLA( 'SGGEVX', -INFO )
547          RETURN
548       ELSE IF( LQUERY ) THEN
549          RETURN
550       END IF
551 *
552 *     Quick return if possible
553 *
554       IF( N.EQ.0 )
555      $   RETURN
556 *
557 *
558 *     Get machine constants
559 *
560       EPS = SLAMCH( 'P' )
561       SMLNUM = SLAMCH( 'S' )
562       BIGNUM = ONE / SMLNUM
563       CALL SLABAD( SMLNUM, BIGNUM )
564       SMLNUM = SQRT( SMLNUM ) / EPS
565       BIGNUM = ONE / SMLNUM
566 *
567 *     Scale A if max element outside range [SMLNUM,BIGNUM]
568 *
569       ANRM = SLANGE( 'M', N, N, A, LDA, WORK )
570       ILASCL = .FALSE.
571       IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
572          ANRMTO = SMLNUM
573          ILASCL = .TRUE.
574       ELSE IF( ANRM.GT.BIGNUM ) THEN
575          ANRMTO = BIGNUM
576          ILASCL = .TRUE.
577       END IF
578       IF( ILASCL )
579      $   CALL SLASCL( 'G', 0, 0, ANRM, ANRMTO, N, N, A, LDA, IERR )
580 *
581 *     Scale B if max element outside range [SMLNUM,BIGNUM]
582 *
583       BNRM = SLANGE( 'M', N, N, B, LDB, WORK )
584       ILBSCL = .FALSE.
585       IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
586          BNRMTO = SMLNUM
587          ILBSCL = .TRUE.
588       ELSE IF( BNRM.GT.BIGNUM ) THEN
589          BNRMTO = BIGNUM
590          ILBSCL = .TRUE.
591       END IF
592       IF( ILBSCL )
593      $   CALL SLASCL( 'G', 0, 0, BNRM, BNRMTO, N, N, B, LDB, IERR )
594 *
595 *     Permute and/or balance the matrix pair (A,B)
596 *     (Workspace: need 6*N if BALANC = 'S' or 'B', 1 otherwise)
597 *
598       CALL SGGBAL( BALANC, N, A, LDA, B, LDB, ILO, IHI, LSCALE, RSCALE,
599      $             WORK, IERR )
600 *
601 *     Compute ABNRM and BBNRM
602 *
603       ABNRM = SLANGE( '1', N, N, A, LDA, WORK( 1 ) )
604       IF( ILASCL ) THEN
605          WORK( 1 ) = ABNRM
606          CALL SLASCL( 'G', 0, 0, ANRMTO, ANRM, 1, 1, WORK( 1 ), 1,
607      $                IERR )
608          ABNRM = WORK( 1 )
609       END IF
610 *
611       BBNRM = SLANGE( '1', N, N, B, LDB, WORK( 1 ) )
612       IF( ILBSCL ) THEN
613          WORK( 1 ) = BBNRM
614          CALL SLASCL( 'G', 0, 0, BNRMTO, BNRM, 1, 1, WORK( 1 ), 1,
615      $                IERR )
616          BBNRM = WORK( 1 )
617       END IF
618 *
619 *     Reduce B to triangular form (QR decomposition of B)
620 *     (Workspace: need N, prefer N*NB )
621 *
622       IROWS = IHI + 1 - ILO
623       IF( ILV .OR. .NOT.WANTSN ) THEN
624          ICOLS = N + 1 - ILO
625       ELSE
626          ICOLS = IROWS
627       END IF
628       ITAU = 1
629       IWRK = ITAU + IROWS
630       CALL SGEQRF( IROWS, ICOLS, B( ILO, ILO ), LDB, WORK( ITAU ),
631      $             WORK( IWRK ), LWORK+1-IWRK, IERR )
632 *
633 *     Apply the orthogonal transformation to A
634 *     (Workspace: need N, prefer N*NB)
635 *
636       CALL SORMQR( 'L', 'T', IROWS, ICOLS, IROWS, B( ILO, ILO ), LDB,
637      $             WORK( ITAU ), A( ILO, ILO ), LDA, WORK( IWRK ),
638      $             LWORK+1-IWRK, IERR )
639 *
640 *     Initialize VL and/or VR
641 *     (Workspace: need N, prefer N*NB)
642 *
643       IF( ILVL ) THEN
644          CALL SLASET( 'Full', N, N, ZERO, ONE, VL, LDVL )
645          IF( IROWS.GT.1 ) THEN
646             CALL SLACPY( 'L', IROWS-1, IROWS-1, B( ILO+1, ILO ), LDB,
647      $                   VL( ILO+1, ILO ), LDVL )
648          END IF
649          CALL SORGQR( IROWS, IROWS, IROWS, VL( ILO, ILO ), LDVL,
650      $                WORK( ITAU ), WORK( IWRK ), LWORK+1-IWRK, IERR )
651       END IF
652 *
653       IF( ILVR )
654      $   CALL SLASET( 'Full', N, N, ZERO, ONE, VR, LDVR )
655 *
656 *     Reduce to generalized Hessenberg form
657 *     (Workspace: none needed)
658 *
659       IF( ILV .OR. .NOT.WANTSN ) THEN
660 *
661 *        Eigenvectors requested -- work on whole matrix.
662 *
663          CALL SGGHRD( JOBVL, JOBVR, N, ILO, IHI, A, LDA, B, LDB, VL,
664      $                LDVL, VR, LDVR, IERR )
665       ELSE
666          CALL SGGHRD( 'N', 'N', IROWS, 1, IROWS, A( ILO, ILO ), LDA,
667      $                B( ILO, ILO ), LDB, VL, LDVL, VR, LDVR, IERR )
668       END IF
669 *
670 *     Perform QZ algorithm (Compute eigenvalues, and optionally, the
671 *     Schur forms and Schur vectors)
672 *     (Workspace: need N)
673 *
674       IF( ILV .OR. .NOT.WANTSN ) THEN
675          CHTEMP = 'S'
676       ELSE
677          CHTEMP = 'E'
678       END IF
679 *
680       CALL SHGEQZ( CHTEMP, JOBVL, JOBVR, N, ILO, IHI, A, LDA, B, LDB,
681      $             ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, WORK,
682      $             LWORK, IERR )
683       IF( IERR.NE.0 ) THEN
684          IF( IERR.GT.0 .AND. IERR.LE.N ) THEN
685             INFO = IERR
686          ELSE IF( IERR.GT.N .AND. IERR.LE.2*N ) THEN
687             INFO = IERR - N
688          ELSE
689             INFO = N + 1
690          END IF
691          GO TO 130
692       END IF
693 *
694 *     Compute Eigenvectors and estimate condition numbers if desired
695 *     (Workspace: STGEVC: need 6*N
696 *                 STGSNA: need 2*N*(N+2)+16 if SENSE = 'V' or 'B',
697 *                         need N otherwise )
698 *
699       IF( ILV .OR. .NOT.WANTSN ) THEN
700          IF( ILV ) THEN
701             IF( ILVL ) THEN
702                IF( ILVR ) THEN
703                   CHTEMP = 'B'
704                ELSE
705                   CHTEMP = 'L'
706                END IF
707             ELSE
708                CHTEMP = 'R'
709             END IF
710 *
711             CALL STGEVC( CHTEMP, 'B', LDUMMA, N, A, LDA, B, LDB, VL,
712      $                   LDVL, VR, LDVR, N, IN, WORK, IERR )
713             IF( IERR.NE.0 ) THEN
714                INFO = N + 2
715                GO TO 130
716             END IF
717          END IF
718 *
719          IF( .NOT.WANTSN ) THEN
720 *
721 *           compute eigenvectors (STGEVC) and estimate condition
722 *           numbers (STGSNA). Note that the definition of the condition
723 *           number is not invariant under transformation (u,v) to
724 *           (Q*u, Z*v), where (u,v) are eigenvectors of the generalized
725 *           Schur form (S,T), Q and Z are orthogonal matrices. In order
726 *           to avoid using extra 2*N*N workspace, we have to recalculate
727 *           eigenvectors and estimate one condition numbers at a time.
728 *
729             PAIR = .FALSE.
730             DO 20 I = 1, N
731 *
732                IF( PAIR ) THEN
733                   PAIR = .FALSE.
734                   GO TO 20
735                END IF
736                MM = 1
737                IF( I.LT.N ) THEN
738                   IF( A( I+1, I ).NE.ZERO ) THEN
739                      PAIR = .TRUE.
740                      MM = 2
741                   END IF
742                END IF
743 *
744                DO 10 J = 1, N
745                   BWORK( J ) = .FALSE.
746    10          CONTINUE
747                IF( MM.EQ.1 ) THEN
748                   BWORK( I ) = .TRUE.
749                ELSE IF( MM.EQ.2 ) THEN
750                   BWORK( I ) = .TRUE.
751                   BWORK( I+1 ) = .TRUE.
752                END IF
753 *
754                IWRK = MM*N + 1
755                IWRK1 = IWRK + MM*N
756 *
757 *              Compute a pair of left and right eigenvectors.
758 *              (compute workspace: need up to 4*N + 6*N)
759 *
760                IF( WANTSE .OR. WANTSB ) THEN
761                   CALL STGEVC( 'B', 'S', BWORK, N, A, LDA, B, LDB,
762      $                         WORK( 1 ), N, WORK( IWRK ), N, MM, M,
763      $                         WORK( IWRK1 ), IERR )
764                   IF( IERR.NE.0 ) THEN
765                      INFO = N + 2
766                      GO TO 130
767                   END IF
768                END IF
769 *
770                CALL STGSNA( SENSE, 'S', BWORK, N, A, LDA, B, LDB,
771      $                      WORK( 1 ), N, WORK( IWRK ), N, RCONDE( I ),
772      $                      RCONDV( I ), MM, M, WORK( IWRK1 ),
773      $                      LWORK-IWRK1+1, IWORK, IERR )
774 *
775    20       CONTINUE
776          END IF
777       END IF
778 *
779 *     Undo balancing on VL and VR and normalization
780 *     (Workspace: none needed)
781 *
782       IF( ILVL ) THEN
783          CALL SGGBAK( BALANC, 'L', N, ILO, IHI, LSCALE, RSCALE, N, VL,
784      $                LDVL, IERR )
785 *
786          DO 70 JC = 1, N
787             IF( ALPHAI( JC ).LT.ZERO )
788      $         GO TO 70
789             TEMP = ZERO
790             IF( ALPHAI( JC ).EQ.ZERO ) THEN
791                DO 30 JR = 1, N
792                   TEMP = MAX( TEMP, ABS( VL( JR, JC ) ) )
793    30          CONTINUE
794             ELSE
795                DO 40 JR = 1, N
796                   TEMP = MAX( TEMP, ABS( VL( JR, JC ) )+
797      $                   ABS( VL( JR, JC+1 ) ) )
798    40          CONTINUE
799             END IF
800             IF( TEMP.LT.SMLNUM )
801      $         GO TO 70
802             TEMP = ONE / TEMP
803             IF( ALPHAI( JC ).EQ.ZERO ) THEN
804                DO 50 JR = 1, N
805                   VL( JR, JC ) = VL( JR, JC )*TEMP
806    50          CONTINUE
807             ELSE
808                DO 60 JR = 1, N
809                   VL( JR, JC ) = VL( JR, JC )*TEMP
810                   VL( JR, JC+1 ) = VL( JR, JC+1 )*TEMP
811    60          CONTINUE
812             END IF
813    70    CONTINUE
814       END IF
815       IF( ILVR ) THEN
816          CALL SGGBAK( BALANC, 'R', N, ILO, IHI, LSCALE, RSCALE, N, VR,
817      $                LDVR, IERR )
818          DO 120 JC = 1, N
819             IF( ALPHAI( JC ).LT.ZERO )
820      $         GO TO 120
821             TEMP = ZERO
822             IF( ALPHAI( JC ).EQ.ZERO ) THEN
823                DO 80 JR = 1, N
824                   TEMP = MAX( TEMP, ABS( VR( JR, JC ) ) )
825    80          CONTINUE
826             ELSE
827                DO 90 JR = 1, N
828                   TEMP = MAX( TEMP, ABS( VR( JR, JC ) )+
829      $                   ABS( VR( JR, JC+1 ) ) )
830    90          CONTINUE
831             END IF
832             IF( TEMP.LT.SMLNUM )
833      $         GO TO 120
834             TEMP = ONE / TEMP
835             IF( ALPHAI( JC ).EQ.ZERO ) THEN
836                DO 100 JR = 1, N
837                   VR( JR, JC ) = VR( JR, JC )*TEMP
838   100          CONTINUE
839             ELSE
840                DO 110 JR = 1, N
841                   VR( JR, JC ) = VR( JR, JC )*TEMP
842                   VR( JR, JC+1 ) = VR( JR, JC+1 )*TEMP
843   110          CONTINUE
844             END IF
845   120    CONTINUE
846       END IF
847 *
848 *     Undo scaling if necessary
849 *
850   130 CONTINUE
851 *
852       IF( ILASCL ) THEN
853          CALL SLASCL( 'G', 0, 0, ANRMTO, ANRM, N, 1, ALPHAR, N, IERR )
854          CALL SLASCL( 'G', 0, 0, ANRMTO, ANRM, N, 1, ALPHAI, N, IERR )
855       END IF
856 *
857       IF( ILBSCL ) THEN
858          CALL SLASCL( 'G', 0, 0, BNRMTO, BNRM, N, 1, BETA, N, IERR )
859       END IF
860 *
861       WORK( 1 ) = MAXWRK
862       RETURN
863 *
864 *     End of SGGEVX
865 *
866       END