3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download STGSJA + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/stgsja.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/stgsja.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/stgsja.f">
21 * SUBROUTINE STGSJA( JOBU, JOBV, JOBQ, M, P, N, K, L, A, LDA, B,
22 * LDB, TOLA, TOLB, ALPHA, BETA, U, LDU, V, LDV,
23 * Q, LDQ, WORK, NCYCLE, INFO )
25 * .. Scalar Arguments ..
26 * CHARACTER JOBQ, JOBU, JOBV
27 * INTEGER INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N,
31 * .. Array Arguments ..
32 * REAL A( LDA, * ), ALPHA( * ), B( LDB, * ),
33 * $ BETA( * ), Q( LDQ, * ), U( LDU, * ),
34 * $ V( LDV, * ), WORK( * )
43 *> STGSJA computes the generalized singular value decomposition (GSVD)
44 *> of two real upper triangular (or trapezoidal) matrices A and B.
46 *> On entry, it is assumed that matrices A and B have the following
47 *> forms, which may be obtained by the preprocessing subroutine SGGSVP
48 *> from a general M-by-N matrix A and P-by-N matrix B:
51 *> A = K ( 0 A12 A13 ) if M-K-L >= 0;
56 *> A = K ( 0 A12 A13 ) if M-K-L < 0;
63 *> where the K-by-K matrix A12 and L-by-L matrix B13 are nonsingular
64 *> upper triangular; A23 is L-by-L upper triangular if M-K-L >= 0,
65 *> otherwise A23 is (M-K)-by-L upper trapezoidal.
69 *> U**T *A*Q = D1*( 0 R ), V**T *B*Q = D2*( 0 R ),
71 *> where U, V and Q are orthogonal matrices.
72 *> R is a nonsingular upper triangular matrix, and D1 and D2 are
73 *> ``diagonal'' matrices, which are of the following structures:
87 *> ( 0 R ) = K ( 0 R11 R12 ) K
92 *> C = diag( ALPHA(K+1), ... , ALPHA(K+L) ),
93 *> S = diag( BETA(K+1), ... , BETA(K+L) ),
96 *> R is stored in A(1:K+L,N-K-L+1:N) on exit.
105 *> D2 = M-K ( 0 S 0 )
110 *> ( 0 R ) = K ( 0 R11 R12 R13 )
111 *> M-K ( 0 0 R22 R23 )
112 *> K+L-M ( 0 0 0 R33 )
115 *> C = diag( ALPHA(K+1), ... , ALPHA(M) ),
116 *> S = diag( BETA(K+1), ... , BETA(M) ),
119 *> R = ( R11 R12 R13 ) is stored in A(1:M, N-K-L+1:N) and R33 is stored
121 *> in B(M-K+1:L,N+M-K-L+1:N) on exit.
123 *> The computation of the orthogonal transformation matrices U, V or Q
124 *> is optional. These matrices may either be formed explicitly, or they
125 *> may be postmultiplied into input matrices U1, V1, or Q1.
133 *> JOBU is CHARACTER*1
134 *> = 'U': U must contain an orthogonal matrix U1 on entry, and
135 *> the product U1*U is returned;
136 *> = 'I': U is initialized to the unit matrix, and the
137 *> orthogonal matrix U is returned;
138 *> = 'N': U is not computed.
143 *> JOBV is CHARACTER*1
144 *> = 'V': V must contain an orthogonal matrix V1 on entry, and
145 *> the product V1*V is returned;
146 *> = 'I': V is initialized to the unit matrix, and the
147 *> orthogonal matrix V is returned;
148 *> = 'N': V is not computed.
153 *> JOBQ is CHARACTER*1
154 *> = 'Q': Q must contain an orthogonal matrix Q1 on entry, and
155 *> the product Q1*Q is returned;
156 *> = 'I': Q is initialized to the unit matrix, and the
157 *> orthogonal matrix Q is returned;
158 *> = 'N': Q is not computed.
164 *> The number of rows of the matrix A. M >= 0.
170 *> The number of rows of the matrix B. P >= 0.
176 *> The number of columns of the matrices A and B. N >= 0.
188 *> K and L specify the subblocks in the input matrices A and B:
189 *> A23 = A(K+1:MIN(K+L,M),N-L+1:N) and B13 = B(1:L,N-L+1:N)
190 *> of A and B, whose GSVD is going to be computed by STGSJA.
191 *> See Further Details.
196 *> A is REAL array, dimension (LDA,N)
197 *> On entry, the M-by-N matrix A.
198 *> On exit, A(N-K+1:N,1:MIN(K+L,M) ) contains the triangular
199 *> matrix R or part of R. See Purpose for details.
205 *> The leading dimension of the array A. LDA >= max(1,M).
210 *> B is REAL array, dimension (LDB,N)
211 *> On entry, the P-by-N matrix B.
212 *> On exit, if necessary, B(M-K+1:L,N+M-K-L+1:N) contains
213 *> a part of R. See Purpose for details.
219 *> The leading dimension of the array B. LDB >= max(1,P).
231 *> TOLA and TOLB are the convergence criteria for the Jacobi-
232 *> Kogbetliantz iteration procedure. Generally, they are the
233 *> same as used in the preprocessing step, say
234 *> TOLA = max(M,N)*norm(A)*MACHEPS,
235 *> TOLB = max(P,N)*norm(B)*MACHEPS.
240 *> ALPHA is REAL array, dimension (N)
245 *> BETA is REAL array, dimension (N)
247 *> On exit, ALPHA and BETA contain the generalized singular
248 *> value pairs of A and B;
251 *> and if M-K-L >= 0,
252 *> ALPHA(K+1:K+L) = diag(C),
253 *> BETA(K+1:K+L) = diag(S),
255 *> ALPHA(K+1:M)= C, ALPHA(M+1:K+L)= 0
256 *> BETA(K+1:M) = S, BETA(M+1:K+L) = 1.
257 *> Furthermore, if K+L < N,
258 *> ALPHA(K+L+1:N) = 0 and
259 *> BETA(K+L+1:N) = 0.
264 *> U is REAL array, dimension (LDU,M)
265 *> On entry, if JOBU = 'U', U must contain a matrix U1 (usually
266 *> the orthogonal matrix returned by SGGSVP).
268 *> if JOBU = 'I', U contains the orthogonal matrix U;
269 *> if JOBU = 'U', U contains the product U1*U.
270 *> If JOBU = 'N', U is not referenced.
276 *> The leading dimension of the array U. LDU >= max(1,M) if
277 *> JOBU = 'U'; LDU >= 1 otherwise.
282 *> V is REAL array, dimension (LDV,P)
283 *> On entry, if JOBV = 'V', V must contain a matrix V1 (usually
284 *> the orthogonal matrix returned by SGGSVP).
286 *> if JOBV = 'I', V contains the orthogonal matrix V;
287 *> if JOBV = 'V', V contains the product V1*V.
288 *> If JOBV = 'N', V is not referenced.
294 *> The leading dimension of the array V. LDV >= max(1,P) if
295 *> JOBV = 'V'; LDV >= 1 otherwise.
300 *> Q is REAL array, dimension (LDQ,N)
301 *> On entry, if JOBQ = 'Q', Q must contain a matrix Q1 (usually
302 *> the orthogonal matrix returned by SGGSVP).
304 *> if JOBQ = 'I', Q contains the orthogonal matrix Q;
305 *> if JOBQ = 'Q', Q contains the product Q1*Q.
306 *> If JOBQ = 'N', Q is not referenced.
312 *> The leading dimension of the array Q. LDQ >= max(1,N) if
313 *> JOBQ = 'Q'; LDQ >= 1 otherwise.
318 *> WORK is REAL array, dimension (2*N)
321 *> \param[out] NCYCLE
324 *> The number of cycles required for convergence.
330 *> = 0: successful exit
331 *> < 0: if INFO = -i, the i-th argument had an illegal value.
332 *> = 1: the procedure does not converge after MAXIT cycles.
336 *> Internal Parameters
337 *> ===================
340 *> MAXIT specifies the total loops that the iterative procedure
341 *> may take. If after MAXIT cycles, the routine fails to
342 *> converge, we return INFO = 1.
348 *> \author Univ. of Tennessee
349 *> \author Univ. of California Berkeley
350 *> \author Univ. of Colorado Denver
353 *> \date November 2011
355 *> \ingroup realOTHERcomputational
357 *> \par Further Details:
358 * =====================
362 *> STGSJA essentially uses a variant of Kogbetliantz algorithm to reduce
363 *> min(L,M-K)-by-L triangular (or trapezoidal) matrix A23 and L-by-L
364 *> matrix B13 to the form:
366 *> U1**T *A13*Q1 = C1*R1; V1**T *B13*Q1 = S1*R1,
368 *> where U1, V1 and Q1 are orthogonal matrix, and Z**T is the transpose
369 *> of Z. C1 and S1 are diagonal matrices satisfying
371 *> C1**2 + S1**2 = I,
373 *> and R1 is an L-by-L nonsingular upper triangular matrix.
376 * =====================================================================
377 SUBROUTINE STGSJA( JOBU, JOBV, JOBQ, M, P, N, K, L, A, LDA, B,
378 $ LDB, TOLA, TOLB, ALPHA, BETA, U, LDU, V, LDV,
379 $ Q, LDQ, WORK, NCYCLE, INFO )
381 * -- LAPACK computational routine (version 3.4.0) --
382 * -- LAPACK is a software package provided by Univ. of Tennessee, --
383 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
386 * .. Scalar Arguments ..
387 CHARACTER JOBQ, JOBU, JOBV
388 INTEGER INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N,
392 * .. Array Arguments ..
393 REAL A( LDA, * ), ALPHA( * ), B( LDB, * ),
394 $ BETA( * ), Q( LDQ, * ), U( LDU, * ),
395 $ V( LDV, * ), WORK( * )
398 * =====================================================================
402 PARAMETER ( MAXIT = 40 )
404 PARAMETER ( ZERO = 0.0E+0, ONE = 1.0E+0 )
406 * .. Local Scalars ..
408 LOGICAL INITQ, INITU, INITV, UPPER, WANTQ, WANTU, WANTV
410 REAL A1, A2, A3, B1, B2, B3, CSQ, CSU, CSV, ERROR,
411 $ GAMMA, RWK, SNQ, SNU, SNV, SSMIN
413 * .. External Functions ..
417 * .. External Subroutines ..
418 EXTERNAL SCOPY, SLAGS2, SLAPLL, SLARTG, SLASET, SROT,
421 * .. Intrinsic Functions ..
422 INTRINSIC ABS, MAX, MIN
424 * .. Executable Statements ..
426 * Decode and test the input parameters
428 INITU = LSAME( JOBU, 'I' )
429 WANTU = INITU .OR. LSAME( JOBU, 'U' )
431 INITV = LSAME( JOBV, 'I' )
432 WANTV = INITV .OR. LSAME( JOBV, 'V' )
434 INITQ = LSAME( JOBQ, 'I' )
435 WANTQ = INITQ .OR. LSAME( JOBQ, 'Q' )
438 IF( .NOT.( INITU .OR. WANTU .OR. LSAME( JOBU, 'N' ) ) ) THEN
440 ELSE IF( .NOT.( INITV .OR. WANTV .OR. LSAME( JOBV, 'N' ) ) ) THEN
442 ELSE IF( .NOT.( INITQ .OR. WANTQ .OR. LSAME( JOBQ, 'N' ) ) ) THEN
444 ELSE IF( M.LT.0 ) THEN
446 ELSE IF( P.LT.0 ) THEN
448 ELSE IF( N.LT.0 ) THEN
450 ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
452 ELSE IF( LDB.LT.MAX( 1, P ) ) THEN
454 ELSE IF( LDU.LT.1 .OR. ( WANTU .AND. LDU.LT.M ) ) THEN
456 ELSE IF( LDV.LT.1 .OR. ( WANTV .AND. LDV.LT.P ) ) THEN
458 ELSE IF( LDQ.LT.1 .OR. ( WANTQ .AND. LDQ.LT.N ) ) THEN
462 CALL XERBLA( 'STGSJA', -INFO )
466 * Initialize U, V and Q, if necessary
469 $ CALL SLASET( 'Full', M, M, ZERO, ONE, U, LDU )
471 $ CALL SLASET( 'Full', P, P, ZERO, ONE, V, LDV )
473 $ CALL SLASET( 'Full', N, N, ZERO, ONE, Q, LDQ )
475 * Loop until convergence
478 DO 40 KCYCLE = 1, MAXIT
489 $ A1 = A( K+I, N-L+I )
491 $ A3 = A( K+J, N-L+J )
498 $ A2 = A( K+I, N-L+J )
502 $ A2 = A( K+J, N-L+I )
506 CALL SLAGS2( UPPER, A1, A2, A3, B1, B2, B3, CSU, SNU,
507 $ CSV, SNV, CSQ, SNQ )
509 * Update (K+I)-th and (K+J)-th rows of matrix A: U**T *A
512 $ CALL SROT( L, A( K+J, N-L+1 ), LDA, A( K+I, N-L+1 ),
515 * Update I-th and J-th rows of matrix B: V**T *B
517 CALL SROT( L, B( J, N-L+1 ), LDB, B( I, N-L+1 ), LDB,
520 * Update (N-L+I)-th and (N-L+J)-th columns of matrices
521 * A and B: A*Q and B*Q
523 CALL SROT( MIN( K+L, M ), A( 1, N-L+J ), 1,
524 $ A( 1, N-L+I ), 1, CSQ, SNQ )
526 CALL SROT( L, B( 1, N-L+J ), 1, B( 1, N-L+I ), 1, CSQ,
531 $ A( K+I, N-L+J ) = ZERO
535 $ A( K+J, N-L+I ) = ZERO
539 * Update orthogonal matrices U, V, Q, if desired.
541 IF( WANTU .AND. K+J.LE.M )
542 $ CALL SROT( M, U( 1, K+J ), 1, U( 1, K+I ), 1, CSU,
546 $ CALL SROT( P, V( 1, J ), 1, V( 1, I ), 1, CSV, SNV )
549 $ CALL SROT( N, Q( 1, N-L+J ), 1, Q( 1, N-L+I ), 1, CSQ,
555 IF( .NOT.UPPER ) THEN
557 * The matrices A13 and B13 were lower triangular at the start
558 * of the cycle, and are now upper triangular.
560 * Convergence test: test the parallelism of the corresponding
564 DO 30 I = 1, MIN( L, M-K )
565 CALL SCOPY( L-I+1, A( K+I, N-L+I ), LDA, WORK, 1 )
566 CALL SCOPY( L-I+1, B( I, N-L+I ), LDB, WORK( L+1 ), 1 )
567 CALL SLAPLL( L-I+1, WORK, 1, WORK( L+1 ), 1, SSMIN )
568 ERROR = MAX( ERROR, SSMIN )
571 IF( ABS( ERROR ).LE.MIN( TOLA, TOLB ) )
579 * The algorithm has not converged after MAXIT cycles.
586 * If ERROR <= MIN(TOLA,TOLB), then the algorithm has converged.
587 * Compute the generalized singular value pairs (ALPHA, BETA), and
588 * set the triangular matrix R to array A.
595 DO 70 I = 1, MIN( L, M-K )
600 IF( A1.NE.ZERO ) THEN
603 * change sign if necessary
605 IF( GAMMA.LT.ZERO ) THEN
606 CALL SSCAL( L-I+1, -ONE, B( I, N-L+I ), LDB )
608 $ CALL SSCAL( P, -ONE, V( 1, I ), 1 )
611 CALL SLARTG( ABS( GAMMA ), ONE, BETA( K+I ), ALPHA( K+I ),
614 IF( ALPHA( K+I ).GE.BETA( K+I ) ) THEN
615 CALL SSCAL( L-I+1, ONE / ALPHA( K+I ), A( K+I, N-L+I ),
618 CALL SSCAL( L-I+1, ONE / BETA( K+I ), B( I, N-L+I ),
620 CALL SCOPY( L-I+1, B( I, N-L+I ), LDB, A( K+I, N-L+I ),
628 CALL SCOPY( L-I+1, B( I, N-L+I ), LDB, A( K+I, N-L+I ),
637 DO 80 I = M + 1, K + L
643 DO 90 I = K + L + 1, N