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

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