001:       SUBROUTINE SGGRQF( M, P, N, A, LDA, TAUA, B, LDB, TAUB, WORK,
002:      $                   LWORK, INFO )
003: *
004: *  -- LAPACK routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       INTEGER            INFO, LDA, LDB, LWORK, M, N, P
010: *     ..
011: *     .. Array Arguments ..
012:       REAL               A( LDA, * ), B( LDB, * ), TAUA( * ), TAUB( * ),
013:      $                   WORK( * )
014: *     ..
015: *
016: *  Purpose
017: *  =======
018: *
019: *  SGGRQF computes a generalized RQ factorization of an M-by-N matrix A
020: *  and a P-by-N matrix B:
021: *
022: *              A = R*Q,        B = Z*T*Q,
023: *
024: *  where Q is an N-by-N orthogonal matrix, Z is a P-by-P orthogonal
025: *  matrix, and R and T assume one of the forms:
026: *
027: *  if M <= N,  R = ( 0  R12 ) M,   or if M > N,  R = ( R11 ) M-N,
028: *                   N-M  M                           ( R21 ) N
029: *                                                       N
030: *
031: *  where R12 or R21 is upper triangular, and
032: *
033: *  if P >= N,  T = ( T11 ) N  ,   or if P < N,  T = ( T11  T12 ) P,
034: *                  (  0  ) P-N                         P   N-P
035: *                     N
036: *
037: *  where T11 is upper triangular.
038: *
039: *  In particular, if B is square and nonsingular, the GRQ factorization
040: *  of A and B implicitly gives the RQ factorization of A*inv(B):
041: *
042: *               A*inv(B) = (R*inv(T))*Z'
043: *
044: *  where inv(B) denotes the inverse of the matrix B, and Z' denotes the
045: *  transpose of the matrix Z.
046: *
047: *  Arguments
048: *  =========
049: *
050: *  M       (input) INTEGER
051: *          The number of rows of the matrix A.  M >= 0.
052: *
053: *  P       (input) INTEGER
054: *          The number of rows of the matrix B.  P >= 0.
055: *
056: *  N       (input) INTEGER
057: *          The number of columns of the matrices A and B. N >= 0.
058: *
059: *  A       (input/output) REAL array, dimension (LDA,N)
060: *          On entry, the M-by-N matrix A.
061: *          On exit, if M <= N, the upper triangle of the subarray
062: *          A(1:M,N-M+1:N) contains the M-by-M upper triangular matrix R;
063: *          if M > N, the elements on and above the (M-N)-th subdiagonal
064: *          contain the M-by-N upper trapezoidal matrix R; the remaining
065: *          elements, with the array TAUA, represent the orthogonal
066: *          matrix Q as a product of elementary reflectors (see Further
067: *          Details).
068: *
069: *  LDA     (input) INTEGER
070: *          The leading dimension of the array A. LDA >= max(1,M).
071: *
072: *  TAUA    (output) REAL array, dimension (min(M,N))
073: *          The scalar factors of the elementary reflectors which
074: *          represent the orthogonal matrix Q (see Further Details).
075: *
076: *  B       (input/output) REAL array, dimension (LDB,N)
077: *          On entry, the P-by-N matrix B.
078: *          On exit, the elements on and above the diagonal of the array
079: *          contain the min(P,N)-by-N upper trapezoidal matrix T (T is
080: *          upper triangular if P >= N); the elements below the diagonal,
081: *          with the array TAUB, represent the orthogonal matrix Z as a
082: *          product of elementary reflectors (see Further Details).
083: *
084: *  LDB     (input) INTEGER
085: *          The leading dimension of the array B. LDB >= max(1,P).
086: *
087: *  TAUB    (output) REAL array, dimension (min(P,N))
088: *          The scalar factors of the elementary reflectors which
089: *          represent the orthogonal matrix Z (see Further Details).
090: *
091: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
092: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
093: *
094: *  LWORK   (input) INTEGER
095: *          The dimension of the array WORK. LWORK >= max(1,N,M,P).
096: *          For optimum performance LWORK >= max(N,M,P)*max(NB1,NB2,NB3),
097: *          where NB1 is the optimal blocksize for the RQ factorization
098: *          of an M-by-N matrix, NB2 is the optimal blocksize for the
099: *          QR factorization of a P-by-N matrix, and NB3 is the optimal
100: *          blocksize for a call of SORMRQ.
101: *
102: *          If LWORK = -1, then a workspace query is assumed; the routine
103: *          only calculates the optimal size of the WORK array, returns
104: *          this value as the first entry of the WORK array, and no error
105: *          message related to LWORK is issued by XERBLA.
106: *
107: *  INFO    (output) INTEGER
108: *          = 0:  successful exit
109: *          < 0:  if INF0= -i, the i-th argument had an illegal value.
110: *
111: *  Further Details
112: *  ===============
113: *
114: *  The matrix Q is represented as a product of elementary reflectors
115: *
116: *     Q = H(1) H(2) . . . H(k), where k = min(m,n).
117: *
118: *  Each H(i) has the form
119: *
120: *     H(i) = I - taua * v * v'
121: *
122: *  where taua is a real scalar, and v is a real vector with
123: *  v(n-k+i+1:n) = 0 and v(n-k+i) = 1; v(1:n-k+i-1) is stored on exit in
124: *  A(m-k+i,1:n-k+i-1), and taua in TAUA(i).
125: *  To form Q explicitly, use LAPACK subroutine SORGRQ.
126: *  To use Q to update another matrix, use LAPACK subroutine SORMRQ.
127: *
128: *  The matrix Z is represented as a product of elementary reflectors
129: *
130: *     Z = H(1) H(2) . . . H(k), where k = min(p,n).
131: *
132: *  Each H(i) has the form
133: *
134: *     H(i) = I - taub * v * v'
135: *
136: *  where taub is a real scalar, and v is a real vector with
137: *  v(1:i-1) = 0 and v(i) = 1; v(i+1:p) is stored on exit in B(i+1:p,i),
138: *  and taub in TAUB(i).
139: *  To form Z explicitly, use LAPACK subroutine SORGQR.
140: *  To use Z to update another matrix, use LAPACK subroutine SORMQR.
141: *
142: *  =====================================================================
143: *
144: *     .. Local Scalars ..
145:       LOGICAL            LQUERY
146:       INTEGER            LOPT, LWKOPT, NB, NB1, NB2, NB3
147: *     ..
148: *     .. External Subroutines ..
149:       EXTERNAL           SGEQRF, SGERQF, SORMRQ, XERBLA
150: *     ..
151: *     .. External Functions ..
152:       INTEGER            ILAENV 
153:       EXTERNAL           ILAENV 
154: *     ..
155: *     .. Intrinsic Functions ..
156:       INTRINSIC          INT, MAX, MIN
157: *     ..
158: *     .. Executable Statements ..
159: *
160: *     Test the input parameters
161: *
162:       INFO = 0
163:       NB1 = ILAENV( 1, 'SGERQF', ' ', M, N, -1, -1 )
164:       NB2 = ILAENV( 1, 'SGEQRF', ' ', P, N, -1, -1 )
165:       NB3 = ILAENV( 1, 'SORMRQ', ' ', M, N, P, -1 )
166:       NB = MAX( NB1, NB2, NB3 )
167:       LWKOPT = MAX( N, M, P)*NB
168:       WORK( 1 ) = LWKOPT
169:       LQUERY = ( LWORK.EQ.-1 )
170:       IF( M.LT.0 ) THEN
171:          INFO = -1
172:       ELSE IF( P.LT.0 ) THEN
173:          INFO = -2
174:       ELSE IF( N.LT.0 ) THEN
175:          INFO = -3
176:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
177:          INFO = -5
178:       ELSE IF( LDB.LT.MAX( 1, P ) ) THEN
179:          INFO = -8
180:       ELSE IF( LWORK.LT.MAX( 1, M, P, N ) .AND. .NOT.LQUERY ) THEN
181:          INFO = -11
182:       END IF
183:       IF( INFO.NE.0 ) THEN
184:          CALL XERBLA( 'SGGRQF', -INFO )
185:          RETURN
186:       ELSE IF( LQUERY ) THEN
187:          RETURN
188:       END IF
189: *
190: *     RQ factorization of M-by-N matrix A: A = R*Q
191: *
192:       CALL SGERQF( M, N, A, LDA, TAUA, WORK, LWORK, INFO )
193:       LOPT = WORK( 1 )
194: *
195: *     Update B := B*Q'
196: *
197:       CALL SORMRQ( 'Right', 'Transpose', P, N, MIN( M, N ),
198:      $             A( MAX( 1, M-N+1 ), 1 ), LDA, TAUA, B, LDB, WORK,
199:      $             LWORK, INFO )
200:       LOPT = MAX( LOPT, INT( WORK( 1 ) ) )
201: *
202: *     QR factorization of P-by-N matrix B: B = Z*T
203: *
204:       CALL SGEQRF( P, N, B, LDB, TAUB, WORK, LWORK, INFO )
205:       WORK( 1 ) = MAX( LOPT, INT( WORK( 1 ) ) )
206: *
207:       RETURN
208: *
209: *     End of SGGRQF
210: *
211:       END
212: