1 *> \brief \b DLALSD uses the singular value decomposition of A to solve the least squares problem.
3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download DLALSD + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dlalsd.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dlalsd.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dlalsd.f">
21 * SUBROUTINE DLALSD( UPLO, SMLSIZ, N, NRHS, D, E, B, LDB, RCOND,
22 * RANK, WORK, IWORK, INFO )
24 * .. Scalar Arguments ..
26 * INTEGER INFO, LDB, N, NRHS, RANK, SMLSIZ
27 * DOUBLE PRECISION RCOND
29 * .. Array Arguments ..
31 * DOUBLE PRECISION B( LDB, * ), D( * ), E( * ), WORK( * )
40 *> DLALSD uses the singular value decomposition of A to solve the least
41 *> squares problem of finding X to minimize the Euclidean norm of each
42 *> column of A*X-B, where A is N-by-N upper bidiagonal, and X and B
43 *> are N-by-NRHS. The solution X overwrites B.
45 *> The singular values of A smaller than RCOND times the largest
46 *> singular value are treated as zero in solving the least squares
47 *> problem; in this case a minimum norm solution is returned.
48 *> The actual singular values are returned in D in ascending order.
50 *> This code makes very mild assumptions about floating point
51 *> arithmetic. It will work on machines with a guard digit in
52 *> add/subtract, or on those binary machines without guard digits
53 *> which subtract like the Cray XMP, Cray YMP, Cray C 90, or Cray 2.
54 *> It could conceivably fail on hexadecimal or decimal machines
55 *> without guard digits, but we know of none.
63 *> UPLO is CHARACTER*1
64 *> = 'U': D and E define an upper bidiagonal matrix.
65 *> = 'L': D and E define a lower bidiagonal matrix.
71 *> The maximum size of the subproblems at the bottom of the
78 *> The dimension of the bidiagonal matrix. N >= 0.
84 *> The number of columns of B. NRHS must be at least 1.
89 *> D is DOUBLE PRECISION array, dimension (N)
90 *> On entry D contains the main diagonal of the bidiagonal
91 *> matrix. On exit, if INFO = 0, D contains its singular values.
96 *> E is DOUBLE PRECISION array, dimension (N-1)
97 *> Contains the super-diagonal entries of the bidiagonal matrix.
98 *> On exit, E has been destroyed.
103 *> B is DOUBLE PRECISION array, dimension (LDB,NRHS)
104 *> On input, B contains the right hand sides of the least
105 *> squares problem. On output, B contains the solution X.
111 *> The leading dimension of B in the calling subprogram.
112 *> LDB must be at least max(1,N).
117 *> RCOND is DOUBLE PRECISION
118 *> The singular values of A less than or equal to RCOND times
119 *> the largest singular value are treated as zero in solving
120 *> the least squares problem. If RCOND is negative,
121 *> machine precision is used instead.
122 *> For example, if diag(S)*X=B were the least squares problem,
123 *> where diag(S) is a diagonal matrix of singular values, the
124 *> solution would be X(i) = B(i) / S(i) if S(i) is greater than
125 *> RCOND*max(S), and X(i) = 0 if S(i) is less than or equal to
132 *> The number of singular values of A greater than RCOND times
133 *> the largest singular value.
138 *> WORK is DOUBLE PRECISION array, dimension at least
139 *> (9*N + 2*N*SMLSIZ + 8*N*NLVL + N*NRHS + (SMLSIZ+1)**2),
140 *> where NLVL = max(0, INT(log_2 (N/(SMLSIZ+1))) + 1).
145 *> IWORK is INTEGER array, dimension at least
152 *> = 0: successful exit.
153 *> < 0: if INFO = -i, the i-th argument had an illegal value.
154 *> > 0: The algorithm failed to compute a singular value while
155 *> working on the submatrix lying in rows and columns
156 *> INFO/(N+1) through MOD(INFO,N+1).
162 *> \author Univ. of Tennessee
163 *> \author Univ. of California Berkeley
164 *> \author Univ. of Colorado Denver
167 *> \date September 2012
169 *> \ingroup doubleOTHERcomputational
171 *> \par Contributors:
174 *> Ming Gu and Ren-Cang Li, Computer Science Division, University of
175 *> California at Berkeley, USA \n
176 *> Osni Marques, LBNL/NERSC, USA \n
178 * =====================================================================
179 SUBROUTINE DLALSD( UPLO, SMLSIZ, N, NRHS, D, E, B, LDB, RCOND,
180 $ RANK, WORK, IWORK, INFO )
182 * -- LAPACK computational routine (version 3.4.2) --
183 * -- LAPACK is a software package provided by Univ. of Tennessee, --
184 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
187 * .. Scalar Arguments ..
189 INTEGER INFO, LDB, N, NRHS, RANK, SMLSIZ
190 DOUBLE PRECISION RCOND
192 * .. Array Arguments ..
194 DOUBLE PRECISION B( LDB, * ), D( * ), E( * ), WORK( * )
197 * =====================================================================
200 DOUBLE PRECISION ZERO, ONE, TWO
201 PARAMETER ( ZERO = 0.0D0, ONE = 1.0D0, TWO = 2.0D0 )
203 * .. Local Scalars ..
204 INTEGER BX, BXST, C, DIFL, DIFR, GIVCOL, GIVNUM,
205 $ GIVPTR, I, ICMPQ1, ICMPQ2, IWK, J, K, NLVL,
206 $ NM1, NSIZE, NSUB, NWORK, PERM, POLES, S, SIZEI,
207 $ SMLSZP, SQRE, ST, ST1, U, VT, Z
208 DOUBLE PRECISION CS, EPS, ORGNRM, R, RCND, SN, TOL
210 * .. External Functions ..
212 DOUBLE PRECISION DLAMCH, DLANST
213 EXTERNAL IDAMAX, DLAMCH, DLANST
215 * .. External Subroutines ..
216 EXTERNAL DCOPY, DGEMM, DLACPY, DLALSA, DLARTG, DLASCL,
217 $ DLASDA, DLASDQ, DLASET, DLASRT, DROT, XERBLA
219 * .. Intrinsic Functions ..
220 INTRINSIC ABS, DBLE, INT, LOG, SIGN
222 * .. Executable Statements ..
224 * Test the input parameters.
230 ELSE IF( NRHS.LT.1 ) THEN
232 ELSE IF( ( LDB.LT.1 ) .OR. ( LDB.LT.N ) ) THEN
236 CALL XERBLA( 'DLALSD', -INFO )
240 EPS = DLAMCH( 'Epsilon' )
242 * Set up the tolerance.
244 IF( ( RCOND.LE.ZERO ) .OR. ( RCOND.GE.ONE ) ) THEN
252 * Quick return if possible.
256 ELSE IF( N.EQ.1 ) THEN
257 IF( D( 1 ).EQ.ZERO ) THEN
258 CALL DLASET( 'A', 1, NRHS, ZERO, ZERO, B, LDB )
261 CALL DLASCL( 'G', 0, 0, D( 1 ), ONE, 1, NRHS, B, LDB, INFO )
262 D( 1 ) = ABS( D( 1 ) )
267 * Rotate the matrix if it is lower bidiagonal.
269 IF( UPLO.EQ.'L' ) THEN
271 CALL DLARTG( D( I ), E( I ), CS, SN, R )
274 D( I+1 ) = CS*D( I+1 )
276 CALL DROT( 1, B( I, 1 ), 1, B( I+1, 1 ), 1, CS, SN )
287 CALL DROT( 1, B( J, I ), 1, B( J+1, I ), 1, CS, SN )
296 ORGNRM = DLANST( 'M', N, D, E )
297 IF( ORGNRM.EQ.ZERO ) THEN
298 CALL DLASET( 'A', N, NRHS, ZERO, ZERO, B, LDB )
302 CALL DLASCL( 'G', 0, 0, ORGNRM, ONE, N, 1, D, N, INFO )
303 CALL DLASCL( 'G', 0, 0, ORGNRM, ONE, NM1, 1, E, NM1, INFO )
305 * If N is smaller than the minimum divide size SMLSIZ, then solve
306 * the problem with another solver.
308 IF( N.LE.SMLSIZ ) THEN
310 CALL DLASET( 'A', N, N, ZERO, ONE, WORK, N )
311 CALL DLASDQ( 'U', 0, N, N, 0, NRHS, D, E, WORK, N, WORK, N, B,
312 $ LDB, WORK( NWORK ), INFO )
316 TOL = RCND*ABS( D( IDAMAX( N, D, 1 ) ) )
318 IF( D( I ).LE.TOL ) THEN
319 CALL DLASET( 'A', 1, NRHS, ZERO, ZERO, B( I, 1 ), LDB )
321 CALL DLASCL( 'G', 0, 0, D( I ), ONE, 1, NRHS, B( I, 1 ),
326 CALL DGEMM( 'T', 'N', N, NRHS, N, ONE, WORK, N, B, LDB, ZERO,
328 CALL DLACPY( 'A', N, NRHS, WORK( NWORK ), N, B, LDB )
332 CALL DLASCL( 'G', 0, 0, ONE, ORGNRM, N, 1, D, N, INFO )
333 CALL DLASRT( 'D', N, D, INFO )
334 CALL DLASCL( 'G', 0, 0, ORGNRM, ONE, N, NRHS, B, LDB, INFO )
339 * Book-keeping and setting up some constants.
341 NLVL = INT( LOG( DBLE( N ) / DBLE( SMLSIZ+1 ) ) / LOG( TWO ) ) + 1
353 GIVNUM = POLES + 2*NLVL*N
354 BX = GIVNUM + 2*NLVL*N
361 GIVCOL = PERM + NLVL*N
362 IWK = GIVCOL + NLVL*N*2
371 IF( ABS( D( I ) ).LT.EPS ) THEN
372 D( I ) = SIGN( EPS, D( I ) )
377 IF( ( ABS( E( I ) ).LT.EPS ) .OR. ( I.EQ.NM1 ) ) THEN
381 * Subproblem found. First determine its size and then
382 * apply divide and conquer on it.
386 * A subproblem with E(I) small for I < NM1.
389 IWORK( SIZEI+NSUB-1 ) = NSIZE
390 ELSE IF( ABS( E( I ) ).GE.EPS ) THEN
392 * A subproblem with E(NM1) not too small but I = NM1.
395 IWORK( SIZEI+NSUB-1 ) = NSIZE
398 * A subproblem with E(NM1) small. This implies an
399 * 1-by-1 subproblem at D(N), which is not solved
403 IWORK( SIZEI+NSUB-1 ) = NSIZE
406 IWORK( SIZEI+NSUB-1 ) = 1
407 CALL DCOPY( NRHS, B( N, 1 ), LDB, WORK( BX+NM1 ), N )
410 IF( NSIZE.EQ.1 ) THEN
412 * This is a 1-by-1 subproblem and is not solved
415 CALL DCOPY( NRHS, B( ST, 1 ), LDB, WORK( BX+ST1 ), N )
416 ELSE IF( NSIZE.LE.SMLSIZ ) THEN
418 * This is a small subproblem and is solved by DLASDQ.
420 CALL DLASET( 'A', NSIZE, NSIZE, ZERO, ONE,
421 $ WORK( VT+ST1 ), N )
422 CALL DLASDQ( 'U', 0, NSIZE, NSIZE, 0, NRHS, D( ST ),
423 $ E( ST ), WORK( VT+ST1 ), N, WORK( NWORK ),
424 $ N, B( ST, 1 ), LDB, WORK( NWORK ), INFO )
428 CALL DLACPY( 'A', NSIZE, NRHS, B( ST, 1 ), LDB,
429 $ WORK( BX+ST1 ), N )
432 * A large problem. Solve it using divide and conquer.
434 CALL DLASDA( ICMPQ1, SMLSIZ, NSIZE, SQRE, D( ST ),
435 $ E( ST ), WORK( U+ST1 ), N, WORK( VT+ST1 ),
436 $ IWORK( K+ST1 ), WORK( DIFL+ST1 ),
437 $ WORK( DIFR+ST1 ), WORK( Z+ST1 ),
438 $ WORK( POLES+ST1 ), IWORK( GIVPTR+ST1 ),
439 $ IWORK( GIVCOL+ST1 ), N, IWORK( PERM+ST1 ),
440 $ WORK( GIVNUM+ST1 ), WORK( C+ST1 ),
441 $ WORK( S+ST1 ), WORK( NWORK ), IWORK( IWK ),
447 CALL DLALSA( ICMPQ2, SMLSIZ, NSIZE, NRHS, B( ST, 1 ),
448 $ LDB, WORK( BXST ), N, WORK( U+ST1 ), N,
449 $ WORK( VT+ST1 ), IWORK( K+ST1 ),
450 $ WORK( DIFL+ST1 ), WORK( DIFR+ST1 ),
451 $ WORK( Z+ST1 ), WORK( POLES+ST1 ),
452 $ IWORK( GIVPTR+ST1 ), IWORK( GIVCOL+ST1 ), N,
453 $ IWORK( PERM+ST1 ), WORK( GIVNUM+ST1 ),
454 $ WORK( C+ST1 ), WORK( S+ST1 ), WORK( NWORK ),
455 $ IWORK( IWK ), INFO )
464 * Apply the singular values and treat the tiny ones as zero.
466 TOL = RCND*ABS( D( IDAMAX( N, D, 1 ) ) )
470 * Some of the elements in D can be negative because 1-by-1
471 * subproblems were not solved explicitly.
473 IF( ABS( D( I ) ).LE.TOL ) THEN
474 CALL DLASET( 'A', 1, NRHS, ZERO, ZERO, WORK( BX+I-1 ), N )
477 CALL DLASCL( 'G', 0, 0, D( I ), ONE, 1, NRHS,
478 $ WORK( BX+I-1 ), N, INFO )
480 D( I ) = ABS( D( I ) )
483 * Now apply back the right singular vectors.
489 NSIZE = IWORK( SIZEI+I-1 )
491 IF( NSIZE.EQ.1 ) THEN
492 CALL DCOPY( NRHS, WORK( BXST ), N, B( ST, 1 ), LDB )
493 ELSE IF( NSIZE.LE.SMLSIZ ) THEN
494 CALL DGEMM( 'T', 'N', NSIZE, NRHS, NSIZE, ONE,
495 $ WORK( VT+ST1 ), N, WORK( BXST ), N, ZERO,
498 CALL DLALSA( ICMPQ2, SMLSIZ, NSIZE, NRHS, WORK( BXST ), N,
499 $ B( ST, 1 ), LDB, WORK( U+ST1 ), N,
500 $ WORK( VT+ST1 ), IWORK( K+ST1 ),
501 $ WORK( DIFL+ST1 ), WORK( DIFR+ST1 ),
502 $ WORK( Z+ST1 ), WORK( POLES+ST1 ),
503 $ IWORK( GIVPTR+ST1 ), IWORK( GIVCOL+ST1 ), N,
504 $ IWORK( PERM+ST1 ), WORK( GIVNUM+ST1 ),
505 $ WORK( C+ST1 ), WORK( S+ST1 ), WORK( NWORK ),
506 $ IWORK( IWK ), INFO )
513 * Unscale and sort the singular values.
515 CALL DLASCL( 'G', 0, 0, ONE, ORGNRM, N, 1, D, N, INFO )
516 CALL DLASRT( 'D', N, D, INFO )
517 CALL DLASCL( 'G', 0, 0, ORGNRM, ONE, N, NRHS, B, LDB, INFO )