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