```001:       SUBROUTINE ZGEEV( JOBVL, JOBVR, N, A, LDA, W, VL, LDVL, VR, LDVR,
002:      \$                  WORK, LWORK, RWORK, INFO )
003: *
004: *  -- LAPACK driver routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       CHARACTER          JOBVL, JOBVR
010:       INTEGER            INFO, LDA, LDVL, LDVR, LWORK, N
011: *     ..
012: *     .. Array Arguments ..
013:       DOUBLE PRECISION   RWORK( * )
014:       COMPLEX*16         A( LDA, * ), VL( LDVL, * ), VR( LDVR, * ),
015:      \$                   W( * ), WORK( * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  ZGEEV computes for an N-by-N complex nonsymmetric matrix A, the
022: *  eigenvalues and, optionally, the left and/or right eigenvectors.
023: *
024: *  The right eigenvector v(j) of A satisfies
025: *                   A * v(j) = lambda(j) * v(j)
026: *  where lambda(j) is its eigenvalue.
027: *  The left eigenvector u(j) of A satisfies
028: *                u(j)**H * A = lambda(j) * u(j)**H
029: *  where u(j)**H denotes the conjugate transpose of u(j).
030: *
031: *  The computed eigenvectors are normalized to have Euclidean norm
032: *  equal to 1 and largest component real.
033: *
034: *  Arguments
035: *  =========
036: *
037: *  JOBVL   (input) CHARACTER*1
038: *          = 'N': left eigenvectors of A are not computed;
039: *          = 'V': left eigenvectors of are computed.
040: *
041: *  JOBVR   (input) CHARACTER*1
042: *          = 'N': right eigenvectors of A are not computed;
043: *          = 'V': right eigenvectors of A are computed.
044: *
045: *  N       (input) INTEGER
046: *          The order of the matrix A. N >= 0.
047: *
048: *  A       (input/output) COMPLEX*16 array, dimension (LDA,N)
049: *          On entry, the N-by-N matrix A.
050: *          On exit, A has been overwritten.
051: *
052: *  LDA     (input) INTEGER
053: *          The leading dimension of the array A.  LDA >= max(1,N).
054: *
055: *  W       (output) COMPLEX*16 array, dimension (N)
056: *          W contains the computed eigenvalues.
057: *
058: *  VL      (output) COMPLEX*16 array, dimension (LDVL,N)
059: *          If JOBVL = 'V', the left eigenvectors u(j) are stored one
060: *          after another in the columns of VL, in the same order
061: *          as their eigenvalues.
062: *          If JOBVL = 'N', VL is not referenced.
063: *          u(j) = VL(:,j), the j-th column of VL.
064: *
065: *  LDVL    (input) INTEGER
066: *          The leading dimension of the array VL.  LDVL >= 1; if
067: *          JOBVL = 'V', LDVL >= N.
068: *
069: *  VR      (output) COMPLEX*16 array, dimension (LDVR,N)
070: *          If JOBVR = 'V', the right eigenvectors v(j) are stored one
071: *          after another in the columns of VR, in the same order
072: *          as their eigenvalues.
073: *          If JOBVR = 'N', VR is not referenced.
074: *          v(j) = VR(:,j), the j-th column of VR.
075: *
076: *  LDVR    (input) INTEGER
077: *          The leading dimension of the array VR.  LDVR >= 1; if
078: *          JOBVR = 'V', LDVR >= N.
079: *
080: *  WORK    (workspace/output) COMPLEX*16 array, dimension (MAX(1,LWORK))
081: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
082: *
083: *  LWORK   (input) INTEGER
084: *          The dimension of the array WORK.  LWORK >= max(1,2*N).
085: *          For good performance, LWORK must generally be larger.
086: *
087: *          If LWORK = -1, then a workspace query is assumed; the routine
088: *          only calculates the optimal size of the WORK array, returns
089: *          this value as the first entry of the WORK array, and no error
090: *          message related to LWORK is issued by XERBLA.
091: *
092: *  RWORK   (workspace) DOUBLE PRECISION array, dimension (2*N)
093: *
094: *  INFO    (output) INTEGER
095: *          = 0:  successful exit
096: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
097: *          > 0:  if INFO = i, the QR algorithm failed to compute all the
098: *                eigenvalues, and no eigenvectors have been computed;
099: *                elements and i+1:N of W contain eigenvalues which have
100: *                converged.
101: *
102: *  =====================================================================
103: *
104: *     .. Parameters ..
105:       DOUBLE PRECISION   ZERO, ONE
106:       PARAMETER          ( ZERO = 0.0D0, ONE = 1.0D0 )
107: *     ..
108: *     .. Local Scalars ..
109:       LOGICAL            LQUERY, SCALEA, WANTVL, WANTVR
110:       CHARACTER          SIDE
111:       INTEGER            HSWORK, I, IBAL, IERR, IHI, ILO, IRWORK, ITAU,
112:      \$                   IWRK, K, MAXWRK, MINWRK, NOUT
113:       DOUBLE PRECISION   ANRM, BIGNUM, CSCALE, EPS, SCL, SMLNUM
114:       COMPLEX*16         TMP
115: *     ..
116: *     .. Local Arrays ..
117:       LOGICAL            SELECT( 1 )
118:       DOUBLE PRECISION   DUM( 1 )
119: *     ..
120: *     .. External Subroutines ..
121:       EXTERNAL           DLABAD, XERBLA, ZDSCAL, ZGEBAK, ZGEBAL, ZGEHRD,
122:      \$                   ZHSEQR, ZLACPY, ZLASCL, ZSCAL, ZTREVC, ZUNGHR
123: *     ..
124: *     .. External Functions ..
125:       LOGICAL            LSAME
126:       INTEGER            IDAMAX, ILAENV
127:       DOUBLE PRECISION   DLAMCH, DZNRM2, ZLANGE
128:       EXTERNAL           LSAME, IDAMAX, ILAENV, DLAMCH, DZNRM2, ZLANGE
129: *     ..
130: *     .. Intrinsic Functions ..
131:       INTRINSIC          DBLE, DCMPLX, DCONJG, DIMAG, MAX, SQRT
132: *     ..
133: *     .. Executable Statements ..
134: *
135: *     Test the input arguments
136: *
137:       INFO = 0
138:       LQUERY = ( LWORK.EQ.-1 )
139:       WANTVL = LSAME( JOBVL, 'V' )
140:       WANTVR = LSAME( JOBVR, 'V' )
141:       IF( ( .NOT.WANTVL ) .AND. ( .NOT.LSAME( JOBVL, 'N' ) ) ) THEN
142:          INFO = -1
143:       ELSE IF( ( .NOT.WANTVR ) .AND. ( .NOT.LSAME( JOBVR, 'N' ) ) ) THEN
144:          INFO = -2
145:       ELSE IF( N.LT.0 ) THEN
146:          INFO = -3
147:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
148:          INFO = -5
149:       ELSE IF( LDVL.LT.1 .OR. ( WANTVL .AND. LDVL.LT.N ) ) THEN
150:          INFO = -8
151:       ELSE IF( LDVR.LT.1 .OR. ( WANTVR .AND. LDVR.LT.N ) ) THEN
152:          INFO = -10
153:       END IF
154: *
155: *     Compute workspace
156: *      (Note: Comments in the code beginning "Workspace:" describe the
157: *       minimal amount of workspace needed at that point in the code,
158: *       as well as the preferred amount for good performance.
159: *       CWorkspace refers to complex workspace, and RWorkspace to real
160: *       workspace. NB refers to the optimal block size for the
161: *       immediately following subroutine, as returned by ILAENV.
162: *       HSWORK refers to the workspace preferred by ZHSEQR, as
163: *       calculated below. HSWORK is computed assuming ILO=1 and IHI=N,
164: *       the worst case.)
165: *
166:       IF( INFO.EQ.0 ) THEN
167:          IF( N.EQ.0 ) THEN
168:             MINWRK = 1
169:             MAXWRK = 1
170:          ELSE
171:             MAXWRK = N + N*ILAENV( 1, 'ZGEHRD', ' ', N, 1, N, 0 )
172:             MINWRK = 2*N
173:             IF( WANTVL ) THEN
174:                MAXWRK = MAX( MAXWRK, N + ( N - 1 )*ILAENV( 1, 'ZUNGHR',
175:      \$                       ' ', N, 1, N, -1 ) )
176:                CALL ZHSEQR( 'S', 'V', N, 1, N, A, LDA, W, VL, LDVL,
177:      \$                WORK, -1, INFO )
178:             ELSE IF( WANTVR ) THEN
179:                MAXWRK = MAX( MAXWRK, N + ( N - 1 )*ILAENV( 1, 'ZUNGHR',
180:      \$                       ' ', N, 1, N, -1 ) )
181:                CALL ZHSEQR( 'S', 'V', N, 1, N, A, LDA, W, VR, LDVR,
182:      \$                WORK, -1, INFO )
183:             ELSE
184:                CALL ZHSEQR( 'E', 'N', N, 1, N, A, LDA, W, VR, LDVR,
185:      \$                WORK, -1, INFO )
186:             END IF
187:             HSWORK = WORK( 1 )
188:             MAXWRK = MAX( MAXWRK, HSWORK, MINWRK )
189:          END IF
190:          WORK( 1 ) = MAXWRK
191: *
192:          IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
193:             INFO = -12
194:          END IF
195:       END IF
196: *
197:       IF( INFO.NE.0 ) THEN
198:          CALL XERBLA( 'ZGEEV ', -INFO )
199:          RETURN
200:       ELSE IF( LQUERY ) THEN
201:          RETURN
202:       END IF
203: *
204: *     Quick return if possible
205: *
206:       IF( N.EQ.0 )
207:      \$   RETURN
208: *
209: *     Get machine constants
210: *
211:       EPS = DLAMCH( 'P' )
212:       SMLNUM = DLAMCH( 'S' )
213:       BIGNUM = ONE / SMLNUM
214:       CALL DLABAD( SMLNUM, BIGNUM )
215:       SMLNUM = SQRT( SMLNUM ) / EPS
216:       BIGNUM = ONE / SMLNUM
217: *
218: *     Scale A if max element outside range [SMLNUM,BIGNUM]
219: *
220:       ANRM = ZLANGE( 'M', N, N, A, LDA, DUM )
221:       SCALEA = .FALSE.
222:       IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
223:          SCALEA = .TRUE.
224:          CSCALE = SMLNUM
225:       ELSE IF( ANRM.GT.BIGNUM ) THEN
226:          SCALEA = .TRUE.
227:          CSCALE = BIGNUM
228:       END IF
229:       IF( SCALEA )
230:      \$   CALL ZLASCL( 'G', 0, 0, ANRM, CSCALE, N, N, A, LDA, IERR )
231: *
232: *     Balance the matrix
233: *     (CWorkspace: none)
234: *     (RWorkspace: need N)
235: *
236:       IBAL = 1
237:       CALL ZGEBAL( 'B', N, A, LDA, ILO, IHI, RWORK( IBAL ), IERR )
238: *
239: *     Reduce to upper Hessenberg form
240: *     (CWorkspace: need 2*N, prefer N+N*NB)
241: *     (RWorkspace: none)
242: *
243:       ITAU = 1
244:       IWRK = ITAU + N
245:       CALL ZGEHRD( N, ILO, IHI, A, LDA, WORK( ITAU ), WORK( IWRK ),
246:      \$             LWORK-IWRK+1, IERR )
247: *
248:       IF( WANTVL ) THEN
249: *
250: *        Want left eigenvectors
251: *        Copy Householder vectors to VL
252: *
253:          SIDE = 'L'
254:          CALL ZLACPY( 'L', N, N, A, LDA, VL, LDVL )
255: *
256: *        Generate unitary matrix in VL
257: *        (CWorkspace: need 2*N-1, prefer N+(N-1)*NB)
258: *        (RWorkspace: none)
259: *
260:          CALL ZUNGHR( N, ILO, IHI, VL, LDVL, WORK( ITAU ), WORK( IWRK ),
261:      \$                LWORK-IWRK+1, IERR )
262: *
263: *        Perform QR iteration, accumulating Schur vectors in VL
264: *        (CWorkspace: need 1, prefer HSWORK (see comments) )
265: *        (RWorkspace: none)
266: *
267:          IWRK = ITAU
268:          CALL ZHSEQR( 'S', 'V', N, ILO, IHI, A, LDA, W, VL, LDVL,
269:      \$                WORK( IWRK ), LWORK-IWRK+1, INFO )
270: *
271:          IF( WANTVR ) THEN
272: *
273: *           Want left and right eigenvectors
274: *           Copy Schur vectors to VR
275: *
276:             SIDE = 'B'
277:             CALL ZLACPY( 'F', N, N, VL, LDVL, VR, LDVR )
278:          END IF
279: *
280:       ELSE IF( WANTVR ) THEN
281: *
282: *        Want right eigenvectors
283: *        Copy Householder vectors to VR
284: *
285:          SIDE = 'R'
286:          CALL ZLACPY( 'L', N, N, A, LDA, VR, LDVR )
287: *
288: *        Generate unitary matrix in VR
289: *        (CWorkspace: need 2*N-1, prefer N+(N-1)*NB)
290: *        (RWorkspace: none)
291: *
292:          CALL ZUNGHR( N, ILO, IHI, VR, LDVR, WORK( ITAU ), WORK( IWRK ),
293:      \$                LWORK-IWRK+1, IERR )
294: *
295: *        Perform QR iteration, accumulating Schur vectors in VR
296: *        (CWorkspace: need 1, prefer HSWORK (see comments) )
297: *        (RWorkspace: none)
298: *
299:          IWRK = ITAU
300:          CALL ZHSEQR( 'S', 'V', N, ILO, IHI, A, LDA, W, VR, LDVR,
301:      \$                WORK( IWRK ), LWORK-IWRK+1, INFO )
302: *
303:       ELSE
304: *
305: *        Compute eigenvalues only
306: *        (CWorkspace: need 1, prefer HSWORK (see comments) )
307: *        (RWorkspace: none)
308: *
309:          IWRK = ITAU
310:          CALL ZHSEQR( 'E', 'N', N, ILO, IHI, A, LDA, W, VR, LDVR,
311:      \$                WORK( IWRK ), LWORK-IWRK+1, INFO )
312:       END IF
313: *
314: *     If INFO > 0 from ZHSEQR, then quit
315: *
316:       IF( INFO.GT.0 )
317:      \$   GO TO 50
318: *
319:       IF( WANTVL .OR. WANTVR ) THEN
320: *
321: *        Compute left and/or right eigenvectors
322: *        (CWorkspace: need 2*N)
323: *        (RWorkspace: need 2*N)
324: *
325:          IRWORK = IBAL + N
326:          CALL ZTREVC( SIDE, 'B', SELECT, N, A, LDA, VL, LDVL, VR, LDVR,
327:      \$                N, NOUT, WORK( IWRK ), RWORK( IRWORK ), IERR )
328:       END IF
329: *
330:       IF( WANTVL ) THEN
331: *
332: *        Undo balancing of left eigenvectors
333: *        (CWorkspace: none)
334: *        (RWorkspace: need N)
335: *
336:          CALL ZGEBAK( 'B', 'L', N, ILO, IHI, RWORK( IBAL ), N, VL, LDVL,
337:      \$                IERR )
338: *
339: *        Normalize left eigenvectors and make largest component real
340: *
341:          DO 20 I = 1, N
342:             SCL = ONE / DZNRM2( N, VL( 1, I ), 1 )
343:             CALL ZDSCAL( N, SCL, VL( 1, I ), 1 )
344:             DO 10 K = 1, N
345:                RWORK( IRWORK+K-1 ) = DBLE( VL( K, I ) )**2 +
346:      \$                               DIMAG( VL( K, I ) )**2
347:    10       CONTINUE
348:             K = IDAMAX( N, RWORK( IRWORK ), 1 )
349:             TMP = DCONJG( VL( K, I ) ) / SQRT( RWORK( IRWORK+K-1 ) )
350:             CALL ZSCAL( N, TMP, VL( 1, I ), 1 )
351:             VL( K, I ) = DCMPLX( DBLE( VL( K, I ) ), ZERO )
352:    20    CONTINUE
353:       END IF
354: *
355:       IF( WANTVR ) THEN
356: *
357: *        Undo balancing of right eigenvectors
358: *        (CWorkspace: none)
359: *        (RWorkspace: need N)
360: *
361:          CALL ZGEBAK( 'B', 'R', N, ILO, IHI, RWORK( IBAL ), N, VR, LDVR,
362:      \$                IERR )
363: *
364: *        Normalize right eigenvectors and make largest component real
365: *
366:          DO 40 I = 1, N
367:             SCL = ONE / DZNRM2( N, VR( 1, I ), 1 )
368:             CALL ZDSCAL( N, SCL, VR( 1, I ), 1 )
369:             DO 30 K = 1, N
370:                RWORK( IRWORK+K-1 ) = DBLE( VR( K, I ) )**2 +
371:      \$                               DIMAG( VR( K, I ) )**2
372:    30       CONTINUE
373:             K = IDAMAX( N, RWORK( IRWORK ), 1 )
374:             TMP = DCONJG( VR( K, I ) ) / SQRT( RWORK( IRWORK+K-1 ) )
375:             CALL ZSCAL( N, TMP, VR( 1, I ), 1 )
376:             VR( K, I ) = DCMPLX( DBLE( VR( K, I ) ), ZERO )
377:    40    CONTINUE
378:       END IF
379: *
380: *     Undo scaling if necessary
381: *
382:    50 CONTINUE
383:       IF( SCALEA ) THEN
384:          CALL ZLASCL( 'G', 0, 0, CSCALE, ANRM, N-INFO, 1, W( INFO+1 ),
385:      \$                MAX( N-INFO, 1 ), IERR )
386:          IF( INFO.GT.0 ) THEN
387:             CALL ZLASCL( 'G', 0, 0, CSCALE, ANRM, ILO-1, 1, W, N, IERR )
388:          END IF
389:       END IF
390: *
391:       WORK( 1 ) = MAXWRK
392:       RETURN
393: *
394: *     End of ZGEEV
395: *
396:       END
397: ```