001:       SUBROUTINE DLARRE( RANGE, N, VL, VU, IL, IU, D, E, E2,
002:      $                    RTOL1, RTOL2, SPLTOL, NSPLIT, ISPLIT, M,
003:      $                    W, WERR, WGAP, IBLOCK, INDEXW, GERS, PIVMIN,
004:      $                    WORK, IWORK, INFO )
005:       IMPLICIT NONE
006: *
007: *  -- LAPACK auxiliary routine (version 3.2) --
008: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
009: *     November 2006
010: *
011: *     .. Scalar Arguments ..
012:       CHARACTER          RANGE
013:       INTEGER            IL, INFO, IU, M, N, NSPLIT
014:       DOUBLE PRECISION  PIVMIN, RTOL1, RTOL2, SPLTOL, VL, VU
015: *     ..
016: *     .. Array Arguments ..
017:       INTEGER            IBLOCK( * ), ISPLIT( * ), IWORK( * ),
018:      $                   INDEXW( * )
019:       DOUBLE PRECISION   D( * ), E( * ), E2( * ), GERS( * ),
020:      $                   W( * ),WERR( * ), WGAP( * ), WORK( * )
021: *     ..
022: *
023: *  Purpose
024: *  =======
025: *
026: *  To find the desired eigenvalues of a given real symmetric
027: *  tridiagonal matrix T, DLARRE sets any "small" off-diagonal
028: *  elements to zero, and for each unreduced block T_i, it finds
029: *  (a) a suitable shift at one end of the block's spectrum,
030: *  (b) the base representation, T_i - sigma_i I = L_i D_i L_i^T, and
031: *  (c) eigenvalues of each L_i D_i L_i^T.
032: *  The representations and eigenvalues found are then used by
033: *  DSTEMR to compute the eigenvectors of T.
034: *  The accuracy varies depending on whether bisection is used to
035: *  find a few eigenvalues or the dqds algorithm (subroutine DLASQ2) to
036: *  conpute all and then discard any unwanted one.
037: *  As an added benefit, DLARRE also outputs the n
038: *  Gerschgorin intervals for the matrices L_i D_i L_i^T.
039: *
040: *  Arguments
041: *  =========
042: *
043: *  RANGE   (input) CHARACTER
044: *          = 'A': ("All")   all eigenvalues will be found.
045: *          = 'V': ("Value") all eigenvalues in the half-open interval
046: *                           (VL, VU] will be found.
047: *          = 'I': ("Index") the IL-th through IU-th eigenvalues (of the
048: *                           entire matrix) will be found.
049: *
050: *  N       (input) INTEGER
051: *          The order of the matrix. N > 0.
052: *
053: *  VL      (input/output) DOUBLE PRECISION
054: *  VU      (input/output) DOUBLE PRECISION
055: *          If RANGE='V', the lower and upper bounds for the eigenvalues.
056: *          Eigenvalues less than or equal to VL, or greater than VU,
057: *          will not be returned.  VL < VU.
058: *          If RANGE='I' or ='A', DLARRE computes bounds on the desired
059: *          part of the spectrum.
060: *
061: *  IL      (input) INTEGER
062: *  IU      (input) INTEGER
063: *          If RANGE='I', the indices (in ascending order) of the
064: *          smallest and largest eigenvalues to be returned.
065: *          1 <= IL <= IU <= N.
066: *
067: *  D       (input/output) DOUBLE PRECISION array, dimension (N)
068: *          On entry, the N diagonal elements of the tridiagonal
069: *          matrix T.
070: *          On exit, the N diagonal elements of the diagonal
071: *          matrices D_i.
072: *
073: *  E       (input/output) DOUBLE PRECISION array, dimension (N)
074: *          On entry, the first (N-1) entries contain the subdiagonal
075: *          elements of the tridiagonal matrix T; E(N) need not be set.
076: *          On exit, E contains the subdiagonal elements of the unit
077: *          bidiagonal matrices L_i. The entries E( ISPLIT( I ) ),
078: *          1 <= I <= NSPLIT, contain the base points sigma_i on output.
079: *
080: *  E2      (input/output) DOUBLE PRECISION array, dimension (N)
081: *          On entry, the first (N-1) entries contain the SQUARES of the
082: *          subdiagonal elements of the tridiagonal matrix T;
083: *          E2(N) need not be set.
084: *          On exit, the entries E2( ISPLIT( I ) ),
085: *          1 <= I <= NSPLIT, have been set to zero
086: *
087: *  RTOL1   (input) DOUBLE PRECISION
088: *  RTOL2   (input) DOUBLE PRECISION
089: *           Parameters for bisection.
090: *           An interval [LEFT,RIGHT] has converged if
091: *           RIGHT-LEFT.LT.MAX( RTOL1*GAP, RTOL2*MAX(|LEFT|,|RIGHT|) )
092: *
093: *  SPLTOL (input) DOUBLE PRECISION
094: *          The threshold for splitting.
095: *
096: *  NSPLIT  (output) INTEGER
097: *          The number of blocks T splits into. 1 <= NSPLIT <= N.
098: *
099: *  ISPLIT  (output) INTEGER array, dimension (N)
100: *          The splitting points, at which T breaks up into blocks.
101: *          The first block consists of rows/columns 1 to ISPLIT(1),
102: *          the second of rows/columns ISPLIT(1)+1 through ISPLIT(2),
103: *          etc., and the NSPLIT-th consists of rows/columns
104: *          ISPLIT(NSPLIT-1)+1 through ISPLIT(NSPLIT)=N.
105: *
106: *  M       (output) INTEGER
107: *          The total number of eigenvalues (of all L_i D_i L_i^T)
108: *          found.
109: *
110: *  W       (output) DOUBLE PRECISION array, dimension (N)
111: *          The first M elements contain the eigenvalues. The
112: *          eigenvalues of each of the blocks, L_i D_i L_i^T, are
113: *          sorted in ascending order ( DLARRE may use the
114: *          remaining N-M elements as workspace).
115: *
116: *  WERR    (output) DOUBLE PRECISION array, dimension (N)
117: *          The error bound on the corresponding eigenvalue in W.
118: *
119: *  WGAP    (output) DOUBLE PRECISION array, dimension (N)
120: *          The separation from the right neighbor eigenvalue in W.
121: *          The gap is only with respect to the eigenvalues of the same block
122: *          as each block has its own representation tree.
123: *          Exception: at the right end of a block we store the left gap
124: *
125: *  IBLOCK  (output) INTEGER array, dimension (N)
126: *          The indices of the blocks (submatrices) associated with the
127: *          corresponding eigenvalues in W; IBLOCK(i)=1 if eigenvalue
128: *          W(i) belongs to the first block from the top, =2 if W(i)
129: *          belongs to the second block, etc.
130: *
131: *  INDEXW  (output) INTEGER array, dimension (N)
132: *          The indices of the eigenvalues within each block (submatrix);
133: *          for example, INDEXW(i)= 10 and IBLOCK(i)=2 imply that the
134: *          i-th eigenvalue W(i) is the 10-th eigenvalue in block 2
135: *
136: *  GERS    (output) DOUBLE PRECISION array, dimension (2*N)
137: *          The N Gerschgorin intervals (the i-th Gerschgorin interval
138: *          is (GERS(2*i-1), GERS(2*i)).
139: *
140: *  PIVMIN  (output) DOUBLE PRECISION
141: *          The minimum pivot in the Sturm sequence for T.
142: *
143: *  WORK    (workspace) DOUBLE PRECISION array, dimension (6*N)
144: *          Workspace.
145: *
146: *  IWORK   (workspace) INTEGER array, dimension (5*N)
147: *          Workspace.
148: *
149: *  INFO    (output) INTEGER
150: *          = 0:  successful exit
151: *          > 0:  A problem occured in DLARRE.
152: *          < 0:  One of the called subroutines signaled an internal problem.
153: *                Needs inspection of the corresponding parameter IINFO
154: *                for further information.
155: *
156: *          =-1:  Problem in DLARRD.
157: *          = 2:  No base representation could be found in MAXTRY iterations.
158: *                Increasing MAXTRY and recompilation might be a remedy.
159: *          =-3:  Problem in DLARRB when computing the refined root
160: *                representation for DLASQ2.
161: *          =-4:  Problem in DLARRB when preforming bisection on the
162: *                desired part of the spectrum.
163: *          =-5:  Problem in DLASQ2.
164: *          =-6:  Problem in DLASQ2.
165: *
166: *  Further Details
167: *  The base representations are required to suffer very little
168: *  element growth and consequently define all their eigenvalues to
169: *  high relative accuracy.
170: *  ===============
171: *
172: *  Based on contributions by
173: *     Beresford Parlett, University of California, Berkeley, USA
174: *     Jim Demmel, University of California, Berkeley, USA
175: *     Inderjit Dhillon, University of Texas, Austin, USA
176: *     Osni Marques, LBNL/NERSC, USA
177: *     Christof Voemel, University of California, Berkeley, USA
178: *
179: *  =====================================================================
180: *
181: *     .. Parameters ..
182:       DOUBLE PRECISION   FAC, FOUR, FOURTH, FUDGE, HALF, HNDRD,
183:      $                   MAXGROWTH, ONE, PERT, TWO, ZERO
184:       PARAMETER          ( ZERO = 0.0D0, ONE = 1.0D0,
185:      $                     TWO = 2.0D0, FOUR=4.0D0,
186:      $                     HNDRD = 100.0D0,
187:      $                     PERT = 8.0D0,
188:      $                     HALF = ONE/TWO, FOURTH = ONE/FOUR, FAC= HALF,
189:      $                     MAXGROWTH = 64.0D0, FUDGE = 2.0D0 )
190:       INTEGER            MAXTRY, ALLRNG, INDRNG, VALRNG
191:       PARAMETER          ( MAXTRY = 6, ALLRNG = 1, INDRNG = 2,
192:      $                     VALRNG = 3 )
193: *     ..
194: *     .. Local Scalars ..
195:       LOGICAL            FORCEB, NOREP, USEDQD
196:       INTEGER            CNT, CNT1, CNT2, I, IBEGIN, IDUM, IEND, IINFO,
197:      $                   IN, INDL, INDU, IRANGE, J, JBLK, MB, MM,
198:      $                   WBEGIN, WEND
199:       DOUBLE PRECISION   AVGAP, BSRTOL, CLWDTH, DMAX, DPIVOT, EABS,
200:      $                   EMAX, EOLD, EPS, GL, GU, ISLEFT, ISRGHT, RTL,
201:      $                   RTOL, S1, S2, SAFMIN, SGNDEF, SIGMA, SPDIAM,
202:      $                   TAU, TMP, TMP1
203: 
204: 
205: *     ..
206: *     .. Local Arrays ..
207:       INTEGER            ISEED( 4 )
208: *     ..
209: *     .. External Functions ..
210:       LOGICAL            LSAME
211:       DOUBLE PRECISION            DLAMCH
212:       EXTERNAL           DLAMCH, LSAME
213: 
214: *     ..
215: *     .. External Subroutines ..
216:       EXTERNAL           DCOPY, DLARNV, DLARRA, DLARRB, DLARRC, DLARRD,
217:      $                   DLASQ2
218: *     ..
219: *     .. Intrinsic Functions ..
220:       INTRINSIC          ABS, MAX, MIN
221: 
222: *     ..
223: *     .. Executable Statements ..
224: *
225: 
226:       INFO = 0
227: 
228: *
229: *     Decode RANGE
230: *
231:       IF( LSAME( RANGE, 'A' ) ) THEN
232:          IRANGE = ALLRNG
233:       ELSE IF( LSAME( RANGE, 'V' ) ) THEN
234:          IRANGE = VALRNG
235:       ELSE IF( LSAME( RANGE, 'I' ) ) THEN
236:          IRANGE = INDRNG
237:       END IF
238: 
239:       M = 0
240: 
241: *     Get machine constants
242:       SAFMIN = DLAMCH( 'S' )
243:       EPS = DLAMCH( 'P' )
244: 
245: *     Set parameters
246:       RTL = SQRT(EPS)
247:       BSRTOL = SQRT(EPS)
248: 
249: *     Treat case of 1x1 matrix for quick return
250:       IF( N.EQ.1 ) THEN
251:          IF( (IRANGE.EQ.ALLRNG).OR.
252:      $       ((IRANGE.EQ.VALRNG).AND.(D(1).GT.VL).AND.(D(1).LE.VU)).OR.
253:      $       ((IRANGE.EQ.INDRNG).AND.(IL.EQ.1).AND.(IU.EQ.1)) ) THEN
254:             M = 1
255:             W(1) = D(1)
256: *           The computation error of the eigenvalue is zero
257:             WERR(1) = ZERO
258:             WGAP(1) = ZERO
259:             IBLOCK( 1 ) = 1
260:             INDEXW( 1 ) = 1
261:             GERS(1) = D( 1 )
262:             GERS(2) = D( 1 )
263:          ENDIF
264: *        store the shift for the initial RRR, which is zero in this case
265:          E(1) = ZERO
266:          RETURN
267:       END IF
268: 
269: *     General case: tridiagonal matrix of order > 1
270: *
271: *     Init WERR, WGAP. Compute Gerschgorin intervals and spectral diameter.
272: *     Compute maximum off-diagonal entry and pivmin.
273:       GL = D(1)
274:       GU = D(1)
275:       EOLD = ZERO
276:       EMAX = ZERO
277:       E(N) = ZERO
278:       DO 5 I = 1,N
279:          WERR(I) = ZERO
280:          WGAP(I) = ZERO
281:          EABS = ABS( E(I) )
282:          IF( EABS .GE. EMAX ) THEN
283:             EMAX = EABS
284:          END IF
285:          TMP1 = EABS + EOLD
286:          GERS( 2*I-1) = D(I) - TMP1
287:          GL =  MIN( GL, GERS( 2*I - 1))
288:          GERS( 2*I ) = D(I) + TMP1
289:          GU = MAX( GU, GERS(2*I) )
290:          EOLD  = EABS
291:  5    CONTINUE
292: *     The minimum pivot allowed in the Sturm sequence for T
293:       PIVMIN = SAFMIN * MAX( ONE, EMAX**2 )
294: *     Compute spectral diameter. The Gerschgorin bounds give an
295: *     estimate that is wrong by at most a factor of SQRT(2)
296:       SPDIAM = GU - GL
297: 
298: *     Compute splitting points
299:       CALL DLARRA( N, D, E, E2, SPLTOL, SPDIAM,
300:      $                    NSPLIT, ISPLIT, IINFO )
301: 
302: *     Can force use of bisection instead of faster DQDS.
303: *     Option left in the code for future multisection work.
304:       FORCEB = .FALSE.
305: 
306: *     Initialize USEDQD, DQDS should be used for ALLRNG unless someone
307: *     explicitly wants bisection.
308:       USEDQD = (( IRANGE.EQ.ALLRNG ) .AND. (.NOT.FORCEB))
309: 
310:       IF( (IRANGE.EQ.ALLRNG) .AND. (.NOT. FORCEB) ) THEN
311: *        Set interval [VL,VU] that contains all eigenvalues
312:          VL = GL
313:          VU = GU
314:       ELSE
315: *        We call DLARRD to find crude approximations to the eigenvalues
316: *        in the desired range. In case IRANGE = INDRNG, we also obtain the
317: *        interval (VL,VU] that contains all the wanted eigenvalues.
318: *        An interval [LEFT,RIGHT] has converged if
319: *        RIGHT-LEFT.LT.RTOL*MAX(ABS(LEFT),ABS(RIGHT))
320: *        DLARRD needs a WORK of size 4*N, IWORK of size 3*N
321:          CALL DLARRD( RANGE, 'B', N, VL, VU, IL, IU, GERS,
322:      $                    BSRTOL, D, E, E2, PIVMIN, NSPLIT, ISPLIT,
323:      $                    MM, W, WERR, VL, VU, IBLOCK, INDEXW,
324:      $                    WORK, IWORK, IINFO )
325:          IF( IINFO.NE.0 ) THEN
326:             INFO = -1
327:             RETURN
328:          ENDIF
329: *        Make sure that the entries M+1 to N in W, WERR, IBLOCK, INDEXW are 0
330:          DO 14 I = MM+1,N
331:             W( I ) = ZERO
332:             WERR( I ) = ZERO
333:             IBLOCK( I ) = 0
334:             INDEXW( I ) = 0
335:  14      CONTINUE
336:       END IF
337: 
338: 
339: ***
340: *     Loop over unreduced blocks
341:       IBEGIN = 1
342:       WBEGIN = 1
343:       DO 170 JBLK = 1, NSPLIT
344:          IEND = ISPLIT( JBLK )
345:          IN = IEND - IBEGIN + 1
346: 
347: *        1 X 1 block
348:          IF( IN.EQ.1 ) THEN
349:             IF( (IRANGE.EQ.ALLRNG).OR.( (IRANGE.EQ.VALRNG).AND.
350:      $         ( D( IBEGIN ).GT.VL ).AND.( D( IBEGIN ).LE.VU ) )
351:      $        .OR. ( (IRANGE.EQ.INDRNG).AND.(IBLOCK(WBEGIN).EQ.JBLK))
352:      $        ) THEN
353:                M = M + 1
354:                W( M ) = D( IBEGIN )
355:                WERR(M) = ZERO
356: *              The gap for a single block doesn't matter for the later
357: *              algorithm and is assigned an arbitrary large value
358:                WGAP(M) = ZERO
359:                IBLOCK( M ) = JBLK
360:                INDEXW( M ) = 1
361:                WBEGIN = WBEGIN + 1
362:             ENDIF
363: *           E( IEND ) holds the shift for the initial RRR
364:             E( IEND ) = ZERO
365:             IBEGIN = IEND + 1
366:             GO TO 170
367:          END IF
368: *
369: *        Blocks of size larger than 1x1
370: *
371: *        E( IEND ) will hold the shift for the initial RRR, for now set it =0
372:          E( IEND ) = ZERO
373: *
374: *        Find local outer bounds GL,GU for the block
375:          GL = D(IBEGIN)
376:          GU = D(IBEGIN)
377:          DO 15 I = IBEGIN , IEND
378:             GL = MIN( GERS( 2*I-1 ), GL )
379:             GU = MAX( GERS( 2*I ), GU )
380:  15      CONTINUE
381:          SPDIAM = GU - GL
382: 
383:          IF(.NOT. ((IRANGE.EQ.ALLRNG).AND.(.NOT.FORCEB)) ) THEN
384: *           Count the number of eigenvalues in the current block.
385:             MB = 0
386:             DO 20 I = WBEGIN,MM
387:                IF( IBLOCK(I).EQ.JBLK ) THEN
388:                   MB = MB+1
389:                ELSE
390:                   GOTO 21
391:                ENDIF
392:  20         CONTINUE
393:  21         CONTINUE
394: 
395:             IF( MB.EQ.0) THEN
396: *              No eigenvalue in the current block lies in the desired range
397: *              E( IEND ) holds the shift for the initial RRR
398:                E( IEND ) = ZERO
399:                IBEGIN = IEND + 1
400:                GO TO 170
401:             ELSE
402: 
403: *              Decide whether dqds or bisection is more efficient
404:                USEDQD = ( (MB .GT. FAC*IN) .AND. (.NOT.FORCEB) )
405:                WEND = WBEGIN + MB - 1
406: *              Calculate gaps for the current block
407: *              In later stages, when representations for individual
408: *              eigenvalues are different, we use SIGMA = E( IEND ).
409:                SIGMA = ZERO
410:                DO 30 I = WBEGIN, WEND - 1
411:                   WGAP( I ) = MAX( ZERO,
412:      $                        W(I+1)-WERR(I+1) - (W(I)+WERR(I)) )
413:  30            CONTINUE
414:                WGAP( WEND ) = MAX( ZERO,
415:      $                     VU - SIGMA - (W( WEND )+WERR( WEND )))
416: *              Find local index of the first and last desired evalue.
417:                INDL = INDEXW(WBEGIN)
418:                INDU = INDEXW( WEND )
419:             ENDIF
420:          ENDIF
421:          IF(( (IRANGE.EQ.ALLRNG) .AND. (.NOT. FORCEB) ).OR.USEDQD) THEN
422: *           Case of DQDS
423: *           Find approximations to the extremal eigenvalues of the block
424:             CALL DLARRK( IN, 1, GL, GU, D(IBEGIN),
425:      $               E2(IBEGIN), PIVMIN, RTL, TMP, TMP1, IINFO )
426:             IF( IINFO.NE.0 ) THEN
427:                INFO = -1
428:                RETURN
429:             ENDIF
430:             ISLEFT = MAX(GL, TMP - TMP1
431:      $               - HNDRD * EPS* ABS(TMP - TMP1))
432: 
433:             CALL DLARRK( IN, IN, GL, GU, D(IBEGIN),
434:      $               E2(IBEGIN), PIVMIN, RTL, TMP, TMP1, IINFO )
435:             IF( IINFO.NE.0 ) THEN
436:                INFO = -1
437:                RETURN
438:             ENDIF
439:             ISRGHT = MIN(GU, TMP + TMP1
440:      $                 + HNDRD * EPS * ABS(TMP + TMP1))
441: *           Improve the estimate of the spectral diameter
442:             SPDIAM = ISRGHT - ISLEFT
443:          ELSE
444: *           Case of bisection
445: *           Find approximations to the wanted extremal eigenvalues
446:             ISLEFT = MAX(GL, W(WBEGIN) - WERR(WBEGIN)
447:      $                  - HNDRD * EPS*ABS(W(WBEGIN)- WERR(WBEGIN) ))
448:             ISRGHT = MIN(GU,W(WEND) + WERR(WEND)
449:      $                  + HNDRD * EPS * ABS(W(WEND)+ WERR(WEND)))
450:          ENDIF
451: 
452: 
453: *        Decide whether the base representation for the current block
454: *        L_JBLK D_JBLK L_JBLK^T = T_JBLK - sigma_JBLK I
455: *        should be on the left or the right end of the current block.
456: *        The strategy is to shift to the end which is "more populated"
457: *        Furthermore, decide whether to use DQDS for the computation of
458: *        the eigenvalue approximations at the end of DLARRE or bisection.
459: *        dqds is chosen if all eigenvalues are desired or the number of
460: *        eigenvalues to be computed is large compared to the blocksize.
461:          IF( ( IRANGE.EQ.ALLRNG ) .AND. (.NOT.FORCEB) ) THEN
462: *           If all the eigenvalues have to be computed, we use dqd
463:             USEDQD = .TRUE.
464: *           INDL is the local index of the first eigenvalue to compute
465:             INDL = 1
466:             INDU = IN
467: *           MB =  number of eigenvalues to compute
468:             MB = IN
469:             WEND = WBEGIN + MB - 1
470: *           Define 1/4 and 3/4 points of the spectrum
471:             S1 = ISLEFT + FOURTH * SPDIAM
472:             S2 = ISRGHT - FOURTH * SPDIAM
473:          ELSE
474: *           DLARRD has computed IBLOCK and INDEXW for each eigenvalue
475: *           approximation.
476: *           choose sigma
477:             IF( USEDQD ) THEN
478:                S1 = ISLEFT + FOURTH * SPDIAM
479:                S2 = ISRGHT - FOURTH * SPDIAM
480:             ELSE
481:                TMP = MIN(ISRGHT,VU) -  MAX(ISLEFT,VL)
482:                S1 =  MAX(ISLEFT,VL) + FOURTH * TMP
483:                S2 =  MIN(ISRGHT,VU) - FOURTH * TMP
484:             ENDIF
485:          ENDIF
486: 
487: *        Compute the negcount at the 1/4 and 3/4 points
488:          IF(MB.GT.1) THEN
489:             CALL DLARRC( 'T', IN, S1, S2, D(IBEGIN),
490:      $                    E(IBEGIN), PIVMIN, CNT, CNT1, CNT2, IINFO)
491:          ENDIF
492: 
493:          IF(MB.EQ.1) THEN
494:             SIGMA = GL
495:             SGNDEF = ONE
496:          ELSEIF( CNT1 - INDL .GE. INDU - CNT2 ) THEN
497:             IF( ( IRANGE.EQ.ALLRNG ) .AND. (.NOT.FORCEB) ) THEN
498:                SIGMA = MAX(ISLEFT,GL)
499:             ELSEIF( USEDQD ) THEN
500: *              use Gerschgorin bound as shift to get pos def matrix
501: *              for dqds
502:                SIGMA = ISLEFT
503:             ELSE
504: *              use approximation of the first desired eigenvalue of the
505: *              block as shift
506:                SIGMA = MAX(ISLEFT,VL)
507:             ENDIF
508:             SGNDEF = ONE
509:          ELSE
510:             IF( ( IRANGE.EQ.ALLRNG ) .AND. (.NOT.FORCEB) ) THEN
511:                SIGMA = MIN(ISRGHT,GU)
512:             ELSEIF( USEDQD ) THEN
513: *              use Gerschgorin bound as shift to get neg def matrix
514: *              for dqds
515:                SIGMA = ISRGHT
516:             ELSE
517: *              use approximation of the first desired eigenvalue of the
518: *              block as shift
519:                SIGMA = MIN(ISRGHT,VU)
520:             ENDIF
521:             SGNDEF = -ONE
522:          ENDIF
523: 
524: 
525: *        An initial SIGMA has been chosen that will be used for computing
526: *        T - SIGMA I = L D L^T
527: *        Define the increment TAU of the shift in case the initial shift
528: *        needs to be refined to obtain a factorization with not too much
529: *        element growth.
530:          IF( USEDQD ) THEN
531: *           The initial SIGMA was to the outer end of the spectrum
532: *           the matrix is definite and we need not retreat.
533:             TAU = SPDIAM*EPS*N + TWO*PIVMIN
534:          ELSE
535:             IF(MB.GT.1) THEN
536:                CLWDTH = W(WEND) + WERR(WEND) - W(WBEGIN) - WERR(WBEGIN)
537:                AVGAP = ABS(CLWDTH / DBLE(WEND-WBEGIN))
538:                IF( SGNDEF.EQ.ONE ) THEN
539:                   TAU = HALF*MAX(WGAP(WBEGIN),AVGAP)
540:                   TAU = MAX(TAU,WERR(WBEGIN))
541:                ELSE
542:                   TAU = HALF*MAX(WGAP(WEND-1),AVGAP)
543:                   TAU = MAX(TAU,WERR(WEND))
544:                ENDIF
545:             ELSE
546:                TAU = WERR(WBEGIN)
547:             ENDIF
548:          ENDIF
549: *
550:          DO 80 IDUM = 1, MAXTRY
551: *           Compute L D L^T factorization of tridiagonal matrix T - sigma I.
552: *           Store D in WORK(1:IN), L in WORK(IN+1:2*IN), and reciprocals of
553: *           pivots in WORK(2*IN+1:3*IN)
554:             DPIVOT = D( IBEGIN ) - SIGMA
555:             WORK( 1 ) = DPIVOT
556:             DMAX = ABS( WORK(1) )
557:             J = IBEGIN
558:             DO 70 I = 1, IN - 1
559:                WORK( 2*IN+I ) = ONE / WORK( I )
560:                TMP = E( J )*WORK( 2*IN+I )
561:                WORK( IN+I ) = TMP
562:                DPIVOT = ( D( J+1 )-SIGMA ) - TMP*E( J )
563:                WORK( I+1 ) = DPIVOT
564:                DMAX = MAX( DMAX, ABS(DPIVOT) )
565:                J = J + 1
566:  70         CONTINUE
567: *           check for element growth
568:             IF( DMAX .GT. MAXGROWTH*SPDIAM ) THEN
569:                NOREP = .TRUE.
570:             ELSE
571:                NOREP = .FALSE.
572:             ENDIF
573:             IF( USEDQD .AND. .NOT.NOREP ) THEN
574: *              Ensure the definiteness of the representation
575: *              All entries of D (of L D L^T) must have the same sign
576:                DO 71 I = 1, IN
577:                   TMP = SGNDEF*WORK( I )
578:                   IF( TMP.LT.ZERO ) NOREP = .TRUE.
579:  71            CONTINUE
580:             ENDIF
581:             IF(NOREP) THEN
582: *              Note that in the case of IRANGE=ALLRNG, we use the Gerschgorin
583: *              shift which makes the matrix definite. So we should end up
584: *              here really only in the case of IRANGE = VALRNG or INDRNG.
585:                IF( IDUM.EQ.MAXTRY-1 ) THEN
586:                   IF( SGNDEF.EQ.ONE ) THEN
587: *                    The fudged Gerschgorin shift should succeed
588:                      SIGMA =
589:      $                    GL - FUDGE*SPDIAM*EPS*N - FUDGE*TWO*PIVMIN
590:                   ELSE
591:                      SIGMA =
592:      $                    GU + FUDGE*SPDIAM*EPS*N + FUDGE*TWO*PIVMIN
593:                   END IF
594:                ELSE
595:                   SIGMA = SIGMA - SGNDEF * TAU
596:                   TAU = TWO * TAU
597:                END IF
598:             ELSE
599: *              an initial RRR is found
600:                GO TO 83
601:             END IF
602:  80      CONTINUE
603: *        if the program reaches this point, no base representation could be
604: *        found in MAXTRY iterations.
605:          INFO = 2
606:          RETURN
607: 
608:  83      CONTINUE
609: *        At this point, we have found an initial base representation
610: *        T - SIGMA I = L D L^T with not too much element growth.
611: *        Store the shift.
612:          E( IEND ) = SIGMA
613: *        Store D and L.
614:          CALL DCOPY( IN, WORK, 1, D( IBEGIN ), 1 )
615:          CALL DCOPY( IN-1, WORK( IN+1 ), 1, E( IBEGIN ), 1 )
616: 
617: 
618:          IF(MB.GT.1 ) THEN
619: *
620: *           Perturb each entry of the base representation by a small
621: *           (but random) relative amount to overcome difficulties with
622: *           glued matrices.
623: *
624:             DO 122 I = 1, 4
625:                ISEED( I ) = 1
626:  122        CONTINUE
627: 
628:             CALL DLARNV(2, ISEED, 2*IN-1, WORK(1))
629:             DO 125 I = 1,IN-1
630:                D(IBEGIN+I-1) = D(IBEGIN+I-1)*(ONE+EPS*PERT*WORK(I))
631:                E(IBEGIN+I-1) = E(IBEGIN+I-1)*(ONE+EPS*PERT*WORK(IN+I))
632:  125        CONTINUE
633:             D(IEND) = D(IEND)*(ONE+EPS*FOUR*WORK(IN))
634: *
635:          ENDIF
636: *
637: *        Don't update the Gerschgorin intervals because keeping track
638: *        of the updates would be too much work in DLARRV.
639: *        We update W instead and use it to locate the proper Gerschgorin
640: *        intervals.
641: 
642: *        Compute the required eigenvalues of L D L' by bisection or dqds
643:          IF ( .NOT.USEDQD ) THEN
644: *           If DLARRD has been used, shift the eigenvalue approximations
645: *           according to their representation. This is necessary for
646: *           a uniform DLARRV since dqds computes eigenvalues of the
647: *           shifted representation. In DLARRV, W will always hold the
648: *           UNshifted eigenvalue approximation.
649:             DO 134 J=WBEGIN,WEND
650:                W(J) = W(J) - SIGMA
651:                WERR(J) = WERR(J) + ABS(W(J)) * EPS
652:  134        CONTINUE
653: *           call DLARRB to reduce eigenvalue error of the approximations
654: *           from DLARRD
655:             DO 135 I = IBEGIN, IEND-1
656:                WORK( I ) = D( I ) * E( I )**2
657:  135        CONTINUE
658: *           use bisection to find EV from INDL to INDU
659:             CALL DLARRB(IN, D(IBEGIN), WORK(IBEGIN),
660:      $                  INDL, INDU, RTOL1, RTOL2, INDL-1,
661:      $                  W(WBEGIN), WGAP(WBEGIN), WERR(WBEGIN),
662:      $                  WORK( 2*N+1 ), IWORK, PIVMIN, SPDIAM,
663:      $                  IN, IINFO )
664:             IF( IINFO .NE. 0 ) THEN
665:                INFO = -4
666:                RETURN
667:             END IF
668: *           DLARRB computes all gaps correctly except for the last one
669: *           Record distance to VU/GU
670:             WGAP( WEND ) = MAX( ZERO,
671:      $           ( VU-SIGMA ) - ( W( WEND ) + WERR( WEND ) ) )
672:             DO 138 I = INDL, INDU
673:                M = M + 1
674:                IBLOCK(M) = JBLK
675:                INDEXW(M) = I
676:  138        CONTINUE
677:          ELSE
678: *           Call dqds to get all eigs (and then possibly delete unwanted
679: *           eigenvalues).
680: *           Note that dqds finds the eigenvalues of the L D L^T representation
681: *           of T to high relative accuracy. High relative accuracy
682: *           might be lost when the shift of the RRR is subtracted to obtain
683: *           the eigenvalues of T. However, T is not guaranteed to define its
684: *           eigenvalues to high relative accuracy anyway.
685: *           Set RTOL to the order of the tolerance used in DLASQ2
686: *           This is an ESTIMATED error, the worst case bound is 4*N*EPS
687: *           which is usually too large and requires unnecessary work to be
688: *           done by bisection when computing the eigenvectors
689:             RTOL = LOG(DBLE(IN)) * FOUR * EPS
690:             J = IBEGIN
691:             DO 140 I = 1, IN - 1
692:                WORK( 2*I-1 ) = ABS( D( J ) )
693:                WORK( 2*I ) = E( J )*E( J )*WORK( 2*I-1 )
694:                J = J + 1
695:   140       CONTINUE
696:             WORK( 2*IN-1 ) = ABS( D( IEND ) )
697:             WORK( 2*IN ) = ZERO
698:             CALL DLASQ2( IN, WORK, IINFO )
699:             IF( IINFO .NE. 0 ) THEN
700: *              If IINFO = -5 then an index is part of a tight cluster
701: *              and should be changed. The index is in IWORK(1) and the
702: *              gap is in WORK(N+1)
703:                INFO = -5
704:                RETURN
705:             ELSE
706: *              Test that all eigenvalues are positive as expected
707:                DO 149 I = 1, IN
708:                   IF( WORK( I ).LT.ZERO ) THEN
709:                      INFO = -6
710:                      RETURN
711:                   ENDIF
712:  149           CONTINUE
713:             END IF
714:             IF( SGNDEF.GT.ZERO ) THEN
715:                DO 150 I = INDL, INDU
716:                   M = M + 1
717:                   W( M ) = WORK( IN-I+1 )
718:                   IBLOCK( M ) = JBLK
719:                   INDEXW( M ) = I
720:  150           CONTINUE
721:             ELSE
722:                DO 160 I = INDL, INDU
723:                   M = M + 1
724:                   W( M ) = -WORK( I )
725:                   IBLOCK( M ) = JBLK
726:                   INDEXW( M ) = I
727:  160           CONTINUE
728:             END IF
729: 
730:             DO 165 I = M - MB + 1, M
731: *              the value of RTOL below should be the tolerance in DLASQ2
732:                WERR( I ) = RTOL * ABS( W(I) )
733:  165        CONTINUE
734:             DO 166 I = M - MB + 1, M - 1
735: *              compute the right gap between the intervals
736:                WGAP( I ) = MAX( ZERO,
737:      $                          W(I+1)-WERR(I+1) - (W(I)+WERR(I)) )
738:  166        CONTINUE
739:             WGAP( M ) = MAX( ZERO,
740:      $           ( VU-SIGMA ) - ( W( M ) + WERR( M ) ) )
741:          END IF
742: *        proceed with next block
743:          IBEGIN = IEND + 1
744:          WBEGIN = WEND + 1
745:  170  CONTINUE
746: *
747: 
748:       RETURN
749: *
750: *     end of DLARRE
751: *
752:       END
753: