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