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.

zbdsqr.f 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. *> \brief \b ZBDSQR
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download ZBDSQR + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zbdsqr.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zbdsqr.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zbdsqr.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE ZBDSQR( UPLO, N, NCVT, NRU, NCC, D, E, VT, LDVT, U,
  22. * LDU, C, LDC, RWORK, INFO )
  23. *
  24. * .. Scalar Arguments ..
  25. * CHARACTER UPLO
  26. * INTEGER INFO, LDC, LDU, LDVT, N, NCC, NCVT, NRU
  27. * ..
  28. * .. Array Arguments ..
  29. * DOUBLE PRECISION D( * ), E( * ), RWORK( * )
  30. * COMPLEX*16 C( LDC, * ), U( LDU, * ), VT( LDVT, * )
  31. * ..
  32. *
  33. *
  34. *> \par Purpose:
  35. * =============
  36. *>
  37. *> \verbatim
  38. *>
  39. *> ZBDSQR computes the singular values and, optionally, the right and/or
  40. *> left singular vectors from the singular value decomposition (SVD) of
  41. *> a real N-by-N (upper or lower) bidiagonal matrix B using the implicit
  42. *> zero-shift QR algorithm. The SVD of B has the form
  43. *>
  44. *> B = Q * S * P**H
  45. *>
  46. *> where S is the diagonal matrix of singular values, Q is an orthogonal
  47. *> matrix of left singular vectors, and P is an orthogonal matrix of
  48. *> right singular vectors. If left singular vectors are requested, this
  49. *> subroutine actually returns U*Q instead of Q, and, if right singular
  50. *> vectors are requested, this subroutine returns P**H*VT instead of
  51. *> P**H, for given complex input matrices U and VT. When U and VT are
  52. *> the unitary matrices that reduce a general matrix A to bidiagonal
  53. *> form: A = U*B*VT, as computed by ZGEBRD, then
  54. *>
  55. *> A = (U*Q) * S * (P**H*VT)
  56. *>
  57. *> is the SVD of A. Optionally, the subroutine may also compute Q**H*C
  58. *> for a given complex input matrix C.
  59. *>
  60. *> See "Computing Small Singular Values of Bidiagonal Matrices With
  61. *> Guaranteed High Relative Accuracy," by J. Demmel and W. Kahan,
  62. *> LAPACK Working Note #3 (or SIAM J. Sci. Statist. Comput. vol. 11,
  63. *> no. 5, pp. 873-912, Sept 1990) and
  64. *> "Accurate singular values and differential qd algorithms," by
  65. *> B. Parlett and V. Fernando, Technical Report CPAM-554, Mathematics
  66. *> Department, University of California at Berkeley, July 1992
  67. *> for a detailed description of the algorithm.
  68. *> \endverbatim
  69. *
  70. * Arguments:
  71. * ==========
  72. *
  73. *> \param[in] UPLO
  74. *> \verbatim
  75. *> UPLO is CHARACTER*1
  76. *> = 'U': B is upper bidiagonal;
  77. *> = 'L': B is lower bidiagonal.
  78. *> \endverbatim
  79. *>
  80. *> \param[in] N
  81. *> \verbatim
  82. *> N is INTEGER
  83. *> The order of the matrix B. N >= 0.
  84. *> \endverbatim
  85. *>
  86. *> \param[in] NCVT
  87. *> \verbatim
  88. *> NCVT is INTEGER
  89. *> The number of columns of the matrix VT. NCVT >= 0.
  90. *> \endverbatim
  91. *>
  92. *> \param[in] NRU
  93. *> \verbatim
  94. *> NRU is INTEGER
  95. *> The number of rows of the matrix U. NRU >= 0.
  96. *> \endverbatim
  97. *>
  98. *> \param[in] NCC
  99. *> \verbatim
  100. *> NCC is INTEGER
  101. *> The number of columns of the matrix C. NCC >= 0.
  102. *> \endverbatim
  103. *>
  104. *> \param[in,out] D
  105. *> \verbatim
  106. *> D is DOUBLE PRECISION array, dimension (N)
  107. *> On entry, the n diagonal elements of the bidiagonal matrix B.
  108. *> On exit, if INFO=0, the singular values of B in decreasing
  109. *> order.
  110. *> \endverbatim
  111. *>
  112. *> \param[in,out] E
  113. *> \verbatim
  114. *> E is DOUBLE PRECISION array, dimension (N-1)
  115. *> On entry, the N-1 offdiagonal elements of the bidiagonal
  116. *> matrix B.
  117. *> On exit, if INFO = 0, E is destroyed; if INFO > 0, D and E
  118. *> will contain the diagonal and superdiagonal elements of a
  119. *> bidiagonal matrix orthogonally equivalent to the one given
  120. *> as input.
  121. *> \endverbatim
  122. *>
  123. *> \param[in,out] VT
  124. *> \verbatim
  125. *> VT is COMPLEX*16 array, dimension (LDVT, NCVT)
  126. *> On entry, an N-by-NCVT matrix VT.
  127. *> On exit, VT is overwritten by P**H * VT.
  128. *> Not referenced if NCVT = 0.
  129. *> \endverbatim
  130. *>
  131. *> \param[in] LDVT
  132. *> \verbatim
  133. *> LDVT is INTEGER
  134. *> The leading dimension of the array VT.
  135. *> LDVT >= max(1,N) if NCVT > 0; LDVT >= 1 if NCVT = 0.
  136. *> \endverbatim
  137. *>
  138. *> \param[in,out] U
  139. *> \verbatim
  140. *> U is COMPLEX*16 array, dimension (LDU, N)
  141. *> On entry, an NRU-by-N matrix U.
  142. *> On exit, U is overwritten by U * Q.
  143. *> Not referenced if NRU = 0.
  144. *> \endverbatim
  145. *>
  146. *> \param[in] LDU
  147. *> \verbatim
  148. *> LDU is INTEGER
  149. *> The leading dimension of the array U. LDU >= max(1,NRU).
  150. *> \endverbatim
  151. *>
  152. *> \param[in,out] C
  153. *> \verbatim
  154. *> C is COMPLEX*16 array, dimension (LDC, NCC)
  155. *> On entry, an N-by-NCC matrix C.
  156. *> On exit, C is overwritten by Q**H * C.
  157. *> Not referenced if NCC = 0.
  158. *> \endverbatim
  159. *>
  160. *> \param[in] LDC
  161. *> \verbatim
  162. *> LDC is INTEGER
  163. *> The leading dimension of the array C.
  164. *> LDC >= max(1,N) if NCC > 0; LDC >=1 if NCC = 0.
  165. *> \endverbatim
  166. *>
  167. *> \param[out] RWORK
  168. *> \verbatim
  169. *> RWORK is DOUBLE PRECISION array, dimension (4*N)
  170. *> \endverbatim
  171. *>
  172. *> \param[out] INFO
  173. *> \verbatim
  174. *> INFO is INTEGER
  175. *> = 0: successful exit
  176. *> < 0: If INFO = -i, the i-th argument had an illegal value
  177. *> > 0: the algorithm did not converge; D and E contain the
  178. *> elements of a bidiagonal matrix which is orthogonally
  179. *> similar to the input matrix B; if INFO = i, i
  180. *> elements of E have not converged to zero.
  181. *> \endverbatim
  182. *
  183. *> \par Internal Parameters:
  184. * =========================
  185. *>
  186. *> \verbatim
  187. *> TOLMUL DOUBLE PRECISION, default = max(10,min(100,EPS**(-1/8)))
  188. *> TOLMUL controls the convergence criterion of the QR loop.
  189. *> If it is positive, TOLMUL*EPS is the desired relative
  190. *> precision in the computed singular values.
  191. *> If it is negative, abs(TOLMUL*EPS*sigma_max) is the
  192. *> desired absolute accuracy in the computed singular
  193. *> values (corresponds to relative accuracy
  194. *> abs(TOLMUL*EPS) in the largest singular value.
  195. *> abs(TOLMUL) should be between 1 and 1/EPS, and preferably
  196. *> between 10 (for fast convergence) and .1/EPS
  197. *> (for there to be some accuracy in the results).
  198. *> Default is to lose at either one eighth or 2 of the
  199. *> available decimal digits in each computed singular value
  200. *> (whichever is smaller).
  201. *>
  202. *> MAXITR INTEGER, default = 6
  203. *> MAXITR controls the maximum number of passes of the
  204. *> algorithm through its inner loop. The algorithms stops
  205. *> (and so fails to converge) if the number of passes
  206. *> through the inner loop exceeds MAXITR*N**2.
  207. *>
  208. *> \endverbatim
  209. *
  210. *> \par Note:
  211. * ===========
  212. *>
  213. *> \verbatim
  214. *> Bug report from Cezary Dendek.
  215. *> On November 3rd 2023, the INTEGER variable MAXIT = MAXITR*N**2 is
  216. *> removed since it can overflow pretty easily (for N larger or equal
  217. *> than 18,919). We instead use MAXITDIVN = MAXITR*N.
  218. *> \endverbatim
  219. *
  220. * Authors:
  221. * ========
  222. *
  223. *> \author Univ. of Tennessee
  224. *> \author Univ. of California Berkeley
  225. *> \author Univ. of Colorado Denver
  226. *> \author NAG Ltd.
  227. *
  228. *> \ingroup bdsqr
  229. *
  230. * =====================================================================
  231. SUBROUTINE ZBDSQR( UPLO, N, NCVT, NRU, NCC, D, E, VT, LDVT, U,
  232. $ LDU, C, LDC, RWORK, INFO )
  233. *
  234. * -- LAPACK computational routine --
  235. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  236. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  237. *
  238. * .. Scalar Arguments ..
  239. CHARACTER UPLO
  240. INTEGER INFO, LDC, LDU, LDVT, N, NCC, NCVT, NRU
  241. * ..
  242. * .. Array Arguments ..
  243. DOUBLE PRECISION D( * ), E( * ), RWORK( * )
  244. COMPLEX*16 C( LDC, * ), U( LDU, * ), VT( LDVT, * )
  245. * ..
  246. *
  247. * =====================================================================
  248. *
  249. * .. Parameters ..
  250. DOUBLE PRECISION ZERO
  251. PARAMETER ( ZERO = 0.0D0 )
  252. DOUBLE PRECISION ONE
  253. PARAMETER ( ONE = 1.0D0 )
  254. DOUBLE PRECISION NEGONE
  255. PARAMETER ( NEGONE = -1.0D0 )
  256. DOUBLE PRECISION HNDRTH
  257. PARAMETER ( HNDRTH = 0.01D0 )
  258. DOUBLE PRECISION TEN
  259. PARAMETER ( TEN = 10.0D0 )
  260. DOUBLE PRECISION HNDRD
  261. PARAMETER ( HNDRD = 100.0D0 )
  262. DOUBLE PRECISION MEIGTH
  263. PARAMETER ( MEIGTH = -0.125D0 )
  264. INTEGER MAXITR
  265. PARAMETER ( MAXITR = 6 )
  266. * ..
  267. * .. Local Scalars ..
  268. LOGICAL LOWER, ROTATE
  269. INTEGER I, IDIR, ISUB, ITER, ITERDIVN, J, LL, LLL, M,
  270. $ MAXITDIVN, NM1, NM12, NM13, OLDLL, OLDM
  271. DOUBLE PRECISION ABSE, ABSS, COSL, COSR, CS, EPS, F, G, H, MU,
  272. $ OLDCS, OLDSN, R, SHIFT, SIGMN, SIGMX, SINL,
  273. $ SINR, SLL, SMAX, SMIN, SMINOA,
  274. $ SN, THRESH, TOL, TOLMUL, UNFL
  275. * ..
  276. * .. External Functions ..
  277. LOGICAL LSAME
  278. DOUBLE PRECISION DLAMCH
  279. EXTERNAL LSAME, DLAMCH
  280. * ..
  281. * .. External Subroutines ..
  282. EXTERNAL DLARTG, DLAS2, DLASQ1, DLASV2, XERBLA, ZDROT,
  283. $ ZDSCAL, ZLASR, ZSWAP
  284. * ..
  285. * .. Intrinsic Functions ..
  286. INTRINSIC ABS, DBLE, MAX, MIN, SIGN, SQRT
  287. * ..
  288. * .. Executable Statements ..
  289. *
  290. * Test the input parameters.
  291. *
  292. INFO = 0
  293. LOWER = LSAME( UPLO, 'L' )
  294. IF( .NOT.LSAME( UPLO, 'U' ) .AND. .NOT.LOWER ) THEN
  295. INFO = -1
  296. ELSE IF( N.LT.0 ) THEN
  297. INFO = -2
  298. ELSE IF( NCVT.LT.0 ) THEN
  299. INFO = -3
  300. ELSE IF( NRU.LT.0 ) THEN
  301. INFO = -4
  302. ELSE IF( NCC.LT.0 ) THEN
  303. INFO = -5
  304. ELSE IF( ( NCVT.EQ.0 .AND. LDVT.LT.1 ) .OR.
  305. $ ( NCVT.GT.0 .AND. LDVT.LT.MAX( 1, N ) ) ) THEN
  306. INFO = -9
  307. ELSE IF( LDU.LT.MAX( 1, NRU ) ) THEN
  308. INFO = -11
  309. ELSE IF( ( NCC.EQ.0 .AND. LDC.LT.1 ) .OR.
  310. $ ( NCC.GT.0 .AND. LDC.LT.MAX( 1, N ) ) ) THEN
  311. INFO = -13
  312. END IF
  313. IF( INFO.NE.0 ) THEN
  314. CALL XERBLA( 'ZBDSQR', -INFO )
  315. RETURN
  316. END IF
  317. IF( N.EQ.0 )
  318. $ RETURN
  319. IF( N.EQ.1 )
  320. $ GO TO 160
  321. *
  322. * ROTATE is true if any singular vectors desired, false otherwise
  323. *
  324. ROTATE = ( NCVT.GT.0 ) .OR. ( NRU.GT.0 ) .OR. ( NCC.GT.0 )
  325. *
  326. * If no singular vectors desired, use qd algorithm
  327. *
  328. IF( .NOT.ROTATE ) THEN
  329. CALL DLASQ1( N, D, E, RWORK, INFO )
  330. *
  331. * If INFO equals 2, dqds didn't finish, try to finish
  332. *
  333. IF( INFO .NE. 2 ) RETURN
  334. INFO = 0
  335. END IF
  336. *
  337. NM1 = N - 1
  338. NM12 = NM1 + NM1
  339. NM13 = NM12 + NM1
  340. IDIR = 0
  341. *
  342. * Get machine constants
  343. *
  344. EPS = DLAMCH( 'Epsilon' )
  345. UNFL = DLAMCH( 'Safe minimum' )
  346. *
  347. * If matrix lower bidiagonal, rotate to be upper bidiagonal
  348. * by applying Givens rotations on the left
  349. *
  350. IF( LOWER ) THEN
  351. DO 10 I = 1, N - 1
  352. CALL DLARTG( D( I ), E( I ), CS, SN, R )
  353. D( I ) = R
  354. E( I ) = SN*D( I+1 )
  355. D( I+1 ) = CS*D( I+1 )
  356. RWORK( I ) = CS
  357. RWORK( NM1+I ) = SN
  358. 10 CONTINUE
  359. *
  360. * Update singular vectors if desired
  361. *
  362. IF( NRU.GT.0 )
  363. $ CALL ZLASR( 'R', 'V', 'F', NRU, N, RWORK( 1 ), RWORK( N ),
  364. $ U, LDU )
  365. IF( NCC.GT.0 )
  366. $ CALL ZLASR( 'L', 'V', 'F', N, NCC, RWORK( 1 ), RWORK( N ),
  367. $ C, LDC )
  368. END IF
  369. *
  370. * Compute singular values to relative accuracy TOL
  371. * (By setting TOL to be negative, algorithm will compute
  372. * singular values to absolute accuracy ABS(TOL)*norm(input matrix))
  373. *
  374. TOLMUL = MAX( TEN, MIN( HNDRD, EPS**MEIGTH ) )
  375. TOL = TOLMUL*EPS
  376. *
  377. * Compute approximate maximum, minimum singular values
  378. *
  379. SMAX = ZERO
  380. DO 20 I = 1, N
  381. SMAX = MAX( SMAX, ABS( D( I ) ) )
  382. 20 CONTINUE
  383. DO 30 I = 1, N - 1
  384. SMAX = MAX( SMAX, ABS( E( I ) ) )
  385. 30 CONTINUE
  386. SMIN = ZERO
  387. IF( TOL.GE.ZERO ) THEN
  388. *
  389. * Relative accuracy desired
  390. *
  391. SMINOA = ABS( D( 1 ) )
  392. IF( SMINOA.EQ.ZERO )
  393. $ GO TO 50
  394. MU = SMINOA
  395. DO 40 I = 2, N
  396. MU = ABS( D( I ) )*( MU / ( MU+ABS( E( I-1 ) ) ) )
  397. SMINOA = MIN( SMINOA, MU )
  398. IF( SMINOA.EQ.ZERO )
  399. $ GO TO 50
  400. 40 CONTINUE
  401. 50 CONTINUE
  402. SMINOA = SMINOA / SQRT( DBLE( N ) )
  403. THRESH = MAX( TOL*SMINOA, MAXITR*(N*(N*UNFL)) )
  404. ELSE
  405. *
  406. * Absolute accuracy desired
  407. *
  408. THRESH = MAX( ABS( TOL )*SMAX, MAXITR*(N*(N*UNFL)) )
  409. END IF
  410. *
  411. * Prepare for main iteration loop for the singular values
  412. * (MAXIT is the maximum number of passes through the inner
  413. * loop permitted before nonconvergence signalled.)
  414. *
  415. MAXITDIVN = MAXITR*N
  416. ITERDIVN = 0
  417. ITER = -1
  418. OLDLL = -1
  419. OLDM = -1
  420. *
  421. * M points to last element of unconverged part of matrix
  422. *
  423. M = N
  424. *
  425. * Begin main iteration loop
  426. *
  427. 60 CONTINUE
  428. *
  429. * Check for convergence or exceeding iteration count
  430. *
  431. IF( M.LE.1 )
  432. $ GO TO 160
  433. IF( ITER.GE.N ) THEN
  434. ITER = ITER - N
  435. ITERDIVN = ITERDIVN + 1
  436. IF( ITERDIVN.GE.MAXITDIVN )
  437. $ GO TO 200
  438. END IF
  439. *
  440. * Find diagonal block of matrix to work on
  441. *
  442. IF( TOL.LT.ZERO .AND. ABS( D( M ) ).LE.THRESH )
  443. $ D( M ) = ZERO
  444. SMAX = ABS( D( M ) )
  445. DO 70 LLL = 1, M - 1
  446. LL = M - LLL
  447. ABSS = ABS( D( LL ) )
  448. ABSE = ABS( E( LL ) )
  449. IF( TOL.LT.ZERO .AND. ABSS.LE.THRESH )
  450. $ D( LL ) = ZERO
  451. IF( ABSE.LE.THRESH )
  452. $ GO TO 80
  453. SMAX = MAX( SMAX, ABSS, ABSE )
  454. 70 CONTINUE
  455. LL = 0
  456. GO TO 90
  457. 80 CONTINUE
  458. E( LL ) = ZERO
  459. *
  460. * Matrix splits since E(LL) = 0
  461. *
  462. IF( LL.EQ.M-1 ) THEN
  463. *
  464. * Convergence of bottom singular value, return to top of loop
  465. *
  466. M = M - 1
  467. GO TO 60
  468. END IF
  469. 90 CONTINUE
  470. LL = LL + 1
  471. *
  472. * E(LL) through E(M-1) are nonzero, E(LL-1) is zero
  473. *
  474. IF( LL.EQ.M-1 ) THEN
  475. *
  476. * 2 by 2 block, handle separately
  477. *
  478. CALL DLASV2( D( M-1 ), E( M-1 ), D( M ), SIGMN, SIGMX, SINR,
  479. $ COSR, SINL, COSL )
  480. D( M-1 ) = SIGMX
  481. E( M-1 ) = ZERO
  482. D( M ) = SIGMN
  483. *
  484. * Compute singular vectors, if desired
  485. *
  486. IF( NCVT.GT.0 )
  487. $ CALL ZDROT( NCVT, VT( M-1, 1 ), LDVT, VT( M, 1 ), LDVT,
  488. $ COSR, SINR )
  489. IF( NRU.GT.0 )
  490. $ CALL ZDROT( NRU, U( 1, M-1 ), 1, U( 1, M ), 1, COSL, SINL )
  491. IF( NCC.GT.0 )
  492. $ CALL ZDROT( NCC, C( M-1, 1 ), LDC, C( M, 1 ), LDC, COSL,
  493. $ SINL )
  494. M = M - 2
  495. GO TO 60
  496. END IF
  497. *
  498. * If working on new submatrix, choose shift direction
  499. * (from larger end diagonal element towards smaller)
  500. *
  501. IF( LL.GT.OLDM .OR. M.LT.OLDLL ) THEN
  502. IF( ABS( D( LL ) ).GE.ABS( D( M ) ) ) THEN
  503. *
  504. * Chase bulge from top (big end) to bottom (small end)
  505. *
  506. IDIR = 1
  507. ELSE
  508. *
  509. * Chase bulge from bottom (big end) to top (small end)
  510. *
  511. IDIR = 2
  512. END IF
  513. END IF
  514. *
  515. * Apply convergence tests
  516. *
  517. IF( IDIR.EQ.1 ) THEN
  518. *
  519. * Run convergence test in forward direction
  520. * First apply standard test to bottom of matrix
  521. *
  522. IF( ABS( E( M-1 ) ).LE.ABS( TOL )*ABS( D( M ) ) .OR.
  523. $ ( TOL.LT.ZERO .AND. ABS( E( M-1 ) ).LE.THRESH ) ) THEN
  524. E( M-1 ) = ZERO
  525. GO TO 60
  526. END IF
  527. *
  528. IF( TOL.GE.ZERO ) THEN
  529. *
  530. * If relative accuracy desired,
  531. * apply convergence criterion forward
  532. *
  533. MU = ABS( D( LL ) )
  534. SMIN = MU
  535. DO 100 LLL = LL, M - 1
  536. IF( ABS( E( LLL ) ).LE.TOL*MU ) THEN
  537. E( LLL ) = ZERO
  538. GO TO 60
  539. END IF
  540. MU = ABS( D( LLL+1 ) )*( MU / ( MU+ABS( E( LLL ) ) ) )
  541. SMIN = MIN( SMIN, MU )
  542. 100 CONTINUE
  543. END IF
  544. *
  545. ELSE
  546. *
  547. * Run convergence test in backward direction
  548. * First apply standard test to top of matrix
  549. *
  550. IF( ABS( E( LL ) ).LE.ABS( TOL )*ABS( D( LL ) ) .OR.
  551. $ ( TOL.LT.ZERO .AND. ABS( E( LL ) ).LE.THRESH ) ) THEN
  552. E( LL ) = ZERO
  553. GO TO 60
  554. END IF
  555. *
  556. IF( TOL.GE.ZERO ) THEN
  557. *
  558. * If relative accuracy desired,
  559. * apply convergence criterion backward
  560. *
  561. MU = ABS( D( M ) )
  562. SMIN = MU
  563. DO 110 LLL = M - 1, LL, -1
  564. IF( ABS( E( LLL ) ).LE.TOL*MU ) THEN
  565. E( LLL ) = ZERO
  566. GO TO 60
  567. END IF
  568. MU = ABS( D( LLL ) )*( MU / ( MU+ABS( E( LLL ) ) ) )
  569. SMIN = MIN( SMIN, MU )
  570. 110 CONTINUE
  571. END IF
  572. END IF
  573. OLDLL = LL
  574. OLDM = M
  575. *
  576. * Compute shift. First, test if shifting would ruin relative
  577. * accuracy, and if so set the shift to zero.
  578. *
  579. IF( TOL.GE.ZERO .AND. N*TOL*( SMIN / SMAX ).LE.
  580. $ MAX( EPS, HNDRTH*TOL ) ) THEN
  581. *
  582. * Use a zero shift to avoid loss of relative accuracy
  583. *
  584. SHIFT = ZERO
  585. ELSE
  586. *
  587. * Compute the shift from 2-by-2 block at end of matrix
  588. *
  589. IF( IDIR.EQ.1 ) THEN
  590. SLL = ABS( D( LL ) )
  591. CALL DLAS2( D( M-1 ), E( M-1 ), D( M ), SHIFT, R )
  592. ELSE
  593. SLL = ABS( D( M ) )
  594. CALL DLAS2( D( LL ), E( LL ), D( LL+1 ), SHIFT, R )
  595. END IF
  596. *
  597. * Test if shift negligible, and if so set to zero
  598. *
  599. IF( SLL.GT.ZERO ) THEN
  600. IF( ( SHIFT / SLL )**2.LT.EPS )
  601. $ SHIFT = ZERO
  602. END IF
  603. END IF
  604. *
  605. * Increment iteration count
  606. *
  607. ITER = ITER + M - LL
  608. *
  609. * If SHIFT = 0, do simplified QR iteration
  610. *
  611. IF( SHIFT.EQ.ZERO ) THEN
  612. IF( IDIR.EQ.1 ) THEN
  613. *
  614. * Chase bulge from top to bottom
  615. * Save cosines and sines for later singular vector updates
  616. *
  617. CS = ONE
  618. OLDCS = ONE
  619. DO 120 I = LL, M - 1
  620. CALL DLARTG( D( I )*CS, E( I ), CS, SN, R )
  621. IF( I.GT.LL )
  622. $ E( I-1 ) = OLDSN*R
  623. CALL DLARTG( OLDCS*R, D( I+1 )*SN, OLDCS, OLDSN, D( I ) )
  624. RWORK( I-LL+1 ) = CS
  625. RWORK( I-LL+1+NM1 ) = SN
  626. RWORK( I-LL+1+NM12 ) = OLDCS
  627. RWORK( I-LL+1+NM13 ) = OLDSN
  628. 120 CONTINUE
  629. H = D( M )*CS
  630. D( M ) = H*OLDCS
  631. E( M-1 ) = H*OLDSN
  632. *
  633. * Update singular vectors
  634. *
  635. IF( NCVT.GT.0 )
  636. $ CALL ZLASR( 'L', 'V', 'F', M-LL+1, NCVT, RWORK( 1 ),
  637. $ RWORK( N ), VT( LL, 1 ), LDVT )
  638. IF( NRU.GT.0 )
  639. $ CALL ZLASR( 'R', 'V', 'F', NRU, M-LL+1, RWORK( NM12+1 ),
  640. $ RWORK( NM13+1 ), U( 1, LL ), LDU )
  641. IF( NCC.GT.0 )
  642. $ CALL ZLASR( 'L', 'V', 'F', M-LL+1, NCC, RWORK( NM12+1 ),
  643. $ RWORK( NM13+1 ), C( LL, 1 ), LDC )
  644. *
  645. * Test convergence
  646. *
  647. IF( ABS( E( M-1 ) ).LE.THRESH )
  648. $ E( M-1 ) = ZERO
  649. *
  650. ELSE
  651. *
  652. * Chase bulge from bottom to top
  653. * Save cosines and sines for later singular vector updates
  654. *
  655. CS = ONE
  656. OLDCS = ONE
  657. DO 130 I = M, LL + 1, -1
  658. CALL DLARTG( D( I )*CS, E( I-1 ), CS, SN, R )
  659. IF( I.LT.M )
  660. $ E( I ) = OLDSN*R
  661. CALL DLARTG( OLDCS*R, D( I-1 )*SN, OLDCS, OLDSN, D( I ) )
  662. RWORK( I-LL ) = CS
  663. RWORK( I-LL+NM1 ) = -SN
  664. RWORK( I-LL+NM12 ) = OLDCS
  665. RWORK( I-LL+NM13 ) = -OLDSN
  666. 130 CONTINUE
  667. H = D( LL )*CS
  668. D( LL ) = H*OLDCS
  669. E( LL ) = H*OLDSN
  670. *
  671. * Update singular vectors
  672. *
  673. IF( NCVT.GT.0 )
  674. $ CALL ZLASR( 'L', 'V', 'B', M-LL+1, NCVT, RWORK( NM12+1 ),
  675. $ RWORK( NM13+1 ), VT( LL, 1 ), LDVT )
  676. IF( NRU.GT.0 )
  677. $ CALL ZLASR( 'R', 'V', 'B', NRU, M-LL+1, RWORK( 1 ),
  678. $ RWORK( N ), U( 1, LL ), LDU )
  679. IF( NCC.GT.0 )
  680. $ CALL ZLASR( 'L', 'V', 'B', M-LL+1, NCC, RWORK( 1 ),
  681. $ RWORK( N ), C( LL, 1 ), LDC )
  682. *
  683. * Test convergence
  684. *
  685. IF( ABS( E( LL ) ).LE.THRESH )
  686. $ E( LL ) = ZERO
  687. END IF
  688. ELSE
  689. *
  690. * Use nonzero shift
  691. *
  692. IF( IDIR.EQ.1 ) THEN
  693. *
  694. * Chase bulge from top to bottom
  695. * Save cosines and sines for later singular vector updates
  696. *
  697. F = ( ABS( D( LL ) )-SHIFT )*
  698. $ ( SIGN( ONE, D( LL ) )+SHIFT / D( LL ) )
  699. G = E( LL )
  700. DO 140 I = LL, M - 1
  701. CALL DLARTG( F, G, COSR, SINR, R )
  702. IF( I.GT.LL )
  703. $ E( I-1 ) = R
  704. F = COSR*D( I ) + SINR*E( I )
  705. E( I ) = COSR*E( I ) - SINR*D( I )
  706. G = SINR*D( I+1 )
  707. D( I+1 ) = COSR*D( I+1 )
  708. CALL DLARTG( F, G, COSL, SINL, R )
  709. D( I ) = R
  710. F = COSL*E( I ) + SINL*D( I+1 )
  711. D( I+1 ) = COSL*D( I+1 ) - SINL*E( I )
  712. IF( I.LT.M-1 ) THEN
  713. G = SINL*E( I+1 )
  714. E( I+1 ) = COSL*E( I+1 )
  715. END IF
  716. RWORK( I-LL+1 ) = COSR
  717. RWORK( I-LL+1+NM1 ) = SINR
  718. RWORK( I-LL+1+NM12 ) = COSL
  719. RWORK( I-LL+1+NM13 ) = SINL
  720. 140 CONTINUE
  721. E( M-1 ) = F
  722. *
  723. * Update singular vectors
  724. *
  725. IF( NCVT.GT.0 )
  726. $ CALL ZLASR( 'L', 'V', 'F', M-LL+1, NCVT, RWORK( 1 ),
  727. $ RWORK( N ), VT( LL, 1 ), LDVT )
  728. IF( NRU.GT.0 )
  729. $ CALL ZLASR( 'R', 'V', 'F', NRU, M-LL+1, RWORK( NM12+1 ),
  730. $ RWORK( NM13+1 ), U( 1, LL ), LDU )
  731. IF( NCC.GT.0 )
  732. $ CALL ZLASR( 'L', 'V', 'F', M-LL+1, NCC, RWORK( NM12+1 ),
  733. $ RWORK( NM13+1 ), C( LL, 1 ), LDC )
  734. *
  735. * Test convergence
  736. *
  737. IF( ABS( E( M-1 ) ).LE.THRESH )
  738. $ E( M-1 ) = ZERO
  739. *
  740. ELSE
  741. *
  742. * Chase bulge from bottom to top
  743. * Save cosines and sines for later singular vector updates
  744. *
  745. F = ( ABS( D( M ) )-SHIFT )*( SIGN( ONE, D( M ) )+SHIFT /
  746. $ D( M ) )
  747. G = E( M-1 )
  748. DO 150 I = M, LL + 1, -1
  749. CALL DLARTG( F, G, COSR, SINR, R )
  750. IF( I.LT.M )
  751. $ E( I ) = R
  752. F = COSR*D( I ) + SINR*E( I-1 )
  753. E( I-1 ) = COSR*E( I-1 ) - SINR*D( I )
  754. G = SINR*D( I-1 )
  755. D( I-1 ) = COSR*D( I-1 )
  756. CALL DLARTG( F, G, COSL, SINL, R )
  757. D( I ) = R
  758. F = COSL*E( I-1 ) + SINL*D( I-1 )
  759. D( I-1 ) = COSL*D( I-1 ) - SINL*E( I-1 )
  760. IF( I.GT.LL+1 ) THEN
  761. G = SINL*E( I-2 )
  762. E( I-2 ) = COSL*E( I-2 )
  763. END IF
  764. RWORK( I-LL ) = COSR
  765. RWORK( I-LL+NM1 ) = -SINR
  766. RWORK( I-LL+NM12 ) = COSL
  767. RWORK( I-LL+NM13 ) = -SINL
  768. 150 CONTINUE
  769. E( LL ) = F
  770. *
  771. * Test convergence
  772. *
  773. IF( ABS( E( LL ) ).LE.THRESH )
  774. $ E( LL ) = ZERO
  775. *
  776. * Update singular vectors if desired
  777. *
  778. IF( NCVT.GT.0 )
  779. $ CALL ZLASR( 'L', 'V', 'B', M-LL+1, NCVT, RWORK( NM12+1 ),
  780. $ RWORK( NM13+1 ), VT( LL, 1 ), LDVT )
  781. IF( NRU.GT.0 )
  782. $ CALL ZLASR( 'R', 'V', 'B', NRU, M-LL+1, RWORK( 1 ),
  783. $ RWORK( N ), U( 1, LL ), LDU )
  784. IF( NCC.GT.0 )
  785. $ CALL ZLASR( 'L', 'V', 'B', M-LL+1, NCC, RWORK( 1 ),
  786. $ RWORK( N ), C( LL, 1 ), LDC )
  787. END IF
  788. END IF
  789. *
  790. * QR iteration finished, go back and check convergence
  791. *
  792. GO TO 60
  793. *
  794. * All singular values converged, so make them positive
  795. *
  796. 160 CONTINUE
  797. DO 170 I = 1, N
  798. IF( D( I ).LT.ZERO ) THEN
  799. D( I ) = -D( I )
  800. *
  801. * Change sign of singular vectors, if desired
  802. *
  803. IF( NCVT.GT.0 )
  804. $ CALL ZDSCAL( NCVT, NEGONE, VT( I, 1 ), LDVT )
  805. END IF
  806. 170 CONTINUE
  807. *
  808. * Sort the singular values into decreasing order (insertion sort on
  809. * singular values, but only one transposition per singular vector)
  810. *
  811. DO 190 I = 1, N - 1
  812. *
  813. * Scan for smallest D(I)
  814. *
  815. ISUB = 1
  816. SMIN = D( 1 )
  817. DO 180 J = 2, N + 1 - I
  818. IF( D( J ).LE.SMIN ) THEN
  819. ISUB = J
  820. SMIN = D( J )
  821. END IF
  822. 180 CONTINUE
  823. IF( ISUB.NE.N+1-I ) THEN
  824. *
  825. * Swap singular values and vectors
  826. *
  827. D( ISUB ) = D( N+1-I )
  828. D( N+1-I ) = SMIN
  829. IF( NCVT.GT.0 )
  830. $ CALL ZSWAP( NCVT, VT( ISUB, 1 ), LDVT, VT( N+1-I, 1 ),
  831. $ LDVT )
  832. IF( NRU.GT.0 )
  833. $ CALL ZSWAP( NRU, U( 1, ISUB ), 1, U( 1, N+1-I ), 1 )
  834. IF( NCC.GT.0 )
  835. $ CALL ZSWAP( NCC, C( ISUB, 1 ), LDC, C( N+1-I, 1 ), LDC )
  836. END IF
  837. 190 CONTINUE
  838. GO TO 220
  839. *
  840. * Maximum number of iterations exceeded, failure to converge
  841. *
  842. 200 CONTINUE
  843. INFO = 0
  844. DO 210 I = 1, N - 1
  845. IF( E( I ).NE.ZERO )
  846. $ INFO = INFO + 1
  847. 210 CONTINUE
  848. 220 CONTINUE
  849. RETURN
  850. *
  851. * End of ZBDSQR
  852. *
  853. END