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.

sbdsqr.f 27 kB

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