LAPACK  3.11.0
LAPACK: Linear Algebra PACKage
dlasd2_deflate.f
1 *> \brief \b DLASD2 merges the two sets of singular values together into a single sorted set. Used by sbdsdc.
2 *
3 * =========== DOCUMENTATION ===========
4 *
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
7 *
8 *> \htmlonly
9 *> Download DLASD2 + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dlasd2.f">
11 *> [TGZ]</a>
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dlasd2.f">
13 *> [ZIP]</a>
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dlasd2.f">
15 *> [TXT]</a>
16 *> \endhtmlonly
17 *
18 * Definition:
19 * ===========
20 *
21 * SUBROUTINE DLASD2( NL, NR, SQRE, K, D, Z, ALPHA, BETA, U, LDU, VT,
22 * LDVT, DSIGMA, U2, LDU2, VT2, LDVT2, IDXP, IDX,
23 * IDXC, IDXQ, COLTYP, INFO )
24 *
25 * .. Scalar Arguments ..
26 * INTEGER INFO, K, LDU, LDU2, LDVT, LDVT2, NL, NR, SQRE
27 * DOUBLE PRECISION ALPHA, BETA
28 * ..
29 * .. Array Arguments ..
30 * INTEGER COLTYP( * ), IDX( * ), IDXC( * ), IDXP( * ),
31 * $ IDXQ( * )
32 * DOUBLE PRECISION D( * ), DSIGMA( * ), U( LDU, * ),
33 * $ U2( LDU2, * ), VT( LDVT, * ), VT2( LDVT2, * ),
34 * $ Z( * )
35 * ..
36 *
37 *
38 *> \par Purpose:
39 * =============
40 *>
41 *> \verbatim
42 *>
43 *> DLASD2 merges the two sets of singular values together into a single
44 *> sorted set. Then it tries to deflate the size of the problem.
45 *> There are two ways in which deflation can occur: when two or more
46 *> singular values are close together or if there is a tiny entry in the
47 *> Z vector. For each such occurrence the order of the related secular
48 *> equation problem is reduced by one.
49 *>
50 *> DLASD2 is called from DLASD1.
51 *> \endverbatim
52 *
53 * Arguments:
54 * ==========
55 *
56 *> \param[in] NL
57 *> \verbatim
58 *> NL is INTEGER
59 *> The row dimension of the upper block. NL >= 1.
60 *> \endverbatim
61 *>
62 *> \param[in] NR
63 *> \verbatim
64 *> NR is INTEGER
65 *> The row dimension of the lower block. NR >= 1.
66 *> \endverbatim
67 *>
68 *> \param[in] SQRE
69 *> \verbatim
70 *> SQRE is INTEGER
71 *> = 0: the lower block is an NR-by-NR square matrix.
72 *> = 1: the lower block is an NR-by-(NR+1) rectangular matrix.
73 *>
74 *> The bidiagonal matrix has N = NL + NR + 1 rows and
75 *> M = N + SQRE >= N columns.
76 *> \endverbatim
77 *>
78 *> \param[out] K
79 *> \verbatim
80 *> K is INTEGER
81 *> Contains the dimension of the non-deflated matrix,
82 *> This is the order of the related secular equation. 1 <= K <=N.
83 *> \endverbatim
84 *>
85 *> \param[in,out] D
86 *> \verbatim
87 *> D is DOUBLE PRECISION array, dimension(N)
88 *> On entry D contains the singular values of the two submatrices
89 *> to be combined. On exit D contains the trailing (N-K) updated
90 *> singular values (those which were deflated) sorted into
91 *> increasing order.
92 *> \endverbatim
93 *>
94 *> \param[out] Z
95 *> \verbatim
96 *> Z is DOUBLE PRECISION array, dimension(N)
97 *> On exit Z contains the updating row vector in the secular
98 *> equation.
99 *> \endverbatim
100 *>
101 *> \param[in] ALPHA
102 *> \verbatim
103 *> ALPHA is DOUBLE PRECISION
104 *> Contains the diagonal element associated with the added row.
105 *> \endverbatim
106 *>
107 *> \param[in] BETA
108 *> \verbatim
109 *> BETA is DOUBLE PRECISION
110 *> Contains the off-diagonal element associated with the added
111 *> row.
112 *> \endverbatim
113 *>
114 *> \param[in,out] U
115 *> \verbatim
116 *> U is DOUBLE PRECISION array, dimension(LDU,N)
117 *> On entry U contains the left singular vectors of two
118 *> submatrices in the two square blocks with corners at (1,1),
119 *> (NL, NL), and (NL+2, NL+2), (N,N).
120 *> On exit U contains the trailing (N-K) updated left singular
121 *> vectors (those which were deflated) in its last N-K columns.
122 *> \endverbatim
123 *>
124 *> \param[in] LDU
125 *> \verbatim
126 *> LDU is INTEGER
127 *> The leading dimension of the array U. LDU >= N.
128 *> \endverbatim
129 *>
130 *> \param[in,out] VT
131 *> \verbatim
132 *> VT is DOUBLE PRECISION array, dimension(LDVT,M)
133 *> On entry VT**T contains the right singular vectors of two
134 *> submatrices in the two square blocks with corners at (1,1),
135 *> (NL+1, NL+1), and (NL+2, NL+2), (M,M).
136 *> On exit VT**T contains the trailing (N-K) updated right singular
137 *> vectors (those which were deflated) in its last N-K columns.
138 *> In case SQRE =1, the last row of VT spans the right null
139 *> space.
140 *> \endverbatim
141 *>
142 *> \param[in] LDVT
143 *> \verbatim
144 *> LDVT is INTEGER
145 *> The leading dimension of the array VT. LDVT >= M.
146 *> \endverbatim
147 *>
148 *> \param[out] DSIGMA
149 *> \verbatim
150 *> DSIGMA is DOUBLE PRECISION array, dimension (N)
151 *> Contains a copy of the diagonal elements (K-1 singular values
152 *> and one zero) in the secular equation.
153 *> \endverbatim
154 *>
155 *> \param[out] U2
156 *> \verbatim
157 *> U2 is DOUBLE PRECISION array, dimension(LDU2,N)
158 *> Contains a copy of the first K-1 left singular vectors which
159 *> will be used by DLASD3 in a matrix multiply (DGEMM) to solve
160 *> for the new left singular vectors. U2 is arranged into four
161 *> blocks. The first block contains a column with 1 at NL+1 and
162 *> zero everywhere else; the second block contains non-zero
163 *> entries only at and above NL; the third contains non-zero
164 *> entries only below NL+1; and the fourth is dense.
165 *> \endverbatim
166 *>
167 *> \param[in] LDU2
168 *> \verbatim
169 *> LDU2 is INTEGER
170 *> The leading dimension of the array U2. LDU2 >= N.
171 *> \endverbatim
172 *>
173 *> \param[out] VT2
174 *> \verbatim
175 *> VT2 is DOUBLE PRECISION array, dimension(LDVT2,N)
176 *> VT2**T contains a copy of the first K right singular vectors
177 *> which will be used by DLASD3 in a matrix multiply (DGEMM) to
178 *> solve for the new right singular vectors. VT2 is arranged into
179 *> three blocks. The first block contains a row that corresponds
180 *> to the special 0 diagonal element in SIGMA; the second block
181 *> contains non-zeros only at and before NL +1; the third block
182 *> contains non-zeros only at and after NL +2.
183 *> \endverbatim
184 *>
185 *> \param[in] LDVT2
186 *> \verbatim
187 *> LDVT2 is INTEGER
188 *> The leading dimension of the array VT2. LDVT2 >= M.
189 *> \endverbatim
190 *>
191 *> \param[out] IDXP
192 *> \verbatim
193 *> IDXP is INTEGER array, dimension(N)
194 *> This will contain the permutation used to place deflated
195 *> values of D at the end of the array. On output IDXP(2:K)
196 *> points to the nondeflated D-values and IDXP(K+1:N)
197 *> points to the deflated singular values.
198 *> \endverbatim
199 *>
200 *> \param[out] IDX
201 *> \verbatim
202 *> IDX is INTEGER array, dimension(N)
203 *> This will contain the permutation used to sort the contents of
204 *> D into ascending order.
205 *> \endverbatim
206 *>
207 *> \param[out] IDXC
208 *> \verbatim
209 *> IDXC is INTEGER array, dimension(N)
210 *> This will contain the permutation used to arrange the columns
211 *> of the deflated U matrix into three groups: the first group
212 *> contains non-zero entries only at and above NL, the second
213 *> contains non-zero entries only below NL+2, and the third is
214 *> dense.
215 *> \endverbatim
216 *>
217 *> \param[in,out] IDXQ
218 *> \verbatim
219 *> IDXQ is INTEGER array, dimension(N)
220 *> This contains the permutation which separately sorts the two
221 *> sub-problems in D into ascending order. Note that entries in
222 *> the first hlaf of this permutation must first be moved one
223 *> position backward; and entries in the second half
224 *> must first have NL+1 added to their values.
225 *> \endverbatim
226 *>
227 *> \param[out] COLTYP
228 *> \verbatim
229 *> COLTYP is INTEGER array, dimension(N)
230 *> As workspace, this will contain a label which will indicate
231 *> which of the following types a column in the U2 matrix or a
232 *> row in the VT2 matrix is:
233 *> 1 : non-zero in the upper half only
234 *> 2 : non-zero in the lower half only
235 *> 3 : dense
236 *> 4 : deflated
237 *>
238 *> On exit, it is an array of dimension 4, with COLTYP(I) being
239 *> the dimension of the I-th type columns.
240 *> \endverbatim
241 *>
242 *> \param[out] INFO
243 *> \verbatim
244 *> INFO is INTEGER
245 *> = 0: successful exit.
246 *> < 0: if INFO = -i, the i-th argument had an illegal value.
247 *> \endverbatim
248 *
249 * Authors:
250 * ========
251 *
252 *> \author Univ. of Tennessee
253 *> \author Univ. of California Berkeley
254 *> \author Univ. of Colorado Denver
255 *> \author NAG Ltd.
256 *
257 *> \ingroup dlasd2_deflate
258 *
259 *> \par Contributors:
260 * ==================
261 *>
262 *> Ming Gu and Huan Ren, Computer Science Division, University of
263 *> California at Berkeley, USA
264 *>
265 * =====================================================================
266  subroutine dlasd2( nl, nr, sqre, k, D, Z, alpha, beta, U, ldu, VT,
267  $ ldvt, dsigma, U2, ldu2, VT2, ldvt2, idxp, idx,
268  $ idxc, idxq, coltype, info )
269 
270  !! lapack auxiliary routine
271  !! lapack is a software package provided by univ. of tennessee,
272  !! univ. of california berkeley, univ. of colorado denver and nag ltd..
273 
274  !! scalar arguments
275  integer info, k, ldu, ldu2, ldvt, ldvt2, nl, nr, sqre
276  double precision alpha, beta
277 
278  !! array arguments
279  integer coltype( * ), idx( * ), idxc( * ), idxp( * ),
280  $ idxq( * )
281  double precision d( * ), dsigma( * ), u( ldu, * ),
282  $ u2( ldu2, * ), vt( ldvt, * ), vt2( ldvt2, * ),
283  $ z( * )
284 
285  !! =====================================================================
286  !! parameters
287  double precision zero, one, two, eight
288  parameter( zero = 0.0d+0, one = 1.0d+0, two = 2.0d+0,
289  $ eight = 8.0d+0 )
290 
291  !! local arrays
292  integer ctot( 4 ), psm( 4 )
293 
294  !! local scalars
295  integer ct, i, idxi, idxj, idxjp, j, jp, jprev, k2, m,
296  $ n, nlp1, nlp2
297  double precision c, eps, hlftol, s, tau, tol, z1
298 
299  !! external functions
300  double precision dlamch, dlapy2
301  external dlamch, dlapy2
302 
303  !! external subroutines
304  external dcopy, dlacpy, dlamrg, dlaset, drot, xerbla
305 
306  !! intrinsic functions
307  intrinsic abs, max
308 
309  !! =====================================================================
310  !! executable statements
311 
312  !! test the input parameters.
313  info = 0
314 
315  if (nl < 1) then
316  info = -1
317  else if (nr < 1) then
318  info = -2
319  else if (( sqre .ne. 1 ) .and. ( sqre .ne. 0 )) then
320  info = -3
321  end if
322 
323  n = nl + nr + 1
324  m = n + sqre
325 
326  if (ldu < n) then
327  info = -10
328  else if (ldvt < m) then
329  info = -12
330  else if (ldu2 < n) then
331  info = -15
332  else if (ldvt2 < m) then
333  info = -17
334  end if
335  if (info .ne. 0) then
336  call xerbla( 'dlasd2', -info )
337  return
338  end if
339 
340  nlp1 = nl + 1
341  nlp2 = nl + 2
342 
343  !! generate the first part of the vector Z; and move the singular
344  !! values in the first part of D one position backward.
345  z1 = alpha*vt( nl+1, nl+1 )
346  z( 1 ) = z1
347  do i = nl, 1, -1
348  z( i+1 ) = alpha*vt( i, nl+1 )
349  d( i+1 ) = d( i )
350  idxq( i+1 ) = idxq( i ) + 1
351  end do
352 
353  !! generate the second part of the vector Z.
354  do i = nl+2, m
355  z( i ) = beta*vt( i, nl+2 )
356  end do
357 
358  !! initialize some reference arrays.
359  do i = 2, nl+1
360  coltype( i ) = 1
361  end do
362  do i = nl+2, n
363  coltype( i ) = 2
364  end do
365 
366  do i = nl+2, n
367  idxq( i ) = idxq( i ) + nl+1
368  end do
369 
370  !! dsigma, idxc, idxc, and the first column of U2
371  !! are used as storage space.
372  do i = 2, n
373  dsigma( i ) = d( idxq( i ) )
374  u2( i, 1 ) = z( idxq( i ) )
375  idxc( i ) = coltype( idxq( i ) )
376  end do
377 
378  !! Get permutation idx that sorts singular values into increasing order,
379  !! merging the 2 sorted lists into 1 list.
380  call dlamrg( nl, nr, dsigma( 2 ), 1, 1, idx( 2 ) )
381 
382  do i = 2, n
383  idxi = 1 + idx( i ) !! +1
384  d( i ) = dsigma( idxi )
385  z( i ) = u2( idxi, 1 )
386  coltype( i ) = idxc( idxi )
387  end do
388 
389  !! calculate the allowable deflation tolerance
390  eps = dlamch( 'epsilon' )
391  tol = eight * eps * max( abs( d(n) ), abs( alpha ), abs( beta ) )
392 
393  !! There are 2 kinds of deflation -- first a value in the Z-vector
394  !! is small, second two (or more) singular values are very close
395  !! together (their difference is small).
396  !!
397  !! If the value in the Z-vector is small, we simply permute the
398  !! array so that the corresponding singular value is moved to the
399  !! end.
400  !!
401  !! If two values in the D-vector are close, we perform a two-sided
402  !! rotation designed to make one of the corresponding Z-vector
403  !! entries zero, and then permute the array so that the deflated
404  !! singular value is moved to the end.
405  !!
406  !! If there are multiple singular values then the problem deflates.
407  !! here the number of equal singular values are found. as each equal
408  !! singular value is found, an elementary reflector is computed to
409  !! rotate the corresponding singular subspace so that the
410  !! corresponding components of Z are zero in this new basis.
411  k = 1
412  k2 = n + 1
413  !do j = 2, n
414  ! if (abs( Z( j ) ) <= tol) then
415  ! !! deflate due to small Z component.
416  ! k2 = k2 - 1
417  ! idxp( k2 ) = j
418  ! coltype( j ) = 4
419  ! if (j == n) then
420  ! go to 120
421  ! end if
422  ! else
423  ! jprev = j
424  ! break !! was: go to 90
425  ! end if
426  !end do
427  90 !continue
428  !j = jprev
429  jprev = -1
430  do j = 2 : n !! was `100 continue`
431  !100 continue
432  !j = j + 1
433  !if (j > n) then
434  ! go to 110
435  !end if
436  if (abs( z( j ) ) <= tol) then
437  !! deflate due to small Z component.
438  k2 = k2 - 1
439  idxp( k2 ) = j
440  coltype( j ) = 4
441  else if (jprev > 0) then
442  if (abs( d( j ) - d( jprev ) ) <= tol) then
443  !! singular values are close enough to allow deflation.
444  s = z( jprev )
445  c = z( j )
446 
447  !! find sqrt(a**2 + b**2) without overflow or
448  !! destructive underflow.
449  tau = dlapy2( c, s )
450  c = c / tau
451  s = -s / tau
452  z( j ) = tau
453  z( jprev ) = zero
454 
455  !! apply back the givens rotation to the left and right
456  !! singular vector matrices.
457  idxjp = idxq( idx( jprev )+1 )
458  idxj = idxq( idx( j )+1 )
459  if (idxjp <= nl+1) then
460  idxjp = idxjp - 1
461  end if
462  if (idxj <= nl+1) then
463  idxj = idxj - 1
464  end if
465  call drot( n, u( 1, idxjp ), 1, u( 1, idxj ), 1, c, s )
466  call drot( m, vt( idxjp, 1 ), ldvt, vt( idxj, 1 ), ldvt, c, s )
467  if (coltype( j ) .ne. coltype( jprev )) then
468  coltype( j ) = 3
469  end if
470  coltype( jprev ) = 4
471  k2 = k2 - 1
472  idxp( k2 ) = jprev
473  jprev = j
474  else
475  k = k + 1
476  u2( k, 1 ) = z( jprev )
477  dsigma( k ) = d( jprev )
478  idxp( k ) = jprev
479  jprev = j
480  end if
481  else
482  !! todo: anything else to do here (jprev == 0)??
483  jprev = j
484  end if
485  end do !! was `go to 100`
486  !go to 100
487  110 continue
488 
489  !! record the last singular value.
490  k = k + 1
491  u2( k, 1 ) = z( jprev )
492  dsigma( k ) = d( jprev )
493  idxp( k ) = jprev
494 
495  120 continue
496 
497  !! count up the total number of the various types of columns, then
498  !! form a permutation which positions the four column types into
499  !! four groups of uniform structure (although one or more of these
500  !! groups may be empty).
501  do j = 1, 4
502  ctot( j ) = 0
503  end do
504  do j = 2, n
505  ct = coltype( j )
506  ctot( ct ) = ctot( ct ) + 1
507  end do
508 
509  !! psm(*) = position in submatrix (of types 1 through 4)
510  psm( 1 ) = 2
511  psm( 2 ) = 2 + ctot( 1 )
512  psm( 3 ) = psm( 2 ) + ctot( 2 )
513  psm( 4 ) = psm( 3 ) + ctot( 3 )
514 
515  !! fill out the idxc array so that the permutation which it induces
516  !! will place all type-1 columns first, all type-2 columns next,
517  !! then all type-3's, and finally all type-4's, starting from the
518  !! second column. this applies similarly to the rows of VT.
519  do j = 2, n
520  jp = idxp( j )
521  ct = coltype( jp )
522  idxc( psm( ct ) ) = j
523  psm( ct ) = psm( ct ) + 1
524  end do
525 
526  !! sort the singular values and corresponding singular vectors into
527  !! dsigma, U2, and VT2 respectively. the singular values/vectors
528  !! which were not deflated go into the first k slots of dsigma, U2,
529  !! and VT2 respectively, while those which were deflated go into the
530  !! last n - k slots, except that the first column/row will be treated
531  !! separately.
532  do j = 2, n
533  jp = idxp( j )
534  dsigma( j ) = d( jp )
535  idxj = idxq( idx( idxp( idxc( j ) ) )+1 )
536  if (idxj <= nl+1) then
537  idxj = idxj - 1
538  end if
539  call dcopy( n, u( 1, idxj ), 1, u2( 1, j ), 1 )
540  call dcopy( m, vt( idxj, 1 ), ldvt, vt2( j, 1 ), ldvt2 )
541  end do
542 
543  !! determine dsigma(1), dsigma(2) and Z(1)
544  dsigma( 1 ) = zero
545  hlftol = tol / two
546  if (abs( dsigma( 2 ) ) <= hlftol) then
547  dsigma( 2 ) = hlftol
548  end if
549  if (m > n) then
550  z( 1 ) = dlapy2( z1, z( m ) )
551  if (z( 1 ) <= tol) then
552  c = one
553  s = zero
554  z( 1 ) = tol
555  else
556  c = z1 / z( 1 )
557  s = z( m ) / z( 1 )
558  end if
559  else
560  if (abs( z1 ) <= tol) then
561  z( 1 ) = tol
562  else
563  z( 1 ) = z1
564  end if
565  end if
566 
567  !! move the rest of the updating row to Z.
568  call dcopy( k-1, u2( 2, 1 ), 1, z( 2 ), 1 )
569 
570  !! determine the first column of U2, the first row of VT2 and the
571  !! last row of VT.
572  call dlaset( 'a', n, 1, zero, zero, u2, ldu2 )
573  u2( nl+1, 1 ) = one
574  if (m > n) then
575  do i = 1, nl+1
576  vt( m, i ) = -s*vt( nl+1, i )
577  vt2( 1, i ) = c*vt( nl+1, i )
578  end do
579  do i = nl+2, m
580  vt2( 1, i ) = s*vt( m, i )
581  vt( m, i ) = c*vt( m, i )
582  end do
583  else
584  call dcopy( m, vt( nl+1, 1 ), ldvt, vt2( 1, 1 ), ldvt2 )
585  end if
586  if (m > n) then
587  call dcopy( m, vt( m, 1 ), ldvt, vt2( m, 1 ), ldvt2 )
588  end if
589 
590  !! the deflated singular values and their corresponding vectors go
591  !! into the back of D, U, and V respectively.
592  if (n > k) then
593  call dcopy( n-k, dsigma( k+1 ), 1, d( k+1 ), 1 )
594  call dlacpy( 'a', n, n-k, u2( 1, k+1 ), ldu2, u( 1, k+1 ),
595  $ ldu )
596  call dlacpy( 'a', n-k, m, vt2( k+1, 1 ), ldvt2, vt( k+1, 1 ),
597  $ ldvt )
598  end if
599 
600  !! copy ctot into coltype for referencing in dlasd3.
601  do j = 1, 4
602  coltype( j ) = ctot( j )
603  end do
604 
605  return
606 
607  !! end of dlasd2
608  end
subroutine xerbla(SRNAME, INFO)
XERBLA
Definition: xerbla.f:60
subroutine dlacpy(UPLO, M, N, A, LDA, B, LDB)
DLACPY copies all or part of one two-dimensional array to another.
Definition: dlacpy.f:103
subroutine dlasd2(NL, NR, SQRE, K, D, Z, ALPHA, BETA, U, LDU, VT, LDVT, DSIGMA, U2, LDU2, VT2, LDVT2, IDXP, IDX, IDXC, IDXQ, COLTYP, INFO)
DLASD2 merges the two sets of singular values together into a single sorted set. Used by sbdsdc...
Definition: dlasd2.f:269
double precision function dlamch(CMACH)
DLAMCH
Definition: dlamch.f:69
subroutine dlamrg(N1, N2, A, DTRD1, DTRD2, INDEX)
DLAMRG creates a permutation list to merge the entries of two independently sorted sets into a single...
Definition: dlamrg.f:99
subroutine drot(N, DX, INCX, DY, INCY, C, S)
DROT
Definition: drot.f:92
subroutine dlaset(UPLO, M, N, ALPHA, BETA, A, LDA)
DLASET initializes the off-diagonal elements and the diagonal elements of a matrix to given values...
Definition: dlaset.f:110
double precision function dlapy2(X, Y)
DLAPY2 returns sqrt(x2+y2).
Definition: dlapy2.f:63
subroutine dcopy(N, DX, INCX, DY, INCY)
DCOPY
Definition: dcopy.f:82