001:       SUBROUTINE SGELSX( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK,
002:      $                   WORK, 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, M, N, NRHS, RANK
010:       REAL               RCOND
011: *     ..
012: *     .. Array Arguments ..
013:       INTEGER            JPVT( * )
014:       REAL               A( LDA, * ), B( LDB, * ), WORK( * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  This routine is deprecated and has been replaced by routine SGELSY.
021: *
022: *  SGELSX computes the minimum-norm solution to a real linear least
023: *  squares problem:
024: *      minimize || A * X - B ||
025: *  using a complete orthogonal factorization of A.  A is an M-by-N
026: *  matrix which may be rank-deficient.
027: *
028: *  Several right hand side vectors b and solution vectors x can be 
029: *  handled in a single call; they are stored as the columns of the
030: *  M-by-NRHS right hand side matrix B and the N-by-NRHS solution
031: *  matrix X.
032: *
033: *  The routine first computes a QR factorization with column pivoting:
034: *      A * P = Q * [ R11 R12 ]
035: *                  [  0  R22 ]
036: *  with R11 defined as the largest leading submatrix whose estimated
037: *  condition number is less than 1/RCOND.  The order of R11, RANK,
038: *  is the effective rank of A.
039: *
040: *  Then, R22 is considered to be negligible, and R12 is annihilated
041: *  by orthogonal transformations from the right, arriving at the
042: *  complete orthogonal factorization:
043: *     A * P = Q * [ T11 0 ] * Z
044: *                 [  0  0 ]
045: *  The minimum-norm solution is then
046: *     X = P * Z' [ inv(T11)*Q1'*B ]
047: *                [        0       ]
048: *  where Q1 consists of the first RANK columns of Q.
049: *
050: *  Arguments
051: *  =========
052: *
053: *  M       (input) INTEGER
054: *          The number of rows of the matrix A.  M >= 0.
055: *
056: *  N       (input) INTEGER
057: *          The number of columns of the matrix A.  N >= 0.
058: *
059: *  NRHS    (input) INTEGER
060: *          The number of right hand sides, i.e., the number of
061: *          columns of matrices B and X. NRHS >= 0.
062: *
063: *  A       (input/output) REAL array, dimension (LDA,N)
064: *          On entry, the M-by-N matrix A.
065: *          On exit, A has been overwritten by details of its
066: *          complete orthogonal factorization.
067: *
068: *  LDA     (input) INTEGER
069: *          The leading dimension of the array A.  LDA >= max(1,M).
070: *
071: *  B       (input/output) REAL array, dimension (LDB,NRHS)
072: *          On entry, the M-by-NRHS right hand side matrix B.
073: *          On exit, the N-by-NRHS solution matrix X.
074: *          If m >= n and RANK = n, the residual sum-of-squares for
075: *          the solution in the i-th column is given by the sum of
076: *          squares of elements N+1:M in that column.
077: *
078: *  LDB     (input) INTEGER
079: *          The leading dimension of the array B. LDB >= max(1,M,N).
080: *
081: *  JPVT    (input/output) INTEGER array, dimension (N)
082: *          On entry, if JPVT(i) .ne. 0, the i-th column of A is an
083: *          initial column, otherwise it is a free column.  Before
084: *          the QR factorization of A, all initial columns are
085: *          permuted to the leading positions; only the remaining
086: *          free columns are moved as a result of column pivoting
087: *          during the factorization.
088: *          On exit, if JPVT(i) = k, then the i-th column of A*P
089: *          was the k-th column of A.
090: *
091: *  RCOND   (input) REAL
092: *          RCOND is used to determine the effective rank of A, which
093: *          is defined as the order of the largest leading triangular
094: *          submatrix R11 in the QR factorization with pivoting of A,
095: *          whose estimated condition number < 1/RCOND.
096: *
097: *  RANK    (output) INTEGER
098: *          The effective rank of A, i.e., the order of the submatrix
099: *          R11.  This is the same as the order of the submatrix T11
100: *          in the complete orthogonal factorization of A.
101: *
102: *  WORK    (workspace) REAL array, dimension
103: *                      (max( min(M,N)+3*N, 2*min(M,N)+NRHS )),
104: *
105: *  INFO    (output) INTEGER
106: *          = 0:  successful exit
107: *          < 0:  if INFO = -i, the i-th argument had an illegal value
108: *
109: *  =====================================================================
110: *
111: *     .. Parameters ..
112:       INTEGER            IMAX, IMIN
113:       PARAMETER          ( IMAX = 1, IMIN = 2 )
114:       REAL               ZERO, ONE, DONE, NTDONE
115:       PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0, DONE = ZERO,
116:      $                   NTDONE = ONE )
117: *     ..
118: *     .. Local Scalars ..
119:       INTEGER            I, IASCL, IBSCL, ISMAX, ISMIN, J, K, MN
120:       REAL               ANRM, BIGNUM, BNRM, C1, C2, S1, S2, SMAX,
121:      $                   SMAXPR, SMIN, SMINPR, SMLNUM, T1, T2
122: *     ..
123: *     .. External Functions ..
124:       REAL               SLAMCH, SLANGE
125:       EXTERNAL           SLAMCH, SLANGE
126: *     ..
127: *     .. External Subroutines ..
128:       EXTERNAL           SGEQPF, SLABAD, SLAIC1, SLASCL, SLASET, SLATZM,
129:      $                   SORM2R, STRSM, STZRQF, XERBLA
130: *     ..
131: *     .. Intrinsic Functions ..
132:       INTRINSIC          ABS, MAX, MIN
133: *     ..
134: *     .. Executable Statements ..
135: *
136:       MN = MIN( M, N )
137:       ISMIN = MN + 1
138:       ISMAX = 2*MN + 1
139: *
140: *     Test the input arguments.
141: *
142:       INFO = 0
143:       IF( M.LT.0 ) THEN
144:          INFO = -1
145:       ELSE IF( N.LT.0 ) THEN
146:          INFO = -2
147:       ELSE IF( NRHS.LT.0 ) THEN
148:          INFO = -3
149:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
150:          INFO = -5
151:       ELSE IF( LDB.LT.MAX( 1, M, N ) ) THEN
152:          INFO = -7
153:       END IF
154: *
155:       IF( INFO.NE.0 ) THEN
156:          CALL XERBLA( 'SGELSX', -INFO )
157:          RETURN
158:       END IF
159: *
160: *     Quick return if possible
161: *
162:       IF( MIN( M, N, NRHS ).EQ.0 ) THEN
163:          RANK = 0
164:          RETURN
165:       END IF
166: *
167: *     Get machine parameters
168: *
169:       SMLNUM = SLAMCH( 'S' ) / SLAMCH( 'P' )
170:       BIGNUM = ONE / SMLNUM
171:       CALL SLABAD( SMLNUM, BIGNUM )
172: *
173: *     Scale A, B if max elements outside range [SMLNUM,BIGNUM]
174: *
175:       ANRM = SLANGE( 'M', M, N, A, LDA, WORK )
176:       IASCL = 0
177:       IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
178: *
179: *        Scale matrix norm up to SMLNUM
180: *
181:          CALL SLASCL( 'G', 0, 0, ANRM, SMLNUM, M, N, A, LDA, INFO )
182:          IASCL = 1
183:       ELSE IF( ANRM.GT.BIGNUM ) THEN
184: *
185: *        Scale matrix norm down to BIGNUM
186: *
187:          CALL SLASCL( 'G', 0, 0, ANRM, BIGNUM, M, N, A, LDA, INFO )
188:          IASCL = 2
189:       ELSE IF( ANRM.EQ.ZERO ) THEN
190: *
191: *        Matrix all zero. Return zero solution.
192: *
193:          CALL SLASET( 'F', MAX( M, N ), NRHS, ZERO, ZERO, B, LDB )
194:          RANK = 0
195:          GO TO 100
196:       END IF
197: *
198:       BNRM = SLANGE( 'M', M, NRHS, B, LDB, WORK )
199:       IBSCL = 0
200:       IF( BNRM.GT.ZERO .AND. BNRM.LT.SMLNUM ) THEN
201: *
202: *        Scale matrix norm up to SMLNUM
203: *
204:          CALL SLASCL( 'G', 0, 0, BNRM, SMLNUM, M, NRHS, B, LDB, INFO )
205:          IBSCL = 1
206:       ELSE IF( BNRM.GT.BIGNUM ) THEN
207: *
208: *        Scale matrix norm down to BIGNUM
209: *
210:          CALL SLASCL( 'G', 0, 0, BNRM, BIGNUM, M, NRHS, B, LDB, INFO )
211:          IBSCL = 2
212:       END IF
213: *
214: *     Compute QR factorization with column pivoting of A:
215: *        A * P = Q * R
216: *
217:       CALL SGEQPF( M, N, A, LDA, JPVT, WORK( 1 ), WORK( MN+1 ), INFO )
218: *
219: *     workspace 3*N. Details of Householder rotations stored
220: *     in WORK(1:MN).
221: *
222: *     Determine RANK using incremental condition estimation
223: *
224:       WORK( ISMIN ) = ONE
225:       WORK( ISMAX ) = ONE
226:       SMAX = ABS( A( 1, 1 ) )
227:       SMIN = SMAX
228:       IF( ABS( A( 1, 1 ) ).EQ.ZERO ) THEN
229:          RANK = 0
230:          CALL SLASET( 'F', MAX( M, N ), NRHS, ZERO, ZERO, B, LDB )
231:          GO TO 100
232:       ELSE
233:          RANK = 1
234:       END IF
235: *
236:    10 CONTINUE
237:       IF( RANK.LT.MN ) THEN
238:          I = RANK + 1
239:          CALL SLAIC1( IMIN, RANK, WORK( ISMIN ), SMIN, A( 1, I ),
240:      $                A( I, I ), SMINPR, S1, C1 )
241:          CALL SLAIC1( IMAX, RANK, WORK( ISMAX ), SMAX, A( 1, I ),
242:      $                A( I, I ), SMAXPR, S2, C2 )
243: *
244:          IF( SMAXPR*RCOND.LE.SMINPR ) THEN
245:             DO 20 I = 1, RANK
246:                WORK( ISMIN+I-1 ) = S1*WORK( ISMIN+I-1 )
247:                WORK( ISMAX+I-1 ) = S2*WORK( ISMAX+I-1 )
248:    20       CONTINUE
249:             WORK( ISMIN+RANK ) = C1
250:             WORK( ISMAX+RANK ) = C2
251:             SMIN = SMINPR
252:             SMAX = SMAXPR
253:             RANK = RANK + 1
254:             GO TO 10
255:          END IF
256:       END IF
257: *
258: *     Logically partition R = [ R11 R12 ]
259: *                             [  0  R22 ]
260: *     where R11 = R(1:RANK,1:RANK)
261: *
262: *     [R11,R12] = [ T11, 0 ] * Y
263: *
264:       IF( RANK.LT.N )
265:      $   CALL STZRQF( RANK, N, A, LDA, WORK( MN+1 ), INFO )
266: *
267: *     Details of Householder rotations stored in WORK(MN+1:2*MN)
268: *
269: *     B(1:M,1:NRHS) := Q' * B(1:M,1:NRHS)
270: *
271:       CALL SORM2R( 'Left', 'Transpose', M, NRHS, MN, A, LDA, WORK( 1 ),
272:      $             B, LDB, WORK( 2*MN+1 ), INFO )
273: *
274: *     workspace NRHS
275: *
276: *     B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS)
277: *
278:       CALL STRSM( 'Left', 'Upper', 'No transpose', 'Non-unit', RANK,
279:      $            NRHS, ONE, A, LDA, B, LDB )
280: *
281:       DO 40 I = RANK + 1, N
282:          DO 30 J = 1, NRHS
283:             B( I, J ) = ZERO
284:    30    CONTINUE
285:    40 CONTINUE
286: *
287: *     B(1:N,1:NRHS) := Y' * B(1:N,1:NRHS)
288: *
289:       IF( RANK.LT.N ) THEN
290:          DO 50 I = 1, RANK
291:             CALL SLATZM( 'Left', N-RANK+1, NRHS, A( I, RANK+1 ), LDA,
292:      $                   WORK( MN+I ), B( I, 1 ), B( RANK+1, 1 ), LDB,
293:      $                   WORK( 2*MN+1 ) )
294:    50    CONTINUE
295:       END IF
296: *
297: *     workspace NRHS
298: *
299: *     B(1:N,1:NRHS) := P * B(1:N,1:NRHS)
300: *
301:       DO 90 J = 1, NRHS
302:          DO 60 I = 1, N
303:             WORK( 2*MN+I ) = NTDONE
304:    60    CONTINUE
305:          DO 80 I = 1, N
306:             IF( WORK( 2*MN+I ).EQ.NTDONE ) THEN
307:                IF( JPVT( I ).NE.I ) THEN
308:                   K = I
309:                   T1 = B( K, J )
310:                   T2 = B( JPVT( K ), J )
311:    70             CONTINUE
312:                   B( JPVT( K ), J ) = T1
313:                   WORK( 2*MN+K ) = DONE
314:                   T1 = T2
315:                   K = JPVT( K )
316:                   T2 = B( JPVT( K ), J )
317:                   IF( JPVT( K ).NE.I )
318:      $               GO TO 70
319:                   B( I, J ) = T1
320:                   WORK( 2*MN+K ) = DONE
321:                END IF
322:             END IF
323:    80    CONTINUE
324:    90 CONTINUE
325: *
326: *     Undo scaling
327: *
328:       IF( IASCL.EQ.1 ) THEN
329:          CALL SLASCL( 'G', 0, 0, ANRM, SMLNUM, N, NRHS, B, LDB, INFO )
330:          CALL SLASCL( 'U', 0, 0, SMLNUM, ANRM, RANK, RANK, A, LDA,
331:      $                INFO )
332:       ELSE IF( IASCL.EQ.2 ) THEN
333:          CALL SLASCL( 'G', 0, 0, ANRM, BIGNUM, N, NRHS, B, LDB, INFO )
334:          CALL SLASCL( 'U', 0, 0, BIGNUM, ANRM, RANK, RANK, A, LDA,
335:      $                INFO )
336:       END IF
337:       IF( IBSCL.EQ.1 ) THEN
338:          CALL SLASCL( 'G', 0, 0, SMLNUM, BNRM, N, NRHS, B, LDB, INFO )
339:       ELSE IF( IBSCL.EQ.2 ) THEN
340:          CALL SLASCL( 'G', 0, 0, BIGNUM, BNRM, N, NRHS, B, LDB, INFO )
341:       END IF
342: *
343:   100 CONTINUE
344: *
345:       RETURN
346: *
347: *     End of SGELSX
348: *
349:       END
350: