001:       SUBROUTINE CGGSVP( JOBU, JOBV, JOBQ, M, P, N, A, LDA, B, LDB,
002:      $                   TOLA, TOLB, K, L, U, LDU, V, LDV, Q, LDQ,
003:      $                   IWORK, RWORK, 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               RWORK( * )
017:       COMPLEX            A( LDA, * ), B( LDB, * ), Q( LDQ, * ),
018:      $                   TAU( * ), U( LDU, * ), V( LDV, * ), WORK( * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  CGGSVP computes unitary 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: *  conjugate transpose of Z.
044: *
045: *  This decomposition is the preprocessing step for computing the
046: *  Generalized Singular Value Decomposition (GSVD), see subroutine
047: *  CGGSVD.
048: *
049: *  Arguments
050: *  =========
051: *
052: *  JOBU    (input) CHARACTER*1
053: *          = 'U':  Unitary matrix U is computed;
054: *          = 'N':  U is not computed.
055: *
056: *  JOBV    (input) CHARACTER*1
057: *          = 'V':  Unitary matrix V is computed;
058: *          = 'N':  V is not computed.
059: *
060: *  JOBQ    (input) CHARACTER*1
061: *          = 'Q':  Unitary 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) COMPLEX 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) COMPLEX 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 section.
103: *          K + L = effective numerical rank of (A',B')'.
104: *
105: *  U       (output) COMPLEX array, dimension (LDU,M)
106: *          If JOBU = 'U', U contains the unitary 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) COMPLEX array, dimension (LDV,P)
114: *          If JOBV = 'V', V contains the unitary 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) COMPLEX array, dimension (LDQ,N)
122: *          If JOBQ = 'Q', Q contains the unitary 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: *  RWORK   (workspace) REAL array, dimension (2*N)
132: *
133: *  TAU     (workspace) COMPLEX array, dimension (N)
134: *
135: *  WORK    (workspace) COMPLEX array, dimension (max(3*N,M,P))
136: *
137: *  INFO    (output) INTEGER
138: *          = 0:  successful exit
139: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
140: *
141: *  Further Details
142: *  ===============
143: *
144: *  The subroutine uses LAPACK subroutine CGEQPF for the QR factorization
145: *  with column pivoting to detect the effective numerical rank of the
146: *  a matrix. It may be replaced by a better rank determination strategy.
147: *
148: *  =====================================================================
149: *
150: *     .. Parameters ..
151:       COMPLEX            CZERO, CONE
152:       PARAMETER          ( CZERO = ( 0.0E+0, 0.0E+0 ),
153:      $                   CONE = ( 1.0E+0, 0.0E+0 ) )
154: *     ..
155: *     .. Local Scalars ..
156:       LOGICAL            FORWRD, WANTQ, WANTU, WANTV
157:       INTEGER            I, J
158:       COMPLEX            T
159: *     ..
160: *     .. External Functions ..
161:       LOGICAL            LSAME
162:       EXTERNAL           LSAME
163: *     ..
164: *     .. External Subroutines ..
165:       EXTERNAL           CGEQPF, CGEQR2, CGERQ2, CLACPY, CLAPMT, CLASET,
166:      $                   CUNG2R, CUNM2R, CUNMR2, XERBLA
167: *     ..
168: *     .. Intrinsic Functions ..
169:       INTRINSIC          ABS, AIMAG, MAX, MIN, REAL
170: *     ..
171: *     .. Statement Functions ..
172:       REAL               CABS1
173: *     ..
174: *     .. Statement Function definitions ..
175:       CABS1( T ) = ABS( REAL( T ) ) + ABS( AIMAG( T ) )
176: *     ..
177: *     .. Executable Statements ..
178: *
179: *     Test the input parameters
180: *
181:       WANTU = LSAME( JOBU, 'U' )
182:       WANTV = LSAME( JOBV, 'V' )
183:       WANTQ = LSAME( JOBQ, 'Q' )
184:       FORWRD = .TRUE.
185: *
186:       INFO = 0
187:       IF( .NOT.( WANTU .OR. LSAME( JOBU, 'N' ) ) ) THEN
188:          INFO = -1
189:       ELSE IF( .NOT.( WANTV .OR. LSAME( JOBV, 'N' ) ) ) THEN
190:          INFO = -2
191:       ELSE IF( .NOT.( WANTQ .OR. LSAME( JOBQ, 'N' ) ) ) THEN
192:          INFO = -3
193:       ELSE IF( M.LT.0 ) THEN
194:          INFO = -4
195:       ELSE IF( P.LT.0 ) THEN
196:          INFO = -5
197:       ELSE IF( N.LT.0 ) THEN
198:          INFO = -6
199:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
200:          INFO = -8
201:       ELSE IF( LDB.LT.MAX( 1, P ) ) THEN
202:          INFO = -10
203:       ELSE IF( LDU.LT.1 .OR. ( WANTU .AND. LDU.LT.M ) ) THEN
204:          INFO = -16
205:       ELSE IF( LDV.LT.1 .OR. ( WANTV .AND. LDV.LT.P ) ) THEN
206:          INFO = -18
207:       ELSE IF( LDQ.LT.1 .OR. ( WANTQ .AND. LDQ.LT.N ) ) THEN
208:          INFO = -20
209:       END IF
210:       IF( INFO.NE.0 ) THEN
211:          CALL XERBLA( 'CGGSVP', -INFO )
212:          RETURN
213:       END IF
214: *
215: *     QR with column pivoting of B: B*P = V*( S11 S12 )
216: *                                           (  0   0  )
217: *
218:       DO 10 I = 1, N
219:          IWORK( I ) = 0
220:    10 CONTINUE
221:       CALL CGEQPF( P, N, B, LDB, IWORK, TAU, WORK, RWORK, INFO )
222: *
223: *     Update A := A*P
224: *
225:       CALL CLAPMT( FORWRD, M, N, A, LDA, IWORK )
226: *
227: *     Determine the effective rank of matrix B.
228: *
229:       L = 0
230:       DO 20 I = 1, MIN( P, N )
231:          IF( CABS1( B( I, I ) ).GT.TOLB )
232:      $      L = L + 1
233:    20 CONTINUE
234: *
235:       IF( WANTV ) THEN
236: *
237: *        Copy the details of V, and form V.
238: *
239:          CALL CLASET( 'Full', P, P, CZERO, CZERO, V, LDV )
240:          IF( P.GT.1 )
241:      $      CALL CLACPY( 'Lower', P-1, N, B( 2, 1 ), LDB, V( 2, 1 ),
242:      $                   LDV )
243:          CALL CUNG2R( P, P, MIN( P, N ), V, LDV, TAU, WORK, INFO )
244:       END IF
245: *
246: *     Clean up B
247: *
248:       DO 40 J = 1, L - 1
249:          DO 30 I = J + 1, L
250:             B( I, J ) = CZERO
251:    30    CONTINUE
252:    40 CONTINUE
253:       IF( P.GT.L )
254:      $   CALL CLASET( 'Full', P-L, N, CZERO, CZERO, B( L+1, 1 ), LDB )
255: *
256:       IF( WANTQ ) THEN
257: *
258: *        Set Q = I and Update Q := Q*P
259: *
260:          CALL CLASET( 'Full', N, N, CZERO, CONE, Q, LDQ )
261:          CALL CLAPMT( FORWRD, N, N, Q, LDQ, IWORK )
262:       END IF
263: *
264:       IF( P.GE.L .AND. N.NE.L ) THEN
265: *
266: *        RQ factorization of ( S11 S12 ) = ( 0 S12 )*Z
267: *
268:          CALL CGERQ2( L, N, B, LDB, TAU, WORK, INFO )
269: *
270: *        Update A := A*Z'
271: *
272:          CALL CUNMR2( 'Right', 'Conjugate transpose', M, N, L, B, LDB,
273:      $                TAU, A, LDA, WORK, INFO )
274:          IF( WANTQ ) THEN
275: *
276: *           Update Q := Q*Z'
277: *
278:             CALL CUNMR2( 'Right', 'Conjugate transpose', N, N, L, B,
279:      $                   LDB, TAU, Q, LDQ, WORK, INFO )
280:          END IF
281: *
282: *        Clean up B
283: *
284:          CALL CLASET( 'Full', L, N-L, CZERO, CZERO, B, LDB )
285:          DO 60 J = N - L + 1, N
286:             DO 50 I = J - N + L + 1, L
287:                B( I, J ) = CZERO
288:    50       CONTINUE
289:    60    CONTINUE
290: *
291:       END IF
292: *
293: *     Let              N-L     L
294: *                A = ( A11    A12 ) M,
295: *
296: *     then the following does the complete QR decomposition of A11:
297: *
298: *              A11 = U*(  0  T12 )*P1'
299: *                      (  0   0  )
300: *
301:       DO 70 I = 1, N - L
302:          IWORK( I ) = 0
303:    70 CONTINUE
304:       CALL CGEQPF( M, N-L, A, LDA, IWORK, TAU, WORK, RWORK, INFO )
305: *
306: *     Determine the effective rank of A11
307: *
308:       K = 0
309:       DO 80 I = 1, MIN( M, N-L )
310:          IF( CABS1( A( I, I ) ).GT.TOLA )
311:      $      K = K + 1
312:    80 CONTINUE
313: *
314: *     Update A12 := U'*A12, where A12 = A( 1:M, N-L+1:N )
315: *
316:       CALL CUNM2R( 'Left', 'Conjugate transpose', M, L, MIN( M, N-L ),
317:      $             A, LDA, TAU, A( 1, N-L+1 ), LDA, WORK, INFO )
318: *
319:       IF( WANTU ) THEN
320: *
321: *        Copy the details of U, and form U
322: *
323:          CALL CLASET( 'Full', M, M, CZERO, CZERO, U, LDU )
324:          IF( M.GT.1 )
325:      $      CALL CLACPY( 'Lower', M-1, N-L, A( 2, 1 ), LDA, U( 2, 1 ),
326:      $                   LDU )
327:          CALL CUNG2R( M, M, MIN( M, N-L ), U, LDU, TAU, WORK, INFO )
328:       END IF
329: *
330:       IF( WANTQ ) THEN
331: *
332: *        Update Q( 1:N, 1:N-L )  = Q( 1:N, 1:N-L )*P1
333: *
334:          CALL CLAPMT( FORWRD, N, N-L, Q, LDQ, IWORK )
335:       END IF
336: *
337: *     Clean up A: set the strictly lower triangular part of
338: *     A(1:K, 1:K) = 0, and A( K+1:M, 1:N-L ) = 0.
339: *
340:       DO 100 J = 1, K - 1
341:          DO 90 I = J + 1, K
342:             A( I, J ) = CZERO
343:    90    CONTINUE
344:   100 CONTINUE
345:       IF( M.GT.K )
346:      $   CALL CLASET( 'Full', M-K, N-L, CZERO, CZERO, A( K+1, 1 ), LDA )
347: *
348:       IF( N-L.GT.K ) THEN
349: *
350: *        RQ factorization of ( T11 T12 ) = ( 0 T12 )*Z1
351: *
352:          CALL CGERQ2( K, N-L, A, LDA, TAU, WORK, INFO )
353: *
354:          IF( WANTQ ) THEN
355: *
356: *           Update Q( 1:N,1:N-L ) = Q( 1:N,1:N-L )*Z1'
357: *
358:             CALL CUNMR2( 'Right', 'Conjugate transpose', N, N-L, K, A,
359:      $                   LDA, TAU, Q, LDQ, WORK, INFO )
360:          END IF
361: *
362: *        Clean up A
363: *
364:          CALL CLASET( 'Full', K, N-L-K, CZERO, CZERO, A, LDA )
365:          DO 120 J = N - L - K + 1, N - L
366:             DO 110 I = J - N + L + K + 1, K
367:                A( I, J ) = CZERO
368:   110       CONTINUE
369:   120    CONTINUE
370: *
371:       END IF
372: *
373:       IF( M.GT.K ) THEN
374: *
375: *        QR factorization of A( K+1:M,N-L+1:N )
376: *
377:          CALL CGEQR2( M-K, L, A( K+1, N-L+1 ), LDA, TAU, WORK, INFO )
378: *
379:          IF( WANTU ) THEN
380: *
381: *           Update U(:,K+1:M) := U(:,K+1:M)*U1
382: *
383:             CALL CUNM2R( 'Right', 'No transpose', M, M-K, MIN( M-K, L ),
384:      $                   A( K+1, N-L+1 ), LDA, TAU, U( 1, K+1 ), LDU,
385:      $                   WORK, INFO )
386:          END IF
387: *
388: *        Clean up
389: *
390:          DO 140 J = N - L + 1, N
391:             DO 130 I = J - N + K + L + 1, M
392:                A( I, J ) = CZERO
393:   130       CONTINUE
394:   140    CONTINUE
395: *
396:       END IF
397: *
398:       RETURN
399: *
400: *     End of CGGSVP
401: *
402:       END
403: