1       SUBROUTINE ZGGSVP( JOBU, JOBV, JOBQ, M, P, N, A, LDA, B, LDB,
  2      $                   TOLA, TOLB, K, L, U, LDU, V, LDV, Q, LDQ,
  3      $                   IWORK, RWORK, TAU, WORK, INFO )
  4 *
  5 *  -- LAPACK routine (version 3.3.1) --
  6 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
  7 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  8 *  -- April 2011                                                      --
  9 *
 10 *     .. Scalar Arguments ..
 11       CHARACTER          JOBQ, JOBU, JOBV
 12       INTEGER            INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N, P
 13       DOUBLE PRECISION   TOLA, TOLB
 14 *     ..
 15 *     .. Array Arguments ..
 16       INTEGER            IWORK( * )
 17       DOUBLE PRECISION   RWORK( * )
 18       COMPLEX*16         A( LDA, * ), B( LDB, * ), Q( LDQ, * ),
 19      $                   TAU( * ), U( LDU, * ), V( LDV, * ), WORK( * )
 20 *     ..
 21 *
 22 *  Purpose
 23 *  =======
 24 *
 25 *  ZGGSVP computes unitary matrices U, V and Q such that
 26 *
 27 *                     N-K-L  K    L
 28 *   U**H*A*Q =     K ( 0    A12  A13 )  if M-K-L >= 0;
 29 *                  L ( 0     0   A23 )
 30 *              M-K-L ( 0     0    0  )
 31 *
 32 *                   N-K-L  K    L
 33 *          =     K ( 0    A12  A13 )  if M-K-L < 0;
 34 *              M-K ( 0     0   A23 )
 35 *
 36 *                   N-K-L  K    L
 37 *   V**H*B*Q =   L ( 0     0   B13 )
 38 *              P-L ( 0     0    0  )
 39 *
 40 *  where the K-by-K matrix A12 and L-by-L matrix B13 are nonsingular
 41 *  upper triangular; A23 is L-by-L upper triangular if M-K-L >= 0,
 42 *  otherwise A23 is (M-K)-by-L upper trapezoidal.  K+L = the effective
 43 *  numerical rank of the (M+P)-by-N matrix (A**H,B**H)**H. 
 44 *
 45 *  This decomposition is the preprocessing step for computing the
 46 *  Generalized Singular Value Decomposition (GSVD), see subroutine
 47 *  ZGGSVD.
 48 *
 49 *  Arguments
 50 *  =========
 51 *
 52 *  JOBU    (input) CHARACTER*1
 53 *          = 'U':  Unitary matrix U is computed;
 54 *          = 'N':  U is not computed.
 55 *
 56 *  JOBV    (input) CHARACTER*1
 57 *          = 'V':  Unitary matrix V is computed;
 58 *          = 'N':  V is not computed.
 59 *
 60 *  JOBQ    (input) CHARACTER*1
 61 *          = 'Q':  Unitary matrix Q is computed;
 62 *          = 'N':  Q is not computed.
 63 *
 64 *  M       (input) INTEGER
 65 *          The number of rows of the matrix A.  M >= 0.
 66 *
 67 *  P       (input) INTEGER
 68 *          The number of rows of the matrix B.  P >= 0.
 69 *
 70 *  N       (input) INTEGER
 71 *          The number of columns of the matrices A and B.  N >= 0.
 72 *
 73 *  A       (input/output) COMPLEX*16 array, dimension (LDA,N)
 74 *          On entry, the M-by-N matrix A.
 75 *          On exit, A contains the triangular (or trapezoidal) matrix
 76 *          described in the Purpose section.
 77 *
 78 *  LDA     (input) INTEGER
 79 *          The leading dimension of the array A. LDA >= max(1,M).
 80 *
 81 *  B       (input/output) COMPLEX*16 array, dimension (LDB,N)
 82 *          On entry, the P-by-N matrix B.
 83 *          On exit, B contains the triangular matrix described in
 84 *          the Purpose section.
 85 *
 86 *  LDB     (input) INTEGER
 87 *          The leading dimension of the array B. LDB >= max(1,P).
 88 *
 89 *  TOLA    (input) DOUBLE PRECISION
 90 *  TOLB    (input) DOUBLE PRECISION
 91 *          TOLA and TOLB are the thresholds to determine the effective
 92 *          numerical rank of matrix B and a subblock of A. Generally,
 93 *          they are set to
 94 *             TOLA = MAX(M,N)*norm(A)*MAZHEPS,
 95 *             TOLB = MAX(P,N)*norm(B)*MAZHEPS.
 96 *          The size of TOLA and TOLB may affect the size of backward
 97 *          errors of the decomposition.
 98 *
 99 *  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**H,B**H)**H.
104 *
105 *  U       (output) COMPLEX*16 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*16 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*16 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) DOUBLE PRECISION array, dimension (2*N)
132 *
133 *  TAU     (workspace) COMPLEX*16 array, dimension (N)
134 *
135 *  WORK    (workspace) COMPLEX*16 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 ZGEQPF 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*16         CZERO, CONE
152       PARAMETER          ( CZERO = ( 0.0D+00.0D+0 ),
153      $                   CONE = ( 1.0D+00.0D+0 ) )
154 *     ..
155 *     .. Local Scalars ..
156       LOGICAL            FORWRD, WANTQ, WANTU, WANTV
157       INTEGER            I, J
158       COMPLEX*16         T
159 *     ..
160 *     .. External Functions ..
161       LOGICAL            LSAME
162       EXTERNAL           LSAME
163 *     ..
164 *     .. External Subroutines ..
165       EXTERNAL           XERBLA, ZGEQPF, ZGEQR2, ZGERQ2, ZLACPY, ZLAPMT,
166      $                   ZLASET, ZUNG2R, ZUNM2R, ZUNMR2
167 *     ..
168 *     .. Intrinsic Functions ..
169       INTRINSIC          ABSDBLEDIMAGMAXMIN
170 *     ..
171 *     .. Statement Functions ..
172       DOUBLE PRECISION   CABS1
173 *     ..
174 *     .. Statement Function definitions ..
175       CABS1( T ) = ABSDBLE( T ) ) + ABSDIMAG( 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.MAX1, M ) ) THEN
200          INFO = -8
201       ELSE IF( LDB.LT.MAX1, 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( 'ZGGSVP'-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 ZGEQPF( P, N, B, LDB, IWORK, TAU, WORK, RWORK, INFO )
222 *
223 *     Update A := A*P
224 *
225       CALL ZLAPMT( FORWRD, M, N, A, LDA, IWORK )
226 *
227 *     Determine the effective rank of matrix B.
228 *
229       L = 0
230       DO 20 I = 1MIN( 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 ZLASET( 'Full', P, P, CZERO, CZERO, V, LDV )
240          IF( P.GT.1 )
241      $      CALL ZLACPY( 'Lower', P-1, N, B( 21 ), LDB, V( 21 ),
242      $                   LDV )
243          CALL ZUNG2R( 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 ZLASET( 'Full', P-L, N, CZERO, CZERO, B( L+11 ), LDB )
255 *
256       IF( WANTQ ) THEN
257 *
258 *        Set Q = I and Update Q := Q*P
259 *
260          CALL ZLASET( 'Full', N, N, CZERO, CONE, Q, LDQ )
261          CALL ZLAPMT( FORWRD, N, N, Q, LDQ, IWORK )
262       END IF
263 *
264       IF( P.GE..AND. N.NE.L ) THEN
265 *
266 *        RQ factorization of ( S11 S12 ) = ( 0 S12 )*Z
267 *
268          CALL ZGERQ2( L, N, B, LDB, TAU, WORK, INFO )
269 *
270 *        Update A := A*Z**H
271 *
272          CALL ZUNMR2( 'Right''Conjugate transpose', M, N, L, B, LDB,
273      $                TAU, A, LDA, WORK, INFO )
274          IF( WANTQ ) THEN
275 *
276 *           Update Q := Q*Z**H
277 *
278             CALL ZUNMR2( '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 ZLASET( '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**H
299 *                      (  0   0  )
300 *
301       DO 70 I = 1, N - L
302          IWORK( I ) = 0
303    70 CONTINUE
304       CALL ZGEQPF( 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 = 1MIN( M, N-L )
310          IF( CABS1( A( I, I ) ).GT.TOLA )
311      $      K = K + 1
312    80 CONTINUE
313 *
314 *     Update A12 := U**H*A12, where A12 = A( 1:M, N-L+1:N )
315 *
316       CALL ZUNM2R( '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 ZLASET( 'Full', M, M, CZERO, CZERO, U, LDU )
324          IF( M.GT.1 )
325      $      CALL ZLACPY( 'Lower', M-1, N-L, A( 21 ), LDA, U( 21 ),
326      $                   LDU )
327          CALL ZUNG2R( 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 ZLAPMT( 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 ZLASET( 'Full', M-K, N-L, CZERO, CZERO, A( K+11 ), LDA )
347 *
348       IF( N-L.GT.K ) THEN
349 *
350 *        RQ factorization of ( T11 T12 ) = ( 0 T12 )*Z1
351 *
352          CALL ZGERQ2( 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**H
357 *
358             CALL ZUNMR2( '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 ZLASET( '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 ZGEQR2( 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 ZUNM2R( '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 ZGGSVP
401 *
402       END