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