1
SUBROUTINE DBDSDC( UPLO, COMPQ, N, D, E, U, LDU, VT, LDVT, Q, IQ,
4
* -- LAPACK routine (version 3.2) --
5
* -- LAPACK is a software package provided by Univ. of Tennessee, --
6
* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
9
* .. Scalar Arguments ..
11
INTEGER INFO, LDU, LDVT, N
13
* .. Array Arguments ..
14
INTEGER IQ( * ), IWORK( * )
15
DOUBLE PRECISION D( * ), E( * ), Q( * ), U( LDU, * ),
16
$ VT( LDVT, * ), WORK( * )
22
* DBDSDC computes the singular value decomposition (SVD) of a real
23
* N-by-N (upper or lower) bidiagonal matrix B: B = U * S * VT,
24
* using a divide and conquer method, where S is a diagonal matrix
25
* with non-negative diagonal elements (the singular values of B), and
26
* U and VT are orthogonal matrices of left and right singular vectors,
27
* respectively. DBDSDC can be used to compute all singular values,
28
* and optionally, singular vectors or singular vectors in compact form.
30
* This code makes very mild assumptions about floating point
31
* arithmetic. It will work on machines with a guard digit in
32
* add/subtract, or on those binary machines without guard digits
33
* which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or Cray-2.
34
* It could conceivably fail on hexadecimal or decimal machines
35
* without guard digits, but we know of none. See DLASD3 for details.
37
* The code currently calls DLASDQ if singular values only are desired.
38
* However, it can be slightly modified to compute singular values
39
* using the divide and conquer method.
44
* UPLO (input) CHARACTER*1
45
* = 'U': B is upper bidiagonal.
46
* = 'L': B is lower bidiagonal.
48
* COMPQ (input) CHARACTER*1
49
* Specifies whether singular vectors are to be computed
51
* = 'N': Compute singular values only;
52
* = 'P': Compute singular values and compute singular
53
* vectors in compact form;
54
* = 'I': Compute singular values and singular vectors.
57
* The order of the matrix B. N >= 0.
59
* D (input/output) DOUBLE PRECISION array, dimension (N)
60
* On entry, the n diagonal elements of the bidiagonal matrix B.
61
* On exit, if INFO=0, the singular values of B.
63
* E (input/output) DOUBLE PRECISION array, dimension (N-1)
64
* On entry, the elements of E contain the offdiagonal
65
* elements of the bidiagonal matrix whose SVD is desired.
66
* On exit, E has been destroyed.
68
* U (output) DOUBLE PRECISION array, dimension (LDU,N)
69
* If COMPQ = 'I', then:
70
* On exit, if INFO = 0, U contains the left singular vectors
71
* of the bidiagonal matrix.
72
* For other values of COMPQ, U is not referenced.
75
* The leading dimension of the array U. LDU >= 1.
76
* If singular vectors are desired, then LDU >= max( 1, N ).
78
* VT (output) DOUBLE PRECISION array, dimension (LDVT,N)
79
* If COMPQ = 'I', then:
80
* On exit, if INFO = 0, VT' contains the right singular
81
* vectors of the bidiagonal matrix.
82
* For other values of COMPQ, VT is not referenced.
84
* LDVT (input) INTEGER
85
* The leading dimension of the array VT. LDVT >= 1.
86
* If singular vectors are desired, then LDVT >= max( 1, N ).
88
* Q (output) DOUBLE PRECISION array, dimension (LDQ)
89
* If COMPQ = 'P', then:
90
* On exit, if INFO = 0, Q and IQ contain the left
91
* and right singular vectors in a compact form,
92
* requiring O(N log N) space instead of 2*N**2.
93
* In particular, Q contains all the DOUBLE PRECISION data in
94
* LDQ >= N*(11 + 2*SMLSIZ + 8*INT(LOG_2(N/(SMLSIZ+1))))
95
* words of memory, where SMLSIZ is returned by ILAENV and
96
* is equal to the maximum size of the subproblems at the
97
* bottom of the computation tree (usually about 25).
98
* For other values of COMPQ, Q is not referenced.
100
* IQ (output) INTEGER array, dimension (LDIQ)
101
* If COMPQ = 'P', then:
102
* On exit, if INFO = 0, Q and IQ contain the left
103
* and right singular vectors in a compact form,
104
* requiring O(N log N) space instead of 2*N**2.
105
* In particular, IQ contains all INTEGER data in
106
* LDIQ >= N*(3 + 3*INT(LOG_2(N/(SMLSIZ+1))))
107
* words of memory, where SMLSIZ is returned by ILAENV and
108
* is equal to the maximum size of the subproblems at the
109
* bottom of the computation tree (usually about 25).
110
* For other values of COMPQ, IQ is not referenced.
112
* WORK (workspace) DOUBLE PRECISION array, dimension (MAX(1,LWORK))
113
* If COMPQ = 'N' then LWORK >= (4 * N).
114
* If COMPQ = 'P' then LWORK >= (6 * N).
115
* If COMPQ = 'I' then LWORK >= (3 * N**2 + 4 * N).
117
* IWORK (workspace) INTEGER array, dimension (8*N)
119
* INFO (output) INTEGER
120
* = 0: successful exit.
121
* < 0: if INFO = -i, the i-th argument had an illegal value.
122
* > 0: The algorithm failed to compute an singular value.
123
* The update process of divide and conquer failed.
128
* Based on contributions by
129
* Ming Gu and Huan Ren, Computer Science Division, University of
130
* California at Berkeley, USA
132
* =====================================================================
133
* Changed dimension statement in comment describing E from (N) to
134
* (N-1). Sven, 17 Feb 05.
135
* =====================================================================
138
DOUBLE PRECISION ZERO, ONE, TWO
139
PARAMETER ( ZERO = 0.0D+0, ONE = 1.0D+0, TWO = 2.0D+0 )
141
* .. Local Scalars ..
142
INTEGER DIFL, DIFR, GIVCOL, GIVNUM, GIVPTR, I, IC,
143
$ ICOMPQ, IERR, II, IS, IU, IUPLO, IVT, J, K, KK,
144
$ MLVL, NM1, NSIZE, PERM, POLES, QSTART, SMLSIZ,
145
$ SMLSZP, SQRE, START, WSTART, Z
146
DOUBLE PRECISION CS, EPS, ORGNRM, P, R, SN
148
* .. External Functions ..
151
DOUBLE PRECISION DLAMCH, DLANST
152
EXTERNAL LSAME, ILAENV, DLAMCH, DLANST
154
* .. External Subroutines ..
155
EXTERNAL DCOPY, DLARTG, DLASCL, DLASD0, DLASDA, DLASDQ,
156
$ DLASET, DLASR, DSWAP, XERBLA
158
* .. Intrinsic Functions ..
159
INTRINSIC ABS, DBLE, INT, LOG, SIGN
161
* .. Executable Statements ..
163
* Test the input parameters.
168
IF( LSAME( UPLO, 'U' ) )
170
IF( LSAME( UPLO, 'L' ) )
172
IF( LSAME( COMPQ, 'N' ) ) THEN
174
ELSE IF( LSAME( COMPQ, 'P' ) ) THEN
176
ELSE IF( LSAME( COMPQ, 'I' ) ) THEN
181
IF( IUPLO.EQ.0 ) THEN
183
ELSE IF( ICOMPQ.LT.0 ) THEN
185
ELSE IF( N.LT.0 ) THEN
187
ELSE IF( ( LDU.LT.1 ) .OR. ( ( ICOMPQ.EQ.2 ) .AND. ( LDU.LT.
190
ELSE IF( ( LDVT.LT.1 ) .OR. ( ( ICOMPQ.EQ.2 ) .AND. ( LDVT.LT.
195
CALL XERBLA( 'DBDSDC', -INFO )
199
* Quick return if possible
203
SMLSIZ = ILAENV( 9, 'DBDSDC', ' ', 0, 0, 0, 0 )
205
IF( ICOMPQ.EQ.1 ) THEN
206
Q( 1 ) = SIGN( ONE, D( 1 ) )
207
Q( 1+SMLSIZ*N ) = ONE
208
ELSE IF( ICOMPQ.EQ.2 ) THEN
209
U( 1, 1 ) = SIGN( ONE, D( 1 ) )
212
D( 1 ) = ABS( D( 1 ) )
217
* If matrix lower bidiagonal, rotate to be upper bidiagonal
218
* by applying Givens rotations on the left
222
IF( ICOMPQ.EQ.1 ) THEN
223
CALL DCOPY( N, D, 1, Q( 1 ), 1 )
224
CALL DCOPY( N-1, E, 1, Q( N+1 ), 1 )
226
IF( IUPLO.EQ.2 ) THEN
230
CALL DLARTG( D( I ), E( I ), CS, SN, R )
233
D( I+1 ) = CS*D( I+1 )
234
IF( ICOMPQ.EQ.1 ) THEN
237
ELSE IF( ICOMPQ.EQ.2 ) THEN
244
* If ICOMPQ = 0, use DLASDQ to compute the singular values.
246
IF( ICOMPQ.EQ.0 ) THEN
247
CALL DLASDQ( 'U', 0, N, 0, 0, 0, D, E, VT, LDVT, U, LDU, U,
248
$ LDU, WORK( WSTART ), INFO )
252
* If N is smaller than the minimum divide size SMLSIZ, then solve
253
* the problem with another solver.
255
IF( N.LE.SMLSIZ ) THEN
256
IF( ICOMPQ.EQ.2 ) THEN
257
CALL DLASET( 'A', N, N, ZERO, ONE, U, LDU )
258
CALL DLASET( 'A', N, N, ZERO, ONE, VT, LDVT )
259
CALL DLASDQ( 'U', 0, N, N, N, 0, D, E, VT, LDVT, U, LDU, U,
260
$ LDU, WORK( WSTART ), INFO )
261
ELSE IF( ICOMPQ.EQ.1 ) THEN
264
CALL DLASET( 'A', N, N, ZERO, ONE, Q( IU+( QSTART-1 )*N ),
266
CALL DLASET( 'A', N, N, ZERO, ONE, Q( IVT+( QSTART-1 )*N ),
268
CALL DLASDQ( 'U', 0, N, N, N, 0, D, E,
269
$ Q( IVT+( QSTART-1 )*N ), N,
270
$ Q( IU+( QSTART-1 )*N ), N,
271
$ Q( IU+( QSTART-1 )*N ), N, WORK( WSTART ),
277
IF( ICOMPQ.EQ.2 ) THEN
278
CALL DLASET( 'A', N, N, ZERO, ONE, U, LDU )
279
CALL DLASET( 'A', N, N, ZERO, ONE, VT, LDVT )
284
ORGNRM = DLANST( 'M', N, D, E )
287
CALL DLASCL( 'G', 0, 0, ORGNRM, ONE, N, 1, D, N, IERR )
288
CALL DLASCL( 'G', 0, 0, ORGNRM, ONE, NM1, 1, E, NM1, IERR )
290
EPS = DLAMCH( 'Epsilon' )
292
MLVL = INT( LOG( DBLE( N ) / DBLE( SMLSIZ+1 ) ) / LOG( TWO ) ) + 1
295
IF( ICOMPQ.EQ.1 ) THEN
304
GIVNUM = POLES + 2*MLVL
313
IF( ABS( D( I ) ).LT.EPS ) THEN
314
D( I ) = SIGN( EPS, D( I ) )
322
IF( ( ABS( E( I ) ).LT.EPS ) .OR. ( I.EQ.NM1 ) ) THEN
324
* Subproblem found. First determine its size and then
325
* apply divide and conquer on it.
329
* A subproblem with E(I) small for I < NM1.
331
NSIZE = I - START + 1
332
ELSE IF( ABS( E( I ) ).GE.EPS ) THEN
334
* A subproblem with E(NM1) not too small but I = NM1.
336
NSIZE = N - START + 1
339
* A subproblem with E(NM1) small. This implies an
340
* 1-by-1 subproblem at D(N). Solve this 1-by-1 problem
343
NSIZE = I - START + 1
344
IF( ICOMPQ.EQ.2 ) THEN
345
U( N, N ) = SIGN( ONE, D( N ) )
347
ELSE IF( ICOMPQ.EQ.1 ) THEN
348
Q( N+( QSTART-1 )*N ) = SIGN( ONE, D( N ) )
349
Q( N+( SMLSIZ+QSTART-1 )*N ) = ONE
351
D( N ) = ABS( D( N ) )
353
IF( ICOMPQ.EQ.2 ) THEN
354
CALL DLASD0( NSIZE, SQRE, D( START ), E( START ),
355
$ U( START, START ), LDU, VT( START, START ),
356
$ LDVT, SMLSIZ, IWORK, WORK( WSTART ), INFO )
358
CALL DLASDA( ICOMPQ, SMLSIZ, NSIZE, SQRE, D( START ),
359
$ E( START ), Q( START+( IU+QSTART-2 )*N ), N,
360
$ Q( START+( IVT+QSTART-2 )*N ),
361
$ IQ( START+K*N ), Q( START+( DIFL+QSTART-2 )*
362
$ N ), Q( START+( DIFR+QSTART-2 )*N ),
363
$ Q( START+( Z+QSTART-2 )*N ),
364
$ Q( START+( POLES+QSTART-2 )*N ),
365
$ IQ( START+GIVPTR*N ), IQ( START+GIVCOL*N ),
366
$ N, IQ( START+PERM*N ),
367
$ Q( START+( GIVNUM+QSTART-2 )*N ),
368
$ Q( START+( IC+QSTART-2 )*N ),
369
$ Q( START+( IS+QSTART-2 )*N ),
370
$ WORK( WSTART ), IWORK, INFO )
381
CALL DLASCL( 'G', 0, 0, ONE, ORGNRM, N, 1, D, N, IERR )
384
* Use Selection Sort to minimize swaps of singular vectors
391
IF( D( J ).GT.P ) THEN
399
IF( ICOMPQ.EQ.1 ) THEN
401
ELSE IF( ICOMPQ.EQ.2 ) THEN
402
CALL DSWAP( N, U( 1, I ), 1, U( 1, KK ), 1 )
403
CALL DSWAP( N, VT( I, 1 ), LDVT, VT( KK, 1 ), LDVT )
405
ELSE IF( ICOMPQ.EQ.1 ) THEN
410
* If ICOMPQ = 1, use IQ(N,1) as the indicator for UPLO
412
IF( ICOMPQ.EQ.1 ) THEN
413
IF( IUPLO.EQ.1 ) THEN
420
* If B is lower bidiagonal, update U by those Givens rotations
421
* which rotated B to be upper bidiagonal
423
IF( ( IUPLO.EQ.2 ) .AND. ( ICOMPQ.EQ.2 ) )
424
$ CALL DLASR( 'L', 'V', 'B', N, N, WORK( 1 ), WORK( N ), U, LDU )