001:       SUBROUTINE SLAG2( A, LDA, B, LDB, SAFMIN, SCALE1, SCALE2, WR1,
002:      $                  WR2, WI )
003: *
004: *  -- LAPACK auxiliary routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       INTEGER            LDA, LDB
010:       REAL               SAFMIN, SCALE1, SCALE2, WI, WR1, WR2
011: *     ..
012: *     .. Array Arguments ..
013:       REAL               A( LDA, * ), B( LDB, * )
014: *     ..
015: *
016: *  Purpose
017: *  =======
018: *
019: *  SLAG2 computes the eigenvalues of a 2 x 2 generalized eigenvalue
020: *  problem  A - w B, with scaling as necessary to avoid over-/underflow.
021: *
022: *  The scaling factor "s" results in a modified eigenvalue equation
023: *
024: *      s A - w B
025: *
026: *  where  s  is a non-negative scaling factor chosen so that  w,  w B,
027: *  and  s A  do not overflow and, if possible, do not underflow, either.
028: *
029: *  Arguments
030: *  =========
031: *
032: *  A       (input) REAL array, dimension (LDA, 2)
033: *          On entry, the 2 x 2 matrix A.  It is assumed that its 1-norm
034: *          is less than 1/SAFMIN.  Entries less than
035: *          sqrt(SAFMIN)*norm(A) are subject to being treated as zero.
036: *
037: *  LDA     (input) INTEGER
038: *          The leading dimension of the array A.  LDA >= 2.
039: *
040: *  B       (input) REAL array, dimension (LDB, 2)
041: *          On entry, the 2 x 2 upper triangular matrix B.  It is
042: *          assumed that the one-norm of B is less than 1/SAFMIN.  The
043: *          diagonals should be at least sqrt(SAFMIN) times the largest
044: *          element of B (in absolute value); if a diagonal is smaller
045: *          than that, then  +/- sqrt(SAFMIN) will be used instead of
046: *          that diagonal.
047: *
048: *  LDB     (input) INTEGER
049: *          The leading dimension of the array B.  LDB >= 2.
050: *
051: *  SAFMIN  (input) REAL
052: *          The smallest positive number s.t. 1/SAFMIN does not
053: *          overflow.  (This should always be SLAMCH('S') -- it is an
054: *          argument in order to avoid having to call SLAMCH frequently.)
055: *
056: *  SCALE1  (output) REAL
057: *          A scaling factor used to avoid over-/underflow in the
058: *          eigenvalue equation which defines the first eigenvalue.  If
059: *          the eigenvalues are complex, then the eigenvalues are
060: *          ( WR1  +/-  WI i ) / SCALE1  (which may lie outside the
061: *          exponent range of the machine), SCALE1=SCALE2, and SCALE1
062: *          will always be positive.  If the eigenvalues are real, then
063: *          the first (real) eigenvalue is  WR1 / SCALE1 , but this may
064: *          overflow or underflow, and in fact, SCALE1 may be zero or
065: *          less than the underflow threshhold if the exact eigenvalue
066: *          is sufficiently large.
067: *
068: *  SCALE2  (output) REAL
069: *          A scaling factor used to avoid over-/underflow in the
070: *          eigenvalue equation which defines the second eigenvalue.  If
071: *          the eigenvalues are complex, then SCALE2=SCALE1.  If the
072: *          eigenvalues are real, then the second (real) eigenvalue is
073: *          WR2 / SCALE2 , but this may overflow or underflow, and in
074: *          fact, SCALE2 may be zero or less than the underflow
075: *          threshhold if the exact eigenvalue is sufficiently large.
076: *
077: *  WR1     (output) REAL
078: *          If the eigenvalue is real, then WR1 is SCALE1 times the
079: *          eigenvalue closest to the (2,2) element of A B**(-1).  If the
080: *          eigenvalue is complex, then WR1=WR2 is SCALE1 times the real
081: *          part of the eigenvalues.
082: *
083: *  WR2     (output) REAL
084: *          If the eigenvalue is real, then WR2 is SCALE2 times the
085: *          other eigenvalue.  If the eigenvalue is complex, then
086: *          WR1=WR2 is SCALE1 times the real part of the eigenvalues.
087: *
088: *  WI      (output) REAL
089: *          If the eigenvalue is real, then WI is zero.  If the
090: *          eigenvalue is complex, then WI is SCALE1 times the imaginary
091: *          part of the eigenvalues.  WI will always be non-negative.
092: *
093: *  =====================================================================
094: *
095: *     .. Parameters ..
096:       REAL               ZERO, ONE, TWO
097:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0, TWO = 2.0E+0 )
098:       REAL               HALF
099:       PARAMETER          ( HALF = ONE / TWO )
100:       REAL               FUZZY1
101:       PARAMETER          ( FUZZY1 = ONE+1.0E-5 )
102: *     ..
103: *     .. Local Scalars ..
104:       REAL               A11, A12, A21, A22, ABI22, ANORM, AS11, AS12,
105:      $                   AS22, ASCALE, B11, B12, B22, BINV11, BINV22,
106:      $                   BMIN, BNORM, BSCALE, BSIZE, C1, C2, C3, C4, C5,
107:      $                   DIFF, DISCR, PP, QQ, R, RTMAX, RTMIN, S1, S2,
108:      $                   SAFMAX, SHIFT, SS, SUM, WABS, WBIG, WDET,
109:      $                   WSCALE, WSIZE, WSMALL
110: *     ..
111: *     .. Intrinsic Functions ..
112:       INTRINSIC          ABS, MAX, MIN, SIGN, SQRT
113: *     ..
114: *     .. Executable Statements ..
115: *
116:       RTMIN = SQRT( SAFMIN )
117:       RTMAX = ONE / RTMIN
118:       SAFMAX = ONE / SAFMIN
119: *
120: *     Scale A
121: *
122:       ANORM = MAX( ABS( A( 1, 1 ) )+ABS( A( 2, 1 ) ),
123:      $        ABS( A( 1, 2 ) )+ABS( A( 2, 2 ) ), SAFMIN )
124:       ASCALE = ONE / ANORM
125:       A11 = ASCALE*A( 1, 1 )
126:       A21 = ASCALE*A( 2, 1 )
127:       A12 = ASCALE*A( 1, 2 )
128:       A22 = ASCALE*A( 2, 2 )
129: *
130: *     Perturb B if necessary to insure non-singularity
131: *
132:       B11 = B( 1, 1 )
133:       B12 = B( 1, 2 )
134:       B22 = B( 2, 2 )
135:       BMIN = RTMIN*MAX( ABS( B11 ), ABS( B12 ), ABS( B22 ), RTMIN )
136:       IF( ABS( B11 ).LT.BMIN )
137:      $   B11 = SIGN( BMIN, B11 )
138:       IF( ABS( B22 ).LT.BMIN )
139:      $   B22 = SIGN( BMIN, B22 )
140: *
141: *     Scale B
142: *
143:       BNORM = MAX( ABS( B11 ), ABS( B12 )+ABS( B22 ), SAFMIN )
144:       BSIZE = MAX( ABS( B11 ), ABS( B22 ) )
145:       BSCALE = ONE / BSIZE
146:       B11 = B11*BSCALE
147:       B12 = B12*BSCALE
148:       B22 = B22*BSCALE
149: *
150: *     Compute larger eigenvalue by method described by C. van Loan
151: *
152: *     ( AS is A shifted by -SHIFT*B )
153: *
154:       BINV11 = ONE / B11
155:       BINV22 = ONE / B22
156:       S1 = A11*BINV11
157:       S2 = A22*BINV22
158:       IF( ABS( S1 ).LE.ABS( S2 ) ) THEN
159:          AS12 = A12 - S1*B12
160:          AS22 = A22 - S1*B22
161:          SS = A21*( BINV11*BINV22 )
162:          ABI22 = AS22*BINV22 - SS*B12
163:          PP = HALF*ABI22
164:          SHIFT = S1
165:       ELSE
166:          AS12 = A12 - S2*B12
167:          AS11 = A11 - S2*B11
168:          SS = A21*( BINV11*BINV22 )
169:          ABI22 = -SS*B12
170:          PP = HALF*( AS11*BINV11+ABI22 )
171:          SHIFT = S2
172:       END IF
173:       QQ = SS*AS12
174:       IF( ABS( PP*RTMIN ).GE.ONE ) THEN
175:          DISCR = ( RTMIN*PP )**2 + QQ*SAFMIN
176:          R = SQRT( ABS( DISCR ) )*RTMAX
177:       ELSE
178:          IF( PP**2+ABS( QQ ).LE.SAFMIN ) THEN
179:             DISCR = ( RTMAX*PP )**2 + QQ*SAFMAX
180:             R = SQRT( ABS( DISCR ) )*RTMIN
181:          ELSE
182:             DISCR = PP**2 + QQ
183:             R = SQRT( ABS( DISCR ) )
184:          END IF
185:       END IF
186: *
187: *     Note: the test of R in the following IF is to cover the case when
188: *           DISCR is small and negative and is flushed to zero during
189: *           the calculation of R.  On machines which have a consistent
190: *           flush-to-zero threshhold and handle numbers above that
191: *           threshhold correctly, it would not be necessary.
192: *
193:       IF( DISCR.GE.ZERO .OR. R.EQ.ZERO ) THEN
194:          SUM = PP + SIGN( R, PP )
195:          DIFF = PP - SIGN( R, PP )
196:          WBIG = SHIFT + SUM
197: *
198: *        Compute smaller eigenvalue
199: *
200:          WSMALL = SHIFT + DIFF
201:          IF( HALF*ABS( WBIG ).GT.MAX( ABS( WSMALL ), SAFMIN ) ) THEN
202:             WDET = ( A11*A22-A12*A21 )*( BINV11*BINV22 )
203:             WSMALL = WDET / WBIG
204:          END IF
205: *
206: *        Choose (real) eigenvalue closest to 2,2 element of A*B**(-1)
207: *        for WR1.
208: *
209:          IF( PP.GT.ABI22 ) THEN
210:             WR1 = MIN( WBIG, WSMALL )
211:             WR2 = MAX( WBIG, WSMALL )
212:          ELSE
213:             WR1 = MAX( WBIG, WSMALL )
214:             WR2 = MIN( WBIG, WSMALL )
215:          END IF
216:          WI = ZERO
217:       ELSE
218: *
219: *        Complex eigenvalues
220: *
221:          WR1 = SHIFT + PP
222:          WR2 = WR1
223:          WI = R
224:       END IF
225: *
226: *     Further scaling to avoid underflow and overflow in computing
227: *     SCALE1 and overflow in computing w*B.
228: *
229: *     This scale factor (WSCALE) is bounded from above using C1 and C2,
230: *     and from below using C3 and C4.
231: *        C1 implements the condition  s A  must never overflow.
232: *        C2 implements the condition  w B  must never overflow.
233: *        C3, with C2,
234: *           implement the condition that s A - w B must never overflow.
235: *        C4 implements the condition  s    should not underflow.
236: *        C5 implements the condition  max(s,|w|) should be at least 2.
237: *
238:       C1 = BSIZE*( SAFMIN*MAX( ONE, ASCALE ) )
239:       C2 = SAFMIN*MAX( ONE, BNORM )
240:       C3 = BSIZE*SAFMIN
241:       IF( ASCALE.LE.ONE .AND. BSIZE.LE.ONE ) THEN
242:          C4 = MIN( ONE, ( ASCALE / SAFMIN )*BSIZE )
243:       ELSE
244:          C4 = ONE
245:       END IF
246:       IF( ASCALE.LE.ONE .OR. BSIZE.LE.ONE ) THEN
247:          C5 = MIN( ONE, ASCALE*BSIZE )
248:       ELSE
249:          C5 = ONE
250:       END IF
251: *
252: *     Scale first eigenvalue
253: *
254:       WABS = ABS( WR1 ) + ABS( WI )
255:       WSIZE = MAX( SAFMIN, C1, FUZZY1*( WABS*C2+C3 ),
256:      $        MIN( C4, HALF*MAX( WABS, C5 ) ) )
257:       IF( WSIZE.NE.ONE ) THEN
258:          WSCALE = ONE / WSIZE
259:          IF( WSIZE.GT.ONE ) THEN
260:             SCALE1 = ( MAX( ASCALE, BSIZE )*WSCALE )*
261:      $               MIN( ASCALE, BSIZE )
262:          ELSE
263:             SCALE1 = ( MIN( ASCALE, BSIZE )*WSCALE )*
264:      $               MAX( ASCALE, BSIZE )
265:          END IF
266:          WR1 = WR1*WSCALE
267:          IF( WI.NE.ZERO ) THEN
268:             WI = WI*WSCALE
269:             WR2 = WR1
270:             SCALE2 = SCALE1
271:          END IF
272:       ELSE
273:          SCALE1 = ASCALE*BSIZE
274:          SCALE2 = SCALE1
275:       END IF
276: *
277: *     Scale second eigenvalue (if real)
278: *
279:       IF( WI.EQ.ZERO ) THEN
280:          WSIZE = MAX( SAFMIN, C1, FUZZY1*( ABS( WR2 )*C2+C3 ),
281:      $           MIN( C4, HALF*MAX( ABS( WR2 ), C5 ) ) )
282:          IF( WSIZE.NE.ONE ) THEN
283:             WSCALE = ONE / WSIZE
284:             IF( WSIZE.GT.ONE ) THEN
285:                SCALE2 = ( MAX( ASCALE, BSIZE )*WSCALE )*
286:      $                  MIN( ASCALE, BSIZE )
287:             ELSE
288:                SCALE2 = ( MIN( ASCALE, BSIZE )*WSCALE )*
289:      $                  MAX( ASCALE, BSIZE )
290:             END IF
291:             WR2 = WR2*WSCALE
292:          ELSE
293:             SCALE2 = ASCALE*BSIZE
294:          END IF
295:       END IF
296: *
297: *     End of SLAG2
298: *
299:       RETURN
300:       END
301: