1 *> \brief <b> SGESVDX computes the singular value decomposition (SVD) for GE matrices</b>
3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download SGESVDX + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgesvdx.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgesvdx.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgesvdx.f">
21 * SUBROUTINE SGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU,
22 * $ IL, IU, NS, S, U, LDU, VT, LDVT, WORK,
23 * $ LWORK, IWORK, INFO )
26 * .. Scalar Arguments ..
27 * CHARACTER JOBU, JOBVT, RANGE
28 * INTEGER IL, INFO, IU, LDA, LDU, LDVT, LWORK, M, N, NS
31 * .. Array Arguments ..
33 * REAL A( LDA, * ), S( * ), U( LDU, * ),
34 * $ VT( LDVT, * ), WORK( * )
43 *> SGESVDX computes the singular value decomposition (SVD) of a real
44 *> M-by-N matrix A, optionally computing the left and/or right singular
45 *> vectors. The SVD is written
47 *> A = U * SIGMA * transpose(V)
49 *> where SIGMA is an M-by-N matrix which is zero except for its
50 *> min(m,n) diagonal elements, U is an M-by-M orthogonal matrix, and
51 *> V is an N-by-N orthogonal matrix. The diagonal elements of SIGMA
52 *> are the singular values of A; they are real and non-negative, and
53 *> are returned in descending order. The first min(m,n) columns of
54 *> U and V are the left and right singular vectors of A.
56 *> SGESVDX uses an eigenvalue problem for obtaining the SVD, which
57 *> allows for the computation of a subset of singular values and
58 *> vectors. See SBDSVDX for details.
60 *> Note that the routine returns V**T, not V.
68 *> JOBU is CHARACTER*1
69 *> Specifies options for computing all or part of the matrix U:
70 *> = 'V': the first min(m,n) columns of U (the left singular
71 *> vectors) or as specified by RANGE are returned in
73 *> = 'N': no columns of U (no left singular vectors) are
79 *> JOBVT is CHARACTER*1
80 *> Specifies options for computing all or part of the matrix
82 *> = 'V': the first min(m,n) rows of V**T (the right singular
83 *> vectors) or as specified by RANGE are returned in
85 *> = 'N': no rows of V**T (no right singular vectors) are
91 *> RANGE is CHARACTER*1
92 *> = 'A': all singular values will be found.
93 *> = 'V': all singular values in the half-open interval (VL,VU]
95 *> = 'I': the IL-th through IU-th singular values will be found.
101 *> The number of rows of the input matrix A. M >= 0.
107 *> The number of columns of the input matrix A. N >= 0.
112 *> A is REAL array, dimension (LDA,N)
113 *> On entry, the M-by-N matrix A.
114 *> On exit, the contents of A are destroyed.
120 *> The leading dimension of the array A. LDA >= max(1,M).
132 *> If RANGE='V', the lower and upper bounds of the interval to
133 *> be searched for singular values. VU > VL.
134 *> Not referenced if RANGE = 'A' or 'I'.
145 *> If RANGE='I', the indices (in ascending order) of the
146 *> smallest and largest singular values to be returned.
147 *> 1 <= IL <= IU <= min(M,N), if min(M,N) > 0.
148 *> Not referenced if RANGE = 'A' or 'V'.
154 *> The total number of singular values found,
155 *> 0 <= NS <= min(M,N).
156 *> If RANGE = 'A', NS = min(M,N); if RANGE = 'I', NS = IU-IL+1.
161 *> S is REAL array, dimension (min(M,N))
162 *> The singular values of A, sorted so that S(i) >= S(i+1).
167 *> U is REAL array, dimension (LDU,UCOL)
168 *> If JOBU = 'V', U contains columns of U (the left singular
169 *> vectors, stored columnwise) as specified by RANGE; if
170 *> JOBU = 'N', U is not referenced.
171 *> Note: The user must ensure that UCOL >= NS; if RANGE = 'V',
172 *> the exact value of NS is not known ILQFin advance and an upper
173 *> bound must be used.
179 *> The leading dimension of the array U. LDU >= 1; if
180 *> JOBU = 'V', LDU >= M.
185 *> VT is REAL array, dimension (LDVT,N)
186 *> If JOBVT = 'V', VT contains the rows of V**T (the right singular
187 *> vectors, stored rowwise) as specified by RANGE; if JOBVT = 'N',
188 *> VT is not referenced.
189 *> Note: The user must ensure that LDVT >= NS; if RANGE = 'V',
190 *> the exact value of NS is not known in advance and an upper
191 *> bound must be used.
197 *> The leading dimension of the array VT. LDVT >= 1; if
198 *> JOBVT = 'V', LDVT >= NS (see above).
203 *> WORK is REAL array, dimension (MAX(1,LWORK))
204 *> On exit, if INFO = 0, WORK(1) returns the optimal LWORK;
210 *> The dimension of the array WORK.
211 *> LWORK >= MAX(1,MIN(M,N)*(MIN(M,N)+4)) for the paths (see
212 *> comments inside the code):
213 *> - PATH 1 (M much larger than N)
214 *> - PATH 1t (N much larger than M)
215 *> LWORK >= MAX(1,MIN(M,N)*2+MAX(M,N)) for the other paths.
216 *> For good performance, LWORK should generally be larger.
218 *> If LWORK = -1, then a workspace query is assumed; the routine
219 *> only calculates the optimal size of the WORK array, returns
220 *> this value as the first entry of the WORK array, and no error
221 *> message related to LWORK is issued by XERBLA.
226 *> IWORK is INTEGER array, dimension (12*MIN(M,N))
227 *> If INFO = 0, the first NS elements of IWORK are zero. If INFO > 0,
228 *> then IWORK contains the indices of the eigenvectors that failed
229 *> to converge in SBDSVDX/SSTEVX.
235 *> = 0: successful exit
236 *> < 0: if INFO = -i, the i-th argument had an illegal value
237 *> > 0: if INFO = i, then i eigenvectors failed to converge
238 *> in SBDSVDX/SSTEVX.
239 *> if INFO = N*2 + 1, an internal error occurred in
246 *> \author Univ. of Tennessee
247 *> \author Univ. of California Berkeley
248 *> \author Univ. of Colorado Denver
251 *> \date November 2015
253 *> \ingroup realGEsing
255 * =====================================================================
256 SUBROUTINE SGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU,
257 $ IL, IU, NS, S, U, LDU, VT, LDVT, WORK,
258 $ LWORK, IWORK, INFO )
260 * -- LAPACK driver routine (version 3.6.0) --
261 * -- LAPACK is a software package provided by Univ. of Tennessee, --
262 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
265 * .. Scalar Arguments ..
266 CHARACTER JOBU, JOBVT, RANGE
267 INTEGER IL, INFO, IU, LDA, LDU, LDVT, LWORK, M, N, NS
270 * .. Array Arguments ..
272 REAL A( LDA, * ), S( * ), U( LDU, * ),
273 $ VT( LDVT, * ), WORK( * )
276 * =====================================================================
280 PARAMETER ( ZERO = 0.0E0, ONE = 1.0E0 )
282 * .. Local Scalars ..
283 CHARACTER JOBZ, RNGTGK
284 LOGICAL ALLS, INDS, LQUERY, VALS, WANTU, WANTVT
285 INTEGER I, ID, IE, IERR, ILQF, ILTGK, IQRF, ISCL,
286 $ ITAU, ITAUP, ITAUQ, ITEMP, ITGKZ, IUTGK,
287 $ J, MAXWRK, MINMN, MINWRK, MNTHR
288 REAL ABSTOL, ANRM, BIGNUM, EPS, SMLNUM
293 * .. External Subroutines ..
294 EXTERNAL SBDSVDX, SGEBRD, SGELQF, SGEQRF, SLACPY,
295 $ SLASCL, SLASET, SORMBR, SORMLQ, SORMQR,
298 * .. External Functions ..
301 REAL SLAMCH, SLANGE, SNRM2
302 EXTERNAL LSAME, ILAENV, SLAMCH, SLANGE, SNRM2
304 * .. Intrinsic Functions ..
305 INTRINSIC MAX, MIN, SQRT
307 * .. Executable Statements ..
309 * Test the input arguments.
313 ABSTOL = 2*SLAMCH('S')
314 LQUERY = ( LWORK.EQ.-1 )
317 WANTU = LSAME( JOBU, 'V' )
318 WANTVT = LSAME( JOBVT, 'V' )
319 IF( WANTU .OR. WANTVT ) THEN
324 ALLS = LSAME( RANGE, 'A' )
325 VALS = LSAME( RANGE, 'V' )
326 INDS = LSAME( RANGE, 'I' )
329 IF( .NOT.LSAME( JOBU, 'V' ) .AND.
330 $ .NOT.LSAME( JOBU, 'N' ) ) THEN
332 ELSE IF( .NOT.LSAME( JOBVT, 'V' ) .AND.
333 $ .NOT.LSAME( JOBVT, 'N' ) ) THEN
335 ELSE IF( .NOT.( ALLS .OR. VALS .OR. INDS ) ) THEN
337 ELSE IF( M.LT.0 ) THEN
339 ELSE IF( N.LT.0 ) THEN
341 ELSE IF( M.GT.LDA ) THEN
343 ELSE IF( MINMN.GT.0 ) THEN
345 IF( VL.LT.ZERO ) THEN
347 ELSE IF( VU.LE.VL ) THEN
351 IF( IL.LT.1 .OR. IL.GT.MAX( 1, MINMN ) ) THEN
353 ELSE IF( IU.LT.MIN( MINMN, IL ) .OR. IU.GT.MINMN ) THEN
358 IF( WANTU .AND. LDU.LT.M ) THEN
360 ELSE IF( WANTVT .AND. LDVT.LT.MINMN ) THEN
367 * (Note: Comments in the code beginning "Workspace:" describe the
368 * minimal amount of workspace needed at that point in the code,
369 * as well as the preferred amount for good performance.
370 * NB refers to the optimal block size for the immediately
371 * following subroutine, as returned by ILAENV.)
376 IF( MINMN.GT.0 ) THEN
378 MNTHR = ILAENV( 6, 'SGESVD', JOBU // JOBVT, M, N, 0, 0 )
379 IF( M.GE.MNTHR ) THEN
381 * Path 1 (M much larger than N)
383 MAXWRK = N*(N*2+16) +
384 $ N*ILAENV( 1, 'SGEQRF', ' ', M, N, -1, -1 )
385 MAXWRK = MAX( MAXWRK, N*(N*2+20) + 2*N*
386 $ ILAENV( 1, 'SGEBRD', ' ', N, N, -1, -1 ) )
390 * Path 2 (M at least N, but not much larger)
392 MAXWRK = N*(N*2+19) + ( M+N )*
393 $ ILAENV( 1, 'SGEBRD', ' ', M, N, -1, -1 )
394 MINWRK = N*(N*2+20) + M
397 MNTHR = ILAENV( 6, 'SGESVD', JOBU // JOBVT, M, N, 0, 0 )
398 IF( N.GE.MNTHR ) THEN
400 * Path 1t (N much larger than M)
402 MAXWRK = M*(M*2+16) +
403 $ M*ILAENV( 1, 'SGELQF', ' ', M, N, -1, -1 )
404 MAXWRK = MAX( MAXWRK, M*(M*2+20) + 2*M*
405 $ ILAENV( 1, 'SGEBRD', ' ', M, M, -1, -1 ) )
409 * Path 2t (N greater than M, but not much larger)
411 MAXWRK = M*(M*2+19) + ( M+N )*
412 $ ILAENV( 1, 'SGEBRD', ' ', M, N, -1, -1 )
413 MINWRK = M*(M*2+20) + N
417 MAXWRK = MAX( MAXWRK, MINWRK )
418 WORK( 1 ) = REAL( MAXWRK )
420 IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
426 CALL XERBLA( 'SGESVDX', -INFO )
428 ELSE IF( LQUERY ) THEN
432 * Quick return if possible
434 IF( M.EQ.0 .OR. N.EQ.0 ) THEN
438 * Set singular values indices accord to RANGE.
454 * Get machine constants
457 SMLNUM = SQRT( SLAMCH( 'S' ) ) / EPS
458 BIGNUM = ONE / SMLNUM
460 * Scale A if max element outside range [SMLNUM,BIGNUM]
462 ANRM = SLANGE( 'M', M, N, A, LDA, DUM )
464 IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
466 CALL SLASCL( 'G', 0, 0, ANRM, SMLNUM, M, N, A, LDA, INFO )
467 ELSE IF( ANRM.GT.BIGNUM ) THEN
469 CALL SLASCL( 'G', 0, 0, ANRM, BIGNUM, M, N, A, LDA, INFO )
474 * A has at least as many rows as columns. If A has sufficiently
475 * more rows than columns, first reduce A using the QR
478 IF( M.GE.MNTHR ) THEN
480 * Path 1 (M much larger than N):
481 * A = Q * R = Q * ( QB * B * PB**T )
482 * = Q * ( QB * ( UB * S * VB**T ) * PB**T )
483 * U = Q * QB * UB; V**T = VB**T * PB**T
486 * (Workspace: need 2*N, prefer N+N*NB)
490 CALL SGEQRF( M, N, A, LDA, WORK( ITAU ), WORK( ITEMP ),
491 $ LWORK-ITEMP+1, INFO )
493 * Copy R into WORK and bidiagonalize it:
494 * (Workspace: need N*N+5*N, prefer N*N+4*N+2*N*NB)
502 CALL SLACPY( 'U', N, N, A, LDA, WORK( IQRF ), N )
503 CALL SLASET( 'L', N-1, N-1, ZERO, ZERO, WORK( IQRF+1 ), N )
504 CALL SGEBRD( N, N, WORK( IQRF ), N, WORK( ID ), WORK( IE ),
505 $ WORK( ITAUQ ), WORK( ITAUP ), WORK( ITEMP ),
506 $ LWORK-ITEMP+1, INFO )
508 * Solve eigenvalue problem TGK*Z=Z*S.
509 * (Workspace: need 14*N + 2*N*(N+1))
512 ITEMP = ITGKZ + N*(N*2+1)
513 CALL SBDSVDX( 'U', JOBZ, RNGTGK, N, WORK( ID ), WORK( IE ),
514 $ VL, VU, ILTGK, IUTGK, NS, S, WORK( ITGKZ ),
515 $ N*2, WORK( ITEMP ), IWORK, INFO)
517 * If needed, compute left singular vectors.
522 CALL SCOPY( N, WORK( J ), 1, U( 1,I ), 1 )
525 CALL SLASET( 'A', M-N, N, ZERO, ZERO, U( N+1,1 ), LDU )
527 * Call SORMBR to compute QB*UB.
528 * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
530 CALL SORMBR( 'Q', 'L', 'N', N, NS, N, WORK( IQRF ), N,
531 $ WORK( ITAUQ ), U, LDU, WORK( ITEMP ),
532 $ LWORK-ITEMP+1, INFO )
534 * Call SORMQR to compute Q*(QB*UB).
535 * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
537 CALL SORMQR( 'L', 'N', M, NS, N, A, LDA,
538 $ WORK( ITAU ), U, LDU, WORK( ITEMP ),
539 $ LWORK-ITEMP+1, INFO )
542 * If needed, compute right singular vectors.
547 CALL SCOPY( N, WORK( J ), 1, VT( I,1 ), LDVT )
551 * Call SORMBR to compute VB**T * PB**T
552 * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
554 CALL SORMBR( 'P', 'R', 'T', NS, N, N, WORK( IQRF ), N,
555 $ WORK( ITAUP ), VT, LDVT, WORK( ITEMP ),
556 $ LWORK-ITEMP+1, INFO )
560 * Path 2 (M at least N, but not much larger)
561 * Reduce A to bidiagonal form without QR decomposition
562 * A = QB * B * PB**T = QB * ( UB * S * VB**T ) * PB**T
563 * U = QB * UB; V**T = VB**T * PB**T
566 * (Workspace: need 4*N+M, prefer 4*N+(M+N)*NB)
573 CALL SGEBRD( M, N, A, LDA, WORK( ID ), WORK( IE ),
574 $ WORK( ITAUQ ), WORK( ITAUP ), WORK( ITEMP ),
575 $ LWORK-ITEMP+1, INFO )
577 * Solve eigenvalue problem TGK*Z=Z*S.
578 * (Workspace: need 14*N + 2*N*(N+1))
581 ITEMP = ITGKZ + N*(N*2+1)
582 CALL SBDSVDX( 'U', JOBZ, RNGTGK, N, WORK( ID ), WORK( IE ),
583 $ VL, VU, ILTGK, IUTGK, NS, S, WORK( ITGKZ ),
584 $ N*2, WORK( ITEMP ), IWORK, INFO)
586 * If needed, compute left singular vectors.
591 CALL SCOPY( N, WORK( J ), 1, U( 1,I ), 1 )
594 CALL SLASET( 'A', M-N, N, ZERO, ZERO, U( N+1,1 ), LDU )
596 * Call SORMBR to compute QB*UB.
597 * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
599 CALL SORMBR( 'Q', 'L', 'N', M, NS, N, A, LDA,
600 $ WORK( ITAUQ ), U, LDU, WORK( ITEMP ),
601 $ LWORK-ITEMP+1, IERR )
604 * If needed, compute right singular vectors.
609 CALL SCOPY( N, WORK( J ), 1, VT( I,1 ), LDVT )
613 * Call SORMBR to compute VB**T * PB**T
614 * (Workspace in WORK( ITEMP ): need N, prefer N*NB)
616 CALL SORMBR( 'P', 'R', 'T', NS, N, N, A, LDA,
617 $ WORK( ITAUP ), VT, LDVT, WORK( ITEMP ),
618 $ LWORK-ITEMP+1, IERR )
623 * A has more columns than rows. If A has sufficiently more
624 * columns than rows, first reduce A using the LQ decomposition.
626 IF( N.GE.MNTHR ) THEN
628 * Path 1t (N much larger than M):
629 * A = L * Q = ( QB * B * PB**T ) * Q
630 * = ( QB * ( UB * S * VB**T ) * PB**T ) * Q
631 * U = QB * UB ; V**T = VB**T * PB**T * Q
634 * (Workspace: need 2*M, prefer M+M*NB)
638 CALL SGELQF( M, N, A, LDA, WORK( ITAU ), WORK( ITEMP ),
639 $ LWORK-ITEMP+1, INFO )
641 * Copy L into WORK and bidiagonalize it:
642 * (Workspace in WORK( ITEMP ): need M*M+5*N, prefer M*M+4*M+2*M*NB)
650 CALL SLACPY( 'L', M, M, A, LDA, WORK( ILQF ), M )
651 CALL SLASET( 'U', M-1, M-1, ZERO, ZERO, WORK( ILQF+M ), M )
652 CALL SGEBRD( M, M, WORK( ILQF ), M, WORK( ID ), WORK( IE ),
653 $ WORK( ITAUQ ), WORK( ITAUP ), WORK( ITEMP ),
654 $ LWORK-ITEMP+1, INFO )
656 * Solve eigenvalue problem TGK*Z=Z*S.
657 * (Workspace: need 2*M*M+14*M)
660 ITEMP = ITGKZ + M*(M*2+1)
661 CALL SBDSVDX( 'U', JOBZ, RNGTGK, M, WORK( ID ), WORK( IE ),
662 $ VL, VU, ILTGK, IUTGK, NS, S, WORK( ITGKZ ),
663 $ M*2, WORK( ITEMP ), IWORK, INFO)
665 * If needed, compute left singular vectors.
670 CALL SCOPY( M, WORK( J ), 1, U( 1,I ), 1 )
674 * Call SORMBR to compute QB*UB.
675 * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
677 CALL SORMBR( 'Q', 'L', 'N', M, NS, M, WORK( ILQF ), M,
678 $ WORK( ITAUQ ), U, LDU, WORK( ITEMP ),
679 $ LWORK-ITEMP+1, INFO )
682 * If needed, compute right singular vectors.
687 CALL SCOPY( M, WORK( J ), 1, VT( I,1 ), LDVT )
690 CALL SLASET( 'A', M, N-M, ZERO, ZERO, VT( 1,M+1 ), LDVT )
692 * Call SORMBR to compute (VB**T)*(PB**T)
693 * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
695 CALL SORMBR( 'P', 'R', 'T', NS, M, M, WORK( ILQF ), M,
696 $ WORK( ITAUP ), VT, LDVT, WORK( ITEMP ),
697 $ LWORK-ITEMP+1, INFO )
699 * Call SORMLQ to compute ((VB**T)*(PB**T))*Q.
700 * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
702 CALL SORMLQ( 'R', 'N', NS, N, M, A, LDA,
703 $ WORK( ITAU ), VT, LDVT, WORK( ITEMP ),
704 $ LWORK-ITEMP+1, INFO )
708 * Path 2t (N greater than M, but not much larger)
709 * Reduce to bidiagonal form without LQ decomposition
710 * A = QB * B * PB**T = QB * ( UB * S * VB**T ) * PB**T
711 * U = QB * UB; V**T = VB**T * PB**T
714 * (Workspace: need 4*M+N, prefer 4*M+(M+N)*NB)
721 CALL SGEBRD( M, N, A, LDA, WORK( ID ), WORK( IE ),
722 $ WORK( ITAUQ ), WORK( ITAUP ), WORK( ITEMP ),
723 $ LWORK-ITEMP+1, INFO )
725 * Solve eigenvalue problem TGK*Z=Z*S.
726 * (Workspace: need 2*M*M+14*M)
729 ITEMP = ITGKZ + M*(M*2+1)
730 CALL SBDSVDX( 'L', JOBZ, RNGTGK, M, WORK( ID ), WORK( IE ),
731 $ VL, VU, ILTGK, IUTGK, NS, S, WORK( ITGKZ ),
732 $ M*2, WORK( ITEMP ), IWORK, INFO)
734 * If needed, compute left singular vectors.
739 CALL SCOPY( M, WORK( J ), 1, U( 1,I ), 1 )
743 * Call SORMBR to compute QB*UB.
744 * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
746 CALL SORMBR( 'Q', 'L', 'N', M, NS, N, A, LDA,
747 $ WORK( ITAUQ ), U, LDU, WORK( ITEMP ),
748 $ LWORK-ITEMP+1, INFO )
751 * If needed, compute right singular vectors.
756 CALL SCOPY( M, WORK( J ), 1, VT( I,1 ), LDVT )
759 CALL SLASET( 'A', M, N-M, ZERO, ZERO, VT( 1,M+1 ), LDVT )
761 * Call SORMBR to compute VB**T * PB**T
762 * (Workspace in WORK( ITEMP ): need M, prefer M*NB)
764 CALL SORMBR( 'P', 'R', 'T', NS, N, M, A, LDA,
765 $ WORK( ITAUP ), VT, LDVT, WORK( ITEMP ),
766 $ LWORK-ITEMP+1, INFO )
771 * Undo scaling if necessary
775 $ CALL SLASCL( 'G', 0, 0, BIGNUM, ANRM, MINMN, 1,
778 $ CALL SLASCL( 'G', 0, 0, SMLNUM, ANRM, MINMN, 1,
782 * Return optimal workspace in WORK(1)
784 WORK( 1 ) = REAL( MAXWRK )