001:       SUBROUTINE SGGSVP( JOBU, JOBV, JOBQ, M, P, N, A, LDA, B, LDB,
002:      $                   TOLA, TOLB, K, L, U, LDU, V, LDV, Q, LDQ,
003:      $                   IWORK, TAU, WORK, 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, P
012:       REAL               TOLA, TOLB
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IWORK( * )
016:       REAL               A( LDA, * ), B( LDB, * ), Q( LDQ, * ),
017:      $                   TAU( * ), U( LDU, * ), V( LDV, * ), WORK( * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  SGGSVP computes orthogonal matrices U, V and Q such that
024: *
025: *                   N-K-L  K    L
026: *   U'*A*Q =     K ( 0    A12  A13 )  if M-K-L >= 0;
027: *                L ( 0     0   A23 )
028: *            M-K-L ( 0     0    0  )
029: *
030: *                   N-K-L  K    L
031: *          =     K ( 0    A12  A13 )  if M-K-L < 0;
032: *              M-K ( 0     0   A23 )
033: *
034: *                 N-K-L  K    L
035: *   V'*B*Q =   L ( 0     0   B13 )
036: *            P-L ( 0     0    0  )
037: *
038: *  where the K-by-K matrix A12 and L-by-L matrix B13 are nonsingular
039: *  upper triangular; A23 is L-by-L upper triangular if M-K-L >= 0,
040: *  otherwise A23 is (M-K)-by-L upper trapezoidal.  K+L = the effective
041: *  numerical rank of the (M+P)-by-N matrix (A',B')'.  Z' denotes the
042: *  transpose of Z.
043: *
044: *  This decomposition is the preprocessing step for computing the
045: *  Generalized Singular Value Decomposition (GSVD), see subroutine
046: *  SGGSVD.
047: *
048: *  Arguments
049: *  =========
050: *
051: *  JOBU    (input) CHARACTER*1
052: *          = 'U':  Orthogonal matrix U is computed;
053: *          = 'N':  U is not computed.
054: *
055: *  JOBV    (input) CHARACTER*1
056: *          = 'V':  Orthogonal matrix V is computed;
057: *          = 'N':  V is not computed.
058: *
059: *  JOBQ    (input) CHARACTER*1
060: *          = 'Q':  Orthogonal matrix Q is computed;
061: *          = 'N':  Q is not computed.
062: *
063: *  M       (input) INTEGER
064: *          The number of rows of the matrix A.  M >= 0.
065: *
066: *  P       (input) INTEGER
067: *          The number of rows of the matrix B.  P >= 0.
068: *
069: *  N       (input) INTEGER
070: *          The number of columns of the matrices A and B.  N >= 0.
071: *
072: *  A       (input/output) REAL array, dimension (LDA,N)
073: *          On entry, the M-by-N matrix A.
074: *          On exit, A contains the triangular (or trapezoidal) matrix
075: *          described in the Purpose section.
076: *
077: *  LDA     (input) INTEGER
078: *          The leading dimension of the array A. LDA >= max(1,M).
079: *
080: *  B       (input/output) REAL array, dimension (LDB,N)
081: *          On entry, the P-by-N matrix B.
082: *          On exit, B contains the triangular matrix described in
083: *          the Purpose section.
084: *
085: *  LDB     (input) INTEGER
086: *          The leading dimension of the array B. LDB >= max(1,P).
087: *
088: *  TOLA    (input) REAL
089: *  TOLB    (input) REAL
090: *          TOLA and TOLB are the thresholds to determine the effective
091: *          numerical rank of matrix B and a subblock of A. Generally,
092: *          they are set to
093: *             TOLA = MAX(M,N)*norm(A)*MACHEPS,
094: *             TOLB = MAX(P,N)*norm(B)*MACHEPS.
095: *          The size of TOLA and TOLB may affect the size of backward
096: *          errors of the decomposition.
097: *
098: *  K       (output) INTEGER
099: *  L       (output) INTEGER
100: *          On exit, K and L specify the dimension of the subblocks
101: *          described in Purpose.
102: *          K + L = effective numerical rank of (A',B')'.
103: *
104: *  U       (output) REAL array, dimension (LDU,M)
105: *          If JOBU = 'U', U contains the orthogonal matrix U.
106: *          If JOBU = 'N', U is not referenced.
107: *
108: *  LDU     (input) INTEGER
109: *          The leading dimension of the array U. LDU >= max(1,M) if
110: *          JOBU = 'U'; LDU >= 1 otherwise.
111: *
112: *  V       (output) REAL array, dimension (LDV,P)
113: *          If JOBV = 'V', V contains the orthogonal matrix V.
114: *          If JOBV = 'N', V is not referenced.
115: *
116: *  LDV     (input) INTEGER
117: *          The leading dimension of the array V. LDV >= max(1,P) if
118: *          JOBV = 'V'; LDV >= 1 otherwise.
119: *
120: *  Q       (output) REAL array, dimension (LDQ,N)
121: *          If JOBQ = 'Q', Q contains the orthogonal matrix Q.
122: *          If JOBQ = 'N', Q is not referenced.
123: *
124: *  LDQ     (input) INTEGER
125: *          The leading dimension of the array Q. LDQ >= max(1,N) if
126: *          JOBQ = 'Q'; LDQ >= 1 otherwise.
127: *
128: *  IWORK   (workspace) INTEGER array, dimension (N)
129: *
130: *  TAU     (workspace) REAL array, dimension (N)
131: *
132: *  WORK    (workspace) REAL array, dimension (max(3*N,M,P))
133: *
134: *  INFO    (output) INTEGER
135: *          = 0:  successful exit
136: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
137: *
138: *
139: *  Further Details
140: *  ===============
141: *
142: *  The subroutine uses LAPACK subroutine SGEQPF for the QR factorization
143: *  with column pivoting to detect the effective numerical rank of the
144: *  a matrix. It may be replaced by a better rank determination strategy.
145: *
146: *  =====================================================================
147: *
148: *     .. Parameters ..
149:       REAL               ZERO, ONE
150:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
151: *     ..
152: *     .. Local Scalars ..
153:       LOGICAL            FORWRD, WANTQ, WANTU, WANTV
154:       INTEGER            I, J
155: *     ..
156: *     .. External Functions ..
157:       LOGICAL            LSAME
158:       EXTERNAL           LSAME
159: *     ..
160: *     .. External Subroutines ..
161:       EXTERNAL           SGEQPF, SGEQR2, SGERQ2, SLACPY, SLAPMT, SLASET,
162:      $                   SORG2R, SORM2R, SORMR2, XERBLA
163: *     ..
164: *     .. Intrinsic Functions ..
165:       INTRINSIC          ABS, MAX, MIN
166: *     ..
167: *     .. Executable Statements ..
168: *
169: *     Test the input parameters
170: *
171:       WANTU = LSAME( JOBU, 'U' )
172:       WANTV = LSAME( JOBV, 'V' )
173:       WANTQ = LSAME( JOBQ, 'Q' )
174:       FORWRD = .TRUE.
175: *
176:       INFO = 0
177:       IF( .NOT.( WANTU .OR. LSAME( JOBU, 'N' ) ) ) THEN
178:          INFO = -1
179:       ELSE IF( .NOT.( WANTV .OR. LSAME( JOBV, 'N' ) ) ) THEN
180:          INFO = -2
181:       ELSE IF( .NOT.( WANTQ .OR. LSAME( JOBQ, 'N' ) ) ) THEN
182:          INFO = -3
183:       ELSE IF( M.LT.0 ) THEN
184:          INFO = -4
185:       ELSE IF( P.LT.0 ) THEN
186:          INFO = -5
187:       ELSE IF( N.LT.0 ) THEN
188:          INFO = -6
189:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
190:          INFO = -8
191:       ELSE IF( LDB.LT.MAX( 1, P ) ) THEN
192:          INFO = -10
193:       ELSE IF( LDU.LT.1 .OR. ( WANTU .AND. LDU.LT.M ) ) THEN
194:          INFO = -16
195:       ELSE IF( LDV.LT.1 .OR. ( WANTV .AND. LDV.LT.P ) ) THEN
196:          INFO = -18
197:       ELSE IF( LDQ.LT.1 .OR. ( WANTQ .AND. LDQ.LT.N ) ) THEN
198:          INFO = -20
199:       END IF
200:       IF( INFO.NE.0 ) THEN
201:          CALL XERBLA( 'SGGSVP', -INFO )
202:          RETURN
203:       END IF
204: *
205: *     QR with column pivoting of B: B*P = V*( S11 S12 )
206: *                                           (  0   0  )
207: *
208:       DO 10 I = 1, N
209:          IWORK( I ) = 0
210:    10 CONTINUE
211:       CALL SGEQPF( P, N, B, LDB, IWORK, TAU, WORK, INFO )
212: *
213: *     Update A := A*P
214: *
215:       CALL SLAPMT( FORWRD, M, N, A, LDA, IWORK )
216: *
217: *     Determine the effective rank of matrix B.
218: *
219:       L = 0
220:       DO 20 I = 1, MIN( P, N )
221:          IF( ABS( B( I, I ) ).GT.TOLB )
222:      $      L = L + 1
223:    20 CONTINUE
224: *
225:       IF( WANTV ) THEN
226: *
227: *        Copy the details of V, and form V.
228: *
229:          CALL SLASET( 'Full', P, P, ZERO, ZERO, V, LDV )
230:          IF( P.GT.1 )
231:      $      CALL SLACPY( 'Lower', P-1, N, B( 2, 1 ), LDB, V( 2, 1 ),
232:      $                   LDV )
233:          CALL SORG2R( P, P, MIN( P, N ), V, LDV, TAU, WORK, INFO )
234:       END IF
235: *
236: *     Clean up B
237: *
238:       DO 40 J = 1, L - 1
239:          DO 30 I = J + 1, L
240:             B( I, J ) = ZERO
241:    30    CONTINUE
242:    40 CONTINUE
243:       IF( P.GT.L )
244:      $   CALL SLASET( 'Full', P-L, N, ZERO, ZERO, B( L+1, 1 ), LDB )
245: *
246:       IF( WANTQ ) THEN
247: *
248: *        Set Q = I and Update Q := Q*P
249: *
250:          CALL SLASET( 'Full', N, N, ZERO, ONE, Q, LDQ )
251:          CALL SLAPMT( FORWRD, N, N, Q, LDQ, IWORK )
252:       END IF
253: *
254:       IF( P.GE.L .AND. N.NE.L ) THEN
255: *
256: *        RQ factorization of (S11 S12): ( S11 S12 ) = ( 0 S12 )*Z
257: *
258:          CALL SGERQ2( L, N, B, LDB, TAU, WORK, INFO )
259: *
260: *        Update A := A*Z'
261: *
262:          CALL SORMR2( 'Right', 'Transpose', M, N, L, B, LDB, TAU, A,
263:      $                LDA, WORK, INFO )
264: *
265:          IF( WANTQ ) THEN
266: *
267: *           Update Q := Q*Z'
268: *
269:             CALL SORMR2( 'Right', 'Transpose', N, N, L, B, LDB, TAU, Q,
270:      $                   LDQ, WORK, INFO )
271:          END IF
272: *
273: *        Clean up B
274: *
275:          CALL SLASET( 'Full', L, N-L, ZERO, ZERO, B, LDB )
276:          DO 60 J = N - L + 1, N
277:             DO 50 I = J - N + L + 1, L
278:                B( I, J ) = ZERO
279:    50       CONTINUE
280:    60    CONTINUE
281: *
282:       END IF
283: *
284: *     Let              N-L     L
285: *                A = ( A11    A12 ) M,
286: *
287: *     then the following does the complete QR decomposition of A11:
288: *
289: *              A11 = U*(  0  T12 )*P1'
290: *                      (  0   0  )
291: *
292:       DO 70 I = 1, N - L
293:          IWORK( I ) = 0
294:    70 CONTINUE
295:       CALL SGEQPF( M, N-L, A, LDA, IWORK, TAU, WORK, INFO )
296: *
297: *     Determine the effective rank of A11
298: *
299:       K = 0
300:       DO 80 I = 1, MIN( M, N-L )
301:          IF( ABS( A( I, I ) ).GT.TOLA )
302:      $      K = K + 1
303:    80 CONTINUE
304: *
305: *     Update A12 := U'*A12, where A12 = A( 1:M, N-L+1:N )
306: *
307:       CALL SORM2R( 'Left', 'Transpose', M, L, MIN( M, N-L ), A, LDA,
308:      $             TAU, A( 1, N-L+1 ), LDA, WORK, INFO )
309: *
310:       IF( WANTU ) THEN
311: *
312: *        Copy the details of U, and form U
313: *
314:          CALL SLASET( 'Full', M, M, ZERO, ZERO, U, LDU )
315:          IF( M.GT.1 )
316:      $      CALL SLACPY( 'Lower', M-1, N-L, A( 2, 1 ), LDA, U( 2, 1 ),
317:      $                   LDU )
318:          CALL SORG2R( M, M, MIN( M, N-L ), U, LDU, TAU, WORK, INFO )
319:       END IF
320: *
321:       IF( WANTQ ) THEN
322: *
323: *        Update Q( 1:N, 1:N-L )  = Q( 1:N, 1:N-L )*P1
324: *
325:          CALL SLAPMT( FORWRD, N, N-L, Q, LDQ, IWORK )
326:       END IF
327: *
328: *     Clean up A: set the strictly lower triangular part of
329: *     A(1:K, 1:K) = 0, and A( K+1:M, 1:N-L ) = 0.
330: *
331:       DO 100 J = 1, K - 1
332:          DO 90 I = J + 1, K
333:             A( I, J ) = ZERO
334:    90    CONTINUE
335:   100 CONTINUE
336:       IF( M.GT.K )
337:      $   CALL SLASET( 'Full', M-K, N-L, ZERO, ZERO, A( K+1, 1 ), LDA )
338: *
339:       IF( N-L.GT.K ) THEN
340: *
341: *        RQ factorization of ( T11 T12 ) = ( 0 T12 )*Z1
342: *
343:          CALL SGERQ2( K, N-L, A, LDA, TAU, WORK, INFO )
344: *
345:          IF( WANTQ ) THEN
346: *
347: *           Update Q( 1:N,1:N-L ) = Q( 1:N,1:N-L )*Z1'
348: *
349:             CALL SORMR2( 'Right', 'Transpose', N, N-L, K, A, LDA, TAU,
350:      $                   Q, LDQ, WORK, INFO )
351:          END IF
352: *
353: *        Clean up A
354: *
355:          CALL SLASET( 'Full', K, N-L-K, ZERO, ZERO, A, LDA )
356:          DO 120 J = N - L - K + 1, N - L
357:             DO 110 I = J - N + L + K + 1, K
358:                A( I, J ) = ZERO
359:   110       CONTINUE
360:   120    CONTINUE
361: *
362:       END IF
363: *
364:       IF( M.GT.K ) THEN
365: *
366: *        QR factorization of A( K+1:M,N-L+1:N )
367: *
368:          CALL SGEQR2( M-K, L, A( K+1, N-L+1 ), LDA, TAU, WORK, INFO )
369: *
370:          IF( WANTU ) THEN
371: *
372: *           Update U(:,K+1:M) := U(:,K+1:M)*U1
373: *
374:             CALL SORM2R( 'Right', 'No transpose', M, M-K, MIN( M-K, L ),
375:      $                   A( K+1, N-L+1 ), LDA, TAU, U( 1, K+1 ), LDU,
376:      $                   WORK, INFO )
377:          END IF
378: *
379: *        Clean up
380: *
381:          DO 140 J = N - L + 1, N
382:             DO 130 I = J - N + K + L + 1, M
383:                A( I, J ) = ZERO
384:   130       CONTINUE
385:   140    CONTINUE
386: *
387:       END IF
388: *
389:       RETURN
390: *
391: *     End of SGGSVP
392: *
393:       END
394: