001:       SUBROUTINE DGEEVX( 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: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       CHARACTER          BALANC, JOBVL, JOBVR, SENSE
011:       INTEGER            IHI, ILO, INFO, LDA, LDVL, LDVR, LWORK, N
012:       DOUBLE PRECISION   ABNRM
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IWORK( * )
016:       DOUBLE PRECISION   A( LDA, * ), RCONDE( * ), RCONDV( * ),
017:      $                   SCALE( * ), VL( LDVL, * ), VR( LDVR, * ),
018:      $                   WI( * ), WORK( * ), WR( * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  DGEEVX computes for an N-by-N real nonsymmetric matrix A, the
025: *  eigenvalues and, optionally, the left and/or right eigenvectors.
026: *
027: *  Optionally also, it computes a balancing transformation to improve
028: *  the conditioning of the eigenvalues and eigenvectors (ILO, IHI,
029: *  SCALE, and ABNRM), reciprocal condition numbers for the eigenvalues
030: *  (RCONDE), and reciprocal condition numbers for the right
031: *  eigenvectors (RCONDV).
032: *
033: *  The right eigenvector v(j) of A satisfies
034: *                   A * v(j) = lambda(j) * v(j)
035: *  where lambda(j) is its eigenvalue.
036: *  The left eigenvector u(j) of A satisfies
037: *                u(j)**H * A = lambda(j) * u(j)**H
038: *  where u(j)**H denotes the conjugate transpose of u(j).
039: *
040: *  The computed eigenvectors are normalized to have Euclidean norm
041: *  equal to 1 and largest component real.
042: *
043: *  Balancing a matrix means permuting the rows and columns to make it
044: *  more nearly upper triangular, and applying a diagonal similarity
045: *  transformation D * A * D**(-1), where D is a diagonal matrix, to
046: *  make its rows and columns closer in norm and the condition numbers
047: *  of its eigenvalues and eigenvectors smaller.  The computed
048: *  reciprocal condition numbers correspond to the balanced matrix.
049: *  Permuting rows and columns will not change the condition numbers
050: *  (in exact arithmetic) but diagonal scaling will.  For further
051: *  explanation of balancing, see section 4.10.2 of the LAPACK
052: *  Users' Guide.
053: *
054: *  Arguments
055: *  =========
056: *
057: *  BALANC  (input) CHARACTER*1
058: *          Indicates how the input matrix should be diagonally scaled
059: *          and/or permuted to improve the conditioning of its
060: *          eigenvalues.
061: *          = 'N': Do not diagonally scale or permute;
062: *          = 'P': Perform permutations to make the matrix more nearly
063: *                 upper triangular. Do not diagonally scale;
064: *          = 'S': Diagonally scale the matrix, i.e. replace A by
065: *                 D*A*D**(-1), where D is a diagonal matrix chosen
066: *                 to make the rows and columns of A more equal in
067: *                 norm. Do not permute;
068: *          = 'B': Both diagonally scale and permute A.
069: *
070: *          Computed reciprocal condition numbers will be for the matrix
071: *          after balancing and/or permuting. Permuting does not change
072: *          condition numbers (in exact arithmetic), but balancing does.
073: *
074: *  JOBVL   (input) CHARACTER*1
075: *          = 'N': left eigenvectors of A are not computed;
076: *          = 'V': left eigenvectors of A are computed.
077: *          If SENSE = 'E' or 'B', JOBVL must = 'V'.
078: *
079: *  JOBVR   (input) CHARACTER*1
080: *          = 'N': right eigenvectors of A are not computed;
081: *          = 'V': right eigenvectors of A are computed.
082: *          If SENSE = 'E' or 'B', JOBVR must = 'V'.
083: *
084: *  SENSE   (input) CHARACTER*1
085: *          Determines which reciprocal condition numbers are computed.
086: *          = 'N': None are computed;
087: *          = 'E': Computed for eigenvalues only;
088: *          = 'V': Computed for right eigenvectors only;
089: *          = 'B': Computed for eigenvalues and right eigenvectors.
090: *
091: *          If SENSE = 'E' or 'B', both left and right eigenvectors
092: *          must also be computed (JOBVL = 'V' and JOBVR = 'V').
093: *
094: *  N       (input) INTEGER
095: *          The order of the matrix A. N >= 0.
096: *
097: *  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)
098: *          On entry, the N-by-N matrix A.
099: *          On exit, A has been overwritten.  If JOBVL = 'V' or
100: *          JOBVR = 'V', A contains the real Schur form of the balanced
101: *          version of the input matrix A.
102: *
103: *  LDA     (input) INTEGER
104: *          The leading dimension of the array A.  LDA >= max(1,N).
105: *
106: *  WR      (output) DOUBLE PRECISION array, dimension (N)
107: *  WI      (output) DOUBLE PRECISION array, dimension (N)
108: *          WR and WI contain the real and imaginary parts,
109: *          respectively, of the computed eigenvalues.  Complex
110: *          conjugate pairs of eigenvalues will appear consecutively
111: *          with the eigenvalue having the positive imaginary part
112: *          first.
113: *
114: *  VL      (output) DOUBLE PRECISION array, dimension (LDVL,N)
115: *          If JOBVL = 'V', the left eigenvectors u(j) are stored one
116: *          after another in the columns of VL, in the same order
117: *          as their eigenvalues.
118: *          If JOBVL = 'N', VL is not referenced.
119: *          If the j-th eigenvalue is real, then u(j) = VL(:,j),
120: *          the j-th column of VL.
121: *          If the j-th and (j+1)-st eigenvalues form a complex
122: *          conjugate pair, then u(j) = VL(:,j) + i*VL(:,j+1) and
123: *          u(j+1) = VL(:,j) - i*VL(:,j+1).
124: *
125: *  LDVL    (input) INTEGER
126: *          The leading dimension of the array VL.  LDVL >= 1; if
127: *          JOBVL = 'V', LDVL >= N.
128: *
129: *  VR      (output) DOUBLE PRECISION array, dimension (LDVR,N)
130: *          If JOBVR = 'V', the right eigenvectors v(j) are stored one
131: *          after another in the columns of VR, in the same order
132: *          as their eigenvalues.
133: *          If JOBVR = 'N', VR is not referenced.
134: *          If the j-th eigenvalue is real, then v(j) = VR(:,j),
135: *          the j-th column of VR.
136: *          If the j-th and (j+1)-st eigenvalues form a complex
137: *          conjugate pair, then v(j) = VR(:,j) + i*VR(:,j+1) and
138: *          v(j+1) = VR(:,j) - i*VR(:,j+1).
139: *
140: *  LDVR    (input) INTEGER
141: *          The leading dimension of the array VR.  LDVR >= 1, and if
142: *          JOBVR = 'V', LDVR >= N.
143: *
144: *  ILO     (output) INTEGER
145: *  IHI     (output) INTEGER
146: *          ILO and IHI are integer values determined when A was
147: *          balanced.  The balanced A(i,j) = 0 if I > J and
148: *          J = 1,...,ILO-1 or I = IHI+1,...,N.
149: *
150: *  SCALE   (output) DOUBLE PRECISION array, dimension (N)
151: *          Details of the permutations and scaling factors applied
152: *          when balancing A.  If P(j) is the index of the row and column
153: *          interchanged with row and column j, and D(j) is the scaling
154: *          factor applied to row and column j, then
155: *          SCALE(J) = P(J),    for J = 1,...,ILO-1
156: *                   = D(J),    for J = ILO,...,IHI
157: *                   = P(J)     for J = IHI+1,...,N.
158: *          The order in which the interchanges are made is N to IHI+1,
159: *          then 1 to ILO-1.
160: *
161: *  ABNRM   (output) DOUBLE PRECISION
162: *          The one-norm of the balanced matrix (the maximum
163: *          of the sum of absolute values of elements of any column).
164: *
165: *  RCONDE  (output) DOUBLE PRECISION array, dimension (N)
166: *          RCONDE(j) is the reciprocal condition number of the j-th
167: *          eigenvalue.
168: *
169: *  RCONDV  (output) DOUBLE PRECISION array, dimension (N)
170: *          RCONDV(j) is the reciprocal condition number of the j-th
171: *          right eigenvector.
172: *
173: *  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK))
174: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
175: *
176: *  LWORK   (input) INTEGER
177: *          The dimension of the array WORK.   If SENSE = 'N' or 'E',
178: *          LWORK >= max(1,2*N), and if JOBVL = 'V' or JOBVR = 'V',
179: *          LWORK >= 3*N.  If SENSE = 'V' or 'B', LWORK >= N*(N+6).
180: *          For good performance, LWORK must generally be larger.
181: *
182: *          If LWORK = -1, then a workspace query is assumed; the routine
183: *          only calculates the optimal size of the WORK array, returns
184: *          this value as the first entry of the WORK array, and no error
185: *          message related to LWORK is issued by XERBLA.
186: *
187: *  IWORK   (workspace) INTEGER array, dimension (2*N-2)
188: *          If SENSE = 'N' or 'E', not referenced.
189: *
190: *  INFO    (output) INTEGER
191: *          = 0:  successful exit
192: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
193: *          > 0:  if INFO = i, the QR algorithm failed to compute all the
194: *                eigenvalues, and no eigenvectors or condition numbers
195: *                have been computed; elements 1:ILO-1 and i+1:N of WR
196: *                and WI contain eigenvalues which have converged.
197: *
198: *  =====================================================================
199: *
200: *     .. Parameters ..
201:       DOUBLE PRECISION   ZERO, ONE
202:       PARAMETER          ( ZERO = 0.0D0, ONE = 1.0D0 )
203: *     ..
204: *     .. Local Scalars ..
205:       LOGICAL            LQUERY, SCALEA, WANTVL, WANTVR, WNTSNB, WNTSNE,
206:      $                   WNTSNN, WNTSNV
207:       CHARACTER          JOB, SIDE
208:       INTEGER            HSWORK, I, ICOND, IERR, ITAU, IWRK, K, MAXWRK,
209:      $                   MINWRK, NOUT
210:       DOUBLE PRECISION   ANRM, BIGNUM, CS, CSCALE, EPS, R, SCL, SMLNUM,
211:      $                   SN
212: *     ..
213: *     .. Local Arrays ..
214:       LOGICAL            SELECT( 1 )
215:       DOUBLE PRECISION   DUM( 1 )
216: *     ..
217: *     .. External Subroutines ..
218:       EXTERNAL           DGEBAK, DGEBAL, DGEHRD, DHSEQR, DLABAD, DLACPY,
219:      $                   DLARTG, DLASCL, DORGHR, DROT, DSCAL, DTREVC,
220:      $                   DTRSNA, XERBLA
221: *     ..
222: *     .. External Functions ..
223:       LOGICAL            LSAME
224:       INTEGER            IDAMAX, ILAENV
225:       DOUBLE PRECISION   DLAMCH, DLANGE, DLAPY2, DNRM2
226:       EXTERNAL           LSAME, IDAMAX, ILAENV, DLAMCH, DLANGE, DLAPY2,
227:      $                   DNRM2
228: *     ..
229: *     .. Intrinsic Functions ..
230:       INTRINSIC          MAX, SQRT
231: *     ..
232: *     .. Executable Statements ..
233: *
234: *     Test the input arguments
235: *
236:       INFO = 0
237:       LQUERY = ( LWORK.EQ.-1 )
238:       WANTVL = LSAME( JOBVL, 'V' )
239:       WANTVR = LSAME( JOBVR, 'V' )
240:       WNTSNN = LSAME( SENSE, 'N' )
241:       WNTSNE = LSAME( SENSE, 'E' )
242:       WNTSNV = LSAME( SENSE, 'V' )
243:       WNTSNB = LSAME( SENSE, 'B' )
244:       IF( .NOT.( LSAME( BALANC, 'N' ) .OR. LSAME( BALANC,
245:      $    'S' ) .OR. LSAME( BALANC, 'P' ) .OR. LSAME( BALANC, 'B' ) ) )
246:      $     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 DHSEQR, 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, 'DGEHRD', ' ', N, 1, N, 0 )
282: *
283:             IF( WANTVL ) THEN
284:                CALL DHSEQR( 'S', 'V', N, 1, N, A, LDA, WR, WI, VL, LDVL,
285:      $                WORK, -1, INFO )
286:             ELSE IF( WANTVR ) THEN
287:                CALL DHSEQR( 'S', 'V', N, 1, N, A, LDA, WR, WI, VR, LDVR,
288:      $                WORK, -1, INFO )
289:             ELSE
290:                IF( WNTSNN ) THEN
291:                   CALL DHSEQR( 'E', 'N', N, 1, N, A, LDA, WR, WI, VR,
292:      $                LDVR, WORK, -1, INFO )
293:                ELSE
294:                   CALL DHSEQR( '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, 'DORGHR',
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( 'DGEEVX', -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 = DLAMCH( 'P' )
342:       SMLNUM = DLAMCH( 'S' )
343:       BIGNUM = ONE / SMLNUM
344:       CALL DLABAD( 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 = DLANGE( '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 DLASCL( 'G', 0, 0, ANRM, CSCALE, N, N, A, LDA, IERR )
362: *
363: *     Balance the matrix and compute ABNRM
364: *
365:       CALL DGEBAL( BALANC, N, A, LDA, ILO, IHI, SCALE, IERR )
366:       ABNRM = DLANGE( '1', N, N, A, LDA, DUM )
367:       IF( SCALEA ) THEN
368:          DUM( 1 ) = ABNRM
369:          CALL DLASCL( '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 DGEHRD( 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 DLACPY( '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 DORGHR( 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 DHSEQR( '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 DLACPY( '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 DLACPY( '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 DORGHR( 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 DHSEQR( '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 DHSEQR( 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 DHSEQR, 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 DTREVC( 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 DTRSNA( 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 DGEBAK( 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 / DNRM2( N, VL( 1, I ), 1 )
485:                CALL DSCAL( N, SCL, VL( 1, I ), 1 )
486:             ELSE IF( WI( I ).GT.ZERO ) THEN
487:                SCL = ONE / DLAPY2( DNRM2( N, VL( 1, I ), 1 ),
488:      $               DNRM2( N, VL( 1, I+1 ), 1 ) )
489:                CALL DSCAL( N, SCL, VL( 1, I ), 1 )
490:                CALL DSCAL( 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 = IDAMAX( N, WORK, 1 )
495:                CALL DLARTG( VL( K, I ), VL( K, I+1 ), CS, SN, R )
496:                CALL DROT( 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 DGEBAK( 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 / DNRM2( N, VR( 1, I ), 1 )
514:                CALL DSCAL( N, SCL, VR( 1, I ), 1 )
515:             ELSE IF( WI( I ).GT.ZERO ) THEN
516:                SCL = ONE / DLAPY2( DNRM2( N, VR( 1, I ), 1 ),
517:      $               DNRM2( N, VR( 1, I+1 ), 1 ) )
518:                CALL DSCAL( N, SCL, VR( 1, I ), 1 )
519:                CALL DSCAL( 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 = IDAMAX( N, WORK, 1 )
524:                CALL DLARTG( VR( K, I ), VR( K, I+1 ), CS, SN, R )
525:                CALL DROT( 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 DLASCL( 'G', 0, 0, CSCALE, ANRM, N-INFO, 1, WR( INFO+1 ),
536:      $                MAX( N-INFO, 1 ), IERR )
537:          CALL DLASCL( '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 DLASCL( 'G', 0, 0, CSCALE, ANRM, N, 1, RCONDV, N,
542:      $                      IERR )
543:          ELSE
544:             CALL DLASCL( 'G', 0, 0, CSCALE, ANRM, ILO-1, 1, WR, N,
545:      $                   IERR )
546:             CALL DLASCL( '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 DGEEVX
555: *
556:       END
557: