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.

zgesvj.f 56 kB

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