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.

cbdsqr.f 26 kB

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