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.

cgesvj.f 57 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. *> \brief <b> CGESVJ </b>
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download CGESVJ + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgesvj.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgesvj.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgesvj.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE CGESVJ( JOBA, JOBU, JOBV, M, N, A, LDA, SVA, MV, V,
  22. * LDV, CWORK, LWORK, RWORK, LRWORK, INFO )
  23. *
  24. * .. Scalar Arguments ..
  25. * INTEGER INFO, LDA, LDV, LWORK, LRWORK, M, MV, N
  26. * CHARACTER*1 JOBA, JOBU, JOBV
  27. * ..
  28. * .. Array Arguments ..
  29. * COMPLEX A( LDA, * ), V( LDV, * ), CWORK( LWORK )
  30. * REAL RWORK( LRWORK ), SVA( N )
  31. * ..
  32. *
  33. *
  34. *> \par Purpose:
  35. * =============
  36. *>
  37. *> \verbatim
  38. *>
  39. *> CGESVJ computes the singular value decomposition (SVD) of a complex
  40. *> M-by-N matrix A, where M >= N. The SVD of A is written as
  41. *> [++] [xx] [x0] [xx]
  42. *> A = U * SIGMA * V^*, [++] = [xx] * [ox] * [xx]
  43. *> [++] [xx]
  44. *> where SIGMA is an N-by-N diagonal matrix, U is an M-by-N orthonormal
  45. *> matrix, and V is an N-by-N unitary matrix. The diagonal elements
  46. *> of SIGMA are the singular values of A. The columns of U and V are the
  47. *> left and the right singular vectors of A, respectively.
  48. *> \endverbatim
  49. *
  50. * Arguments:
  51. * ==========
  52. *
  53. *> \param[in] JOBA
  54. *> \verbatim
  55. *> JOBA is CHARACTER*1
  56. *> Specifies the structure of A.
  57. *> = 'L': The input matrix A is lower triangular;
  58. *> = 'U': The input matrix A is upper triangular;
  59. *> = 'G': The input matrix A is general M-by-N matrix, M >= N.
  60. *> \endverbatim
  61. *>
  62. *> \param[in] JOBU
  63. *> \verbatim
  64. *> JOBU is CHARACTER*1
  65. *> Specifies whether to compute the left singular vectors
  66. *> (columns of U):
  67. *> = 'U' or 'F': The left singular vectors corresponding to the nonzero
  68. *> singular values are computed and returned in the leading
  69. *> columns of A. See more details in the description of A.
  70. *> The default numerical orthogonality threshold is set to
  71. *> approximately TOL=CTOL*EPS, CTOL=SQRT(M), EPS=SLAMCH('E').
  72. *> = 'C': Analogous to JOBU='U', except that user can control the
  73. *> level of numerical orthogonality of the computed left
  74. *> singular vectors. TOL can be set to TOL = CTOL*EPS, where
  75. *> CTOL is given on input in the array WORK.
  76. *> No CTOL smaller than ONE is allowed. CTOL greater
  77. *> than 1 / EPS is meaningless. The option 'C'
  78. *> can be used if M*EPS is satisfactory orthogonality
  79. *> of the computed left singular vectors, so CTOL=M could
  80. *> save few sweeps of Jacobi rotations.
  81. *> See the descriptions of A and WORK(1).
  82. *> = 'N': The matrix U is not computed. However, see the
  83. *> description of A.
  84. *> \endverbatim
  85. *>
  86. *> \param[in] JOBV
  87. *> \verbatim
  88. *> JOBV is CHARACTER*1
  89. *> Specifies whether to compute the right singular vectors, that
  90. *> is, the matrix V:
  91. *> = 'V' or 'J': the matrix V is computed and returned in the array V
  92. *> = 'A': the Jacobi rotations are applied to the MV-by-N
  93. *> array V. In other words, the right singular vector
  94. *> matrix V is not computed explicitly; instead it is
  95. *> applied to an MV-by-N matrix initially stored in the
  96. *> first MV rows of V.
  97. *> = 'N': the matrix V is not computed and the array V is not
  98. *> referenced
  99. *> \endverbatim
  100. *>
  101. *> \param[in] M
  102. *> \verbatim
  103. *> M is INTEGER
  104. *> The number of rows of the input matrix A. 1/SLAMCH('E') > M >= 0.
  105. *> \endverbatim
  106. *>
  107. *> \param[in] N
  108. *> \verbatim
  109. *> N is INTEGER
  110. *> The number of columns of the input matrix A.
  111. *> M >= N >= 0.
  112. *> \endverbatim
  113. *>
  114. *> \param[in,out] A
  115. *> \verbatim
  116. *> A is COMPLEX array, dimension (LDA,N)
  117. *> On entry, the M-by-N matrix A.
  118. *> On exit,
  119. *> If JOBU = 'U' .OR. JOBU = 'C':
  120. *> If INFO = 0 :
  121. *> RANKA orthonormal columns of U are returned in the
  122. *> leading RANKA columns of the array A. Here RANKA <= N
  123. *> is the number of computed singular values of A that are
  124. *> above the underflow threshold SLAMCH('S'). The singular
  125. *> vectors corresponding to underflowed or zero singular
  126. *> values are not computed. The value of RANKA is returned
  127. *> in the array RWORK as RANKA=NINT(RWORK(2)). Also see the
  128. *> descriptions of SVA and RWORK. The computed columns of U
  129. *> are mutually numerically orthogonal up to approximately
  130. *> TOL=SQRT(M)*EPS (default); or TOL=CTOL*EPS (JOBU = 'C'),
  131. *> see the description of JOBU.
  132. *> If INFO > 0,
  133. *> the procedure CGESVJ did not converge in the given number
  134. *> of iterations (sweeps). In that case, the computed
  135. *> columns of U may not be orthogonal up to TOL. The output
  136. *> U (stored in A), SIGMA (given by the computed singular
  137. *> values in SVA(1:N)) and V is still a decomposition of the
  138. *> input matrix A in the sense that the residual
  139. *> || A - SCALE * U * SIGMA * V^* ||_2 / ||A||_2 is small.
  140. *> If JOBU = 'N':
  141. *> If INFO = 0 :
  142. *> Note that the left singular vectors are 'for free' in the
  143. *> one-sided Jacobi SVD algorithm. However, if only the
  144. *> singular values are needed, the level of numerical
  145. *> orthogonality of U is not an issue and iterations are
  146. *> stopped when the columns of the iterated matrix are
  147. *> numerically orthogonal up to approximately M*EPS. Thus,
  148. *> on exit, A contains the columns of U scaled with the
  149. *> corresponding singular values.
  150. *> If INFO > 0 :
  151. *> the procedure CGESVJ did not converge in the given number
  152. *> of iterations (sweeps).
  153. *> \endverbatim
  154. *>
  155. *> \param[in] LDA
  156. *> \verbatim
  157. *> LDA is INTEGER
  158. *> The leading dimension of the array A. LDA >= max(1,M).
  159. *> \endverbatim
  160. *>
  161. *> \param[out] SVA
  162. *> \verbatim
  163. *> SVA is REAL array, dimension (N)
  164. *> On exit,
  165. *> If INFO = 0 :
  166. *> depending on the value SCALE = RWORK(1), we have:
  167. *> If SCALE = ONE:
  168. *> SVA(1:N) contains the computed singular values of A.
  169. *> During the computation SVA contains the Euclidean column
  170. *> norms of the iterated matrices in the array A.
  171. *> If SCALE .NE. ONE:
  172. *> The singular values of A are SCALE*SVA(1:N), and this
  173. *> factored representation is due to the fact that some of the
  174. *> singular values of A might underflow or overflow.
  175. *>
  176. *> If INFO > 0 :
  177. *> the procedure CGESVJ did not converge in the given number of
  178. *> iterations (sweeps) and SCALE*SVA(1:N) may not be accurate.
  179. *> \endverbatim
  180. *>
  181. *> \param[in] MV
  182. *> \verbatim
  183. *> MV is INTEGER
  184. *> If JOBV = 'A', then the product of Jacobi rotations in CGESVJ
  185. *> is applied to the first MV rows of V. See the description of JOBV.
  186. *> \endverbatim
  187. *>
  188. *> \param[in,out] V
  189. *> \verbatim
  190. *> V is COMPLEX array, dimension (LDV,N)
  191. *> If JOBV = 'V', then V contains on exit the N-by-N matrix of
  192. *> the right singular vectors;
  193. *> If JOBV = 'A', then V contains the product of the computed right
  194. *> singular vector matrix and the initial matrix in
  195. *> the array V.
  196. *> If JOBV = 'N', then V is not referenced.
  197. *> \endverbatim
  198. *>
  199. *> \param[in] LDV
  200. *> \verbatim
  201. *> LDV is INTEGER
  202. *> The leading dimension of the array V, LDV >= 1.
  203. *> If JOBV = 'V', then LDV >= max(1,N).
  204. *> If JOBV = 'A', then LDV >= max(1,MV) .
  205. *> \endverbatim
  206. *>
  207. *> \param[in,out] CWORK
  208. *> \verbatim
  209. *> CWORK is COMPLEX array, dimension (max(1,LWORK))
  210. *> Used as workspace.
  211. *> \endverbatim
  212. *>
  213. *> \param[in] LWORK
  214. *> \verbatim
  215. *> LWORK is INTEGER.
  216. *> Length of CWORK.
  217. *> LWORK >= 1, if MIN(M,N) = 0, and LWORK >= M+N, otherwise.
  218. *>
  219. *> If on entry LWORK = -1, then a workspace query is assumed and
  220. *> no computation is done; CWORK(1) is set to the minial (and optimal)
  221. *> length of CWORK.
  222. *> \endverbatim
  223. *>
  224. *> \param[in,out] RWORK
  225. *> \verbatim
  226. *> RWORK is REAL array, dimension (max(6,LRWORK))
  227. *> On entry,
  228. *> If JOBU = 'C' :
  229. *> RWORK(1) = CTOL, where CTOL defines the threshold for convergence.
  230. *> The process stops if all columns of A are mutually
  231. *> orthogonal up to CTOL*EPS, EPS=SLAMCH('E').
  232. *> It is required that CTOL >= ONE, i.e. it is not
  233. *> allowed to force the routine to obtain orthogonality
  234. *> below EPSILON.
  235. *> On exit,
  236. *> RWORK(1) = SCALE is the scaling factor such that SCALE*SVA(1:N)
  237. *> are the computed singular values of A.
  238. *> (See description of SVA().)
  239. *> RWORK(2) = NINT(RWORK(2)) is the number of the computed nonzero
  240. *> singular values.
  241. *> RWORK(3) = NINT(RWORK(3)) is the number of the computed singular
  242. *> values that are larger than the underflow threshold.
  243. *> RWORK(4) = NINT(RWORK(4)) is the number of sweeps of Jacobi
  244. *> rotations needed for numerical convergence.
  245. *> RWORK(5) = max_{i.NE.j} |COS(A(:,i),A(:,j))| in the last sweep.
  246. *> This is useful information in cases when CGESVJ did
  247. *> not converge, as it can be used to estimate whether
  248. *> the output is still useful and for post festum analysis.
  249. *> RWORK(6) = the largest absolute value over all sines of the
  250. *> Jacobi rotation angles in the last sweep. It can be
  251. *> useful for a post festum analysis.
  252. *> \endverbatim
  253. *>
  254. *> \param[in] LRWORK
  255. *> \verbatim
  256. *> LRWORK is INTEGER
  257. *> Length of RWORK.
  258. *> LRWORK >= 1, if MIN(M,N) = 0, and LRWORK >= MAX(6,N), otherwise
  259. *>
  260. *> If on entry LRWORK = -1, then a workspace query is assumed and
  261. *> no computation is done; RWORK(1) is set to the minial (and optimal)
  262. *> length of RWORK.
  263. *> \endverbatim
  264. *>
  265. *> \param[out] INFO
  266. *> \verbatim
  267. *> INFO is INTEGER
  268. *> = 0: successful exit.
  269. *> < 0: if INFO = -i, then the i-th argument had an illegal value
  270. *> > 0: CGESVJ did not converge in the maximal allowed number
  271. *> (NSWEEP=30) of sweeps. The output may still be useful.
  272. *> See the description of RWORK.
  273. *> \endverbatim
  274. *>
  275. * Authors:
  276. * ========
  277. *
  278. *> \author Univ. of Tennessee
  279. *> \author Univ. of California Berkeley
  280. *> \author Univ. of Colorado Denver
  281. *> \author NAG Ltd.
  282. *
  283. *> \ingroup gesvj
  284. *
  285. *> \par Further Details:
  286. * =====================
  287. *>
  288. *> \verbatim
  289. *>
  290. *> The orthogonal N-by-N matrix V is obtained as a product of Jacobi plane
  291. *> rotations. In the case of underflow of the tangent of the Jacobi angle, a
  292. *> modified Jacobi transformation of Drmac [3] is used. Pivot strategy uses
  293. *> column interchanges of de Rijk [1]. The relative accuracy of the computed
  294. *> singular values and the accuracy of the computed singular vectors (in
  295. *> angle metric) is as guaranteed by the theory of Demmel and Veselic [2].
  296. *> The condition number that determines the accuracy in the full rank case
  297. *> is essentially min_{D=diag} kappa(A*D), where kappa(.) is the
  298. *> spectral condition number. The best performance of this Jacobi SVD
  299. *> procedure is achieved if used in an accelerated version of Drmac and
  300. *> Veselic [4,5], and it is the kernel routine in the SIGMA library [6].
  301. *> Some tuning parameters (marked with [TP]) are available for the
  302. *> implementer.
  303. *> The computational range for the nonzero singular values is the machine
  304. *> number interval ( UNDERFLOW , OVERFLOW ). In extreme cases, even
  305. *> denormalized singular values can be computed with the corresponding
  306. *> gradual loss of accurate digits.
  307. *> \endverbatim
  308. *
  309. *> \par Contributor:
  310. * ==================
  311. *>
  312. *> \verbatim
  313. *>
  314. *> ============
  315. *>
  316. *> Zlatko Drmac (Zagreb, Croatia)
  317. *>
  318. *> \endverbatim
  319. *
  320. *> \par References:
  321. * ================
  322. *>
  323. *> \verbatim
  324. *>
  325. *> [1] P. P. M. De Rijk: A one-sided Jacobi algorithm for computing the
  326. *> singular value decomposition on a vector computer.
  327. *> SIAM J. Sci. Stat. Comp., Vol. 10 (1998), pp. 359-371.
  328. *> [2] J. Demmel and K. Veselic: Jacobi method is more accurate than QR.
  329. *> [3] Z. Drmac: Implementation of Jacobi rotations for accurate singular
  330. *> value computation in floating point arithmetic.
  331. *> SIAM J. Sci. Comp., Vol. 18 (1997), pp. 1200-1222.
  332. *> [4] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I.
  333. *> SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342.
  334. *> LAPACK Working note 169.
  335. *> [5] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II.
  336. *> SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362.
  337. *> LAPACK Working note 170.
  338. *> [6] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV,
  339. *> QSVD, (H,K)-SVD computations.
  340. *> Department of Mathematics, University of Zagreb, 2008, 2015.
  341. *> \endverbatim
  342. *
  343. *> \par Bugs, examples and comments:
  344. * =================================
  345. *>
  346. *> \verbatim
  347. *> ===========================
  348. *> Please report all bugs and send interesting test examples and comments to
  349. *> drmac@math.hr. Thank you.
  350. *> \endverbatim
  351. *>
  352. * =====================================================================
  353. SUBROUTINE CGESVJ( JOBA, JOBU, JOBV, M, N, A, LDA, SVA, MV, V,
  354. $ LDV, CWORK, LWORK, RWORK, LRWORK, INFO )
  355. *
  356. * -- LAPACK computational routine --
  357. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  358. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  359. *
  360. IMPLICIT NONE
  361. * .. Scalar Arguments ..
  362. INTEGER INFO, LDA, LDV, LWORK, LRWORK, M, MV, N
  363. CHARACTER*1 JOBA, JOBU, JOBV
  364. * ..
  365. * .. Array Arguments ..
  366. COMPLEX A( LDA, * ), V( LDV, * ), CWORK( LWORK )
  367. REAL RWORK( LRWORK ), SVA( N )
  368. * ..
  369. *
  370. * =====================================================================
  371. *
  372. * .. Local Parameters ..
  373. REAL ZERO, HALF, ONE
  374. PARAMETER ( ZERO = 0.0E0, HALF = 0.5E0, ONE = 1.0E0)
  375. COMPLEX CZERO, CONE
  376. PARAMETER ( CZERO = (0.0E0, 0.0E0), CONE = (1.0E0, 0.0E0) )
  377. INTEGER NSWEEP
  378. PARAMETER ( NSWEEP = 30 )
  379. * ..
  380. * .. Local Scalars ..
  381. COMPLEX AAPQ, OMPQ
  382. REAL AAPP, AAPP0, AAPQ1, AAQQ, APOAQ, AQOAP, BIG,
  383. $ BIGTHETA, CS, CTOL, EPSLN, MXAAPQ,
  384. $ MXSINJ, ROOTBIG, ROOTEPS, ROOTSFMIN, ROOTTOL,
  385. $ SKL, SFMIN, SMALL, SN, T, TEMP1, THETA, THSIGN, TOL
  386. INTEGER BLSKIP, EMPTSW, i, ibr, IERR, igl, IJBLSK, ir1,
  387. $ ISWROT, jbc, jgl, KBL, LKAHEAD, MVL, N2, N34,
  388. $ N4, NBL, NOTROT, p, PSKIPPED, q, ROWSKIP, SWBAND,
  389. $ MINMN, LWMIN, LRWMIN
  390. LOGICAL APPLV, GOSCALE, LOWER, LQUERY, LSVEC, NOSCALE, ROTOK,
  391. $ RSVEC, UCTOL, UPPER
  392. * ..
  393. * ..
  394. * .. Intrinsic Functions ..
  395. INTRINSIC ABS, MAX, MIN, CONJG, REAL, SIGN, SQRT
  396. * ..
  397. * .. External Functions ..
  398. * ..
  399. * from BLAS
  400. REAL SCNRM2
  401. COMPLEX CDOTC
  402. EXTERNAL CDOTC, SCNRM2
  403. INTEGER ISAMAX
  404. EXTERNAL ISAMAX
  405. * from LAPACK
  406. REAL SLAMCH, SROUNDUP_LWORK
  407. EXTERNAL SLAMCH, SROUNDUP_LWORK
  408. LOGICAL LSAME
  409. EXTERNAL LSAME
  410. * ..
  411. * .. External Subroutines ..
  412. * ..
  413. * from BLAS
  414. EXTERNAL CCOPY, CROT, CSSCAL, CSWAP, CAXPY
  415. * from LAPACK
  416. EXTERNAL CLASCL, CLASET, CLASSQ, SLASCL, XERBLA
  417. EXTERNAL CGSVJ0, CGSVJ1
  418. * ..
  419. * .. Executable Statements ..
  420. *
  421. * Test the input arguments
  422. *
  423. LSVEC = LSAME( JOBU, 'U' ) .OR. LSAME( JOBU, 'F' )
  424. UCTOL = LSAME( JOBU, 'C' )
  425. RSVEC = LSAME( JOBV, 'V' ) .OR. LSAME( JOBV, 'J' )
  426. APPLV = LSAME( JOBV, 'A' )
  427. UPPER = LSAME( JOBA, 'U' )
  428. LOWER = LSAME( JOBA, 'L' )
  429. *
  430. MINMN = MIN( M, N )
  431. IF( MINMN.EQ.0 ) THEN
  432. LWMIN = 1
  433. LRWMIN = 1
  434. ELSE
  435. LWMIN = M + N
  436. LRWMIN = MAX( 6, N )
  437. END IF
  438. *
  439. LQUERY = ( LWORK.EQ.-1 ) .OR. ( LRWORK.EQ.-1 )
  440. IF( .NOT.( UPPER .OR. LOWER .OR. LSAME( JOBA, 'G' ) ) ) THEN
  441. INFO = -1
  442. ELSE IF( .NOT.( LSVEC .OR. UCTOL .OR. LSAME( JOBU, 'N' ) ) ) THEN
  443. INFO = -2
  444. ELSE IF( .NOT.( RSVEC .OR. APPLV .OR. LSAME( JOBV, 'N' ) ) ) THEN
  445. INFO = -3
  446. ELSE IF( M.LT.0 ) THEN
  447. INFO = -4
  448. ELSE IF( ( N.LT.0 ) .OR. ( N.GT.M ) ) THEN
  449. INFO = -5
  450. ELSE IF( LDA.LT.M ) THEN
  451. INFO = -7
  452. ELSE IF( MV.LT.0 ) THEN
  453. INFO = -9
  454. ELSE IF( ( RSVEC .AND. ( LDV.LT.N ) ) .OR.
  455. $ ( APPLV .AND. ( LDV.LT.MV ) ) ) THEN
  456. INFO = -11
  457. ELSE IF( UCTOL .AND. ( RWORK( 1 ).LE.ONE ) ) THEN
  458. INFO = -12
  459. ELSE IF( LWORK.LT.LWMIN .AND. ( .NOT.LQUERY ) ) THEN
  460. INFO = -13
  461. ELSE IF( LRWORK.LT.LRWMIN .AND. ( .NOT.LQUERY ) ) THEN
  462. INFO = -15
  463. ELSE
  464. INFO = 0
  465. END IF
  466. *
  467. * #:(
  468. IF( INFO.NE.0 ) THEN
  469. CALL XERBLA( 'CGESVJ', -INFO )
  470. RETURN
  471. ELSE IF( LQUERY ) THEN
  472. CWORK( 1 ) = SROUNDUP_LWORK( LWMIN )
  473. RWORK( 1 ) = SROUNDUP_LWORK( LRWMIN )
  474. RETURN
  475. END IF
  476. *
  477. * #:) Quick return for void matrix
  478. *
  479. IF( MINMN.EQ.0 ) RETURN
  480. *
  481. * Set numerical parameters
  482. * The stopping criterion for Jacobi rotations is
  483. *
  484. * max_{i<>j}|A(:,i)^* * A(:,j)| / (||A(:,i)||*||A(:,j)||) < CTOL*EPS
  485. *
  486. * where EPS is the round-off and CTOL is defined as follows:
  487. *
  488. IF( UCTOL ) THEN
  489. * ... user controlled
  490. CTOL = RWORK( 1 )
  491. ELSE
  492. * ... default
  493. IF( LSVEC .OR. RSVEC .OR. APPLV ) THEN
  494. CTOL = SQRT( REAL( M ) )
  495. ELSE
  496. CTOL = REAL( M )
  497. END IF
  498. END IF
  499. * ... and the machine dependent parameters are
  500. *[!] (Make sure that SLAMCH() works properly on the target machine.)
  501. *
  502. EPSLN = SLAMCH( 'Epsilon' )
  503. ROOTEPS = SQRT( EPSLN )
  504. SFMIN = SLAMCH( 'SafeMinimum' )
  505. ROOTSFMIN = SQRT( SFMIN )
  506. SMALL = SFMIN / EPSLN
  507. * BIG = SLAMCH( 'Overflow' )
  508. BIG = ONE / SFMIN
  509. ROOTBIG = ONE / ROOTSFMIN
  510. * LARGE = BIG / SQRT( REAL( M*N ) )
  511. BIGTHETA = ONE / ROOTEPS
  512. *
  513. TOL = CTOL*EPSLN
  514. ROOTTOL = SQRT( TOL )
  515. *
  516. IF( REAL( M )*EPSLN.GE.ONE ) THEN
  517. INFO = -4
  518. CALL XERBLA( 'CGESVJ', -INFO )
  519. RETURN
  520. END IF
  521. *
  522. * Initialize the right singular vector matrix.
  523. *
  524. IF( RSVEC ) THEN
  525. MVL = N
  526. CALL CLASET( 'A', MVL, N, CZERO, CONE, V, LDV )
  527. ELSE IF( APPLV ) THEN
  528. MVL = MV
  529. END IF
  530. RSVEC = RSVEC .OR. APPLV
  531. *
  532. * Initialize SVA( 1:N ) = ( ||A e_i||_2, i = 1:N )
  533. *(!) If necessary, scale A to protect the largest singular value
  534. * from overflow. It is possible that saving the largest singular
  535. * value destroys the information about the small ones.
  536. * This initial scaling is almost minimal in the sense that the
  537. * goal is to make sure that no column norm overflows, and that
  538. * SQRT(N)*max_i SVA(i) does not overflow. If INFinite entries
  539. * in A are detected, the procedure returns with INFO=-6.
  540. *
  541. SKL = ONE / SQRT( REAL( M )*REAL( N ) )
  542. NOSCALE = .TRUE.
  543. GOSCALE = .TRUE.
  544. *
  545. IF( LOWER ) THEN
  546. * the input matrix is M-by-N lower triangular (trapezoidal)
  547. DO 1874 p = 1, N
  548. AAPP = ZERO
  549. AAQQ = ONE
  550. CALL CLASSQ( M-p+1, A( p, p ), 1, AAPP, AAQQ )
  551. IF( AAPP.GT.BIG ) THEN
  552. INFO = -6
  553. CALL XERBLA( 'CGESVJ', -INFO )
  554. RETURN
  555. END IF
  556. AAQQ = SQRT( AAQQ )
  557. IF( ( AAPP.LT.( BIG / AAQQ ) ) .AND. NOSCALE ) THEN
  558. SVA( p ) = AAPP*AAQQ
  559. ELSE
  560. NOSCALE = .FALSE.
  561. SVA( p ) = AAPP*( AAQQ*SKL )
  562. IF( GOSCALE ) THEN
  563. GOSCALE = .FALSE.
  564. DO 1873 q = 1, p - 1
  565. SVA( q ) = SVA( q )*SKL
  566. 1873 CONTINUE
  567. END IF
  568. END IF
  569. 1874 CONTINUE
  570. ELSE IF( UPPER ) THEN
  571. * the input matrix is M-by-N upper triangular (trapezoidal)
  572. DO 2874 p = 1, N
  573. AAPP = ZERO
  574. AAQQ = ONE
  575. CALL CLASSQ( p, A( 1, p ), 1, AAPP, AAQQ )
  576. IF( AAPP.GT.BIG ) THEN
  577. INFO = -6
  578. CALL XERBLA( 'CGESVJ', -INFO )
  579. RETURN
  580. END IF
  581. AAQQ = SQRT( AAQQ )
  582. IF( ( AAPP.LT.( BIG / AAQQ ) ) .AND. NOSCALE ) THEN
  583. SVA( p ) = AAPP*AAQQ
  584. ELSE
  585. NOSCALE = .FALSE.
  586. SVA( p ) = AAPP*( AAQQ*SKL )
  587. IF( GOSCALE ) THEN
  588. GOSCALE = .FALSE.
  589. DO 2873 q = 1, p - 1
  590. SVA( q ) = SVA( q )*SKL
  591. 2873 CONTINUE
  592. END IF
  593. END IF
  594. 2874 CONTINUE
  595. ELSE
  596. * the input matrix is M-by-N general dense
  597. DO 3874 p = 1, N
  598. AAPP = ZERO
  599. AAQQ = ONE
  600. CALL CLASSQ( M, A( 1, p ), 1, AAPP, AAQQ )
  601. IF( AAPP.GT.BIG ) THEN
  602. INFO = -6
  603. CALL XERBLA( 'CGESVJ', -INFO )
  604. RETURN
  605. END IF
  606. AAQQ = SQRT( AAQQ )
  607. IF( ( AAPP.LT.( BIG / AAQQ ) ) .AND. NOSCALE ) THEN
  608. SVA( p ) = AAPP*AAQQ
  609. ELSE
  610. NOSCALE = .FALSE.
  611. SVA( p ) = AAPP*( AAQQ*SKL )
  612. IF( GOSCALE ) THEN
  613. GOSCALE = .FALSE.
  614. DO 3873 q = 1, p - 1
  615. SVA( q ) = SVA( q )*SKL
  616. 3873 CONTINUE
  617. END IF
  618. END IF
  619. 3874 CONTINUE
  620. END IF
  621. *
  622. IF( NOSCALE )SKL = ONE
  623. *
  624. * Move the smaller part of the spectrum from the underflow threshold
  625. *(!) Start by determining the position of the nonzero entries of the
  626. * array SVA() relative to ( SFMIN, BIG ).
  627. *
  628. AAPP = ZERO
  629. AAQQ = BIG
  630. DO 4781 p = 1, N
  631. IF( SVA( p ).NE.ZERO )AAQQ = MIN( AAQQ, SVA( p ) )
  632. AAPP = MAX( AAPP, SVA( p ) )
  633. 4781 CONTINUE
  634. *
  635. * #:) Quick return for zero matrix
  636. *
  637. IF( AAPP.EQ.ZERO ) THEN
  638. IF( LSVEC )CALL CLASET( 'G', M, N, CZERO, CONE, A, LDA )
  639. RWORK( 1 ) = ONE
  640. RWORK( 2 ) = ZERO
  641. RWORK( 3 ) = ZERO
  642. RWORK( 4 ) = ZERO
  643. RWORK( 5 ) = ZERO
  644. RWORK( 6 ) = ZERO
  645. RETURN
  646. END IF
  647. *
  648. * #:) Quick return for one-column matrix
  649. *
  650. IF( N.EQ.1 ) THEN
  651. IF( LSVEC )CALL CLASCL( 'G', 0, 0, SVA( 1 ), SKL, M, 1,
  652. $ A( 1, 1 ), LDA, IERR )
  653. RWORK( 1 ) = ONE / SKL
  654. IF( SVA( 1 ).GE.SFMIN ) THEN
  655. RWORK( 2 ) = ONE
  656. ELSE
  657. RWORK( 2 ) = ZERO
  658. END IF
  659. RWORK( 3 ) = ZERO
  660. RWORK( 4 ) = ZERO
  661. RWORK( 5 ) = ZERO
  662. RWORK( 6 ) = ZERO
  663. RETURN
  664. END IF
  665. *
  666. * Protect small singular values from underflow, and try to
  667. * avoid underflows/overflows in computing Jacobi rotations.
  668. *
  669. SN = SQRT( SFMIN / EPSLN )
  670. TEMP1 = SQRT( BIG / REAL( N ) )
  671. IF( ( AAPP.LE.SN ) .OR. ( AAQQ.GE.TEMP1 ) .OR.
  672. $ ( ( SN.LE.AAQQ ) .AND. ( AAPP.LE.TEMP1 ) ) ) THEN
  673. TEMP1 = MIN( BIG, TEMP1 / AAPP )
  674. * AAQQ = AAQQ*TEMP1
  675. * AAPP = AAPP*TEMP1
  676. ELSE IF( ( AAQQ.LE.SN ) .AND. ( AAPP.LE.TEMP1 ) ) THEN
  677. TEMP1 = MIN( SN / AAQQ, BIG / ( AAPP*SQRT( REAL( N ) ) ) )
  678. * AAQQ = AAQQ*TEMP1
  679. * AAPP = AAPP*TEMP1
  680. ELSE IF( ( AAQQ.GE.SN ) .AND. ( AAPP.GE.TEMP1 ) ) THEN
  681. TEMP1 = MAX( SN / AAQQ, TEMP1 / AAPP )
  682. * AAQQ = AAQQ*TEMP1
  683. * AAPP = AAPP*TEMP1
  684. ELSE IF( ( AAQQ.LE.SN ) .AND. ( AAPP.GE.TEMP1 ) ) THEN
  685. TEMP1 = MIN( SN / AAQQ, BIG / ( SQRT( REAL( N ) )*AAPP ) )
  686. * AAQQ = AAQQ*TEMP1
  687. * AAPP = AAPP*TEMP1
  688. ELSE
  689. TEMP1 = ONE
  690. END IF
  691. *
  692. * Scale, if necessary
  693. *
  694. IF( TEMP1.NE.ONE ) THEN
  695. CALL SLASCL( 'G', 0, 0, ONE, TEMP1, N, 1, SVA, N, IERR )
  696. END IF
  697. SKL = TEMP1*SKL
  698. IF( SKL.NE.ONE ) THEN
  699. CALL CLASCL( JOBA, 0, 0, ONE, SKL, M, N, A, LDA, IERR )
  700. SKL = ONE / SKL
  701. END IF
  702. *
  703. * Row-cyclic Jacobi SVD algorithm with column pivoting
  704. *
  705. EMPTSW = ( N*( N-1 ) ) / 2
  706. NOTROT = 0
  707. DO 1868 q = 1, N
  708. CWORK( q ) = CONE
  709. 1868 CONTINUE
  710. *
  711. *
  712. *
  713. SWBAND = 3
  714. *[TP] SWBAND is a tuning parameter [TP]. It is meaningful and effective
  715. * if CGESVJ is used as a computational routine in the preconditioned
  716. * Jacobi SVD algorithm CGEJSV. For sweeps i=1:SWBAND the procedure
  717. * works on pivots inside a band-like region around the diagonal.
  718. * The boundaries are determined dynamically, based on the number of
  719. * pivots above a threshold.
  720. *
  721. KBL = MIN( 8, N )
  722. *[TP] KBL is a tuning parameter that defines the tile size in the
  723. * tiling of the p-q loops of pivot pairs. In general, an optimal
  724. * value of KBL depends on the matrix dimensions and on the
  725. * parameters of the computer's memory.
  726. *
  727. NBL = N / KBL
  728. IF( ( NBL*KBL ).NE.N )NBL = NBL + 1
  729. *
  730. BLSKIP = KBL**2
  731. *[TP] BLKSKIP is a tuning parameter that depends on SWBAND and KBL.
  732. *
  733. ROWSKIP = MIN( 5, KBL )
  734. *[TP] ROWSKIP is a tuning parameter.
  735. *
  736. LKAHEAD = 1
  737. *[TP] LKAHEAD is a tuning parameter.
  738. *
  739. * Quasi block transformations, using the lower (upper) triangular
  740. * structure of the input matrix. The quasi-block-cycling usually
  741. * invokes cubic convergence. Big part of this cycle is done inside
  742. * canonical subspaces of dimensions less than M.
  743. *
  744. IF( ( LOWER .OR. UPPER ) .AND. ( N.GT.MAX( 64, 4*KBL ) ) ) THEN
  745. *[TP] The number of partition levels and the actual partition are
  746. * tuning parameters.
  747. N4 = N / 4
  748. N2 = N / 2
  749. N34 = 3*N4
  750. IF( APPLV ) THEN
  751. q = 0
  752. ELSE
  753. q = 1
  754. END IF
  755. *
  756. IF( LOWER ) THEN
  757. *
  758. * This works very well on lower triangular matrices, in particular
  759. * in the framework of the preconditioned Jacobi SVD (xGEJSV).
  760. * The idea is simple:
  761. * [+ 0 0 0] Note that Jacobi transformations of [0 0]
  762. * [+ + 0 0] [0 0]
  763. * [+ + x 0] actually work on [x 0] [x 0]
  764. * [+ + x x] [x x]. [x x]
  765. *
  766. CALL CGSVJ0( JOBV, M-N34, N-N34, A( N34+1, N34+1 ), LDA,
  767. $ CWORK( N34+1 ), SVA( N34+1 ), MVL,
  768. $ V( N34*q+1, N34+1 ), LDV, EPSLN, SFMIN, TOL,
  769. $ 2, CWORK( N+1 ), LWORK-N, IERR )
  770. CALL CGSVJ0( JOBV, M-N2, N34-N2, A( N2+1, N2+1 ), LDA,
  771. $ CWORK( N2+1 ), SVA( N2+1 ), MVL,
  772. $ V( N2*q+1, N2+1 ), LDV, EPSLN, SFMIN, TOL, 2,
  773. $ CWORK( N+1 ), LWORK-N, IERR )
  774. CALL CGSVJ1( JOBV, M-N2, N-N2, N4, A( N2+1, N2+1 ), LDA,
  775. $ CWORK( N2+1 ), SVA( N2+1 ), MVL,
  776. $ V( N2*q+1, N2+1 ), LDV, EPSLN, SFMIN, TOL, 1,
  777. $ CWORK( N+1 ), LWORK-N, IERR )
  778. *
  779. CALL CGSVJ0( JOBV, M-N4, N2-N4, A( N4+1, N4+1 ), LDA,
  780. $ CWORK( N4+1 ), SVA( N4+1 ), MVL,
  781. $ V( N4*q+1, N4+1 ), LDV, EPSLN, SFMIN, TOL, 1,
  782. $ CWORK( N+1 ), LWORK-N, IERR )
  783. *
  784. CALL CGSVJ0( JOBV, M, N4, A, LDA, CWORK, SVA, MVL, V, LDV,
  785. $ EPSLN, SFMIN, TOL, 1, CWORK( N+1 ), LWORK-N,
  786. $ IERR )
  787. *
  788. CALL CGSVJ1( JOBV, M, N2, N4, A, LDA, CWORK, SVA, MVL, V,
  789. $ LDV, EPSLN, SFMIN, TOL, 1, CWORK( N+1 ),
  790. $ LWORK-N, IERR )
  791. *
  792. *
  793. ELSE IF( UPPER ) THEN
  794. *
  795. *
  796. CALL CGSVJ0( JOBV, N4, N4, A, LDA, CWORK, SVA, MVL, V, LDV,
  797. $ EPSLN, SFMIN, TOL, 2, CWORK( N+1 ), LWORK-N,
  798. $ IERR )
  799. *
  800. CALL CGSVJ0( JOBV, N2, N4, A( 1, N4+1 ), LDA, CWORK( N4+1 ),
  801. $ SVA( N4+1 ), MVL, V( N4*q+1, N4+1 ), LDV,
  802. $ EPSLN, SFMIN, TOL, 1, CWORK( N+1 ), LWORK-N,
  803. $ IERR )
  804. *
  805. CALL CGSVJ1( JOBV, N2, N2, N4, A, LDA, CWORK, SVA, MVL, V,
  806. $ LDV, EPSLN, SFMIN, TOL, 1, CWORK( N+1 ),
  807. $ LWORK-N, IERR )
  808. *
  809. CALL CGSVJ0( JOBV, N2+N4, N4, A( 1, N2+1 ), LDA,
  810. $ CWORK( N2+1 ), SVA( N2+1 ), MVL,
  811. $ V( N2*q+1, N2+1 ), LDV, EPSLN, SFMIN, TOL, 1,
  812. $ CWORK( N+1 ), LWORK-N, IERR )
  813. END IF
  814. *
  815. END IF
  816. *
  817. * .. Row-cyclic pivot strategy with de Rijk's pivoting ..
  818. *
  819. DO 1993 i = 1, NSWEEP
  820. *
  821. * .. go go go ...
  822. *
  823. MXAAPQ = ZERO
  824. MXSINJ = ZERO
  825. ISWROT = 0
  826. *
  827. NOTROT = 0
  828. PSKIPPED = 0
  829. *
  830. * Each sweep is unrolled using KBL-by-KBL tiles over the pivot pairs
  831. * 1 <= p < q <= N. This is the first step toward a blocked implementation
  832. * of the rotations. New implementation, based on block transformations,
  833. * is under development.
  834. *
  835. DO 2000 ibr = 1, NBL
  836. *
  837. igl = ( ibr-1 )*KBL + 1
  838. *
  839. DO 1002 ir1 = 0, MIN( LKAHEAD, NBL-ibr )
  840. *
  841. igl = igl + ir1*KBL
  842. *
  843. DO 2001 p = igl, MIN( igl+KBL-1, N-1 )
  844. *
  845. * .. de Rijk's pivoting
  846. *
  847. q = ISAMAX( N-p+1, SVA( p ), 1 ) + p - 1
  848. IF( p.NE.q ) THEN
  849. CALL CSWAP( M, A( 1, p ), 1, A( 1, q ), 1 )
  850. IF( RSVEC )CALL CSWAP( MVL, V( 1, p ), 1,
  851. $ V( 1, q ), 1 )
  852. TEMP1 = SVA( p )
  853. SVA( p ) = SVA( q )
  854. SVA( q ) = TEMP1
  855. AAPQ = CWORK(p)
  856. CWORK(p) = CWORK(q)
  857. CWORK(q) = AAPQ
  858. END IF
  859. *
  860. IF( ir1.EQ.0 ) THEN
  861. *
  862. * Column norms are periodically updated by explicit
  863. * norm computation.
  864. *[!] Caveat:
  865. * Unfortunately, some BLAS implementations compute SCNRM2(M,A(1,p),1)
  866. * as SQRT(S=CDOTC(M,A(1,p),1,A(1,p),1)), which may cause the result to
  867. * overflow for ||A(:,p)||_2 > SQRT(overflow_threshold), and to
  868. * underflow for ||A(:,p)||_2 < SQRT(underflow_threshold).
  869. * Hence, SCNRM2 cannot be trusted, not even in the case when
  870. * the true norm is far from the under(over)flow boundaries.
  871. * If properly implemented SCNRM2 is available, the IF-THEN-ELSE-END IF
  872. * below should be replaced with "AAPP = SCNRM2( M, A(1,p), 1 )".
  873. *
  874. IF( ( SVA( p ).LT.ROOTBIG ) .AND.
  875. $ ( SVA( p ).GT.ROOTSFMIN ) ) THEN
  876. SVA( p ) = SCNRM2( M, A( 1, p ), 1 )
  877. ELSE
  878. TEMP1 = ZERO
  879. AAPP = ONE
  880. CALL CLASSQ( M, A( 1, p ), 1, TEMP1, AAPP )
  881. SVA( p ) = TEMP1*SQRT( AAPP )
  882. END IF
  883. AAPP = SVA( p )
  884. ELSE
  885. AAPP = SVA( p )
  886. END IF
  887. *
  888. IF( AAPP.GT.ZERO ) THEN
  889. *
  890. PSKIPPED = 0
  891. *
  892. DO 2002 q = p + 1, MIN( igl+KBL-1, N )
  893. *
  894. AAQQ = SVA( q )
  895. *
  896. IF( AAQQ.GT.ZERO ) THEN
  897. *
  898. AAPP0 = AAPP
  899. IF( AAQQ.GE.ONE ) THEN
  900. ROTOK = ( SMALL*AAPP ).LE.AAQQ
  901. IF( AAPP.LT.( BIG / AAQQ ) ) THEN
  902. AAPQ = ( CDOTC( M, A( 1, p ), 1,
  903. $ A( 1, q ), 1 ) / AAQQ ) / AAPP
  904. ELSE
  905. CALL CCOPY( M, A( 1, p ), 1,
  906. $ CWORK(N+1), 1 )
  907. CALL CLASCL( 'G', 0, 0, AAPP, ONE,
  908. $ M, 1, CWORK(N+1), LDA, IERR )
  909. AAPQ = CDOTC( M, CWORK(N+1), 1,
  910. $ A( 1, q ), 1 ) / AAQQ
  911. END IF
  912. ELSE
  913. ROTOK = AAPP.LE.( AAQQ / SMALL )
  914. IF( AAPP.GT.( SMALL / AAQQ ) ) THEN
  915. AAPQ = ( CDOTC( M, A( 1, p ), 1,
  916. $ A( 1, q ), 1 ) / AAPP ) / AAQQ
  917. ELSE
  918. CALL CCOPY( M, A( 1, q ), 1,
  919. $ CWORK(N+1), 1 )
  920. CALL CLASCL( 'G', 0, 0, AAQQ,
  921. $ ONE, M, 1,
  922. $ CWORK(N+1), LDA, IERR )
  923. AAPQ = CDOTC( M, A(1, p ), 1,
  924. $ CWORK(N+1), 1 ) / AAPP
  925. END IF
  926. END IF
  927. *
  928. * AAPQ = AAPQ * CONJG( CWORK(p) ) * CWORK(q)
  929. AAPQ1 = -ABS(AAPQ)
  930. MXAAPQ = MAX( MXAAPQ, -AAPQ1 )
  931. *
  932. * TO rotate or NOT to rotate, THAT is the question ...
  933. *
  934. IF( ABS( AAPQ1 ).GT.TOL ) THEN
  935. OMPQ = AAPQ / ABS(AAPQ)
  936. *
  937. * .. rotate
  938. *[RTD] ROTATED = ROTATED + ONE
  939. *
  940. IF( ir1.EQ.0 ) THEN
  941. NOTROT = 0
  942. PSKIPPED = 0
  943. ISWROT = ISWROT + 1
  944. END IF
  945. *
  946. IF( ROTOK ) THEN
  947. *
  948. AQOAP = AAQQ / AAPP
  949. APOAQ = AAPP / AAQQ
  950. THETA = -HALF*ABS( AQOAP-APOAQ )/AAPQ1
  951. *
  952. IF( ABS( THETA ).GT.BIGTHETA ) THEN
  953. *
  954. T = HALF / THETA
  955. CS = ONE
  956. CALL CROT( M, A(1,p), 1, A(1,q), 1,
  957. $ CS, CONJG(OMPQ)*T )
  958. IF ( RSVEC ) THEN
  959. CALL CROT( MVL, V(1,p), 1,
  960. $ V(1,q), 1, CS, CONJG(OMPQ)*T )
  961. END IF
  962. SVA( q ) = AAQQ*SQRT( MAX( ZERO,
  963. $ ONE+T*APOAQ*AAPQ1 ) )
  964. AAPP = AAPP*SQRT( MAX( ZERO,
  965. $ ONE-T*AQOAP*AAPQ1 ) )
  966. MXSINJ = MAX( MXSINJ, ABS( T ) )
  967. *
  968. ELSE
  969. *
  970. * .. choose correct signum for THETA and rotate
  971. *
  972. THSIGN = -SIGN( ONE, AAPQ1 )
  973. T = ONE / ( THETA+THSIGN*
  974. $ SQRT( ONE+THETA*THETA ) )
  975. CS = SQRT( ONE / ( ONE+T*T ) )
  976. SN = T*CS
  977. *
  978. MXSINJ = MAX( MXSINJ, ABS( SN ) )
  979. SVA( q ) = AAQQ*SQRT( MAX( ZERO,
  980. $ ONE+T*APOAQ*AAPQ1 ) )
  981. AAPP = AAPP*SQRT( MAX( ZERO,
  982. $ ONE-T*AQOAP*AAPQ1 ) )
  983. *
  984. CALL CROT( M, A(1,p), 1, A(1,q), 1,
  985. $ CS, CONJG(OMPQ)*SN )
  986. IF ( RSVEC ) THEN
  987. CALL CROT( MVL, V(1,p), 1,
  988. $ V(1,q), 1, CS, CONJG(OMPQ)*SN )
  989. END IF
  990. END IF
  991. CWORK(p) = -CWORK(q) * OMPQ
  992. *
  993. ELSE
  994. * .. have to use modified Gram-Schmidt like transformation
  995. CALL CCOPY( M, A( 1, p ), 1,
  996. $ CWORK(N+1), 1 )
  997. CALL CLASCL( 'G', 0, 0, AAPP, ONE, M,
  998. $ 1, CWORK(N+1), LDA,
  999. $ IERR )
  1000. CALL CLASCL( 'G', 0, 0, AAQQ, ONE, M,
  1001. $ 1, A( 1, q ), LDA, IERR )
  1002. CALL CAXPY( M, -AAPQ, CWORK(N+1), 1,
  1003. $ A( 1, q ), 1 )
  1004. CALL CLASCL( 'G', 0, 0, ONE, AAQQ, M,
  1005. $ 1, A( 1, q ), LDA, IERR )
  1006. SVA( q ) = AAQQ*SQRT( MAX( ZERO,
  1007. $ ONE-AAPQ1*AAPQ1 ) )
  1008. MXSINJ = MAX( MXSINJ, SFMIN )
  1009. END IF
  1010. * END IF ROTOK THEN ... ELSE
  1011. *
  1012. * In the case of cancellation in updating SVA(q), SVA(p)
  1013. * recompute SVA(q), SVA(p).
  1014. *
  1015. IF( ( SVA( q ) / AAQQ )**2.LE.ROOTEPS )
  1016. $ THEN
  1017. IF( ( AAQQ.LT.ROOTBIG ) .AND.
  1018. $ ( AAQQ.GT.ROOTSFMIN ) ) THEN
  1019. SVA( q ) = SCNRM2( M, A( 1, q ), 1 )
  1020. ELSE
  1021. T = ZERO
  1022. AAQQ = ONE
  1023. CALL CLASSQ( M, A( 1, q ), 1, T,
  1024. $ AAQQ )
  1025. SVA( q ) = T*SQRT( AAQQ )
  1026. END IF
  1027. END IF
  1028. IF( ( AAPP / AAPP0 ).LE.ROOTEPS ) THEN
  1029. IF( ( AAPP.LT.ROOTBIG ) .AND.
  1030. $ ( AAPP.GT.ROOTSFMIN ) ) THEN
  1031. AAPP = SCNRM2( M, A( 1, p ), 1 )
  1032. ELSE
  1033. T = ZERO
  1034. AAPP = ONE
  1035. CALL CLASSQ( M, A( 1, p ), 1, T,
  1036. $ AAPP )
  1037. AAPP = T*SQRT( AAPP )
  1038. END IF
  1039. SVA( p ) = AAPP
  1040. END IF
  1041. *
  1042. ELSE
  1043. * A(:,p) and A(:,q) already numerically orthogonal
  1044. IF( ir1.EQ.0 )NOTROT = NOTROT + 1
  1045. *[RTD] SKIPPED = SKIPPED + 1
  1046. PSKIPPED = PSKIPPED + 1
  1047. END IF
  1048. ELSE
  1049. * A(:,q) is zero column
  1050. IF( ir1.EQ.0 )NOTROT = NOTROT + 1
  1051. PSKIPPED = PSKIPPED + 1
  1052. END IF
  1053. *
  1054. IF( ( i.LE.SWBAND ) .AND.
  1055. $ ( PSKIPPED.GT.ROWSKIP ) ) THEN
  1056. IF( ir1.EQ.0 )AAPP = -AAPP
  1057. NOTROT = 0
  1058. GO TO 2103
  1059. END IF
  1060. *
  1061. 2002 CONTINUE
  1062. * END q-LOOP
  1063. *
  1064. 2103 CONTINUE
  1065. * bailed out of q-loop
  1066. *
  1067. SVA( p ) = AAPP
  1068. *
  1069. ELSE
  1070. SVA( p ) = AAPP
  1071. IF( ( ir1.EQ.0 ) .AND. ( AAPP.EQ.ZERO ) )
  1072. $ NOTROT = NOTROT + MIN( igl+KBL-1, N ) - p
  1073. END IF
  1074. *
  1075. 2001 CONTINUE
  1076. * end of the p-loop
  1077. * end of doing the block ( ibr, ibr )
  1078. 1002 CONTINUE
  1079. * end of ir1-loop
  1080. *
  1081. * ... go to the off diagonal blocks
  1082. *
  1083. igl = ( ibr-1 )*KBL + 1
  1084. *
  1085. DO 2010 jbc = ibr + 1, NBL
  1086. *
  1087. jgl = ( jbc-1 )*KBL + 1
  1088. *
  1089. * doing the block at ( ibr, jbc )
  1090. *
  1091. IJBLSK = 0
  1092. DO 2100 p = igl, MIN( igl+KBL-1, N )
  1093. *
  1094. AAPP = SVA( p )
  1095. IF( AAPP.GT.ZERO ) THEN
  1096. *
  1097. PSKIPPED = 0
  1098. *
  1099. DO 2200 q = jgl, MIN( jgl+KBL-1, N )
  1100. *
  1101. AAQQ = SVA( q )
  1102. IF( AAQQ.GT.ZERO ) THEN
  1103. AAPP0 = AAPP
  1104. *
  1105. * .. M x 2 Jacobi SVD ..
  1106. *
  1107. * Safe Gram matrix computation
  1108. *
  1109. IF( AAQQ.GE.ONE ) THEN
  1110. IF( AAPP.GE.AAQQ ) THEN
  1111. ROTOK = ( SMALL*AAPP ).LE.AAQQ
  1112. ELSE
  1113. ROTOK = ( SMALL*AAQQ ).LE.AAPP
  1114. END IF
  1115. IF( AAPP.LT.( BIG / AAQQ ) ) THEN
  1116. AAPQ = ( CDOTC( M, A( 1, p ), 1,
  1117. $ A( 1, q ), 1 ) / AAQQ ) / AAPP
  1118. ELSE
  1119. CALL CCOPY( M, A( 1, p ), 1,
  1120. $ CWORK(N+1), 1 )
  1121. CALL CLASCL( 'G', 0, 0, AAPP,
  1122. $ ONE, M, 1,
  1123. $ CWORK(N+1), LDA, IERR )
  1124. AAPQ = CDOTC( M, CWORK(N+1), 1,
  1125. $ A( 1, q ), 1 ) / AAQQ
  1126. END IF
  1127. ELSE
  1128. IF( AAPP.GE.AAQQ ) THEN
  1129. ROTOK = AAPP.LE.( AAQQ / SMALL )
  1130. ELSE
  1131. ROTOK = AAQQ.LE.( AAPP / SMALL )
  1132. END IF
  1133. IF( AAPP.GT.( SMALL / AAQQ ) ) THEN
  1134. AAPQ = ( CDOTC( M, A( 1, p ), 1,
  1135. $ A( 1, q ), 1 ) / MAX(AAQQ,AAPP) )
  1136. $ / MIN(AAQQ,AAPP)
  1137. ELSE
  1138. CALL CCOPY( M, A( 1, q ), 1,
  1139. $ CWORK(N+1), 1 )
  1140. CALL CLASCL( 'G', 0, 0, AAQQ,
  1141. $ ONE, M, 1,
  1142. $ CWORK(N+1), LDA, IERR )
  1143. AAPQ = CDOTC( M, A( 1, p ), 1,
  1144. $ CWORK(N+1), 1 ) / AAPP
  1145. END IF
  1146. END IF
  1147. *
  1148. * AAPQ = AAPQ * CONJG(CWORK(p))*CWORK(q)
  1149. AAPQ1 = -ABS(AAPQ)
  1150. MXAAPQ = MAX( MXAAPQ, -AAPQ1 )
  1151. *
  1152. * TO rotate or NOT to rotate, THAT is the question ...
  1153. *
  1154. IF( ABS( AAPQ1 ).GT.TOL ) THEN
  1155. OMPQ = AAPQ / ABS(AAPQ)
  1156. NOTROT = 0
  1157. *[RTD] ROTATED = ROTATED + 1
  1158. PSKIPPED = 0
  1159. ISWROT = ISWROT + 1
  1160. *
  1161. IF( ROTOK ) THEN
  1162. *
  1163. AQOAP = AAQQ / AAPP
  1164. APOAQ = AAPP / AAQQ
  1165. THETA = -HALF*ABS( AQOAP-APOAQ )/ AAPQ1
  1166. IF( AAQQ.GT.AAPP0 )THETA = -THETA
  1167. *
  1168. IF( ABS( THETA ).GT.BIGTHETA ) THEN
  1169. T = HALF / THETA
  1170. CS = ONE
  1171. CALL CROT( M, A(1,p), 1, A(1,q), 1,
  1172. $ CS, CONJG(OMPQ)*T )
  1173. IF( RSVEC ) THEN
  1174. CALL CROT( MVL, V(1,p), 1,
  1175. $ V(1,q), 1, CS, CONJG(OMPQ)*T )
  1176. END IF
  1177. SVA( q ) = AAQQ*SQRT( MAX( ZERO,
  1178. $ ONE+T*APOAQ*AAPQ1 ) )
  1179. AAPP = AAPP*SQRT( MAX( ZERO,
  1180. $ ONE-T*AQOAP*AAPQ1 ) )
  1181. MXSINJ = MAX( MXSINJ, ABS( T ) )
  1182. ELSE
  1183. *
  1184. * .. choose correct signum for THETA and rotate
  1185. *
  1186. THSIGN = -SIGN( ONE, AAPQ1 )
  1187. IF( AAQQ.GT.AAPP0 )THSIGN = -THSIGN
  1188. T = ONE / ( THETA+THSIGN*
  1189. $ SQRT( ONE+THETA*THETA ) )
  1190. CS = SQRT( ONE / ( ONE+T*T ) )
  1191. SN = T*CS
  1192. MXSINJ = MAX( MXSINJ, ABS( SN ) )
  1193. SVA( q ) = AAQQ*SQRT( MAX( ZERO,
  1194. $ ONE+T*APOAQ*AAPQ1 ) )
  1195. AAPP = AAPP*SQRT( MAX( ZERO,
  1196. $ ONE-T*AQOAP*AAPQ1 ) )
  1197. *
  1198. CALL CROT( M, A(1,p), 1, A(1,q), 1,
  1199. $ CS, CONJG(OMPQ)*SN )
  1200. IF( RSVEC ) THEN
  1201. CALL CROT( MVL, V(1,p), 1,
  1202. $ V(1,q), 1, CS, CONJG(OMPQ)*SN )
  1203. END IF
  1204. END IF
  1205. CWORK(p) = -CWORK(q) * OMPQ
  1206. *
  1207. ELSE
  1208. * .. have to use modified Gram-Schmidt like transformation
  1209. IF( AAPP.GT.AAQQ ) THEN
  1210. CALL CCOPY( M, A( 1, p ), 1,
  1211. $ CWORK(N+1), 1 )
  1212. CALL CLASCL( 'G', 0, 0, AAPP, ONE,
  1213. $ M, 1, CWORK(N+1),LDA,
  1214. $ IERR )
  1215. CALL CLASCL( 'G', 0, 0, AAQQ, ONE,
  1216. $ M, 1, A( 1, q ), LDA,
  1217. $ IERR )
  1218. CALL CAXPY( M, -AAPQ, CWORK(N+1),
  1219. $ 1, A( 1, q ), 1 )
  1220. CALL CLASCL( 'G', 0, 0, ONE, AAQQ,
  1221. $ M, 1, A( 1, q ), LDA,
  1222. $ IERR )
  1223. SVA( q ) = AAQQ*SQRT( MAX( ZERO,
  1224. $ ONE-AAPQ1*AAPQ1 ) )
  1225. MXSINJ = MAX( MXSINJ, SFMIN )
  1226. ELSE
  1227. CALL CCOPY( M, A( 1, q ), 1,
  1228. $ CWORK(N+1), 1 )
  1229. CALL CLASCL( 'G', 0, 0, AAQQ, ONE,
  1230. $ M, 1, CWORK(N+1),LDA,
  1231. $ IERR )
  1232. CALL CLASCL( 'G', 0, 0, AAPP, ONE,
  1233. $ M, 1, A( 1, p ), LDA,
  1234. $ IERR )
  1235. CALL CAXPY( M, -CONJG(AAPQ),
  1236. $ CWORK(N+1), 1, A( 1, p ), 1 )
  1237. CALL CLASCL( 'G', 0, 0, ONE, AAPP,
  1238. $ M, 1, A( 1, p ), LDA,
  1239. $ IERR )
  1240. SVA( p ) = AAPP*SQRT( MAX( ZERO,
  1241. $ ONE-AAPQ1*AAPQ1 ) )
  1242. MXSINJ = MAX( MXSINJ, SFMIN )
  1243. END IF
  1244. END IF
  1245. * END IF ROTOK THEN ... ELSE
  1246. *
  1247. * In the case of cancellation in updating SVA(q), SVA(p)
  1248. * .. recompute SVA(q), SVA(p)
  1249. IF( ( SVA( q ) / AAQQ )**2.LE.ROOTEPS )
  1250. $ THEN
  1251. IF( ( AAQQ.LT.ROOTBIG ) .AND.
  1252. $ ( AAQQ.GT.ROOTSFMIN ) ) THEN
  1253. SVA( q ) = SCNRM2( M, A( 1, q ), 1)
  1254. ELSE
  1255. T = ZERO
  1256. AAQQ = ONE
  1257. CALL CLASSQ( M, A( 1, q ), 1, T,
  1258. $ AAQQ )
  1259. SVA( q ) = T*SQRT( AAQQ )
  1260. END IF
  1261. END IF
  1262. IF( ( AAPP / AAPP0 )**2.LE.ROOTEPS ) THEN
  1263. IF( ( AAPP.LT.ROOTBIG ) .AND.
  1264. $ ( AAPP.GT.ROOTSFMIN ) ) THEN
  1265. AAPP = SCNRM2( M, A( 1, p ), 1 )
  1266. ELSE
  1267. T = ZERO
  1268. AAPP = ONE
  1269. CALL CLASSQ( M, A( 1, p ), 1, T,
  1270. $ AAPP )
  1271. AAPP = T*SQRT( AAPP )
  1272. END IF
  1273. SVA( p ) = AAPP
  1274. END IF
  1275. * end of OK rotation
  1276. ELSE
  1277. NOTROT = NOTROT + 1
  1278. *[RTD] SKIPPED = SKIPPED + 1
  1279. PSKIPPED = PSKIPPED + 1
  1280. IJBLSK = IJBLSK + 1
  1281. END IF
  1282. ELSE
  1283. NOTROT = NOTROT + 1
  1284. PSKIPPED = PSKIPPED + 1
  1285. IJBLSK = IJBLSK + 1
  1286. END IF
  1287. *
  1288. IF( ( i.LE.SWBAND ) .AND. ( IJBLSK.GE.BLSKIP ) )
  1289. $ THEN
  1290. SVA( p ) = AAPP
  1291. NOTROT = 0
  1292. GO TO 2011
  1293. END IF
  1294. IF( ( i.LE.SWBAND ) .AND.
  1295. $ ( PSKIPPED.GT.ROWSKIP ) ) THEN
  1296. AAPP = -AAPP
  1297. NOTROT = 0
  1298. GO TO 2203
  1299. END IF
  1300. *
  1301. 2200 CONTINUE
  1302. * end of the q-loop
  1303. 2203 CONTINUE
  1304. *
  1305. SVA( p ) = AAPP
  1306. *
  1307. ELSE
  1308. *
  1309. IF( AAPP.EQ.ZERO )NOTROT = NOTROT +
  1310. $ MIN( jgl+KBL-1, N ) - jgl + 1
  1311. IF( AAPP.LT.ZERO )NOTROT = 0
  1312. *
  1313. END IF
  1314. *
  1315. 2100 CONTINUE
  1316. * end of the p-loop
  1317. 2010 CONTINUE
  1318. * end of the jbc-loop
  1319. 2011 CONTINUE
  1320. *2011 bailed out of the jbc-loop
  1321. DO 2012 p = igl, MIN( igl+KBL-1, N )
  1322. SVA( p ) = ABS( SVA( p ) )
  1323. 2012 CONTINUE
  1324. ***
  1325. 2000 CONTINUE
  1326. *2000 :: end of the ibr-loop
  1327. *
  1328. * .. update SVA(N)
  1329. IF( ( SVA( N ).LT.ROOTBIG ) .AND. ( SVA( N ).GT.ROOTSFMIN ) )
  1330. $ THEN
  1331. SVA( N ) = SCNRM2( M, A( 1, N ), 1 )
  1332. ELSE
  1333. T = ZERO
  1334. AAPP = ONE
  1335. CALL CLASSQ( M, A( 1, N ), 1, T, AAPP )
  1336. SVA( N ) = T*SQRT( AAPP )
  1337. END IF
  1338. *
  1339. * Additional steering devices
  1340. *
  1341. IF( ( i.LT.SWBAND ) .AND. ( ( MXAAPQ.LE.ROOTTOL ) .OR.
  1342. $ ( ISWROT.LE.N ) ) )SWBAND = i
  1343. *
  1344. IF( ( i.GT.SWBAND+1 ) .AND. ( MXAAPQ.LT.SQRT( REAL( N ) )*
  1345. $ TOL ) .AND. ( REAL( N )*MXAAPQ*MXSINJ.LT.TOL ) ) THEN
  1346. GO TO 1994
  1347. END IF
  1348. *
  1349. IF( NOTROT.GE.EMPTSW )GO TO 1994
  1350. *
  1351. 1993 CONTINUE
  1352. * end i=1:NSWEEP loop
  1353. *
  1354. * #:( Reaching this point means that the procedure has not converged.
  1355. INFO = NSWEEP - 1
  1356. GO TO 1995
  1357. *
  1358. 1994 CONTINUE
  1359. * #:) Reaching this point means numerical convergence after the i-th
  1360. * sweep.
  1361. *
  1362. INFO = 0
  1363. * #:) INFO = 0 confirms successful iterations.
  1364. 1995 CONTINUE
  1365. *
  1366. * Sort the singular values and find how many are above
  1367. * the underflow threshold.
  1368. *
  1369. N2 = 0
  1370. N4 = 0
  1371. DO 5991 p = 1, N - 1
  1372. q = ISAMAX( N-p+1, SVA( p ), 1 ) + p - 1
  1373. IF( p.NE.q ) THEN
  1374. TEMP1 = SVA( p )
  1375. SVA( p ) = SVA( q )
  1376. SVA( q ) = TEMP1
  1377. CALL CSWAP( M, A( 1, p ), 1, A( 1, q ), 1 )
  1378. IF( RSVEC )CALL CSWAP( MVL, V( 1, p ), 1, V( 1, q ), 1 )
  1379. END IF
  1380. IF( SVA( p ).NE.ZERO ) THEN
  1381. N4 = N4 + 1
  1382. IF( SVA( p )*SKL.GT.SFMIN )N2 = N2 + 1
  1383. END IF
  1384. 5991 CONTINUE
  1385. IF( SVA( N ).NE.ZERO ) THEN
  1386. N4 = N4 + 1
  1387. IF( SVA( N )*SKL.GT.SFMIN )N2 = N2 + 1
  1388. END IF
  1389. *
  1390. * Normalize the left singular vectors.
  1391. *
  1392. IF( LSVEC .OR. UCTOL ) THEN
  1393. DO 1998 p = 1, N4
  1394. * CALL CSSCAL( M, ONE / SVA( p ), A( 1, p ), 1 )
  1395. CALL CLASCL( 'G',0,0, SVA(p), ONE, M, 1, A(1,p), M, IERR )
  1396. 1998 CONTINUE
  1397. END IF
  1398. *
  1399. * Scale the product of Jacobi rotations.
  1400. *
  1401. IF( RSVEC ) THEN
  1402. DO 2399 p = 1, N
  1403. TEMP1 = ONE / SCNRM2( MVL, V( 1, p ), 1 )
  1404. CALL CSSCAL( MVL, TEMP1, V( 1, p ), 1 )
  1405. 2399 CONTINUE
  1406. END IF
  1407. *
  1408. * Undo scaling, if necessary (and possible).
  1409. IF( ( ( SKL.GT.ONE ) .AND. ( SVA( 1 ).LT.( BIG / SKL ) ) )
  1410. $ .OR. ( ( SKL.LT.ONE ) .AND. ( SVA( MAX( N2, 1 ) ) .GT.
  1411. $ ( SFMIN / SKL ) ) ) ) THEN
  1412. DO 2400 p = 1, N
  1413. SVA( P ) = SKL*SVA( P )
  1414. 2400 CONTINUE
  1415. SKL = ONE
  1416. END IF
  1417. *
  1418. RWORK( 1 ) = SKL
  1419. * The singular values of A are SKL*SVA(1:N). If SKL.NE.ONE
  1420. * then some of the singular values may overflow or underflow and
  1421. * the spectrum is given in this factored representation.
  1422. *
  1423. RWORK( 2 ) = REAL( N4 )
  1424. * N4 is the number of computed nonzero singular values of A.
  1425. *
  1426. RWORK( 3 ) = REAL( N2 )
  1427. * N2 is the number of singular values of A greater than SFMIN.
  1428. * If N2<N, SVA(N2:N) contains ZEROS and/or denormalized numbers
  1429. * that may carry some information.
  1430. *
  1431. RWORK( 4 ) = REAL( i )
  1432. * i is the index of the last sweep before declaring convergence.
  1433. *
  1434. RWORK( 5 ) = MXAAPQ
  1435. * MXAAPQ is the largest absolute value of scaled pivots in the
  1436. * last sweep
  1437. *
  1438. RWORK( 6 ) = MXSINJ
  1439. * MXSINJ is the largest absolute value of the sines of Jacobi angles
  1440. * in the last sweep
  1441. *
  1442. RETURN
  1443. * ..
  1444. * .. END OF CGESVJ
  1445. * ..
  1446. END
  1447. *