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

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