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