001:       SUBROUTINE DTRSEN( JOB, COMPQ, SELECT, N, T, LDT, Q, LDQ, WR, WI,
002:      $                   M, S, SEP, WORK, LWORK, IWORK, LIWORK, INFO )
003: *
004: *  -- LAPACK routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       CHARACTER          COMPQ, JOB
010:       INTEGER            INFO, LDQ, LDT, LIWORK, LWORK, M, N
011:       DOUBLE PRECISION   S, SEP
012: *     ..
013: *     .. Array Arguments ..
014:       LOGICAL            SELECT( * )
015:       INTEGER            IWORK( * )
016:       DOUBLE PRECISION   Q( LDQ, * ), T( LDT, * ), WI( * ), WORK( * ),
017:      $                   WR( * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  DTRSEN reorders the real Schur factorization of a real matrix
024: *  A = Q*T*Q**T, so that a selected cluster of eigenvalues appears in
025: *  the leading diagonal blocks of the upper quasi-triangular matrix T,
026: *  and the leading columns of Q form an orthonormal basis of the
027: *  corresponding right invariant subspace.
028: *
029: *  Optionally the routine computes the reciprocal condition numbers of
030: *  the cluster of eigenvalues and/or the invariant subspace.
031: *
032: *  T must be in Schur canonical form (as returned by DHSEQR), that is,
033: *  block upper triangular with 1-by-1 and 2-by-2 diagonal blocks; each
034: *  2-by-2 diagonal block has its diagonal elemnts equal and its
035: *  off-diagonal elements of opposite sign.
036: *
037: *  Arguments
038: *  =========
039: *
040: *  JOB     (input) CHARACTER*1
041: *          Specifies whether condition numbers are required for the
042: *          cluster of eigenvalues (S) or the invariant subspace (SEP):
043: *          = 'N': none;
044: *          = 'E': for eigenvalues only (S);
045: *          = 'V': for invariant subspace only (SEP);
046: *          = 'B': for both eigenvalues and invariant subspace (S and
047: *                 SEP).
048: *
049: *  COMPQ   (input) CHARACTER*1
050: *          = 'V': update the matrix Q of Schur vectors;
051: *          = 'N': do not update Q.
052: *
053: *  SELECT  (input) LOGICAL array, dimension (N)
054: *          SELECT specifies the eigenvalues in the selected cluster. To
055: *          select a real eigenvalue w(j), SELECT(j) must be set to
056: *          .TRUE.. To select a complex conjugate pair of eigenvalues
057: *          w(j) and w(j+1), corresponding to a 2-by-2 diagonal block,
058: *          either SELECT(j) or SELECT(j+1) or both must be set to
059: *          .TRUE.; a complex conjugate pair of eigenvalues must be
060: *          either both included in the cluster or both excluded.
061: *
062: *  N       (input) INTEGER
063: *          The order of the matrix T. N >= 0.
064: *
065: *  T       (input/output) DOUBLE PRECISION array, dimension (LDT,N)
066: *          On entry, the upper quasi-triangular matrix T, in Schur
067: *          canonical form.
068: *          On exit, T is overwritten by the reordered matrix T, again in
069: *          Schur canonical form, with the selected eigenvalues in the
070: *          leading diagonal blocks.
071: *
072: *  LDT     (input) INTEGER
073: *          The leading dimension of the array T. LDT >= max(1,N).
074: *
075: *  Q       (input/output) DOUBLE PRECISION array, dimension (LDQ,N)
076: *          On entry, if COMPQ = 'V', the matrix Q of Schur vectors.
077: *          On exit, if COMPQ = 'V', Q has been postmultiplied by the
078: *          orthogonal transformation matrix which reorders T; the
079: *          leading M columns of Q form an orthonormal basis for the
080: *          specified invariant subspace.
081: *          If COMPQ = 'N', Q is not referenced.
082: *
083: *  LDQ     (input) INTEGER
084: *          The leading dimension of the array Q.
085: *          LDQ >= 1; and if COMPQ = 'V', LDQ >= N.
086: *
087: *  WR      (output) DOUBLE PRECISION array, dimension (N)
088: *  WI      (output) DOUBLE PRECISION array, dimension (N)
089: *          The real and imaginary parts, respectively, of the reordered
090: *          eigenvalues of T. The eigenvalues are stored in the same
091: *          order as on the diagonal of T, with WR(i) = T(i,i) and, if
092: *          T(i:i+1,i:i+1) is a 2-by-2 diagonal block, WI(i) > 0 and
093: *          WI(i+1) = -WI(i). Note that if a complex eigenvalue is
094: *          sufficiently ill-conditioned, then its value may differ
095: *          significantly from its value before reordering.
096: *
097: *  M       (output) INTEGER
098: *          The dimension of the specified invariant subspace.
099: *          0 < = M <= N.
100: *
101: *  S       (output) DOUBLE PRECISION
102: *          If JOB = 'E' or 'B', S is a lower bound on the reciprocal
103: *          condition number for the selected cluster of eigenvalues.
104: *          S cannot underestimate the true reciprocal condition number
105: *          by more than a factor of sqrt(N). If M = 0 or N, S = 1.
106: *          If JOB = 'N' or 'V', S is not referenced.
107: *
108: *  SEP     (output) DOUBLE PRECISION
109: *          If JOB = 'V' or 'B', SEP is the estimated reciprocal
110: *          condition number of the specified invariant subspace. If
111: *          M = 0 or N, SEP = norm(T).
112: *          If JOB = 'N' or 'E', SEP is not referenced.
113: *
114: *  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK))
115: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
116: *
117: *  LWORK   (input) INTEGER
118: *          The dimension of the array WORK.
119: *          If JOB = 'N', LWORK >= max(1,N);
120: *          if JOB = 'E', LWORK >= max(1,M*(N-M));
121: *          if JOB = 'V' or 'B', LWORK >= max(1,2*M*(N-M)).
122: *
123: *          If LWORK = -1, then a workspace query is assumed; the routine
124: *          only calculates the optimal size of the WORK array, returns
125: *          this value as the first entry of the WORK array, and no error
126: *          message related to LWORK is issued by XERBLA.
127: *
128: *  IWORK   (workspace) INTEGER array, dimension (MAX(1,LIWORK))
129: *          On exit, if INFO = 0, IWORK(1) returns the optimal LIWORK.
130: *
131: *  LIWORK  (input) INTEGER
132: *          The dimension of the array IWORK.
133: *          If JOB = 'N' or 'E', LIWORK >= 1;
134: *          if JOB = 'V' or 'B', LIWORK >= max(1,M*(N-M)).
135: *
136: *          If LIWORK = -1, then a workspace query is assumed; the
137: *          routine only calculates the optimal size of the IWORK array,
138: *          returns this value as the first entry of the IWORK array, and
139: *          no error message related to LIWORK is issued by XERBLA.
140: *
141: *  INFO    (output) INTEGER
142: *          = 0: successful exit
143: *          < 0: if INFO = -i, the i-th argument had an illegal value
144: *          = 1: reordering of T failed because some eigenvalues are too
145: *               close to separate (the problem is very ill-conditioned);
146: *               T may have been partially reordered, and WR and WI
147: *               contain the eigenvalues in the same order as in T; S and
148: *               SEP (if requested) are set to zero.
149: *
150: *  Further Details
151: *  ===============
152: *
153: *  DTRSEN first collects the selected eigenvalues by computing an
154: *  orthogonal transformation Z to move them to the top left corner of T.
155: *  In other words, the selected eigenvalues are the eigenvalues of T11
156: *  in:
157: *
158: *                Z'*T*Z = ( T11 T12 ) n1
159: *                         (  0  T22 ) n2
160: *                            n1  n2
161: *
162: *  where N = n1+n2 and Z' means the transpose of Z. The first n1 columns
163: *  of Z span the specified invariant subspace of T.
164: *
165: *  If T has been obtained from the real Schur factorization of a matrix
166: *  A = Q*T*Q', then the reordered real Schur factorization of A is given
167: *  by A = (Q*Z)*(Z'*T*Z)*(Q*Z)', and the first n1 columns of Q*Z span
168: *  the corresponding invariant subspace of A.
169: *
170: *  The reciprocal condition number of the average of the eigenvalues of
171: *  T11 may be returned in S. S lies between 0 (very badly conditioned)
172: *  and 1 (very well conditioned). It is computed as follows. First we
173: *  compute R so that
174: *
175: *                         P = ( I  R ) n1
176: *                             ( 0  0 ) n2
177: *                               n1 n2
178: *
179: *  is the projector on the invariant subspace associated with T11.
180: *  R is the solution of the Sylvester equation:
181: *
182: *                        T11*R - R*T22 = T12.
183: *
184: *  Let F-norm(M) denote the Frobenius-norm of M and 2-norm(M) denote
185: *  the two-norm of M. Then S is computed as the lower bound
186: *
187: *                      (1 + F-norm(R)**2)**(-1/2)
188: *
189: *  on the reciprocal of 2-norm(P), the true reciprocal condition number.
190: *  S cannot underestimate 1 / 2-norm(P) by more than a factor of
191: *  sqrt(N).
192: *
193: *  An approximate error bound for the computed average of the
194: *  eigenvalues of T11 is
195: *
196: *                         EPS * norm(T) / S
197: *
198: *  where EPS is the machine precision.
199: *
200: *  The reciprocal condition number of the right invariant subspace
201: *  spanned by the first n1 columns of Z (or of Q*Z) is returned in SEP.
202: *  SEP is defined as the separation of T11 and T22:
203: *
204: *                     sep( T11, T22 ) = sigma-min( C )
205: *
206: *  where sigma-min(C) is the smallest singular value of the
207: *  n1*n2-by-n1*n2 matrix
208: *
209: *     C  = kprod( I(n2), T11 ) - kprod( transpose(T22), I(n1) )
210: *
211: *  I(m) is an m by m identity matrix, and kprod denotes the Kronecker
212: *  product. We estimate sigma-min(C) by the reciprocal of an estimate of
213: *  the 1-norm of inverse(C). The true reciprocal 1-norm of inverse(C)
214: *  cannot differ from sigma-min(C) by more than a factor of sqrt(n1*n2).
215: *
216: *  When SEP is small, small changes in T can cause large changes in
217: *  the invariant subspace. An approximate bound on the maximum angular
218: *  error in the computed right invariant subspace is
219: *
220: *                      EPS * norm(T) / SEP
221: *
222: *  =====================================================================
223: *
224: *     .. Parameters ..
225:       DOUBLE PRECISION   ZERO, ONE
226:       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+0 )
227: *     ..
228: *     .. Local Scalars ..
229:       LOGICAL            LQUERY, PAIR, SWAP, WANTBH, WANTQ, WANTS,
230:      $                   WANTSP
231:       INTEGER            IERR, K, KASE, KK, KS, LIWMIN, LWMIN, N1, N2,
232:      $                   NN
233:       DOUBLE PRECISION   EST, RNORM, SCALE
234: *     ..
235: *     .. Local Arrays ..
236:       INTEGER            ISAVE( 3 )
237: *     ..
238: *     .. External Functions ..
239:       LOGICAL            LSAME
240:       DOUBLE PRECISION   DLANGE
241:       EXTERNAL           LSAME, DLANGE
242: *     ..
243: *     .. External Subroutines ..
244:       EXTERNAL           DLACN2, DLACPY, DTREXC, DTRSYL, XERBLA
245: *     ..
246: *     .. Intrinsic Functions ..
247:       INTRINSIC          ABS, MAX, SQRT
248: *     ..
249: *     .. Executable Statements ..
250: *
251: *     Decode and test the input parameters
252: *
253:       WANTBH = LSAME( JOB, 'B' )
254:       WANTS = LSAME( JOB, 'E' ) .OR. WANTBH
255:       WANTSP = LSAME( JOB, 'V' ) .OR. WANTBH
256:       WANTQ = LSAME( COMPQ, 'V' )
257: *
258:       INFO = 0
259:       LQUERY = ( LWORK.EQ.-1 )
260:       IF( .NOT.LSAME( JOB, 'N' ) .AND. .NOT.WANTS .AND. .NOT.WANTSP )
261:      $     THEN
262:          INFO = -1
263:       ELSE IF( .NOT.LSAME( COMPQ, 'N' ) .AND. .NOT.WANTQ ) THEN
264:          INFO = -2
265:       ELSE IF( N.LT.0 ) THEN
266:          INFO = -4
267:       ELSE IF( LDT.LT.MAX( 1, N ) ) THEN
268:          INFO = -6
269:       ELSE IF( LDQ.LT.1 .OR. ( WANTQ .AND. LDQ.LT.N ) ) THEN
270:          INFO = -8
271:       ELSE
272: *
273: *        Set M to the dimension of the specified invariant subspace,
274: *        and test LWORK and LIWORK.
275: *
276:          M = 0
277:          PAIR = .FALSE.
278:          DO 10 K = 1, N
279:             IF( PAIR ) THEN
280:                PAIR = .FALSE.
281:             ELSE
282:                IF( K.LT.N ) THEN
283:                   IF( T( K+1, K ).EQ.ZERO ) THEN
284:                      IF( SELECT( K ) )
285:      $                  M = M + 1
286:                   ELSE
287:                      PAIR = .TRUE.
288:                      IF( SELECT( K ) .OR. SELECT( K+1 ) )
289:      $                  M = M + 2
290:                   END IF
291:                ELSE
292:                   IF( SELECT( N ) )
293:      $               M = M + 1
294:                END IF
295:             END IF
296:    10    CONTINUE
297: *
298:          N1 = M
299:          N2 = N - M
300:          NN = N1*N2
301: *
302:          IF( WANTSP ) THEN
303:             LWMIN = MAX( 1, 2*NN )
304:             LIWMIN = MAX( 1, NN )
305:          ELSE IF( LSAME( JOB, 'N' ) ) THEN
306:             LWMIN = MAX( 1, N )
307:             LIWMIN = 1
308:          ELSE IF( LSAME( JOB, 'E' ) ) THEN
309:             LWMIN = MAX( 1, NN )
310:             LIWMIN = 1
311:          END IF
312: *
313:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
314:             INFO = -15
315:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
316:             INFO = -17
317:          END IF
318:       END IF
319: *
320:       IF( INFO.EQ.0 ) THEN
321:          WORK( 1 ) = LWMIN
322:          IWORK( 1 ) = LIWMIN
323:       END IF
324: *
325:       IF( INFO.NE.0 ) THEN
326:          CALL XERBLA( 'DTRSEN', -INFO )
327:          RETURN
328:       ELSE IF( LQUERY ) THEN
329:          RETURN
330:       END IF
331: *
332: *     Quick return if possible.
333: *
334:       IF( M.EQ.N .OR. M.EQ.0 ) THEN
335:          IF( WANTS )
336:      $      S = ONE
337:          IF( WANTSP )
338:      $      SEP = DLANGE( '1', N, N, T, LDT, WORK )
339:          GO TO 40
340:       END IF
341: *
342: *     Collect the selected blocks at the top-left corner of T.
343: *
344:       KS = 0
345:       PAIR = .FALSE.
346:       DO 20 K = 1, N
347:          IF( PAIR ) THEN
348:             PAIR = .FALSE.
349:          ELSE
350:             SWAP = SELECT( K )
351:             IF( K.LT.N ) THEN
352:                IF( T( K+1, K ).NE.ZERO ) THEN
353:                   PAIR = .TRUE.
354:                   SWAP = SWAP .OR. SELECT( K+1 )
355:                END IF
356:             END IF
357:             IF( SWAP ) THEN
358:                KS = KS + 1
359: *
360: *              Swap the K-th block to position KS.
361: *
362:                IERR = 0
363:                KK = K
364:                IF( K.NE.KS )
365:      $            CALL DTREXC( COMPQ, N, T, LDT, Q, LDQ, KK, KS, WORK,
366:      $                         IERR )
367:                IF( IERR.EQ.1 .OR. IERR.EQ.2 ) THEN
368: *
369: *                 Blocks too close to swap: exit.
370: *
371:                   INFO = 1
372:                   IF( WANTS )
373:      $               S = ZERO
374:                   IF( WANTSP )
375:      $               SEP = ZERO
376:                   GO TO 40
377:                END IF
378:                IF( PAIR )
379:      $            KS = KS + 1
380:             END IF
381:          END IF
382:    20 CONTINUE
383: *
384:       IF( WANTS ) THEN
385: *
386: *        Solve Sylvester equation for R:
387: *
388: *           T11*R - R*T22 = scale*T12
389: *
390:          CALL DLACPY( 'F', N1, N2, T( 1, N1+1 ), LDT, WORK, N1 )
391:          CALL DTRSYL( 'N', 'N', -1, N1, N2, T, LDT, T( N1+1, N1+1 ),
392:      $                LDT, WORK, N1, SCALE, IERR )
393: *
394: *        Estimate the reciprocal of the condition number of the cluster
395: *        of eigenvalues.
396: *
397:          RNORM = DLANGE( 'F', N1, N2, WORK, N1, WORK )
398:          IF( RNORM.EQ.ZERO ) THEN
399:             S = ONE
400:          ELSE
401:             S = SCALE / ( SQRT( SCALE*SCALE / RNORM+RNORM )*
402:      $          SQRT( RNORM ) )
403:          END IF
404:       END IF
405: *
406:       IF( WANTSP ) THEN
407: *
408: *        Estimate sep(T11,T22).
409: *
410:          EST = ZERO
411:          KASE = 0
412:    30    CONTINUE
413:          CALL DLACN2( NN, WORK( NN+1 ), WORK, IWORK, EST, KASE, ISAVE )
414:          IF( KASE.NE.0 ) THEN
415:             IF( KASE.EQ.1 ) THEN
416: *
417: *              Solve  T11*R - R*T22 = scale*X.
418: *
419:                CALL DTRSYL( 'N', 'N', -1, N1, N2, T, LDT,
420:      $                      T( N1+1, N1+1 ), LDT, WORK, N1, SCALE,
421:      $                      IERR )
422:             ELSE
423: *
424: *              Solve  T11'*R - R*T22' = scale*X.
425: *
426:                CALL DTRSYL( 'T', 'T', -1, N1, N2, T, LDT,
427:      $                      T( N1+1, N1+1 ), LDT, WORK, N1, SCALE,
428:      $                      IERR )
429:             END IF
430:             GO TO 30
431:          END IF
432: *
433:          SEP = SCALE / EST
434:       END IF
435: *
436:    40 CONTINUE
437: *
438: *     Store the output eigenvalues in WR and WI.
439: *
440:       DO 50 K = 1, N
441:          WR( K ) = T( K, K )
442:          WI( K ) = ZERO
443:    50 CONTINUE
444:       DO 60 K = 1, N - 1
445:          IF( T( K+1, K ).NE.ZERO ) THEN
446:             WI( K ) = SQRT( ABS( T( K, K+1 ) ) )*
447:      $                SQRT( ABS( T( K+1, K ) ) )
448:             WI( K+1 ) = -WI( K )
449:          END IF
450:    60 CONTINUE
451: *
452:       WORK( 1 ) = LWMIN
453:       IWORK( 1 ) = LIWMIN
454: *
455:       RETURN
456: *
457: *     End of DTRSEN
458: *
459:       END
460: