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

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