3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download DBDSQR + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dbdsqr.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dbdsqr.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dbdsqr.f">
21 * SUBROUTINE DBDSQR( UPLO, N, NCVT, NRU, NCC, D, E, VT, LDVT, U,
22 * LDU, C, LDC, WORK, INFO )
24 * .. Scalar Arguments ..
26 * INTEGER INFO, LDC, LDU, LDVT, N, NCC, NCVT, NRU
28 * .. Array Arguments ..
29 * DOUBLE PRECISION C( LDC, * ), D( * ), E( * ), U( LDU, * ),
30 * $ VT( LDVT, * ), WORK( * )
39 *> DBDSQR computes the singular values and, optionally, the right and/or
40 *> left singular vectors from the singular value decomposition (SVD) of
41 *> a real N-by-N (upper or lower) bidiagonal matrix B using the implicit
42 *> zero-shift QR algorithm. The SVD of B has the form
46 *> where S is the diagonal matrix of singular values, Q is an orthogonal
47 *> matrix of left singular vectors, and P is an orthogonal matrix of
48 *> right singular vectors. If left singular vectors are requested, this
49 *> subroutine actually returns U*Q instead of Q, and, if right singular
50 *> vectors are requested, this subroutine returns P**T*VT instead of
51 *> P**T, for given real input matrices U and VT. When U and VT are the
52 *> orthogonal matrices that reduce a general matrix A to bidiagonal
53 *> form: A = U*B*VT, as computed by DGEBRD, then
55 *> A = (U*Q) * S * (P**T*VT)
57 *> is the SVD of A. Optionally, the subroutine may also compute Q**T*C
58 *> for a given real input matrix C.
60 *> See "Computing Small Singular Values of Bidiagonal Matrices With
61 *> Guaranteed High Relative Accuracy," by J. Demmel and W. Kahan,
62 *> LAPACK Working Note #3 (or SIAM J. Sci. Statist. Comput. vol. 11,
63 *> no. 5, pp. 873-912, Sept 1990) and
64 *> "Accurate singular values and differential qd algorithms," by
65 *> B. Parlett and V. Fernando, Technical Report CPAM-554, Mathematics
66 *> Department, University of California at Berkeley, July 1992
67 *> for a detailed description of the algorithm.
75 *> UPLO is CHARACTER*1
76 *> = 'U': B is upper bidiagonal;
77 *> = 'L': B is lower bidiagonal.
83 *> The order of the matrix B. N >= 0.
89 *> The number of columns of the matrix VT. NCVT >= 0.
95 *> The number of rows of the matrix U. NRU >= 0.
101 *> The number of columns of the matrix C. NCC >= 0.
106 *> D is DOUBLE PRECISION array, dimension (N)
107 *> On entry, the n diagonal elements of the bidiagonal matrix B.
108 *> On exit, if INFO=0, the singular values of B in decreasing
114 *> E is DOUBLE PRECISION array, dimension (N-1)
115 *> On entry, the N-1 offdiagonal elements of the bidiagonal
117 *> On exit, if INFO = 0, E is destroyed; if INFO > 0, D and E
118 *> will contain the diagonal and superdiagonal elements of a
119 *> bidiagonal matrix orthogonally equivalent to the one given
125 *> VT is DOUBLE PRECISION array, dimension (LDVT, NCVT)
126 *> On entry, an N-by-NCVT matrix VT.
127 *> On exit, VT is overwritten by P**T * VT.
128 *> Not referenced if NCVT = 0.
134 *> The leading dimension of the array VT.
135 *> LDVT >= max(1,N) if NCVT > 0; LDVT >= 1 if NCVT = 0.
140 *> U is DOUBLE PRECISION array, dimension (LDU, N)
141 *> On entry, an NRU-by-N matrix U.
142 *> On exit, U is overwritten by U * Q.
143 *> Not referenced if NRU = 0.
149 *> The leading dimension of the array U. LDU >= max(1,NRU).
154 *> C is DOUBLE PRECISION array, dimension (LDC, NCC)
155 *> On entry, an N-by-NCC matrix C.
156 *> On exit, C is overwritten by Q**T * C.
157 *> Not referenced if NCC = 0.
163 *> The leading dimension of the array C.
164 *> LDC >= max(1,N) if NCC > 0; LDC >=1 if NCC = 0.
169 *> WORK is DOUBLE PRECISION array, dimension (4*N)
175 *> = 0: successful exit
176 *> < 0: If INFO = -i, the i-th argument had an illegal value
178 *> if NCVT = NRU = NCC = 0,
179 *> = 1, a split was marked by a positive value in E
180 *> = 2, current block of Z not diagonalized after 30*N
181 *> iterations (in inner while loop)
182 *> = 3, termination criterion of outer while loop not met
183 *> (program created more than N unreduced blocks)
184 *> else NCVT = NRU = NCC = 0,
185 *> the algorithm did not converge; D and E contain the
186 *> elements of a bidiagonal matrix which is orthogonally
187 *> similar to the input matrix B; if INFO = i, i
188 *> elements of E have not converged to zero.
191 *> \par Internal Parameters:
192 * =========================
195 *> TOLMUL DOUBLE PRECISION, default = max(10,min(100,EPS**(-1/8)))
196 *> TOLMUL controls the convergence criterion of the QR loop.
197 *> If it is positive, TOLMUL*EPS is the desired relative
198 *> precision in the computed singular values.
199 *> If it is negative, abs(TOLMUL*EPS*sigma_max) is the
200 *> desired absolute accuracy in the computed singular
201 *> values (corresponds to relative accuracy
202 *> abs(TOLMUL*EPS) in the largest singular value.
203 *> abs(TOLMUL) should be between 1 and 1/EPS, and preferably
204 *> between 10 (for fast convergence) and .1/EPS
205 *> (for there to be some accuracy in the results).
206 *> Default is to lose at either one eighth or 2 of the
207 *> available decimal digits in each computed singular value
208 *> (whichever is smaller).
210 *> MAXITR INTEGER, default = 6
211 *> MAXITR controls the maximum number of passes of the
212 *> algorithm through its inner loop. The algorithms stops
213 *> (and so fails to converge) if the number of passes
214 *> through the inner loop exceeds MAXITR*N**2.
220 *> \author Univ. of Tennessee
221 *> \author Univ. of California Berkeley
222 *> \author Univ. of Colorado Denver
225 *> \date November 2011
227 *> \ingroup auxOTHERcomputational
229 * =====================================================================
230 SUBROUTINE DBDSQR( UPLO, N, NCVT, NRU, NCC, D, E, VT, LDVT, U,
231 $ LDU, C, LDC, WORK, INFO )
233 * -- LAPACK computational routine (version 3.4.0) --
234 * -- LAPACK is a software package provided by Univ. of Tennessee, --
235 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
238 * .. Scalar Arguments ..
240 INTEGER INFO, LDC, LDU, LDVT, N, NCC, NCVT, NRU
242 * .. Array Arguments ..
243 DOUBLE PRECISION C( LDC, * ), D( * ), E( * ), U( LDU, * ),
244 $ VT( LDVT, * ), WORK( * )
247 * =====================================================================
250 DOUBLE PRECISION ZERO
251 PARAMETER ( ZERO = 0.0D0 )
253 PARAMETER ( ONE = 1.0D0 )
254 DOUBLE PRECISION NEGONE
255 PARAMETER ( NEGONE = -1.0D0 )
256 DOUBLE PRECISION HNDRTH
257 PARAMETER ( HNDRTH = 0.01D0 )
259 PARAMETER ( TEN = 10.0D0 )
260 DOUBLE PRECISION HNDRD
261 PARAMETER ( HNDRD = 100.0D0 )
262 DOUBLE PRECISION MEIGTH
263 PARAMETER ( MEIGTH = -0.125D0 )
265 PARAMETER ( MAXITR = 6 )
267 * .. Local Scalars ..
268 LOGICAL LOWER, ROTATE
269 INTEGER I, IDIR, ISUB, ITER, J, LL, LLL, M, MAXIT, NM1,
270 $ NM12, NM13, OLDLL, OLDM
271 DOUBLE PRECISION ABSE, ABSS, COSL, COSR, CS, EPS, F, G, H, MU,
272 $ OLDCS, OLDSN, R, SHIFT, SIGMN, SIGMX, SINL,
273 $ SINR, SLL, SMAX, SMIN, SMINL, SMINOA,
274 $ SN, THRESH, TOL, TOLMUL, UNFL
276 * .. External Functions ..
278 DOUBLE PRECISION DLAMCH
279 EXTERNAL LSAME, DLAMCH
281 * .. External Subroutines ..
282 EXTERNAL DLARTG, DLAS2, DLASQ1, DLASR, DLASV2, DROT,
283 $ DSCAL, DSWAP, XERBLA
285 * .. Intrinsic Functions ..
286 INTRINSIC ABS, DBLE, MAX, MIN, SIGN, SQRT
288 * .. Executable Statements ..
290 * Test the input parameters.
293 LOWER = LSAME( UPLO, 'L' )
294 IF( .NOT.LSAME( UPLO, 'U' ) .AND. .NOT.LOWER ) THEN
296 ELSE IF( N.LT.0 ) THEN
298 ELSE IF( NCVT.LT.0 ) THEN
300 ELSE IF( NRU.LT.0 ) THEN
302 ELSE IF( NCC.LT.0 ) THEN
304 ELSE IF( ( NCVT.EQ.0 .AND. LDVT.LT.1 ) .OR.
305 $ ( NCVT.GT.0 .AND. LDVT.LT.MAX( 1, N ) ) ) THEN
307 ELSE IF( LDU.LT.MAX( 1, NRU ) ) THEN
309 ELSE IF( ( NCC.EQ.0 .AND. LDC.LT.1 ) .OR.
310 $ ( NCC.GT.0 .AND. LDC.LT.MAX( 1, N ) ) ) THEN
314 CALL XERBLA( 'DBDSQR', -INFO )
322 * ROTATE is true if any singular vectors desired, false otherwise
324 ROTATE = ( NCVT.GT.0 ) .OR. ( NRU.GT.0 ) .OR. ( NCC.GT.0 )
326 * If no singular vectors desired, use qd algorithm
328 IF( .NOT.ROTATE ) THEN
329 CALL DLASQ1( N, D, E, WORK, INFO )
331 * If INFO equals 2, dqds didn't finish, try to finish
333 IF( INFO .NE. 2 ) RETURN
342 * Get machine constants
344 EPS = DLAMCH( 'Epsilon' )
345 UNFL = DLAMCH( 'Safe minimum' )
347 * If matrix lower bidiagonal, rotate to be upper bidiagonal
348 * by applying Givens rotations on the left
352 CALL DLARTG( D( I ), E( I ), CS, SN, R )
355 D( I+1 ) = CS*D( I+1 )
360 * Update singular vectors if desired
363 $ CALL DLASR( 'R', 'V', 'F', NRU, N, WORK( 1 ), WORK( N ), U,
366 $ CALL DLASR( 'L', 'V', 'F', N, NCC, WORK( 1 ), WORK( N ), C,
370 * Compute singular values to relative accuracy TOL
371 * (By setting TOL to be negative, algorithm will compute
372 * singular values to absolute accuracy ABS(TOL)*norm(input matrix))
374 TOLMUL = MAX( TEN, MIN( HNDRD, EPS**MEIGTH ) )
377 * Compute approximate maximum, minimum singular values
381 SMAX = MAX( SMAX, ABS( D( I ) ) )
384 SMAX = MAX( SMAX, ABS( E( I ) ) )
387 IF( TOL.GE.ZERO ) THEN
389 * Relative accuracy desired
391 SMINOA = ABS( D( 1 ) )
396 MU = ABS( D( I ) )*( MU / ( MU+ABS( E( I-1 ) ) ) )
397 SMINOA = MIN( SMINOA, MU )
402 SMINOA = SMINOA / SQRT( DBLE( N ) )
403 THRESH = MAX( TOL*SMINOA, MAXITR*N*N*UNFL )
406 * Absolute accuracy desired
408 THRESH = MAX( ABS( TOL )*SMAX, MAXITR*N*N*UNFL )
411 * Prepare for main iteration loop for the singular values
412 * (MAXIT is the maximum number of passes through the inner
413 * loop permitted before nonconvergence signalled.)
420 * M points to last element of unconverged part of matrix
424 * Begin main iteration loop
428 * Check for convergence or exceeding iteration count
435 * Find diagonal block of matrix to work on
437 IF( TOL.LT.ZERO .AND. ABS( D( M ) ).LE.THRESH )
443 ABSS = ABS( D( LL ) )
444 ABSE = ABS( E( LL ) )
445 IF( TOL.LT.ZERO .AND. ABSS.LE.THRESH )
449 SMIN = MIN( SMIN, ABSS )
450 SMAX = MAX( SMAX, ABSS, ABSE )
457 * Matrix splits since E(LL) = 0
461 * Convergence of bottom singular value, return to top of loop
469 * E(LL) through E(M-1) are nonzero, E(LL-1) is zero
473 * 2 by 2 block, handle separately
475 CALL DLASV2( D( M-1 ), E( M-1 ), D( M ), SIGMN, SIGMX, SINR,
481 * Compute singular vectors, if desired
484 $ CALL DROT( NCVT, VT( M-1, 1 ), LDVT, VT( M, 1 ), LDVT, COSR,
487 $ CALL DROT( NRU, U( 1, M-1 ), 1, U( 1, M ), 1, COSL, SINL )
489 $ CALL DROT( NCC, C( M-1, 1 ), LDC, C( M, 1 ), LDC, COSL,
495 * If working on new submatrix, choose shift direction
496 * (from larger end diagonal element towards smaller)
498 IF( LL.GT.OLDM .OR. M.LT.OLDLL ) THEN
499 IF( ABS( D( LL ) ).GE.ABS( D( M ) ) ) THEN
501 * Chase bulge from top (big end) to bottom (small end)
506 * Chase bulge from bottom (big end) to top (small end)
512 * Apply convergence tests
516 * Run convergence test in forward direction
517 * First apply standard test to bottom of matrix
519 IF( ABS( E( M-1 ) ).LE.ABS( TOL )*ABS( D( M ) ) .OR.
520 $ ( TOL.LT.ZERO .AND. ABS( E( M-1 ) ).LE.THRESH ) ) THEN
525 IF( TOL.GE.ZERO ) THEN
527 * If relative accuracy desired,
528 * apply convergence criterion forward
532 DO 100 LLL = LL, M - 1
533 IF( ABS( E( LLL ) ).LE.TOL*MU ) THEN
537 MU = ABS( D( LLL+1 ) )*( MU / ( MU+ABS( E( LLL ) ) ) )
538 SMINL = MIN( SMINL, MU )
544 * Run convergence test in backward direction
545 * First apply standard test to top of matrix
547 IF( ABS( E( LL ) ).LE.ABS( TOL )*ABS( D( LL ) ) .OR.
548 $ ( TOL.LT.ZERO .AND. ABS( E( LL ) ).LE.THRESH ) ) THEN
553 IF( TOL.GE.ZERO ) THEN
555 * If relative accuracy desired,
556 * apply convergence criterion backward
560 DO 110 LLL = M - 1, LL, -1
561 IF( ABS( E( LLL ) ).LE.TOL*MU ) THEN
565 MU = ABS( D( LLL ) )*( MU / ( MU+ABS( E( LLL ) ) ) )
566 SMINL = MIN( SMINL, MU )
573 * Compute shift. First, test if shifting would ruin relative
574 * accuracy, and if so set the shift to zero.
576 IF( TOL.GE.ZERO .AND. N*TOL*( SMINL / SMAX ).LE.
577 $ MAX( EPS, HNDRTH*TOL ) ) THEN
579 * Use a zero shift to avoid loss of relative accuracy
584 * Compute the shift from 2-by-2 block at end of matrix
588 CALL DLAS2( D( M-1 ), E( M-1 ), D( M ), SHIFT, R )
591 CALL DLAS2( D( LL ), E( LL ), D( LL+1 ), SHIFT, R )
594 * Test if shift negligible, and if so set to zero
596 IF( SLL.GT.ZERO ) THEN
597 IF( ( SHIFT / SLL )**2.LT.EPS )
602 * Increment iteration count
606 * If SHIFT = 0, do simplified QR iteration
608 IF( SHIFT.EQ.ZERO ) THEN
611 * Chase bulge from top to bottom
612 * Save cosines and sines for later singular vector updates
617 CALL DLARTG( D( I )*CS, E( I ), CS, SN, R )
620 CALL DLARTG( OLDCS*R, D( I+1 )*SN, OLDCS, OLDSN, D( I ) )
622 WORK( I-LL+1+NM1 ) = SN
623 WORK( I-LL+1+NM12 ) = OLDCS
624 WORK( I-LL+1+NM13 ) = OLDSN
630 * Update singular vectors
633 $ CALL DLASR( 'L', 'V', 'F', M-LL+1, NCVT, WORK( 1 ),
634 $ WORK( N ), VT( LL, 1 ), LDVT )
636 $ CALL DLASR( 'R', 'V', 'F', NRU, M-LL+1, WORK( NM12+1 ),
637 $ WORK( NM13+1 ), U( 1, LL ), LDU )
639 $ CALL DLASR( 'L', 'V', 'F', M-LL+1, NCC, WORK( NM12+1 ),
640 $ WORK( NM13+1 ), C( LL, 1 ), LDC )
644 IF( ABS( E( M-1 ) ).LE.THRESH )
649 * Chase bulge from bottom to top
650 * Save cosines and sines for later singular vector updates
654 DO 130 I = M, LL + 1, -1
655 CALL DLARTG( D( I )*CS, E( I-1 ), CS, SN, R )
658 CALL DLARTG( OLDCS*R, D( I-1 )*SN, OLDCS, OLDSN, D( I ) )
660 WORK( I-LL+NM1 ) = -SN
661 WORK( I-LL+NM12 ) = OLDCS
662 WORK( I-LL+NM13 ) = -OLDSN
668 * Update singular vectors
671 $ CALL DLASR( 'L', 'V', 'B', M-LL+1, NCVT, WORK( NM12+1 ),
672 $ WORK( NM13+1 ), VT( LL, 1 ), LDVT )
674 $ CALL DLASR( 'R', 'V', 'B', NRU, M-LL+1, WORK( 1 ),
675 $ WORK( N ), U( 1, LL ), LDU )
677 $ CALL DLASR( 'L', 'V', 'B', M-LL+1, NCC, WORK( 1 ),
678 $ WORK( N ), C( LL, 1 ), LDC )
682 IF( ABS( E( LL ) ).LE.THRESH )
691 * Chase bulge from top to bottom
692 * Save cosines and sines for later singular vector updates
694 F = ( ABS( D( LL ) )-SHIFT )*
695 $ ( SIGN( ONE, D( LL ) )+SHIFT / D( LL ) )
698 CALL DLARTG( F, G, COSR, SINR, R )
701 F = COSR*D( I ) + SINR*E( I )
702 E( I ) = COSR*E( I ) - SINR*D( I )
704 D( I+1 ) = COSR*D( I+1 )
705 CALL DLARTG( F, G, COSL, SINL, R )
707 F = COSL*E( I ) + SINL*D( I+1 )
708 D( I+1 ) = COSL*D( I+1 ) - SINL*E( I )
711 E( I+1 ) = COSL*E( I+1 )
713 WORK( I-LL+1 ) = COSR
714 WORK( I-LL+1+NM1 ) = SINR
715 WORK( I-LL+1+NM12 ) = COSL
716 WORK( I-LL+1+NM13 ) = SINL
720 * Update singular vectors
723 $ CALL DLASR( 'L', 'V', 'F', M-LL+1, NCVT, WORK( 1 ),
724 $ WORK( N ), VT( LL, 1 ), LDVT )
726 $ CALL DLASR( 'R', 'V', 'F', NRU, M-LL+1, WORK( NM12+1 ),
727 $ WORK( NM13+1 ), U( 1, LL ), LDU )
729 $ CALL DLASR( 'L', 'V', 'F', M-LL+1, NCC, WORK( NM12+1 ),
730 $ WORK( NM13+1 ), C( LL, 1 ), LDC )
734 IF( ABS( E( M-1 ) ).LE.THRESH )
739 * Chase bulge from bottom to top
740 * Save cosines and sines for later singular vector updates
742 F = ( ABS( D( M ) )-SHIFT )*( SIGN( ONE, D( M ) )+SHIFT /
745 DO 150 I = M, LL + 1, -1
746 CALL DLARTG( F, G, COSR, SINR, R )
749 F = COSR*D( I ) + SINR*E( I-1 )
750 E( I-1 ) = COSR*E( I-1 ) - SINR*D( I )
752 D( I-1 ) = COSR*D( I-1 )
753 CALL DLARTG( F, G, COSL, SINL, R )
755 F = COSL*E( I-1 ) + SINL*D( I-1 )
756 D( I-1 ) = COSL*D( I-1 ) - SINL*E( I-1 )
759 E( I-2 ) = COSL*E( I-2 )
762 WORK( I-LL+NM1 ) = -SINR
763 WORK( I-LL+NM12 ) = COSL
764 WORK( I-LL+NM13 ) = -SINL
770 IF( ABS( E( LL ) ).LE.THRESH )
773 * Update singular vectors if desired
776 $ CALL DLASR( 'L', 'V', 'B', M-LL+1, NCVT, WORK( NM12+1 ),
777 $ WORK( NM13+1 ), VT( LL, 1 ), LDVT )
779 $ CALL DLASR( 'R', 'V', 'B', NRU, M-LL+1, WORK( 1 ),
780 $ WORK( N ), U( 1, LL ), LDU )
782 $ CALL DLASR( 'L', 'V', 'B', M-LL+1, NCC, WORK( 1 ),
783 $ WORK( N ), C( LL, 1 ), LDC )
787 * QR iteration finished, go back and check convergence
791 * All singular values converged, so make them positive
795 IF( D( I ).LT.ZERO ) THEN
798 * Change sign of singular vectors, if desired
801 $ CALL DSCAL( NCVT, NEGONE, VT( I, 1 ), LDVT )
805 * Sort the singular values into decreasing order (insertion sort on
806 * singular values, but only one transposition per singular vector)
810 * Scan for smallest D(I)
814 DO 180 J = 2, N + 1 - I
815 IF( D( J ).LE.SMIN ) THEN
820 IF( ISUB.NE.N+1-I ) THEN
822 * Swap singular values and vectors
824 D( ISUB ) = D( N+1-I )
827 $ CALL DSWAP( NCVT, VT( ISUB, 1 ), LDVT, VT( N+1-I, 1 ),
830 $ CALL DSWAP( NRU, U( 1, ISUB ), 1, U( 1, N+1-I ), 1 )
832 $ CALL DSWAP( NCC, C( ISUB, 1 ), LDC, C( N+1-I, 1 ), LDC )
837 * Maximum number of iterations exceeded, failure to converge