001:       SUBROUTINE CHEGV( ITYPE, JOBZ, UPLO, N, A, LDA, B, LDB, W, WORK,
002:      $                  LWORK, RWORK, INFO )
003: *
004: *  -- LAPACK driver routine (version 3.2) --
005: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
006: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       CHARACTER          JOBZ, UPLO
011:       INTEGER            INFO, ITYPE, LDA, LDB, LWORK, N
012: *     ..
013: *     .. Array Arguments ..
014:       REAL               RWORK( * ), W( * )
015:       COMPLEX            A( LDA, * ), B( LDB, * ), WORK( * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  CHEGV computes all the eigenvalues, and optionally, the eigenvectors
022: *  of a complex generalized Hermitian-definite eigenproblem, of the form
023: *  A*x=(lambda)*B*x,  A*Bx=(lambda)*x,  or B*A*x=(lambda)*x.
024: *  Here A and B are assumed to be Hermitian and B is also
025: *  positive definite.
026: *
027: *  Arguments
028: *  =========
029: *
030: *  ITYPE   (input) INTEGER
031: *          Specifies the problem type to be solved:
032: *          = 1:  A*x = (lambda)*B*x
033: *          = 2:  A*B*x = (lambda)*x
034: *          = 3:  B*A*x = (lambda)*x
035: *
036: *  JOBZ    (input) CHARACTER*1
037: *          = 'N':  Compute eigenvalues only;
038: *          = 'V':  Compute eigenvalues and eigenvectors.
039: *
040: *  UPLO    (input) CHARACTER*1
041: *          = 'U':  Upper triangles of A and B are stored;
042: *          = 'L':  Lower triangles of A and B are stored.
043: *
044: *  N       (input) INTEGER
045: *          The order of the matrices A and B.  N >= 0.
046: *
047: *  A       (input/output) COMPLEX array, dimension (LDA, N)
048: *          On entry, the Hermitian matrix A.  If UPLO = 'U', the
049: *          leading N-by-N upper triangular part of A contains the
050: *          upper triangular part of the matrix A.  If UPLO = 'L',
051: *          the leading N-by-N lower triangular part of A contains
052: *          the lower triangular part of the matrix A.
053: *
054: *          On exit, if JOBZ = 'V', then if INFO = 0, A contains the
055: *          matrix Z of eigenvectors.  The eigenvectors are normalized
056: *          as follows:
057: *          if ITYPE = 1 or 2, Z**H*B*Z = I;
058: *          if ITYPE = 3, Z**H*inv(B)*Z = I.
059: *          If JOBZ = 'N', then on exit the upper triangle (if UPLO='U')
060: *          or the lower triangle (if UPLO='L') of A, including the
061: *          diagonal, is destroyed.
062: *
063: *  LDA     (input) INTEGER
064: *          The leading dimension of the array A.  LDA >= max(1,N).
065: *
066: *  B       (input/output) COMPLEX array, dimension (LDB, N)
067: *          On entry, the Hermitian positive definite matrix B.
068: *          If UPLO = 'U', the leading N-by-N upper triangular part of B
069: *          contains the upper triangular part of the matrix B.
070: *          If UPLO = 'L', the leading N-by-N lower triangular part of B
071: *          contains the lower triangular part of the matrix B.
072: *
073: *          On exit, if INFO <= N, the part of B containing the matrix is
074: *          overwritten by the triangular factor U or L from the Cholesky
075: *          factorization B = U**H*U or B = L*L**H.
076: *
077: *  LDB     (input) INTEGER
078: *          The leading dimension of the array B.  LDB >= max(1,N).
079: *
080: *  W       (output) REAL array, dimension (N)
081: *          If INFO = 0, the eigenvalues in ascending order.
082: *
083: *  WORK    (workspace/output) COMPLEX array, dimension (MAX(1,LWORK))
084: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
085: *
086: *  LWORK   (input) INTEGER
087: *          The length of the array WORK.  LWORK >= max(1,2*N-1).
088: *          For optimal efficiency, LWORK >= (NB+1)*N,
089: *          where NB is the blocksize for CHETRD returned by ILAENV.
090: *
091: *          If LWORK = -1, then a workspace query is assumed; the routine
092: *          only calculates the optimal size of the WORK array, returns
093: *          this value as the first entry of the WORK array, and no error
094: *          message related to LWORK is issued by XERBLA.
095: *
096: *  RWORK   (workspace) REAL array, dimension (max(1, 3*N-2))
097: *
098: *  INFO    (output) INTEGER
099: *          = 0:  successful exit
100: *          < 0:  if INFO = -i, the i-th argument had an illegal value
101: *          > 0:  CPOTRF or CHEEV returned an error code:
102: *             <= N:  if INFO = i, CHEEV failed to converge;
103: *                    i off-diagonal elements of an intermediate
104: *                    tridiagonal form did not converge to zero;
105: *             > N:   if INFO = N + i, for 1 <= i <= N, then the leading
106: *                    minor of order i of B is not positive definite.
107: *                    The factorization of B could not be completed and
108: *                    no eigenvalues or eigenvectors were computed.
109: *
110: *  =====================================================================
111: *
112: *     .. Parameters ..
113:       COMPLEX            ONE
114:       PARAMETER          ( ONE = ( 1.0E+0, 0.0E+0 ) )
115: *     ..
116: *     .. Local Scalars ..
117:       LOGICAL            LQUERY, UPPER, WANTZ
118:       CHARACTER          TRANS
119:       INTEGER            LWKOPT, NB, NEIG
120: *     ..
121: *     .. External Functions ..
122:       LOGICAL            LSAME
123:       INTEGER            ILAENV
124:       EXTERNAL           ILAENV, LSAME
125: *     ..
126: *     .. External Subroutines ..
127:       EXTERNAL           CHEEV, CHEGST, CPOTRF, CTRMM, CTRSM, XERBLA
128: *     ..
129: *     .. Intrinsic Functions ..
130:       INTRINSIC          MAX
131: *     ..
132: *     .. Executable Statements ..
133: *
134: *     Test the input parameters.
135: *
136:       WANTZ = LSAME( JOBZ, 'V' )
137:       UPPER = LSAME( UPLO, 'U' )
138:       LQUERY = ( LWORK.EQ. -1 )
139: *
140:       INFO = 0
141:       IF( ITYPE.LT.1 .OR. ITYPE.GT.3 ) THEN
142:          INFO = -1
143:       ELSE IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
144:          INFO = -2
145:       ELSE IF( .NOT.( UPPER .OR. LSAME( UPLO, 'L' ) ) ) THEN
146:          INFO = -3
147:       ELSE IF( N.LT.0 ) THEN
148:          INFO = -4
149:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
150:          INFO = -6
151:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
152:          INFO = -8
153:       END IF
154: *
155:       IF( INFO.EQ.0 ) THEN
156:          NB = ILAENV( 1, 'CHETRD', UPLO, N, -1, -1, -1 )
157:          LWKOPT = MAX( 1, ( NB + 1 )*N )
158:          WORK( 1 ) = LWKOPT
159: *
160:          IF( LWORK.LT.MAX( 1, 2*N-1 ) .AND. .NOT.LQUERY ) THEN
161:             INFO = -11
162:          END IF
163:       END IF
164: *
165:       IF( INFO.NE.0 ) THEN
166:          CALL XERBLA( 'CHEGV ', -INFO )
167:          RETURN
168:       ELSE IF( LQUERY ) THEN
169:          RETURN
170:       END IF
171: *
172: *     Quick return if possible
173: *
174:       IF( N.EQ.0 )
175:      $   RETURN
176: *
177: *     Form a Cholesky factorization of B.
178: *
179:       CALL CPOTRF( UPLO, N, B, LDB, INFO )
180:       IF( INFO.NE.0 ) THEN
181:          INFO = N + INFO
182:          RETURN
183:       END IF
184: *
185: *     Transform problem to standard eigenvalue problem and solve.
186: *
187:       CALL CHEGST( ITYPE, UPLO, N, A, LDA, B, LDB, INFO )
188:       CALL CHEEV( JOBZ, UPLO, N, A, LDA, W, WORK, LWORK, RWORK, INFO )
189: *
190:       IF( WANTZ ) THEN
191: *
192: *        Backtransform eigenvectors to the original problem.
193: *
194:          NEIG = N
195:          IF( INFO.GT.0 )
196:      $      NEIG = INFO - 1
197:          IF( ITYPE.EQ.1 .OR. ITYPE.EQ.2 ) THEN
198: *
199: *           For A*x=(lambda)*B*x and A*B*x=(lambda)*x;
200: *           backtransform eigenvectors: x = inv(L)'*y or inv(U)*y
201: *
202:             IF( UPPER ) THEN
203:                TRANS = 'N'
204:             ELSE
205:                TRANS = 'C'
206:             END IF
207: *
208:             CALL CTRSM( 'Left', UPLO, TRANS, 'Non-unit', N, NEIG, ONE,
209:      $                  B, LDB, A, LDA )
210: *
211:          ELSE IF( ITYPE.EQ.3 ) THEN
212: *
213: *           For B*A*x=(lambda)*x;
214: *           backtransform eigenvectors: x = L*y or U'*y
215: *
216:             IF( UPPER ) THEN
217:                TRANS = 'C'
218:             ELSE
219:                TRANS = 'N'
220:             END IF
221: *
222:             CALL CTRMM( 'Left', UPLO, TRANS, 'Non-unit', N, NEIG, ONE,
223:      $                  B, LDB, A, LDA )
224:          END IF
225:       END IF
226: *
227:       WORK( 1 ) = LWKOPT
228: *
229:       RETURN
230: *
231: *     End of CHEGV
232: *
233:       END
234: