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