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 56 kB

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