001:       SUBROUTINE SGGEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, B, LDB,
002:      $                   ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, ILO,
003:      $                   IHI, LSCALE, RSCALE, ABNRM, BBNRM, RCONDE,
004:      $                   RCONDV, WORK, LWORK, IWORK, BWORK, INFO )
005: *
006: *  -- LAPACK driver routine (version 3.2) --
007: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
008: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
009: *     November 2006
010: *
011: *     .. Scalar Arguments ..
012:       CHARACTER          BALANC, JOBVL, JOBVR, SENSE
013:       INTEGER            IHI, ILO, INFO, LDA, LDB, LDVL, LDVR, LWORK, N
014:       REAL               ABNRM, BBNRM
015: *     ..
016: *     .. Array Arguments ..
017:       LOGICAL            BWORK( * )
018:       INTEGER            IWORK( * )
019:       REAL               A( LDA, * ), ALPHAI( * ), ALPHAR( * ),
020:      $                   B( LDB, * ), BETA( * ), LSCALE( * ),
021:      $                   RCONDE( * ), RCONDV( * ), RSCALE( * ),
022:      $                   VL( LDVL, * ), VR( LDVR, * ), WORK( * )
023: *     ..
024: *
025: *  Purpose
026: *  =======
027: *
028: *  SGGEVX computes for a pair of N-by-N real nonsymmetric matrices (A,B)
029: *  the generalized eigenvalues, and optionally, the left and/or right
030: *  generalized eigenvectors.
031: *
032: *  Optionally also, it computes a balancing transformation to improve
033: *  the conditioning of the eigenvalues and eigenvectors (ILO, IHI,
034: *  LSCALE, RSCALE, ABNRM, and BBNRM), reciprocal condition numbers for
035: *  the eigenvalues (RCONDE), and reciprocal condition numbers for the
036: *  right eigenvectors (RCONDV).
037: *
038: *  A generalized eigenvalue for a pair of matrices (A,B) is a scalar
039: *  lambda or a ratio alpha/beta = lambda, such that A - lambda*B is
040: *  singular. It is usually represented as the pair (alpha,beta), as
041: *  there is a reasonable interpretation for beta=0, and even for both
042: *  being zero.
043: *
044: *  The right eigenvector v(j) corresponding to the eigenvalue lambda(j)
045: *  of (A,B) satisfies
046: *
047: *                   A * v(j) = lambda(j) * B * v(j) .
048: *
049: *  The left eigenvector u(j) corresponding to the eigenvalue lambda(j)
050: *  of (A,B) satisfies
051: *
052: *                   u(j)**H * A  = lambda(j) * u(j)**H * B.
053: *
054: *  where u(j)**H is the conjugate-transpose of u(j).
055: *
056: *
057: *  Arguments
058: *  =========
059: *
060: *  BALANC  (input) CHARACTER*1
061: *          Specifies the balance option to be performed.
062: *          = 'N':  do not diagonally scale or permute;
063: *          = 'P':  permute only;
064: *          = 'S':  scale only;
065: *          = 'B':  both permute and scale.
066: *          Computed reciprocal condition numbers will be for the
067: *          matrices after permuting and/or balancing. Permuting does
068: *          not change condition numbers (in exact arithmetic), but
069: *          balancing does.
070: *
071: *  JOBVL   (input) CHARACTER*1
072: *          = 'N':  do not compute the left generalized eigenvectors;
073: *          = 'V':  compute the left generalized eigenvectors.
074: *
075: *  JOBVR   (input) CHARACTER*1
076: *          = 'N':  do not compute the right generalized eigenvectors;
077: *          = 'V':  compute the right generalized eigenvectors.
078: *
079: *  SENSE   (input) CHARACTER*1
080: *          Determines which reciprocal condition numbers are computed.
081: *          = 'N': none are computed;
082: *          = 'E': computed for eigenvalues only;
083: *          = 'V': computed for eigenvectors only;
084: *          = 'B': computed for eigenvalues and eigenvectors.
085: *
086: *  N       (input) INTEGER
087: *          The order of the matrices A, B, VL, and VR.  N >= 0.
088: *
089: *  A       (input/output) REAL array, dimension (LDA, N)
090: *          On entry, the matrix A in the pair (A,B).
091: *          On exit, A has been overwritten. If JOBVL='V' or JOBVR='V'
092: *          or both, then A contains the first part of the real Schur
093: *          form of the "balanced" versions of the input A and B.
094: *
095: *  LDA     (input) INTEGER
096: *          The leading dimension of A.  LDA >= max(1,N).
097: *
098: *  B       (input/output) REAL array, dimension (LDB, N)
099: *          On entry, the matrix B in the pair (A,B).
100: *          On exit, B has been overwritten. If JOBVL='V' or JOBVR='V'
101: *          or both, then B contains the second part of the real Schur
102: *          form of the "balanced" versions of the input A and B.
103: *
104: *  LDB     (input) INTEGER
105: *          The leading dimension of B.  LDB >= max(1,N).
106: *
107: *  ALPHAR  (output) REAL array, dimension (N)
108: *  ALPHAI  (output) REAL array, dimension (N)
109: *  BETA    (output) REAL array, dimension (N)
110: *          On exit, (ALPHAR(j) + ALPHAI(j)*i)/BETA(j), j=1,...,N, will
111: *          be the generalized eigenvalues.  If ALPHAI(j) is zero, then
112: *          the j-th eigenvalue is real; if positive, then the j-th and
113: *          (j+1)-st eigenvalues are a complex conjugate pair, with
114: *          ALPHAI(j+1) negative.
115: *
116: *          Note: the quotients ALPHAR(j)/BETA(j) and ALPHAI(j)/BETA(j)
117: *          may easily over- or underflow, and BETA(j) may even be zero.
118: *          Thus, the user should avoid naively computing the ratio
119: *          ALPHA/BETA. However, ALPHAR and ALPHAI will be always less
120: *          than and usually comparable with norm(A) in magnitude, and
121: *          BETA always less than and usually comparable with norm(B).
122: *
123: *  VL      (output) REAL array, dimension (LDVL,N)
124: *          If JOBVL = 'V', the left eigenvectors u(j) are stored one
125: *          after another in the columns of VL, in the same order as
126: *          their eigenvalues. If the j-th eigenvalue is real, then
127: *          u(j) = VL(:,j), the j-th column of VL. If the j-th and
128: *          (j+1)-th eigenvalues form a complex conjugate pair, then
129: *          u(j) = VL(:,j)+i*VL(:,j+1) and u(j+1) = VL(:,j)-i*VL(:,j+1).
130: *          Each eigenvector will be scaled so the largest component have
131: *          abs(real part) + abs(imag. part) = 1.
132: *          Not referenced if JOBVL = 'N'.
133: *
134: *  LDVL    (input) INTEGER
135: *          The leading dimension of the matrix VL. LDVL >= 1, and
136: *          if JOBVL = 'V', LDVL >= N.
137: *
138: *  VR      (output) REAL array, dimension (LDVR,N)
139: *          If JOBVR = 'V', the right eigenvectors v(j) are stored one
140: *          after another in the columns of VR, in the same order as
141: *          their eigenvalues. If the j-th eigenvalue is real, then
142: *          v(j) = VR(:,j), the j-th column of VR. If the j-th and
143: *          (j+1)-th eigenvalues form a complex conjugate pair, then
144: *          v(j) = VR(:,j)+i*VR(:,j+1) and v(j+1) = VR(:,j)-i*VR(:,j+1).
145: *          Each eigenvector will be scaled so the largest component have
146: *          abs(real part) + abs(imag. part) = 1.
147: *          Not referenced if JOBVR = 'N'.
148: *
149: *  LDVR    (input) INTEGER
150: *          The leading dimension of the matrix VR. LDVR >= 1, and
151: *          if JOBVR = 'V', LDVR >= N.
152: *
153: *  ILO     (output) INTEGER
154: *  IHI     (output) INTEGER
155: *          ILO and IHI are integer values such that on exit
156: *          A(i,j) = 0 and B(i,j) = 0 if i > j and
157: *          j = 1,...,ILO-1 or i = IHI+1,...,N.
158: *          If BALANC = 'N' or 'S', ILO = 1 and IHI = N.
159: *
160: *  LSCALE  (output) REAL array, dimension (N)
161: *          Details of the permutations and scaling factors applied
162: *          to the left side of A and B.  If PL(j) is the index of the
163: *          row interchanged with row j, and DL(j) is the scaling
164: *          factor applied to row j, then
165: *            LSCALE(j) = PL(j)  for j = 1,...,ILO-1
166: *                      = DL(j)  for j = ILO,...,IHI
167: *                      = PL(j)  for j = IHI+1,...,N.
168: *          The order in which the interchanges are made is N to IHI+1,
169: *          then 1 to ILO-1.
170: *
171: *  RSCALE  (output) REAL array, dimension (N)
172: *          Details of the permutations and scaling factors applied
173: *          to the right side of A and B.  If PR(j) is the index of the
174: *          column interchanged with column j, and DR(j) is the scaling
175: *          factor applied to column j, then
176: *            RSCALE(j) = PR(j)  for j = 1,...,ILO-1
177: *                      = DR(j)  for j = ILO,...,IHI
178: *                      = PR(j)  for j = IHI+1,...,N
179: *          The order in which the interchanges are made is N to IHI+1,
180: *          then 1 to ILO-1.
181: *
182: *  ABNRM   (output) REAL
183: *          The one-norm of the balanced matrix A.
184: *
185: *  BBNRM   (output) REAL
186: *          The one-norm of the balanced matrix B.
187: *
188: *  RCONDE  (output) REAL array, dimension (N)
189: *          If SENSE = 'E' or 'B', the reciprocal condition numbers of
190: *          the eigenvalues, stored in consecutive elements of the array.
191: *          For a complex conjugate pair of eigenvalues two consecutive
192: *          elements of RCONDE are set to the same value. Thus RCONDE(j),
193: *          RCONDV(j), and the j-th columns of VL and VR all correspond
194: *          to the j-th eigenpair.
195: *          If SENSE = 'N' or 'V', RCONDE is not referenced.
196: *
197: *  RCONDV  (output) REAL array, dimension (N)
198: *          If SENSE = 'V' or 'B', the estimated reciprocal condition
199: *          numbers of the eigenvectors, stored in consecutive elements
200: *          of the array. For a complex eigenvector two consecutive
201: *          elements of RCONDV are set to the same value. If the
202: *          eigenvalues cannot be reordered to compute RCONDV(j),
203: *          RCONDV(j) is set to 0; this can only occur when the true
204: *          value would be very small anyway.
205: *          If SENSE = 'N' or 'E', RCONDV is not referenced.
206: *
207: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
208: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
209: *
210: *  LWORK   (input) INTEGER
211: *          The dimension of the array WORK. LWORK >= max(1,2*N).
212: *          If BALANC = 'S' or 'B', or JOBVL = 'V', or JOBVR = 'V',
213: *          LWORK >= max(1,6*N).
214: *          If SENSE = 'E', LWORK >= max(1,10*N).
215: *          If SENSE = 'V' or 'B', LWORK >= 2*N*N+8*N+16.
216: *
217: *          If LWORK = -1, then a workspace query is assumed; the routine
218: *          only calculates the optimal size of the WORK array, returns
219: *          this value as the first entry of the WORK array, and no error
220: *          message related to LWORK is issued by XERBLA.
221: *
222: *  IWORK   (workspace) INTEGER array, dimension (N+6)
223: *          If SENSE = 'E', IWORK is not referenced.
224: *
225: *  BWORK   (workspace) LOGICAL array, dimension (N)
226: *          If SENSE = 'N', BWORK is not referenced.
227: *
228: *  INFO    (output) INTEGER
229: *          = 0:  successful exit
230: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
231: *          = 1,...,N:
232: *                The QZ iteration failed.  No eigenvectors have been
233: *                calculated, but ALPHAR(j), ALPHAI(j), and BETA(j)
234: *                should be correct for j=INFO+1,...,N.
235: *          > N:  =N+1: other than QZ iteration failed in SHGEQZ.
236: *                =N+2: error return from STGEVC.
237: *
238: *  Further Details
239: *  ===============
240: *
241: *  Balancing a matrix pair (A,B) includes, first, permuting rows and
242: *  columns to isolate eigenvalues, second, applying diagonal similarity
243: *  transformation to the rows and columns to make the rows and columns
244: *  as close in norm as possible. The computed reciprocal condition
245: *  numbers correspond to the balanced matrix. Permuting rows and columns
246: *  will not change the condition numbers (in exact arithmetic) but
247: *  diagonal scaling will.  For further explanation of balancing, see
248: *  section 4.11.1.2 of LAPACK Users' Guide.
249: *
250: *  An approximate error bound on the chordal distance between the i-th
251: *  computed generalized eigenvalue w and the corresponding exact
252: *  eigenvalue lambda is
253: *
254: *       chord(w, lambda) <= EPS * norm(ABNRM, BBNRM) / RCONDE(I)
255: *
256: *  An approximate error bound for the angle between the i-th computed
257: *  eigenvector VL(i) or VR(i) is given by
258: *
259: *       EPS * norm(ABNRM, BBNRM) / DIF(i).
260: *
261: *  For further explanation of the reciprocal condition numbers RCONDE
262: *  and RCONDV, see section 4.11 of LAPACK User's Guide.
263: *
264: *  =====================================================================
265: *
266: *     .. Parameters ..
267:       REAL               ZERO, ONE
268:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
269: *     ..
270: *     .. Local Scalars ..
271:       LOGICAL            ILASCL, ILBSCL, ILV, ILVL, ILVR, LQUERY, NOSCL,
272:      $                   PAIR, WANTSB, WANTSE, WANTSN, WANTSV
273:       CHARACTER          CHTEMP
274:       INTEGER            I, ICOLS, IERR, IJOBVL, IJOBVR, IN, IROWS,
275:      $                   ITAU, IWRK, IWRK1, J, JC, JR, M, MAXWRK,
276:      $                   MINWRK, MM
277:       REAL               ANRM, ANRMTO, BIGNUM, BNRM, BNRMTO, EPS,
278:      $                   SMLNUM, TEMP
279: *     ..
280: *     .. Local Arrays ..
281:       LOGICAL            LDUMMA( 1 )
282: *     ..
283: *     .. External Subroutines ..
284:       EXTERNAL           SGEQRF, SGGBAK, SGGBAL, SGGHRD, SHGEQZ, SLABAD,
285:      $                   SLACPY, SLASCL, SLASET, SORGQR, SORMQR, STGEVC,
286:      $                   STGSNA, XERBLA
287: *     ..
288: *     .. External Functions ..
289:       LOGICAL            LSAME
290:       INTEGER            ILAENV
291:       REAL               SLAMCH, SLANGE
292:       EXTERNAL           LSAME, ILAENV, SLAMCH, SLANGE
293: *     ..
294: *     .. Intrinsic Functions ..
295:       INTRINSIC          ABS, MAX, SQRT
296: *     ..
297: *     .. Executable Statements ..
298: *
299: *     Decode the input arguments
300: *
301:       IF( LSAME( JOBVL, 'N' ) ) THEN
302:          IJOBVL = 1
303:          ILVL = .FALSE.
304:       ELSE IF( LSAME( JOBVL, 'V' ) ) THEN
305:          IJOBVL = 2
306:          ILVL = .TRUE.
307:       ELSE
308:          IJOBVL = -1
309:          ILVL = .FALSE.
310:       END IF
311: *
312:       IF( LSAME( JOBVR, 'N' ) ) THEN
313:          IJOBVR = 1
314:          ILVR = .FALSE.
315:       ELSE IF( LSAME( JOBVR, 'V' ) ) THEN
316:          IJOBVR = 2
317:          ILVR = .TRUE.
318:       ELSE
319:          IJOBVR = -1
320:          ILVR = .FALSE.
321:       END IF
322:       ILV = ILVL .OR. ILVR
323: *
324:       NOSCL  = LSAME( BALANC, 'N' ) .OR. LSAME( BALANC, 'P' )
325:       WANTSN = LSAME( SENSE, 'N' )
326:       WANTSE = LSAME( SENSE, 'E' )
327:       WANTSV = LSAME( SENSE, 'V' )
328:       WANTSB = LSAME( SENSE, 'B' )
329: *
330: *     Test the input arguments
331: *
332:       INFO = 0
333:       LQUERY = ( LWORK.EQ.-1 )
334:       IF( .NOT.( NOSCL .OR. LSAME( BALANC, 'S' ) .OR.
335:      $    LSAME( BALANC, 'B' ) ) ) THEN
336:          INFO = -1
337:       ELSE IF( IJOBVL.LE.0 ) THEN
338:          INFO = -2
339:       ELSE IF( IJOBVR.LE.0 ) THEN
340:          INFO = -3
341:       ELSE IF( .NOT.( WANTSN .OR. WANTSE .OR. WANTSB .OR. WANTSV ) )
342:      $          THEN
343:          INFO = -4
344:       ELSE IF( N.LT.0 ) THEN
345:          INFO = -5
346:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
347:          INFO = -7
348:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
349:          INFO = -9
350:       ELSE IF( LDVL.LT.1 .OR. ( ILVL .AND. LDVL.LT.N ) ) THEN
351:          INFO = -14
352:       ELSE IF( LDVR.LT.1 .OR. ( ILVR .AND. LDVR.LT.N ) ) THEN
353:          INFO = -16
354:       END IF
355: *
356: *     Compute workspace
357: *      (Note: Comments in the code beginning "Workspace:" describe the
358: *       minimal amount of workspace needed at that point in the code,
359: *       as well as the preferred amount for good performance.
360: *       NB refers to the optimal block size for the immediately
361: *       following subroutine, as returned by ILAENV. The workspace is
362: *       computed assuming ILO = 1 and IHI = N, the worst case.)
363: *
364:       IF( INFO.EQ.0 ) THEN
365:          IF( N.EQ.0 ) THEN
366:             MINWRK = 1
367:             MAXWRK = 1
368:          ELSE
369:             IF( NOSCL .AND. .NOT.ILV ) THEN
370:                MINWRK = 2*N
371:             ELSE
372:                MINWRK = 6*N
373:             END IF
374:             IF( WANTSE ) THEN
375:                MINWRK = 10*N
376:             ELSE IF( WANTSV .OR. WANTSB ) THEN
377:                MINWRK = 2*N*( N + 4 ) + 16
378:             END IF
379:             MAXWRK = MINWRK
380:             MAXWRK = MAX( MAXWRK,
381:      $                    N + N*ILAENV( 1, 'SGEQRF', ' ', N, 1, N, 0 ) )
382:             MAXWRK = MAX( MAXWRK,
383:      $                    N + N*ILAENV( 1, 'SORMQR', ' ', N, 1, N, 0 ) )
384:             IF( ILVL ) THEN
385:                MAXWRK = MAX( MAXWRK, N +
386:      $                       N*ILAENV( 1, 'SORGQR', ' ', N, 1, N, 0 ) )
387:             END IF
388:          END IF
389:          WORK( 1 ) = MAXWRK
390: *
391:          IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
392:             INFO = -26
393:          END IF
394:       END IF
395: *
396:       IF( INFO.NE.0 ) THEN
397:          CALL XERBLA( 'SGGEVX', -INFO )
398:          RETURN
399:       ELSE IF( LQUERY ) THEN
400:          RETURN
401:       END IF
402: *
403: *     Quick return if possible
404: *
405:       IF( N.EQ.0 )
406:      $   RETURN
407: *
408: *
409: *     Get machine constants
410: *
411:       EPS = SLAMCH( 'P' )
412:       SMLNUM = SLAMCH( 'S' )
413:       BIGNUM = ONE / SMLNUM
414:       CALL SLABAD( SMLNUM, BIGNUM )
415:       SMLNUM = SQRT( SMLNUM ) / EPS
416:       BIGNUM = ONE / SMLNUM
417: *
418: *     Scale A if max element outside range [SMLNUM,BIGNUM]
419: *
420:       ANRM = SLANGE( 'M', N, N, A, LDA, WORK )
421:       ILASCL = .FALSE.
422:       IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
423:          ANRMTO = SMLNUM
424:          ILASCL = .TRUE.
425:       ELSE IF( ANRM.GT.BIGNUM ) THEN
426:          ANRMTO = BIGNUM
427:          ILASCL = .TRUE.
428:       END IF
429:       IF( ILASCL )
430:      $   CALL SLASCL( 'G', 0, 0, ANRM, ANRMTO, N, N, A, LDA, IERR )
431: *
432: *     Scale B if max element outside range [SMLNUM,BIGNUM]
433: *
434:       BNRM = SLANGE( 'M', N, N, B, LDB, WORK )
435:       ILBSCL = .FALSE.
436:       IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
437:          BNRMTO = SMLNUM
438:          ILBSCL = .TRUE.
439:       ELSE IF( BNRM.GT.BIGNUM ) THEN
440:          BNRMTO = BIGNUM
441:          ILBSCL = .TRUE.
442:       END IF
443:       IF( ILBSCL )
444:      $   CALL SLASCL( 'G', 0, 0, BNRM, BNRMTO, N, N, B, LDB, IERR )
445: *
446: *     Permute and/or balance the matrix pair (A,B)
447: *     (Workspace: need 6*N if BALANC = 'S' or 'B', 1 otherwise)
448: *
449:       CALL SGGBAL( BALANC, N, A, LDA, B, LDB, ILO, IHI, LSCALE, RSCALE,
450:      $             WORK, IERR )
451: *
452: *     Compute ABNRM and BBNRM
453: *
454:       ABNRM = SLANGE( '1', N, N, A, LDA, WORK( 1 ) )
455:       IF( ILASCL ) THEN
456:          WORK( 1 ) = ABNRM
457:          CALL SLASCL( 'G', 0, 0, ANRMTO, ANRM, 1, 1, WORK( 1 ), 1,
458:      $                IERR )
459:          ABNRM = WORK( 1 )
460:       END IF
461: *
462:       BBNRM = SLANGE( '1', N, N, B, LDB, WORK( 1 ) )
463:       IF( ILBSCL ) THEN
464:          WORK( 1 ) = BBNRM
465:          CALL SLASCL( 'G', 0, 0, BNRMTO, BNRM, 1, 1, WORK( 1 ), 1,
466:      $                IERR )
467:          BBNRM = WORK( 1 )
468:       END IF
469: *
470: *     Reduce B to triangular form (QR decomposition of B)
471: *     (Workspace: need N, prefer N*NB )
472: *
473:       IROWS = IHI + 1 - ILO
474:       IF( ILV .OR. .NOT.WANTSN ) THEN
475:          ICOLS = N + 1 - ILO
476:       ELSE
477:          ICOLS = IROWS
478:       END IF
479:       ITAU = 1
480:       IWRK = ITAU + IROWS
481:       CALL SGEQRF( IROWS, ICOLS, B( ILO, ILO ), LDB, WORK( ITAU ),
482:      $             WORK( IWRK ), LWORK+1-IWRK, IERR )
483: *
484: *     Apply the orthogonal transformation to A
485: *     (Workspace: need N, prefer N*NB)
486: *
487:       CALL SORMQR( 'L', 'T', IROWS, ICOLS, IROWS, B( ILO, ILO ), LDB,
488:      $             WORK( ITAU ), A( ILO, ILO ), LDA, WORK( IWRK ),
489:      $             LWORK+1-IWRK, IERR )
490: *
491: *     Initialize VL and/or VR
492: *     (Workspace: need N, prefer N*NB)
493: *
494:       IF( ILVL ) THEN
495:          CALL SLASET( 'Full', N, N, ZERO, ONE, VL, LDVL )
496:          IF( IROWS.GT.1 ) THEN
497:             CALL SLACPY( 'L', IROWS-1, IROWS-1, B( ILO+1, ILO ), LDB,
498:      $                   VL( ILO+1, ILO ), LDVL )
499:          END IF
500:          CALL SORGQR( IROWS, IROWS, IROWS, VL( ILO, ILO ), LDVL,
501:      $                WORK( ITAU ), WORK( IWRK ), LWORK+1-IWRK, IERR )
502:       END IF
503: *
504:       IF( ILVR )
505:      $   CALL SLASET( 'Full', N, N, ZERO, ONE, VR, LDVR )
506: *
507: *     Reduce to generalized Hessenberg form
508: *     (Workspace: none needed)
509: *
510:       IF( ILV .OR. .NOT.WANTSN ) THEN
511: *
512: *        Eigenvectors requested -- work on whole matrix.
513: *
514:          CALL SGGHRD( JOBVL, JOBVR, N, ILO, IHI, A, LDA, B, LDB, VL,
515:      $                LDVL, VR, LDVR, IERR )
516:       ELSE
517:          CALL SGGHRD( 'N', 'N', IROWS, 1, IROWS, A( ILO, ILO ), LDA,
518:      $                B( ILO, ILO ), LDB, VL, LDVL, VR, LDVR, IERR )
519:       END IF
520: *
521: *     Perform QZ algorithm (Compute eigenvalues, and optionally, the
522: *     Schur forms and Schur vectors)
523: *     (Workspace: need N)
524: *
525:       IF( ILV .OR. .NOT.WANTSN ) THEN
526:          CHTEMP = 'S'
527:       ELSE
528:          CHTEMP = 'E'
529:       END IF
530: *
531:       CALL SHGEQZ( CHTEMP, JOBVL, JOBVR, N, ILO, IHI, A, LDA, B, LDB,
532:      $             ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, WORK,
533:      $             LWORK, IERR )
534:       IF( IERR.NE.0 ) THEN
535:          IF( IERR.GT.0 .AND. IERR.LE.N ) THEN
536:             INFO = IERR
537:          ELSE IF( IERR.GT.N .AND. IERR.LE.2*N ) THEN
538:             INFO = IERR - N
539:          ELSE
540:             INFO = N + 1
541:          END IF
542:          GO TO 130
543:       END IF
544: *
545: *     Compute Eigenvectors and estimate condition numbers if desired
546: *     (Workspace: STGEVC: need 6*N
547: *                 STGSNA: need 2*N*(N+2)+16 if SENSE = 'V' or 'B',
548: *                         need N otherwise )
549: *
550:       IF( ILV .OR. .NOT.WANTSN ) THEN
551:          IF( ILV ) THEN
552:             IF( ILVL ) THEN
553:                IF( ILVR ) THEN
554:                   CHTEMP = 'B'
555:                ELSE
556:                   CHTEMP = 'L'
557:                END IF
558:             ELSE
559:                CHTEMP = 'R'
560:             END IF
561: *
562:             CALL STGEVC( CHTEMP, 'B', LDUMMA, N, A, LDA, B, LDB, VL,
563:      $                   LDVL, VR, LDVR, N, IN, WORK, IERR )
564:             IF( IERR.NE.0 ) THEN
565:                INFO = N + 2
566:                GO TO 130
567:             END IF
568:          END IF
569: *
570:          IF( .NOT.WANTSN ) THEN
571: *
572: *           compute eigenvectors (STGEVC) and estimate condition
573: *           numbers (STGSNA). Note that the definition of the condition
574: *           number is not invariant under transformation (u,v) to
575: *           (Q*u, Z*v), where (u,v) are eigenvectors of the generalized
576: *           Schur form (S,T), Q and Z are orthogonal matrices. In order
577: *           to avoid using extra 2*N*N workspace, we have to recalculate
578: *           eigenvectors and estimate one condition numbers at a time.
579: *
580:             PAIR = .FALSE.
581:             DO 20 I = 1, N
582: *
583:                IF( PAIR ) THEN
584:                   PAIR = .FALSE.
585:                   GO TO 20
586:                END IF
587:                MM = 1
588:                IF( I.LT.N ) THEN
589:                   IF( A( I+1, I ).NE.ZERO ) THEN
590:                      PAIR = .TRUE.
591:                      MM = 2
592:                   END IF
593:                END IF
594: *
595:                DO 10 J = 1, N
596:                   BWORK( J ) = .FALSE.
597:    10          CONTINUE
598:                IF( MM.EQ.1 ) THEN
599:                   BWORK( I ) = .TRUE.
600:                ELSE IF( MM.EQ.2 ) THEN
601:                   BWORK( I ) = .TRUE.
602:                   BWORK( I+1 ) = .TRUE.
603:                END IF
604: *
605:                IWRK = MM*N + 1
606:                IWRK1 = IWRK + MM*N
607: *
608: *              Compute a pair of left and right eigenvectors.
609: *              (compute workspace: need up to 4*N + 6*N)
610: *
611:                IF( WANTSE .OR. WANTSB ) THEN
612:                   CALL STGEVC( 'B', 'S', BWORK, N, A, LDA, B, LDB,
613:      $                         WORK( 1 ), N, WORK( IWRK ), N, MM, M,
614:      $                         WORK( IWRK1 ), IERR )
615:                   IF( IERR.NE.0 ) THEN
616:                      INFO = N + 2
617:                      GO TO 130
618:                   END IF
619:                END IF
620: *
621:                CALL STGSNA( SENSE, 'S', BWORK, N, A, LDA, B, LDB,
622:      $                      WORK( 1 ), N, WORK( IWRK ), N, RCONDE( I ),
623:      $                      RCONDV( I ), MM, M, WORK( IWRK1 ),
624:      $                      LWORK-IWRK1+1, IWORK, IERR )
625: *
626:    20       CONTINUE
627:          END IF
628:       END IF
629: *
630: *     Undo balancing on VL and VR and normalization
631: *     (Workspace: none needed)
632: *
633:       IF( ILVL ) THEN
634:          CALL SGGBAK( BALANC, 'L', N, ILO, IHI, LSCALE, RSCALE, N, VL,
635:      $                LDVL, IERR )
636: *
637:          DO 70 JC = 1, N
638:             IF( ALPHAI( JC ).LT.ZERO )
639:      $         GO TO 70
640:             TEMP = ZERO
641:             IF( ALPHAI( JC ).EQ.ZERO ) THEN
642:                DO 30 JR = 1, N
643:                   TEMP = MAX( TEMP, ABS( VL( JR, JC ) ) )
644:    30          CONTINUE
645:             ELSE
646:                DO 40 JR = 1, N
647:                   TEMP = MAX( TEMP, ABS( VL( JR, JC ) )+
648:      $                   ABS( VL( JR, JC+1 ) ) )
649:    40          CONTINUE
650:             END IF
651:             IF( TEMP.LT.SMLNUM )
652:      $         GO TO 70
653:             TEMP = ONE / TEMP
654:             IF( ALPHAI( JC ).EQ.ZERO ) THEN
655:                DO 50 JR = 1, N
656:                   VL( JR, JC ) = VL( JR, JC )*TEMP
657:    50          CONTINUE
658:             ELSE
659:                DO 60 JR = 1, N
660:                   VL( JR, JC ) = VL( JR, JC )*TEMP
661:                   VL( JR, JC+1 ) = VL( JR, JC+1 )*TEMP
662:    60          CONTINUE
663:             END IF
664:    70    CONTINUE
665:       END IF
666:       IF( ILVR ) THEN
667:          CALL SGGBAK( BALANC, 'R', N, ILO, IHI, LSCALE, RSCALE, N, VR,
668:      $                LDVR, IERR )
669:          DO 120 JC = 1, N
670:             IF( ALPHAI( JC ).LT.ZERO )
671:      $         GO TO 120
672:             TEMP = ZERO
673:             IF( ALPHAI( JC ).EQ.ZERO ) THEN
674:                DO 80 JR = 1, N
675:                   TEMP = MAX( TEMP, ABS( VR( JR, JC ) ) )
676:    80          CONTINUE
677:             ELSE
678:                DO 90 JR = 1, N
679:                   TEMP = MAX( TEMP, ABS( VR( JR, JC ) )+
680:      $                   ABS( VR( JR, JC+1 ) ) )
681:    90          CONTINUE
682:             END IF
683:             IF( TEMP.LT.SMLNUM )
684:      $         GO TO 120
685:             TEMP = ONE / TEMP
686:             IF( ALPHAI( JC ).EQ.ZERO ) THEN
687:                DO 100 JR = 1, N
688:                   VR( JR, JC ) = VR( JR, JC )*TEMP
689:   100          CONTINUE
690:             ELSE
691:                DO 110 JR = 1, N
692:                   VR( JR, JC ) = VR( JR, JC )*TEMP
693:                   VR( JR, JC+1 ) = VR( JR, JC+1 )*TEMP
694:   110          CONTINUE
695:             END IF
696:   120    CONTINUE
697:       END IF
698: *
699: *     Undo scaling if necessary
700: *
701:       IF( ILASCL ) THEN
702:          CALL SLASCL( 'G', 0, 0, ANRMTO, ANRM, N, 1, ALPHAR, N, IERR )
703:          CALL SLASCL( 'G', 0, 0, ANRMTO, ANRM, N, 1, ALPHAI, N, IERR )
704:       END IF
705: *
706:       IF( ILBSCL ) THEN
707:          CALL SLASCL( 'G', 0, 0, BNRMTO, BNRM, N, 1, BETA, N, IERR )
708:       END IF
709: *
710:   130 CONTINUE
711:       WORK( 1 ) = MAXWRK
712: *
713:       RETURN
714: *
715: *     End of SGGEVX
716: *
717:       END
718: