1 *> \brief <b> DGEEVX computes the eigenvalues and, optionally, the left and/or right eigenvectors for GE matrices</b>
3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download DGEEVX + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dgeevx.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dgeevx.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dgeevx.f">
21 * SUBROUTINE DGEEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, WR, WI,
22 * VL, LDVL, VR, LDVR, ILO, IHI, SCALE, ABNRM,
23 * RCONDE, RCONDV, WORK, LWORK, IWORK, INFO )
25 * .. Scalar Arguments ..
26 * CHARACTER BALANC, JOBVL, JOBVR, SENSE
27 * INTEGER IHI, ILO, INFO, LDA, LDVL, LDVR, LWORK, N
28 * DOUBLE PRECISION ABNRM
30 * .. Array Arguments ..
32 * DOUBLE PRECISION A( LDA, * ), RCONDE( * ), RCONDV( * ),
33 * $ SCALE( * ), VL( LDVL, * ), VR( LDVR, * ),
34 * $ WI( * ), WORK( * ), WR( * )
43 *> DGEEVX computes for an N-by-N real nonsymmetric matrix A, the
44 *> eigenvalues and, optionally, the left and/or right eigenvectors.
46 *> Optionally also, it computes a balancing transformation to improve
47 *> the conditioning of the eigenvalues and eigenvectors (ILO, IHI,
48 *> SCALE, and ABNRM), reciprocal condition numbers for the eigenvalues
49 *> (RCONDE), and reciprocal condition numbers for the right
50 *> eigenvectors (RCONDV).
52 *> The right eigenvector v(j) of A satisfies
53 *> A * v(j) = lambda(j) * v(j)
54 *> where lambda(j) is its eigenvalue.
55 *> The left eigenvector u(j) of A satisfies
56 *> u(j)**H * A = lambda(j) * u(j)**H
57 *> where u(j)**H denotes the conjugate-transpose of u(j).
59 *> The computed eigenvectors are normalized to have Euclidean norm
60 *> equal to 1 and largest component real.
62 *> Balancing a matrix means permuting the rows and columns to make it
63 *> more nearly upper triangular, and applying a diagonal similarity
64 *> transformation D * A * D**(-1), where D is a diagonal matrix, to
65 *> make its rows and columns closer in norm and the condition numbers
66 *> of its eigenvalues and eigenvectors smaller. The computed
67 *> reciprocal condition numbers correspond to the balanced matrix.
68 *> Permuting rows and columns will not change the condition numbers
69 *> (in exact arithmetic) but diagonal scaling will. For further
70 *> explanation of balancing, see section 4.10.2 of the LAPACK
79 *> BALANC is CHARACTER*1
80 *> Indicates how the input matrix should be diagonally scaled
81 *> and/or permuted to improve the conditioning of its
83 *> = 'N': Do not diagonally scale or permute;
84 *> = 'P': Perform permutations to make the matrix more nearly
85 *> upper triangular. Do not diagonally scale;
86 *> = 'S': Diagonally scale the matrix, i.e. replace A by
87 *> D*A*D**(-1), where D is a diagonal matrix chosen
88 *> to make the rows and columns of A more equal in
89 *> norm. Do not permute;
90 *> = 'B': Both diagonally scale and permute A.
92 *> Computed reciprocal condition numbers will be for the matrix
93 *> after balancing and/or permuting. Permuting does not change
94 *> condition numbers (in exact arithmetic), but balancing does.
99 *> JOBVL is CHARACTER*1
100 *> = 'N': left eigenvectors of A are not computed;
101 *> = 'V': left eigenvectors of A are computed.
102 *> If SENSE = 'E' or 'B', JOBVL must = 'V'.
107 *> JOBVR is CHARACTER*1
108 *> = 'N': right eigenvectors of A are not computed;
109 *> = 'V': right eigenvectors of A are computed.
110 *> If SENSE = 'E' or 'B', JOBVR must = 'V'.
115 *> SENSE is CHARACTER*1
116 *> Determines which reciprocal condition numbers are computed.
117 *> = 'N': None are computed;
118 *> = 'E': Computed for eigenvalues only;
119 *> = 'V': Computed for right eigenvectors only;
120 *> = 'B': Computed for eigenvalues and right eigenvectors.
122 *> If SENSE = 'E' or 'B', both left and right eigenvectors
123 *> must also be computed (JOBVL = 'V' and JOBVR = 'V').
129 *> The order of the matrix A. N >= 0.
134 *> A is DOUBLE PRECISION array, dimension (LDA,N)
135 *> On entry, the N-by-N matrix A.
136 *> On exit, A has been overwritten. If JOBVL = 'V' or
137 *> JOBVR = 'V', A contains the real Schur form of the balanced
138 *> version of the input matrix A.
144 *> The leading dimension of the array A. LDA >= max(1,N).
149 *> WR is DOUBLE PRECISION array, dimension (N)
154 *> WI is DOUBLE PRECISION array, dimension (N)
155 *> WR and WI contain the real and imaginary parts,
156 *> respectively, of the computed eigenvalues. Complex
157 *> conjugate pairs of eigenvalues will appear consecutively
158 *> with the eigenvalue having the positive imaginary part
164 *> VL is DOUBLE PRECISION array, dimension (LDVL,N)
165 *> If JOBVL = 'V', the left eigenvectors u(j) are stored one
166 *> after another in the columns of VL, in the same order
167 *> as their eigenvalues.
168 *> If JOBVL = 'N', VL is not referenced.
169 *> If the j-th eigenvalue is real, then u(j) = VL(:,j),
170 *> the j-th column of VL.
171 *> If the j-th and (j+1)-st eigenvalues form a complex
172 *> conjugate pair, then u(j) = VL(:,j) + i*VL(:,j+1) and
173 *> u(j+1) = VL(:,j) - i*VL(:,j+1).
179 *> The leading dimension of the array VL. LDVL >= 1; if
180 *> JOBVL = 'V', LDVL >= N.
185 *> VR is DOUBLE PRECISION array, dimension (LDVR,N)
186 *> If JOBVR = 'V', the right eigenvectors v(j) are stored one
187 *> after another in the columns of VR, in the same order
188 *> as their eigenvalues.
189 *> If JOBVR = 'N', VR is not referenced.
190 *> If the j-th eigenvalue is real, then v(j) = VR(:,j),
191 *> the j-th column of VR.
192 *> If the j-th and (j+1)-st eigenvalues form a complex
193 *> conjugate pair, then v(j) = VR(:,j) + i*VR(:,j+1) and
194 *> v(j+1) = VR(:,j) - i*VR(:,j+1).
200 *> The leading dimension of the array VR. LDVR >= 1, and if
201 *> JOBVR = 'V', LDVR >= N.
212 *> ILO and IHI are integer values determined when A was
213 *> balanced. The balanced A(i,j) = 0 if I > J and
214 *> J = 1,...,ILO-1 or I = IHI+1,...,N.
219 *> SCALE is DOUBLE PRECISION array, dimension (N)
220 *> Details of the permutations and scaling factors applied
221 *> when balancing A. If P(j) is the index of the row and column
222 *> interchanged with row and column j, and D(j) is the scaling
223 *> factor applied to row and column j, then
224 *> SCALE(J) = P(J), for J = 1,...,ILO-1
225 *> = D(J), for J = ILO,...,IHI
226 *> = P(J) for J = IHI+1,...,N.
227 *> The order in which the interchanges are made is N to IHI+1,
233 *> ABNRM is DOUBLE PRECISION
234 *> The one-norm of the balanced matrix (the maximum
235 *> of the sum of absolute values of elements of any column).
238 *> \param[out] RCONDE
240 *> RCONDE is DOUBLE PRECISION array, dimension (N)
241 *> RCONDE(j) is the reciprocal condition number of the j-th
245 *> \param[out] RCONDV
247 *> RCONDV is DOUBLE PRECISION array, dimension (N)
248 *> RCONDV(j) is the reciprocal condition number of the j-th
249 *> right eigenvector.
254 *> WORK is DOUBLE PRECISION array, dimension (MAX(1,LWORK))
255 *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
261 *> The dimension of the array WORK. If SENSE = 'N' or 'E',
262 *> LWORK >= max(1,2*N), and if JOBVL = 'V' or JOBVR = 'V',
263 *> LWORK >= 3*N. If SENSE = 'V' or 'B', LWORK >= N*(N+6).
264 *> For good performance, LWORK must generally be larger.
266 *> If LWORK = -1, then a workspace query is assumed; the routine
267 *> only calculates the optimal size of the WORK array, returns
268 *> this value as the first entry of the WORK array, and no error
269 *> message related to LWORK is issued by XERBLA.
274 *> IWORK is INTEGER array, dimension (2*N-2)
275 *> If SENSE = 'N' or 'E', not referenced.
281 *> = 0: successful exit
282 *> < 0: if INFO = -i, the i-th argument had an illegal value.
283 *> > 0: if INFO = i, the QR algorithm failed to compute all the
284 *> eigenvalues, and no eigenvectors or condition numbers
285 *> have been computed; elements 1:ILO-1 and i+1:N of WR
286 *> and WI contain eigenvalues which have converged.
292 *> \author Univ. of Tennessee
293 *> \author Univ. of California Berkeley
294 *> \author Univ. of Colorado Denver
299 * @precisions fortran d -> s
301 *> \ingroup doubleGEeigen
303 * =====================================================================
304 SUBROUTINE DGEEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, WR, WI,
305 $ VL, LDVL, VR, LDVR, ILO, IHI, SCALE, ABNRM,
306 $ RCONDE, RCONDV, WORK, LWORK, IWORK, INFO )
309 * -- LAPACK driver routine (version 3.6.1) --
310 * -- LAPACK is a software package provided by Univ. of Tennessee, --
311 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
314 * .. Scalar Arguments ..
315 CHARACTER BALANC, JOBVL, JOBVR, SENSE
316 INTEGER IHI, ILO, INFO, LDA, LDVL, LDVR, LWORK, N
317 DOUBLE PRECISION ABNRM
319 * .. Array Arguments ..
321 DOUBLE PRECISION A( LDA, * ), RCONDE( * ), RCONDV( * ),
322 $ SCALE( * ), VL( LDVL, * ), VR( LDVR, * ),
323 $ WI( * ), WORK( * ), WR( * )
326 * =====================================================================
329 DOUBLE PRECISION ZERO, ONE
330 PARAMETER ( ZERO = 0.0D0, ONE = 1.0D0 )
332 * .. Local Scalars ..
333 LOGICAL LQUERY, SCALEA, WANTVL, WANTVR, WNTSNB, WNTSNE,
336 INTEGER HSWORK, I, ICOND, IERR, ITAU, IWRK, K,
337 $ LWORK_TREVC, MAXWRK, MINWRK, NOUT
338 DOUBLE PRECISION ANRM, BIGNUM, CS, CSCALE, EPS, R, SCL, SMLNUM,
343 DOUBLE PRECISION DUM( 1 )
345 * .. External Subroutines ..
346 EXTERNAL DGEBAK, DGEBAL, DGEHRD, DHSEQR, DLABAD, DLACPY,
347 $ DLARTG, DLASCL, DORGHR, DROT, DSCAL, DTREVC3,
350 * .. External Functions ..
352 INTEGER IDAMAX, ILAENV
353 DOUBLE PRECISION DLAMCH, DLANGE, DLAPY2, DNRM2
354 EXTERNAL LSAME, IDAMAX, ILAENV, DLAMCH, DLANGE, DLAPY2,
357 * .. Intrinsic Functions ..
360 * .. Executable Statements ..
362 * Test the input arguments
365 LQUERY = ( LWORK.EQ.-1 )
366 WANTVL = LSAME( JOBVL, 'V' )
367 WANTVR = LSAME( JOBVR, 'V' )
368 WNTSNN = LSAME( SENSE, 'N' )
369 WNTSNE = LSAME( SENSE, 'E' )
370 WNTSNV = LSAME( SENSE, 'V' )
371 WNTSNB = LSAME( SENSE, 'B' )
372 IF( .NOT.( LSAME( BALANC, 'N' ) .OR. LSAME( BALANC, 'S' )
373 $ .OR. LSAME( BALANC, 'P' ) .OR. LSAME( BALANC, 'B' ) ) )
376 ELSE IF( ( .NOT.WANTVL ) .AND. ( .NOT.LSAME( JOBVL, 'N' ) ) ) THEN
378 ELSE IF( ( .NOT.WANTVR ) .AND. ( .NOT.LSAME( JOBVR, 'N' ) ) ) THEN
380 ELSE IF( .NOT.( WNTSNN .OR. WNTSNE .OR. WNTSNB .OR. WNTSNV ) .OR.
381 $ ( ( WNTSNE .OR. WNTSNB ) .AND. .NOT.( WANTVL .AND.
384 ELSE IF( N.LT.0 ) THEN
386 ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
388 ELSE IF( LDVL.LT.1 .OR. ( WANTVL .AND. LDVL.LT.N ) ) THEN
390 ELSE IF( LDVR.LT.1 .OR. ( WANTVR .AND. LDVR.LT.N ) ) THEN
395 * (Note: Comments in the code beginning "Workspace:" describe the
396 * minimal amount of workspace needed at that point in the code,
397 * as well as the preferred amount for good performance.
398 * NB refers to the optimal block size for the immediately
399 * following subroutine, as returned by ILAENV.
400 * HSWORK refers to the workspace preferred by DHSEQR, as
401 * calculated below. HSWORK is computed assuming ILO=1 and IHI=N,
409 MAXWRK = N + N*ILAENV( 1, 'DGEHRD', ' ', N, 1, N, 0 )
412 CALL DTREVC3( 'L', 'B', SELECT, N, A, LDA,
413 $ VL, LDVL, VR, LDVR,
414 $ N, NOUT, WORK, -1, IERR )
415 LWORK_TREVC = INT( WORK(1) )
416 MAXWRK = MAX( MAXWRK, N + LWORK_TREVC )
417 CALL DHSEQR( 'S', 'V', N, 1, N, A, LDA, WR, WI, VL, LDVL,
419 ELSE IF( WANTVR ) THEN
420 CALL DTREVC3( 'R', 'B', SELECT, N, A, LDA,
421 $ VL, LDVL, VR, LDVR,
422 $ N, NOUT, WORK, -1, IERR )
423 LWORK_TREVC = INT( WORK(1) )
424 MAXWRK = MAX( MAXWRK, N + LWORK_TREVC )
425 CALL DHSEQR( 'S', 'V', N, 1, N, A, LDA, WR, WI, VR, LDVR,
429 CALL DHSEQR( 'E', 'N', N, 1, N, A, LDA, WR, WI, VR,
430 $ LDVR, WORK, -1, INFO )
432 CALL DHSEQR( 'S', 'N', N, 1, N, A, LDA, WR, WI, VR,
433 $ LDVR, WORK, -1, INFO )
436 HSWORK = INT( WORK(1) )
438 IF( ( .NOT.WANTVL ) .AND. ( .NOT.WANTVR ) ) THEN
441 $ MINWRK = MAX( MINWRK, N*N+6*N )
442 MAXWRK = MAX( MAXWRK, HSWORK )
444 $ MAXWRK = MAX( MAXWRK, N*N + 6*N )
447 IF( ( .NOT.WNTSNN ) .AND. ( .NOT.WNTSNE ) )
448 $ MINWRK = MAX( MINWRK, N*N + 6*N )
449 MAXWRK = MAX( MAXWRK, HSWORK )
450 MAXWRK = MAX( MAXWRK, N + ( N - 1 )*ILAENV( 1, 'DORGHR',
451 $ ' ', N, 1, N, -1 ) )
452 IF( ( .NOT.WNTSNN ) .AND. ( .NOT.WNTSNE ) )
453 $ MAXWRK = MAX( MAXWRK, N*N + 6*N )
454 MAXWRK = MAX( MAXWRK, 3*N )
456 MAXWRK = MAX( MAXWRK, MINWRK )
460 IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
466 CALL XERBLA( 'DGEEVX', -INFO )
468 ELSE IF( LQUERY ) THEN
472 * Quick return if possible
477 * Get machine constants
480 SMLNUM = DLAMCH( 'S' )
481 BIGNUM = ONE / SMLNUM
482 CALL DLABAD( SMLNUM, BIGNUM )
483 SMLNUM = SQRT( SMLNUM ) / EPS
484 BIGNUM = ONE / SMLNUM
486 * Scale A if max element outside range [SMLNUM,BIGNUM]
489 ANRM = DLANGE( 'M', N, N, A, LDA, DUM )
491 IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
494 ELSE IF( ANRM.GT.BIGNUM ) THEN
499 $ CALL DLASCL( 'G', 0, 0, ANRM, CSCALE, N, N, A, LDA, IERR )
501 * Balance the matrix and compute ABNRM
503 CALL DGEBAL( BALANC, N, A, LDA, ILO, IHI, SCALE, IERR )
504 ABNRM = DLANGE( '1', N, N, A, LDA, DUM )
507 CALL DLASCL( 'G', 0, 0, CSCALE, ANRM, 1, 1, DUM, 1, IERR )
511 * Reduce to upper Hessenberg form
512 * (Workspace: need 2*N, prefer N+N*NB)
516 CALL DGEHRD( N, ILO, IHI, A, LDA, WORK( ITAU ), WORK( IWRK ),
517 $ LWORK-IWRK+1, IERR )
521 * Want left eigenvectors
522 * Copy Householder vectors to VL
525 CALL DLACPY( 'L', N, N, A, LDA, VL, LDVL )
527 * Generate orthogonal matrix in VL
528 * (Workspace: need 2*N-1, prefer N+(N-1)*NB)
530 CALL DORGHR( N, ILO, IHI, VL, LDVL, WORK( ITAU ), WORK( IWRK ),
531 $ LWORK-IWRK+1, IERR )
533 * Perform QR iteration, accumulating Schur vectors in VL
534 * (Workspace: need 1, prefer HSWORK (see comments) )
537 CALL DHSEQR( 'S', 'V', N, ILO, IHI, A, LDA, WR, WI, VL, LDVL,
538 $ WORK( IWRK ), LWORK-IWRK+1, INFO )
542 * Want left and right eigenvectors
543 * Copy Schur vectors to VR
546 CALL DLACPY( 'F', N, N, VL, LDVL, VR, LDVR )
549 ELSE IF( WANTVR ) THEN
551 * Want right eigenvectors
552 * Copy Householder vectors to VR
555 CALL DLACPY( 'L', N, N, A, LDA, VR, LDVR )
557 * Generate orthogonal matrix in VR
558 * (Workspace: need 2*N-1, prefer N+(N-1)*NB)
560 CALL DORGHR( N, ILO, IHI, VR, LDVR, WORK( ITAU ), WORK( IWRK ),
561 $ LWORK-IWRK+1, IERR )
563 * Perform QR iteration, accumulating Schur vectors in VR
564 * (Workspace: need 1, prefer HSWORK (see comments) )
567 CALL DHSEQR( 'S', 'V', N, ILO, IHI, A, LDA, WR, WI, VR, LDVR,
568 $ WORK( IWRK ), LWORK-IWRK+1, INFO )
572 * Compute eigenvalues only
573 * If condition numbers desired, compute Schur form
581 * (Workspace: need 1, prefer HSWORK (see comments) )
584 CALL DHSEQR( JOB, 'N', N, ILO, IHI, A, LDA, WR, WI, VR, LDVR,
585 $ WORK( IWRK ), LWORK-IWRK+1, INFO )
588 * If INFO .NE. 0 from DHSEQR, then quit
593 IF( WANTVL .OR. WANTVR ) THEN
595 * Compute left and/or right eigenvectors
596 * (Workspace: need 3*N, prefer N + 2*N*NB)
598 CALL DTREVC3( SIDE, 'B', SELECT, N, A, LDA, VL, LDVL, VR, LDVR,
599 $ N, NOUT, WORK( IWRK ), LWORK-IWRK+1, IERR )
602 * Compute condition numbers if desired
603 * (Workspace: need N*N+6*N unless SENSE = 'E')
605 IF( .NOT.WNTSNN ) THEN
606 CALL DTRSNA( SENSE, 'A', SELECT, N, A, LDA, VL, LDVL, VR, LDVR,
607 $ RCONDE, RCONDV, N, NOUT, WORK( IWRK ), N, IWORK,
613 * Undo balancing of left eigenvectors
615 CALL DGEBAK( BALANC, 'L', N, ILO, IHI, SCALE, N, VL, LDVL,
618 * Normalize left eigenvectors and make largest component real
621 IF( WI( I ).EQ.ZERO ) THEN
622 SCL = ONE / DNRM2( N, VL( 1, I ), 1 )
623 CALL DSCAL( N, SCL, VL( 1, I ), 1 )
624 ELSE IF( WI( I ).GT.ZERO ) THEN
625 SCL = ONE / DLAPY2( DNRM2( N, VL( 1, I ), 1 ),
626 $ DNRM2( N, VL( 1, I+1 ), 1 ) )
627 CALL DSCAL( N, SCL, VL( 1, I ), 1 )
628 CALL DSCAL( N, SCL, VL( 1, I+1 ), 1 )
630 WORK( K ) = VL( K, I )**2 + VL( K, I+1 )**2
632 K = IDAMAX( N, WORK, 1 )
633 CALL DLARTG( VL( K, I ), VL( K, I+1 ), CS, SN, R )
634 CALL DROT( N, VL( 1, I ), 1, VL( 1, I+1 ), 1, CS, SN )
642 * Undo balancing of right eigenvectors
644 CALL DGEBAK( BALANC, 'R', N, ILO, IHI, SCALE, N, VR, LDVR,
647 * Normalize right eigenvectors and make largest component real
650 IF( WI( I ).EQ.ZERO ) THEN
651 SCL = ONE / DNRM2( N, VR( 1, I ), 1 )
652 CALL DSCAL( N, SCL, VR( 1, I ), 1 )
653 ELSE IF( WI( I ).GT.ZERO ) THEN
654 SCL = ONE / DLAPY2( DNRM2( N, VR( 1, I ), 1 ),
655 $ DNRM2( N, VR( 1, I+1 ), 1 ) )
656 CALL DSCAL( N, SCL, VR( 1, I ), 1 )
657 CALL DSCAL( N, SCL, VR( 1, I+1 ), 1 )
659 WORK( K ) = VR( K, I )**2 + VR( K, I+1 )**2
661 K = IDAMAX( N, WORK, 1 )
662 CALL DLARTG( VR( K, I ), VR( K, I+1 ), CS, SN, R )
663 CALL DROT( N, VR( 1, I ), 1, VR( 1, I+1 ), 1, CS, SN )
669 * Undo scaling if necessary
673 CALL DLASCL( 'G', 0, 0, CSCALE, ANRM, N-INFO, 1, WR( INFO+1 ),
674 $ MAX( N-INFO, 1 ), IERR )
675 CALL DLASCL( 'G', 0, 0, CSCALE, ANRM, N-INFO, 1, WI( INFO+1 ),
676 $ MAX( N-INFO, 1 ), IERR )
678 IF( ( WNTSNV .OR. WNTSNB ) .AND. ICOND.EQ.0 )
679 $ CALL DLASCL( 'G', 0, 0, CSCALE, ANRM, N, 1, RCONDV, N,
682 CALL DLASCL( 'G', 0, 0, CSCALE, ANRM, ILO-1, 1, WR, N,
684 CALL DLASCL( 'G', 0, 0, CSCALE, ANRM, ILO-1, 1, WI, N,