LAPACK 3.12.1
LAPACK: Linear Algebra PACKage
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sgetsls.f
Go to the documentation of this file.
1*> \brief \b SGETSLS
2*
3* Definition:
4* ===========
5*
6* SUBROUTINE SGETSLS( TRANS, M, N, NRHS, A, LDA, B, LDB,
7* $ WORK, LWORK, INFO )
8*
9* .. Scalar Arguments ..
10* CHARACTER TRANS
11* INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS
12* ..
13* .. Array Arguments ..
14* REAL A( LDA, * ), B( LDB, * ), WORK( * )
15* ..
16*
17*
18*> \par Purpose:
19* =============
20*>
21*> \verbatim
22*>
23*> SGETSLS solves overdetermined or underdetermined real linear systems
24*> involving an M-by-N matrix A, using a tall skinny QR or short wide LQ
25*> factorization of A.
26*>
27*> It is assumed that A has full rank, and only a rudimentary protection
28*> against rank-deficient matrices is provided. This subroutine only detects
29*> exact rank-deficiency, where a diagonal element of the triangular factor
30*> of A is exactly zero.
31*>
32*> It is conceivable for one (or more) of the diagonal elements of the triangular
33*> factor of A to be subnormally tiny numbers without this subroutine signalling
34*> an error. The solutions computed for such almost-rank-deficient matrices may
35*> be less accurate due to a loss of numerical precision.
36*>
37*>
38*> The following options are provided:
39*>
40*> 1. If TRANS = 'N' and m >= n: find the least squares solution of
41*> an overdetermined system, i.e., solve the least squares problem
42*> minimize || B - A*X ||.
43*>
44*> 2. If TRANS = 'N' and m < n: find the minimum norm solution of
45*> an underdetermined system A * X = B.
46*>
47*> 3. If TRANS = 'T' and m >= n: find the minimum norm solution of
48*> an undetermined system A**T * X = B.
49*>
50*> 4. If TRANS = 'T' and m < n: find the least squares solution of
51*> an overdetermined system, i.e., solve the least squares problem
52*> minimize || B - A**T * X ||.
53*>
54*> Several right hand side vectors b and solution vectors x can be
55*> handled in a single call; they are stored as the columns of the
56*> M-by-NRHS right hand side matrix B and the N-by-NRHS solution
57*> matrix X.
58*> \endverbatim
59*
60* Arguments:
61* ==========
62*
63*> \param[in] TRANS
64*> \verbatim
65*> TRANS is CHARACTER*1
66*> = 'N': the linear system involves A;
67*> = 'T': the linear system involves A**T.
68*> \endverbatim
69*>
70*> \param[in] M
71*> \verbatim
72*> M is INTEGER
73*> The number of rows of the matrix A. M >= 0.
74*> \endverbatim
75*>
76*> \param[in] N
77*> \verbatim
78*> N is INTEGER
79*> The number of columns of the matrix A. N >= 0.
80*> \endverbatim
81*>
82*> \param[in] NRHS
83*> \verbatim
84*> NRHS is INTEGER
85*> The number of right hand sides, i.e., the number of
86*> columns of the matrices B and X. NRHS >=0.
87*> \endverbatim
88*>
89*> \param[in,out] A
90*> \verbatim
91*> A is REAL array, dimension (LDA,N)
92*> On entry, the M-by-N matrix A.
93*> On exit,
94*> A is overwritten by details of its QR or LQ
95*> factorization as returned by SGEQR or SGELQ.
96*> \endverbatim
97*>
98*> \param[in] LDA
99*> \verbatim
100*> LDA is INTEGER
101*> The leading dimension of the array A. LDA >= max(1,M).
102*> \endverbatim
103*>
104*> \param[in,out] B
105*> \verbatim
106*> B is REAL array, dimension (LDB,NRHS)
107*> On entry, the matrix B of right hand side vectors, stored
108*> columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS
109*> if TRANS = 'T'.
110*> On exit, if INFO = 0, B is overwritten by the solution
111*> vectors, stored columnwise:
112*> if TRANS = 'N' and m >= n, rows 1 to n of B contain the least
113*> squares solution vectors.
114*> if TRANS = 'N' and m < n, rows 1 to N of B contain the
115*> minimum norm solution vectors;
116*> if TRANS = 'T' and m >= n, rows 1 to M of B contain the
117*> minimum norm solution vectors;
118*> if TRANS = 'T' and m < n, rows 1 to M of B contain the
119*> least squares solution vectors.
120*> \endverbatim
121*>
122*> \param[in] LDB
123*> \verbatim
124*> LDB is INTEGER
125*> The leading dimension of the array B. LDB >= MAX(1,M,N).
126*> \endverbatim
127*>
128*> \param[out] WORK
129*> \verbatim
130*> (workspace) REAL array, dimension (MAX(1,LWORK))
131*> On exit, if INFO = 0, WORK(1) contains optimal (or either minimal
132*> or optimal, if query was assumed) LWORK.
133*> See LWORK for details.
134*> \endverbatim
135*>
136*> \param[in] LWORK
137*> \verbatim
138*> LWORK is INTEGER
139*> The dimension of the array WORK. LWORK >= 1.
140*> If LWORK = -1 or -2, then a workspace query is assumed.
141*> If LWORK = -1, the routine calculates optimal size of WORK for the
142*> optimal performance and returns this value in WORK(1).
143*> If LWORK = -2, the routine calculates minimal size of WORK and
144*> returns this value in WORK(1).
145*> \endverbatim
146*>
147*> \param[out] INFO
148*> \verbatim
149*> INFO is INTEGER
150*> = 0: successful exit
151*> < 0: if INFO = -i, the i-th argument had an illegal value
152*> > 0: if INFO = i, the i-th diagonal element of the
153*> triangular factor of A is exactly zero, so that A does not have
154*> full rank; the least squares solution could not be
155*> computed.
156*> \endverbatim
157*
158* Authors:
159* ========
160*
161*> \author Univ. of Tennessee
162*> \author Univ. of California Berkeley
163*> \author Univ. of Colorado Denver
164*> \author NAG Ltd.
165*
166*> \ingroup getsls
167*
168* =====================================================================
169 SUBROUTINE sgetsls( TRANS, M, N, NRHS, A, LDA, B, LDB,
170 $ WORK, LWORK, INFO )
171*
172* -- LAPACK driver routine --
173* -- LAPACK is a software package provided by Univ. of Tennessee, --
174* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
175*
176* .. Scalar Arguments ..
177 CHARACTER TRANS
178 INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS
179* ..
180* .. Array Arguments ..
181 REAL A( LDA, * ), B( LDB, * ), WORK( * )
182*
183* ..
184*
185* =====================================================================
186*
187* .. Parameters ..
188 REAL ZERO, ONE
189 parameter( zero = 0.0e0, one = 1.0e0 )
190* ..
191* .. Local Scalars ..
192 LOGICAL LQUERY, TRAN
193 INTEGER I, IASCL, IBSCL, J, MAXMN, BROW,
194 $ scllen, tszo, tszm, lwo, lwm, lw1, lw2,
195 $ wsizeo, wsizem, info2
196 REAL ANRM, BIGNUM, BNRM, SMLNUM, TQ( 5 ), WORKQ( 1 )
197* ..
198* .. External Functions ..
199 LOGICAL LSAME
200 REAL SLAMCH, SLANGE, SROUNDUP_LWORK
201 EXTERNAL lsame, slamch, slange,
202 $ sroundup_lwork
203* ..
204* .. External Subroutines ..
205 EXTERNAL sgeqr, sgemqr, slascl, slaset,
207* ..
208* .. Intrinsic Functions ..
209 INTRINSIC max, min, int
210* ..
211* .. Executable Statements ..
212*
213* Test the input arguments.
214*
215 info = 0
216 maxmn = max( m, n )
217 tran = lsame( trans, 'T' )
218*
219 lquery = ( lwork.EQ.-1 .OR. lwork.EQ.-2 )
220 IF( .NOT.( lsame( trans, 'N' ) .OR.
221 $ lsame( trans, 'T' ) ) ) THEN
222 info = -1
223 ELSE IF( m.LT.0 ) THEN
224 info = -2
225 ELSE IF( n.LT.0 ) THEN
226 info = -3
227 ELSE IF( nrhs.LT.0 ) THEN
228 info = -4
229 ELSE IF( lda.LT.max( 1, m ) ) THEN
230 info = -6
231 ELSE IF( ldb.LT.max( 1, m, n ) ) THEN
232 info = -8
233 END IF
234*
235 IF( info.EQ.0 ) THEN
236*
237* Determine the optimum and minimum LWORK
238*
239 IF( min( m, n, nrhs ).EQ.0 ) THEN
240 wsizeo = 1
241 wsizem = 1
242 ELSE IF( m.GE.n ) THEN
243 CALL sgeqr( m, n, a, lda, tq, -1, workq, -1, info2 )
244 tszo = int( tq( 1 ) )
245 lwo = int( workq( 1 ) )
246 CALL sgemqr( 'L', trans, m, nrhs, n, a, lda, tq,
247 $ tszo, b, ldb, workq, -1, info2 )
248 lwo = max( lwo, int( workq( 1 ) ) )
249 CALL sgeqr( m, n, a, lda, tq, -2, workq, -2, info2 )
250 tszm = int( tq( 1 ) )
251 lwm = int( workq( 1 ) )
252 CALL sgemqr( 'L', trans, m, nrhs, n, a, lda, tq,
253 $ tszm, b, ldb, workq, -1, info2 )
254 lwm = max( lwm, int( workq( 1 ) ) )
255 wsizeo = tszo + lwo
256 wsizem = tszm + lwm
257 ELSE
258 CALL sgelq( m, n, a, lda, tq, -1, workq, -1, info2 )
259 tszo = int( tq( 1 ) )
260 lwo = int( workq( 1 ) )
261 CALL sgemlq( 'L', trans, n, nrhs, m, a, lda, tq,
262 $ tszo, b, ldb, workq, -1, info2 )
263 lwo = max( lwo, int( workq( 1 ) ) )
264 CALL sgelq( m, n, a, lda, tq, -2, workq, -2, info2 )
265 tszm = int( tq( 1 ) )
266 lwm = int( workq( 1 ) )
267 CALL sgemlq( 'L', trans, n, nrhs, m, a, lda, tq,
268 $ tszm, b, ldb, workq, -1, info2 )
269 lwm = max( lwm, int( workq( 1 ) ) )
270 wsizeo = tszo + lwo
271 wsizem = tszm + lwm
272 END IF
273*
274 IF( ( lwork.LT.wsizem ).AND.( .NOT.lquery ) ) THEN
275 info = -10
276 END IF
277*
278 work( 1 ) = sroundup_lwork( wsizeo )
279*
280 END IF
281*
282 IF( info.NE.0 ) THEN
283 CALL xerbla( 'SGETSLS', -info )
284 RETURN
285 END IF
286 IF( lquery ) THEN
287 IF( lwork.EQ.-2 ) work( 1 ) = sroundup_lwork( wsizem )
288 RETURN
289 END IF
290 IF( lwork.LT.wsizeo ) THEN
291 lw1 = tszm
292 lw2 = lwm
293 ELSE
294 lw1 = tszo
295 lw2 = lwo
296 END IF
297*
298* Quick return if possible
299*
300 IF( min( m, n, nrhs ).EQ.0 ) THEN
301 CALL slaset( 'FULL', max( m, n ), nrhs, zero, zero,
302 $ b, ldb )
303 RETURN
304 END IF
305*
306* Get machine parameters
307*
308 smlnum = slamch( 'S' ) / slamch( 'P' )
309 bignum = one / smlnum
310*
311* Scale A, B if max element outside range [SMLNUM,BIGNUM]
312*
313 anrm = slange( 'M', m, n, a, lda, work )
314 iascl = 0
315 IF( anrm.GT.zero .AND. anrm.LT.smlnum ) THEN
316*
317* Scale matrix norm up to SMLNUM
318*
319 CALL slascl( 'G', 0, 0, anrm, smlnum, m, n, a, lda, info )
320 iascl = 1
321 ELSE IF( anrm.GT.bignum ) THEN
322*
323* Scale matrix norm down to BIGNUM
324*
325 CALL slascl( 'G', 0, 0, anrm, bignum, m, n, a, lda, info )
326 iascl = 2
327 ELSE IF( anrm.EQ.zero ) THEN
328*
329* Matrix all zero. Return zero solution.
330*
331 CALL slaset( 'F', maxmn, nrhs, zero, zero, b, ldb )
332 GO TO 50
333 END IF
334*
335 brow = m
336 IF ( tran ) THEN
337 brow = n
338 END IF
339 bnrm = slange( 'M', brow, nrhs, b, ldb, work )
340 ibscl = 0
341 IF( bnrm.GT.zero .AND. bnrm.LT.smlnum ) THEN
342*
343* Scale matrix norm up to SMLNUM
344*
345 CALL slascl( 'G', 0, 0, bnrm, smlnum, brow, nrhs, b, ldb,
346 $ info )
347 ibscl = 1
348 ELSE IF( bnrm.GT.bignum ) THEN
349*
350* Scale matrix norm down to BIGNUM
351*
352 CALL slascl( 'G', 0, 0, bnrm, bignum, brow, nrhs, b, ldb,
353 $ info )
354 ibscl = 2
355 END IF
356*
357 IF ( m.GE.n ) THEN
358*
359* compute QR factorization of A
360*
361 CALL sgeqr( m, n, a, lda, work( lw2+1 ), lw1,
362 $ work( 1 ), lw2, info )
363 IF ( .NOT.tran ) THEN
364*
365* Least-Squares Problem min || A * X - B ||
366*
367* B(1:M,1:NRHS) := Q**T * B(1:M,1:NRHS)
368*
369 CALL sgemqr( 'L' , 'T', m, nrhs, n, a, lda,
370 $ work( lw2+1 ), lw1, b, ldb, work( 1 ), lw2,
371 $ info )
372*
373* B(1:N,1:NRHS) := inv(R) * B(1:N,1:NRHS)
374*
375 CALL strtrs( 'U', 'N', 'N', n, nrhs,
376 $ a, lda, b, ldb, info )
377 IF( info.GT.0 ) THEN
378 RETURN
379 END IF
380 scllen = n
381 ELSE
382*
383* Overdetermined system of equations A**T * X = B
384*
385* B(1:N,1:NRHS) := inv(R**T) * B(1:N,1:NRHS)
386*
387 CALL strtrs( 'U', 'T', 'N', n, nrhs,
388 $ a, lda, b, ldb, info )
389*
390 IF( info.GT.0 ) THEN
391 RETURN
392 END IF
393*
394* B(N+1:M,1:NRHS) = ZERO
395*
396 DO 20 j = 1, nrhs
397 DO 10 i = n + 1, m
398 b( i, j ) = zero
399 10 CONTINUE
400 20 CONTINUE
401*
402* B(1:M,1:NRHS) := Q(1:N,:) * B(1:N,1:NRHS)
403*
404 CALL sgemqr( 'L', 'N', m, nrhs, n, a, lda,
405 $ work( lw2+1 ), lw1, b, ldb, work( 1 ), lw2,
406 $ info )
407*
408 scllen = m
409*
410 END IF
411*
412 ELSE
413*
414* Compute LQ factorization of A
415*
416 CALL sgelq( m, n, a, lda, work( lw2+1 ), lw1,
417 $ work( 1 ), lw2, info )
418*
419* workspace at least M, optimally M*NB.
420*
421 IF( .NOT.tran ) THEN
422*
423* underdetermined system of equations A * X = B
424*
425* B(1:M,1:NRHS) := inv(L) * B(1:M,1:NRHS)
426*
427 CALL strtrs( 'L', 'N', 'N', m, nrhs,
428 $ a, lda, b, ldb, info )
429*
430 IF( info.GT.0 ) THEN
431 RETURN
432 END IF
433*
434* B(M+1:N,1:NRHS) = 0
435*
436 DO 40 j = 1, nrhs
437 DO 30 i = m + 1, n
438 b( i, j ) = zero
439 30 CONTINUE
440 40 CONTINUE
441*
442* B(1:N,1:NRHS) := Q(1:N,:)**T * B(1:M,1:NRHS)
443*
444 CALL sgemlq( 'L', 'T', n, nrhs, m, a, lda,
445 $ work( lw2+1 ), lw1, b, ldb, work( 1 ), lw2,
446 $ info )
447*
448* workspace at least NRHS, optimally NRHS*NB
449*
450 scllen = n
451*
452 ELSE
453*
454* overdetermined system min || A**T * X - B ||
455*
456* B(1:N,1:NRHS) := Q * B(1:N,1:NRHS)
457*
458 CALL sgemlq( 'L', 'N', n, nrhs, m, a, lda,
459 $ work( lw2+1 ), lw1, b, ldb, work( 1 ), lw2,
460 $ info )
461*
462* workspace at least NRHS, optimally NRHS*NB
463*
464* B(1:M,1:NRHS) := inv(L**T) * B(1:M,1:NRHS)
465*
466 CALL strtrs( 'Lower', 'Transpose', 'Non-unit', m, nrhs,
467 $ a, lda, b, ldb, info )
468*
469 IF( info.GT.0 ) THEN
470 RETURN
471 END IF
472*
473 scllen = m
474*
475 END IF
476*
477 END IF
478*
479* Undo scaling
480*
481 IF( iascl.EQ.1 ) THEN
482 CALL slascl( 'G', 0, 0, anrm, smlnum, scllen, nrhs, b, ldb,
483 $ info )
484 ELSE IF( iascl.EQ.2 ) THEN
485 CALL slascl( 'G', 0, 0, anrm, bignum, scllen, nrhs, b, ldb,
486 $ info )
487 END IF
488 IF( ibscl.EQ.1 ) THEN
489 CALL slascl( 'G', 0, 0, smlnum, bnrm, scllen, nrhs, b, ldb,
490 $ info )
491 ELSE IF( ibscl.EQ.2 ) THEN
492 CALL slascl( 'G', 0, 0, bignum, bnrm, scllen, nrhs, b, ldb,
493 $ info )
494 END IF
495*
496 50 CONTINUE
497 work( 1 ) = sroundup_lwork( tszo + lwo )
498 RETURN
499*
500* End of SGETSLS
501*
502 END
subroutine xerbla(srname, info)
Definition cblat2.f:3285
subroutine sgelq(m, n, a, lda, t, tsize, work, lwork, info)
SGELQ
Definition sgelq.f:174
subroutine sgemlq(side, trans, m, n, k, a, lda, t, tsize, c, ldc, work, lwork, info)
SGEMLQ
Definition sgemlq.f:173
subroutine sgemqr(side, trans, m, n, k, a, lda, t, tsize, c, ldc, work, lwork, info)
SGEMQR
Definition sgemqr.f:174
subroutine sgeqr(m, n, a, lda, t, tsize, work, lwork, info)
SGEQR
Definition sgeqr.f:176
subroutine sgetsls(trans, m, n, nrhs, a, lda, b, ldb, work, lwork, info)
SGETSLS
Definition sgetsls.f:171
subroutine slascl(type, kl, ku, cfrom, cto, m, n, a, lda, info)
SLASCL multiplies a general rectangular matrix by a real scalar defined as cto/cfrom.
Definition slascl.f:142
subroutine slaset(uplo, m, n, alpha, beta, a, lda)
SLASET initializes the off-diagonal elements and the diagonal elements of a matrix to given values.
Definition slaset.f:108
subroutine strtrs(uplo, trans, diag, n, nrhs, a, lda, b, ldb, info)
STRTRS
Definition strtrs.f:144