1       SUBROUTINE DGGSVP( JOBU, JOBV, JOBQ, M, P, N, A, LDA, B, LDB,
  2      $                   TOLA, TOLB, K, L, U, LDU, V, LDV, Q, LDQ,
  3      $                   IWORK, 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   A( LDA, * ), B( LDB, * ), Q( LDQ, * ),
 18      $                   TAU( * ), U( LDU, * ), V( LDV, * ), WORK( * )
 19 *     ..
 20 *
 21 *  Purpose
 22 *  =======
 23 *
 24 *  DGGSVP computes orthogonal matrices U, V and Q such that
 25 *
 26 *                     N-K-L  K    L
 27 *   U**T*A*Q =     K ( 0    A12  A13 )  if M-K-L >= 0;
 28 *                  L ( 0     0   A23 )
 29 *              M-K-L ( 0     0    0  )
 30 *
 31 *                   N-K-L  K    L
 32 *          =     K ( 0    A12  A13 )  if M-K-L < 0;
 33 *              M-K ( 0     0   A23 )
 34 *
 35 *                   N-K-L  K    L
 36 *   V**T*B*Q =   L ( 0     0   B13 )
 37 *              P-L ( 0     0    0  )
 38 *
 39 *  where the K-by-K matrix A12 and L-by-L matrix B13 are nonsingular
 40 *  upper triangular; A23 is L-by-L upper triangular if M-K-L >= 0,
 41 *  otherwise A23 is (M-K)-by-L upper trapezoidal.  K+L = the effective
 42 *  numerical rank of the (M+P)-by-N matrix (A**T,B**T)**T. 
 43 *
 44 *  This decomposition is the preprocessing step for computing the
 45 *  Generalized Singular Value Decomposition (GSVD), see subroutine
 46 *  DGGSVD.
 47 *
 48 *  Arguments
 49 *  =========
 50 *
 51 *  JOBU    (input) CHARACTER*1
 52 *          = 'U':  Orthogonal matrix U is computed;
 53 *          = 'N':  U is not computed.
 54 *
 55 *  JOBV    (input) CHARACTER*1
 56 *          = 'V':  Orthogonal matrix V is computed;
 57 *          = 'N':  V is not computed.
 58 *
 59 *  JOBQ    (input) CHARACTER*1
 60 *          = 'Q':  Orthogonal matrix Q is computed;
 61 *          = 'N':  Q is not computed.
 62 *
 63 *  M       (input) INTEGER
 64 *          The number of rows of the matrix A.  M >= 0.
 65 *
 66 *  P       (input) INTEGER
 67 *          The number of rows of the matrix B.  P >= 0.
 68 *
 69 *  N       (input) INTEGER
 70 *          The number of columns of the matrices A and B.  N >= 0.
 71 *
 72 *  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)
 73 *          On entry, the M-by-N matrix A.
 74 *          On exit, A contains the triangular (or trapezoidal) matrix
 75 *          described in the Purpose section.
 76 *
 77 *  LDA     (input) INTEGER
 78 *          The leading dimension of the array A. LDA >= max(1,M).
 79 *
 80 *  B       (input/output) DOUBLE PRECISION array, dimension (LDB,N)
 81 *          On entry, the P-by-N matrix B.
 82 *          On exit, B contains the triangular matrix described in
 83 *          the Purpose section.
 84 *
 85 *  LDB     (input) INTEGER
 86 *          The leading dimension of the array B. LDB >= max(1,P).
 87 *
 88 *  TOLA    (input) DOUBLE PRECISION
 89 *  TOLB    (input) DOUBLE PRECISION
 90 *          TOLA and TOLB are the thresholds to determine the effective
 91 *          numerical rank of matrix B and a subblock of A. Generally,
 92 *          they are set to
 93 *             TOLA = MAX(M,N)*norm(A)*MAZHEPS,
 94 *             TOLB = MAX(P,N)*norm(B)*MAZHEPS.
 95 *          The size of TOLA and TOLB may affect the size of backward
 96 *          errors of the decomposition.
 97 *
 98 *  K       (output) INTEGER
 99 *  L       (output) INTEGER
100 *          On exit, K and L specify the dimension of the subblocks
101 *          described in Purpose section.
102 *          K + L = effective numerical rank of (A**T,B**T)**T.
103 *
104 *  U       (output) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION array, dimension (N)
131 *
132 *  WORK    (workspace) DOUBLE PRECISION 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 DGEQPF 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       DOUBLE PRECISION   ZERO, ONE
150       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+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           DGEQPF, DGEQR2, DGERQ2, DLACPY, DLAPMT, DLASET,
162      $                   DORG2R, DORM2R, DORMR2, XERBLA
163 *     ..
164 *     .. Intrinsic Functions ..
165       INTRINSIC          ABSMAXMIN
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.MAX1, M ) ) THEN
190          INFO = -8
191       ELSE IF( LDB.LT.MAX1, 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( 'DGGSVP'-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 DGEQPF( P, N, B, LDB, IWORK, TAU, WORK, INFO )
212 *
213 *     Update A := A*P
214 *
215       CALL DLAPMT( FORWRD, M, N, A, LDA, IWORK )
216 *
217 *     Determine the effective rank of matrix B.
218 *
219       L = 0
220       DO 20 I = 1MIN( P, N )
221          IFABS( 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 DLASET( 'Full', P, P, ZERO, ZERO, V, LDV )
230          IF( P.GT.1 )
231      $      CALL DLACPY( 'Lower', P-1, N, B( 21 ), LDB, V( 21 ),
232      $                   LDV )
233          CALL DORG2R( 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 DLASET( 'Full', P-L, N, ZERO, ZERO, B( L+11 ), LDB )
245 *
246       IF( WANTQ ) THEN
247 *
248 *        Set Q = I and Update Q := Q*P
249 *
250          CALL DLASET( 'Full', N, N, ZERO, ONE, Q, LDQ )
251          CALL DLAPMT( FORWRD, N, N, Q, LDQ, IWORK )
252       END IF
253 *
254       IF( P.GE..AND. N.NE.L ) THEN
255 *
256 *        RQ factorization of (S11 S12): ( S11 S12 ) = ( 0 S12 )*Z
257 *
258          CALL DGERQ2( L, N, B, LDB, TAU, WORK, INFO )
259 *
260 *        Update A := A*Z**T
261 *
262          CALL DORMR2( 'Right''Transpose', M, N, L, B, LDB, TAU, A,
263      $                LDA, WORK, INFO )
264 *
265          IF( WANTQ ) THEN
266 *
267 *           Update Q := Q*Z**T
268 *
269             CALL DORMR2( 'Right''Transpose', N, N, L, B, LDB, TAU, Q,
270      $                   LDQ, WORK, INFO )
271          END IF
272 *
273 *        Clean up B
274 *
275          CALL DLASET( '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**T
290 *                      (  0   0  )
291 *
292       DO 70 I = 1, N - L
293          IWORK( I ) = 0
294    70 CONTINUE
295       CALL DGEQPF( 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 = 1MIN( M, N-L )
301          IFABS( A( I, I ) ).GT.TOLA )
302      $      K = K + 1
303    80 CONTINUE
304 *
305 *     Update A12 := U**T*A12, where A12 = A( 1:M, N-L+1:N )
306 *
307       CALL DORM2R( '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 DLASET( 'Full', M, M, ZERO, ZERO, U, LDU )
315          IF( M.GT.1 )
316      $      CALL DLACPY( 'Lower', M-1, N-L, A( 21 ), LDA, U( 21 ),
317      $                   LDU )
318          CALL DORG2R( 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 DLAPMT( 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 DLASET( 'Full', M-K, N-L, ZERO, ZERO, A( K+11 ), LDA )
338 *
339       IF( N-L.GT.K ) THEN
340 *
341 *        RQ factorization of ( T11 T12 ) = ( 0 T12 )*Z1
342 *
343          CALL DGERQ2( 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**T
348 *
349             CALL DORMR2( '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 DLASET( '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 DGEQR2( 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 DORM2R( '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 DGGSVP
392 *
393       END