001:       SUBROUTINE SGGSVD( JOBU, JOBV, JOBQ, M, N, P, K, L, A, LDA, B,
002:      $                   LDB, ALPHA, BETA, U, LDU, V, LDV, Q, LDQ, WORK,
003:      $                   IWORK, INFO )
004: *
005: *  -- LAPACK driver routine (version 3.2) --
006: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
007: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
008: *     November 2006
009: *
010: *     .. Scalar Arguments ..
011:       CHARACTER          JOBQ, JOBU, JOBV
012:       INTEGER            INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N, P
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IWORK( * )
016:       REAL               A( LDA, * ), ALPHA( * ), B( LDB, * ),
017:      $                   BETA( * ), Q( LDQ, * ), U( LDU, * ),
018:      $                   V( LDV, * ), WORK( * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  SGGSVD computes the generalized singular value decomposition (GSVD)
025: *  of an M-by-N real matrix A and P-by-N real matrix B:
026: *
027: *      U'*A*Q = D1*( 0 R ),    V'*B*Q = D2*( 0 R )
028: *
029: *  where U, V and Q are orthogonal matrices, and Z' is the transpose
030: *  of Z.  Let K+L = the effective numerical rank of the matrix (A',B')',
031: *  then R is a K+L-by-K+L nonsingular upper triangular matrix, D1 and
032: *  D2 are M-by-(K+L) and P-by-(K+L) "diagonal" matrices and of the
033: *  following structures, respectively:
034: *
035: *  If M-K-L >= 0,
036: *
037: *                      K  L
038: *         D1 =     K ( I  0 )
039: *                  L ( 0  C )
040: *              M-K-L ( 0  0 )
041: *
042: *                    K  L
043: *         D2 =   L ( 0  S )
044: *              P-L ( 0  0 )
045: *
046: *                  N-K-L  K    L
047: *    ( 0 R ) = K (  0   R11  R12 )
048: *              L (  0    0   R22 )
049: *
050: *  where
051: *
052: *    C = diag( ALPHA(K+1), ... , ALPHA(K+L) ),
053: *    S = diag( BETA(K+1),  ... , BETA(K+L) ),
054: *    C**2 + S**2 = I.
055: *
056: *    R is stored in A(1:K+L,N-K-L+1:N) on exit.
057: *
058: *  If M-K-L < 0,
059: *
060: *                    K M-K K+L-M
061: *         D1 =   K ( I  0    0   )
062: *              M-K ( 0  C    0   )
063: *
064: *                      K M-K K+L-M
065: *         D2 =   M-K ( 0  S    0  )
066: *              K+L-M ( 0  0    I  )
067: *                P-L ( 0  0    0  )
068: *
069: *                     N-K-L  K   M-K  K+L-M
070: *    ( 0 R ) =     K ( 0    R11  R12  R13  )
071: *                M-K ( 0     0   R22  R23  )
072: *              K+L-M ( 0     0    0   R33  )
073: *
074: *  where
075: *
076: *    C = diag( ALPHA(K+1), ... , ALPHA(M) ),
077: *    S = diag( BETA(K+1),  ... , BETA(M) ),
078: *    C**2 + S**2 = I.
079: *
080: *    (R11 R12 R13 ) is stored in A(1:M, N-K-L+1:N), and R33 is stored
081: *    ( 0  R22 R23 )
082: *    in B(M-K+1:L,N+M-K-L+1:N) on exit.
083: *
084: *  The routine computes C, S, R, and optionally the orthogonal
085: *  transformation matrices U, V and Q.
086: *
087: *  In particular, if B is an N-by-N nonsingular matrix, then the GSVD of
088: *  A and B implicitly gives the SVD of A*inv(B):
089: *                       A*inv(B) = U*(D1*inv(D2))*V'.
090: *  If ( A',B')' has orthonormal columns, then the GSVD of A and B is
091: *  also equal to the CS decomposition of A and B. Furthermore, the GSVD
092: *  can be used to derive the solution of the eigenvalue problem:
093: *                       A'*A x = lambda* B'*B x.
094: *  In some literature, the GSVD of A and B is presented in the form
095: *                   U'*A*X = ( 0 D1 ),   V'*B*X = ( 0 D2 )
096: *  where U and V are orthogonal and X is nonsingular, D1 and D2 are
097: *  ``diagonal''.  The former GSVD form can be converted to the latter
098: *  form by taking the nonsingular matrix X as
099: *
100: *                       X = Q*( I   0    )
101: *                             ( 0 inv(R) ).
102: *
103: *  Arguments
104: *  =========
105: *
106: *  JOBU    (input) CHARACTER*1
107: *          = 'U':  Orthogonal matrix U is computed;
108: *          = 'N':  U is not computed.
109: *
110: *  JOBV    (input) CHARACTER*1
111: *          = 'V':  Orthogonal matrix V is computed;
112: *          = 'N':  V is not computed.
113: *
114: *  JOBQ    (input) CHARACTER*1
115: *          = 'Q':  Orthogonal matrix Q is computed;
116: *          = 'N':  Q is not computed.
117: *
118: *  M       (input) INTEGER
119: *          The number of rows of the matrix A.  M >= 0.
120: *
121: *  N       (input) INTEGER
122: *          The number of columns of the matrices A and B.  N >= 0.
123: *
124: *  P       (input) INTEGER
125: *          The number of rows of the matrix B.  P >= 0.
126: *
127: *  K       (output) INTEGER
128: *  L       (output) INTEGER
129: *          On exit, K and L specify the dimension of the subblocks
130: *          described in the Purpose section.
131: *          K + L = effective numerical rank of (A',B')'.
132: *
133: *  A       (input/output) REAL array, dimension (LDA,N)
134: *          On entry, the M-by-N matrix A.
135: *          On exit, A contains the triangular matrix R, or part of R.
136: *          See Purpose for details.
137: *
138: *  LDA     (input) INTEGER
139: *          The leading dimension of the array A. LDA >= max(1,M).
140: *
141: *  B       (input/output) REAL array, dimension (LDB,N)
142: *          On entry, the P-by-N matrix B.
143: *          On exit, B contains the triangular matrix R if M-K-L < 0.
144: *          See Purpose for details.
145: *
146: *  LDB     (input) INTEGER
147: *          The leading dimension of the array B. LDB >= max(1,P).
148: *
149: *  ALPHA   (output) REAL array, dimension (N)
150: *  BETA    (output) REAL array, dimension (N)
151: *          On exit, ALPHA and BETA contain the generalized singular
152: *          value pairs of A and B;
153: *            ALPHA(1:K) = 1,
154: *            BETA(1:K)  = 0,
155: *          and if M-K-L >= 0,
156: *            ALPHA(K+1:K+L) = C,
157: *            BETA(K+1:K+L)  = S,
158: *          or if M-K-L < 0,
159: *            ALPHA(K+1:M)=C, ALPHA(M+1:K+L)=0
160: *            BETA(K+1:M) =S, BETA(M+1:K+L) =1
161: *          and
162: *            ALPHA(K+L+1:N) = 0
163: *            BETA(K+L+1:N)  = 0
164: *
165: *  U       (output) REAL array, dimension (LDU,M)
166: *          If JOBU = 'U', U contains the M-by-M orthogonal matrix U.
167: *          If JOBU = 'N', U is not referenced.
168: *
169: *  LDU     (input) INTEGER
170: *          The leading dimension of the array U. LDU >= max(1,M) if
171: *          JOBU = 'U'; LDU >= 1 otherwise.
172: *
173: *  V       (output) REAL array, dimension (LDV,P)
174: *          If JOBV = 'V', V contains the P-by-P orthogonal matrix V.
175: *          If JOBV = 'N', V is not referenced.
176: *
177: *  LDV     (input) INTEGER
178: *          The leading dimension of the array V. LDV >= max(1,P) if
179: *          JOBV = 'V'; LDV >= 1 otherwise.
180: *
181: *  Q       (output) REAL array, dimension (LDQ,N)
182: *          If JOBQ = 'Q', Q contains the N-by-N orthogonal matrix Q.
183: *          If JOBQ = 'N', Q is not referenced.
184: *
185: *  LDQ     (input) INTEGER
186: *          The leading dimension of the array Q. LDQ >= max(1,N) if
187: *          JOBQ = 'Q'; LDQ >= 1 otherwise.
188: *
189: *  WORK    (workspace) REAL array,
190: *                      dimension (max(3*N,M,P)+N)
191: *
192: *  IWORK   (workspace/output) INTEGER array, dimension (N)
193: *          On exit, IWORK stores the sorting information. More
194: *          precisely, the following loop will sort ALPHA
195: *             for I = K+1, min(M,K+L)
196: *                 swap ALPHA(I) and ALPHA(IWORK(I))
197: *             endfor
198: *          such that ALPHA(1) >= ALPHA(2) >= ... >= ALPHA(N).
199: *
200: *  INFO    (output) INTEGER
201: *          = 0:  successful exit
202: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
203: *          > 0:  if INFO = 1, the Jacobi-type procedure failed to
204: *                converge.  For further details, see subroutine STGSJA.
205: *
206: *  Internal Parameters
207: *  ===================
208: *
209: *  TOLA    REAL
210: *  TOLB    REAL
211: *          TOLA and TOLB are the thresholds to determine the effective
212: *          rank of (A',B')'. Generally, they are set to
213: *                   TOLA = MAX(M,N)*norm(A)*MACHEPS,
214: *                   TOLB = MAX(P,N)*norm(B)*MACHEPS.
215: *          The size of TOLA and TOLB may affect the size of backward
216: *          errors of the decomposition.
217: *
218: *  Further Details
219: *  ===============
220: *
221: *  2-96 Based on modifications by
222: *     Ming Gu and Huan Ren, Computer Science Division, University of
223: *     California at Berkeley, USA
224: *
225: *  =====================================================================
226: *
227: *     .. Local Scalars ..
228:       LOGICAL            WANTQ, WANTU, WANTV
229:       INTEGER            I, IBND, ISUB, J, NCYCLE
230:       REAL               ANORM, BNORM, SMAX, TEMP, TOLA, TOLB, ULP, UNFL
231: *     ..
232: *     .. External Functions ..
233:       LOGICAL            LSAME
234:       REAL               SLAMCH, SLANGE
235:       EXTERNAL           LSAME, SLAMCH, SLANGE
236: *     ..
237: *     .. External Subroutines ..
238:       EXTERNAL           SCOPY, SGGSVP, STGSJA, XERBLA
239: *     ..
240: *     .. Intrinsic Functions ..
241:       INTRINSIC          MAX, MIN
242: *     ..
243: *     .. Executable Statements ..
244: *
245: *     Test the input parameters
246: *
247:       WANTU = LSAME( JOBU, 'U' )
248:       WANTV = LSAME( JOBV, 'V' )
249:       WANTQ = LSAME( JOBQ, 'Q' )
250: *
251:       INFO = 0
252:       IF( .NOT.( WANTU .OR. LSAME( JOBU, 'N' ) ) ) THEN
253:          INFO = -1
254:       ELSE IF( .NOT.( WANTV .OR. LSAME( JOBV, 'N' ) ) ) THEN
255:          INFO = -2
256:       ELSE IF( .NOT.( WANTQ .OR. LSAME( JOBQ, 'N' ) ) ) THEN
257:          INFO = -3
258:       ELSE IF( M.LT.0 ) THEN
259:          INFO = -4
260:       ELSE IF( N.LT.0 ) THEN
261:          INFO = -5
262:       ELSE IF( P.LT.0 ) THEN
263:          INFO = -6
264:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
265:          INFO = -10
266:       ELSE IF( LDB.LT.MAX( 1, P ) ) THEN
267:          INFO = -12
268:       ELSE IF( LDU.LT.1 .OR. ( WANTU .AND. LDU.LT.M ) ) THEN
269:          INFO = -16
270:       ELSE IF( LDV.LT.1 .OR. ( WANTV .AND. LDV.LT.P ) ) THEN
271:          INFO = -18
272:       ELSE IF( LDQ.LT.1 .OR. ( WANTQ .AND. LDQ.LT.N ) ) THEN
273:          INFO = -20
274:       END IF
275:       IF( INFO.NE.0 ) THEN
276:          CALL XERBLA( 'SGGSVD', -INFO )
277:          RETURN
278:       END IF
279: *
280: *     Compute the Frobenius norm of matrices A and B
281: *
282:       ANORM = SLANGE( '1', M, N, A, LDA, WORK )
283:       BNORM = SLANGE( '1', P, N, B, LDB, WORK )
284: *
285: *     Get machine precision and set up threshold for determining
286: *     the effective numerical rank of the matrices A and B.
287: *
288:       ULP = SLAMCH( 'Precision' )
289:       UNFL = SLAMCH( 'Safe Minimum' )
290:       TOLA = MAX( M, N )*MAX( ANORM, UNFL )*ULP
291:       TOLB = MAX( P, N )*MAX( BNORM, UNFL )*ULP
292: *
293: *     Preprocessing
294: *
295:       CALL SGGSVP( JOBU, JOBV, JOBQ, M, P, N, A, LDA, B, LDB, TOLA,
296:      $             TOLB, K, L, U, LDU, V, LDV, Q, LDQ, IWORK, WORK,
297:      $             WORK( N+1 ), INFO )
298: *
299: *     Compute the GSVD of two upper "triangular" matrices
300: *
301:       CALL STGSJA( JOBU, JOBV, JOBQ, M, P, N, K, L, A, LDA, B, LDB,
302:      $             TOLA, TOLB, ALPHA, BETA, U, LDU, V, LDV, Q, LDQ,
303:      $             WORK, NCYCLE, INFO )
304: *
305: *     Sort the singular values and store the pivot indices in IWORK
306: *     Copy ALPHA to WORK, then sort ALPHA in WORK
307: *
308:       CALL SCOPY( N, ALPHA, 1, WORK, 1 )
309:       IBND = MIN( L, M-K )
310:       DO 20 I = 1, IBND
311: *
312: *        Scan for largest ALPHA(K+I)
313: *
314:          ISUB = I
315:          SMAX = WORK( K+I )
316:          DO 10 J = I + 1, IBND
317:             TEMP = WORK( K+J )
318:             IF( TEMP.GT.SMAX ) THEN
319:                ISUB = J
320:                SMAX = TEMP
321:             END IF
322:    10    CONTINUE
323:          IF( ISUB.NE.I ) THEN
324:             WORK( K+ISUB ) = WORK( K+I )
325:             WORK( K+I ) = SMAX
326:             IWORK( K+I ) = K + ISUB
327:          ELSE
328:             IWORK( K+I ) = K + I
329:          END IF
330:    20 CONTINUE
331: *
332:       RETURN
333: *
334: *     End of SGGSVD
335: *
336:       END
337: