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

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