You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbdsdc.f 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. *> \brief \b DBDSDC
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download DBDSDC + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dbdsdc.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dbdsdc.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dbdsdc.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE DBDSDC( UPLO, COMPQ, N, D, E, U, LDU, VT, LDVT, Q, IQ,
  22. * WORK, IWORK, INFO )
  23. *
  24. * .. Scalar Arguments ..
  25. * CHARACTER COMPQ, UPLO
  26. * INTEGER INFO, LDU, LDVT, N
  27. * ..
  28. * .. Array Arguments ..
  29. * INTEGER IQ( * ), IWORK( * )
  30. * DOUBLE PRECISION D( * ), E( * ), Q( * ), U( LDU, * ),
  31. * $ VT( LDVT, * ), WORK( * )
  32. * ..
  33. *
  34. *
  35. *> \par Purpose:
  36. * =============
  37. *>
  38. *> \verbatim
  39. *>
  40. *> DBDSDC computes the singular value decomposition (SVD) of a real
  41. *> N-by-N (upper or lower) bidiagonal matrix B: B = U * S * VT,
  42. *> using a divide and conquer method, where S is a diagonal matrix
  43. *> with non-negative diagonal elements (the singular values of B), and
  44. *> U and VT are orthogonal matrices of left and right singular vectors,
  45. *> respectively. DBDSDC can be used to compute all singular values,
  46. *> and optionally, singular vectors or singular vectors in compact form.
  47. *>
  48. *> The code currently calls DLASDQ if singular values only are desired.
  49. *> However, it can be slightly modified to compute singular values
  50. *> using the divide and conquer method.
  51. *> \endverbatim
  52. *
  53. * Arguments:
  54. * ==========
  55. *
  56. *> \param[in] UPLO
  57. *> \verbatim
  58. *> UPLO is CHARACTER*1
  59. *> = 'U': B is upper bidiagonal.
  60. *> = 'L': B is lower bidiagonal.
  61. *> \endverbatim
  62. *>
  63. *> \param[in] COMPQ
  64. *> \verbatim
  65. *> COMPQ is CHARACTER*1
  66. *> Specifies whether singular vectors are to be computed
  67. *> as follows:
  68. *> = 'N': Compute singular values only;
  69. *> = 'P': Compute singular values and compute singular
  70. *> vectors in compact form;
  71. *> = 'I': Compute singular values and singular vectors.
  72. *> \endverbatim
  73. *>
  74. *> \param[in] N
  75. *> \verbatim
  76. *> N is INTEGER
  77. *> The order of the matrix B. N >= 0.
  78. *> \endverbatim
  79. *>
  80. *> \param[in,out] D
  81. *> \verbatim
  82. *> D is DOUBLE PRECISION array, dimension (N)
  83. *> On entry, the n diagonal elements of the bidiagonal matrix B.
  84. *> On exit, if INFO=0, the singular values of B.
  85. *> \endverbatim
  86. *>
  87. *> \param[in,out] E
  88. *> \verbatim
  89. *> E is DOUBLE PRECISION array, dimension (N-1)
  90. *> On entry, the elements of E contain the offdiagonal
  91. *> elements of the bidiagonal matrix whose SVD is desired.
  92. *> On exit, E has been destroyed.
  93. *> \endverbatim
  94. *>
  95. *> \param[out] U
  96. *> \verbatim
  97. *> U is DOUBLE PRECISION array, dimension (LDU,N)
  98. *> If COMPQ = 'I', then:
  99. *> On exit, if INFO = 0, U contains the left singular vectors
  100. *> of the bidiagonal matrix.
  101. *> For other values of COMPQ, U is not referenced.
  102. *> \endverbatim
  103. *>
  104. *> \param[in] LDU
  105. *> \verbatim
  106. *> LDU is INTEGER
  107. *> The leading dimension of the array U. LDU >= 1.
  108. *> If singular vectors are desired, then LDU >= max( 1, N ).
  109. *> \endverbatim
  110. *>
  111. *> \param[out] VT
  112. *> \verbatim
  113. *> VT is DOUBLE PRECISION array, dimension (LDVT,N)
  114. *> If COMPQ = 'I', then:
  115. *> On exit, if INFO = 0, VT**T contains the right singular
  116. *> vectors of the bidiagonal matrix.
  117. *> For other values of COMPQ, VT is not referenced.
  118. *> \endverbatim
  119. *>
  120. *> \param[in] LDVT
  121. *> \verbatim
  122. *> LDVT is INTEGER
  123. *> The leading dimension of the array VT. LDVT >= 1.
  124. *> If singular vectors are desired, then LDVT >= max( 1, N ).
  125. *> \endverbatim
  126. *>
  127. *> \param[out] Q
  128. *> \verbatim
  129. *> Q is DOUBLE PRECISION array, dimension (LDQ)
  130. *> If COMPQ = 'P', then:
  131. *> On exit, if INFO = 0, Q and IQ contain the left
  132. *> and right singular vectors in a compact form,
  133. *> requiring O(N log N) space instead of 2*N**2.
  134. *> In particular, Q contains all the DOUBLE PRECISION data in
  135. *> LDQ >= N*(11 + 2*SMLSIZ + 8*INT(LOG_2(N/(SMLSIZ+1))))
  136. *> words of memory, where SMLSIZ is returned by ILAENV and
  137. *> is equal to the maximum size of the subproblems at the
  138. *> bottom of the computation tree (usually about 25).
  139. *> For other values of COMPQ, Q is not referenced.
  140. *> \endverbatim
  141. *>
  142. *> \param[out] IQ
  143. *> \verbatim
  144. *> IQ is INTEGER array, dimension (LDIQ)
  145. *> If COMPQ = 'P', then:
  146. *> On exit, if INFO = 0, Q and IQ contain the left
  147. *> and right singular vectors in a compact form,
  148. *> requiring O(N log N) space instead of 2*N**2.
  149. *> In particular, IQ contains all INTEGER data in
  150. *> LDIQ >= N*(3 + 3*INT(LOG_2(N/(SMLSIZ+1))))
  151. *> words of memory, where SMLSIZ is returned by ILAENV and
  152. *> is equal to the maximum size of the subproblems at the
  153. *> bottom of the computation tree (usually about 25).
  154. *> For other values of COMPQ, IQ is not referenced.
  155. *> \endverbatim
  156. *>
  157. *> \param[out] WORK
  158. *> \verbatim
  159. *> WORK is DOUBLE PRECISION array, dimension (MAX(1,LWORK))
  160. *> If COMPQ = 'N' then LWORK >= (4 * N).
  161. *> If COMPQ = 'P' then LWORK >= (6 * N).
  162. *> If COMPQ = 'I' then LWORK >= (3 * N**2 + 4 * N).
  163. *> \endverbatim
  164. *>
  165. *> \param[out] IWORK
  166. *> \verbatim
  167. *> IWORK is INTEGER array, dimension (8*N)
  168. *> \endverbatim
  169. *>
  170. *> \param[out] INFO
  171. *> \verbatim
  172. *> INFO is INTEGER
  173. *> = 0: successful exit.
  174. *> < 0: if INFO = -i, the i-th argument had an illegal value.
  175. *> > 0: The algorithm failed to compute a singular value.
  176. *> The update process of divide and conquer failed.
  177. *> \endverbatim
  178. *
  179. * Authors:
  180. * ========
  181. *
  182. *> \author Univ. of Tennessee
  183. *> \author Univ. of California Berkeley
  184. *> \author Univ. of Colorado Denver
  185. *> \author NAG Ltd.
  186. *
  187. *> \ingroup auxOTHERcomputational
  188. *
  189. *> \par Contributors:
  190. * ==================
  191. *>
  192. *> Ming Gu and Huan Ren, Computer Science Division, University of
  193. *> California at Berkeley, USA
  194. *>
  195. * =====================================================================
  196. SUBROUTINE DBDSDC( UPLO, COMPQ, N, D, E, U, LDU, VT, LDVT, Q, IQ,
  197. $ WORK, IWORK, INFO )
  198. *
  199. * -- LAPACK computational routine --
  200. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  201. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  202. *
  203. * .. Scalar Arguments ..
  204. CHARACTER COMPQ, UPLO
  205. INTEGER INFO, LDU, LDVT, N
  206. * ..
  207. * .. Array Arguments ..
  208. INTEGER IQ( * ), IWORK( * )
  209. DOUBLE PRECISION D( * ), E( * ), Q( * ), U( LDU, * ),
  210. $ VT( LDVT, * ), WORK( * )
  211. * ..
  212. *
  213. * =====================================================================
  214. * Changed dimension statement in comment describing E from (N) to
  215. * (N-1). Sven, 17 Feb 05.
  216. * =====================================================================
  217. *
  218. * .. Parameters ..
  219. DOUBLE PRECISION ZERO, ONE, TWO
  220. PARAMETER ( ZERO = 0.0D+0, ONE = 1.0D+0, TWO = 2.0D+0 )
  221. * ..
  222. * .. Local Scalars ..
  223. INTEGER DIFL, DIFR, GIVCOL, GIVNUM, GIVPTR, I, IC,
  224. $ ICOMPQ, IERR, II, IS, IU, IUPLO, IVT, J, K, KK,
  225. $ MLVL, NM1, NSIZE, PERM, POLES, QSTART, SMLSIZ,
  226. $ SMLSZP, SQRE, START, WSTART, Z
  227. DOUBLE PRECISION CS, EPS, ORGNRM, P, R, SN
  228. * ..
  229. * .. External Functions ..
  230. LOGICAL LSAME
  231. INTEGER ILAENV
  232. DOUBLE PRECISION DLAMCH, DLANST
  233. EXTERNAL LSAME, ILAENV, DLAMCH, DLANST
  234. * ..
  235. * .. External Subroutines ..
  236. EXTERNAL DCOPY, DLARTG, DLASCL, DLASD0, DLASDA, DLASDQ,
  237. $ DLASET, DLASR, DSWAP, XERBLA
  238. * ..
  239. * .. Intrinsic Functions ..
  240. INTRINSIC ABS, DBLE, INT, LOG, SIGN
  241. * ..
  242. * .. Executable Statements ..
  243. *
  244. * Test the input parameters.
  245. *
  246. INFO = 0
  247. *
  248. IUPLO = 0
  249. IF( LSAME( UPLO, 'U' ) )
  250. $ IUPLO = 1
  251. IF( LSAME( UPLO, 'L' ) )
  252. $ IUPLO = 2
  253. IF( LSAME( COMPQ, 'N' ) ) THEN
  254. ICOMPQ = 0
  255. ELSE IF( LSAME( COMPQ, 'P' ) ) THEN
  256. ICOMPQ = 1
  257. ELSE IF( LSAME( COMPQ, 'I' ) ) THEN
  258. ICOMPQ = 2
  259. ELSE
  260. ICOMPQ = -1
  261. END IF
  262. IF( IUPLO.EQ.0 ) THEN
  263. INFO = -1
  264. ELSE IF( ICOMPQ.LT.0 ) THEN
  265. INFO = -2
  266. ELSE IF( N.LT.0 ) THEN
  267. INFO = -3
  268. ELSE IF( ( LDU.LT.1 ) .OR. ( ( ICOMPQ.EQ.2 ) .AND. ( LDU.LT.
  269. $ N ) ) ) THEN
  270. INFO = -7
  271. ELSE IF( ( LDVT.LT.1 ) .OR. ( ( ICOMPQ.EQ.2 ) .AND. ( LDVT.LT.
  272. $ N ) ) ) THEN
  273. INFO = -9
  274. END IF
  275. IF( INFO.NE.0 ) THEN
  276. CALL XERBLA( 'DBDSDC', -INFO )
  277. RETURN
  278. END IF
  279. *
  280. * Quick return if possible
  281. *
  282. IF( N.EQ.0 )
  283. $ RETURN
  284. SMLSIZ = ILAENV( 9, 'DBDSDC', ' ', 0, 0, 0, 0 )
  285. IF( N.EQ.1 ) THEN
  286. IF( ICOMPQ.EQ.1 ) THEN
  287. Q( 1 ) = SIGN( ONE, D( 1 ) )
  288. Q( 1+SMLSIZ*N ) = ONE
  289. ELSE IF( ICOMPQ.EQ.2 ) THEN
  290. U( 1, 1 ) = SIGN( ONE, D( 1 ) )
  291. VT( 1, 1 ) = ONE
  292. END IF
  293. D( 1 ) = ABS( D( 1 ) )
  294. RETURN
  295. END IF
  296. NM1 = N - 1
  297. *
  298. * If matrix lower bidiagonal, rotate to be upper bidiagonal
  299. * by applying Givens rotations on the left
  300. *
  301. WSTART = 1
  302. QSTART = 3
  303. IF( ICOMPQ.EQ.1 ) THEN
  304. CALL DCOPY( N, D, 1, Q( 1 ), 1 )
  305. CALL DCOPY( N-1, E, 1, Q( N+1 ), 1 )
  306. END IF
  307. IF( IUPLO.EQ.2 ) THEN
  308. QSTART = 5
  309. IF( ICOMPQ .EQ. 2 ) WSTART = 2*N - 1
  310. DO 10 I = 1, N - 1
  311. CALL DLARTG( D( I ), E( I ), CS, SN, R )
  312. D( I ) = R
  313. E( I ) = SN*D( I+1 )
  314. D( I+1 ) = CS*D( I+1 )
  315. IF( ICOMPQ.EQ.1 ) THEN
  316. Q( I+2*N ) = CS
  317. Q( I+3*N ) = SN
  318. ELSE IF( ICOMPQ.EQ.2 ) THEN
  319. WORK( I ) = CS
  320. WORK( NM1+I ) = -SN
  321. END IF
  322. 10 CONTINUE
  323. END IF
  324. *
  325. * If ICOMPQ = 0, use DLASDQ to compute the singular values.
  326. *
  327. IF( ICOMPQ.EQ.0 ) THEN
  328. * Ignore WSTART, instead using WORK( 1 ), since the two vectors
  329. * for CS and -SN above are added only if ICOMPQ == 2,
  330. * and adding them exceeds documented WORK size of 4*n.
  331. CALL DLASDQ( 'U', 0, N, 0, 0, 0, D, E, VT, LDVT, U, LDU, U,
  332. $ LDU, WORK( 1 ), INFO )
  333. GO TO 40
  334. END IF
  335. *
  336. * If N is smaller than the minimum divide size SMLSIZ, then solve
  337. * the problem with another solver.
  338. *
  339. IF( N.LE.SMLSIZ ) THEN
  340. IF( ICOMPQ.EQ.2 ) THEN
  341. CALL DLASET( 'A', N, N, ZERO, ONE, U, LDU )
  342. CALL DLASET( 'A', N, N, ZERO, ONE, VT, LDVT )
  343. CALL DLASDQ( 'U', 0, N, N, N, 0, D, E, VT, LDVT, U, LDU, U,
  344. $ LDU, WORK( WSTART ), INFO )
  345. ELSE IF( ICOMPQ.EQ.1 ) THEN
  346. IU = 1
  347. IVT = IU + N
  348. CALL DLASET( 'A', N, N, ZERO, ONE, Q( IU+( QSTART-1 )*N ),
  349. $ N )
  350. CALL DLASET( 'A', N, N, ZERO, ONE, Q( IVT+( QSTART-1 )*N ),
  351. $ N )
  352. CALL DLASDQ( 'U', 0, N, N, N, 0, D, E,
  353. $ Q( IVT+( QSTART-1 )*N ), N,
  354. $ Q( IU+( QSTART-1 )*N ), N,
  355. $ Q( IU+( QSTART-1 )*N ), N, WORK( WSTART ),
  356. $ INFO )
  357. END IF
  358. GO TO 40
  359. END IF
  360. *
  361. IF( ICOMPQ.EQ.2 ) THEN
  362. CALL DLASET( 'A', N, N, ZERO, ONE, U, LDU )
  363. CALL DLASET( 'A', N, N, ZERO, ONE, VT, LDVT )
  364. END IF
  365. *
  366. * Scale.
  367. *
  368. ORGNRM = DLANST( 'M', N, D, E )
  369. IF( ORGNRM.EQ.ZERO )
  370. $ RETURN
  371. CALL DLASCL( 'G', 0, 0, ORGNRM, ONE, N, 1, D, N, IERR )
  372. CALL DLASCL( 'G', 0, 0, ORGNRM, ONE, NM1, 1, E, NM1, IERR )
  373. *
  374. EPS = (0.9D+0)*DLAMCH( 'Epsilon' )
  375. *
  376. MLVL = INT( LOG( DBLE( N ) / DBLE( SMLSIZ+1 ) ) / LOG( TWO ) ) + 1
  377. SMLSZP = SMLSIZ + 1
  378. *
  379. IF( ICOMPQ.EQ.1 ) THEN
  380. IU = 1
  381. IVT = 1 + SMLSIZ
  382. DIFL = IVT + SMLSZP
  383. DIFR = DIFL + MLVL
  384. Z = DIFR + MLVL*2
  385. IC = Z + MLVL
  386. IS = IC + 1
  387. POLES = IS + 1
  388. GIVNUM = POLES + 2*MLVL
  389. *
  390. K = 1
  391. GIVPTR = 2
  392. PERM = 3
  393. GIVCOL = PERM + MLVL
  394. END IF
  395. *
  396. DO 20 I = 1, N
  397. IF( ABS( D( I ) ).LT.EPS ) THEN
  398. D( I ) = SIGN( EPS, D( I ) )
  399. END IF
  400. 20 CONTINUE
  401. *
  402. START = 1
  403. SQRE = 0
  404. *
  405. DO 30 I = 1, NM1
  406. IF( ( ABS( E( I ) ).LT.EPS ) .OR. ( I.EQ.NM1 ) ) THEN
  407. *
  408. * Subproblem found. First determine its size and then
  409. * apply divide and conquer on it.
  410. *
  411. IF( I.LT.NM1 ) THEN
  412. *
  413. * A subproblem with E(I) small for I < NM1.
  414. *
  415. NSIZE = I - START + 1
  416. ELSE IF( ABS( E( I ) ).GE.EPS ) THEN
  417. *
  418. * A subproblem with E(NM1) not too small but I = NM1.
  419. *
  420. NSIZE = N - START + 1
  421. ELSE
  422. *
  423. * A subproblem with E(NM1) small. This implies an
  424. * 1-by-1 subproblem at D(N). Solve this 1-by-1 problem
  425. * first.
  426. *
  427. NSIZE = I - START + 1
  428. IF( ICOMPQ.EQ.2 ) THEN
  429. U( N, N ) = SIGN( ONE, D( N ) )
  430. VT( N, N ) = ONE
  431. ELSE IF( ICOMPQ.EQ.1 ) THEN
  432. Q( N+( QSTART-1 )*N ) = SIGN( ONE, D( N ) )
  433. Q( N+( SMLSIZ+QSTART-1 )*N ) = ONE
  434. END IF
  435. D( N ) = ABS( D( N ) )
  436. END IF
  437. IF( ICOMPQ.EQ.2 ) THEN
  438. CALL DLASD0( NSIZE, SQRE, D( START ), E( START ),
  439. $ U( START, START ), LDU, VT( START, START ),
  440. $ LDVT, SMLSIZ, IWORK, WORK( WSTART ), INFO )
  441. ELSE
  442. CALL DLASDA( ICOMPQ, SMLSIZ, NSIZE, SQRE, D( START ),
  443. $ E( START ), Q( START+( IU+QSTART-2 )*N ), N,
  444. $ Q( START+( IVT+QSTART-2 )*N ),
  445. $ IQ( START+K*N ), Q( START+( DIFL+QSTART-2 )*
  446. $ N ), Q( START+( DIFR+QSTART-2 )*N ),
  447. $ Q( START+( Z+QSTART-2 )*N ),
  448. $ Q( START+( POLES+QSTART-2 )*N ),
  449. $ IQ( START+GIVPTR*N ), IQ( START+GIVCOL*N ),
  450. $ N, IQ( START+PERM*N ),
  451. $ Q( START+( GIVNUM+QSTART-2 )*N ),
  452. $ Q( START+( IC+QSTART-2 )*N ),
  453. $ Q( START+( IS+QSTART-2 )*N ),
  454. $ WORK( WSTART ), IWORK, INFO )
  455. END IF
  456. IF( INFO.NE.0 ) THEN
  457. RETURN
  458. END IF
  459. START = I + 1
  460. END IF
  461. 30 CONTINUE
  462. *
  463. * Unscale
  464. *
  465. CALL DLASCL( 'G', 0, 0, ONE, ORGNRM, N, 1, D, N, IERR )
  466. 40 CONTINUE
  467. *
  468. * Use Selection Sort to minimize swaps of singular vectors
  469. *
  470. DO 60 II = 2, N
  471. I = II - 1
  472. KK = I
  473. P = D( I )
  474. DO 50 J = II, N
  475. IF( D( J ).GT.P ) THEN
  476. KK = J
  477. P = D( J )
  478. END IF
  479. 50 CONTINUE
  480. IF( KK.NE.I ) THEN
  481. D( KK ) = D( I )
  482. D( I ) = P
  483. IF( ICOMPQ.EQ.1 ) THEN
  484. IQ( I ) = KK
  485. ELSE IF( ICOMPQ.EQ.2 ) THEN
  486. CALL DSWAP( N, U( 1, I ), 1, U( 1, KK ), 1 )
  487. CALL DSWAP( N, VT( I, 1 ), LDVT, VT( KK, 1 ), LDVT )
  488. END IF
  489. ELSE IF( ICOMPQ.EQ.1 ) THEN
  490. IQ( I ) = I
  491. END IF
  492. 60 CONTINUE
  493. *
  494. * If ICOMPQ = 1, use IQ(N,1) as the indicator for UPLO
  495. *
  496. IF( ICOMPQ.EQ.1 ) THEN
  497. IF( IUPLO.EQ.1 ) THEN
  498. IQ( N ) = 1
  499. ELSE
  500. IQ( N ) = 0
  501. END IF
  502. END IF
  503. *
  504. * If B is lower bidiagonal, update U by those Givens rotations
  505. * which rotated B to be upper bidiagonal
  506. *
  507. IF( ( IUPLO.EQ.2 ) .AND. ( ICOMPQ.EQ.2 ) )
  508. $ CALL DLASR( 'L', 'V', 'B', N, N, WORK( 1 ), WORK( N ), U, LDU )
  509. *
  510. RETURN
  511. *
  512. * End of DBDSDC
  513. *
  514. END