001:       SUBROUTINE CHEGVX( ITYPE, JOBZ, RANGE, UPLO, N, A, LDA, B, LDB,
002:      $                   VL, VU, IL, IU, ABSTOL, M, W, Z, LDZ, WORK,
003:      $                   LWORK, RWORK, IWORK, IFAIL, 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          JOBZ, RANGE, UPLO
011:       INTEGER            IL, INFO, ITYPE, IU, LDA, LDB, LDZ, LWORK, M, N
012:       REAL               ABSTOL, VL, VU
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IFAIL( * ), IWORK( * )
016:       REAL               RWORK( * ), W( * )
017:       COMPLEX            A( LDA, * ), B( LDB, * ), WORK( * ),
018:      $                   Z( LDZ, * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  CHEGVX computes selected eigenvalues, and optionally, eigenvectors
025: *  of a complex generalized Hermitian-definite eigenproblem, of the form
026: *  A*x=(lambda)*B*x,  A*Bx=(lambda)*x,  or B*A*x=(lambda)*x.  Here A and
027: *  B are assumed to be Hermitian and B is also positive definite.
028: *  Eigenvalues and eigenvectors can be selected by specifying either a
029: *  range of values or a range of indices for the desired eigenvalues.
030: *
031: *  Arguments
032: *  =========
033: *
034: *  ITYPE   (input) INTEGER
035: *          Specifies the problem type to be solved:
036: *          = 1:  A*x = (lambda)*B*x
037: *          = 2:  A*B*x = (lambda)*x
038: *          = 3:  B*A*x = (lambda)*x
039: *
040: *  JOBZ    (input) CHARACTER*1
041: *          = 'N':  Compute eigenvalues only;
042: *          = 'V':  Compute eigenvalues and eigenvectors.
043: *
044: *  RANGE   (input) CHARACTER*1
045: *          = 'A': all eigenvalues will be found.
046: *          = 'V': all eigenvalues in the half-open interval (VL,VU]
047: *                 will be found.
048: *          = 'I': the IL-th through IU-th eigenvalues will be found.
049: **
050: *  UPLO    (input) CHARACTER*1
051: *          = 'U':  Upper triangles of A and B are stored;
052: *          = 'L':  Lower triangles of A and B are stored.
053: *
054: *  N       (input) INTEGER
055: *          The order of the matrices A and B.  N >= 0.
056: *
057: *  A       (input/output) COMPLEX array, dimension (LDA, N)
058: *          On entry, the Hermitian matrix A.  If UPLO = 'U', the
059: *          leading N-by-N upper triangular part of A contains the
060: *          upper triangular part of the matrix A.  If UPLO = 'L',
061: *          the leading N-by-N lower triangular part of A contains
062: *          the lower triangular part of the matrix A.
063: *
064: *          On exit,  the lower triangle (if UPLO='L') or the upper
065: *          triangle (if UPLO='U') of A, including the diagonal, is
066: *          destroyed.
067: *
068: *  LDA     (input) INTEGER
069: *          The leading dimension of the array A.  LDA >= max(1,N).
070: *
071: *  B       (input/output) COMPLEX array, dimension (LDB, N)
072: *          On entry, the Hermitian matrix B.  If UPLO = 'U', the
073: *          leading N-by-N upper triangular part of B contains the
074: *          upper triangular part of the matrix B.  If UPLO = 'L',
075: *          the leading N-by-N lower triangular part of B contains
076: *          the lower triangular part of the matrix B.
077: *
078: *          On exit, if INFO <= N, the part of B containing the matrix is
079: *          overwritten by the triangular factor U or L from the Cholesky
080: *          factorization B = U**H*U or B = L*L**H.
081: *
082: *  LDB     (input) INTEGER
083: *          The leading dimension of the array B.  LDB >= max(1,N).
084: *
085: *  VL      (input) REAL
086: *  VU      (input) REAL
087: *          If RANGE='V', the lower and upper bounds of the interval to
088: *          be searched for eigenvalues. VL < VU.
089: *          Not referenced if RANGE = 'A' or 'I'.
090: *
091: *  IL      (input) INTEGER
092: *  IU      (input) INTEGER
093: *          If RANGE='I', the indices (in ascending order) of the
094: *          smallest and largest eigenvalues to be returned.
095: *          1 <= IL <= IU <= N, if N > 0; IL = 1 and IU = 0 if N = 0.
096: *          Not referenced if RANGE = 'A' or 'V'.
097: *
098: *  ABSTOL  (input) REAL
099: *          The absolute error tolerance for the eigenvalues.
100: *          An approximate eigenvalue is accepted as converged
101: *          when it is determined to lie in an interval [a,b]
102: *          of width less than or equal to
103: *
104: *                  ABSTOL + EPS *   max( |a|,|b| ) ,
105: *
106: *          where EPS is the machine precision.  If ABSTOL is less than
107: *          or equal to zero, then  EPS*|T|  will be used in its place,
108: *          where |T| is the 1-norm of the tridiagonal matrix obtained
109: *          by reducing A to tridiagonal form.
110: *
111: *          Eigenvalues will be computed most accurately when ABSTOL is
112: *          set to twice the underflow threshold 2*SLAMCH('S'), not zero.
113: *          If this routine returns with INFO>0, indicating that some
114: *          eigenvectors did not converge, try setting ABSTOL to
115: *          2*SLAMCH('S').
116: *
117: *  M       (output) INTEGER
118: *          The total number of eigenvalues found.  0 <= M <= N.
119: *          If RANGE = 'A', M = N, and if RANGE = 'I', M = IU-IL+1.
120: *
121: *  W       (output) REAL array, dimension (N)
122: *          The first M elements contain the selected
123: *          eigenvalues in ascending order.
124: *
125: *  Z       (output) COMPLEX array, dimension (LDZ, max(1,M))
126: *          If JOBZ = 'N', then Z is not referenced.
127: *          If JOBZ = 'V', then if INFO = 0, the first M columns of Z
128: *          contain the orthonormal eigenvectors of the matrix A
129: *          corresponding to the selected eigenvalues, with the i-th
130: *          column of Z holding the eigenvector associated with W(i).
131: *          The eigenvectors are normalized as follows:
132: *          if ITYPE = 1 or 2, Z**T*B*Z = I;
133: *          if ITYPE = 3, Z**T*inv(B)*Z = I.
134: *
135: *          If an eigenvector fails to converge, then that column of Z
136: *          contains the latest approximation to the eigenvector, and the
137: *          index of the eigenvector is returned in IFAIL.
138: *          Note: the user must ensure that at least max(1,M) columns are
139: *          supplied in the array Z; if RANGE = 'V', the exact value of M
140: *          is not known in advance and an upper bound must be used.
141: *
142: *  LDZ     (input) INTEGER
143: *          The leading dimension of the array Z.  LDZ >= 1, and if
144: *          JOBZ = 'V', LDZ >= max(1,N).
145: *
146: *  WORK    (workspace/output) COMPLEX array, dimension (MAX(1,LWORK))
147: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
148: *
149: *  LWORK   (input) INTEGER
150: *          The length of the array WORK.  LWORK >= max(1,2*N).
151: *          For optimal efficiency, LWORK >= (NB+1)*N,
152: *          where NB is the blocksize for CHETRD returned by ILAENV.
153: *
154: *          If LWORK = -1, then a workspace query is assumed; the routine
155: *          only calculates the optimal size of the WORK array, returns
156: *          this value as the first entry of the WORK array, and no error
157: *          message related to LWORK is issued by XERBLA.
158: *
159: *  RWORK   (workspace) REAL array, dimension (7*N)
160: *
161: *  IWORK   (workspace) INTEGER array, dimension (5*N)
162: *
163: *  IFAIL   (output) INTEGER array, dimension (N)
164: *          If JOBZ = 'V', then if INFO = 0, the first M elements of
165: *          IFAIL are zero.  If INFO > 0, then IFAIL contains the
166: *          indices of the eigenvectors that failed to converge.
167: *          If JOBZ = 'N', then IFAIL is not referenced.
168: *
169: *  INFO    (output) INTEGER
170: *          = 0:  successful exit
171: *          < 0:  if INFO = -i, the i-th argument had an illegal value
172: *          > 0:  CPOTRF or CHEEVX returned an error code:
173: *             <= N:  if INFO = i, CHEEVX failed to converge;
174: *                    i eigenvectors failed to converge.  Their indices
175: *                    are stored in array IFAIL.
176: *             > N:   if INFO = N + i, for 1 <= i <= N, then the leading
177: *                    minor of order i of B is not positive definite.
178: *                    The factorization of B could not be completed and
179: *                    no eigenvalues or eigenvectors were computed.
180: *
181: *  Further Details
182: *  ===============
183: *
184: *  Based on contributions by
185: *     Mark Fahey, Department of Mathematics, Univ. of Kentucky, USA
186: *
187: *  =====================================================================
188: *
189: *     .. Parameters ..
190:       COMPLEX            CONE
191:       PARAMETER          ( CONE = ( 1.0E+0, 0.0E+0 ) )
192: *     ..
193: *     .. Local Scalars ..
194:       LOGICAL            ALLEIG, INDEIG, LQUERY, UPPER, VALEIG, WANTZ
195:       CHARACTER          TRANS
196:       INTEGER            LWKOPT, NB
197: *     ..
198: *     .. External Functions ..
199:       LOGICAL            LSAME
200:       INTEGER            ILAENV
201:       EXTERNAL           ILAENV, LSAME
202: *     ..
203: *     .. External Subroutines ..
204:       EXTERNAL           CHEEVX, CHEGST, CPOTRF, CTRMM, CTRSM, XERBLA
205: *     ..
206: *     .. Intrinsic Functions ..
207:       INTRINSIC          MAX, MIN
208: *     ..
209: *     .. Executable Statements ..
210: *
211: *     Test the input parameters.
212: *
213:       WANTZ = LSAME( JOBZ, 'V' )
214:       UPPER = LSAME( UPLO, 'U' )
215:       ALLEIG = LSAME( RANGE, 'A' )
216:       VALEIG = LSAME( RANGE, 'V' )
217:       INDEIG = LSAME( RANGE, 'I' )
218:       LQUERY = ( LWORK.EQ.-1 )
219: *
220:       INFO = 0
221:       IF( ITYPE.LT.1 .OR. ITYPE.GT.3 ) THEN
222:          INFO = -1
223:       ELSE IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
224:          INFO = -2
225:       ELSE IF( .NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
226:          INFO = -3
227:       ELSE IF( .NOT.( UPPER .OR. LSAME( UPLO, 'L' ) ) ) THEN
228:          INFO = -4
229:       ELSE IF( N.LT.0 ) THEN
230:          INFO = -5
231:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
232:          INFO = -7
233:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
234:          INFO = -9
235:       ELSE
236:          IF( VALEIG ) THEN
237:             IF( N.GT.0 .AND. VU.LE.VL )
238:      $         INFO = -11
239:          ELSE IF( INDEIG ) THEN
240:             IF( IL.LT.1 .OR. IL.GT.MAX( 1, N ) ) THEN
241:                INFO = -12
242:             ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
243:                INFO = -13
244:             END IF
245:          END IF
246:       END IF
247:       IF (INFO.EQ.0) THEN
248:          IF (LDZ.LT.1 .OR. (WANTZ .AND. LDZ.LT.N)) THEN
249:             INFO = -18
250:          END IF
251:       END IF
252: *
253:       IF( INFO.EQ.0 ) THEN
254:          NB = ILAENV( 1, 'CHETRD', UPLO, N, -1, -1, -1 )
255:          LWKOPT = MAX( 1, ( NB + 1 )*N )
256:          WORK( 1 ) = LWKOPT
257: *
258:          IF( LWORK.LT.MAX( 1, 2*N ) .AND. .NOT.LQUERY ) THEN
259:             INFO = -20
260:          END IF
261:       END IF
262: *
263:       IF( INFO.NE.0 ) THEN
264:          CALL XERBLA( 'CHEGVX', -INFO )
265:          RETURN
266:       ELSE IF( LQUERY ) THEN
267:          RETURN
268:       END IF
269: *
270: *     Quick return if possible
271: *
272:       M = 0
273:       IF( N.EQ.0 ) THEN
274:          RETURN
275:       END IF
276: *
277: *     Form a Cholesky factorization of B.
278: *
279:       CALL CPOTRF( UPLO, N, B, LDB, INFO )
280:       IF( INFO.NE.0 ) THEN
281:          INFO = N + INFO
282:          RETURN
283:       END IF
284: *
285: *     Transform problem to standard eigenvalue problem and solve.
286: *
287:       CALL CHEGST( ITYPE, UPLO, N, A, LDA, B, LDB, INFO )
288:       CALL CHEEVX( JOBZ, RANGE, UPLO, N, A, LDA, VL, VU, IL, IU, ABSTOL,
289:      $             M, W, Z, LDZ, WORK, LWORK, RWORK, IWORK, IFAIL,
290:      $             INFO )
291: *
292:       IF( WANTZ ) THEN
293: *
294: *        Backtransform eigenvectors to the original problem.
295: *
296:          IF( INFO.GT.0 )
297:      $      M = INFO - 1
298:          IF( ITYPE.EQ.1 .OR. ITYPE.EQ.2 ) THEN
299: *
300: *           For A*x=(lambda)*B*x and A*B*x=(lambda)*x;
301: *           backtransform eigenvectors: x = inv(L)'*y or inv(U)*y
302: *
303:             IF( UPPER ) THEN
304:                TRANS = 'N'
305:             ELSE
306:                TRANS = 'C'
307:             END IF
308: *
309:             CALL CTRSM( 'Left', UPLO, TRANS, 'Non-unit', N, M, CONE, B,
310:      $                  LDB, Z, LDZ )
311: *
312:          ELSE IF( ITYPE.EQ.3 ) THEN
313: *
314: *           For B*A*x=(lambda)*x;
315: *           backtransform eigenvectors: x = L*y or U'*y
316: *
317:             IF( UPPER ) THEN
318:                TRANS = 'C'
319:             ELSE
320:                TRANS = 'N'
321:             END IF
322: *
323:             CALL CTRMM( 'Left', UPLO, TRANS, 'Non-unit', N, M, CONE, B,
324:      $                  LDB, Z, LDZ )
325:          END IF
326:       END IF
327: *
328: *     Set WORK(1) to optimal complex workspace size.
329: *
330:       WORK( 1 ) = LWKOPT
331: *
332:       RETURN
333: *
334: *     End of CHEGVX
335: *
336:       END
337: