3 /* Table of constant values */
5 static doublereal c_b15 = -.125;
6 static integer c__1 = 1;
7 static real c_b49 = 1.f;
8 static real c_b72 = -1.f;
10 /* Subroutine */ int sbdsqr_(char *uplo, integer *n, integer *ncvt, integer *
11 nru, integer *ncc, real *d__, real *e, real *vt, integer *ldvt, real *
12 u, integer *ldu, real *c__, integer *ldc, real *work, integer *info)
14 /* System generated locals */
15 integer c_dim1, c_offset, u_dim1, u_offset, vt_dim1, vt_offset, i__1,
17 real r__1, r__2, r__3, r__4;
20 /* Builtin functions */
21 double pow_dd(doublereal *, doublereal *), sqrt(doublereal), r_sign(real *
30 integer nm1, nm12, nm13, lll;
31 real eps, sll, tol, abse;
37 real unfl, sinl, cosr, smin, smax, sinr;
38 extern /* Subroutine */ int srot_(integer *, real *, integer *, real *,
39 integer *, real *, real *), slas2_(real *, real *, real *, real *,
41 extern logical lsame_(char *, char *);
43 extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
45 real shift, sigmn, oldsn;
48 extern /* Subroutine */ int slasr_(char *, char *, char *, integer *,
49 integer *, real *, real *, real *, integer *);
52 extern /* Subroutine */ int sswap_(integer *, real *, integer *, real *,
53 integer *), slasq1_(integer *, real *, real *, real *, integer *),
54 slasv2_(real *, real *, real *, real *, real *, real *, real *,
56 extern doublereal slamch_(char *);
57 extern /* Subroutine */ int xerbla_(char *, integer *);
59 extern /* Subroutine */ int slartg_(real *, real *, real *, real *, real *
66 /* -- LAPACK routine (version 3.1.1) -- */
67 /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
70 /* .. Scalar Arguments .. */
72 /* .. Array Arguments .. */
78 /* SBDSQR computes the singular values and, optionally, the right and/or */
79 /* left singular vectors from the singular value decomposition (SVD) of */
80 /* a real N-by-N (upper or lower) bidiagonal matrix B using the implicit */
81 /* zero-shift QR algorithm. The SVD of B has the form */
83 /* B = Q * S * P**T */
85 /* where S is the diagonal matrix of singular values, Q is an orthogonal */
86 /* matrix of left singular vectors, and P is an orthogonal matrix of */
87 /* right singular vectors. If left singular vectors are requested, this */
88 /* subroutine actually returns U*Q instead of Q, and, if right singular */
89 /* vectors are requested, this subroutine returns P**T*VT instead of */
90 /* P**T, for given real input matrices U and VT. When U and VT are the */
91 /* orthogonal matrices that reduce a general matrix A to bidiagonal */
92 /* form: A = U*B*VT, as computed by SGEBRD, then */
94 /* A = (U*Q) * S * (P**T*VT) */
96 /* is the SVD of A. Optionally, the subroutine may also compute Q**T*C */
97 /* for a given real input matrix C. */
99 /* See "Computing Small Singular Values of Bidiagonal Matrices With */
100 /* Guaranteed High Relative Accuracy," by J. Demmel and W. Kahan, */
101 /* LAPACK Working Note #3 (or SIAM J. Sci. Statist. Comput. vol. 11, */
102 /* no. 5, pp. 873-912, Sept 1990) and */
103 /* "Accurate singular values and differential qd algorithms," by */
104 /* B. Parlett and V. Fernando, Technical Report CPAM-554, Mathematics */
105 /* Department, University of California at Berkeley, July 1992 */
106 /* for a detailed description of the algorithm. */
111 /* UPLO (input) CHARACTER*1 */
112 /* = 'U': B is upper bidiagonal; */
113 /* = 'L': B is lower bidiagonal. */
115 /* N (input) INTEGER */
116 /* The order of the matrix B. N >= 0. */
118 /* NCVT (input) INTEGER */
119 /* The number of columns of the matrix VT. NCVT >= 0. */
121 /* NRU (input) INTEGER */
122 /* The number of rows of the matrix U. NRU >= 0. */
124 /* NCC (input) INTEGER */
125 /* The number of columns of the matrix C. NCC >= 0. */
127 /* D (input/output) REAL array, dimension (N) */
128 /* On entry, the n diagonal elements of the bidiagonal matrix B. */
129 /* On exit, if INFO=0, the singular values of B in decreasing */
132 /* E (input/output) REAL array, dimension (N-1) */
133 /* On entry, the N-1 offdiagonal elements of the bidiagonal */
135 /* On exit, if INFO = 0, E is destroyed; if INFO > 0, D and E */
136 /* will contain the diagonal and superdiagonal elements of a */
137 /* bidiagonal matrix orthogonally equivalent to the one given */
140 /* VT (input/output) REAL array, dimension (LDVT, NCVT) */
141 /* On entry, an N-by-NCVT matrix VT. */
142 /* On exit, VT is overwritten by P**T * VT. */
143 /* Not referenced if NCVT = 0. */
145 /* LDVT (input) INTEGER */
146 /* The leading dimension of the array VT. */
147 /* LDVT >= max(1,N) if NCVT > 0; LDVT >= 1 if NCVT = 0. */
149 /* U (input/output) REAL array, dimension (LDU, N) */
150 /* On entry, an NRU-by-N matrix U. */
151 /* On exit, U is overwritten by U * Q. */
152 /* Not referenced if NRU = 0. */
154 /* LDU (input) INTEGER */
155 /* The leading dimension of the array U. LDU >= max(1,NRU). */
157 /* C (input/output) REAL array, dimension (LDC, NCC) */
158 /* On entry, an N-by-NCC matrix C. */
159 /* On exit, C is overwritten by Q**T * C. */
160 /* Not referenced if NCC = 0. */
162 /* LDC (input) INTEGER */
163 /* The leading dimension of the array C. */
164 /* LDC >= max(1,N) if NCC > 0; LDC >=1 if NCC = 0. */
166 /* WORK (workspace) REAL array, dimension (2*N) */
167 /* if NCVT = NRU = NCC = 0, (max(1, 4*N)) otherwise */
169 /* INFO (output) INTEGER */
170 /* = 0: successful exit */
171 /* < 0: If INFO = -i, the i-th argument had an illegal value */
172 /* > 0: the algorithm did not converge; D and E contain the */
173 /* elements of a bidiagonal matrix which is orthogonally */
174 /* similar to the input matrix B; if INFO = i, i */
175 /* elements of E have not converged to zero. */
177 /* Internal Parameters */
178 /* =================== */
180 /* TOLMUL REAL, default = max(10,min(100,EPS**(-1/8))) */
181 /* TOLMUL controls the convergence criterion of the QR loop. */
182 /* If it is positive, TOLMUL*EPS is the desired relative */
183 /* precision in the computed singular values. */
184 /* If it is negative, abs(TOLMUL*EPS*sigma_max) is the */
185 /* desired absolute accuracy in the computed singular */
186 /* values (corresponds to relative accuracy */
187 /* abs(TOLMUL*EPS) in the largest singular value. */
188 /* abs(TOLMUL) should be between 1 and 1/EPS, and preferably */
189 /* between 10 (for fast convergence) and .1/EPS */
190 /* (for there to be some accuracy in the results). */
191 /* Default is to lose at either one eighth or 2 of the */
192 /* available decimal digits in each computed singular value */
193 /* (whichever is smaller). */
195 /* MAXITR INTEGER, default = 6 */
196 /* MAXITR controls the maximum number of passes of the */
197 /* algorithm through its inner loop. The algorithms stops */
198 /* (and so fails to converge) if the number of passes */
199 /* through the inner loop exceeds MAXITR*N**2. */
201 /* ===================================================================== */
203 /* .. Parameters .. */
205 /* .. Local Scalars .. */
207 /* .. External Functions .. */
209 /* .. External Subroutines .. */
211 /* .. Intrinsic Functions .. */
213 /* .. Executable Statements .. */
215 /* Test the input parameters. */
217 /* Parameter adjustments */
221 vt_offset = 1 + vt_dim1;
224 u_offset = 1 + u_dim1;
227 c_offset = 1 + c_dim1;
233 lower = lsame_(uplo, "L");
234 if (! lsame_(uplo, "U") && ! lower) {
238 } else if (*ncvt < 0) {
240 } else if (*nru < 0) {
242 } else if (*ncc < 0) {
244 } else if (*ncvt == 0 && *ldvt < 1 || *ncvt > 0 && *ldvt < max(1,*n)) {
246 } else if (*ldu < max(1,*nru)) {
248 } else if (*ncc == 0 && *ldc < 1 || *ncc > 0 && *ldc < max(1,*n)) {
253 xerbla_("SBDSQR", &i__1);
263 /* ROTATE is true if any singular vectors desired, false otherwise */
265 rotate = *ncvt > 0 || *nru > 0 || *ncc > 0;
267 /* If no singular vectors desired, use qd algorithm */
270 slasq1_(n, &d__[1], &e[1], &work[1], info);
279 /* Get machine constants */
281 eps = slamch_("Epsilon");
282 unfl = slamch_("Safe minimum");
284 /* If matrix lower bidiagonal, rotate to be upper bidiagonal */
285 /* by applying Givens rotations on the left */
289 for (i__ = 1; i__ <= i__1; ++i__) {
290 slartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
292 e[i__] = sn * d__[i__ + 1];
293 d__[i__ + 1] = cs * d__[i__ + 1];
295 work[nm1 + i__] = sn;
299 /* Update singular vectors if desired */
302 slasr_("R", "V", "F", nru, n, &work[1], &work[*n], &u[u_offset],
306 slasr_("L", "V", "F", n, ncc, &work[1], &work[*n], &c__[c_offset],
311 /* Compute singular values to relative accuracy TOL */
312 /* (By setting TOL to be negative, algorithm will compute */
313 /* singular values to absolute accuracy ABS(TOL)*norm(input matrix)) */
317 d__1 = (doublereal) eps;
318 r__3 = 100.f, r__4 = pow_dd(&d__1, &c_b15);
319 r__1 = 10.f, r__2 = dmin(r__3,r__4);
320 tolmul = dmax(r__1,r__2);
323 /* Compute approximate maximum, minimum singular values */
327 for (i__ = 1; i__ <= i__1; ++i__) {
329 r__2 = smax, r__3 = (r__1 = d__[i__], dabs(r__1));
330 smax = dmax(r__2,r__3);
334 for (i__ = 1; i__ <= i__1; ++i__) {
336 r__2 = smax, r__3 = (r__1 = e[i__], dabs(r__1));
337 smax = dmax(r__2,r__3);
343 /* Relative accuracy desired */
345 sminoa = dabs(d__[1]);
351 for (i__ = 2; i__ <= i__1; ++i__) {
352 mu = (r__2 = d__[i__], dabs(r__2)) * (mu / (mu + (r__1 = e[i__ -
354 sminoa = dmin(sminoa,mu);
361 sminoa /= sqrt((real) (*n));
363 r__1 = tol * sminoa, r__2 = *n * 6 * *n * unfl;
364 thresh = dmax(r__1,r__2);
367 /* Absolute accuracy desired */
370 r__1 = dabs(tol) * smax, r__2 = *n * 6 * *n * unfl;
371 thresh = dmax(r__1,r__2);
374 /* Prepare for main iteration loop for the singular values */
375 /* (MAXIT is the maximum number of passes through the inner */
376 /* loop permitted before nonconvergence signalled.) */
383 /* M points to last element of unconverged part of matrix */
387 /* Begin main iteration loop */
391 /* Check for convergence or exceeding iteration count */
400 /* Find diagonal block of matrix to work on */
402 if (tol < 0.f && (r__1 = d__[m], dabs(r__1)) <= thresh) {
405 smax = (r__1 = d__[m], dabs(r__1));
408 for (lll = 1; lll <= i__1; ++lll) {
410 abss = (r__1 = d__[ll], dabs(r__1));
411 abse = (r__1 = e[ll], dabs(r__1));
412 if (tol < 0.f && abss <= thresh) {
415 if (abse <= thresh) {
418 smin = dmin(smin,abss);
420 r__1 = max(smax,abss);
421 smax = dmax(r__1,abse);
429 /* Matrix splits since E(LL) = 0 */
433 /* Convergence of bottom singular value, return to top of loop */
441 /* E(LL) through E(M-1) are nonzero, E(LL-1) is zero */
445 /* 2 by 2 block, handle separately */
447 slasv2_(&d__[m - 1], &e[m - 1], &d__[m], &sigmn, &sigmx, &sinr, &cosr,
453 /* Compute singular vectors, if desired */
456 srot_(ncvt, &vt[m - 1 + vt_dim1], ldvt, &vt[m + vt_dim1], ldvt, &
460 srot_(nru, &u[(m - 1) * u_dim1 + 1], &c__1, &u[m * u_dim1 + 1], &
464 srot_(ncc, &c__[m - 1 + c_dim1], ldc, &c__[m + c_dim1], ldc, &
471 /* If working on new submatrix, choose shift direction */
472 /* (from larger end diagonal element towards smaller) */
474 if (ll > oldm || m < oldll) {
475 if ((r__1 = d__[ll], dabs(r__1)) >= (r__2 = d__[m], dabs(r__2))) {
477 /* Chase bulge from top (big end) to bottom (small end) */
482 /* Chase bulge from bottom (big end) to top (small end) */
488 /* Apply convergence tests */
492 /* Run convergence test in forward direction */
493 /* First apply standard test to bottom of matrix */
495 if ((r__2 = e[m - 1], dabs(r__2)) <= dabs(tol) * (r__1 = d__[m], dabs(
496 r__1)) || tol < 0.f && (r__3 = e[m - 1], dabs(r__3)) <=
504 /* If relative accuracy desired, */
505 /* apply convergence criterion forward */
507 mu = (r__1 = d__[ll], dabs(r__1));
510 for (lll = ll; lll <= i__1; ++lll) {
511 if ((r__1 = e[lll], dabs(r__1)) <= tol * mu) {
515 mu = (r__2 = d__[lll + 1], dabs(r__2)) * (mu / (mu + (r__1 =
516 e[lll], dabs(r__1))));
517 sminl = dmin(sminl,mu);
524 /* Run convergence test in backward direction */
525 /* First apply standard test to top of matrix */
527 if ((r__2 = e[ll], dabs(r__2)) <= dabs(tol) * (r__1 = d__[ll], dabs(
528 r__1)) || tol < 0.f && (r__3 = e[ll], dabs(r__3)) <= thresh) {
535 /* If relative accuracy desired, */
536 /* apply convergence criterion backward */
538 mu = (r__1 = d__[m], dabs(r__1));
541 for (lll = m - 1; lll >= i__1; --lll) {
542 if ((r__1 = e[lll], dabs(r__1)) <= tol * mu) {
546 mu = (r__2 = d__[lll], dabs(r__2)) * (mu / (mu + (r__1 = e[
548 sminl = dmin(sminl,mu);
556 /* Compute shift. First, test if shifting would ruin relative */
557 /* accuracy, and if so set the shift to zero. */
560 r__1 = eps, r__2 = tol * .01f;
561 if (tol >= 0.f && *n * tol * (sminl / smax) <= dmax(r__1,r__2)) {
563 /* Use a zero shift to avoid loss of relative accuracy */
568 /* Compute the shift from 2-by-2 block at end of matrix */
571 sll = (r__1 = d__[ll], dabs(r__1));
572 slas2_(&d__[m - 1], &e[m - 1], &d__[m], &shift, &r__);
574 sll = (r__1 = d__[m], dabs(r__1));
575 slas2_(&d__[ll], &e[ll], &d__[ll + 1], &shift, &r__);
578 /* Test if shift negligible, and if so set to zero */
581 /* Computing 2nd power */
583 if (r__1 * r__1 < eps) {
589 /* Increment iteration count */
591 iter = iter + m - ll;
593 /* If SHIFT = 0, do simplified QR iteration */
598 /* Chase bulge from top to bottom */
599 /* Save cosines and sines for later singular vector updates */
604 for (i__ = ll; i__ <= i__1; ++i__) {
605 r__1 = d__[i__] * cs;
606 slartg_(&r__1, &e[i__], &cs, &sn, &r__);
608 e[i__ - 1] = oldsn * r__;
611 r__2 = d__[i__ + 1] * sn;
612 slartg_(&r__1, &r__2, &oldcs, &oldsn, &d__[i__]);
613 work[i__ - ll + 1] = cs;
614 work[i__ - ll + 1 + nm1] = sn;
615 work[i__ - ll + 1 + nm12] = oldcs;
616 work[i__ - ll + 1 + nm13] = oldsn;
620 d__[m] = h__ * oldcs;
621 e[m - 1] = h__ * oldsn;
623 /* Update singular vectors */
627 slasr_("L", "V", "F", &i__1, ncvt, &work[1], &work[*n], &vt[
628 ll + vt_dim1], ldvt);
632 slasr_("R", "V", "F", nru, &i__1, &work[nm12 + 1], &work[nm13
633 + 1], &u[ll * u_dim1 + 1], ldu);
637 slasr_("L", "V", "F", &i__1, ncc, &work[nm12 + 1], &work[nm13
638 + 1], &c__[ll + c_dim1], ldc);
641 /* Test convergence */
643 if ((r__1 = e[m - 1], dabs(r__1)) <= thresh) {
649 /* Chase bulge from bottom to top */
650 /* Save cosines and sines for later singular vector updates */
655 for (i__ = m; i__ >= i__1; --i__) {
656 r__1 = d__[i__] * cs;
657 slartg_(&r__1, &e[i__ - 1], &cs, &sn, &r__);
659 e[i__] = oldsn * r__;
662 r__2 = d__[i__ - 1] * sn;
663 slartg_(&r__1, &r__2, &oldcs, &oldsn, &d__[i__]);
665 work[i__ - ll + nm1] = -sn;
666 work[i__ - ll + nm12] = oldcs;
667 work[i__ - ll + nm13] = -oldsn;
671 d__[ll] = h__ * oldcs;
674 /* Update singular vectors */
678 slasr_("L", "V", "B", &i__1, ncvt, &work[nm12 + 1], &work[
679 nm13 + 1], &vt[ll + vt_dim1], ldvt);
683 slasr_("R", "V", "B", nru, &i__1, &work[1], &work[*n], &u[ll *
688 slasr_("L", "V", "B", &i__1, ncc, &work[1], &work[*n], &c__[
692 /* Test convergence */
694 if ((r__1 = e[ll], dabs(r__1)) <= thresh) {
700 /* Use nonzero shift */
704 /* Chase bulge from top to bottom */
705 /* Save cosines and sines for later singular vector updates */
707 f = ((r__1 = d__[ll], dabs(r__1)) - shift) * (r_sign(&c_b49, &d__[
708 ll]) + shift / d__[ll]);
711 for (i__ = ll; i__ <= i__1; ++i__) {
712 slartg_(&f, &g, &cosr, &sinr, &r__);
716 f = cosr * d__[i__] + sinr * e[i__];
717 e[i__] = cosr * e[i__] - sinr * d__[i__];
718 g = sinr * d__[i__ + 1];
719 d__[i__ + 1] = cosr * d__[i__ + 1];
720 slartg_(&f, &g, &cosl, &sinl, &r__);
722 f = cosl * e[i__] + sinl * d__[i__ + 1];
723 d__[i__ + 1] = cosl * d__[i__ + 1] - sinl * e[i__];
725 g = sinl * e[i__ + 1];
726 e[i__ + 1] = cosl * e[i__ + 1];
728 work[i__ - ll + 1] = cosr;
729 work[i__ - ll + 1 + nm1] = sinr;
730 work[i__ - ll + 1 + nm12] = cosl;
731 work[i__ - ll + 1 + nm13] = sinl;
736 /* Update singular vectors */
740 slasr_("L", "V", "F", &i__1, ncvt, &work[1], &work[*n], &vt[
741 ll + vt_dim1], ldvt);
745 slasr_("R", "V", "F", nru, &i__1, &work[nm12 + 1], &work[nm13
746 + 1], &u[ll * u_dim1 + 1], ldu);
750 slasr_("L", "V", "F", &i__1, ncc, &work[nm12 + 1], &work[nm13
751 + 1], &c__[ll + c_dim1], ldc);
754 /* Test convergence */
756 if ((r__1 = e[m - 1], dabs(r__1)) <= thresh) {
762 /* Chase bulge from bottom to top */
763 /* Save cosines and sines for later singular vector updates */
765 f = ((r__1 = d__[m], dabs(r__1)) - shift) * (r_sign(&c_b49, &d__[
766 m]) + shift / d__[m]);
769 for (i__ = m; i__ >= i__1; --i__) {
770 slartg_(&f, &g, &cosr, &sinr, &r__);
774 f = cosr * d__[i__] + sinr * e[i__ - 1];
775 e[i__ - 1] = cosr * e[i__ - 1] - sinr * d__[i__];
776 g = sinr * d__[i__ - 1];
777 d__[i__ - 1] = cosr * d__[i__ - 1];
778 slartg_(&f, &g, &cosl, &sinl, &r__);
780 f = cosl * e[i__ - 1] + sinl * d__[i__ - 1];
781 d__[i__ - 1] = cosl * d__[i__ - 1] - sinl * e[i__ - 1];
783 g = sinl * e[i__ - 2];
784 e[i__ - 2] = cosl * e[i__ - 2];
786 work[i__ - ll] = cosr;
787 work[i__ - ll + nm1] = -sinr;
788 work[i__ - ll + nm12] = cosl;
789 work[i__ - ll + nm13] = -sinl;
794 /* Test convergence */
796 if ((r__1 = e[ll], dabs(r__1)) <= thresh) {
800 /* Update singular vectors if desired */
804 slasr_("L", "V", "B", &i__1, ncvt, &work[nm12 + 1], &work[
805 nm13 + 1], &vt[ll + vt_dim1], ldvt);
809 slasr_("R", "V", "B", nru, &i__1, &work[1], &work[*n], &u[ll *
814 slasr_("L", "V", "B", &i__1, ncc, &work[1], &work[*n], &c__[
820 /* QR iteration finished, go back and check convergence */
824 /* All singular values converged, so make them positive */
828 for (i__ = 1; i__ <= i__1; ++i__) {
829 if (d__[i__] < 0.f) {
830 d__[i__] = -d__[i__];
832 /* Change sign of singular vectors, if desired */
835 sscal_(ncvt, &c_b72, &vt[i__ + vt_dim1], ldvt);
841 /* Sort the singular values into decreasing order (insertion sort on */
842 /* singular values, but only one transposition per singular vector) */
845 for (i__ = 1; i__ <= i__1; ++i__) {
847 /* Scan for smallest D(I) */
852 for (j = 2; j <= i__2; ++j) {
853 if (d__[j] <= smin) {
859 if (isub != *n + 1 - i__) {
861 /* Swap singular values and vectors */
863 d__[isub] = d__[*n + 1 - i__];
864 d__[*n + 1 - i__] = smin;
866 sswap_(ncvt, &vt[isub + vt_dim1], ldvt, &vt[*n + 1 - i__ +
870 sswap_(nru, &u[isub * u_dim1 + 1], &c__1, &u[(*n + 1 - i__) *
874 sswap_(ncc, &c__[isub + c_dim1], ldc, &c__[*n + 1 - i__ +
882 /* Maximum number of iterations exceeded, failure to converge */
887 for (i__ = 1; i__ <= i__1; ++i__) {