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

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