```001:       SUBROUTINE SGELS( TRANS, M, N, NRHS, A, LDA, B, LDB, 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:       CHARACTER          TRANS
010:       INTEGER            INFO, LDA, LDB, LWORK, M, N, NRHS
011: *     ..
012: *     .. Array Arguments ..
013:       REAL               A( LDA, * ), B( LDB, * ), WORK( * )
014: *     ..
015: *
016: *  Purpose
017: *  =======
018: *
019: *  SGELS solves overdetermined or underdetermined real linear systems
020: *  involving an M-by-N matrix A, or its transpose, using a QR or LQ
021: *  factorization of A.  It is assumed that A has full rank.
022: *
023: *  The following options are provided:
024: *
025: *  1. If TRANS = 'N' and m >= n:  find the least squares solution of
026: *     an overdetermined system, i.e., solve the least squares problem
027: *                  minimize || B - A*X ||.
028: *
029: *  2. If TRANS = 'N' and m < n:  find the minimum norm solution of
030: *     an underdetermined system A * X = B.
031: *
032: *  3. If TRANS = 'T' and m >= n:  find the minimum norm solution of
033: *     an undetermined system A**T * X = B.
034: *
035: *  4. If TRANS = 'T' and m < n:  find the least squares solution of
036: *     an overdetermined system, i.e., solve the least squares problem
037: *                  minimize || B - A**T * X ||.
038: *
039: *  Several right hand side vectors b and solution vectors x can be
040: *  handled in a single call; they are stored as the columns of the
041: *  M-by-NRHS right hand side matrix B and the N-by-NRHS solution
042: *  matrix X.
043: *
044: *  Arguments
045: *  =========
046: *
047: *  TRANS   (input) CHARACTER*1
048: *          = 'N': the linear system involves A;
049: *          = 'T': the linear system involves A**T.
050: *
051: *  M       (input) INTEGER
052: *          The number of rows of the matrix A.  M >= 0.
053: *
054: *  N       (input) INTEGER
055: *          The number of columns of the matrix A.  N >= 0.
056: *
057: *  NRHS    (input) INTEGER
058: *          The number of right hand sides, i.e., the number of
059: *          columns of the matrices B and X. NRHS >=0.
060: *
061: *  A       (input/output) REAL array, dimension (LDA,N)
062: *          On entry, the M-by-N matrix A.
063: *          On exit,
064: *            if M >= N, A is overwritten by details of its QR
065: *                       factorization as returned by SGEQRF;
066: *            if M <  N, A is overwritten by details of its LQ
067: *                       factorization as returned by SGELQF.
068: *
069: *  LDA     (input) INTEGER
070: *          The leading dimension of the array A.  LDA >= max(1,M).
071: *
072: *  B       (input/output) REAL array, dimension (LDB,NRHS)
073: *          On entry, the matrix B of right hand side vectors, stored
074: *          columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS
075: *          if TRANS = 'T'.
076: *          On exit, if INFO = 0, B is overwritten by the solution
077: *          vectors, stored columnwise:
078: *          if TRANS = 'N' and m >= n, rows 1 to n of B contain the least
079: *          squares solution vectors; the residual sum of squares for the
080: *          solution in each column is given by the sum of squares of
081: *          elements N+1 to M in that column;
082: *          if TRANS = 'N' and m < n, rows 1 to N of B contain the
083: *          minimum norm solution vectors;
084: *          if TRANS = 'T' and m >= n, rows 1 to M of B contain the
085: *          minimum norm solution vectors;
086: *          if TRANS = 'T' and m < n, rows 1 to M of B contain the
087: *          least squares solution vectors; the residual sum of squares
088: *          for the solution in each column is given by the sum of
089: *          squares of elements M+1 to N in that column.
090: *
091: *  LDB     (input) INTEGER
092: *          The leading dimension of the array B. LDB >= MAX(1,M,N).
093: *
094: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
095: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
096: *
097: *  LWORK   (input) INTEGER
098: *          The dimension of the array WORK.
099: *          LWORK >= max( 1, MN + max( MN, NRHS ) ).
100: *          For optimal performance,
101: *          LWORK >= max( 1, MN + max( MN, NRHS )*NB ).
102: *          where MN = min(M,N) and NB is the optimum block size.
103: *
104: *          If LWORK = -1, then a workspace query is assumed; the routine
105: *          only calculates the optimal size of the WORK array, returns
106: *          this value as the first entry of the WORK array, and no error
107: *          message related to LWORK is issued by XERBLA.
108: *
109: *  INFO    (output) INTEGER
110: *          = 0:  successful exit
111: *          < 0:  if INFO = -i, the i-th argument had an illegal value
112: *          > 0:  if INFO =  i, the i-th diagonal element of the
113: *                triangular factor of A is zero, so that A does not have
114: *                full rank; the least squares solution could not be
115: *                computed.
116: *
117: *  =====================================================================
118: *
119: *     .. Parameters ..
120:       REAL               ZERO, ONE
121:       PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0 )
122: *     ..
123: *     .. Local Scalars ..
124:       LOGICAL            LQUERY, TPSD
125:       INTEGER            BROW, I, IASCL, IBSCL, J, MN, NB, SCLLEN, WSIZE
126:       REAL               ANRM, BIGNUM, BNRM, SMLNUM
127: *     ..
128: *     .. Local Arrays ..
129:       REAL               RWORK( 1 )
130: *     ..
131: *     .. External Functions ..
132:       LOGICAL            LSAME
133:       INTEGER            ILAENV
134:       REAL               SLAMCH, SLANGE
135:       EXTERNAL           LSAME, ILAENV, SLAMCH, SLANGE
136: *     ..
137: *     .. External Subroutines ..
138:       EXTERNAL           SGELQF, SGEQRF, SLABAD, SLASCL, SLASET, SORMLQ,
139:      \$                   SORMQR, STRTRS, XERBLA
140: *     ..
141: *     .. Intrinsic Functions ..
142:       INTRINSIC          MAX, MIN, REAL
143: *     ..
144: *     .. Executable Statements ..
145: *
146: *     Test the input arguments.
147: *
148:       INFO = 0
149:       MN = MIN( M, N )
150:       LQUERY = ( LWORK.EQ.-1 )
151:       IF( .NOT.( LSAME( TRANS, 'N' ) .OR. LSAME( TRANS, 'T' ) ) ) THEN
152:          INFO = -1
153:       ELSE IF( M.LT.0 ) THEN
154:          INFO = -2
155:       ELSE IF( N.LT.0 ) THEN
156:          INFO = -3
157:       ELSE IF( NRHS.LT.0 ) THEN
158:          INFO = -4
159:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
160:          INFO = -6
161:       ELSE IF( LDB.LT.MAX( 1, M, N ) ) THEN
162:          INFO = -8
163:       ELSE IF( LWORK.LT.MAX( 1, MN + MAX( MN, NRHS ) ) .AND.
164:      \$   .NOT.LQUERY ) THEN
165:          INFO = -10
166:       END IF
167: *
168: *     Figure out optimal block size
169: *
170:       IF( INFO.EQ.0 .OR. INFO.EQ.-10 ) THEN
171: *
172:          TPSD = .TRUE.
173:          IF( LSAME( TRANS, 'N' ) )
174:      \$      TPSD = .FALSE.
175: *
176:          IF( M.GE.N ) THEN
177:             NB = ILAENV( 1, 'SGEQRF', ' ', M, N, -1, -1 )
178:             IF( TPSD ) THEN
179:                NB = MAX( NB, ILAENV( 1, 'SORMQR', 'LN', M, NRHS, N,
180:      \$              -1 ) )
181:             ELSE
182:                NB = MAX( NB, ILAENV( 1, 'SORMQR', 'LT', M, NRHS, N,
183:      \$              -1 ) )
184:             END IF
185:          ELSE
186:             NB = ILAENV( 1, 'SGELQF', ' ', M, N, -1, -1 )
187:             IF( TPSD ) THEN
188:                NB = MAX( NB, ILAENV( 1, 'SORMLQ', 'LT', N, NRHS, M,
189:      \$              -1 ) )
190:             ELSE
191:                NB = MAX( NB, ILAENV( 1, 'SORMLQ', 'LN', N, NRHS, M,
192:      \$              -1 ) )
193:             END IF
194:          END IF
195: *
196:          WSIZE = MAX( 1, MN + MAX( MN, NRHS )*NB )
197:          WORK( 1 ) = REAL( WSIZE )
198: *
199:       END IF
200: *
201:       IF( INFO.NE.0 ) THEN
202:          CALL XERBLA( 'SGELS ', -INFO )
203:          RETURN
204:       ELSE IF( LQUERY ) THEN
205:          RETURN
206:       END IF
207: *
208: *     Quick return if possible
209: *
210:       IF( MIN( M, N, NRHS ).EQ.0 ) THEN
211:          CALL SLASET( 'Full', MAX( M, N ), NRHS, ZERO, ZERO, B, LDB )
212:          RETURN
213:       END IF
214: *
215: *     Get machine parameters
216: *
217:       SMLNUM = SLAMCH( 'S' ) / SLAMCH( 'P' )
218:       BIGNUM = ONE / SMLNUM
219:       CALL SLABAD( SMLNUM, BIGNUM )
220: *
221: *     Scale A, B if max element outside range [SMLNUM,BIGNUM]
222: *
223:       ANRM = SLANGE( 'M', M, N, A, LDA, RWORK )
224:       IASCL = 0
225:       IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
226: *
227: *        Scale matrix norm up to SMLNUM
228: *
229:          CALL SLASCL( 'G', 0, 0, ANRM, SMLNUM, M, N, A, LDA, INFO )
230:          IASCL = 1
231:       ELSE IF( ANRM.GT.BIGNUM ) THEN
232: *
233: *        Scale matrix norm down to BIGNUM
234: *
235:          CALL SLASCL( 'G', 0, 0, ANRM, BIGNUM, M, N, A, LDA, INFO )
236:          IASCL = 2
237:       ELSE IF( ANRM.EQ.ZERO ) THEN
238: *
239: *        Matrix all zero. Return zero solution.
240: *
241:          CALL SLASET( 'F', MAX( M, N ), NRHS, ZERO, ZERO, B, LDB )
242:          GO TO 50
243:       END IF
244: *
245:       BROW = M
246:       IF( TPSD )
247:      \$   BROW = N
248:       BNRM = SLANGE( 'M', BROW, NRHS, B, LDB, RWORK )
249:       IBSCL = 0
250:       IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
251: *
252: *        Scale matrix norm up to SMLNUM
253: *
254:          CALL SLASCL( 'G', 0, 0, BNRM, SMLNUM, BROW, NRHS, B, LDB,
255:      \$                INFO )
256:          IBSCL = 1
257:       ELSE IF( BNRM.GT.BIGNUM ) THEN
258: *
259: *        Scale matrix norm down to BIGNUM
260: *
261:          CALL SLASCL( 'G', 0, 0, BNRM, BIGNUM, BROW, NRHS, B, LDB,
262:      \$                INFO )
263:          IBSCL = 2
264:       END IF
265: *
266:       IF( M.GE.N ) THEN
267: *
268: *        compute QR factorization of A
269: *
270:          CALL SGEQRF( M, N, A, LDA, WORK( 1 ), WORK( MN+1 ), LWORK-MN,
271:      \$                INFO )
272: *
273: *        workspace at least N, optimally N*NB
274: *
275:          IF( .NOT.TPSD ) THEN
276: *
277: *           Least-Squares Problem min || A * X - B ||
278: *
279: *           B(1:M,1:NRHS) := Q' * B(1:M,1:NRHS)
280: *
281:             CALL SORMQR( 'Left', 'Transpose', M, NRHS, N, A, LDA,
282:      \$                   WORK( 1 ), B, LDB, WORK( MN+1 ), LWORK-MN,
283:      \$                   INFO )
284: *
285: *           workspace at least NRHS, optimally NRHS*NB
286: *
287: *           B(1:N,1:NRHS) := inv(R) * B(1:N,1:NRHS)
288: *
289:             CALL STRTRS( 'Upper', 'No transpose', 'Non-unit', N, NRHS,
290:      \$                   A, LDA, B, LDB, INFO )
291: *
292:             IF( INFO.GT.0 ) THEN
293:                RETURN
294:             END IF
295: *
296:             SCLLEN = N
297: *
298:          ELSE
299: *
300: *           Overdetermined system of equations A' * X = B
301: *
302: *           B(1:N,1:NRHS) := inv(R') * B(1:N,1:NRHS)
303: *
304:             CALL STRTRS( 'Upper', 'Transpose', 'Non-unit', N, NRHS,
305:      \$                   A, LDA, B, LDB, INFO )
306: *
307:             IF( INFO.GT.0 ) THEN
308:                RETURN
309:             END IF
310: *
311: *           B(N+1:M,1:NRHS) = ZERO
312: *
313:             DO 20 J = 1, NRHS
314:                DO 10 I = N + 1, M
315:                   B( I, J ) = ZERO
316:    10          CONTINUE
317:    20       CONTINUE
318: *
319: *           B(1:M,1:NRHS) := Q(1:N,:) * B(1:N,1:NRHS)
320: *
321:             CALL SORMQR( 'Left', 'No transpose', M, NRHS, N, A, LDA,
322:      \$                   WORK( 1 ), B, LDB, WORK( MN+1 ), LWORK-MN,
323:      \$                   INFO )
324: *
325: *           workspace at least NRHS, optimally NRHS*NB
326: *
327:             SCLLEN = M
328: *
329:          END IF
330: *
331:       ELSE
332: *
333: *        Compute LQ factorization of A
334: *
335:          CALL SGELQF( M, N, A, LDA, WORK( 1 ), WORK( MN+1 ), LWORK-MN,
336:      \$                INFO )
337: *
338: *        workspace at least M, optimally M*NB.
339: *
340:          IF( .NOT.TPSD ) THEN
341: *
342: *           underdetermined system of equations A * X = B
343: *
344: *           B(1:M,1:NRHS) := inv(L) * B(1:M,1:NRHS)
345: *
346:             CALL STRTRS( 'Lower', 'No transpose', 'Non-unit', M, NRHS,
347:      \$                   A, LDA, B, LDB, INFO )
348: *
349:             IF( INFO.GT.0 ) THEN
350:                RETURN
351:             END IF
352: *
353: *           B(M+1:N,1:NRHS) = 0
354: *
355:             DO 40 J = 1, NRHS
356:                DO 30 I = M + 1, N
357:                   B( I, J ) = ZERO
358:    30          CONTINUE
359:    40       CONTINUE
360: *
361: *           B(1:N,1:NRHS) := Q(1:N,:)' * B(1:M,1:NRHS)
362: *
363:             CALL SORMLQ( 'Left', 'Transpose', N, NRHS, M, A, LDA,
364:      \$                   WORK( 1 ), B, LDB, WORK( MN+1 ), LWORK-MN,
365:      \$                   INFO )
366: *
367: *           workspace at least NRHS, optimally NRHS*NB
368: *
369:             SCLLEN = N
370: *
371:          ELSE
372: *
373: *           overdetermined system min || A' * X - B ||
374: *
375: *           B(1:N,1:NRHS) := Q * B(1:N,1:NRHS)
376: *
377:             CALL SORMLQ( 'Left', 'No transpose', N, NRHS, M, A, LDA,
378:      \$                   WORK( 1 ), B, LDB, WORK( MN+1 ), LWORK-MN,
379:      \$                   INFO )
380: *
381: *           workspace at least NRHS, optimally NRHS*NB
382: *
383: *           B(1:M,1:NRHS) := inv(L') * B(1:M,1:NRHS)
384: *
385:             CALL STRTRS( 'Lower', 'Transpose', 'Non-unit', M, NRHS,
386:      \$                   A, LDA, B, LDB, INFO )
387: *
388:             IF( INFO.GT.0 ) THEN
389:                RETURN
390:             END IF
391: *
392:             SCLLEN = M
393: *
394:          END IF
395: *
396:       END IF
397: *
398: *     Undo scaling
399: *
400:       IF( IASCL.EQ.1 ) THEN
401:          CALL SLASCL( 'G', 0, 0, ANRM, SMLNUM, SCLLEN, NRHS, B, LDB,
402:      \$                INFO )
403:       ELSE IF( IASCL.EQ.2 ) THEN
404:          CALL SLASCL( 'G', 0, 0, ANRM, BIGNUM, SCLLEN, NRHS, B, LDB,
405:      \$                INFO )
406:       END IF
407:       IF( IBSCL.EQ.1 ) THEN
408:          CALL SLASCL( 'G', 0, 0, SMLNUM, BNRM, SCLLEN, NRHS, B, LDB,
409:      \$                INFO )
410:       ELSE IF( IBSCL.EQ.2 ) THEN
411:          CALL SLASCL( 'G', 0, 0, BIGNUM, BNRM, SCLLEN, NRHS, B, LDB,
412:      \$                INFO )
413:       END IF
414: *
415:    50 CONTINUE
416:       WORK( 1 ) = REAL( WSIZE )
417: *
418:       RETURN
419: *
420: *     End of SGELS
421: *
422:       END
423: ```