3 /* Table of constant values */
5 static integer c__1 = 1;
7 /* Subroutine */ int dlasd7_(integer *icompq, integer *nl, integer *nr,
8 integer *sqre, integer *k, doublereal *d__, doublereal *z__,
9 doublereal *zw, doublereal *vf, doublereal *vfw, doublereal *vl,
10 doublereal *vlw, doublereal *alpha, doublereal *beta, doublereal *
11 dsigma, integer *idx, integer *idxp, integer *idxq, integer *perm,
12 integer *givptr, integer *givcol, integer *ldgcol, doublereal *givnum,
13 integer *ldgnum, doublereal *c__, doublereal *s, integer *info)
15 /* System generated locals */
16 integer givcol_dim1, givcol_offset, givnum_dim1, givnum_offset, i__1;
17 doublereal d__1, d__2;
20 integer i__, j, m, n, k2;
23 doublereal eps, tau, tol;
24 integer nlp1, nlp2, idxi, idxj;
25 extern /* Subroutine */ int drot_(integer *, doublereal *, integer *,
26 doublereal *, integer *, doublereal *, doublereal *);
28 extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *,
29 doublereal *, integer *);
31 extern doublereal dlapy2_(doublereal *, doublereal *), dlamch_(char *);
32 extern /* Subroutine */ int dlamrg_(integer *, integer *, doublereal *,
33 integer *, integer *, integer *), xerbla_(char *, integer *);
37 /* -- LAPACK auxiliary routine (version 3.1) -- */
38 /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
41 /* .. Scalar Arguments .. */
43 /* .. Array Arguments .. */
49 /* DLASD7 merges the two sets of singular values together into a single */
50 /* sorted set. Then it tries to deflate the size of the problem. There */
51 /* are two ways in which deflation can occur: when two or more singular */
52 /* values are close together or if there is a tiny entry in the Z */
53 /* vector. For each such occurrence the order of the related */
54 /* secular equation problem is reduced by one. */
56 /* DLASD7 is called from DLASD6. */
61 /* ICOMPQ (input) INTEGER */
62 /* Specifies whether singular vectors are to be computed */
63 /* in compact form, as follows: */
64 /* = 0: Compute singular values only. */
65 /* = 1: Compute singular vectors of upper */
66 /* bidiagonal matrix in compact form. */
68 /* NL (input) INTEGER */
69 /* The row dimension of the upper block. NL >= 1. */
71 /* NR (input) INTEGER */
72 /* The row dimension of the lower block. NR >= 1. */
74 /* SQRE (input) INTEGER */
75 /* = 0: the lower block is an NR-by-NR square matrix. */
76 /* = 1: the lower block is an NR-by-(NR+1) rectangular matrix. */
78 /* The bidiagonal matrix has */
79 /* N = NL + NR + 1 rows and */
80 /* M = N + SQRE >= N columns. */
82 /* K (output) INTEGER */
83 /* Contains the dimension of the non-deflated matrix, this is */
84 /* the order of the related secular equation. 1 <= K <=N. */
86 /* D (input/output) DOUBLE PRECISION array, dimension ( N ) */
87 /* On entry D contains the singular values of the two submatrices */
88 /* to be combined. On exit D contains the trailing (N-K) updated */
89 /* singular values (those which were deflated) sorted into */
90 /* increasing order. */
92 /* Z (output) DOUBLE PRECISION array, dimension ( M ) */
93 /* On exit Z contains the updating row vector in the secular */
96 /* ZW (workspace) DOUBLE PRECISION array, dimension ( M ) */
97 /* Workspace for Z. */
99 /* VF (input/output) DOUBLE PRECISION array, dimension ( M ) */
100 /* On entry, VF(1:NL+1) contains the first components of all */
101 /* right singular vectors of the upper block; and VF(NL+2:M) */
102 /* contains the first components of all right singular vectors */
103 /* of the lower block. On exit, VF contains the first components */
104 /* of all right singular vectors of the bidiagonal matrix. */
106 /* VFW (workspace) DOUBLE PRECISION array, dimension ( M ) */
107 /* Workspace for VF. */
109 /* VL (input/output) DOUBLE PRECISION array, dimension ( M ) */
110 /* On entry, VL(1:NL+1) contains the last components of all */
111 /* right singular vectors of the upper block; and VL(NL+2:M) */
112 /* contains the last components of all right singular vectors */
113 /* of the lower block. On exit, VL contains the last components */
114 /* of all right singular vectors of the bidiagonal matrix. */
116 /* VLW (workspace) DOUBLE PRECISION array, dimension ( M ) */
117 /* Workspace for VL. */
119 /* ALPHA (input) DOUBLE PRECISION */
120 /* Contains the diagonal element associated with the added row. */
122 /* BETA (input) DOUBLE PRECISION */
123 /* Contains the off-diagonal element associated with the added */
126 /* DSIGMA (output) DOUBLE PRECISION array, dimension ( N ) */
127 /* Contains a copy of the diagonal elements (K-1 singular values */
128 /* and one zero) in the secular equation. */
130 /* IDX (workspace) INTEGER array, dimension ( N ) */
131 /* This will contain the permutation used to sort the contents of */
132 /* D into ascending order. */
134 /* IDXP (workspace) INTEGER array, dimension ( N ) */
135 /* This will contain the permutation used to place deflated */
136 /* values of D at the end of the array. On output IDXP(2:K) */
137 /* points to the nondeflated D-values and IDXP(K+1:N) */
138 /* points to the deflated singular values. */
140 /* IDXQ (input) INTEGER array, dimension ( N ) */
141 /* This contains the permutation which separately sorts the two */
142 /* sub-problems in D into ascending order. Note that entries in */
143 /* the first half of this permutation must first be moved one */
144 /* position backward; and entries in the second half */
145 /* must first have NL+1 added to their values. */
147 /* PERM (output) INTEGER array, dimension ( N ) */
148 /* The permutations (from deflation and sorting) to be applied */
149 /* to each singular block. Not referenced if ICOMPQ = 0. */
151 /* GIVPTR (output) INTEGER */
152 /* The number of Givens rotations which took place in this */
153 /* subproblem. Not referenced if ICOMPQ = 0. */
155 /* GIVCOL (output) INTEGER array, dimension ( LDGCOL, 2 ) */
156 /* Each pair of numbers indicates a pair of columns to take place */
157 /* in a Givens rotation. Not referenced if ICOMPQ = 0. */
159 /* LDGCOL (input) INTEGER */
160 /* The leading dimension of GIVCOL, must be at least N. */
162 /* GIVNUM (output) DOUBLE PRECISION array, dimension ( LDGNUM, 2 ) */
163 /* Each number indicates the C or S value to be used in the */
164 /* corresponding Givens rotation. Not referenced if ICOMPQ = 0. */
166 /* LDGNUM (input) INTEGER */
167 /* The leading dimension of GIVNUM, must be at least N. */
169 /* C (output) DOUBLE PRECISION */
170 /* C contains garbage if SQRE =0 and the C-value of a Givens */
171 /* rotation related to the right null space if SQRE = 1. */
173 /* S (output) DOUBLE PRECISION */
174 /* S contains garbage if SQRE =0 and the S-value of a Givens */
175 /* rotation related to the right null space if SQRE = 1. */
177 /* INFO (output) INTEGER */
178 /* = 0: successful exit. */
179 /* < 0: if INFO = -i, the i-th argument had an illegal value. */
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 /* ===================================================================== */
190 /* .. Parameters .. */
192 /* .. Local Scalars .. */
195 /* .. External Subroutines .. */
197 /* .. External Functions .. */
199 /* .. Intrinsic Functions .. */
201 /* .. Executable Statements .. */
203 /* Test the input parameters. */
205 /* Parameter adjustments */
218 givcol_dim1 = *ldgcol;
219 givcol_offset = 1 + givcol_dim1;
220 givcol -= givcol_offset;
221 givnum_dim1 = *ldgnum;
222 givnum_offset = 1 + givnum_dim1;
223 givnum -= givnum_offset;
230 if (*icompq < 0 || *icompq > 1) {
232 } else if (*nl < 1) {
234 } else if (*nr < 1) {
236 } else if (*sqre < 0 || *sqre > 1) {
238 } else if (*ldgcol < n) {
240 } else if (*ldgnum < n) {
245 xerbla_("DLASD7", &i__1);
255 /* Generate the first part of the vector Z and move the singular */
256 /* values in the first part of D one position backward. */
258 z1 = *alpha * vl[nlp1];
261 for (i__ = *nl; i__ >= 1; --i__) {
262 z__[i__ + 1] = *alpha * vl[i__];
264 vf[i__ + 1] = vf[i__];
265 d__[i__ + 1] = d__[i__];
266 idxq[i__ + 1] = idxq[i__] + 1;
271 /* Generate the second part of the vector Z. */
274 for (i__ = nlp2; i__ <= i__1; ++i__) {
275 z__[i__] = *beta * vf[i__];
280 /* Sort the singular values into increasing order */
283 for (i__ = nlp2; i__ <= i__1; ++i__) {
288 /* DSIGMA, IDXC, IDXC, and ZW are used as storage space. */
291 for (i__ = 2; i__ <= i__1; ++i__) {
292 dsigma[i__] = d__[idxq[i__]];
293 zw[i__] = z__[idxq[i__]];
294 vfw[i__] = vf[idxq[i__]];
295 vlw[i__] = vl[idxq[i__]];
299 dlamrg_(nl, nr, &dsigma[2], &c__1, &c__1, &idx[2]);
302 for (i__ = 2; i__ <= i__1; ++i__) {
304 d__[i__] = dsigma[idxi];
311 /* Calculate the allowable deflation tolerence */
313 eps = dlamch_("Epsilon");
315 d__1 = abs(*alpha), d__2 = abs(*beta);
316 tol = max(d__1,d__2);
318 d__2 = (d__1 = d__[n], abs(d__1));
319 tol = eps * 64. * max(d__2,tol);
321 /* There are 2 kinds of deflation -- first a value in the z-vector */
322 /* is small, second two (or more) singular values are very close */
323 /* together (their difference is small). */
325 /* If the value in the z-vector is small, we simply permute the */
326 /* array so that the corresponding singular value is moved to the */
329 /* If two values in the D-vector are close, we perform a two-sided */
330 /* rotation designed to make one of the corresponding z-vector */
331 /* entries zero, and then permute the array so that the deflated */
332 /* singular value is moved to the end. */
334 /* If there are multiple singular values then the problem deflates. */
335 /* Here the number of equal singular values are found. As each equal */
336 /* singular value is found, an elementary reflector is computed to */
337 /* rotate the corresponding singular subspace so that the */
338 /* corresponding components of Z are zero in this new basis. */
343 for (j = 2; j <= i__1; ++j) {
344 if ((d__1 = z__[j], abs(d__1)) <= tol) {
346 /* Deflate due to small z component. */
366 if ((d__1 = z__[j], abs(d__1)) <= tol) {
368 /* Deflate due to small z component. */
374 /* Check if singular values are close enough to allow deflation. */
376 if ((d__1 = d__[j] - d__[jprev], abs(d__1)) <= tol) {
378 /* Deflation is possible. */
383 /* Find sqrt(a**2+b**2) without overflow or */
384 /* destructive underflow. */
386 tau = dlapy2_(c__, s);
392 /* Record the appropriate Givens rotation */
396 idxjp = idxq[idx[jprev] + 1];
397 idxj = idxq[idx[j] + 1];
404 givcol[*givptr + (givcol_dim1 << 1)] = idxjp;
405 givcol[*givptr + givcol_dim1] = idxj;
406 givnum[*givptr + (givnum_dim1 << 1)] = *c__;
407 givnum[*givptr + givnum_dim1] = *s;
409 drot_(&c__1, &vf[jprev], &c__1, &vf[j], &c__1, c__, s);
410 drot_(&c__1, &vl[jprev], &c__1, &vl[j], &c__1, c__, s);
417 dsigma[*k] = d__[jprev];
425 /* Record the last singular value. */
429 dsigma[*k] = d__[jprev];
434 /* Sort the singular values into DSIGMA. The singular values which */
435 /* were not deflated go into the first K slots of DSIGMA, except */
436 /* that DSIGMA(1) is treated separately. */
439 for (j = 2; j <= i__1; ++j) {
448 for (j = 2; j <= i__1; ++j) {
450 perm[j] = idxq[idx[jp] + 1];
451 if (perm[j] <= nlp1) {
458 /* The deflated singular values go back into the last N - K slots of */
462 dcopy_(&i__1, &dsigma[*k + 1], &c__1, &d__[*k + 1], &c__1);
464 /* Determine DSIGMA(1), DSIGMA(2), Z(1), VF(1), VL(1), VF(M), and */
469 if (abs(dsigma[2]) <= hlftol) {
473 z__[1] = dlapy2_(&z1, &z__[m]);
480 *s = -z__[m] / z__[1];
482 drot_(&c__1, &vf[m], &c__1, &vf[1], &c__1, c__, s);
483 drot_(&c__1, &vl[m], &c__1, &vl[1], &c__1, c__, s);
485 if (abs(z1) <= tol) {
492 /* Restore Z, VF, and VL. */
495 dcopy_(&i__1, &zw[2], &c__1, &z__[2], &c__1);
497 dcopy_(&i__1, &vfw[2], &c__1, &vf[2], &c__1);
499 dcopy_(&i__1, &vlw[2], &c__1, &vl[2], &c__1);