001:       SUBROUTINE CTGSJA( JOBU, JOBV, JOBQ, M, P, N, K, L, A, LDA, B,
002:      $                   LDB, TOLA, TOLB, ALPHA, BETA, U, LDU, V, LDV,
003:      $                   Q, LDQ, WORK, NCYCLE, INFO )
004: *
005: *  -- LAPACK routine (version 3.2) --
006: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       CHARACTER          JOBQ, JOBU, JOBV
011:       INTEGER            INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N,
012:      $                   NCYCLE, P
013:       REAL               TOLA, TOLB
014: *     ..
015: *     .. Array Arguments ..
016:       REAL               ALPHA( * ), BETA( * )
017:       COMPLEX            A( LDA, * ), B( LDB, * ), Q( LDQ, * ),
018:      $                   U( LDU, * ), V( LDV, * ), WORK( * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  CTGSJA computes the generalized singular value decomposition (GSVD)
025: *  of two complex upper triangular (or trapezoidal) matrices A and B.
026: *
027: *  On entry, it is assumed that matrices A and B have the following
028: *  forms, which may be obtained by the preprocessing subroutine CGGSVP
029: *  from a general M-by-N matrix A and P-by-N matrix B:
030: *
031: *               N-K-L  K    L
032: *     A =    K ( 0    A12  A13 ) if M-K-L >= 0;
033: *            L ( 0     0   A23 )
034: *        M-K-L ( 0     0    0  )
035: *
036: *             N-K-L  K    L
037: *     A =  K ( 0    A12  A13 ) if M-K-L < 0;
038: *        M-K ( 0     0   A23 )
039: *
040: *             N-K-L  K    L
041: *     B =  L ( 0     0   B13 )
042: *        P-L ( 0     0    0  )
043: *
044: *  where the K-by-K matrix A12 and L-by-L matrix B13 are nonsingular
045: *  upper triangular; A23 is L-by-L upper triangular if M-K-L >= 0,
046: *  otherwise A23 is (M-K)-by-L upper trapezoidal.
047: *
048: *  On exit,
049: *
050: *         U'*A*Q = D1*( 0 R ),    V'*B*Q = D2*( 0 R ),
051: *
052: *  where U, V and Q are unitary matrices, Z' denotes the conjugate
053: *  transpose of Z, R is a nonsingular upper triangular matrix, and D1
054: *  and D2 are ``diagonal'' matrices, which are of the following
055: *  structures:
056: *
057: *  If M-K-L >= 0,
058: *
059: *                      K  L
060: *         D1 =     K ( I  0 )
061: *                  L ( 0  C )
062: *              M-K-L ( 0  0 )
063: *
064: *                     K  L
065: *         D2 = L   ( 0  S )
066: *              P-L ( 0  0 )
067: *
068: *                 N-K-L  K    L
069: *    ( 0 R ) = K (  0   R11  R12 ) K
070: *              L (  0    0   R22 ) L
071: *
072: *  where
073: *
074: *    C = diag( ALPHA(K+1), ... , ALPHA(K+L) ),
075: *    S = diag( BETA(K+1),  ... , BETA(K+L) ),
076: *    C**2 + S**2 = I.
077: *
078: *    R is stored in A(1:K+L,N-K-L+1:N) on exit.
079: *
080: *  If M-K-L < 0,
081: *
082: *                 K M-K K+L-M
083: *      D1 =   K ( I  0    0   )
084: *           M-K ( 0  C    0   )
085: *
086: *                   K M-K K+L-M
087: *      D2 =   M-K ( 0  S    0   )
088: *           K+L-M ( 0  0    I   )
089: *             P-L ( 0  0    0   )
090: *
091: *                 N-K-L  K   M-K  K+L-M
092: * ( 0 R ) =    K ( 0    R11  R12  R13  )
093: *            M-K ( 0     0   R22  R23  )
094: *          K+L-M ( 0     0    0   R33  )
095: *
096: *  where
097: *  C = diag( ALPHA(K+1), ... , ALPHA(M) ),
098: *  S = diag( BETA(K+1),  ... , BETA(M) ),
099: *  C**2 + S**2 = I.
100: *
101: *  R = ( R11 R12 R13 ) is stored in A(1:M, N-K-L+1:N) and R33 is stored
102: *      (  0  R22 R23 )
103: *  in B(M-K+1:L,N+M-K-L+1:N) on exit.
104: *
105: *  The computation of the unitary transformation matrices U, V or Q
106: *  is optional.  These matrices may either be formed explicitly, or they
107: *  may be postmultiplied into input matrices U1, V1, or Q1.
108: *
109: *  Arguments
110: *  =========
111: *
112: *  JOBU    (input) CHARACTER*1
113: *          = 'U':  U must contain a unitary matrix U1 on entry, and
114: *                  the product U1*U is returned;
115: *          = 'I':  U is initialized to the unit matrix, and the
116: *                  unitary matrix U is returned;
117: *          = 'N':  U is not computed.
118: *
119: *  JOBV    (input) CHARACTER*1
120: *          = 'V':  V must contain a unitary matrix V1 on entry, and
121: *                  the product V1*V is returned;
122: *          = 'I':  V is initialized to the unit matrix, and the
123: *                  unitary matrix V is returned;
124: *          = 'N':  V is not computed.
125: *
126: *  JOBQ    (input) CHARACTER*1
127: *          = 'Q':  Q must contain a unitary matrix Q1 on entry, and
128: *                  the product Q1*Q is returned;
129: *          = 'I':  Q is initialized to the unit matrix, and the
130: *                  unitary matrix Q is returned;
131: *          = 'N':  Q is not computed.
132: *
133: *  M       (input) INTEGER
134: *          The number of rows of the matrix A.  M >= 0.
135: *
136: *  P       (input) INTEGER
137: *          The number of rows of the matrix B.  P >= 0.
138: *
139: *  N       (input) INTEGER
140: *          The number of columns of the matrices A and B.  N >= 0.
141: *
142: *  K       (input) INTEGER
143: *  L       (input) INTEGER
144: *          K and L specify the subblocks in the input matrices A and B:
145: *          A23 = A(K+1:MIN(K+L,M),N-L+1:N) and B13 = B(1:L,,N-L+1:N)
146: *          of A and B, whose GSVD is going to be computed by CTGSJA.
147: *          See Further details.
148: *
149: *  A       (input/output) COMPLEX array, dimension (LDA,N)
150: *          On entry, the M-by-N matrix A.
151: *          On exit, A(N-K+1:N,1:MIN(K+L,M) ) contains the triangular
152: *          matrix R or part of R.  See Purpose for details.
153: *
154: *  LDA     (input) INTEGER
155: *          The leading dimension of the array A. LDA >= max(1,M).
156: *
157: *  B       (input/output) COMPLEX array, dimension (LDB,N)
158: *          On entry, the P-by-N matrix B.
159: *          On exit, if necessary, B(M-K+1:L,N+M-K-L+1:N) contains
160: *          a part of R.  See Purpose for details.
161: *
162: *  LDB     (input) INTEGER
163: *          The leading dimension of the array B. LDB >= max(1,P).
164: *
165: *  TOLA    (input) REAL
166: *  TOLB    (input) REAL
167: *          TOLA and TOLB are the convergence criteria for the Jacobi-
168: *          Kogbetliantz iteration procedure. Generally, they are the
169: *          same as used in the preprocessing step, say
170: *              TOLA = MAX(M,N)*norm(A)*MACHEPS,
171: *              TOLB = MAX(P,N)*norm(B)*MACHEPS.
172: *
173: *  ALPHA   (output) REAL array, dimension (N)
174: *  BETA    (output) REAL array, dimension (N)
175: *          On exit, ALPHA and BETA contain the generalized singular
176: *          value pairs of A and B;
177: *            ALPHA(1:K) = 1,
178: *            BETA(1:K)  = 0,
179: *          and if M-K-L >= 0,
180: *            ALPHA(K+1:K+L) = diag(C),
181: *            BETA(K+1:K+L)  = diag(S),
182: *          or if M-K-L < 0,
183: *            ALPHA(K+1:M)= C, ALPHA(M+1:K+L)= 0
184: *            BETA(K+1:M) = S, BETA(M+1:K+L) = 1.
185: *          Furthermore, if K+L < N,
186: *            ALPHA(K+L+1:N) = 0
187: *            BETA(K+L+1:N)  = 0.
188: *
189: *  U       (input/output) COMPLEX array, dimension (LDU,M)
190: *          On entry, if JOBU = 'U', U must contain a matrix U1 (usually
191: *          the unitary matrix returned by CGGSVP).
192: *          On exit,
193: *          if JOBU = 'I', U contains the unitary matrix U;
194: *          if JOBU = 'U', U contains the product U1*U.
195: *          If JOBU = 'N', U is not referenced.
196: *
197: *  LDU     (input) INTEGER
198: *          The leading dimension of the array U. LDU >= max(1,M) if
199: *          JOBU = 'U'; LDU >= 1 otherwise.
200: *
201: *  V       (input/output) COMPLEX array, dimension (LDV,P)
202: *          On entry, if JOBV = 'V', V must contain a matrix V1 (usually
203: *          the unitary matrix returned by CGGSVP).
204: *          On exit,
205: *          if JOBV = 'I', V contains the unitary matrix V;
206: *          if JOBV = 'V', V contains the product V1*V.
207: *          If JOBV = 'N', V is not referenced.
208: *
209: *  LDV     (input) INTEGER
210: *          The leading dimension of the array V. LDV >= max(1,P) if
211: *          JOBV = 'V'; LDV >= 1 otherwise.
212: *
213: *  Q       (input/output) COMPLEX array, dimension (LDQ,N)
214: *          On entry, if JOBQ = 'Q', Q must contain a matrix Q1 (usually
215: *          the unitary matrix returned by CGGSVP).
216: *          On exit,
217: *          if JOBQ = 'I', Q contains the unitary matrix Q;
218: *          if JOBQ = 'Q', Q contains the product Q1*Q.
219: *          If JOBQ = 'N', Q is not referenced.
220: *
221: *  LDQ     (input) INTEGER
222: *          The leading dimension of the array Q. LDQ >= max(1,N) if
223: *          JOBQ = 'Q'; LDQ >= 1 otherwise.
224: *
225: *  WORK    (workspace) COMPLEX array, dimension (2*N)
226: *
227: *  NCYCLE  (output) INTEGER
228: *          The number of cycles required for convergence.
229: *
230: *  INFO    (output) INTEGER
231: *          = 0:  successful exit
232: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
233: *          = 1:  the procedure does not converge after MAXIT cycles.
234: *
235: *  Internal Parameters
236: *  ===================
237: *
238: *  MAXIT   INTEGER
239: *          MAXIT specifies the total loops that the iterative procedure
240: *          may take. If after MAXIT cycles, the routine fails to
241: *          converge, we return INFO = 1.
242: *
243: *  Further Details
244: *  ===============
245: *
246: *  CTGSJA essentially uses a variant of Kogbetliantz algorithm to reduce
247: *  min(L,M-K)-by-L triangular (or trapezoidal) matrix A23 and L-by-L
248: *  matrix B13 to the form:
249: *
250: *           U1'*A13*Q1 = C1*R1; V1'*B13*Q1 = S1*R1,
251: *
252: *  where U1, V1 and Q1 are unitary matrix, and Z' is the conjugate
253: *  transpose of Z.  C1 and S1 are diagonal matrices satisfying
254: *
255: *                C1**2 + S1**2 = I,
256: *
257: *  and R1 is an L-by-L nonsingular upper triangular matrix.
258: *
259: *  =====================================================================
260: *
261: *     .. Parameters ..
262:       INTEGER            MAXIT
263:       PARAMETER          ( MAXIT = 40 )
264:       REAL               ZERO, ONE
265:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
266:       COMPLEX            CZERO, CONE
267:       PARAMETER          ( CZERO = ( 0.0E+0, 0.0E+0 ),
268:      $                   CONE = ( 1.0E+0, 0.0E+0 ) )
269: *     ..
270: *     .. Local Scalars ..
271: *
272:       LOGICAL            INITQ, INITU, INITV, UPPER, WANTQ, WANTU, WANTV
273:       INTEGER            I, J, KCYCLE
274:       REAL               A1, A3, B1, B3, CSQ, CSU, CSV, ERROR, GAMMA,
275:      $                   RWK, SSMIN
276:       COMPLEX            A2, B2, SNQ, SNU, SNV
277: *     ..
278: *     .. External Functions ..
279:       LOGICAL            LSAME
280:       EXTERNAL           LSAME
281: *     ..
282: *     .. External Subroutines ..
283:       EXTERNAL           CCOPY, CLAGS2, CLAPLL, CLASET, CROT, CSSCAL,
284:      $                   SLARTG, XERBLA
285: *     ..
286: *     .. Intrinsic Functions ..
287:       INTRINSIC          ABS, CONJG, MAX, MIN, REAL
288: *     ..
289: *     .. Executable Statements ..
290: *
291: *     Decode and test the input parameters
292: *
293:       INITU = LSAME( JOBU, 'I' )
294:       WANTU = INITU .OR. LSAME( JOBU, 'U' )
295: *
296:       INITV = LSAME( JOBV, 'I' )
297:       WANTV = INITV .OR. LSAME( JOBV, 'V' )
298: *
299:       INITQ = LSAME( JOBQ, 'I' )
300:       WANTQ = INITQ .OR. LSAME( JOBQ, 'Q' )
301: *
302:       INFO = 0
303:       IF( .NOT.( INITU .OR. WANTU .OR. LSAME( JOBU, 'N' ) ) ) THEN
304:          INFO = -1
305:       ELSE IF( .NOT.( INITV .OR. WANTV .OR. LSAME( JOBV, 'N' ) ) ) THEN
306:          INFO = -2
307:       ELSE IF( .NOT.( INITQ .OR. WANTQ .OR. LSAME( JOBQ, 'N' ) ) ) THEN
308:          INFO = -3
309:       ELSE IF( M.LT.0 ) THEN
310:          INFO = -4
311:       ELSE IF( P.LT.0 ) THEN
312:          INFO = -5
313:       ELSE IF( N.LT.0 ) THEN
314:          INFO = -6
315:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
316:          INFO = -10
317:       ELSE IF( LDB.LT.MAX( 1, P ) ) THEN
318:          INFO = -12
319:       ELSE IF( LDU.LT.1 .OR. ( WANTU .AND. LDU.LT.M ) ) THEN
320:          INFO = -18
321:       ELSE IF( LDV.LT.1 .OR. ( WANTV .AND. LDV.LT.P ) ) THEN
322:          INFO = -20
323:       ELSE IF( LDQ.LT.1 .OR. ( WANTQ .AND. LDQ.LT.N ) ) THEN
324:          INFO = -22
325:       END IF
326:       IF( INFO.NE.0 ) THEN
327:          CALL XERBLA( 'CTGSJA', -INFO )
328:          RETURN
329:       END IF
330: *
331: *     Initialize U, V and Q, if necessary
332: *
333:       IF( INITU )
334:      $   CALL CLASET( 'Full', M, M, CZERO, CONE, U, LDU )
335:       IF( INITV )
336:      $   CALL CLASET( 'Full', P, P, CZERO, CONE, V, LDV )
337:       IF( INITQ )
338:      $   CALL CLASET( 'Full', N, N, CZERO, CONE, Q, LDQ )
339: *
340: *     Loop until convergence
341: *
342:       UPPER = .FALSE.
343:       DO 40 KCYCLE = 1, MAXIT
344: *
345:          UPPER = .NOT.UPPER
346: *
347:          DO 20 I = 1, L - 1
348:             DO 10 J = I + 1, L
349: *
350:                A1 = ZERO
351:                A2 = CZERO
352:                A3 = ZERO
353:                IF( K+I.LE.M )
354:      $            A1 = REAL( A( K+I, N-L+I ) )
355:                IF( K+J.LE.M )
356:      $            A3 = REAL( A( K+J, N-L+J ) )
357: *
358:                B1 = REAL( B( I, N-L+I ) )
359:                B3 = REAL( B( J, N-L+J ) )
360: *
361:                IF( UPPER ) THEN
362:                   IF( K+I.LE.M )
363:      $               A2 = A( K+I, N-L+J )
364:                   B2 = B( I, N-L+J )
365:                ELSE
366:                   IF( K+J.LE.M )
367:      $               A2 = A( K+J, N-L+I )
368:                   B2 = B( J, N-L+I )
369:                END IF
370: *
371:                CALL CLAGS2( UPPER, A1, A2, A3, B1, B2, B3, CSU, SNU,
372:      $                      CSV, SNV, CSQ, SNQ )
373: *
374: *              Update (K+I)-th and (K+J)-th rows of matrix A: U'*A
375: *
376:                IF( K+J.LE.M )
377:      $            CALL CROT( L, A( K+J, N-L+1 ), LDA, A( K+I, N-L+1 ),
378:      $                       LDA, CSU, CONJG( SNU ) )
379: *
380: *              Update I-th and J-th rows of matrix B: V'*B
381: *
382:                CALL CROT( L, B( J, N-L+1 ), LDB, B( I, N-L+1 ), LDB,
383:      $                    CSV, CONJG( SNV ) )
384: *
385: *              Update (N-L+I)-th and (N-L+J)-th columns of matrices
386: *              A and B: A*Q and B*Q
387: *
388:                CALL CROT( MIN( K+L, M ), A( 1, N-L+J ), 1,
389:      $                    A( 1, N-L+I ), 1, CSQ, SNQ )
390: *
391:                CALL CROT( L, B( 1, N-L+J ), 1, B( 1, N-L+I ), 1, CSQ,
392:      $                    SNQ )
393: *
394:                IF( UPPER ) THEN
395:                   IF( K+I.LE.M )
396:      $               A( K+I, N-L+J ) = CZERO
397:                   B( I, N-L+J ) = CZERO
398:                ELSE
399:                   IF( K+J.LE.M )
400:      $               A( K+J, N-L+I ) = CZERO
401:                   B( J, N-L+I ) = CZERO
402:                END IF
403: *
404: *              Ensure that the diagonal elements of A and B are real.
405: *
406:                IF( K+I.LE.M )
407:      $            A( K+I, N-L+I ) = REAL( A( K+I, N-L+I ) )
408:                IF( K+J.LE.M )
409:      $            A( K+J, N-L+J ) = REAL( A( K+J, N-L+J ) )
410:                B( I, N-L+I ) = REAL( B( I, N-L+I ) )
411:                B( J, N-L+J ) = REAL( B( J, N-L+J ) )
412: *
413: *              Update unitary matrices U, V, Q, if desired.
414: *
415:                IF( WANTU .AND. K+J.LE.M )
416:      $            CALL CROT( M, U( 1, K+J ), 1, U( 1, K+I ), 1, CSU,
417:      $                       SNU )
418: *
419:                IF( WANTV )
420:      $            CALL CROT( P, V( 1, J ), 1, V( 1, I ), 1, CSV, SNV )
421: *
422:                IF( WANTQ )
423:      $            CALL CROT( N, Q( 1, N-L+J ), 1, Q( 1, N-L+I ), 1, CSQ,
424:      $                       SNQ )
425: *
426:    10       CONTINUE
427:    20    CONTINUE
428: *
429:          IF( .NOT.UPPER ) THEN
430: *
431: *           The matrices A13 and B13 were lower triangular at the start
432: *           of the cycle, and are now upper triangular.
433: *
434: *           Convergence test: test the parallelism of the corresponding
435: *           rows of A and B.
436: *
437:             ERROR = ZERO
438:             DO 30 I = 1, MIN( L, M-K )
439:                CALL CCOPY( L-I+1, A( K+I, N-L+I ), LDA, WORK, 1 )
440:                CALL CCOPY( L-I+1, B( I, N-L+I ), LDB, WORK( L+1 ), 1 )
441:                CALL CLAPLL( L-I+1, WORK, 1, WORK( L+1 ), 1, SSMIN )
442:                ERROR = MAX( ERROR, SSMIN )
443:    30       CONTINUE
444: *
445:             IF( ABS( ERROR ).LE.MIN( TOLA, TOLB ) )
446:      $         GO TO 50
447:          END IF
448: *
449: *        End of cycle loop
450: *
451:    40 CONTINUE
452: *
453: *     The algorithm has not converged after MAXIT cycles.
454: *
455:       INFO = 1
456:       GO TO 100
457: *
458:    50 CONTINUE
459: *
460: *     If ERROR <= MIN(TOLA,TOLB), then the algorithm has converged.
461: *     Compute the generalized singular value pairs (ALPHA, BETA), and
462: *     set the triangular matrix R to array A.
463: *
464:       DO 60 I = 1, K
465:          ALPHA( I ) = ONE
466:          BETA( I ) = ZERO
467:    60 CONTINUE
468: *
469:       DO 70 I = 1, MIN( L, M-K )
470: *
471:          A1 = REAL( A( K+I, N-L+I ) )
472:          B1 = REAL( B( I, N-L+I ) )
473: *
474:          IF( A1.NE.ZERO ) THEN
475:             GAMMA = B1 / A1
476: *
477:             IF( GAMMA.LT.ZERO ) THEN
478:                CALL CSSCAL( L-I+1, -ONE, B( I, N-L+I ), LDB )
479:                IF( WANTV )
480:      $            CALL CSSCAL( P, -ONE, V( 1, I ), 1 )
481:             END IF
482: *
483:             CALL SLARTG( ABS( GAMMA ), ONE, BETA( K+I ), ALPHA( K+I ),
484:      $                   RWK )
485: *
486:             IF( ALPHA( K+I ).GE.BETA( K+I ) ) THEN
487:                CALL CSSCAL( L-I+1, ONE / ALPHA( K+I ), A( K+I, N-L+I ),
488:      $                      LDA )
489:             ELSE
490:                CALL CSSCAL( L-I+1, ONE / BETA( K+I ), B( I, N-L+I ),
491:      $                      LDB )
492:                CALL CCOPY( L-I+1, B( I, N-L+I ), LDB, A( K+I, N-L+I ),
493:      $                     LDA )
494:             END IF
495: *
496:          ELSE
497:             ALPHA( K+I ) = ZERO
498:             BETA( K+I ) = ONE
499:             CALL CCOPY( L-I+1, B( I, N-L+I ), LDB, A( K+I, N-L+I ),
500:      $                  LDA )
501:          END IF
502:    70 CONTINUE
503: *
504: *     Post-assignment
505: *
506:       DO 80 I = M + 1, K + L
507:          ALPHA( I ) = ZERO
508:          BETA( I ) = ONE
509:    80 CONTINUE
510: *
511:       IF( K+L.LT.N ) THEN
512:          DO 90 I = K + L + 1, N
513:             ALPHA( I ) = ZERO
514:             BETA( I ) = ZERO
515:    90    CONTINUE
516:       END IF
517: *
518:   100 CONTINUE
519:       NCYCLE = KCYCLE
520: *
521:       RETURN
522: *
523: *     End of CTGSJA
524: *
525:       END
526: