001:       SUBROUTINE SGGGLM( N, M, P, A, LDA, B, LDB, D, X, Y, WORK, LWORK,
002:      $                   INFO )
003: *
004: *  -- LAPACK driver 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, * ), D( * ), WORK( * ),
013:      $                   X( * ), Y( * )
014: *     ..
015: *
016: *  Purpose
017: *  =======
018: *
019: *  SGGGLM solves a general Gauss-Markov linear model (GLM) problem:
020: *
021: *          minimize || y ||_2   subject to   d = A*x + B*y
022: *              x
023: *
024: *  where A is an N-by-M matrix, B is an N-by-P matrix, and d is a
025: *  given N-vector. It is assumed that M <= N <= M+P, and
026: *
027: *             rank(A) = M    and    rank( A B ) = N.
028: *
029: *  Under these assumptions, the constrained equation is always
030: *  consistent, and there is a unique solution x and a minimal 2-norm
031: *  solution y, which is obtained using a generalized QR factorization
032: *  of the matrices (A, B) given by
033: *
034: *     A = Q*(R),   B = Q*T*Z.
035: *           (0)
036: *
037: *  In particular, if matrix B is square nonsingular, then the problem
038: *  GLM is equivalent to the following weighted linear least squares
039: *  problem
040: *
041: *               minimize || inv(B)*(d-A*x) ||_2
042: *                   x
043: *
044: *  where inv(B) denotes the inverse of B.
045: *
046: *  Arguments
047: *  =========
048: *
049: *  N       (input) INTEGER
050: *          The number of rows of the matrices A and B.  N >= 0.
051: *
052: *  M       (input) INTEGER
053: *          The number of columns of the matrix A.  0 <= M <= N.
054: *
055: *  P       (input) INTEGER
056: *          The number of columns of the matrix B.  P >= N-M.
057: *
058: *  A       (input/output) REAL array, dimension (LDA,M)
059: *          On entry, the N-by-M matrix A.
060: *          On exit, the upper triangular part of the array A contains
061: *          the M-by-M upper triangular matrix R.
062: *
063: *  LDA     (input) INTEGER
064: *          The leading dimension of the array A. LDA >= max(1,N).
065: *
066: *  B       (input/output) REAL array, dimension (LDB,P)
067: *          On entry, the N-by-P matrix B.
068: *          On exit, if N <= P, the upper triangle of the subarray
069: *          B(1:N,P-N+1:P) contains the N-by-N upper triangular matrix T;
070: *          if N > P, the elements on and above the (N-P)th subdiagonal
071: *          contain the N-by-P upper trapezoidal matrix T.
072: *
073: *  LDB     (input) INTEGER
074: *          The leading dimension of the array B. LDB >= max(1,N).
075: *
076: *  D       (input/output) REAL array, dimension (N)
077: *          On entry, D is the left hand side of the GLM equation.
078: *          On exit, D is destroyed.
079: *
080: *  X       (output) REAL array, dimension (M)
081: *  Y       (output) REAL array, dimension (P)
082: *          On exit, X and Y are the solutions of the GLM problem.
083: *
084: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
085: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
086: *
087: *  LWORK   (input) INTEGER
088: *          The dimension of the array WORK. LWORK >= max(1,N+M+P).
089: *          For optimum performance, LWORK >= M+min(N,P)+max(N,P)*NB,
090: *          where NB is an upper bound for the optimal blocksizes for
091: *          SGEQRF, SGERQF, SORMQR and SORMRQ.
092: *
093: *          If LWORK = -1, then a workspace query is assumed; the routine
094: *          only calculates the optimal size of the WORK array, returns
095: *          this value as the first entry of the WORK array, and no error
096: *          message related to LWORK is issued by XERBLA.
097: *
098: *  INFO    (output) INTEGER
099: *          = 0:  successful exit.
100: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
101: *          = 1:  the upper triangular factor R associated with A in the
102: *                generalized QR factorization of the pair (A, B) is
103: *                singular, so that rank(A) < M; the least squares
104: *                solution could not be computed.
105: *          = 2:  the bottom (N-M) by (N-M) part of the upper trapezoidal
106: *                factor T associated with B in the generalized QR
107: *                factorization of the pair (A, B) is singular, so that
108: *                rank( A B ) < N; the least squares solution could not
109: *                be computed.
110: *
111: *  ===================================================================
112: *
113: *     .. Parameters ..
114:       REAL               ZERO, ONE
115:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
116: *     ..
117: *     .. Local Scalars ..
118:       LOGICAL            LQUERY
119:       INTEGER            I, LOPT, LWKMIN, LWKOPT, NB, NB1, NB2, NB3,
120:      $                   NB4, NP
121: *     ..
122: *     .. External Subroutines ..
123:       EXTERNAL           SCOPY, SGEMV, SGGQRF, SORMQR, SORMRQ, STRTRS,
124:      $                   XERBLA
125: *     ..
126: *     .. External Functions ..
127:       INTEGER            ILAENV
128:       EXTERNAL           ILAENV 
129: *     ..
130: *     .. Intrinsic Functions ..
131:       INTRINSIC          INT, MAX, MIN
132: *     ..
133: *     .. Executable Statements ..
134: *
135: *     Test the input parameters
136: *
137:       INFO = 0
138:       NP = MIN( N, P )
139:       LQUERY = ( LWORK.EQ.-1 )
140:       IF( N.LT.0 ) THEN
141:          INFO = -1
142:       ELSE IF( M.LT.0 .OR. M.GT.N ) THEN
143:          INFO = -2
144:       ELSE IF( P.LT.0 .OR. P.LT.N-M ) THEN
145:          INFO = -3
146:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
147:          INFO = -5
148:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
149:          INFO = -7
150:       END IF
151: *
152: *     Calculate workspace
153: *
154:       IF( INFO.EQ.0) THEN
155:          IF( N.EQ.0 ) THEN
156:             LWKMIN = 1
157:             LWKOPT = 1
158:          ELSE
159:             NB1 = ILAENV( 1, 'SGEQRF', ' ', N, M, -1, -1 )
160:             NB2 = ILAENV( 1, 'SGERQF', ' ', N, M, -1, -1 )
161:             NB3 = ILAENV( 1, 'SORMQR', ' ', N, M, P, -1 )
162:             NB4 = ILAENV( 1, 'SORMRQ', ' ', N, M, P, -1 )
163:             NB = MAX( NB1, NB2, NB3, NB4 )
164:             LWKMIN = M + N + P
165:             LWKOPT = M + NP + MAX( N, P )*NB
166:          END IF
167:          WORK( 1 ) = LWKOPT
168: *
169:          IF( LWORK.LT.LWKMIN .AND. .NOT.LQUERY ) THEN
170:             INFO = -12
171:          END IF
172:       END IF
173: *
174:       IF( INFO.NE.0 ) THEN
175:          CALL XERBLA( 'SGGGLM', -INFO )
176:          RETURN
177:       ELSE IF( LQUERY ) THEN
178:          RETURN
179:       END IF
180: *
181: *     Quick return if possible
182: *
183:       IF( N.EQ.0 )
184:      $   RETURN
185: *
186: *     Compute the GQR factorization of matrices A and B:
187: *
188: *            Q'*A = ( R11 ) M,    Q'*B*Z' = ( T11   T12 ) M
189: *                   (  0  ) N-M             (  0    T22 ) N-M
190: *                      M                     M+P-N  N-M
191: *
192: *     where R11 and T22 are upper triangular, and Q and Z are
193: *     orthogonal.
194: *
195:       CALL SGGQRF( N, M, P, A, LDA, WORK, B, LDB, WORK( M+1 ),
196:      $             WORK( M+NP+1 ), LWORK-M-NP, INFO )
197:       LOPT = WORK( M+NP+1 )
198: *
199: *     Update left-hand-side vector d = Q'*d = ( d1 ) M
200: *                                             ( d2 ) N-M
201: *
202:       CALL SORMQR( 'Left', 'Transpose', N, 1, M, A, LDA, WORK, D,
203:      $             MAX( 1, N ), WORK( M+NP+1 ), LWORK-M-NP, INFO )
204:       LOPT = MAX( LOPT, INT( WORK( M+NP+1 ) ) )
205: *
206: *     Solve T22*y2 = d2 for y2
207: *
208:       IF( N.GT.M ) THEN
209:          CALL STRTRS( 'Upper', 'No transpose', 'Non unit', N-M, 1,
210:      $                B( M+1, M+P-N+1 ), LDB, D( M+1 ), N-M, INFO )
211: *
212:          IF( INFO.GT.0 ) THEN
213:             INFO = 1
214:             RETURN
215:          END IF
216: *
217:          CALL SCOPY( N-M, D( M+1 ), 1, Y( M+P-N+1 ), 1 )
218:       END IF
219: *
220: *     Set y1 = 0
221: *
222:       DO 10 I = 1, M + P - N
223:          Y( I ) = ZERO
224:    10 CONTINUE
225: *
226: *     Update d1 = d1 - T12*y2
227: *
228:       CALL SGEMV( 'No transpose', M, N-M, -ONE, B( 1, M+P-N+1 ), LDB,
229:      $            Y( M+P-N+1 ), 1, ONE, D, 1 )
230: *
231: *     Solve triangular system: R11*x = d1
232: *
233:       IF( M.GT.0 ) THEN
234:          CALL STRTRS( 'Upper', 'No Transpose', 'Non unit', M, 1, A, LDA,
235:      $                D, M, INFO )
236: *
237:          IF( INFO.GT.0 ) THEN
238:             INFO = 2
239:             RETURN
240:          END IF
241: *
242: *        Copy D to X
243: *
244:          CALL SCOPY( M, D, 1, X, 1 )
245:       END IF
246: *
247: *     Backward transformation y = Z'*y
248: *
249:       CALL SORMRQ( 'Left', 'Transpose', P, 1, NP,
250:      $             B( MAX( 1, N-P+1 ), 1 ), LDB, WORK( M+1 ), Y,
251:      $             MAX( 1, P ), WORK( M+NP+1 ), LWORK-M-NP, INFO )
252:       WORK( 1 ) = M + NP + MAX( LOPT, INT( WORK( M+NP+1 ) ) )
253: *
254:       RETURN
255: *
256: *     End of SGGGLM
257: *
258:       END
259: