3 /* Table of constant values */
5 static integer c__1 = 1;
7 /* Subroutine */ int slasd7_(integer *icompq, integer *nl, integer *nr,
8 integer *sqre, integer *k, real *d__, real *z__, real *zw, real *vf,
9 real *vfw, real *vl, real *vlw, real *alpha, real *beta, real *dsigma,
10 integer *idx, integer *idxp, integer *idxq, integer *perm, integer *
11 givptr, integer *givcol, integer *ldgcol, real *givnum, integer *
12 ldgnum, real *c__, real *s, integer *info)
14 /* System generated locals */
15 integer givcol_dim1, givcol_offset, givnum_dim1, givnum_offset, i__1;
19 integer i__, j, m, n, k2;
23 integer nlp1, nlp2, idxi, idxj;
24 extern /* Subroutine */ int srot_(integer *, real *, integer *, real *,
25 integer *, real *, real *);
27 extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *,
29 extern doublereal slapy2_(real *, real *), slamch_(char *);
30 extern /* Subroutine */ int xerbla_(char *, integer *), slamrg_(
31 integer *, integer *, real *, integer *, integer *, integer *);
35 /* -- LAPACK auxiliary routine (version 3.1) -- */
36 /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
39 /* .. Scalar Arguments .. */
41 /* .. Array Arguments .. */
47 /* SLASD7 merges the two sets of singular values together into a single */
48 /* sorted set. Then it tries to deflate the size of the problem. There */
49 /* are two ways in which deflation can occur: when two or more singular */
50 /* values are close together or if there is a tiny entry in the Z */
51 /* vector. For each such occurrence the order of the related */
52 /* secular equation problem is reduced by one. */
54 /* SLASD7 is called from SLASD6. */
59 /* ICOMPQ (input) INTEGER */
60 /* Specifies whether singular vectors are to be computed */
61 /* in compact form, as follows: */
62 /* = 0: Compute singular values only. */
63 /* = 1: Compute singular vectors of upper */
64 /* bidiagonal matrix in compact form. */
66 /* NL (input) INTEGER */
67 /* The row dimension of the upper block. NL >= 1. */
69 /* NR (input) INTEGER */
70 /* The row dimension of the lower block. NR >= 1. */
72 /* SQRE (input) INTEGER */
73 /* = 0: the lower block is an NR-by-NR square matrix. */
74 /* = 1: the lower block is an NR-by-(NR+1) rectangular matrix. */
76 /* The bidiagonal matrix has */
77 /* N = NL + NR + 1 rows and */
78 /* M = N + SQRE >= N columns. */
80 /* K (output) INTEGER */
81 /* Contains the dimension of the non-deflated matrix, this is */
82 /* the order of the related secular equation. 1 <= K <=N. */
84 /* D (input/output) REAL array, dimension ( N ) */
85 /* On entry D contains the singular values of the two submatrices */
86 /* to be combined. On exit D contains the trailing (N-K) updated */
87 /* singular values (those which were deflated) sorted into */
88 /* increasing order. */
90 /* Z (output) REAL array, dimension ( M ) */
91 /* On exit Z contains the updating row vector in the secular */
94 /* ZW (workspace) REAL array, dimension ( M ) */
95 /* Workspace for Z. */
97 /* VF (input/output) REAL array, dimension ( M ) */
98 /* On entry, VF(1:NL+1) contains the first components of all */
99 /* right singular vectors of the upper block; and VF(NL+2:M) */
100 /* contains the first components of all right singular vectors */
101 /* of the lower block. On exit, VF contains the first components */
102 /* of all right singular vectors of the bidiagonal matrix. */
104 /* VFW (workspace) REAL array, dimension ( M ) */
105 /* Workspace for VF. */
107 /* VL (input/output) REAL array, dimension ( M ) */
108 /* On entry, VL(1:NL+1) contains the last components of all */
109 /* right singular vectors of the upper block; and VL(NL+2:M) */
110 /* contains the last components of all right singular vectors */
111 /* of the lower block. On exit, VL contains the last components */
112 /* of all right singular vectors of the bidiagonal matrix. */
114 /* VLW (workspace) REAL array, dimension ( M ) */
115 /* Workspace for VL. */
117 /* ALPHA (input) REAL */
118 /* Contains the diagonal element associated with the added row. */
120 /* BETA (input) REAL */
121 /* Contains the off-diagonal element associated with the added */
124 /* DSIGMA (output) REAL array, dimension ( N ) */
125 /* Contains a copy of the diagonal elements (K-1 singular values */
126 /* and one zero) in the secular equation. */
128 /* IDX (workspace) INTEGER array, dimension ( N ) */
129 /* This will contain the permutation used to sort the contents of */
130 /* D into ascending order. */
132 /* IDXP (workspace) INTEGER array, dimension ( N ) */
133 /* This will contain the permutation used to place deflated */
134 /* values of D at the end of the array. On output IDXP(2:K) */
135 /* points to the nondeflated D-values and IDXP(K+1:N) */
136 /* points to the deflated singular values. */
138 /* IDXQ (input) INTEGER array, dimension ( N ) */
139 /* This contains the permutation which separately sorts the two */
140 /* sub-problems in D into ascending order. Note that entries in */
141 /* the first half of this permutation must first be moved one */
142 /* position backward; and entries in the second half */
143 /* must first have NL+1 added to their values. */
145 /* PERM (output) INTEGER array, dimension ( N ) */
146 /* The permutations (from deflation and sorting) to be applied */
147 /* to each singular block. Not referenced if ICOMPQ = 0. */
149 /* GIVPTR (output) INTEGER */
150 /* The number of Givens rotations which took place in this */
151 /* subproblem. Not referenced if ICOMPQ = 0. */
153 /* GIVCOL (output) INTEGER array, dimension ( LDGCOL, 2 ) */
154 /* Each pair of numbers indicates a pair of columns to take place */
155 /* in a Givens rotation. Not referenced if ICOMPQ = 0. */
157 /* LDGCOL (input) INTEGER */
158 /* The leading dimension of GIVCOL, must be at least N. */
160 /* GIVNUM (output) REAL array, dimension ( LDGNUM, 2 ) */
161 /* Each number indicates the C or S value to be used in the */
162 /* corresponding Givens rotation. Not referenced if ICOMPQ = 0. */
164 /* LDGNUM (input) INTEGER */
165 /* The leading dimension of GIVNUM, must be at least N. */
167 /* C (output) REAL */
168 /* C contains garbage if SQRE =0 and the C-value of a Givens */
169 /* rotation related to the right null space if SQRE = 1. */
171 /* S (output) REAL */
172 /* S contains garbage if SQRE =0 and the S-value of a Givens */
173 /* rotation related to the right null space if SQRE = 1. */
175 /* INFO (output) INTEGER */
176 /* = 0: successful exit. */
177 /* < 0: if INFO = -i, the i-th argument had an illegal value. */
179 /* Further Details */
180 /* =============== */
182 /* Based on contributions by */
183 /* Ming Gu and Huan Ren, Computer Science Division, University of */
184 /* California at Berkeley, USA */
186 /* ===================================================================== */
188 /* .. Parameters .. */
190 /* .. Local Scalars .. */
193 /* .. External Subroutines .. */
195 /* .. External Functions .. */
197 /* .. Intrinsic Functions .. */
199 /* .. Executable Statements .. */
201 /* Test the input parameters. */
203 /* Parameter adjustments */
216 givcol_dim1 = *ldgcol;
217 givcol_offset = 1 + givcol_dim1;
218 givcol -= givcol_offset;
219 givnum_dim1 = *ldgnum;
220 givnum_offset = 1 + givnum_dim1;
221 givnum -= givnum_offset;
228 if (*icompq < 0 || *icompq > 1) {
230 } else if (*nl < 1) {
232 } else if (*nr < 1) {
234 } else if (*sqre < 0 || *sqre > 1) {
236 } else if (*ldgcol < n) {
238 } else if (*ldgnum < n) {
243 xerbla_("SLASD7", &i__1);
253 /* Generate the first part of the vector Z and move the singular */
254 /* values in the first part of D one position backward. */
256 z1 = *alpha * vl[nlp1];
259 for (i__ = *nl; i__ >= 1; --i__) {
260 z__[i__ + 1] = *alpha * vl[i__];
262 vf[i__ + 1] = vf[i__];
263 d__[i__ + 1] = d__[i__];
264 idxq[i__ + 1] = idxq[i__] + 1;
269 /* Generate the second part of the vector Z. */
272 for (i__ = nlp2; i__ <= i__1; ++i__) {
273 z__[i__] = *beta * vf[i__];
278 /* Sort the singular values into increasing order */
281 for (i__ = nlp2; i__ <= i__1; ++i__) {
286 /* DSIGMA, IDXC, IDXC, and ZW are used as storage space. */
289 for (i__ = 2; i__ <= i__1; ++i__) {
290 dsigma[i__] = d__[idxq[i__]];
291 zw[i__] = z__[idxq[i__]];
292 vfw[i__] = vf[idxq[i__]];
293 vlw[i__] = vl[idxq[i__]];
297 slamrg_(nl, nr, &dsigma[2], &c__1, &c__1, &idx[2]);
300 for (i__ = 2; i__ <= i__1; ++i__) {
302 d__[i__] = dsigma[idxi];
309 /* Calculate the allowable deflation tolerence */
311 eps = slamch_("Epsilon");
313 r__1 = dabs(*alpha), r__2 = dabs(*beta);
314 tol = dmax(r__1,r__2);
316 r__2 = (r__1 = d__[n], dabs(r__1));
317 tol = eps * 64.f * dmax(r__2,tol);
319 /* There are 2 kinds of deflation -- first a value in the z-vector */
320 /* is small, second two (or more) singular values are very close */
321 /* together (their difference is small). */
323 /* If the value in the z-vector is small, we simply permute the */
324 /* array so that the corresponding singular value is moved to the */
327 /* If two values in the D-vector are close, we perform a two-sided */
328 /* rotation designed to make one of the corresponding z-vector */
329 /* entries zero, and then permute the array so that the deflated */
330 /* singular value is moved to the end. */
332 /* If there are multiple singular values then the problem deflates. */
333 /* Here the number of equal singular values are found. As each equal */
334 /* singular value is found, an elementary reflector is computed to */
335 /* rotate the corresponding singular subspace so that the */
336 /* corresponding components of Z are zero in this new basis. */
341 for (j = 2; j <= i__1; ++j) {
342 if ((r__1 = z__[j], dabs(r__1)) <= tol) {
344 /* Deflate due to small z component. */
364 if ((r__1 = z__[j], dabs(r__1)) <= tol) {
366 /* Deflate due to small z component. */
372 /* Check if singular values are close enough to allow deflation. */
374 if ((r__1 = d__[j] - d__[jprev], dabs(r__1)) <= tol) {
376 /* Deflation is possible. */
381 /* Find sqrt(a**2+b**2) without overflow or */
382 /* destructive underflow. */
384 tau = slapy2_(c__, s);
390 /* Record the appropriate Givens rotation */
394 idxjp = idxq[idx[jprev] + 1];
395 idxj = idxq[idx[j] + 1];
402 givcol[*givptr + (givcol_dim1 << 1)] = idxjp;
403 givcol[*givptr + givcol_dim1] = idxj;
404 givnum[*givptr + (givnum_dim1 << 1)] = *c__;
405 givnum[*givptr + givnum_dim1] = *s;
407 srot_(&c__1, &vf[jprev], &c__1, &vf[j], &c__1, c__, s);
408 srot_(&c__1, &vl[jprev], &c__1, &vl[j], &c__1, c__, s);
415 dsigma[*k] = d__[jprev];
423 /* Record the last singular value. */
427 dsigma[*k] = d__[jprev];
432 /* Sort the singular values into DSIGMA. The singular values which */
433 /* were not deflated go into the first K slots of DSIGMA, except */
434 /* that DSIGMA(1) is treated separately. */
437 for (j = 2; j <= i__1; ++j) {
446 for (j = 2; j <= i__1; ++j) {
448 perm[j] = idxq[idx[jp] + 1];
449 if (perm[j] <= nlp1) {
456 /* The deflated singular values go back into the last N - K slots of */
460 scopy_(&i__1, &dsigma[*k + 1], &c__1, &d__[*k + 1], &c__1);
462 /* Determine DSIGMA(1), DSIGMA(2), Z(1), VF(1), VL(1), VF(M), and */
467 if (dabs(dsigma[2]) <= hlftol) {
471 z__[1] = slapy2_(&z1, &z__[m]);
478 *s = -z__[m] / z__[1];
480 srot_(&c__1, &vf[m], &c__1, &vf[1], &c__1, c__, s);
481 srot_(&c__1, &vl[m], &c__1, &vl[1], &c__1, c__, s);
483 if (dabs(z1) <= tol) {
490 /* Restore Z, VF, and VL. */
493 scopy_(&i__1, &zw[2], &c__1, &z__[2], &c__1);
495 scopy_(&i__1, &vfw[2], &c__1, &vf[2], &c__1);
497 scopy_(&i__1, &vlw[2], &c__1, &vl[2], &c__1);