001:       SUBROUTINE SLASD6( ICOMPQ, NL, NR, SQRE, D, VF, VL, ALPHA, BETA,
002:      $                   IDXQ, PERM, GIVPTR, GIVCOL, LDGCOL, GIVNUM,
003:      $                   LDGNUM, POLES, DIFL, DIFR, Z, K, C, S, WORK,
004:      $                   IWORK, INFO )
005: *
006: *  -- LAPACK auxiliary routine (version 3.2) --
007: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
008: *     November 2006
009: *
010: *     .. Scalar Arguments ..
011:       INTEGER            GIVPTR, ICOMPQ, INFO, K, LDGCOL, LDGNUM, NL,
012:      $                   NR, SQRE
013:       REAL               ALPHA, BETA, C, S
014: *     ..
015: *     .. Array Arguments ..
016:       INTEGER            GIVCOL( LDGCOL, * ), IDXQ( * ), IWORK( * ),
017:      $                   PERM( * )
018:       REAL               D( * ), DIFL( * ), DIFR( * ),
019:      $                   GIVNUM( LDGNUM, * ), POLES( LDGNUM, * ),
020:      $                   VF( * ), VL( * ), WORK( * ), Z( * )
021: *     ..
022: *
023: *  Purpose
024: *  =======
025: *
026: *  SLASD6 computes the SVD of an updated upper bidiagonal matrix B
027: *  obtained by merging two smaller ones by appending a row. This
028: *  routine is used only for the problem which requires all singular
029: *  values and optionally singular vector matrices in factored form.
030: *  B is an N-by-M matrix with N = NL + NR + 1 and M = N + SQRE.
031: *  A related subroutine, SLASD1, handles the case in which all singular
032: *  values and singular vectors of the bidiagonal matrix are desired.
033: *
034: *  SLASD6 computes the SVD as follows:
035: *
036: *                ( D1(in)  0    0     0 )
037: *    B = U(in) * (   Z1'   a   Z2'    b ) * VT(in)
038: *                (   0     0   D2(in) 0 )
039: *
040: *      = U(out) * ( D(out) 0) * VT(out)
041: *
042: *  where Z' = (Z1' a Z2' b) = u' VT', and u is a vector of dimension M
043: *  with ALPHA and BETA in the NL+1 and NL+2 th entries and zeros
044: *  elsewhere; and the entry b is empty if SQRE = 0.
045: *
046: *  The singular values of B can be computed using D1, D2, the first
047: *  components of all the right singular vectors of the lower block, and
048: *  the last components of all the right singular vectors of the upper
049: *  block. These components are stored and updated in VF and VL,
050: *  respectively, in SLASD6. Hence U and VT are not explicitly
051: *  referenced.
052: *
053: *  The singular values are stored in D. The algorithm consists of two
054: *  stages:
055: *
056: *        The first stage consists of deflating the size of the problem
057: *        when there are multiple singular values or if there is a zero
058: *        in the Z vector. For each such occurence the dimension of the
059: *        secular equation problem is reduced by one. This stage is
060: *        performed by the routine SLASD7.
061: *
062: *        The second stage consists of calculating the updated
063: *        singular values. This is done by finding the roots of the
064: *        secular equation via the routine SLASD4 (as called by SLASD8).
065: *        This routine also updates VF and VL and computes the distances
066: *        between the updated singular values and the old singular
067: *        values.
068: *
069: *  SLASD6 is called from SLASDA.
070: *
071: *  Arguments
072: *  =========
073: *
074: *  ICOMPQ (input) INTEGER
075: *         Specifies whether singular vectors are to be computed in
076: *         factored form:
077: *         = 0: Compute singular values only.
078: *         = 1: Compute singular vectors in factored form as well.
079: *
080: *  NL     (input) INTEGER
081: *         The row dimension of the upper block.  NL >= 1.
082: *
083: *  NR     (input) INTEGER
084: *         The row dimension of the lower block.  NR >= 1.
085: *
086: *  SQRE   (input) INTEGER
087: *         = 0: the lower block is an NR-by-NR square matrix.
088: *         = 1: the lower block is an NR-by-(NR+1) rectangular matrix.
089: *
090: *         The bidiagonal matrix has row dimension N = NL + NR + 1,
091: *         and column dimension M = N + SQRE.
092: *
093: *  D      (input/output) REAL array, dimension (NL+NR+1).
094: *         On entry D(1:NL,1:NL) contains the singular values of the
095: *         upper block, and D(NL+2:N) contains the singular values
096: *         of the lower block. On exit D(1:N) contains the singular
097: *         values of the modified matrix.
098: *
099: *  VF     (input/output) REAL 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.
105: *
106: *  VL     (input/output) REAL array, dimension (M)
107: *         On entry, VL(1:NL+1) contains the  last components of all
108: *         right singular vectors of the upper block; and VL(NL+2:M)
109: *         contains the last components of all right singular vectors of
110: *         the lower block. On exit, VL contains the last components of
111: *         all right singular vectors of the bidiagonal matrix.
112: *
113: *  ALPHA  (input/output) REAL
114: *         Contains the diagonal element associated with the added row.
115: *
116: *  BETA   (input/output) REAL
117: *         Contains the off-diagonal element associated with the added
118: *         row.
119: *
120: *  IDXQ   (output) INTEGER array, dimension (N)
121: *         This contains the permutation which will reintegrate the
122: *         subproblem just solved back into sorted order, i.e.
123: *         D( IDXQ( I = 1, N ) ) will be in ascending order.
124: *
125: *  PERM   (output) INTEGER array, dimension ( N )
126: *         The permutations (from deflation and sorting) to be applied
127: *         to each block. Not referenced if ICOMPQ = 0.
128: *
129: *  GIVPTR (output) INTEGER
130: *         The number of Givens rotations which took place in this
131: *         subproblem. Not referenced if ICOMPQ = 0.
132: *
133: *  GIVCOL (output) INTEGER array, dimension ( LDGCOL, 2 )
134: *         Each pair of numbers indicates a pair of columns to take place
135: *         in a Givens rotation. Not referenced if ICOMPQ = 0.
136: *
137: *  LDGCOL (input) INTEGER
138: *         leading dimension of GIVCOL, must be at least N.
139: *
140: *  GIVNUM (output) REAL array, dimension ( LDGNUM, 2 )
141: *         Each number indicates the C or S value to be used in the
142: *         corresponding Givens rotation. Not referenced if ICOMPQ = 0.
143: *
144: *  LDGNUM (input) INTEGER
145: *         The leading dimension of GIVNUM and POLES, must be at least N.
146: *
147: *  POLES  (output) REAL array, dimension ( LDGNUM, 2 )
148: *         On exit, POLES(1,*) is an array containing the new singular
149: *         values obtained from solving the secular equation, and
150: *         POLES(2,*) is an array containing the poles in the secular
151: *         equation. Not referenced if ICOMPQ = 0.
152: *
153: *  DIFL   (output) REAL array, dimension ( N )
154: *         On exit, DIFL(I) is the distance between I-th updated
155: *         (undeflated) singular value and the I-th (undeflated) old
156: *         singular value.
157: *
158: *  DIFR   (output) REAL array,
159: *                  dimension ( LDGNUM, 2 ) if ICOMPQ = 1 and
160: *                  dimension ( N ) if ICOMPQ = 0.
161: *         On exit, DIFR(I, 1) is the distance between I-th updated
162: *         (undeflated) singular value and the I+1-th (undeflated) old
163: *         singular value.
164: *
165: *         If ICOMPQ = 1, DIFR(1:K,2) is an array containing the
166: *         normalizing factors for the right singular vector matrix.
167: *
168: *         See SLASD8 for details on DIFL and DIFR.
169: *
170: *  Z      (output) REAL array, dimension ( M )
171: *         The first elements of this array contain the components
172: *         of the deflation-adjusted updating row vector.
173: *
174: *  K      (output) INTEGER
175: *         Contains the dimension of the non-deflated matrix,
176: *         This is the order of the related secular equation. 1 <= K <=N.
177: *
178: *  C      (output) REAL
179: *         C contains garbage if SQRE =0 and the C-value of a Givens
180: *         rotation related to the right null space if SQRE = 1.
181: *
182: *  S      (output) REAL
183: *         S contains garbage if SQRE =0 and the S-value of a Givens
184: *         rotation related to the right null space if SQRE = 1.
185: *
186: *  WORK   (workspace) REAL array, dimension ( 4 * M )
187: *
188: *  IWORK  (workspace) INTEGER array, dimension ( 3 * N )
189: *
190: *  INFO   (output) INTEGER
191: *          = 0:  successful exit.
192: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
193: *          > 0:  if INFO = 1, an singular value did not converge
194: *
195: *  Further Details
196: *  ===============
197: *
198: *  Based on contributions by
199: *     Ming Gu and Huan Ren, Computer Science Division, University of
200: *     California at Berkeley, USA
201: *
202: *  =====================================================================
203: *
204: *     .. Parameters ..
205:       REAL               ONE, ZERO
206:       PARAMETER          ( ONE = 1.0E+0, ZERO = 0.0E+0 )
207: *     ..
208: *     .. Local Scalars ..
209:       INTEGER            I, IDX, IDXC, IDXP, ISIGMA, IVFW, IVLW, IW, M,
210:      $                   N, N1, N2
211:       REAL               ORGNRM
212: *     ..
213: *     .. External Subroutines ..
214:       EXTERNAL           SCOPY, SLAMRG, SLASCL, SLASD7, SLASD8, XERBLA
215: *     ..
216: *     .. Intrinsic Functions ..
217:       INTRINSIC          ABS, MAX
218: *     ..
219: *     .. Executable Statements ..
220: *
221: *     Test the input parameters.
222: *
223:       INFO = 0
224:       N = NL + NR + 1
225:       M = N + SQRE
226: *
227:       IF( ( ICOMPQ.LT.0 ) .OR. ( ICOMPQ.GT.1 ) ) THEN
228:          INFO = -1
229:       ELSE IF( NL.LT.1 ) THEN
230:          INFO = -2
231:       ELSE IF( NR.LT.1 ) THEN
232:          INFO = -3
233:       ELSE IF( ( SQRE.LT.0 ) .OR. ( SQRE.GT.1 ) ) THEN
234:          INFO = -4
235:       ELSE IF( LDGCOL.LT.N ) THEN
236:          INFO = -14
237:       ELSE IF( LDGNUM.LT.N ) THEN
238:          INFO = -16
239:       END IF
240:       IF( INFO.NE.0 ) THEN
241:          CALL XERBLA( 'SLASD6', -INFO )
242:          RETURN
243:       END IF
244: *
245: *     The following values are for bookkeeping purposes only.  They are
246: *     integer pointers which indicate the portion of the workspace
247: *     used by a particular array in SLASD7 and SLASD8.
248: *
249:       ISIGMA = 1
250:       IW = ISIGMA + N
251:       IVFW = IW + M
252:       IVLW = IVFW + M
253: *
254:       IDX = 1
255:       IDXC = IDX + N
256:       IDXP = IDXC + N
257: *
258: *     Scale.
259: *
260:       ORGNRM = MAX( ABS( ALPHA ), ABS( BETA ) )
261:       D( NL+1 ) = ZERO
262:       DO 10 I = 1, N
263:          IF( ABS( D( I ) ).GT.ORGNRM ) THEN
264:             ORGNRM = ABS( D( I ) )
265:          END IF
266:    10 CONTINUE
267:       CALL SLASCL( 'G', 0, 0, ORGNRM, ONE, N, 1, D, N, INFO )
268:       ALPHA = ALPHA / ORGNRM
269:       BETA = BETA / ORGNRM
270: *
271: *     Sort and Deflate singular values.
272: *
273:       CALL SLASD7( ICOMPQ, NL, NR, SQRE, K, D, Z, WORK( IW ), VF,
274:      $             WORK( IVFW ), VL, WORK( IVLW ), ALPHA, BETA,
275:      $             WORK( ISIGMA ), IWORK( IDX ), IWORK( IDXP ), IDXQ,
276:      $             PERM, GIVPTR, GIVCOL, LDGCOL, GIVNUM, LDGNUM, C, S,
277:      $             INFO )
278: *
279: *     Solve Secular Equation, compute DIFL, DIFR, and update VF, VL.
280: *
281:       CALL SLASD8( ICOMPQ, K, D, Z, VF, VL, DIFL, DIFR, LDGNUM,
282:      $             WORK( ISIGMA ), WORK( IW ), INFO )
283: *
284: *     Save the poles if ICOMPQ = 1.
285: *
286:       IF( ICOMPQ.EQ.1 ) THEN
287:          CALL SCOPY( K, D, 1, POLES( 1, 1 ), 1 )
288:          CALL SCOPY( K, WORK( ISIGMA ), 1, POLES( 1, 2 ), 1 )
289:       END IF
290: *
291: *     Unscale.
292: *
293:       CALL SLASCL( 'G', 0, 0, ONE, ORGNRM, N, 1, D, N, INFO )
294: *
295: *     Prepare the IDXQ sorting permutation.
296: *
297:       N1 = K
298:       N2 = N - K
299:       CALL SLAMRG( N1, N2, D, 1, -1, IDXQ )
300: *
301:       RETURN
302: *
303: *     End of SLASD6
304: *
305:       END
306: