3 /* Table of constant values */
5 static integer c__9 = 9;
6 static integer c__0 = 0;
7 static doublereal c_b15 = 1.;
8 static integer c__1 = 1;
9 static doublereal c_b29 = 0.;
11 /* Subroutine */ int dbdsdc_(char *uplo, char *compq, integer *n, doublereal *
12 d__, doublereal *e, doublereal *u, integer *ldu, doublereal *vt,
13 integer *ldvt, doublereal *q, integer *iq, doublereal *work, integer *
16 /* System generated locals */
17 integer u_dim1, u_offset, vt_dim1, vt_offset, i__1, i__2;
20 /* Builtin functions */
21 double d_sign(doublereal *, doublereal *), log(doublereal);
26 integer z__, ic, ii, kk;
32 integer ivt, difl, difr, ierr, perm, mlvl, sqre;
33 extern logical lsame_(char *, char *);
34 extern /* Subroutine */ int dlasr_(char *, char *, char *, integer *,
35 integer *, doublereal *, doublereal *, doublereal *, integer *), dcopy_(integer *, doublereal *, integer *
36 , doublereal *, integer *), dswap_(integer *, doublereal *,
37 integer *, doublereal *, integer *);
38 integer poles, iuplo, nsize, start;
39 extern /* Subroutine */ int dlasd0_(integer *, integer *, doublereal *,
40 doublereal *, doublereal *, integer *, doublereal *, integer *,
41 integer *, integer *, doublereal *, integer *);
42 extern doublereal dlamch_(char *);
43 extern /* Subroutine */ int dlasda_(integer *, integer *, integer *,
44 integer *, doublereal *, doublereal *, doublereal *, integer *,
45 doublereal *, integer *, doublereal *, doublereal *, doublereal *,
46 doublereal *, integer *, integer *, integer *, integer *,
47 doublereal *, doublereal *, doublereal *, doublereal *, integer *,
48 integer *), dlascl_(char *, integer *, integer *, doublereal *,
49 doublereal *, integer *, integer *, doublereal *, integer *,
50 integer *), dlasdq_(char *, integer *, integer *, integer
51 *, integer *, integer *, doublereal *, doublereal *, doublereal *,
52 integer *, doublereal *, integer *, doublereal *, integer *,
53 doublereal *, integer *), dlaset_(char *, integer *,
54 integer *, doublereal *, doublereal *, doublereal *, integer *), dlartg_(doublereal *, doublereal *, doublereal *,
55 doublereal *, doublereal *);
56 extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
57 integer *, integer *);
58 extern /* Subroutine */ int xerbla_(char *, integer *);
60 extern doublereal dlanst_(char *, integer *, doublereal *, doublereal *);
63 integer givnum, givptr, qstart, smlsiz, wstart, smlszp;
66 /* -- LAPACK routine (version 3.1) -- */
67 /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
70 /* .. Scalar Arguments .. */
72 /* .. Array Arguments .. */
78 /* DBDSDC computes the singular value decomposition (SVD) of a real */
79 /* N-by-N (upper or lower) bidiagonal matrix B: B = U * S * VT, */
80 /* using a divide and conquer method, where S is a diagonal matrix */
81 /* with non-negative diagonal elements (the singular values of B), and */
82 /* U and VT are orthogonal matrices of left and right singular vectors, */
83 /* respectively. DBDSDC can be used to compute all singular values, */
84 /* and optionally, singular vectors or singular vectors in compact form. */
86 /* This code makes very mild assumptions about floating point */
87 /* arithmetic. It will work on machines with a guard digit in */
88 /* add/subtract, or on those binary machines without guard digits */
89 /* which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or Cray-2. */
90 /* It could conceivably fail on hexadecimal or decimal machines */
91 /* without guard digits, but we know of none. See DLASD3 for details. */
93 /* The code currently calls DLASDQ if singular values only are desired. */
94 /* However, it can be slightly modified to compute singular values */
95 /* using the divide and conquer method. */
100 /* UPLO (input) CHARACTER*1 */
101 /* = 'U': B is upper bidiagonal. */
102 /* = 'L': B is lower bidiagonal. */
104 /* COMPQ (input) CHARACTER*1 */
105 /* Specifies whether singular vectors are to be computed */
107 /* = 'N': Compute singular values only; */
108 /* = 'P': Compute singular values and compute singular */
109 /* vectors in compact form; */
110 /* = 'I': Compute singular values and singular vectors. */
112 /* N (input) INTEGER */
113 /* The order of the matrix B. N >= 0. */
115 /* D (input/output) DOUBLE PRECISION array, dimension (N) */
116 /* On entry, the n diagonal elements of the bidiagonal matrix B. */
117 /* On exit, if INFO=0, the singular values of B. */
119 /* E (input/output) DOUBLE PRECISION array, dimension (N-1) */
120 /* On entry, the elements of E contain the offdiagonal */
121 /* elements of the bidiagonal matrix whose SVD is desired. */
122 /* On exit, E has been destroyed. */
124 /* U (output) DOUBLE PRECISION array, dimension (LDU,N) */
125 /* If COMPQ = 'I', then: */
126 /* On exit, if INFO = 0, U contains the left singular vectors */
127 /* of the bidiagonal matrix. */
128 /* For other values of COMPQ, U is not referenced. */
130 /* LDU (input) INTEGER */
131 /* The leading dimension of the array U. LDU >= 1. */
132 /* If singular vectors are desired, then LDU >= max( 1, N ). */
134 /* VT (output) DOUBLE PRECISION array, dimension (LDVT,N) */
135 /* If COMPQ = 'I', then: */
136 /* On exit, if INFO = 0, VT' contains the right singular */
137 /* vectors of the bidiagonal matrix. */
138 /* For other values of COMPQ, VT is not referenced. */
140 /* LDVT (input) INTEGER */
141 /* The leading dimension of the array VT. LDVT >= 1. */
142 /* If singular vectors are desired, then LDVT >= max( 1, N ). */
144 /* Q (output) DOUBLE PRECISION array, dimension (LDQ) */
145 /* If COMPQ = 'P', then: */
146 /* On exit, if INFO = 0, Q and IQ contain the left */
147 /* and right singular vectors in a compact form, */
148 /* requiring O(N log N) space instead of 2*N**2. */
149 /* In particular, Q contains all the DOUBLE PRECISION data in */
150 /* LDQ >= N*(11 + 2*SMLSIZ + 8*INT(LOG_2(N/(SMLSIZ+1)))) */
151 /* words of memory, where SMLSIZ is returned by ILAENV and */
152 /* is equal to the maximum size of the subproblems at the */
153 /* bottom of the computation tree (usually about 25). */
154 /* For other values of COMPQ, Q is not referenced. */
156 /* IQ (output) INTEGER array, dimension (LDIQ) */
157 /* If COMPQ = 'P', then: */
158 /* On exit, if INFO = 0, Q and IQ contain the left */
159 /* and right singular vectors in a compact form, */
160 /* requiring O(N log N) space instead of 2*N**2. */
161 /* In particular, IQ contains all INTEGER data in */
162 /* LDIQ >= N*(3 + 3*INT(LOG_2(N/(SMLSIZ+1)))) */
163 /* words of memory, where SMLSIZ is returned by ILAENV and */
164 /* is equal to the maximum size of the subproblems at the */
165 /* bottom of the computation tree (usually about 25). */
166 /* For other values of COMPQ, IQ is not referenced. */
168 /* WORK (workspace) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
169 /* If COMPQ = 'N' then LWORK >= (4 * N). */
170 /* If COMPQ = 'P' then LWORK >= (6 * N). */
171 /* If COMPQ = 'I' then LWORK >= (3 * N**2 + 4 * N). */
173 /* IWORK (workspace) INTEGER array, dimension (8*N) */
175 /* INFO (output) INTEGER */
176 /* = 0: successful exit. */
177 /* < 0: if INFO = -i, the i-th argument had an illegal value. */
178 /* > 0: The algorithm failed to compute an singular value. */
179 /* The update process of divide and conquer failed. */
181 /* Further Details */
182 /* =============== */
184 /* Based on contributions by */
185 /* Ming Gu and Huan Ren, Computer Science Division, University of */
186 /* California at Berkeley, USA */
188 /* ===================================================================== */
189 /* Changed dimension statement in comment describing E from (N) to */
190 /* (N-1). Sven, 17 Feb 05. */
191 /* ===================================================================== */
193 /* .. Parameters .. */
195 /* .. Local Scalars .. */
197 /* .. External Functions .. */
199 /* .. External Subroutines .. */
201 /* .. Intrinsic Functions .. */
203 /* .. Executable Statements .. */
205 /* Test the input parameters. */
207 /* Parameter adjustments */
211 u_offset = 1 + u_dim1;
214 vt_offset = 1 + vt_dim1;
225 if (lsame_(uplo, "U")) {
228 if (lsame_(uplo, "L")) {
231 if (lsame_(compq, "N")) {
233 } else if (lsame_(compq, "P")) {
235 } else if (lsame_(compq, "I")) {
242 } else if (icompq < 0) {
246 } else if (*ldu < 1 || icompq == 2 && *ldu < *n) {
248 } else if (*ldvt < 1 || icompq == 2 && *ldvt < *n) {
253 xerbla_("DBDSDC", &i__1);
257 /* Quick return if possible */
262 smlsiz = ilaenv_(&c__9, "DBDSDC", " ", &c__0, &c__0, &c__0, &c__0);
265 q[1] = d_sign(&c_b15, &d__[1]);
266 q[smlsiz * *n + 1] = 1.;
267 } else if (icompq == 2) {
268 u[u_dim1 + 1] = d_sign(&c_b15, &d__[1]);
269 vt[vt_dim1 + 1] = 1.;
271 d__[1] = abs(d__[1]);
276 /* If matrix lower bidiagonal, rotate to be upper bidiagonal */
277 /* by applying Givens rotations on the left */
282 dcopy_(n, &d__[1], &c__1, &q[1], &c__1);
284 dcopy_(&i__1, &e[1], &c__1, &q[*n + 1], &c__1);
288 wstart = (*n << 1) - 1;
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 q[i__ + (*n << 1)] = cs;
297 q[i__ + *n * 3] = sn;
298 } else if (icompq == 2) {
300 work[nm1 + i__] = -sn;
306 /* If ICOMPQ = 0, use DLASDQ to compute the singular values. */
309 dlasdq_("U", &c__0, n, &c__0, &c__0, &c__0, &d__[1], &e[1], &vt[
310 vt_offset], ldvt, &u[u_offset], ldu, &u[u_offset], ldu, &work[
315 /* If N is smaller than the minimum divide size SMLSIZ, then solve */
316 /* the problem with another solver. */
320 dlaset_("A", n, n, &c_b29, &c_b15, &u[u_offset], ldu);
321 dlaset_("A", n, n, &c_b29, &c_b15, &vt[vt_offset], ldvt);
322 dlasdq_("U", &c__0, n, n, n, &c__0, &d__[1], &e[1], &vt[vt_offset]
323 , ldvt, &u[u_offset], ldu, &u[u_offset], ldu, &work[
325 } else if (icompq == 1) {
328 dlaset_("A", n, n, &c_b29, &c_b15, &q[iu + (qstart - 1) * *n], n);
329 dlaset_("A", n, n, &c_b29, &c_b15, &q[ivt + (qstart - 1) * *n], n);
330 dlasdq_("U", &c__0, n, n, n, &c__0, &d__[1], &e[1], &q[ivt + (
331 qstart - 1) * *n], n, &q[iu + (qstart - 1) * *n], n, &q[
332 iu + (qstart - 1) * *n], n, &work[wstart], info);
338 dlaset_("A", n, n, &c_b29, &c_b15, &u[u_offset], ldu);
339 dlaset_("A", n, n, &c_b29, &c_b15, &vt[vt_offset], ldvt);
344 orgnrm = dlanst_("M", n, &d__[1], &e[1]);
348 dlascl_("G", &c__0, &c__0, &orgnrm, &c_b15, n, &c__1, &d__[1], n, &ierr);
349 dlascl_("G", &c__0, &c__0, &orgnrm, &c_b15, &nm1, &c__1, &e[1], &nm1, &
352 eps = dlamch_("Epsilon");
354 mlvl = (integer) (log((doublereal) (*n) / (doublereal) (smlsiz + 1)) /
363 z__ = difr + (mlvl << 1);
367 givnum = poles + (mlvl << 1);
372 givcol = perm + mlvl;
376 for (i__ = 1; i__ <= i__1; ++i__) {
377 if ((d__1 = d__[i__], abs(d__1)) < eps) {
378 d__[i__] = d_sign(&eps, &d__[i__]);
387 for (i__ = 1; i__ <= i__1; ++i__) {
388 if ((d__1 = e[i__], abs(d__1)) < eps || i__ == nm1) {
390 /* Subproblem found. First determine its size and then */
391 /* apply divide and conquer on it. */
395 /* A subproblem with E(I) small for I < NM1. */
397 nsize = i__ - start + 1;
398 } else if ((d__1 = e[i__], abs(d__1)) >= eps) {
400 /* A subproblem with E(NM1) not too small but I = NM1. */
402 nsize = *n - start + 1;
405 /* A subproblem with E(NM1) small. This implies an */
406 /* 1-by-1 subproblem at D(N). Solve this 1-by-1 problem */
409 nsize = i__ - start + 1;
411 u[*n + *n * u_dim1] = d_sign(&c_b15, &d__[*n]);
412 vt[*n + *n * vt_dim1] = 1.;
413 } else if (icompq == 1) {
414 q[*n + (qstart - 1) * *n] = d_sign(&c_b15, &d__[*n]);
415 q[*n + (smlsiz + qstart - 1) * *n] = 1.;
417 d__[*n] = (d__1 = d__[*n], abs(d__1));
420 dlasd0_(&nsize, &sqre, &d__[start], &e[start], &u[start +
421 start * u_dim1], ldu, &vt[start + start * vt_dim1],
422 ldvt, &smlsiz, &iwork[1], &work[wstart], info);
424 dlasda_(&icompq, &smlsiz, &nsize, &sqre, &d__[start], &e[
425 start], &q[start + (iu + qstart - 2) * *n], n, &q[
426 start + (ivt + qstart - 2) * *n], &iq[start + k * *n],
427 &q[start + (difl + qstart - 2) * *n], &q[start + (
428 difr + qstart - 2) * *n], &q[start + (z__ + qstart -
429 2) * *n], &q[start + (poles + qstart - 2) * *n], &iq[
430 start + givptr * *n], &iq[start + givcol * *n], n, &
431 iq[start + perm * *n], &q[start + (givnum + qstart -
432 2) * *n], &q[start + (ic + qstart - 2) * *n], &q[
433 start + (is + qstart - 2) * *n], &work[wstart], &
446 dlascl_("G", &c__0, &c__0, &c_b15, &orgnrm, n, &c__1, &d__[1], n, &ierr);
449 /* Use Selection Sort to minimize swaps of singular vectors */
452 for (ii = 2; ii <= i__1; ++ii) {
457 for (j = ii; j <= i__2; ++j) {
469 } else if (icompq == 2) {
470 dswap_(n, &u[i__ * u_dim1 + 1], &c__1, &u[kk * u_dim1 + 1], &
472 dswap_(n, &vt[i__ + vt_dim1], ldvt, &vt[kk + vt_dim1], ldvt);
474 } else if (icompq == 1) {
480 /* If ICOMPQ = 1, use IQ(N,1) as the indicator for UPLO */
490 /* If B is lower bidiagonal, update U by those Givens rotations */
491 /* which rotated B to be upper bidiagonal */
493 if (iuplo == 2 && icompq == 2) {
494 dlasr_("L", "V", "B", n, n, &work[1], &work[*n], &u[u_offset], ldu);