001:       SUBROUTINE SLASDA( ICOMPQ, SMLSIZ, N, SQRE, D, E, U, LDU, VT, K,
002:      $                   DIFL, DIFR, Z, POLES, GIVPTR, GIVCOL, LDGCOL,
003:      $                   PERM, GIVNUM, C, S, WORK, IWORK, INFO )
004: *
005: *  -- LAPACK auxiliary routine (version 3.2) --
006: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
007: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
008: *     November 2006
009: *
010: *     .. Scalar Arguments ..
011:       INTEGER            ICOMPQ, INFO, LDGCOL, LDU, N, SMLSIZ, SQRE
012: *     ..
013: *     .. Array Arguments ..
014:       INTEGER            GIVCOL( LDGCOL, * ), GIVPTR( * ), IWORK( * ),
015:      $                   K( * ), PERM( LDGCOL, * )
016:       REAL               C( * ), D( * ), DIFL( LDU, * ), DIFR( LDU, * ),
017:      $                   E( * ), GIVNUM( LDU, * ), POLES( LDU, * ),
018:      $                   S( * ), U( LDU, * ), VT( LDU, * ), WORK( * ),
019:      $                   Z( LDU, * )
020: *     ..
021: *
022: *  Purpose
023: *  =======
024: *
025: *  Using a divide and conquer approach, SLASDA computes the singular
026: *  value decomposition (SVD) of a real upper bidiagonal N-by-M matrix
027: *  B with diagonal D and offdiagonal E, where M = N + SQRE. The
028: *  algorithm computes the singular values in the SVD B = U * S * VT.
029: *  The orthogonal matrices U and VT are optionally computed in
030: *  compact form.
031: *
032: *  A related subroutine, SLASD0, computes the singular values and
033: *  the singular vectors in explicit form.
034: *
035: *  Arguments
036: *  =========
037: *
038: *  ICOMPQ (input) INTEGER
039: *         Specifies whether singular vectors are to be computed
040: *         in compact form, as follows
041: *         = 0: Compute singular values only.
042: *         = 1: Compute singular vectors of upper bidiagonal
043: *              matrix in compact form.
044: *
045: *  SMLSIZ (input) INTEGER
046: *         The maximum size of the subproblems at the bottom of the
047: *         computation tree.
048: *
049: *  N      (input) INTEGER
050: *         The row dimension of the upper bidiagonal matrix. This is
051: *         also the dimension of the main diagonal array D.
052: *
053: *  SQRE   (input) INTEGER
054: *         Specifies the column dimension of the bidiagonal matrix.
055: *         = 0: The bidiagonal matrix has column dimension M = N;
056: *         = 1: The bidiagonal matrix has column dimension M = N + 1.
057: *
058: *  D      (input/output) REAL array, dimension ( N )
059: *         On entry D contains the main diagonal of the bidiagonal
060: *         matrix. On exit D, if INFO = 0, contains its singular values.
061: *
062: *  E      (input) REAL array, dimension ( M-1 )
063: *         Contains the subdiagonal entries of the bidiagonal matrix.
064: *         On exit, E has been destroyed.
065: *
066: *  U      (output) REAL array,
067: *         dimension ( LDU, SMLSIZ ) if ICOMPQ = 1, and not referenced
068: *         if ICOMPQ = 0. If ICOMPQ = 1, on exit, U contains the left
069: *         singular vector matrices of all subproblems at the bottom
070: *         level.
071: *
072: *  LDU    (input) INTEGER, LDU = > N.
073: *         The leading dimension of arrays U, VT, DIFL, DIFR, POLES,
074: *         GIVNUM, and Z.
075: *
076: *  VT     (output) REAL array,
077: *         dimension ( LDU, SMLSIZ+1 ) if ICOMPQ = 1, and not referenced
078: *         if ICOMPQ = 0. If ICOMPQ = 1, on exit, VT' contains the right
079: *         singular vector matrices of all subproblems at the bottom
080: *         level.
081: *
082: *  K      (output) INTEGER array, dimension ( N ) 
083: *         if ICOMPQ = 1 and dimension 1 if ICOMPQ = 0.
084: *         If ICOMPQ = 1, on exit, K(I) is the dimension of the I-th
085: *         secular equation on the computation tree.
086: *
087: *  DIFL   (output) REAL array, dimension ( LDU, NLVL ),
088: *         where NLVL = floor(log_2 (N/SMLSIZ))).
089: *
090: *  DIFR   (output) REAL array,
091: *                  dimension ( LDU, 2 * NLVL ) if ICOMPQ = 1 and
092: *                  dimension ( N ) if ICOMPQ = 0.
093: *         If ICOMPQ = 1, on exit, DIFL(1:N, I) and DIFR(1:N, 2 * I - 1)
094: *         record distances between singular values on the I-th
095: *         level and singular values on the (I -1)-th level, and
096: *         DIFR(1:N, 2 * I ) contains the normalizing factors for
097: *         the right singular vector matrix. See SLASD8 for details.
098: *
099: *  Z      (output) REAL array,
100: *                  dimension ( LDU, NLVL ) if ICOMPQ = 1 and
101: *                  dimension ( N ) if ICOMPQ = 0.
102: *         The first K elements of Z(1, I) contain the components of
103: *         the deflation-adjusted updating row vector for subproblems
104: *         on the I-th level.
105: *
106: *  POLES  (output) REAL array,
107: *         dimension ( LDU, 2 * NLVL ) if ICOMPQ = 1, and not referenced
108: *         if ICOMPQ = 0. If ICOMPQ = 1, on exit, POLES(1, 2*I - 1) and
109: *         POLES(1, 2*I) contain  the new and old singular values
110: *         involved in the secular equations on the I-th level.
111: *
112: *  GIVPTR (output) INTEGER array,
113: *         dimension ( N ) if ICOMPQ = 1, and not referenced if
114: *         ICOMPQ = 0. If ICOMPQ = 1, on exit, GIVPTR( I ) records
115: *         the number of Givens rotations performed on the I-th
116: *         problem on the computation tree.
117: *
118: *  GIVCOL (output) INTEGER array,
119: *         dimension ( LDGCOL, 2 * NLVL ) if ICOMPQ = 1, and not
120: *         referenced if ICOMPQ = 0. If ICOMPQ = 1, on exit, for each I,
121: *         GIVCOL(1, 2 *I - 1) and GIVCOL(1, 2 *I) record the locations
122: *         of Givens rotations performed on the I-th level on the
123: *         computation tree.
124: *
125: *  LDGCOL (input) INTEGER, LDGCOL = > N.
126: *         The leading dimension of arrays GIVCOL and PERM.
127: *
128: *  PERM   (output) INTEGER array, dimension ( LDGCOL, NLVL ) 
129: *         if ICOMPQ = 1, and not referenced
130: *         if ICOMPQ = 0. If ICOMPQ = 1, on exit, PERM(1, I) records
131: *         permutations done on the I-th level of the computation tree.
132: *
133: *  GIVNUM (output) REAL array,
134: *         dimension ( LDU,  2 * NLVL ) if ICOMPQ = 1, and not
135: *         referenced if ICOMPQ = 0. If ICOMPQ = 1, on exit, for each I,
136: *         GIVNUM(1, 2 *I - 1) and GIVNUM(1, 2 *I) record the C- and S-
137: *         values of Givens rotations performed on the I-th level on
138: *         the computation tree.
139: *
140: *  C      (output) REAL array,
141: *         dimension ( N ) if ICOMPQ = 1, and dimension 1 if ICOMPQ = 0.
142: *         If ICOMPQ = 1 and the I-th subproblem is not square, on exit,
143: *         C( I ) contains the C-value of a Givens rotation related to
144: *         the right null space of the I-th subproblem.
145: *
146: *  S      (output) REAL array, dimension ( N ) if
147: *         ICOMPQ = 1, and dimension 1 if ICOMPQ = 0. If ICOMPQ = 1
148: *         and the I-th subproblem is not square, on exit, S( I )
149: *         contains the S-value of a Givens rotation related to
150: *         the right null space of the I-th subproblem.
151: *
152: *  WORK   (workspace) REAL array, dimension
153: *         (6 * N + (SMLSIZ + 1)*(SMLSIZ + 1)).
154: *
155: *  IWORK  (workspace) INTEGER array, dimension (7*N).
156: *
157: *  INFO   (output) INTEGER
158: *          = 0:  successful exit.
159: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
160: *          > 0:  if INFO = 1, an singular value did not converge
161: *
162: *  Further Details
163: *  ===============
164: *
165: *  Based on contributions by
166: *     Ming Gu and Huan Ren, Computer Science Division, University of
167: *     California at Berkeley, USA
168: *
169: *  =====================================================================
170: *
171: *     .. Parameters ..
172:       REAL               ZERO, ONE
173:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
174: *     ..
175: *     .. Local Scalars ..
176:       INTEGER            I, I1, IC, IDXQ, IDXQI, IM1, INODE, ITEMP, IWK,
177:      $                   J, LF, LL, LVL, LVL2, M, NCC, ND, NDB1, NDIML,
178:      $                   NDIMR, NL, NLF, NLP1, NLVL, NR, NRF, NRP1, NRU,
179:      $                   NWORK1, NWORK2, SMLSZP, SQREI, VF, VFI, VL, VLI
180:       REAL               ALPHA, BETA
181: *     ..
182: *     .. External Subroutines ..
183:       EXTERNAL           SCOPY, SLASD6, SLASDQ, SLASDT, SLASET, XERBLA
184: *     ..
185: *     .. Executable Statements ..
186: *
187: *     Test the input parameters.
188: *
189:       INFO = 0
190: *
191:       IF( ( ICOMPQ.LT.0 ) .OR. ( ICOMPQ.GT.1 ) ) THEN
192:          INFO = -1
193:       ELSE IF( SMLSIZ.LT.3 ) THEN
194:          INFO = -2
195:       ELSE IF( N.LT.0 ) THEN
196:          INFO = -3
197:       ELSE IF( ( SQRE.LT.0 ) .OR. ( SQRE.GT.1 ) ) THEN
198:          INFO = -4
199:       ELSE IF( LDU.LT.( N+SQRE ) ) THEN
200:          INFO = -8
201:       ELSE IF( LDGCOL.LT.N ) THEN
202:          INFO = -17
203:       END IF
204:       IF( INFO.NE.0 ) THEN
205:          CALL XERBLA( 'SLASDA', -INFO )
206:          RETURN
207:       END IF
208: *
209:       M = N + SQRE
210: *
211: *     If the input matrix is too small, call SLASDQ to find the SVD.
212: *
213:       IF( N.LE.SMLSIZ ) THEN
214:          IF( ICOMPQ.EQ.0 ) THEN
215:             CALL SLASDQ( 'U', SQRE, N, 0, 0, 0, D, E, VT, LDU, U, LDU,
216:      $                   U, LDU, WORK, INFO )
217:          ELSE
218:             CALL SLASDQ( 'U', SQRE, N, M, N, 0, D, E, VT, LDU, U, LDU,
219:      $                   U, LDU, WORK, INFO )
220:          END IF
221:          RETURN
222:       END IF
223: *
224: *     Book-keeping and  set up the computation tree.
225: *
226:       INODE = 1
227:       NDIML = INODE + N
228:       NDIMR = NDIML + N
229:       IDXQ = NDIMR + N
230:       IWK = IDXQ + N
231: *
232:       NCC = 0
233:       NRU = 0
234: *
235:       SMLSZP = SMLSIZ + 1
236:       VF = 1
237:       VL = VF + M
238:       NWORK1 = VL + M
239:       NWORK2 = NWORK1 + SMLSZP*SMLSZP
240: *
241:       CALL SLASDT( N, NLVL, ND, IWORK( INODE ), IWORK( NDIML ),
242:      $             IWORK( NDIMR ), SMLSIZ )
243: *
244: *     for the nodes on bottom level of the tree, solve
245: *     their subproblems by SLASDQ.
246: *
247:       NDB1 = ( ND+1 ) / 2
248:       DO 30 I = NDB1, ND
249: *
250: *        IC : center row of each node
251: *        NL : number of rows of left  subproblem
252: *        NR : number of rows of right subproblem
253: *        NLF: starting row of the left   subproblem
254: *        NRF: starting row of the right  subproblem
255: *
256:          I1 = I - 1
257:          IC = IWORK( INODE+I1 )
258:          NL = IWORK( NDIML+I1 )
259:          NLP1 = NL + 1
260:          NR = IWORK( NDIMR+I1 )
261:          NLF = IC - NL
262:          NRF = IC + 1
263:          IDXQI = IDXQ + NLF - 2
264:          VFI = VF + NLF - 1
265:          VLI = VL + NLF - 1
266:          SQREI = 1
267:          IF( ICOMPQ.EQ.0 ) THEN
268:             CALL SLASET( 'A', NLP1, NLP1, ZERO, ONE, WORK( NWORK1 ),
269:      $                   SMLSZP )
270:             CALL SLASDQ( 'U', SQREI, NL, NLP1, NRU, NCC, D( NLF ),
271:      $                   E( NLF ), WORK( NWORK1 ), SMLSZP,
272:      $                   WORK( NWORK2 ), NL, WORK( NWORK2 ), NL,
273:      $                   WORK( NWORK2 ), INFO )
274:             ITEMP = NWORK1 + NL*SMLSZP
275:             CALL SCOPY( NLP1, WORK( NWORK1 ), 1, WORK( VFI ), 1 )
276:             CALL SCOPY( NLP1, WORK( ITEMP ), 1, WORK( VLI ), 1 )
277:          ELSE
278:             CALL SLASET( 'A', NL, NL, ZERO, ONE, U( NLF, 1 ), LDU )
279:             CALL SLASET( 'A', NLP1, NLP1, ZERO, ONE, VT( NLF, 1 ), LDU )
280:             CALL SLASDQ( 'U', SQREI, NL, NLP1, NL, NCC, D( NLF ),
281:      $                   E( NLF ), VT( NLF, 1 ), LDU, U( NLF, 1 ), LDU,
282:      $                   U( NLF, 1 ), LDU, WORK( NWORK1 ), INFO )
283:             CALL SCOPY( NLP1, VT( NLF, 1 ), 1, WORK( VFI ), 1 )
284:             CALL SCOPY( NLP1, VT( NLF, NLP1 ), 1, WORK( VLI ), 1 )
285:          END IF
286:          IF( INFO.NE.0 ) THEN
287:             RETURN
288:          END IF
289:          DO 10 J = 1, NL
290:             IWORK( IDXQI+J ) = J
291:    10    CONTINUE
292:          IF( ( I.EQ.ND ) .AND. ( SQRE.EQ.0 ) ) THEN
293:             SQREI = 0
294:          ELSE
295:             SQREI = 1
296:          END IF
297:          IDXQI = IDXQI + NLP1
298:          VFI = VFI + NLP1
299:          VLI = VLI + NLP1
300:          NRP1 = NR + SQREI
301:          IF( ICOMPQ.EQ.0 ) THEN
302:             CALL SLASET( 'A', NRP1, NRP1, ZERO, ONE, WORK( NWORK1 ),
303:      $                   SMLSZP )
304:             CALL SLASDQ( 'U', SQREI, NR, NRP1, NRU, NCC, D( NRF ),
305:      $                   E( NRF ), WORK( NWORK1 ), SMLSZP,
306:      $                   WORK( NWORK2 ), NR, WORK( NWORK2 ), NR,
307:      $                   WORK( NWORK2 ), INFO )
308:             ITEMP = NWORK1 + ( NRP1-1 )*SMLSZP
309:             CALL SCOPY( NRP1, WORK( NWORK1 ), 1, WORK( VFI ), 1 )
310:             CALL SCOPY( NRP1, WORK( ITEMP ), 1, WORK( VLI ), 1 )
311:          ELSE
312:             CALL SLASET( 'A', NR, NR, ZERO, ONE, U( NRF, 1 ), LDU )
313:             CALL SLASET( 'A', NRP1, NRP1, ZERO, ONE, VT( NRF, 1 ), LDU )
314:             CALL SLASDQ( 'U', SQREI, NR, NRP1, NR, NCC, D( NRF ),
315:      $                   E( NRF ), VT( NRF, 1 ), LDU, U( NRF, 1 ), LDU,
316:      $                   U( NRF, 1 ), LDU, WORK( NWORK1 ), INFO )
317:             CALL SCOPY( NRP1, VT( NRF, 1 ), 1, WORK( VFI ), 1 )
318:             CALL SCOPY( NRP1, VT( NRF, NRP1 ), 1, WORK( VLI ), 1 )
319:          END IF
320:          IF( INFO.NE.0 ) THEN
321:             RETURN
322:          END IF
323:          DO 20 J = 1, NR
324:             IWORK( IDXQI+J ) = J
325:    20    CONTINUE
326:    30 CONTINUE
327: *
328: *     Now conquer each subproblem bottom-up.
329: *
330:       J = 2**NLVL
331:       DO 50 LVL = NLVL, 1, -1
332:          LVL2 = LVL*2 - 1
333: *
334: *        Find the first node LF and last node LL on
335: *        the current level LVL.
336: *
337:          IF( LVL.EQ.1 ) THEN
338:             LF = 1
339:             LL = 1
340:          ELSE
341:             LF = 2**( LVL-1 )
342:             LL = 2*LF - 1
343:          END IF
344:          DO 40 I = LF, LL
345:             IM1 = I - 1
346:             IC = IWORK( INODE+IM1 )
347:             NL = IWORK( NDIML+IM1 )
348:             NR = IWORK( NDIMR+IM1 )
349:             NLF = IC - NL
350:             NRF = IC + 1
351:             IF( I.EQ.LL ) THEN
352:                SQREI = SQRE
353:             ELSE
354:                SQREI = 1
355:             END IF
356:             VFI = VF + NLF - 1
357:             VLI = VL + NLF - 1
358:             IDXQI = IDXQ + NLF - 1
359:             ALPHA = D( IC )
360:             BETA = E( IC )
361:             IF( ICOMPQ.EQ.0 ) THEN
362:                CALL SLASD6( ICOMPQ, NL, NR, SQREI, D( NLF ),
363:      $                      WORK( VFI ), WORK( VLI ), ALPHA, BETA,
364:      $                      IWORK( IDXQI ), PERM, GIVPTR( 1 ), GIVCOL,
365:      $                      LDGCOL, GIVNUM, LDU, POLES, DIFL, DIFR, Z,
366:      $                      K( 1 ), C( 1 ), S( 1 ), WORK( NWORK1 ),
367:      $                      IWORK( IWK ), INFO )
368:             ELSE
369:                J = J - 1
370:                CALL SLASD6( ICOMPQ, NL, NR, SQREI, D( NLF ),
371:      $                      WORK( VFI ), WORK( VLI ), ALPHA, BETA,
372:      $                      IWORK( IDXQI ), PERM( NLF, LVL ),
373:      $                      GIVPTR( J ), GIVCOL( NLF, LVL2 ), LDGCOL,
374:      $                      GIVNUM( NLF, LVL2 ), LDU,
375:      $                      POLES( NLF, LVL2 ), DIFL( NLF, LVL ),
376:      $                      DIFR( NLF, LVL2 ), Z( NLF, LVL ), K( J ),
377:      $                      C( J ), S( J ), WORK( NWORK1 ),
378:      $                      IWORK( IWK ), INFO )
379:             END IF
380:             IF( INFO.NE.0 ) THEN
381:                RETURN
382:             END IF
383:    40    CONTINUE
384:    50 CONTINUE
385: *
386:       RETURN
387: *
388: *     End of SLASDA
389: *
390:       END
391: