3 /* Table of constant values */
5 static doublereal c_b15 = -.125;
6 static integer c__1 = 1;
7 static doublereal c_b49 = 1.;
8 static doublereal c_b72 = -1.;
10 /* Subroutine */ int dbdsqr_(char *uplo, integer *n, integer *ncvt, integer *
11 nru, integer *ncc, doublereal *d__, doublereal *e, doublereal *vt,
12 integer *ldvt, doublereal *u, integer *ldu, doublereal *c__, integer *
13 ldc, doublereal *work, integer *info)
15 /* System generated locals */
16 integer c_dim1, c_offset, u_dim1, u_offset, vt_dim1, vt_offset, i__1,
18 doublereal d__1, d__2, d__3, d__4;
20 /* Builtin functions */
21 double pow_dd(doublereal *, doublereal *), sqrt(doublereal), d_sign(
22 doublereal *, doublereal *);
30 integer nm1, nm12, nm13, lll;
31 doublereal eps, sll, tol, abse;
37 doublereal unfl, sinl, cosr, smin, smax, sinr;
38 extern /* Subroutine */ int drot_(integer *, doublereal *, integer *,
39 doublereal *, integer *, doublereal *, doublereal *), dlas2_(
40 doublereal *, doublereal *, doublereal *, doublereal *,
41 doublereal *), dscal_(integer *, doublereal *, doublereal *,
43 extern logical lsame_(char *, char *);
45 extern /* Subroutine */ int dlasr_(char *, char *, char *, integer *,
46 integer *, doublereal *, doublereal *, doublereal *, integer *);
48 doublereal shift, sigmn, oldsn;
49 extern /* Subroutine */ int dswap_(integer *, doublereal *, integer *,
50 doublereal *, integer *);
52 doublereal sminl, sigmx;
54 extern /* Subroutine */ int dlasq1_(integer *, doublereal *, doublereal *,
55 doublereal *, integer *), dlasv2_(doublereal *, doublereal *,
56 doublereal *, doublereal *, doublereal *, doublereal *,
57 doublereal *, doublereal *, doublereal *);
58 extern doublereal dlamch_(char *);
59 extern /* Subroutine */ int dlartg_(doublereal *, doublereal *,
60 doublereal *, doublereal *, doublereal *), xerbla_(char *,
62 doublereal sminoa, thresh;
67 /* -- LAPACK routine (version 3.1.1) -- */
68 /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
71 /* .. Scalar Arguments .. */
73 /* .. Array Arguments .. */
79 /* DBDSQR computes the singular values and, optionally, the right and/or */
80 /* left singular vectors from the singular value decomposition (SVD) of */
81 /* a real N-by-N (upper or lower) bidiagonal matrix B using the implicit */
82 /* zero-shift QR algorithm. The SVD of B has the form */
84 /* B = Q * S * P**T */
86 /* where S is the diagonal matrix of singular values, Q is an orthogonal */
87 /* matrix of left singular vectors, and P is an orthogonal matrix of */
88 /* right singular vectors. If left singular vectors are requested, this */
89 /* subroutine actually returns U*Q instead of Q, and, if right singular */
90 /* vectors are requested, this subroutine returns P**T*VT instead of */
91 /* P**T, for given real input matrices U and VT. When U and VT are the */
92 /* orthogonal matrices that reduce a general matrix A to bidiagonal */
93 /* form: A = U*B*VT, as computed by DGEBRD, then */
95 /* A = (U*Q) * S * (P**T*VT) */
97 /* is the SVD of A. Optionally, the subroutine may also compute Q**T*C */
98 /* for a given real input matrix C. */
100 /* See "Computing Small Singular Values of Bidiagonal Matrices With */
101 /* Guaranteed High Relative Accuracy," by J. Demmel and W. Kahan, */
102 /* LAPACK Working Note #3 (or SIAM J. Sci. Statist. Comput. vol. 11, */
103 /* no. 5, pp. 873-912, Sept 1990) and */
104 /* "Accurate singular values and differential qd algorithms," by */
105 /* B. Parlett and V. Fernando, Technical Report CPAM-554, Mathematics */
106 /* Department, University of California at Berkeley, July 1992 */
107 /* for a detailed description of the algorithm. */
112 /* UPLO (input) CHARACTER*1 */
113 /* = 'U': B is upper bidiagonal; */
114 /* = 'L': B is lower bidiagonal. */
116 /* N (input) INTEGER */
117 /* The order of the matrix B. N >= 0. */
119 /* NCVT (input) INTEGER */
120 /* The number of columns of the matrix VT. NCVT >= 0. */
122 /* NRU (input) INTEGER */
123 /* The number of rows of the matrix U. NRU >= 0. */
125 /* NCC (input) INTEGER */
126 /* The number of columns of the matrix C. NCC >= 0. */
128 /* D (input/output) DOUBLE PRECISION array, dimension (N) */
129 /* On entry, the n diagonal elements of the bidiagonal matrix B. */
130 /* On exit, if INFO=0, the singular values of B in decreasing */
133 /* E (input/output) DOUBLE PRECISION array, dimension (N-1) */
134 /* On entry, the N-1 offdiagonal elements of the bidiagonal */
136 /* On exit, if INFO = 0, E is destroyed; if INFO > 0, D and E */
137 /* will contain the diagonal and superdiagonal elements of a */
138 /* bidiagonal matrix orthogonally equivalent to the one given */
141 /* VT (input/output) DOUBLE PRECISION array, dimension (LDVT, NCVT) */
142 /* On entry, an N-by-NCVT matrix VT. */
143 /* On exit, VT is overwritten by P**T * VT. */
144 /* Not referenced if NCVT = 0. */
146 /* LDVT (input) INTEGER */
147 /* The leading dimension of the array VT. */
148 /* LDVT >= max(1,N) if NCVT > 0; LDVT >= 1 if NCVT = 0. */
150 /* U (input/output) DOUBLE PRECISION array, dimension (LDU, N) */
151 /* On entry, an NRU-by-N matrix U. */
152 /* On exit, U is overwritten by U * Q. */
153 /* Not referenced if NRU = 0. */
155 /* LDU (input) INTEGER */
156 /* The leading dimension of the array U. LDU >= max(1,NRU). */
158 /* C (input/output) DOUBLE PRECISION array, dimension (LDC, NCC) */
159 /* On entry, an N-by-NCC matrix C. */
160 /* On exit, C is overwritten by Q**T * C. */
161 /* Not referenced if NCC = 0. */
163 /* LDC (input) INTEGER */
164 /* The leading dimension of the array C. */
165 /* LDC >= max(1,N) if NCC > 0; LDC >=1 if NCC = 0. */
167 /* WORK (workspace) DOUBLE PRECISION array, dimension (2*N) */
168 /* if NCVT = NRU = NCC = 0, (max(1, 4*N)) otherwise */
170 /* INFO (output) INTEGER */
171 /* = 0: successful exit */
172 /* < 0: If INFO = -i, the i-th argument had an illegal value */
173 /* > 0: the algorithm did not converge; D and E contain the */
174 /* elements of a bidiagonal matrix which is orthogonally */
175 /* similar to the input matrix B; if INFO = i, i */
176 /* elements of E have not converged to zero. */
178 /* Internal Parameters */
179 /* =================== */
181 /* TOLMUL DOUBLE PRECISION, default = max(10,min(100,EPS**(-1/8))) */
182 /* TOLMUL controls the convergence criterion of the QR loop. */
183 /* If it is positive, TOLMUL*EPS is the desired relative */
184 /* precision in the computed singular values. */
185 /* If it is negative, abs(TOLMUL*EPS*sigma_max) is the */
186 /* desired absolute accuracy in the computed singular */
187 /* values (corresponds to relative accuracy */
188 /* abs(TOLMUL*EPS) in the largest singular value. */
189 /* abs(TOLMUL) should be between 1 and 1/EPS, and preferably */
190 /* between 10 (for fast convergence) and .1/EPS */
191 /* (for there to be some accuracy in the results). */
192 /* Default is to lose at either one eighth or 2 of the */
193 /* available decimal digits in each computed singular value */
194 /* (whichever is smaller). */
196 /* MAXITR INTEGER, default = 6 */
197 /* MAXITR controls the maximum number of passes of the */
198 /* algorithm through its inner loop. The algorithms stops */
199 /* (and so fails to converge) if the number of passes */
200 /* through the inner loop exceeds MAXITR*N**2. */
202 /* ===================================================================== */
204 /* .. Parameters .. */
206 /* .. Local Scalars .. */
208 /* .. External Functions .. */
210 /* .. External Subroutines .. */
212 /* .. Intrinsic Functions .. */
214 /* .. Executable Statements .. */
216 /* Test the input parameters. */
218 /* Parameter adjustments */
222 vt_offset = 1 + vt_dim1;
225 u_offset = 1 + u_dim1;
228 c_offset = 1 + c_dim1;
234 lower = lsame_(uplo, "L");
235 if (! lsame_(uplo, "U") && ! lower) {
239 } else if (*ncvt < 0) {
241 } else if (*nru < 0) {
243 } else if (*ncc < 0) {
245 } else if (*ncvt == 0 && *ldvt < 1 || *ncvt > 0 && *ldvt < max(1,*n)) {
247 } else if (*ldu < max(1,*nru)) {
249 } else if (*ncc == 0 && *ldc < 1 || *ncc > 0 && *ldc < max(1,*n)) {
254 xerbla_("DBDSQR", &i__1);
264 /* ROTATE is true if any singular vectors desired, false otherwise */
266 rotate = *ncvt > 0 || *nru > 0 || *ncc > 0;
268 /* If no singular vectors desired, use qd algorithm */
271 dlasq1_(n, &d__[1], &e[1], &work[1], info);
280 /* Get machine constants */
282 eps = dlamch_("Epsilon");
283 unfl = dlamch_("Safe minimum");
285 /* If matrix lower bidiagonal, rotate to be upper bidiagonal */
286 /* by applying Givens rotations on the left */
290 for (i__ = 1; i__ <= i__1; ++i__) {
291 dlartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
293 e[i__] = sn * d__[i__ + 1];
294 d__[i__ + 1] = cs * d__[i__ + 1];
296 work[nm1 + i__] = sn;
300 /* Update singular vectors if desired */
303 dlasr_("R", "V", "F", nru, n, &work[1], &work[*n], &u[u_offset],
307 dlasr_("L", "V", "F", n, ncc, &work[1], &work[*n], &c__[c_offset],
312 /* Compute singular values to relative accuracy TOL */
313 /* (By setting TOL to be negative, algorithm will compute */
314 /* singular values to absolute accuracy ABS(TOL)*norm(input matrix)) */
318 d__3 = 100., d__4 = pow_dd(&eps, &c_b15);
319 d__1 = 10., d__2 = min(d__3,d__4);
320 tolmul = max(d__1,d__2);
323 /* Compute approximate maximum, minimum singular values */
327 for (i__ = 1; i__ <= i__1; ++i__) {
329 d__2 = smax, d__3 = (d__1 = d__[i__], abs(d__1));
330 smax = max(d__2,d__3);
334 for (i__ = 1; i__ <= i__1; ++i__) {
336 d__2 = smax, d__3 = (d__1 = e[i__], abs(d__1));
337 smax = max(d__2,d__3);
343 /* Relative accuracy desired */
345 sminoa = abs(d__[1]);
351 for (i__ = 2; i__ <= i__1; ++i__) {
352 mu = (d__2 = d__[i__], abs(d__2)) * (mu / (mu + (d__1 = e[i__ - 1]
354 sminoa = min(sminoa,mu);
361 sminoa /= sqrt((doublereal) (*n));
363 d__1 = tol * sminoa, d__2 = *n * 6 * *n * unfl;
364 thresh = max(d__1,d__2);
367 /* Absolute accuracy desired */
370 d__1 = abs(tol) * smax, d__2 = *n * 6 * *n * unfl;
371 thresh = max(d__1,d__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. && (d__1 = d__[m], abs(d__1)) <= thresh) {
405 smax = (d__1 = d__[m], abs(d__1));
408 for (lll = 1; lll <= i__1; ++lll) {
410 abss = (d__1 = d__[ll], abs(d__1));
411 abse = (d__1 = e[ll], abs(d__1));
412 if (tol < 0. && abss <= thresh) {
415 if (abse <= thresh) {
418 smin = min(smin,abss);
420 d__1 = max(smax,abss);
421 smax = max(d__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 dlasv2_(&d__[m - 1], &e[m - 1], &d__[m], &sigmn, &sigmx, &sinr, &cosr,
453 /* Compute singular vectors, if desired */
456 drot_(ncvt, &vt[m - 1 + vt_dim1], ldvt, &vt[m + vt_dim1], ldvt, &
460 drot_(nru, &u[(m - 1) * u_dim1 + 1], &c__1, &u[m * u_dim1 + 1], &
464 drot_(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 ((d__1 = d__[ll], abs(d__1)) >= (d__2 = d__[m], abs(d__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 ((d__2 = e[m - 1], abs(d__2)) <= abs(tol) * (d__1 = d__[m], abs(
496 d__1)) || tol < 0. && (d__3 = e[m - 1], abs(d__3)) <= thresh)
504 /* If relative accuracy desired, */
505 /* apply convergence criterion forward */
507 mu = (d__1 = d__[ll], abs(d__1));
510 for (lll = ll; lll <= i__1; ++lll) {
511 if ((d__1 = e[lll], abs(d__1)) <= tol * mu) {
515 mu = (d__2 = d__[lll + 1], abs(d__2)) * (mu / (mu + (d__1 = e[
517 sminl = min(sminl,mu);
524 /* Run convergence test in backward direction */
525 /* First apply standard test to top of matrix */
527 if ((d__2 = e[ll], abs(d__2)) <= abs(tol) * (d__1 = d__[ll], abs(d__1)
528 ) || tol < 0. && (d__3 = e[ll], abs(d__3)) <= thresh) {
535 /* If relative accuracy desired, */
536 /* apply convergence criterion backward */
538 mu = (d__1 = d__[m], abs(d__1));
541 for (lll = m - 1; lll >= i__1; --lll) {
542 if ((d__1 = e[lll], abs(d__1)) <= tol * mu) {
546 mu = (d__2 = d__[lll], abs(d__2)) * (mu / (mu + (d__1 = e[lll]
548 sminl = min(sminl,mu);
556 /* Compute shift. First, test if shifting would ruin relative */
557 /* accuracy, and if so set the shift to zero. */
560 d__1 = eps, d__2 = tol * .01;
561 if (tol >= 0. && *n * tol * (sminl / smax) <= max(d__1,d__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 = (d__1 = d__[ll], abs(d__1));
572 dlas2_(&d__[m - 1], &e[m - 1], &d__[m], &shift, &r__);
574 sll = (d__1 = d__[m], abs(d__1));
575 dlas2_(&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 (d__1 * d__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 d__1 = d__[i__] * cs;
606 dlartg_(&d__1, &e[i__], &cs, &sn, &r__);
608 e[i__ - 1] = oldsn * r__;
611 d__2 = d__[i__ + 1] * sn;
612 dlartg_(&d__1, &d__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 dlasr_("L", "V", "F", &i__1, ncvt, &work[1], &work[*n], &vt[
628 ll + vt_dim1], ldvt);
632 dlasr_("R", "V", "F", nru, &i__1, &work[nm12 + 1], &work[nm13
633 + 1], &u[ll * u_dim1 + 1], ldu);
637 dlasr_("L", "V", "F", &i__1, ncc, &work[nm12 + 1], &work[nm13
638 + 1], &c__[ll + c_dim1], ldc);
641 /* Test convergence */
643 if ((d__1 = e[m - 1], abs(d__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 d__1 = d__[i__] * cs;
657 dlartg_(&d__1, &e[i__ - 1], &cs, &sn, &r__);
659 e[i__] = oldsn * r__;
662 d__2 = d__[i__ - 1] * sn;
663 dlartg_(&d__1, &d__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 dlasr_("L", "V", "B", &i__1, ncvt, &work[nm12 + 1], &work[
679 nm13 + 1], &vt[ll + vt_dim1], ldvt);
683 dlasr_("R", "V", "B", nru, &i__1, &work[1], &work[*n], &u[ll *
688 dlasr_("L", "V", "B", &i__1, ncc, &work[1], &work[*n], &c__[
692 /* Test convergence */
694 if ((d__1 = e[ll], abs(d__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 = ((d__1 = d__[ll], abs(d__1)) - shift) * (d_sign(&c_b49, &d__[
708 ll]) + shift / d__[ll]);
711 for (i__ = ll; i__ <= i__1; ++i__) {
712 dlartg_(&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 dlartg_(&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 dlasr_("L", "V", "F", &i__1, ncvt, &work[1], &work[*n], &vt[
741 ll + vt_dim1], ldvt);
745 dlasr_("R", "V", "F", nru, &i__1, &work[nm12 + 1], &work[nm13
746 + 1], &u[ll * u_dim1 + 1], ldu);
750 dlasr_("L", "V", "F", &i__1, ncc, &work[nm12 + 1], &work[nm13
751 + 1], &c__[ll + c_dim1], ldc);
754 /* Test convergence */
756 if ((d__1 = e[m - 1], abs(d__1)) <= thresh) {
762 /* Chase bulge from bottom to top */
763 /* Save cosines and sines for later singular vector updates */
765 f = ((d__1 = d__[m], abs(d__1)) - shift) * (d_sign(&c_b49, &d__[m]
769 for (i__ = m; i__ >= i__1; --i__) {
770 dlartg_(&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 dlartg_(&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 ((d__1 = e[ll], abs(d__1)) <= thresh) {
800 /* Update singular vectors if desired */
804 dlasr_("L", "V", "B", &i__1, ncvt, &work[nm12 + 1], &work[
805 nm13 + 1], &vt[ll + vt_dim1], ldvt);
809 dlasr_("R", "V", "B", nru, &i__1, &work[1], &work[*n], &u[ll *
814 dlasr_("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__) {
830 d__[i__] = -d__[i__];
832 /* Change sign of singular vectors, if desired */
835 dscal_(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 dswap_(ncvt, &vt[isub + vt_dim1], ldvt, &vt[*n + 1 - i__ +
870 dswap_(nru, &u[isub * u_dim1 + 1], &c__1, &u[(*n + 1 - i__) *
874 dswap_(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__) {