168 SUBROUTINE dgelss( M, N, NRHS, A, LDA, B, LDB, S, RCOND, RANK,
169 $ WORK, LWORK, INFO )
176 INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
177 DOUBLE PRECISION RCOND
180 DOUBLE PRECISION A( LDA, * ), B( LDB, * ), S( * ), WORK( * )
186 DOUBLE PRECISION ZERO, ONE
187 parameter( zero = 0.0d+0, one = 1.0d+0 )
191 INTEGER BDSPAC, BL, CHUNK, I, IASCL, IBSCL, IE, IL,
192 $ itau, itaup, itauq, iwork, ldwork, maxmn,
193 $ maxwrk, minmn, minwrk, mm, mnthr
194 INTEGER LWORK_DGEQRF, LWORK_DORMQR, LWORK_DGEBRD,
195 $ lwork_dormbr, lwork_dorgbr, lwork_dormlq,
197 DOUBLE PRECISION ANRM, BIGNUM, BNRM, EPS, SFMIN, SMLNUM, THR
200 DOUBLE PRECISION DUM( 1 )
210 DOUBLE PRECISION DLAMCH, DLANGE
211 EXTERNAL ilaenv, dlamch, dlange
223 lquery = ( lwork.EQ.-1 )
226 ELSE IF( n.LT.0 )
THEN
228 ELSE IF( nrhs.LT.0 )
THEN
230 ELSE IF( lda.LT.max( 1, m ) )
THEN
232 ELSE IF( ldb.LT.max( 1, maxmn ) )
THEN
246 IF( minmn.GT.0 )
THEN
248 mnthr = ilaenv( 6,
'DGELSS',
' ', m, n, nrhs, -1 )
249 IF( m.GE.n .AND. m.GE.mnthr )
THEN
255 CALL dgeqrf( m, n, a, lda, dum(1), dum(1), -1, info )
256 lwork_dgeqrf = int( dum(1) )
258 CALL dormqr(
'L',
'T', m, nrhs, n, a, lda, dum(1), b,
259 $ ldb, dum(1), -1, info )
260 lwork_dormqr = int( dum(1) )
262 maxwrk = max( maxwrk, n + lwork_dgeqrf )
263 maxwrk = max( maxwrk, n + lwork_dormqr )
271 bdspac = max( 1, 5*n )
273 CALL dgebrd( mm, n, a, lda, s, dum(1), dum(1),
274 $ dum(1), dum(1), -1, info )
275 lwork_dgebrd = int( dum(1) )
277 CALL dormbr(
'Q',
'L',
'T', mm, nrhs, n, a, lda,
279 $ b, ldb, dum(1), -1, info )
280 lwork_dormbr = int( dum(1) )
282 CALL dorgbr(
'P', n, n, n, a, lda, dum(1),
284 lwork_dorgbr = int( dum(1) )
286 maxwrk = max( maxwrk, 3*n + lwork_dgebrd )
287 maxwrk = max( maxwrk, 3*n + lwork_dormbr )
288 maxwrk = max( maxwrk, 3*n + lwork_dorgbr )
289 maxwrk = max( maxwrk, bdspac )
290 maxwrk = max( maxwrk, n*nrhs )
291 minwrk = max( 3*n + mm, 3*n + nrhs, bdspac )
292 maxwrk = max( minwrk, maxwrk )
298 bdspac = max( 1, 5*m )
299 minwrk = max( 3*m+nrhs, 3*m+n, bdspac )
300 IF( n.GE.mnthr )
THEN
306 CALL dgelqf( m, n, a, lda, dum(1), dum(1),
308 lwork_dgelqf = int( dum(1) )
310 CALL dgebrd( m, m, a, lda, s, dum(1), dum(1),
311 $ dum(1), dum(1), -1, info )
312 lwork_dgebrd = int( dum(1) )
314 CALL dormbr(
'Q',
'L',
'T', m, nrhs, n, a, lda,
315 $ dum(1), b, ldb, dum(1), -1, info )
316 lwork_dormbr = int( dum(1) )
318 CALL dorgbr(
'P', m, m, m, a, lda, dum(1),
320 lwork_dorgbr = int( dum(1) )
322 CALL dormlq(
'L',
'T', n, nrhs, m, a, lda, dum(1),
323 $ b, ldb, dum(1), -1, info )
324 lwork_dormlq = int( dum(1) )
326 maxwrk = m + lwork_dgelqf
327 maxwrk = max( maxwrk, m*m + 4*m + lwork_dgebrd )
328 maxwrk = max( maxwrk, m*m + 4*m + lwork_dormbr )
329 maxwrk = max( maxwrk, m*m + 4*m + lwork_dorgbr )
330 maxwrk = max( maxwrk, m*m + m + bdspac )
332 maxwrk = max( maxwrk, m*m + m + m*nrhs )
334 maxwrk = max( maxwrk, m*m + 2*m )
336 maxwrk = max( maxwrk, m + lwork_dormlq )
342 CALL dgebrd( m, n, a, lda, s, dum(1), dum(1),
343 $ dum(1), dum(1), -1, info )
344 lwork_dgebrd = int( dum(1) )
346 CALL dormbr(
'Q',
'L',
'T', m, nrhs, m, a, lda,
347 $ dum(1), b, ldb, dum(1), -1, info )
348 lwork_dormbr = int( dum(1) )
350 CALL dorgbr(
'P', m, n, m, a, lda, dum(1),
352 lwork_dorgbr = int( dum(1) )
353 maxwrk = 3*m + lwork_dgebrd
354 maxwrk = max( maxwrk, 3*m + lwork_dormbr )
355 maxwrk = max( maxwrk, 3*m + lwork_dorgbr )
356 maxwrk = max( maxwrk, bdspac )
357 maxwrk = max( maxwrk, n*nrhs )
360 maxwrk = max( minwrk, maxwrk )
364 IF( lwork.LT.minwrk .AND. .NOT.lquery )
369 CALL xerbla(
'DGELSS', -info )
371 ELSE IF( lquery )
THEN
377 IF( m.EQ.0 .OR. n.EQ.0 )
THEN
385 sfmin = dlamch(
'S' )
387 bignum = one / smlnum
391 anrm = dlange(
'M', m, n, a, lda, work )
393 IF( anrm.GT.zero .AND. anrm.LT.smlnum )
THEN
397 CALL dlascl(
'G', 0, 0, anrm, smlnum, m, n, a, lda, info )
399 ELSE IF( anrm.GT.bignum )
THEN
403 CALL dlascl(
'G', 0, 0, anrm, bignum, m, n, a, lda, info )
405 ELSE IF( anrm.EQ.zero )
THEN
409 CALL dlaset(
'F', max( m, n ), nrhs, zero, zero, b, ldb )
410 CALL dlaset(
'F', minmn, 1, zero, zero, s, minmn )
417 bnrm = dlange(
'M', m, nrhs, b, ldb, work )
419 IF( bnrm.GT.zero .AND. bnrm.LT.smlnum )
THEN
423 CALL dlascl(
'G', 0, 0, bnrm, smlnum, m, nrhs, b, ldb,
426 ELSE IF( bnrm.GT.bignum )
THEN
430 CALL dlascl(
'G', 0, 0, bnrm, bignum, m, nrhs, b, ldb,
442 IF( m.GE.mnthr )
THEN
453 CALL dgeqrf( m, n, a, lda, work( itau ), work( iwork ),
454 $ lwork-iwork+1, info )
459 CALL dormqr(
'L',
'T', m, nrhs, n, a, lda, work( itau ),
461 $ ldb, work( iwork ), lwork-iwork+1, info )
466 $
CALL dlaset(
'L', n-1, n-1, zero, zero, a( 2, 1 ),
478 CALL dgebrd( mm, n, a, lda, s, work( ie ), work( itauq ),
479 $ work( itaup ), work( iwork ), lwork-iwork+1,
485 CALL dormbr(
'Q',
'L',
'T', mm, nrhs, n, a, lda,
487 $ b, ldb, work( iwork ), lwork-iwork+1, info )
492 CALL dorgbr(
'P', n, n, n, a, lda, work( itaup ),
493 $ work( iwork ), lwork-iwork+1, info )
501 CALL dbdsqr(
'U', n, n, 0, nrhs, s, work( ie ), a, lda, dum,
502 $ 1, b, ldb, work( iwork ), info )
508 thr = max( rcond*s( 1 ), sfmin )
510 $ thr = max( eps*s( 1 ), sfmin )
513 IF( s( i ).GT.thr )
THEN
514 CALL drscl( nrhs, s( i ), b( i, 1 ), ldb )
517 CALL dlaset(
'F', 1, nrhs, zero, zero, b( i, 1 ),
525 IF( lwork.GE.ldb*nrhs .AND. nrhs.GT.1 )
THEN
526 CALL dgemm(
'T',
'N', n, nrhs, n, one, a, lda, b, ldb,
529 CALL dlacpy(
'G', n, nrhs, work, ldb, b, ldb )
530 ELSE IF( nrhs.GT.1 )
THEN
532 DO 20 i = 1, nrhs, chunk
533 bl = min( nrhs-i+1, chunk )
534 CALL dgemm(
'T',
'N', n, bl, n, one, a, lda, b( 1,
536 $ ldb, zero, work, n )
537 CALL dlacpy(
'G', n, bl, work, n, b( 1, i ), ldb )
539 ELSE IF( nrhs.EQ.1 )
THEN
540 CALL dgemv(
'T', n, n, one, a, lda, b, 1, zero, work, 1 )
541 CALL dcopy( n, work, 1, b, 1 )
544 ELSE IF( n.GE.mnthr .AND. lwork.GE.4*m+m*m+
545 $ max( m, 2*m-4, nrhs, n-3*m ) )
THEN
551 IF( lwork.GE.max( 4*m+m*lda+max( m, 2*m-4, nrhs, n-3*m ),
552 $ m*lda+m+m*nrhs ) )ldwork = lda
559 CALL dgelqf( m, n, a, lda, work( itau ), work( iwork ),
560 $ lwork-iwork+1, info )
565 CALL dlacpy(
'L', m, m, a, lda, work( il ), ldwork )
566 CALL dlaset(
'U', m-1, m-1, zero, zero, work( il+ldwork ),
576 CALL dgebrd( m, m, work( il ), ldwork, s, work( ie ),
577 $ work( itauq ), work( itaup ), work( iwork ),
578 $ lwork-iwork+1, info )
583 CALL dormbr(
'Q',
'L',
'T', m, nrhs, m, work( il ), ldwork,
584 $ work( itauq ), b, ldb, work( iwork ),
585 $ lwork-iwork+1, info )
590 CALL dorgbr(
'P', m, m, m, work( il ), ldwork,
592 $ work( iwork ), lwork-iwork+1, info )
600 CALL dbdsqr(
'U', m, m, 0, nrhs, s, work( ie ), work( il ),
601 $ ldwork, a, lda, b, ldb, work( iwork ), info )
607 thr = max( rcond*s( 1 ), sfmin )
609 $ thr = max( eps*s( 1 ), sfmin )
612 IF( s( i ).GT.thr )
THEN
613 CALL drscl( nrhs, s( i ), b( i, 1 ), ldb )
616 CALL dlaset(
'F', 1, nrhs, zero, zero, b( i, 1 ),
625 IF( lwork.GE.ldb*nrhs+iwork-1 .AND. nrhs.GT.1 )
THEN
626 CALL dgemm(
'T',
'N', m, nrhs, m, one, work( il ),
628 $ b, ldb, zero, work( iwork ), ldb )
629 CALL dlacpy(
'G', m, nrhs, work( iwork ), ldb, b, ldb )
630 ELSE IF( nrhs.GT.1 )
THEN
631 chunk = ( lwork-iwork+1 ) / m
632 DO 40 i = 1, nrhs, chunk
633 bl = min( nrhs-i+1, chunk )
634 CALL dgemm(
'T',
'N', m, bl, m, one, work( il ),
636 $ b( 1, i ), ldb, zero, work( iwork ), m )
637 CALL dlacpy(
'G', m, bl, work( iwork ), m, b( 1, i ),
640 ELSE IF( nrhs.EQ.1 )
THEN
641 CALL dgemv(
'T', m, m, one, work( il ), ldwork, b( 1,
642 $ 1 ),1, zero, work( iwork ), 1 )
643 CALL dcopy( m, work( iwork ), 1, b( 1, 1 ), 1 )
648 CALL dlaset(
'F', n-m, nrhs, zero, zero, b( m+1, 1 ), ldb )
654 CALL dormlq(
'L',
'T', n, nrhs, m, a, lda, work( itau ), b,
655 $ ldb, work( iwork ), lwork-iwork+1, info )
669 CALL dgebrd( m, n, a, lda, s, work( ie ), work( itauq ),
670 $ work( itaup ), work( iwork ), lwork-iwork+1,
676 CALL dormbr(
'Q',
'L',
'T', m, nrhs, n, a, lda,
678 $ b, ldb, work( iwork ), lwork-iwork+1, info )
683 CALL dorgbr(
'P', m, n, m, a, lda, work( itaup ),
684 $ work( iwork ), lwork-iwork+1, info )
692 CALL dbdsqr(
'L', m, n, 0, nrhs, s, work( ie ), a, lda, dum,
693 $ 1, b, ldb, work( iwork ), info )
699 thr = max( rcond*s( 1 ), sfmin )
701 $ thr = max( eps*s( 1 ), sfmin )
704 IF( s( i ).GT.thr )
THEN
705 CALL drscl( nrhs, s( i ), b( i, 1 ), ldb )
708 CALL dlaset(
'F', 1, nrhs, zero, zero, b( i, 1 ),
716 IF( lwork.GE.ldb*nrhs .AND. nrhs.GT.1 )
THEN
717 CALL dgemm(
'T',
'N', n, nrhs, m, one, a, lda, b, ldb,
720 CALL dlacpy(
'F', n, nrhs, work, ldb, b, ldb )
721 ELSE IF( nrhs.GT.1 )
THEN
723 DO 60 i = 1, nrhs, chunk
724 bl = min( nrhs-i+1, chunk )
725 CALL dgemm(
'T',
'N', n, bl, m, one, a, lda, b( 1,
727 $ ldb, zero, work, n )
728 CALL dlacpy(
'F', n, bl, work, n, b( 1, i ), ldb )
730 ELSE IF( nrhs.EQ.1 )
THEN
731 CALL dgemv(
'T', m, n, one, a, lda, b, 1, zero, work, 1 )
732 CALL dcopy( n, work, 1, b, 1 )
738 IF( iascl.EQ.1 )
THEN
739 CALL dlascl(
'G', 0, 0, anrm, smlnum, n, nrhs, b, ldb,
741 CALL dlascl(
'G', 0, 0, smlnum, anrm, minmn, 1, s, minmn,
743 ELSE IF( iascl.EQ.2 )
THEN
744 CALL dlascl(
'G', 0, 0, anrm, bignum, n, nrhs, b, ldb,
746 CALL dlascl(
'G', 0, 0, bignum, anrm, minmn, 1, s, minmn,
749 IF( ibscl.EQ.1 )
THEN
750 CALL dlascl(
'G', 0, 0, smlnum, bnrm, n, nrhs, b, ldb,
752 ELSE IF( ibscl.EQ.2 )
THEN
753 CALL dlascl(
'G', 0, 0, bignum, bnrm, n, nrhs, b, ldb,