```001:       SUBROUTINE CHPGVD( ITYPE, JOBZ, UPLO, N, AP, BP, W, Z, LDZ, WORK,
002:      \$                   LWORK, RWORK, LRWORK, IWORK, LIWORK, 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          JOBZ, UPLO
010:       INTEGER            INFO, ITYPE, LDZ, LIWORK, LRWORK, LWORK, N
011: *     ..
012: *     .. Array Arguments ..
013:       INTEGER            IWORK( * )
014:       REAL               RWORK( * ), W( * )
015:       COMPLEX            AP( * ), BP( * ), WORK( * ), Z( LDZ, * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  CHPGVD 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.  Here A and
024: *  B are assumed to be Hermitian, stored in packed format, and B is also
025: *  positive definite.
026: *  If eigenvectors are desired, it uses a divide and conquer algorithm.
027: *
028: *  The divide and conquer algorithm makes very mild assumptions about
029: *  floating point arithmetic. It will work on machines with a guard
030: *  digit in add/subtract, or on those binary machines without guard
031: *  digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or
032: *  Cray-2. It could conceivably fail on hexadecimal or decimal machines
033: *  without guard digits, but we know of none.
034: *
035: *  Arguments
036: *  =========
037: *
038: *  ITYPE   (input) INTEGER
039: *          Specifies the problem type to be solved:
040: *          = 1:  A*x = (lambda)*B*x
041: *          = 2:  A*B*x = (lambda)*x
042: *          = 3:  B*A*x = (lambda)*x
043: *
044: *  JOBZ    (input) CHARACTER*1
045: *          = 'N':  Compute eigenvalues only;
046: *          = 'V':  Compute eigenvalues and eigenvectors.
047: *
048: *  UPLO    (input) CHARACTER*1
049: *          = 'U':  Upper triangles of A and B are stored;
050: *          = 'L':  Lower triangles of A and B are stored.
051: *
052: *  N       (input) INTEGER
053: *          The order of the matrices A and B.  N >= 0.
054: *
055: *  AP      (input/output) COMPLEX array, dimension (N*(N+1)/2)
056: *          On entry, the upper or lower triangle of the Hermitian matrix
057: *          A, packed columnwise in a linear array.  The j-th column of A
058: *          is stored in the array AP as follows:
059: *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
060: *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
061: *
062: *          On exit, the contents of AP are destroyed.
063: *
064: *  BP      (input/output) COMPLEX array, dimension (N*(N+1)/2)
065: *          On entry, the upper or lower triangle of the Hermitian matrix
066: *          B, packed columnwise in a linear array.  The j-th column of B
067: *          is stored in the array BP as follows:
068: *          if UPLO = 'U', BP(i + (j-1)*j/2) = B(i,j) for 1<=i<=j;
069: *          if UPLO = 'L', BP(i + (j-1)*(2*n-j)/2) = B(i,j) for j<=i<=n.
070: *
071: *          On exit, the triangular factor U or L from the Cholesky
072: *          factorization B = U**H*U or B = L*L**H, in the same storage
073: *          format as B.
074: *
075: *  W       (output) REAL array, dimension (N)
076: *          If INFO = 0, the eigenvalues in ascending order.
077: *
078: *  Z       (output) COMPLEX array, dimension (LDZ, N)
079: *          If JOBZ = 'V', then if INFO = 0, Z contains the matrix Z of
080: *          eigenvectors.  The eigenvectors are normalized as follows:
081: *          if ITYPE = 1 or 2, Z**H*B*Z = I;
082: *          if ITYPE = 3, Z**H*inv(B)*Z = I.
083: *          If JOBZ = 'N', then Z is not referenced.
084: *
085: *  LDZ     (input) INTEGER
086: *          The leading dimension of the array Z.  LDZ >= 1, and if
087: *          JOBZ = 'V', LDZ >= max(1,N).
088: *
089: *  WORK    (workspace) COMPLEX array, dimension (MAX(1,LWORK))
090: *          On exit, if INFO = 0, WORK(1) returns the required LWORK.
091: *
092: *  LWORK   (input) INTEGER
093: *          The dimension of array WORK.
094: *          If N <= 1,               LWORK >= 1.
095: *          If JOBZ = 'N' and N > 1, LWORK >= N.
096: *          If JOBZ = 'V' and N > 1, LWORK >= 2*N.
097: *
098: *          If LWORK = -1, then a workspace query is assumed; the routine
099: *          only calculates the required sizes of the WORK, RWORK and
100: *          IWORK arrays, returns these values as the first entries of
101: *          the WORK, RWORK and IWORK arrays, and no error message
102: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
103: *
104: *  RWORK   (workspace) REAL array, dimension (MAX(1,LRWORK))
105: *          On exit, if INFO = 0, RWORK(1) returns the required LRWORK.
106: *
107: *  LRWORK  (input) INTEGER
108: *          The dimension of array RWORK.
109: *          If N <= 1,               LRWORK >= 1.
110: *          If JOBZ = 'N' and N > 1, LRWORK >= N.
111: *          If JOBZ = 'V' and N > 1, LRWORK >= 1 + 5*N + 2*N**2.
112: *
113: *          If LRWORK = -1, then a workspace query is assumed; the
114: *          routine only calculates the required sizes of the WORK, RWORK
115: *          and IWORK arrays, returns these values as the first entries
116: *          of the WORK, RWORK and IWORK arrays, and no error message
117: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
118: *
119: *  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK))
120: *          On exit, if INFO = 0, IWORK(1) returns the required LIWORK.
121: *
122: *  LIWORK  (input) INTEGER
123: *          The dimension of array IWORK.
124: *          If JOBZ  = 'N' or N <= 1, LIWORK >= 1.
125: *          If JOBZ  = 'V' and N > 1, LIWORK >= 3 + 5*N.
126: *
127: *          If LIWORK = -1, then a workspace query is assumed; the
128: *          routine only calculates the required sizes of the WORK, RWORK
129: *          and IWORK arrays, returns these values as the first entries
130: *          of the WORK, RWORK and IWORK arrays, and no error message
131: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
132: *
133: *  INFO    (output) INTEGER
134: *          = 0:  successful exit
135: *          < 0:  if INFO = -i, the i-th argument had an illegal value
136: *          > 0:  CPPTRF or CHPEVD returned an error code:
137: *             <= N:  if INFO = i, CHPEVD failed to converge;
138: *                    i off-diagonal elements of an intermediate
139: *                    tridiagonal form did not convergeto zero;
140: *             > N:   if INFO = N + i, for 1 <= i <= n, then the leading
141: *                    minor of order i of B is not positive definite.
142: *                    The factorization of B could not be completed and
143: *                    no eigenvalues or eigenvectors were computed.
144: *
145: *  Further Details
146: *  ===============
147: *
148: *  Based on contributions by
149: *     Mark Fahey, Department of Mathematics, Univ. of Kentucky, USA
150: *
151: *  =====================================================================
152: *
153: *     .. Local Scalars ..
154:       LOGICAL            LQUERY, UPPER, WANTZ
155:       CHARACTER          TRANS
156:       INTEGER            J, LIWMIN, LRWMIN, LWMIN, NEIG
157: *     ..
158: *     .. External Functions ..
159:       LOGICAL            LSAME
160:       EXTERNAL           LSAME
161: *     ..
162: *     .. External Subroutines ..
163:       EXTERNAL           CHPEVD, CHPGST, CPPTRF, CTPMV, CTPSV, XERBLA
164: *     ..
165: *     .. Intrinsic Functions ..
166:       INTRINSIC          MAX, REAL
167: *     ..
168: *     .. Executable Statements ..
169: *
170: *     Test the input parameters.
171: *
172:       WANTZ = LSAME( JOBZ, 'V' )
173:       UPPER = LSAME( UPLO, 'U' )
174:       LQUERY = ( LWORK.EQ.-1 .OR. LRWORK.EQ.-1 .OR. LIWORK.EQ.-1 )
175: *
176:       INFO = 0
177:       IF( ITYPE.LT.1 .OR. ITYPE.GT.3 ) THEN
178:          INFO = -1
179:       ELSE IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
180:          INFO = -2
181:       ELSE IF( .NOT.( UPPER .OR. LSAME( UPLO, 'L' ) ) ) THEN
182:          INFO = -3
183:       ELSE IF( N.LT.0 ) THEN
184:          INFO = -4
185:       ELSE IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
186:          INFO = -9
187:       END IF
188: *
189:       IF( INFO.EQ.0 ) THEN
190:          IF( N.LE.1 ) THEN
191:             LWMIN = 1
192:             LIWMIN = 1
193:             LRWMIN = 1
194:          ELSE
195:             IF( WANTZ ) THEN
196:                LWMIN = 2*N
197:                LRWMIN = 1 + 5*N + 2*N**2
198:                LIWMIN = 3 + 5*N
199:             ELSE
200:                LWMIN = N
201:                LRWMIN = N
202:                LIWMIN = 1
203:             END IF
204:          END IF
205:          WORK( 1 ) = LWMIN
206:          RWORK( 1 ) = LRWMIN
207:          IWORK( 1 ) = LIWMIN
208: *
209:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
210:             INFO = -11
211:          ELSE IF( LRWORK.LT.LRWMIN .AND. .NOT.LQUERY ) THEN
212:             INFO = -13
213:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
214:             INFO = -15
215:          END IF
216:       END IF
217: *
218:       IF( INFO.NE.0 ) THEN
219:          CALL XERBLA( 'CHPGVD', -INFO )
220:          RETURN
221:       ELSE IF( LQUERY ) THEN
222:          RETURN
223:       END IF
224: *
225: *     Quick return if possible
226: *
227:       IF( N.EQ.0 )
228:      \$   RETURN
229: *
230: *     Form a Cholesky factorization of B.
231: *
232:       CALL CPPTRF( UPLO, N, BP, INFO )
233:       IF( INFO.NE.0 ) THEN
234:          INFO = N + INFO
235:          RETURN
236:       END IF
237: *
238: *     Transform problem to standard eigenvalue problem and solve.
239: *
240:       CALL CHPGST( ITYPE, UPLO, N, AP, BP, INFO )
241:       CALL CHPEVD( JOBZ, UPLO, N, AP, W, Z, LDZ, WORK, LWORK, RWORK,
242:      \$             LRWORK, IWORK, LIWORK, INFO )
243:       LWMIN = MAX( REAL( LWMIN ), REAL( WORK( 1 ) ) )
244:       LRWMIN = MAX( REAL( LRWMIN ), REAL( RWORK( 1 ) ) )
245:       LIWMIN = MAX( REAL( LIWMIN ), REAL( IWORK( 1 ) ) )
246: *
247:       IF( WANTZ ) THEN
248: *
249: *        Backtransform eigenvectors to the original problem.
250: *
251:          NEIG = N
252:          IF( INFO.GT.0 )
253:      \$      NEIG = INFO - 1
254:          IF( ITYPE.EQ.1 .OR. ITYPE.EQ.2 ) THEN
255: *
256: *           For A*x=(lambda)*B*x and A*B*x=(lambda)*x;
257: *           backtransform eigenvectors: x = inv(L)'*y or inv(U)*y
258: *
259:             IF( UPPER ) THEN
260:                TRANS = 'N'
261:             ELSE
262:                TRANS = 'C'
263:             END IF
264: *
265:             DO 10 J = 1, NEIG
266:                CALL CTPSV( UPLO, TRANS, 'Non-unit', N, BP, Z( 1, J ),
267:      \$                     1 )
268:    10       CONTINUE
269: *
270:          ELSE IF( ITYPE.EQ.3 ) THEN
271: *
272: *           For B*A*x=(lambda)*x;
273: *           backtransform eigenvectors: x = L*y or U'*y
274: *
275:             IF( UPPER ) THEN
276:                TRANS = 'C'
277:             ELSE
278:                TRANS = 'N'
279:             END IF
280: *
281:             DO 20 J = 1, NEIG
282:                CALL CTPMV( UPLO, TRANS, 'Non-unit', N, BP, Z( 1, J ),
283:      \$                     1 )
284:    20       CONTINUE
285:          END IF
286:       END IF
287: *
288:       WORK( 1 ) = LWMIN
289:       RWORK( 1 ) = LRWMIN
290:       IWORK( 1 ) = LIWMIN
291:       RETURN
292: *
293: *     End of CHPGVD
294: *
295:       END
296: ```