LAPACK 3.12.1
LAPACK: Linear Algebra PACKage
Loading...
Searching...
No Matches

◆ sgetsls()

subroutine sgetsls ( character trans,
integer m,
integer n,
integer nrhs,
real, dimension( lda, * ) a,
integer lda,
real, dimension( ldb, * ) b,
integer ldb,
real, dimension( * ) work,
integer lwork,
integer info )

SGETSLS

Purpose:
!>
!> SGETSLS solves overdetermined or underdetermined real linear systems
!> involving an M-by-N matrix A, using a tall skinny QR or short wide LQ
!> factorization of A.
!>
!> It is assumed that A has full rank, and only a rudimentary protection
!> against rank-deficient matrices is provided. This subroutine only detects
!> exact rank-deficiency, where a diagonal element of the triangular factor
!> of A is exactly zero.
!>
!> It is conceivable for one (or more) of the diagonal elements of the triangular
!> factor of A to be subnormally tiny numbers without this subroutine signalling
!> an error. The solutions computed for such almost-rank-deficient matrices may
!> be less accurate due to a loss of numerical precision.
!>
!>
!> The following options are provided:
!>
!> 1. If TRANS = 'N' and m >= n:  find the least squares solution of
!>    an overdetermined system, i.e., solve the least squares problem
!>                 minimize || B - A*X ||.
!>
!> 2. If TRANS = 'N' and m < n:  find the minimum norm solution of
!>    an underdetermined system A * X = B.
!>
!> 3. If TRANS = 'T' and m >= n:  find the minimum norm solution of
!>    an undetermined system A**T * X = B.
!>
!> 4. If TRANS = 'T' and m < n:  find the least squares solution of
!>    an overdetermined system, i.e., solve the least squares problem
!>                 minimize || B - A**T * X ||.
!>
!> Several right hand side vectors b and solution vectors x can be
!> handled in a single call; they are stored as the columns of the
!> M-by-NRHS right hand side matrix B and the N-by-NRHS solution
!> matrix X.
!> 
Parameters
[in]TRANS
!>          TRANS is CHARACTER*1
!>          = 'N': the linear system involves A;
!>          = 'T': the linear system involves A**T.
!> 
[in]M
!>          M is INTEGER
!>          The number of rows of the matrix A.  M >= 0.
!> 
[in]N
!>          N is INTEGER
!>          The number of columns of the matrix A.  N >= 0.
!> 
[in]NRHS
!>          NRHS is INTEGER
!>          The number of right hand sides, i.e., the number of
!>          columns of the matrices B and X. NRHS >=0.
!> 
[in,out]A
!>          A is REAL array, dimension (LDA,N)
!>          On entry, the M-by-N matrix A.
!>          On exit,
!>          A is overwritten by details of its QR or LQ
!>          factorization as returned by SGEQR or SGELQ.
!> 
[in]LDA
!>          LDA is INTEGER
!>          The leading dimension of the array A.  LDA >= max(1,M).
!> 
[in,out]B
!>          B is REAL array, dimension (LDB,NRHS)
!>          On entry, the matrix B of right hand side vectors, stored
!>          columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS
!>          if TRANS = 'T'.
!>          On exit, if INFO = 0, B is overwritten by the solution
!>          vectors, stored columnwise:
!>          if TRANS = 'N' and m >= n, rows 1 to n of B contain the least
!>          squares solution vectors.
!>          if TRANS = 'N' and m < n, rows 1 to N of B contain the
!>          minimum norm solution vectors;
!>          if TRANS = 'T' and m >= n, rows 1 to M of B contain the
!>          minimum norm solution vectors;
!>          if TRANS = 'T' and m < n, rows 1 to M of B contain the
!>          least squares solution vectors.
!> 
[in]LDB
!>          LDB is INTEGER
!>          The leading dimension of the array B. LDB >= MAX(1,M,N).
!> 
[out]WORK
!>          (workspace) REAL array, dimension (MAX(1,LWORK))
!>          On exit, if INFO = 0, WORK(1) contains optimal (or either minimal
!>          or optimal, if query was assumed) LWORK.
!>          See LWORK for details.
!> 
[in]LWORK
!>          LWORK is INTEGER
!>          The dimension of the array WORK. LWORK >= 1.
!>          If LWORK = -1 or -2, then a workspace query is assumed.
!>          If LWORK = -1, the routine calculates optimal size of WORK for the
!>          optimal performance and returns this value in WORK(1).
!>          If LWORK = -2, the routine calculates minimal size of WORK and 
!>          returns this value in WORK(1).
!> 
[out]INFO
!>          INFO is INTEGER
!>          = 0:  successful exit
!>          < 0:  if INFO = -i, the i-th argument had an illegal value
!>          > 0:  if INFO =  i, the i-th diagonal element of the
!>                triangular factor of A is exactly zero, so that A does not have
!>                full rank; the least squares solution could not be
!>                computed.
!> 
Author
Univ. of Tennessee
Univ. of California Berkeley
Univ. of Colorado Denver
NAG Ltd.

Definition at line 169 of file sgetsls.f.

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,
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*
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
real function slamch(cmach)
SLAMCH
Definition slamch.f:68
real function slange(norm, m, n, a, lda, work)
SLANGE returns the value of the 1-norm, Frobenius norm, infinity-norm, or the largest absolute value ...
Definition slange.f:112
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
logical function lsame(ca, cb)
LSAME
Definition lsame.f:48
real function sroundup_lwork(lwork)
SROUNDUP_LWORK
subroutine strtrs(uplo, trans, diag, n, nrhs, a, lda, b, ldb, info)
STRTRS
Definition strtrs.f:144
Here is the call graph for this function:
Here is the caller graph for this function: