001:       SUBROUTINE SGEEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, WR, WI,
002:      $                   VL, LDVL, VR, LDVR, ILO, IHI, SCALE, ABNRM,
003:      $                   RCONDE, RCONDV, WORK, LWORK, IWORK, INFO )
004: *
005: *  -- LAPACK driver routine (version 3.2) --
006: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
007: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
008: *     November 2006
009: *
010: *     .. Scalar Arguments ..
011:       CHARACTER          BALANC, JOBVL, JOBVR, SENSE
012:       INTEGER            IHI, ILO, INFO, LDA, LDVL, LDVR, LWORK, N
013:       REAL               ABNRM
014: *     ..
015: *     .. Array Arguments ..
016:       INTEGER            IWORK( * )
017:       REAL               A( LDA, * ), RCONDE( * ), RCONDV( * ),
018:      $                   SCALE( * ), VL( LDVL, * ), VR( LDVR, * ),
019:      $                   WI( * ), WORK( * ), WR( * )
020: *     ..
021: *
022: *  Purpose
023: *  =======
024: *
025: *  SGEEVX computes for an N-by-N real nonsymmetric matrix A, the
026: *  eigenvalues and, optionally, the left and/or right eigenvectors.
027: *
028: *  Optionally also, it computes a balancing transformation to improve
029: *  the conditioning of the eigenvalues and eigenvectors (ILO, IHI,
030: *  SCALE, and ABNRM), reciprocal condition numbers for the eigenvalues
031: *  (RCONDE), and reciprocal condition numbers for the right
032: *  eigenvectors (RCONDV).
033: *
034: *  The right eigenvector v(j) of A satisfies
035: *                   A * v(j) = lambda(j) * v(j)
036: *  where lambda(j) is its eigenvalue.
037: *  The left eigenvector u(j) of A satisfies
038: *                u(j)**H * A = lambda(j) * u(j)**H
039: *  where u(j)**H denotes the conjugate transpose of u(j).
040: *
041: *  The computed eigenvectors are normalized to have Euclidean norm
042: *  equal to 1 and largest component real.
043: *
044: *  Balancing a matrix means permuting the rows and columns to make it
045: *  more nearly upper triangular, and applying a diagonal similarity
046: *  transformation D * A * D**(-1), where D is a diagonal matrix, to
047: *  make its rows and columns closer in norm and the condition numbers
048: *  of its eigenvalues and eigenvectors smaller.  The computed
049: *  reciprocal condition numbers correspond to the balanced matrix.
050: *  Permuting rows and columns will not change the condition numbers
051: *  (in exact arithmetic) but diagonal scaling will.  For further
052: *  explanation of balancing, see section 4.10.2 of the LAPACK
053: *  Users' Guide.
054: *
055: *  Arguments
056: *  =========
057: *
058: *  BALANC  (input) CHARACTER*1
059: *          Indicates how the input matrix should be diagonally scaled
060: *          and/or permuted to improve the conditioning of its
061: *          eigenvalues.
062: *          = 'N': Do not diagonally scale or permute;
063: *          = 'P': Perform permutations to make the matrix more nearly
064: *                 upper triangular. Do not diagonally scale;
065: *          = 'S': Diagonally scale the matrix, i.e. replace A by
066: *                 D*A*D**(-1), where D is a diagonal matrix chosen
067: *                 to make the rows and columns of A more equal in
068: *                 norm. Do not permute;
069: *          = 'B': Both diagonally scale and permute A.
070: *
071: *          Computed reciprocal condition numbers will be for the matrix
072: *          after balancing and/or permuting. Permuting does not change
073: *          condition numbers (in exact arithmetic), but balancing does.
074: *
075: *  JOBVL   (input) CHARACTER*1
076: *          = 'N': left eigenvectors of A are not computed;
077: *          = 'V': left eigenvectors of A are computed.
078: *          If SENSE = 'E' or 'B', JOBVL must = 'V'.
079: *
080: *  JOBVR   (input) CHARACTER*1
081: *          = 'N': right eigenvectors of A are not computed;
082: *          = 'V': right eigenvectors of A are computed.
083: *          If SENSE = 'E' or 'B', JOBVR must = 'V'.
084: *
085: *  SENSE   (input) CHARACTER*1
086: *          Determines which reciprocal condition numbers are computed.
087: *          = 'N': None are computed;
088: *          = 'E': Computed for eigenvalues only;
089: *          = 'V': Computed for right eigenvectors only;
090: *          = 'B': Computed for eigenvalues and right eigenvectors.
091: *
092: *          If SENSE = 'E' or 'B', both left and right eigenvectors
093: *          must also be computed (JOBVL = 'V' and JOBVR = 'V').
094: *
095: *  N       (input) INTEGER
096: *          The order of the matrix A. N >= 0.
097: *
098: *  A       (input/output) REAL array, dimension (LDA,N)
099: *          On entry, the N-by-N matrix A.
100: *          On exit, A has been overwritten.  If JOBVL = 'V' or
101: *          JOBVR = 'V', A contains the real Schur form of the balanced
102: *          version of the input matrix A.
103: *
104: *  LDA     (input) INTEGER
105: *          The leading dimension of the array A.  LDA >= max(1,N).
106: *
107: *  WR      (output) REAL array, dimension (N)
108: *  WI      (output) REAL array, dimension (N)
109: *          WR and WI contain the real and imaginary parts,
110: *          respectively, of the computed eigenvalues.  Complex
111: *          conjugate pairs of eigenvalues will appear consecutively
112: *          with the eigenvalue having the positive imaginary part
113: *          first.
114: *
115: *  VL      (output) REAL array, dimension (LDVL,N)
116: *          If JOBVL = 'V', the left eigenvectors u(j) are stored one
117: *          after another in the columns of VL, in the same order
118: *          as their eigenvalues.
119: *          If JOBVL = 'N', VL is not referenced.
120: *          If the j-th eigenvalue is real, then u(j) = VL(:,j),
121: *          the j-th column of VL.
122: *          If the j-th and (j+1)-st eigenvalues form a complex
123: *          conjugate pair, then u(j) = VL(:,j) + i*VL(:,j+1) and
124: *          u(j+1) = VL(:,j) - i*VL(:,j+1).
125: *
126: *  LDVL    (input) INTEGER
127: *          The leading dimension of the array VL.  LDVL >= 1; if
128: *          JOBVL = 'V', LDVL >= N.
129: *
130: *  VR      (output) REAL array, dimension (LDVR,N)
131: *          If JOBVR = 'V', the right eigenvectors v(j) are stored one
132: *          after another in the columns of VR, in the same order
133: *          as their eigenvalues.
134: *          If JOBVR = 'N', VR is not referenced.
135: *          If the j-th eigenvalue is real, then v(j) = VR(:,j),
136: *          the j-th column of VR.
137: *          If the j-th and (j+1)-st eigenvalues form a complex
138: *          conjugate pair, then v(j) = VR(:,j) + i*VR(:,j+1) and
139: *          v(j+1) = VR(:,j) - i*VR(:,j+1).
140: *
141: *  LDVR    (input) INTEGER
142: *          The leading dimension of the array VR.  LDVR >= 1, and if
143: *          JOBVR = 'V', LDVR >= N.
144: *
145: *  ILO     (output) INTEGER
146: *  IHI     (output) INTEGER
147: *          ILO and IHI are integer values determined when A was
148: *          balanced.  The balanced A(i,j) = 0 if I > J and 
149: *          J = 1,...,ILO-1 or I = IHI+1,...,N.
150: *
151: *  SCALE   (output) REAL array, dimension (N)
152: *          Details of the permutations and scaling factors applied
153: *          when balancing A.  If P(j) is the index of the row and column
154: *          interchanged with row and column j, and D(j) is the scaling
155: *          factor applied to row and column j, then
156: *          SCALE(J) = P(J),    for J = 1,...,ILO-1
157: *                   = D(J),    for J = ILO,...,IHI
158: *                   = P(J)     for J = IHI+1,...,N.
159: *          The order in which the interchanges are made is N to IHI+1,
160: *          then 1 to ILO-1.
161: *
162: *  ABNRM   (output) REAL
163: *          The one-norm of the balanced matrix (the maximum
164: *          of the sum of absolute values of elements of any column).
165: *
166: *  RCONDE  (output) REAL array, dimension (N)
167: *          RCONDE(j) is the reciprocal condition number of the j-th
168: *          eigenvalue.
169: *
170: *  RCONDV  (output) REAL array, dimension (N)
171: *          RCONDV(j) is the reciprocal condition number of the j-th
172: *          right eigenvector.
173: *
174: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
175: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
176: *
177: *  LWORK   (input) INTEGER
178: *          The dimension of the array WORK.   If SENSE = 'N' or 'E',
179: *          LWORK >= max(1,2*N), and if JOBVL = 'V' or JOBVR = 'V',
180: *          LWORK >= 3*N.  If SENSE = 'V' or 'B', LWORK >= N*(N+6).
181: *          For good performance, LWORK must generally be larger.
182: *
183: *          If LWORK = -1, then a workspace query is assumed; the routine
184: *          only calculates the optimal size of the WORK array, returns
185: *          this value as the first entry of the WORK array, and no error
186: *          message related to LWORK is issued by XERBLA.
187: *
188: *  IWORK   (workspace) INTEGER array, dimension (2*N-2)
189: *          If SENSE = 'N' or 'E', not referenced.
190: *
191: *  INFO    (output) INTEGER
192: *          = 0:  successful exit
193: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
194: *          > 0:  if INFO = i, the QR algorithm failed to compute all the
195: *                eigenvalues, and no eigenvectors or condition numbers
196: *                have been computed; elements 1:ILO-1 and i+1:N of WR
197: *                and WI contain eigenvalues which have converged.
198: *
199: *  =====================================================================
200: *
201: *     .. Parameters ..
202:       REAL               ZERO, ONE
203:       PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0 )
204: *     ..
205: *     .. Local Scalars ..
206:       LOGICAL            LQUERY, SCALEA, WANTVL, WANTVR, WNTSNB, WNTSNE,
207:      $                   WNTSNN, WNTSNV
208:       CHARACTER          JOB, SIDE
209:       INTEGER            HSWORK, I, ICOND, IERR, ITAU, IWRK, K, MAXWRK,
210:      $                   MINWRK, NOUT
211:       REAL               ANRM, BIGNUM, CS, CSCALE, EPS, R, SCL, SMLNUM,
212:      $                   SN
213: *     ..
214: *     .. Local Arrays ..
215:       LOGICAL            SELECT( 1 )
216:       REAL               DUM( 1 )
217: *     ..
218: *     .. External Subroutines ..
219:       EXTERNAL           SGEBAK, SGEBAL, SGEHRD, SHSEQR, SLABAD, SLACPY,
220:      $                   SLARTG, SLASCL, SORGHR, SROT, SSCAL, STREVC,
221:      $                   STRSNA, XERBLA
222: *     ..
223: *     .. External Functions ..
224:       LOGICAL            LSAME
225:       INTEGER            ILAENV, ISAMAX
226:       REAL               SLAMCH, SLANGE, SLAPY2, SNRM2
227:       EXTERNAL           LSAME, ILAENV, ISAMAX, SLAMCH, SLANGE, SLAPY2,
228:      $                   SNRM2
229: *     ..
230: *     .. Intrinsic Functions ..
231:       INTRINSIC          MAX, SQRT
232: *     ..
233: *     .. Executable Statements ..
234: *
235: *     Test the input arguments
236: *
237:       INFO = 0
238:       LQUERY = ( LWORK.EQ.-1 )
239:       WANTVL = LSAME( JOBVL, 'V' )
240:       WANTVR = LSAME( JOBVR, 'V' )
241:       WNTSNN = LSAME( SENSE, 'N' )
242:       WNTSNE = LSAME( SENSE, 'E' )
243:       WNTSNV = LSAME( SENSE, 'V' )
244:       WNTSNB = LSAME( SENSE, 'B' )
245:       IF( .NOT.( LSAME( BALANC, 'N' ) .OR. LSAME( BALANC, 'S' ) .OR.
246:      $    LSAME( BALANC, 'P' ) .OR. LSAME( BALANC, 'B' ) ) ) THEN
247:          INFO = -1
248:       ELSE IF( ( .NOT.WANTVL ) .AND. ( .NOT.LSAME( JOBVL, 'N' ) ) ) THEN
249:          INFO = -2
250:       ELSE IF( ( .NOT.WANTVR ) .AND. ( .NOT.LSAME( JOBVR, 'N' ) ) ) THEN
251:          INFO = -3
252:       ELSE IF( .NOT.( WNTSNN .OR. WNTSNE .OR. WNTSNB .OR. WNTSNV ) .OR.
253:      $         ( ( WNTSNE .OR. WNTSNB ) .AND. .NOT.( WANTVL .AND.
254:      $         WANTVR ) ) ) THEN
255:          INFO = -4
256:       ELSE IF( N.LT.0 ) THEN
257:          INFO = -5
258:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
259:          INFO = -7
260:       ELSE IF( LDVL.LT.1 .OR. ( WANTVL .AND. LDVL.LT.N ) ) THEN
261:          INFO = -11
262:       ELSE IF( LDVR.LT.1 .OR. ( WANTVR .AND. LDVR.LT.N ) ) THEN
263:          INFO = -13
264:       END IF
265: *
266: *     Compute workspace
267: *      (Note: Comments in the code beginning "Workspace:" describe the
268: *       minimal amount of workspace needed at that point in the code,
269: *       as well as the preferred amount for good performance.
270: *       NB refers to the optimal block size for the immediately
271: *       following subroutine, as returned by ILAENV.
272: *       HSWORK refers to the workspace preferred by SHSEQR, as
273: *       calculated below. HSWORK is computed assuming ILO=1 and IHI=N,
274: *       the worst case.)
275: *
276:       IF( INFO.EQ.0 ) THEN
277:          IF( N.EQ.0 ) THEN
278:             MINWRK = 1
279:             MAXWRK = 1
280:          ELSE
281:             MAXWRK = N + N*ILAENV( 1, 'SGEHRD', ' ', N, 1, N, 0 )
282: *
283:             IF( WANTVL ) THEN
284:                CALL SHSEQR( 'S', 'V', N, 1, N, A, LDA, WR, WI, VL, LDVL,
285:      $                WORK, -1, INFO )
286:             ELSE IF( WANTVR ) THEN
287:                CALL SHSEQR( 'S', 'V', N, 1, N, A, LDA, WR, WI, VR, LDVR,
288:      $                WORK, -1, INFO )
289:             ELSE
290:                IF( WNTSNN ) THEN
291:                   CALL SHSEQR( 'E', 'N', N, 1, N, A, LDA, WR, WI, VR,
292:      $                LDVR, WORK, -1, INFO )
293:                ELSE
294:                   CALL SHSEQR( 'S', 'N', N, 1, N, A, LDA, WR, WI, VR,
295:      $                LDVR, WORK, -1, INFO )
296:                END IF
297:             END IF
298:             HSWORK = WORK( 1 )
299: *
300:             IF( ( .NOT.WANTVL ) .AND. ( .NOT.WANTVR ) ) THEN
301:                MINWRK = 2*N
302:                IF( .NOT.WNTSNN )
303:      $            MINWRK = MAX( MINWRK, N*N+6*N )
304:                MAXWRK = MAX( MAXWRK, HSWORK )
305:                IF( .NOT.WNTSNN )
306:      $            MAXWRK = MAX( MAXWRK, N*N + 6*N )
307:             ELSE
308:                MINWRK = 3*N
309:                IF( ( .NOT.WNTSNN ) .AND. ( .NOT.WNTSNE ) )
310:      $            MINWRK = MAX( MINWRK, N*N + 6*N )
311:                MAXWRK = MAX( MAXWRK, HSWORK )
312:                MAXWRK = MAX( MAXWRK, N + ( N - 1 )*ILAENV( 1, 'SORGHR',
313:      $                       ' ', N, 1, N, -1 ) )
314:                IF( ( .NOT.WNTSNN ) .AND. ( .NOT.WNTSNE ) )
315:      $            MAXWRK = MAX( MAXWRK, N*N + 6*N )
316:                MAXWRK = MAX( MAXWRK, 3*N )
317:             END IF
318:             MAXWRK = MAX( MAXWRK, MINWRK )
319:          END IF
320:          WORK( 1 ) = MAXWRK
321: *
322:          IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
323:             INFO = -21
324:          END IF
325:       END IF
326: *
327:       IF( INFO.NE.0 ) THEN
328:          CALL XERBLA( 'SGEEVX', -INFO )
329:          RETURN
330:       ELSE IF( LQUERY ) THEN
331:          RETURN
332:       END IF
333: *
334: *     Quick return if possible
335: *
336:       IF( N.EQ.0 )
337:      $   RETURN
338: *
339: *     Get machine constants
340: *
341:       EPS = SLAMCH( 'P' )
342:       SMLNUM = SLAMCH( 'S' )
343:       BIGNUM = ONE / SMLNUM
344:       CALL SLABAD( SMLNUM, BIGNUM )
345:       SMLNUM = SQRT( SMLNUM ) / EPS
346:       BIGNUM = ONE / SMLNUM
347: *
348: *     Scale A if max element outside range [SMLNUM,BIGNUM]
349: *
350:       ICOND = 0
351:       ANRM = SLANGE( 'M', N, N, A, LDA, DUM )
352:       SCALEA = .FALSE.
353:       IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
354:          SCALEA = .TRUE.
355:          CSCALE = SMLNUM
356:       ELSE IF( ANRM.GT.BIGNUM ) THEN
357:          SCALEA = .TRUE.
358:          CSCALE = BIGNUM
359:       END IF
360:       IF( SCALEA )
361:      $   CALL SLASCL( 'G', 0, 0, ANRM, CSCALE, N, N, A, LDA, IERR )
362: *
363: *     Balance the matrix and compute ABNRM
364: *
365:       CALL SGEBAL( BALANC, N, A, LDA, ILO, IHI, SCALE, IERR )
366:       ABNRM = SLANGE( '1', N, N, A, LDA, DUM )
367:       IF( SCALEA ) THEN
368:          DUM( 1 ) = ABNRM
369:          CALL SLASCL( 'G', 0, 0, CSCALE, ANRM, 1, 1, DUM, 1, IERR )
370:          ABNRM = DUM( 1 )
371:       END IF
372: *
373: *     Reduce to upper Hessenberg form
374: *     (Workspace: need 2*N, prefer N+N*NB)
375: *
376:       ITAU = 1
377:       IWRK = ITAU + N
378:       CALL SGEHRD( N, ILO, IHI, A, LDA, WORK( ITAU ), WORK( IWRK ),
379:      $             LWORK-IWRK+1, IERR )
380: *
381:       IF( WANTVL ) THEN
382: *
383: *        Want left eigenvectors
384: *        Copy Householder vectors to VL
385: *
386:          SIDE = 'L'
387:          CALL SLACPY( 'L', N, N, A, LDA, VL, LDVL )
388: *
389: *        Generate orthogonal matrix in VL
390: *        (Workspace: need 2*N-1, prefer N+(N-1)*NB)
391: *
392:          CALL SORGHR( N, ILO, IHI, VL, LDVL, WORK( ITAU ), WORK( IWRK ),
393:      $                LWORK-IWRK+1, IERR )
394: *
395: *        Perform QR iteration, accumulating Schur vectors in VL
396: *        (Workspace: need 1, prefer HSWORK (see comments) )
397: *
398:          IWRK = ITAU
399:          CALL SHSEQR( 'S', 'V', N, ILO, IHI, A, LDA, WR, WI, VL, LDVL,
400:      $                WORK( IWRK ), LWORK-IWRK+1, INFO )
401: *
402:          IF( WANTVR ) THEN
403: *
404: *           Want left and right eigenvectors
405: *           Copy Schur vectors to VR
406: *
407:             SIDE = 'B'
408:             CALL SLACPY( 'F', N, N, VL, LDVL, VR, LDVR )
409:          END IF
410: *
411:       ELSE IF( WANTVR ) THEN
412: *
413: *        Want right eigenvectors
414: *        Copy Householder vectors to VR
415: *
416:          SIDE = 'R'
417:          CALL SLACPY( 'L', N, N, A, LDA, VR, LDVR )
418: *
419: *        Generate orthogonal matrix in VR
420: *        (Workspace: need 2*N-1, prefer N+(N-1)*NB)
421: *
422:          CALL SORGHR( N, ILO, IHI, VR, LDVR, WORK( ITAU ), WORK( IWRK ),
423:      $                LWORK-IWRK+1, IERR )
424: *
425: *        Perform QR iteration, accumulating Schur vectors in VR
426: *        (Workspace: need 1, prefer HSWORK (see comments) )
427: *
428:          IWRK = ITAU
429:          CALL SHSEQR( 'S', 'V', N, ILO, IHI, A, LDA, WR, WI, VR, LDVR,
430:      $                WORK( IWRK ), LWORK-IWRK+1, INFO )
431: *
432:       ELSE
433: *
434: *        Compute eigenvalues only
435: *        If condition numbers desired, compute Schur form
436: *
437:          IF( WNTSNN ) THEN
438:             JOB = 'E'
439:          ELSE
440:             JOB = 'S'
441:          END IF
442: *
443: *        (Workspace: need 1, prefer HSWORK (see comments) )
444: *
445:          IWRK = ITAU
446:          CALL SHSEQR( JOB, 'N', N, ILO, IHI, A, LDA, WR, WI, VR, LDVR,
447:      $                WORK( IWRK ), LWORK-IWRK+1, INFO )
448:       END IF
449: *
450: *     If INFO > 0 from SHSEQR, then quit
451: *
452:       IF( INFO.GT.0 )
453:      $   GO TO 50
454: *
455:       IF( WANTVL .OR. WANTVR ) THEN
456: *
457: *        Compute left and/or right eigenvectors
458: *        (Workspace: need 3*N)
459: *
460:          CALL STREVC( SIDE, 'B', SELECT, N, A, LDA, VL, LDVL, VR, LDVR,
461:      $                N, NOUT, WORK( IWRK ), IERR )
462:       END IF
463: *
464: *     Compute condition numbers if desired
465: *     (Workspace: need N*N+6*N unless SENSE = 'E')
466: *
467:       IF( .NOT.WNTSNN ) THEN
468:          CALL STRSNA( SENSE, 'A', SELECT, N, A, LDA, VL, LDVL, VR, LDVR,
469:      $                RCONDE, RCONDV, N, NOUT, WORK( IWRK ), N, IWORK,
470:      $                ICOND )
471:       END IF
472: *
473:       IF( WANTVL ) THEN
474: *
475: *        Undo balancing of left eigenvectors
476: *
477:          CALL SGEBAK( BALANC, 'L', N, ILO, IHI, SCALE, N, VL, LDVL,
478:      $                IERR )
479: *
480: *        Normalize left eigenvectors and make largest component real
481: *
482:          DO 20 I = 1, N
483:             IF( WI( I ).EQ.ZERO ) THEN
484:                SCL = ONE / SNRM2( N, VL( 1, I ), 1 )
485:                CALL SSCAL( N, SCL, VL( 1, I ), 1 )
486:             ELSE IF( WI( I ).GT.ZERO ) THEN
487:                SCL = ONE / SLAPY2( SNRM2( N, VL( 1, I ), 1 ),
488:      $               SNRM2( N, VL( 1, I+1 ), 1 ) )
489:                CALL SSCAL( N, SCL, VL( 1, I ), 1 )
490:                CALL SSCAL( N, SCL, VL( 1, I+1 ), 1 )
491:                DO 10 K = 1, N
492:                   WORK( K ) = VL( K, I )**2 + VL( K, I+1 )**2
493:    10          CONTINUE
494:                K = ISAMAX( N, WORK, 1 )
495:                CALL SLARTG( VL( K, I ), VL( K, I+1 ), CS, SN, R )
496:                CALL SROT( N, VL( 1, I ), 1, VL( 1, I+1 ), 1, CS, SN )
497:                VL( K, I+1 ) = ZERO
498:             END IF
499:    20    CONTINUE
500:       END IF
501: *
502:       IF( WANTVR ) THEN
503: *
504: *        Undo balancing of right eigenvectors
505: *
506:          CALL SGEBAK( BALANC, 'R', N, ILO, IHI, SCALE, N, VR, LDVR,
507:      $                IERR )
508: *
509: *        Normalize right eigenvectors and make largest component real
510: *
511:          DO 40 I = 1, N
512:             IF( WI( I ).EQ.ZERO ) THEN
513:                SCL = ONE / SNRM2( N, VR( 1, I ), 1 )
514:                CALL SSCAL( N, SCL, VR( 1, I ), 1 )
515:             ELSE IF( WI( I ).GT.ZERO ) THEN
516:                SCL = ONE / SLAPY2( SNRM2( N, VR( 1, I ), 1 ),
517:      $               SNRM2( N, VR( 1, I+1 ), 1 ) )
518:                CALL SSCAL( N, SCL, VR( 1, I ), 1 )
519:                CALL SSCAL( N, SCL, VR( 1, I+1 ), 1 )
520:                DO 30 K = 1, N
521:                   WORK( K ) = VR( K, I )**2 + VR( K, I+1 )**2
522:    30          CONTINUE
523:                K = ISAMAX( N, WORK, 1 )
524:                CALL SLARTG( VR( K, I ), VR( K, I+1 ), CS, SN, R )
525:                CALL SROT( N, VR( 1, I ), 1, VR( 1, I+1 ), 1, CS, SN )
526:                VR( K, I+1 ) = ZERO
527:             END IF
528:    40    CONTINUE
529:       END IF
530: *
531: *     Undo scaling if necessary
532: *
533:    50 CONTINUE
534:       IF( SCALEA ) THEN
535:          CALL SLASCL( 'G', 0, 0, CSCALE, ANRM, N-INFO, 1, WR( INFO+1 ),
536:      $                MAX( N-INFO, 1 ), IERR )
537:          CALL SLASCL( 'G', 0, 0, CSCALE, ANRM, N-INFO, 1, WI( INFO+1 ),
538:      $                MAX( N-INFO, 1 ), IERR )
539:          IF( INFO.EQ.0 ) THEN
540:             IF( ( WNTSNV .OR. WNTSNB ) .AND. ICOND.EQ.0 )
541:      $         CALL SLASCL( 'G', 0, 0, CSCALE, ANRM, N, 1, RCONDV, N,
542:      $                      IERR )
543:          ELSE
544:             CALL SLASCL( 'G', 0, 0, CSCALE, ANRM, ILO-1, 1, WR, N,
545:      $                   IERR )
546:             CALL SLASCL( 'G', 0, 0, CSCALE, ANRM, ILO-1, 1, WI, N,
547:      $                   IERR )
548:          END IF
549:       END IF
550: *
551:       WORK( 1 ) = MAXWRK
552:       RETURN
553: *
554: *     End of SGEEVX
555: *
556:       END
557: