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