```001:       SUBROUTINE SGEGS( JOBVSL, JOBVSR, N, A, LDA, B, LDB, ALPHAR,
002:      \$                  ALPHAI, BETA, VSL, LDVSL, VSR, LDVSR, WORK,
003:      \$                  LWORK, 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          JOBVSL, JOBVSR
011:       INTEGER            INFO, LDA, LDB, LDVSL, LDVSR, LWORK, N
012: *     ..
013: *     .. Array Arguments ..
014:       REAL               A( LDA, * ), ALPHAI( * ), ALPHAR( * ),
015:      \$                   B( LDB, * ), BETA( * ), VSL( LDVSL, * ),
016:      \$                   VSR( LDVSR, * ), WORK( * )
017: *     ..
018: *
019: *  Purpose
020: *  =======
021: *
022: *  This routine is deprecated and has been replaced by routine SGGES.
023: *
024: *  SGEGS computes the eigenvalues, real Schur form, and, optionally,
025: *  left and or/right Schur vectors of a real matrix pair (A,B).
026: *  Given two square matrices A and B, the generalized real Schur
027: *  factorization has the form
028: *
029: *    A = Q*S*Z**T,  B = Q*T*Z**T
030: *
031: *  where Q and Z are orthogonal matrices, T is upper triangular, and S
032: *  is an upper quasi-triangular matrix with 1-by-1 and 2-by-2 diagonal
033: *  blocks, the 2-by-2 blocks corresponding to complex conjugate pairs
034: *  of eigenvalues of (A,B).  The columns of Q are the left Schur vectors
035: *  and the columns of Z are the right Schur vectors.
036: *
037: *  If only the eigenvalues of (A,B) are needed, the driver routine
038: *  SGEGV should be used instead.  See SGEGV for a description of the
039: *  eigenvalues of the generalized nonsymmetric eigenvalue problem
040: *  (GNEP).
041: *
042: *  Arguments
043: *  =========
044: *
045: *  JOBVSL  (input) CHARACTER*1
046: *          = 'N':  do not compute the left Schur vectors;
047: *          = 'V':  compute the left Schur vectors (returned in VSL).
048: *
049: *  JOBVSR  (input) CHARACTER*1
050: *          = 'N':  do not compute the right Schur vectors;
051: *          = 'V':  compute the right Schur vectors (returned in VSR).
052: *
053: *  N       (input) INTEGER
054: *          The order of the matrices A, B, VSL, and VSR.  N >= 0.
055: *
056: *  A       (input/output) REAL array, dimension (LDA, N)
057: *          On entry, the matrix A.
058: *          On exit, the upper quasi-triangular matrix S from the
059: *          generalized real Schur factorization.
060: *
061: *  LDA     (input) INTEGER
062: *          The leading dimension of A.  LDA >= max(1,N).
063: *
064: *  B       (input/output) REAL array, dimension (LDB, N)
065: *          On entry, the matrix B.
066: *          On exit, the upper triangular matrix T from the generalized
067: *          real Schur factorization.
068: *
069: *  LDB     (input) INTEGER
070: *          The leading dimension of B.  LDB >= max(1,N).
071: *
072: *  ALPHAR  (output) REAL array, dimension (N)
073: *          The real parts of each scalar alpha defining an eigenvalue
074: *          of GNEP.
075: *
076: *  ALPHAI  (output) REAL array, dimension (N)
077: *          The imaginary parts of each scalar alpha defining an
078: *          eigenvalue of GNEP.  If ALPHAI(j) is zero, then the j-th
079: *          eigenvalue is real; if positive, then the j-th and (j+1)-st
080: *          eigenvalues are a complex conjugate pair, with
081: *          ALPHAI(j+1) = -ALPHAI(j).
082: *
083: *  BETA    (output) REAL array, dimension (N)
084: *          The scalars beta that define the eigenvalues of GNEP.
085: *          Together, the quantities alpha = (ALPHAR(j),ALPHAI(j)) and
086: *          beta = BETA(j) represent the j-th eigenvalue of the matrix
087: *          pair (A,B), in one of the forms lambda = alpha/beta or
088: *          mu = beta/alpha.  Since either lambda or mu may overflow,
089: *          they should not, in general, be computed.
090: *
091: *  VSL     (output) REAL array, dimension (LDVSL,N)
092: *          If JOBVSL = 'V', the matrix of left Schur vectors Q.
093: *          Not referenced if JOBVSL = 'N'.
094: *
095: *  LDVSL   (input) INTEGER
096: *          The leading dimension of the matrix VSL. LDVSL >=1, and
097: *          if JOBVSL = 'V', LDVSL >= N.
098: *
099: *  VSR     (output) REAL array, dimension (LDVSR,N)
100: *          If JOBVSR = 'V', the matrix of right Schur vectors Z.
101: *          Not referenced if JOBVSR = 'N'.
102: *
103: *  LDVSR   (input) INTEGER
104: *          The leading dimension of the matrix VSR. LDVSR >= 1, and
105: *          if JOBVSR = 'V', LDVSR >= N.
106: *
107: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
108: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
109: *
110: *  LWORK   (input) INTEGER
111: *          The dimension of the array WORK.  LWORK >= max(1,4*N).
112: *          For good performance, LWORK must generally be larger.
113: *          To compute the optimal value of LWORK, call ILAENV to get
114: *          blocksizes (for SGEQRF, SORMQR, and SORGQR.)  Then compute:
115: *          NB  -- MAX of the blocksizes for SGEQRF, SORMQR, and SORGQR
116: *          The optimal LWORK is  2*N + N*(NB+1).
117: *
118: *          If LWORK = -1, then a workspace query is assumed; the routine
119: *          only calculates the optimal size of the WORK array, returns
120: *          this value as the first entry of the WORK array, and no error
121: *          message related to LWORK is issued by XERBLA.
122: *
123: *  INFO    (output) INTEGER
124: *          = 0:  successful exit
125: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
126: *          = 1,...,N:
127: *                The QZ iteration failed.  (A,B) are not in Schur
128: *                form, but ALPHAR(j), ALPHAI(j), and BETA(j) should
129: *                be correct for j=INFO+1,...,N.
130: *          > N:  errors that usually indicate LAPACK problems:
131: *                =N+1: error return from SGGBAL
132: *                =N+2: error return from SGEQRF
133: *                =N+3: error return from SORMQR
134: *                =N+4: error return from SORGQR
135: *                =N+5: error return from SGGHRD
136: *                =N+6: error return from SHGEQZ (other than failed
137: *                                                iteration)
138: *                =N+7: error return from SGGBAK (computing VSL)
139: *                =N+8: error return from SGGBAK (computing VSR)
140: *                =N+9: error return from SLASCL (various places)
141: *
142: *  =====================================================================
143: *
144: *     .. Parameters ..
145:       REAL               ZERO, ONE
146:       PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0 )
147: *     ..
148: *     .. Local Scalars ..
149:       LOGICAL            ILASCL, ILBSCL, ILVSL, ILVSR, LQUERY
150:       INTEGER            ICOLS, IHI, IINFO, IJOBVL, IJOBVR, ILEFT,
151:      \$                   ILO, IRIGHT, IROWS, ITAU, IWORK, LOPT, LWKMIN,
152:      \$                   LWKOPT, NB, NB1, NB2, NB3
153:       REAL               ANRM, ANRMTO, BIGNUM, BNRM, BNRMTO, EPS,
154:      \$                   SAFMIN, SMLNUM
155: *     ..
156: *     .. External Subroutines ..
157:       EXTERNAL           SGEQRF, SGGBAK, SGGBAL, SGGHRD, SHGEQZ, SLACPY,
158:      \$                   SLASCL, SLASET, SORGQR, SORMQR, XERBLA
159: *     ..
160: *     .. External Functions ..
161:       LOGICAL            LSAME
162:       INTEGER            ILAENV
163:       REAL               SLAMCH, SLANGE
164:       EXTERNAL           ILAENV, LSAME, SLAMCH, SLANGE
165: *     ..
166: *     .. Intrinsic Functions ..
167:       INTRINSIC          INT, MAX
168: *     ..
169: *     .. Executable Statements ..
170: *
171: *     Decode the input arguments
172: *
173:       IF( LSAME( JOBVSL, 'N' ) ) THEN
174:          IJOBVL = 1
175:          ILVSL = .FALSE.
176:       ELSE IF( LSAME( JOBVSL, 'V' ) ) THEN
177:          IJOBVL = 2
178:          ILVSL = .TRUE.
179:       ELSE
180:          IJOBVL = -1
181:          ILVSL = .FALSE.
182:       END IF
183: *
184:       IF( LSAME( JOBVSR, 'N' ) ) THEN
185:          IJOBVR = 1
186:          ILVSR = .FALSE.
187:       ELSE IF( LSAME( JOBVSR, 'V' ) ) THEN
188:          IJOBVR = 2
189:          ILVSR = .TRUE.
190:       ELSE
191:          IJOBVR = -1
192:          ILVSR = .FALSE.
193:       END IF
194: *
195: *     Test the input arguments
196: *
197:       LWKMIN = MAX( 4*N, 1 )
198:       LWKOPT = LWKMIN
199:       WORK( 1 ) = LWKOPT
200:       LQUERY = ( LWORK.EQ.-1 )
201:       INFO = 0
202:       IF( IJOBVL.LE.0 ) THEN
203:          INFO = -1
204:       ELSE IF( IJOBVR.LE.0 ) THEN
205:          INFO = -2
206:       ELSE IF( N.LT.0 ) THEN
207:          INFO = -3
208:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
209:          INFO = -5
210:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
211:          INFO = -7
212:       ELSE IF( LDVSL.LT.1 .OR. ( ILVSL .AND. LDVSL.LT.N ) ) THEN
213:          INFO = -12
214:       ELSE IF( LDVSR.LT.1 .OR. ( ILVSR .AND. LDVSR.LT.N ) ) THEN
215:          INFO = -14
216:       ELSE IF( LWORK.LT.LWKMIN .AND. .NOT.LQUERY ) THEN
217:          INFO = -16
218:       END IF
219: *
220:       IF( INFO.EQ.0 ) THEN
221:          NB1 = ILAENV( 1, 'SGEQRF', ' ', N, N, -1, -1 )
222:          NB2 = ILAENV( 1, 'SORMQR', ' ', N, N, N, -1 )
223:          NB3 = ILAENV( 1, 'SORGQR', ' ', N, N, N, -1 )
224:          NB = MAX( NB1, NB2, NB3 )
225:          LOPT = 2*N+N*(NB+1)
226:          WORK( 1 ) = LOPT
227:       END IF
228: *
229:       IF( INFO.NE.0 ) THEN
230:          CALL XERBLA( 'SGEGS ', -INFO )
231:          RETURN
232:       ELSE IF( LQUERY ) THEN
233:          RETURN
234:       END IF
235: *
236: *     Quick return if possible
237: *
238:       IF( N.EQ.0 )
239:      \$   RETURN
240: *
241: *     Get machine constants
242: *
243:       EPS = SLAMCH( 'E' )*SLAMCH( 'B' )
244:       SAFMIN = SLAMCH( 'S' )
245:       SMLNUM = N*SAFMIN / EPS
246:       BIGNUM = ONE / SMLNUM
247: *
248: *     Scale A if max element outside range [SMLNUM,BIGNUM]
249: *
250:       ANRM = SLANGE( 'M', N, N, A, LDA, WORK )
251:       ILASCL = .FALSE.
252:       IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
253:          ANRMTO = SMLNUM
254:          ILASCL = .TRUE.
255:       ELSE IF( ANRM.GT.BIGNUM ) THEN
256:          ANRMTO = BIGNUM
257:          ILASCL = .TRUE.
258:       END IF
259: *
260:       IF( ILASCL ) THEN
261:          CALL SLASCL( 'G', -1, -1, ANRM, ANRMTO, N, N, A, LDA, IINFO )
262:          IF( IINFO.NE.0 ) THEN
263:             INFO = N + 9
264:             RETURN
265:          END IF
266:       END IF
267: *
268: *     Scale B if max element outside range [SMLNUM,BIGNUM]
269: *
270:       BNRM = SLANGE( 'M', N, N, B, LDB, WORK )
271:       ILBSCL = .FALSE.
272:       IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
273:          BNRMTO = SMLNUM
274:          ILBSCL = .TRUE.
275:       ELSE IF( BNRM.GT.BIGNUM ) THEN
276:          BNRMTO = BIGNUM
277:          ILBSCL = .TRUE.
278:       END IF
279: *
280:       IF( ILBSCL ) THEN
281:          CALL SLASCL( 'G', -1, -1, BNRM, BNRMTO, N, N, B, LDB, IINFO )
282:          IF( IINFO.NE.0 ) THEN
283:             INFO = N + 9
284:             RETURN
285:          END IF
286:       END IF
287: *
288: *     Permute the matrix to make it more nearly triangular
289: *     Workspace layout:  (2*N words -- "work..." not actually used)
290: *        left_permutation, right_permutation, work...
291: *
292:       ILEFT = 1
293:       IRIGHT = N + 1
294:       IWORK = IRIGHT + N
295:       CALL SGGBAL( 'P', N, A, LDA, B, LDB, ILO, IHI, WORK( ILEFT ),
296:      \$             WORK( IRIGHT ), WORK( IWORK ), IINFO )
297:       IF( IINFO.NE.0 ) THEN
298:          INFO = N + 1
299:          GO TO 10
300:       END IF
301: *
302: *     Reduce B to triangular form, and initialize VSL and/or VSR
303: *     Workspace layout:  ("work..." must have at least N words)
304: *        left_permutation, right_permutation, tau, work...
305: *
306:       IROWS = IHI + 1 - ILO
307:       ICOLS = N + 1 - ILO
308:       ITAU = IWORK
309:       IWORK = ITAU + IROWS
310:       CALL SGEQRF( IROWS, ICOLS, B( ILO, ILO ), LDB, WORK( ITAU ),
311:      \$             WORK( IWORK ), LWORK+1-IWORK, IINFO )
312:       IF( IINFO.GE.0 )
313:      \$   LWKOPT = MAX( LWKOPT, INT( WORK( IWORK ) )+IWORK-1 )
314:       IF( IINFO.NE.0 ) THEN
315:          INFO = N + 2
316:          GO TO 10
317:       END IF
318: *
319:       CALL SORMQR( 'L', 'T', IROWS, ICOLS, IROWS, B( ILO, ILO ), LDB,
320:      \$             WORK( ITAU ), A( ILO, ILO ), LDA, WORK( IWORK ),
321:      \$             LWORK+1-IWORK, IINFO )
322:       IF( IINFO.GE.0 )
323:      \$   LWKOPT = MAX( LWKOPT, INT( WORK( IWORK ) )+IWORK-1 )
324:       IF( IINFO.NE.0 ) THEN
325:          INFO = N + 3
326:          GO TO 10
327:       END IF
328: *
329:       IF( ILVSL ) THEN
330:          CALL SLASET( 'Full', N, N, ZERO, ONE, VSL, LDVSL )
331:          CALL SLACPY( 'L', IROWS-1, IROWS-1, B( ILO+1, ILO ), LDB,
332:      \$                VSL( ILO+1, ILO ), LDVSL )
333:          CALL SORGQR( IROWS, IROWS, IROWS, VSL( ILO, ILO ), LDVSL,
334:      \$                WORK( ITAU ), WORK( IWORK ), LWORK+1-IWORK,
335:      \$                IINFO )
336:          IF( IINFO.GE.0 )
337:      \$      LWKOPT = MAX( LWKOPT, INT( WORK( IWORK ) )+IWORK-1 )
338:          IF( IINFO.NE.0 ) THEN
339:             INFO = N + 4
340:             GO TO 10
341:          END IF
342:       END IF
343: *
344:       IF( ILVSR )
345:      \$   CALL SLASET( 'Full', N, N, ZERO, ONE, VSR, LDVSR )
346: *
347: *     Reduce to generalized Hessenberg form
348: *
349:       CALL SGGHRD( JOBVSL, JOBVSR, N, ILO, IHI, A, LDA, B, LDB, VSL,
350:      \$             LDVSL, VSR, LDVSR, IINFO )
351:       IF( IINFO.NE.0 ) THEN
352:          INFO = N + 5
353:          GO TO 10
354:       END IF
355: *
356: *     Perform QZ algorithm, computing Schur vectors if desired
357: *     Workspace layout:  ("work..." must have at least 1 word)
358: *        left_permutation, right_permutation, work...
359: *
360:       IWORK = ITAU
361:       CALL SHGEQZ( 'S', JOBVSL, JOBVSR, N, ILO, IHI, A, LDA, B, LDB,
362:      \$             ALPHAR, ALPHAI, BETA, VSL, LDVSL, VSR, LDVSR,
363:      \$             WORK( IWORK ), LWORK+1-IWORK, IINFO )
364:       IF( IINFO.GE.0 )
365:      \$   LWKOPT = MAX( LWKOPT, INT( WORK( IWORK ) )+IWORK-1 )
366:       IF( IINFO.NE.0 ) THEN
367:          IF( IINFO.GT.0 .AND. IINFO.LE.N ) THEN
368:             INFO = IINFO
369:          ELSE IF( IINFO.GT.N .AND. IINFO.LE.2*N ) THEN
370:             INFO = IINFO - N
371:          ELSE
372:             INFO = N + 6
373:          END IF
374:          GO TO 10
375:       END IF
376: *
377: *     Apply permutation to VSL and VSR
378: *
379:       IF( ILVSL ) THEN
380:          CALL SGGBAK( 'P', 'L', N, ILO, IHI, WORK( ILEFT ),
381:      \$                WORK( IRIGHT ), N, VSL, LDVSL, IINFO )
382:          IF( IINFO.NE.0 ) THEN
383:             INFO = N + 7
384:             GO TO 10
385:          END IF
386:       END IF
387:       IF( ILVSR ) THEN
388:          CALL SGGBAK( 'P', 'R', N, ILO, IHI, WORK( ILEFT ),
389:      \$                WORK( IRIGHT ), N, VSR, LDVSR, IINFO )
390:          IF( IINFO.NE.0 ) THEN
391:             INFO = N + 8
392:             GO TO 10
393:          END IF
394:       END IF
395: *
396: *     Undo scaling
397: *
398:       IF( ILASCL ) THEN
399:          CALL SLASCL( 'H', -1, -1, ANRMTO, ANRM, N, N, A, LDA, IINFO )
400:          IF( IINFO.NE.0 ) THEN
401:             INFO = N + 9
402:             RETURN
403:          END IF
404:          CALL SLASCL( 'G', -1, -1, ANRMTO, ANRM, N, 1, ALPHAR, N,
405:      \$                IINFO )
406:          IF( IINFO.NE.0 ) THEN
407:             INFO = N + 9
408:             RETURN
409:          END IF
410:          CALL SLASCL( 'G', -1, -1, ANRMTO, ANRM, N, 1, ALPHAI, N,
411:      \$                IINFO )
412:          IF( IINFO.NE.0 ) THEN
413:             INFO = N + 9
414:             RETURN
415:          END IF
416:       END IF
417: *
418:       IF( ILBSCL ) THEN
419:          CALL SLASCL( 'U', -1, -1, BNRMTO, BNRM, N, N, B, LDB, IINFO )
420:          IF( IINFO.NE.0 ) THEN
421:             INFO = N + 9
422:             RETURN
423:          END IF
424:          CALL SLASCL( 'G', -1, -1, BNRMTO, BNRM, N, 1, BETA, N, IINFO )
425:          IF( IINFO.NE.0 ) THEN
426:             INFO = N + 9
427:             RETURN
428:          END IF
429:       END IF
430: *
431:    10 CONTINUE
432:       WORK( 1 ) = LWKOPT
433: *
434:       RETURN
435: *
436: *     End of SGEGS
437: *
438:       END
439: ```