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.

cgejsv.f 76 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. *> \brief \b CGEJSV
  2. *
  3. * =========== DOCUMENTATION ===========
  4. *
  5. * Online html documentation available at
  6. * http://www.netlib.org/lapack/explore-html/
  7. *
  8. *> \htmlonly
  9. *> Download CGEJSV + dependencies
  10. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgejsv.f">
  11. *> [TGZ]</a>
  12. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgejsv.f">
  13. *> [ZIP]</a>
  14. *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgejsv.f">
  15. *> [TXT]</a>
  16. *> \endhtmlonly
  17. *
  18. * Definition:
  19. * ===========
  20. *
  21. * SUBROUTINE CGEJSV( JOBA, JOBU, JOBV, JOBR, JOBT, JOBP,
  22. * M, N, A, LDA, SVA, U, LDU, V, LDV,
  23. * CWORK, LWORK, RWORK, LRWORK, IWORK, INFO )
  24. *
  25. * .. Scalar Arguments ..
  26. * IMPLICIT NONE
  27. * INTEGER INFO, LDA, LDU, LDV, LWORK, M, N
  28. * ..
  29. * .. Array Arguments ..
  30. * COMPLEX A( LDA, * ), U( LDU, * ), V( LDV, * ), CWORK( LWORK )
  31. * REAL SVA( N ), RWORK( LRWORK )
  32. * INTEGER IWORK( * )
  33. * CHARACTER*1 JOBA, JOBP, JOBR, JOBT, JOBU, JOBV
  34. * ..
  35. *
  36. *
  37. *> \par Purpose:
  38. * =============
  39. *>
  40. *> \verbatim
  41. *>
  42. *> CGEJSV computes the singular value decomposition (SVD) of a real M-by-N
  43. *> matrix [A], where M >= N. The SVD of [A] is written as
  44. *>
  45. *> [A] = [U] * [SIGMA] * [V]^*,
  46. *>
  47. *> where [SIGMA] is an N-by-N (M-by-N) matrix which is zero except for its N
  48. *> diagonal elements, [U] is an M-by-N (or M-by-M) orthonormal matrix, and
  49. *> [V] is an N-by-N orthogonal matrix. The diagonal elements of [SIGMA] are
  50. *> the singular values of [A]. The columns of [U] and [V] are the left and
  51. *> the right singular vectors of [A], respectively. The matrices [U] and [V]
  52. *> are computed and stored in the arrays U and V, respectively. The diagonal
  53. *> of [SIGMA] is computed and stored in the array SVA.
  54. *>
  55. *> Arguments:
  56. *> ==========
  57. *>
  58. *> \param[in] JOBA
  59. *> \verbatim
  60. *> JOBA is CHARACTER*1
  61. *> Specifies the level of accuracy:
  62. *> = 'C': This option works well (high relative accuracy) if A = B * D,
  63. *> with well-conditioned B and arbitrary diagonal matrix D.
  64. *> The accuracy cannot be spoiled by COLUMN scaling. The
  65. *> accuracy of the computed output depends on the condition of
  66. *> B, and the procedure aims at the best theoretical accuracy.
  67. *> The relative error max_{i=1:N}|d sigma_i| / sigma_i is
  68. *> bounded by f(M,N)*epsilon* cond(B), independent of D.
  69. *> The input matrix is preprocessed with the QRF with column
  70. *> pivoting. This initial preprocessing and preconditioning by
  71. *> a rank revealing QR factorization is common for all values of
  72. *> JOBA. Additional actions are specified as follows:
  73. *> = 'E': Computation as with 'C' with an additional estimate of the
  74. *> condition number of B. It provides a realistic error bound.
  75. *> = 'F': If A = D1 * C * D2 with ill-conditioned diagonal scalings
  76. *> D1, D2, and well-conditioned matrix C, this option gives
  77. *> higher accuracy than the 'C' option. If the structure of the
  78. *> input matrix is not known, and relative accuracy is
  79. *> desirable, then this option is advisable. The input matrix A
  80. *> is preprocessed with QR factorization with FULL (row and
  81. *> column) pivoting.
  82. *> = 'G' Computation as with 'F' with an additional estimate of the
  83. *> condition number of B, where A=D*B. If A has heavily weighted
  84. *> rows, then using this condition number gives too pessimistic
  85. *> error bound.
  86. *> = 'A': Small singular values are the noise and the matrix is treated
  87. *> as numerically rank defficient. The error in the computed
  88. *> singular values is bounded by f(m,n)*epsilon*||A||.
  89. *> The computed SVD A = U * S * V^* restores A up to
  90. *> f(m,n)*epsilon*||A||.
  91. *> This gives the procedure the licence to discard (set to zero)
  92. *> all singular values below N*epsilon*||A||.
  93. *> = 'R': Similar as in 'A'. Rank revealing property of the initial
  94. *> QR factorization is used do reveal (using triangular factor)
  95. *> a gap sigma_{r+1} < epsilon * sigma_r in which case the
  96. *> numerical RANK is declared to be r. The SVD is computed with
  97. *> absolute error bounds, but more accurately than with 'A'.
  98. *> \endverbatim
  99. *>
  100. *> \param[in] JOBU
  101. *> \verbatim
  102. *> JOBU is CHARACTER*1
  103. *> Specifies whether to compute the columns of U:
  104. *> = 'U': N columns of U are returned in the array U.
  105. *> = 'F': full set of M left sing. vectors is returned in the array U.
  106. *> = 'W': U may be used as workspace of length M*N. See the description
  107. *> of U.
  108. *> = 'N': U is not computed.
  109. *> \endverbatim
  110. *>
  111. *> \param[in] JOBV
  112. *> \verbatim
  113. *> JOBV is CHARACTER*1
  114. *> Specifies whether to compute the matrix V:
  115. *> = 'V': N columns of V are returned in the array V; Jacobi rotations
  116. *> are not explicitly accumulated.
  117. *> = 'J': N columns of V are returned in the array V, but they are
  118. *> computed as the product of Jacobi rotations. This option is
  119. *> allowed only if JOBU .NE. 'N', i.e. in computing the full SVD.
  120. *> = 'W': V may be used as workspace of length N*N. See the description
  121. *> of V.
  122. *> = 'N': V is not computed.
  123. *> \endverbatim
  124. *>
  125. *> \param[in] JOBR
  126. *> \verbatim
  127. *> JOBR is CHARACTER*1
  128. *> Specifies the RANGE for the singular values. Issues the licence to
  129. *> set to zero small positive singular values if they are outside
  130. *> specified range. If A .NE. 0 is scaled so that the largest singular
  131. *> value of c*A is around SQRT(BIG), BIG=SLAMCH('O'), then JOBR issues
  132. *> the licence to kill columns of A whose norm in c*A is less than
  133. *> SQRT(SFMIN) (for JOBR.EQ.'R'), or less than SMALL=SFMIN/EPSLN,
  134. *> where SFMIN=SLAMCH('S'), EPSLN=SLAMCH('E').
  135. *> = 'N': Do not kill small columns of c*A. This option assumes that
  136. *> BLAS and QR factorizations and triangular solvers are
  137. *> implemented to work in that range. If the condition of A
  138. *> is greater than BIG, use CGESVJ.
  139. *> = 'R': RESTRICTED range for sigma(c*A) is [SQRT(SFMIN), SQRT(BIG)]
  140. *> (roughly, as described above). This option is recommended.
  141. *> ===========================
  142. *> For computing the singular values in the FULL range [SFMIN,BIG]
  143. *> use CGESVJ.
  144. *> \endverbatim
  145. *>
  146. *> \param[in] JOBT
  147. *> \verbatim
  148. *> JOBT is CHARACTER*1
  149. *> If the matrix is square then the procedure may determine to use
  150. *> transposed A if A^* seems to be better with respect to convergence.
  151. *> If the matrix is not square, JOBT is ignored. This is subject to
  152. *> changes in the future.
  153. *> The decision is based on two values of entropy over the adjoint
  154. *> orbit of A^* * A. See the descriptions of WORK(6) and WORK(7).
  155. *> = 'T': transpose if entropy test indicates possibly faster
  156. *> convergence of Jacobi process if A^* is taken as input. If A is
  157. *> replaced with A^*, then the row pivoting is included automatically.
  158. *> = 'N': do not speculate.
  159. *> This option can be used to compute only the singular values, or the
  160. *> full SVD (U, SIGMA and V). For only one set of singular vectors
  161. *> (U or V), the caller should provide both U and V, as one of the
  162. *> matrices is used as workspace if the matrix A is transposed.
  163. *> The implementer can easily remove this constraint and make the
  164. *> code more complicated. See the descriptions of U and V.
  165. *> \endverbatim
  166. *>
  167. *> \param[in] JOBP
  168. *> \verbatim
  169. *> JOBP is CHARACTER*1
  170. *> Issues the licence to introduce structured perturbations to drown
  171. *> denormalized numbers. This licence should be active if the
  172. *> denormals are poorly implemented, causing slow computation,
  173. *> especially in cases of fast convergence (!). For details see [1,2].
  174. *> For the sake of simplicity, this perturbations are included only
  175. *> when the full SVD or only the singular values are requested. The
  176. *> implementer/user can easily add the perturbation for the cases of
  177. *> computing one set of singular vectors.
  178. *> = 'P': introduce perturbation
  179. *> = 'N': do not perturb
  180. *> \endverbatim
  181. *>
  182. *> \param[in] M
  183. *> \verbatim
  184. *> M is INTEGER
  185. *> The number of rows of the input matrix A. M >= 0.
  186. *> \endverbatim
  187. *>
  188. *> \param[in] N
  189. *> \verbatim
  190. *> N is INTEGER
  191. *> The number of columns of the input matrix A. M >= N >= 0.
  192. *> \endverbatim
  193. *>
  194. *> \param[in,out] A
  195. *> \verbatim
  196. *> A is COMPLEX array, dimension (LDA,N)
  197. *> On entry, the M-by-N matrix A.
  198. *> \endverbatim
  199. *>
  200. *> \param[in] LDA
  201. *> \verbatim
  202. *> LDA is INTEGER
  203. *> The leading dimension of the array A. LDA >= max(1,M).
  204. *> \endverbatim
  205. *>
  206. *> \param[out] SVA
  207. *> \verbatim
  208. *> SVA is REAL array, dimension (N)
  209. *> On exit,
  210. *> - For WORK(1)/WORK(2) = ONE: The singular values of A. During the
  211. *> computation SVA contains Euclidean column norms of the
  212. *> iterated matrices in the array A.
  213. *> - For WORK(1) .NE. WORK(2): The singular values of A are
  214. *> (WORK(1)/WORK(2)) * SVA(1:N). This factored form is used if
  215. *> sigma_max(A) overflows or if small singular values have been
  216. *> saved from underflow by scaling the input matrix A.
  217. *> - If JOBR='R' then some of the singular values may be returned
  218. *> as exact zeros obtained by "set to zero" because they are
  219. *> below the numerical rank threshold or are denormalized numbers.
  220. *> \endverbatim
  221. *>
  222. *> \param[out] U
  223. *> \verbatim
  224. *> U is COMPLEX array, dimension ( LDU, N )
  225. *> If JOBU = 'U', then U contains on exit the M-by-N matrix of
  226. *> the left singular vectors.
  227. *> If JOBU = 'F', then U contains on exit the M-by-M matrix of
  228. *> the left singular vectors, including an ONB
  229. *> of the orthogonal complement of the Range(A).
  230. *> If JOBU = 'W' .AND. (JOBV.EQ.'V' .AND. JOBT.EQ.'T' .AND. M.EQ.N),
  231. *> then U is used as workspace if the procedure
  232. *> replaces A with A^*. In that case, [V] is computed
  233. *> in U as left singular vectors of A^* and then
  234. *> copied back to the V array. This 'W' option is just
  235. *> a reminder to the caller that in this case U is
  236. *> reserved as workspace of length N*N.
  237. *> If JOBU = 'N' U is not referenced.
  238. *> \endverbatim
  239. *>
  240. *> \param[in] LDU
  241. *> \verbatim
  242. *> LDU is INTEGER
  243. *> The leading dimension of the array U, LDU >= 1.
  244. *> IF JOBU = 'U' or 'F' or 'W', then LDU >= M.
  245. *> \endverbatim
  246. *>
  247. *> \param[out] V
  248. *> \verbatim
  249. *> V is COMPLEX array, dimension ( LDV, N )
  250. *> If JOBV = 'V', 'J' then V contains on exit the N-by-N matrix of
  251. *> the right singular vectors;
  252. *> If JOBV = 'W', AND (JOBU.EQ.'U' AND JOBT.EQ.'T' AND M.EQ.N),
  253. *> then V is used as workspace if the pprocedure
  254. *> replaces A with A^*. In that case, [U] is computed
  255. *> in V as right singular vectors of A^* and then
  256. *> copied back to the U array. This 'W' option is just
  257. *> a reminder to the caller that in this case V is
  258. *> reserved as workspace of length N*N.
  259. *> If JOBV = 'N' V is not referenced.
  260. *> \endverbatim
  261. *>
  262. *> \param[in] LDV
  263. *> \verbatim
  264. *> LDV is INTEGER
  265. *> The leading dimension of the array V, LDV >= 1.
  266. *> If JOBV = 'V' or 'J' or 'W', then LDV >= N.
  267. *> \endverbatim
  268. *>
  269. *> \param[out] CWORK
  270. *> \verbatim
  271. *> CWORK is COMPLEX array, dimension at least LWORK.
  272. *> \endverbatim
  273. *>
  274. *> \param[in] LWORK
  275. *> \verbatim
  276. *> LWORK is INTEGER
  277. *> Length of CWORK to confirm proper allocation of workspace.
  278. *> LWORK depends on the job:
  279. *>
  280. *> 1. If only SIGMA is needed ( JOBU.EQ.'N', JOBV.EQ.'N' ) and
  281. *> 1.1 .. no scaled condition estimate required (JOBE.EQ.'N'):
  282. *> LWORK >= 2*N+1. This is the minimal requirement.
  283. *> ->> For optimal performance (blocked code) the optimal value
  284. *> is LWORK >= N + (N+1)*NB. Here NB is the optimal
  285. *> block size for CGEQP3 and CGEQRF.
  286. *> In general, optimal LWORK is computed as
  287. *> LWORK >= max(N+LWORK(CGEQP3),N+LWORK(CGEQRF)).
  288. *> 1.2. .. an estimate of the scaled condition number of A is
  289. *> required (JOBA='E', or 'G'). In this case, LWORK the minimal
  290. *> requirement is LWORK >= N*N + 3*N.
  291. *> ->> For optimal performance (blocked code) the optimal value
  292. *> is LWORK >= max(N+(N+1)*NB, N*N+3*N).
  293. *> In general, the optimal length LWORK is computed as
  294. *> LWORK >= max(N+LWORK(CGEQP3),N+LWORK(CGEQRF),
  295. *> N+N*N+LWORK(CPOCON)).
  296. *>
  297. *> 2. If SIGMA and the right singular vectors are needed (JOBV.EQ.'V'),
  298. *> (JOBU.EQ.'N')
  299. *> -> the minimal requirement is LWORK >= 3*N.
  300. *> -> For optimal performance, LWORK >= max(N+(N+1)*NB, 3*N,2*N+N*NB),
  301. *> where NB is the optimal block size for CGEQP3, CGEQRF, CGELQ,
  302. *> CUNMLQ. In general, the optimal length LWORK is computed as
  303. *> LWORK >= max(N+LWORK(CGEQP3), N+LWORK(CPOCON), N+LWORK(CGESVJ),
  304. *> N+LWORK(CGELQF), 2*N+LWORK(CGEQRF), N+LWORK(CUNMLQ)).
  305. *>
  306. *> 3. If SIGMA and the left singular vectors are needed
  307. *> -> the minimal requirement is LWORK >= 3*N.
  308. *> -> For optimal performance:
  309. *> if JOBU.EQ.'U' :: LWORK >= max(3*N, N+(N+1)*NB, 2*N+N*NB),
  310. *> where NB is the optimal block size for CGEQP3, CGEQRF, CUNMQR.
  311. *> In general, the optimal length LWORK is computed as
  312. *> LWORK >= max(N+LWORK(CGEQP3),N+LWORK(CPOCON),
  313. *> 2*N+LWORK(CGEQRF), N+LWORK(CUNMQR)).
  314. *>
  315. *> 4. If the full SVD is needed: (JOBU.EQ.'U' or JOBU.EQ.'F') and
  316. *> 4.1. if JOBV.EQ.'V'
  317. *> the minimal requirement is LWORK >= 5*N+2*N*N.
  318. *> 4.2. if JOBV.EQ.'J' the minimal requirement is
  319. *> LWORK >= 4*N+N*N.
  320. *> In both cases, the allocated CWORK can accomodate blocked runs
  321. *> of CGEQP3, CGEQRF, CGELQF, SUNMQR, CUNMLQ.
  322. *> \endverbatim
  323. *>
  324. *> \param[out] RWORK
  325. *> \verbatim
  326. *> RWORK is REAL array, dimension at least LRWORK.
  327. *> On exit,
  328. *> RWORK(1) = Determines the scaling factor SCALE = RWORK(2) / RWORK(1)
  329. *> such that SCALE*SVA(1:N) are the computed singular values
  330. *> of A. (See the description of SVA().)
  331. *> RWORK(2) = See the description of RWORK(1).
  332. *> RWORK(3) = SCONDA is an estimate for the condition number of
  333. *> column equilibrated A. (If JOBA .EQ. 'E' or 'G')
  334. *> SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1).
  335. *> It is computed using SPOCON. It holds
  336. *> N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA
  337. *> where R is the triangular factor from the QRF of A.
  338. *> However, if R is truncated and the numerical rank is
  339. *> determined to be strictly smaller than N, SCONDA is
  340. *> returned as -1, thus indicating that the smallest
  341. *> singular values might be lost.
  342. *>
  343. *> If full SVD is needed, the following two condition numbers are
  344. *> useful for the analysis of the algorithm. They are provied for
  345. *> a developer/implementer who is familiar with the details of
  346. *> the method.
  347. *>
  348. *> RWORK(4) = an estimate of the scaled condition number of the
  349. *> triangular factor in the first QR factorization.
  350. *> RWORK(5) = an estimate of the scaled condition number of the
  351. *> triangular factor in the second QR factorization.
  352. *> The following two parameters are computed if JOBT .EQ. 'T'.
  353. *> They are provided for a developer/implementer who is familiar
  354. *> with the details of the method.
  355. *> RWORK(6) = the entropy of A^* * A :: this is the Shannon entropy
  356. *> of diag(A^* * A) / Trace(A^* * A) taken as point in the
  357. *> probability simplex.
  358. *> RWORK(7) = the entropy of A * A^*. (See the description of RWORK(6).)
  359. *> \endverbatim
  360. *>
  361. *> \param[in] LRWORK
  362. *> \verbatim
  363. *> LRWORK is INTEGER
  364. *> Length of RWORK to confirm proper allocation of workspace.
  365. *> LRWORK depends on the job:
  366. *>
  367. *> 1. If only singular values are requested i.e. if
  368. *> LSAME(JOBU,'N') .AND. LSAME(JOBV,'N')
  369. *> then:
  370. *> 1.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'),
  371. *> then LRWORK = max( 7, N + 2 * M ).
  372. *> 1.2. Otherwise, LRWORK = max( 7, 2 * N ).
  373. *> 2. If singular values with the right singular vectors are requested
  374. *> i.e. if
  375. *> (LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) .AND.
  376. *> .NOT.(LSAME(JOBU,'U').OR.LSAME(JOBU,'F'))
  377. *> then:
  378. *> 2.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'),
  379. *> then LRWORK = max( 7, N + 2 * M ).
  380. *> 2.2. Otherwise, LRWORK = max( 7, 2 * N ).
  381. *> 3. If singular values with the left singular vectors are requested, i.e. if
  382. *> (LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) .AND.
  383. *> .NOT.(LSAME(JOBV,'V').OR.LSAME(JOBV,'J'))
  384. *> then:
  385. *> 3.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'),
  386. *> then LRWORK = max( 7, N + 2 * M ).
  387. *> 3.2. Otherwise, LRWORK = max( 7, 2 * N ).
  388. *> 4. If singular values with both the left and the right singular vectors
  389. *> are requested, i.e. if
  390. *> (LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) .AND.
  391. *> (LSAME(JOBV,'V').OR.LSAME(JOBV,'J'))
  392. *> then:
  393. *> 4.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'),
  394. *> then LRWORK = max( 7, N + 2 * M ).
  395. *> 4.2. Otherwise, LRWORK = max( 7, 2 * N ).
  396. *> \endverbatim
  397. *>
  398. *> \param[out] IWORK
  399. *> \verbatim
  400. *> IWORK is INTEGER array, of dimension:
  401. *> If LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), then
  402. *> the dimension of IWORK is max( 3, 2 * N + M ).
  403. *> Otherwise, the dimension of IWORK is
  404. *> -> max( 3, 2*N ) for full SVD
  405. *> -> max( 3, N ) for singular values only or singular
  406. *> values with one set of singular vectors (left or right)
  407. *> On exit,
  408. *> IWORK(1) = the numerical rank determined after the initial
  409. *> QR factorization with pivoting. See the descriptions
  410. *> of JOBA and JOBR.
  411. *> IWORK(2) = the number of the computed nonzero singular values
  412. *> IWORK(3) = if nonzero, a warning message:
  413. *> If IWORK(3).EQ.1 then some of the column norms of A
  414. *> were denormalized floats. The requested high accuracy
  415. *> is not warranted by the data.
  416. *> \endverbatim
  417. *>
  418. *> \param[out] INFO
  419. *> \verbatim
  420. *> INFO is INTEGER
  421. *> < 0 : if INFO = -i, then the i-th argument had an illegal value.
  422. *> = 0 : successfull exit;
  423. *> > 0 : CGEJSV did not converge in the maximal allowed number
  424. *> of sweeps. The computed values may be inaccurate.
  425. *> \endverbatim
  426. *
  427. * Authors:
  428. * ========
  429. *
  430. *> \author Univ. of Tennessee
  431. *> \author Univ. of California Berkeley
  432. *> \author Univ. of Colorado Denver
  433. *> \author NAG Ltd.
  434. *
  435. *> \date November 2015
  436. *
  437. *> \ingroup complexGEsing
  438. *
  439. *> \par Further Details:
  440. * =====================
  441. *>
  442. *> \verbatim
  443. *>
  444. *> CGEJSV implements a preconditioned Jacobi SVD algorithm. It uses CGEQP3,
  445. *> CGEQRF, and CGELQF as preprocessors and preconditioners. Optionally, an
  446. *> additional row pivoting can be used as a preprocessor, which in some
  447. *> cases results in much higher accuracy. An example is matrix A with the
  448. *> structure A = D1 * C * D2, where D1, D2 are arbitrarily ill-conditioned
  449. *> diagonal matrices and C is well-conditioned matrix. In that case, complete
  450. *> pivoting in the first QR factorizations provides accuracy dependent on the
  451. *> condition number of C, and independent of D1, D2. Such higher accuracy is
  452. *> not completely understood theoretically, but it works well in practice.
  453. *> Further, if A can be written as A = B*D, with well-conditioned B and some
  454. *> diagonal D, then the high accuracy is guaranteed, both theoretically and
  455. *> in software, independent of D. For more details see [1], [2].
  456. *> The computational range for the singular values can be the full range
  457. *> ( UNDERFLOW,OVERFLOW ), provided that the machine arithmetic and the BLAS
  458. *> & LAPACK routines called by CGEJSV are implemented to work in that range.
  459. *> If that is not the case, then the restriction for safe computation with
  460. *> the singular values in the range of normalized IEEE numbers is that the
  461. *> spectral condition number kappa(A)=sigma_max(A)/sigma_min(A) does not
  462. *> overflow. This code (CGEJSV) is best used in this restricted range,
  463. *> meaning that singular values of magnitude below ||A||_2 / SLAMCH('O') are
  464. *> returned as zeros. See JOBR for details on this.
  465. *> Further, this implementation is somewhat slower than the one described
  466. *> in [1,2] due to replacement of some non-LAPACK components, and because
  467. *> the choice of some tuning parameters in the iterative part (CGESVJ) is
  468. *> left to the implementer on a particular machine.
  469. *> The rank revealing QR factorization (in this code: CGEQP3) should be
  470. *> implemented as in [3]. We have a new version of CGEQP3 under development
  471. *> that is more robust than the current one in LAPACK, with a cleaner cut in
  472. *> rank defficient cases. It will be available in the SIGMA library [4].
  473. *> If M is much larger than N, it is obvious that the inital QRF with
  474. *> column pivoting can be preprocessed by the QRF without pivoting. That
  475. *> well known trick is not used in CGEJSV because in some cases heavy row
  476. *> weighting can be treated with complete pivoting. The overhead in cases
  477. *> M much larger than N is then only due to pivoting, but the benefits in
  478. *> terms of accuracy have prevailed. The implementer/user can incorporate
  479. *> this extra QRF step easily. The implementer can also improve data movement
  480. *> (matrix transpose, matrix copy, matrix transposed copy) - this
  481. *> implementation of CGEJSV uses only the simplest, naive data movement.
  482. *
  483. *> \par Contributors:
  484. * ==================
  485. *>
  486. *> Zlatko Drmac (Zagreb, Croatia) and Kresimir Veselic (Hagen, Germany)
  487. *
  488. *> \par References:
  489. * ================
  490. *>
  491. *> \verbatim
  492. *>
  493. *> [1] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I.
  494. *> SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342.
  495. *> LAPACK Working note 169.
  496. *> [2] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II.
  497. *> SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362.
  498. *> LAPACK Working note 170.
  499. *> [3] Z. Drmac and Z. Bujanovic: On the failure of rank-revealing QR
  500. *> factorization software - a case study.
  501. *> ACM Trans. math. Softw. Vol. 35, No 2 (2008), pp. 1-28.
  502. *> LAPACK Working note 176.
  503. *> [4] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV,
  504. *> QSVD, (H,K)-SVD computations.
  505. *> Department of Mathematics, University of Zagreb, 2008.
  506. *> \endverbatim
  507. *
  508. *> \par Bugs, examples and comments:
  509. * =================================
  510. *>
  511. *> Please report all bugs and send interesting examples and/or comments to
  512. *> drmac@math.hr. Thank you.
  513. *>
  514. * =====================================================================
  515. SUBROUTINE CGEJSV( JOBA, JOBU, JOBV, JOBR, JOBT, JOBP,
  516. $ M, N, A, LDA, SVA, U, LDU, V, LDV,
  517. $ CWORK, LWORK, RWORK, LRWORK, IWORK, INFO )
  518. *
  519. * -- LAPACK computational routine (version 3.6.0) --
  520. * -- LAPACK is a software package provided by Univ. of Tennessee, --
  521. * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  522. * November 2015
  523. *
  524. * .. Scalar Arguments ..
  525. IMPLICIT NONE
  526. INTEGER INFO, LDA, LDU, LDV, LWORK, LRWORK, M, N
  527. * ..
  528. * .. Array Arguments ..
  529. COMPLEX A( LDA, * ), U( LDU, * ), V( LDV, * ), CWORK( LWORK )
  530. REAL SVA( N ), RWORK( * )
  531. INTEGER IWORK( * )
  532. CHARACTER*1 JOBA, JOBP, JOBR, JOBT, JOBU, JOBV
  533. * ..
  534. *
  535. * ===========================================================================
  536. *
  537. * .. Local Parameters ..
  538. REAL ZERO, ONE
  539. PARAMETER ( ZERO = 0.0E0, ONE = 1.0E0 )
  540. COMPLEX CZERO, CONE
  541. PARAMETER ( CZERO = ( 0.0E0, 0.0E0 ), CONE = ( 1.0E0, 0.0E0 ) )
  542. * ..
  543. * .. Local Scalars ..
  544. COMPLEX CTEMP
  545. REAL AAPP, AAQQ, AATMAX, AATMIN, BIG, BIG1, COND_OK,
  546. $ CONDR1, CONDR2, ENTRA, ENTRAT, EPSLN, MAXPRJ, SCALEM,
  547. $ SCONDA, SFMIN, SMALL, TEMP1, USCAL1, USCAL2, XSC
  548. INTEGER IERR, N1, NR, NUMRANK, p, q, WARNING
  549. LOGICAL ALMORT, DEFR, ERREST, GOSCAL, JRACC, KILL, LSVEC,
  550. $ L2ABER, L2KILL, L2PERT, L2RANK, L2TRAN,
  551. $ NOSCAL, ROWPIV, RSVEC, TRANSP
  552. * ..
  553. * .. Intrinsic Functions ..
  554. INTRINSIC ABS, CONJG, ALOG, AMAX1, AMIN1, CMPLX, FLOAT,
  555. $ MAX0, MIN0, NINT, SIGN, SQRT
  556. * ..
  557. * .. External Functions ..
  558. REAL SLAMCH, SCNRM2
  559. INTEGER ISAMAX
  560. LOGICAL LSAME
  561. EXTERNAL ISAMAX, LSAME, SLAMCH, SCNRM2
  562. * ..
  563. * .. External Subroutines ..
  564. EXTERNAL CCOPY, CGELQF, CGEQP3, CGEQRF, CLACPY, CLASCL,
  565. $ CLASET, CLASSQ, CLASWP, CUNGQR, CUNMLQ,
  566. $ CUNMQR, CPOCON, SSCAL, CSSCAL, CSWAP, CTRSM, XERBLA
  567. *
  568. EXTERNAL CGESVJ
  569. * ..
  570. *
  571. * Test the input arguments
  572. *
  573. LSVEC = LSAME( JOBU, 'U' ) .OR. LSAME( JOBU, 'F' )
  574. JRACC = LSAME( JOBV, 'J' )
  575. RSVEC = LSAME( JOBV, 'V' ) .OR. JRACC
  576. ROWPIV = LSAME( JOBA, 'F' ) .OR. LSAME( JOBA, 'G' )
  577. L2RANK = LSAME( JOBA, 'R' )
  578. L2ABER = LSAME( JOBA, 'A' )
  579. ERREST = LSAME( JOBA, 'E' ) .OR. LSAME( JOBA, 'G' )
  580. L2TRAN = LSAME( JOBT, 'T' )
  581. L2KILL = LSAME( JOBR, 'R' )
  582. DEFR = LSAME( JOBR, 'N' )
  583. L2PERT = LSAME( JOBP, 'P' )
  584. *
  585. IF ( .NOT.(ROWPIV .OR. L2RANK .OR. L2ABER .OR.
  586. $ ERREST .OR. LSAME( JOBA, 'C' ) )) THEN
  587. INFO = - 1
  588. ELSE IF ( .NOT.( LSVEC .OR. LSAME( JOBU, 'N' ) .OR.
  589. $ LSAME( JOBU, 'W' )) ) THEN
  590. INFO = - 2
  591. ELSE IF ( .NOT.( RSVEC .OR. LSAME( JOBV, 'N' ) .OR.
  592. $ LSAME( JOBV, 'W' )) .OR. ( JRACC .AND. (.NOT.LSVEC) ) ) THEN
  593. INFO = - 3
  594. ELSE IF ( .NOT. ( L2KILL .OR. DEFR ) ) THEN
  595. INFO = - 4
  596. ELSE IF ( .NOT. ( L2TRAN .OR. LSAME( JOBT, 'N' ) ) ) THEN
  597. INFO = - 5
  598. ELSE IF ( .NOT. ( L2PERT .OR. LSAME( JOBP, 'N' ) ) ) THEN
  599. INFO = - 6
  600. ELSE IF ( M .LT. 0 ) THEN
  601. INFO = - 7
  602. ELSE IF ( ( N .LT. 0 ) .OR. ( N .GT. M ) ) THEN
  603. INFO = - 8
  604. ELSE IF ( LDA .LT. M ) THEN
  605. INFO = - 10
  606. ELSE IF ( LSVEC .AND. ( LDU .LT. M ) ) THEN
  607. INFO = - 13
  608. ELSE IF ( RSVEC .AND. ( LDV .LT. N ) ) THEN
  609. INFO = - 15
  610. ELSE IF ( (.NOT.(LSVEC .OR. RSVEC .OR. ERREST).AND.
  611. $ (LWORK .LT. 2*N+1)) .OR.
  612. $ (.NOT.(LSVEC .OR. RSVEC) .AND. ERREST .AND.
  613. $ (LWORK .LT. N*N+3*N)) .OR.
  614. $ (LSVEC .AND. (.NOT.RSVEC) .AND. (LWORK .LT. 3*N))
  615. $ .OR.
  616. $ (RSVEC .AND. (.NOT.LSVEC) .AND. (LWORK .LT. 3*N))
  617. $ .OR.
  618. $ (LSVEC .AND. RSVEC .AND. (.NOT.JRACC) .AND.
  619. $ (LWORK.LT.5*N+2*N*N))
  620. $ .OR. (LSVEC .AND. RSVEC .AND. JRACC .AND.
  621. $ LWORK.LT.4*N+N*N))
  622. $ THEN
  623. INFO = - 17
  624. ELSE IF ( LRWORK.LT. MAX0(N+2*M,7)) THEN
  625. INFO = -19
  626. ELSE
  627. * #:)
  628. INFO = 0
  629. END IF
  630. *
  631. IF ( INFO .NE. 0 ) THEN
  632. * #:(
  633. CALL XERBLA( 'CGEJSV', - INFO )
  634. RETURN
  635. END IF
  636. *
  637. * Quick return for void matrix (Y3K safe)
  638. * #:)
  639. IF ( ( M .EQ. 0 ) .OR. ( N .EQ. 0 ) ) RETURN
  640. *
  641. * Determine whether the matrix U should be M x N or M x M
  642. *
  643. IF ( LSVEC ) THEN
  644. N1 = N
  645. IF ( LSAME( JOBU, 'F' ) ) N1 = M
  646. END IF
  647. *
  648. * Set numerical parameters
  649. *
  650. *! NOTE: Make sure SLAMCH() does not fail on the target architecture.
  651. *
  652. EPSLN = SLAMCH('Epsilon')
  653. SFMIN = SLAMCH('SafeMinimum')
  654. SMALL = SFMIN / EPSLN
  655. BIG = SLAMCH('O')
  656. * BIG = ONE / SFMIN
  657. *
  658. * Initialize SVA(1:N) = diag( ||A e_i||_2 )_1^N
  659. *
  660. *(!) If necessary, scale SVA() to protect the largest norm from
  661. * overflow. It is possible that this scaling pushes the smallest
  662. * column norm left from the underflow threshold (extreme case).
  663. *
  664. SCALEM = ONE / SQRT(FLOAT(M)*FLOAT(N))
  665. NOSCAL = .TRUE.
  666. GOSCAL = .TRUE.
  667. DO 1874 p = 1, N
  668. AAPP = ZERO
  669. AAQQ = ONE
  670. CALL CLASSQ( M, A(1,p), 1, AAPP, AAQQ )
  671. IF ( AAPP .GT. BIG ) THEN
  672. INFO = - 9
  673. CALL XERBLA( 'CGEJSV', -INFO )
  674. RETURN
  675. END IF
  676. AAQQ = SQRT(AAQQ)
  677. IF ( ( AAPP .LT. (BIG / AAQQ) ) .AND. NOSCAL ) THEN
  678. SVA(p) = AAPP * AAQQ
  679. ELSE
  680. NOSCAL = .FALSE.
  681. SVA(p) = AAPP * ( AAQQ * SCALEM )
  682. IF ( GOSCAL ) THEN
  683. GOSCAL = .FALSE.
  684. CALL SSCAL( p-1, SCALEM, SVA, 1 )
  685. END IF
  686. END IF
  687. 1874 CONTINUE
  688. *
  689. IF ( NOSCAL ) SCALEM = ONE
  690. *
  691. AAPP = ZERO
  692. AAQQ = BIG
  693. DO 4781 p = 1, N
  694. AAPP = AMAX1( AAPP, SVA(p) )
  695. IF ( SVA(p) .NE. ZERO ) AAQQ = AMIN1( AAQQ, SVA(p) )
  696. 4781 CONTINUE
  697. *
  698. * Quick return for zero M x N matrix
  699. * #:)
  700. IF ( AAPP .EQ. ZERO ) THEN
  701. IF ( LSVEC ) CALL CLASET( 'G', M, N1, CZERO, CONE, U, LDU )
  702. IF ( RSVEC ) CALL CLASET( 'G', N, N, CZERO, CONE, V, LDV )
  703. RWORK(1) = ONE
  704. RWORK(2) = ONE
  705. IF ( ERREST ) RWORK(3) = ONE
  706. IF ( LSVEC .AND. RSVEC ) THEN
  707. RWORK(4) = ONE
  708. RWORK(5) = ONE
  709. END IF
  710. IF ( L2TRAN ) THEN
  711. RWORK(6) = ZERO
  712. RWORK(7) = ZERO
  713. END IF
  714. IWORK(1) = 0
  715. IWORK(2) = 0
  716. IWORK(3) = 0
  717. RETURN
  718. END IF
  719. *
  720. * Issue warning if denormalized column norms detected. Override the
  721. * high relative accuracy request. Issue licence to kill columns
  722. * (set them to zero) whose norm is less than sigma_max / BIG (roughly).
  723. * #:(
  724. WARNING = 0
  725. IF ( AAQQ .LE. SFMIN ) THEN
  726. L2RANK = .TRUE.
  727. L2KILL = .TRUE.
  728. WARNING = 1
  729. END IF
  730. *
  731. * Quick return for one-column matrix
  732. * #:)
  733. IF ( N .EQ. 1 ) THEN
  734. *
  735. IF ( LSVEC ) THEN
  736. CALL CLASCL( 'G',0,0,SVA(1),SCALEM, M,1,A(1,1),LDA,IERR )
  737. CALL CLACPY( 'A', M, 1, A, LDA, U, LDU )
  738. * computing all M left singular vectors of the M x 1 matrix
  739. IF ( N1 .NE. N ) THEN
  740. CALL CGEQRF( M, N, U,LDU, CWORK, CWORK(N+1),LWORK-N,IERR )
  741. CALL CUNGQR( M,N1,1, U,LDU,CWORK,CWORK(N+1),LWORK-N,IERR )
  742. CALL CCOPY( M, A(1,1), 1, U(1,1), 1 )
  743. END IF
  744. END IF
  745. IF ( RSVEC ) THEN
  746. V(1,1) = CONE
  747. END IF
  748. IF ( SVA(1) .LT. (BIG*SCALEM) ) THEN
  749. SVA(1) = SVA(1) / SCALEM
  750. SCALEM = ONE
  751. END IF
  752. RWORK(1) = ONE / SCALEM
  753. RWORK(2) = ONE
  754. IF ( SVA(1) .NE. ZERO ) THEN
  755. IWORK(1) = 1
  756. IF ( ( SVA(1) / SCALEM) .GE. SFMIN ) THEN
  757. IWORK(2) = 1
  758. ELSE
  759. IWORK(2) = 0
  760. END IF
  761. ELSE
  762. IWORK(1) = 0
  763. IWORK(2) = 0
  764. END IF
  765. IWORK(3) = 0
  766. IF ( ERREST ) RWORK(3) = ONE
  767. IF ( LSVEC .AND. RSVEC ) THEN
  768. RWORK(4) = ONE
  769. RWORK(5) = ONE
  770. END IF
  771. IF ( L2TRAN ) THEN
  772. RWORK(6) = ZERO
  773. RWORK(7) = ZERO
  774. END IF
  775. RETURN
  776. *
  777. END IF
  778. *
  779. TRANSP = .FALSE.
  780. L2TRAN = L2TRAN .AND. ( M .EQ. N )
  781. *
  782. AATMAX = -ONE
  783. AATMIN = BIG
  784. IF ( ROWPIV .OR. L2TRAN ) THEN
  785. *
  786. * Compute the row norms, needed to determine row pivoting sequence
  787. * (in the case of heavily row weighted A, row pivoting is strongly
  788. * advised) and to collect information needed to compare the
  789. * structures of A * A^* and A^* * A (in the case L2TRAN.EQ..TRUE.).
  790. *
  791. IF ( L2TRAN ) THEN
  792. DO 1950 p = 1, M
  793. XSC = ZERO
  794. TEMP1 = ONE
  795. CALL CLASSQ( N, A(p,1), LDA, XSC, TEMP1 )
  796. * CLASSQ gets both the ell_2 and the ell_infinity norm
  797. * in one pass through the vector
  798. RWORK(M+N+p) = XSC * SCALEM
  799. RWORK(N+p) = XSC * (SCALEM*SQRT(TEMP1))
  800. AATMAX = AMAX1( AATMAX, RWORK(N+p) )
  801. IF (RWORK(N+p) .NE. ZERO)
  802. $ AATMIN = AMIN1(AATMIN,RWORK(N+p))
  803. 1950 CONTINUE
  804. ELSE
  805. DO 1904 p = 1, M
  806. RWORK(M+N+p) = SCALEM*ABS( A(p,ISAMAX(N,A(p,1),LDA)) )
  807. AATMAX = AMAX1( AATMAX, RWORK(M+N+p) )
  808. AATMIN = AMIN1( AATMIN, RWORK(M+N+p) )
  809. 1904 CONTINUE
  810. END IF
  811. *
  812. END IF
  813. *
  814. * For square matrix A try to determine whether A^* would be better
  815. * input for the preconditioned Jacobi SVD, with faster convergence.
  816. * The decision is based on an O(N) function of the vector of column
  817. * and row norms of A, based on the Shannon entropy. This should give
  818. * the right choice in most cases when the difference actually matters.
  819. * It may fail and pick the slower converging side.
  820. *
  821. ENTRA = ZERO
  822. ENTRAT = ZERO
  823. IF ( L2TRAN ) THEN
  824. *
  825. XSC = ZERO
  826. TEMP1 = ONE
  827. CALL CLASSQ( N, SVA, 1, XSC, TEMP1 )
  828. TEMP1 = ONE / TEMP1
  829. *
  830. ENTRA = ZERO
  831. DO 1113 p = 1, N
  832. BIG1 = ( ( SVA(p) / XSC )**2 ) * TEMP1
  833. IF ( BIG1 .NE. ZERO ) ENTRA = ENTRA + BIG1 * ALOG(BIG1)
  834. 1113 CONTINUE
  835. ENTRA = - ENTRA / ALOG(FLOAT(N))
  836. *
  837. * Now, SVA().^2/Trace(A^* * A) is a point in the probability simplex.
  838. * It is derived from the diagonal of A^* * A. Do the same with the
  839. * diagonal of A * A^*, compute the entropy of the corresponding
  840. * probability distribution. Note that A * A^* and A^* * A have the
  841. * same trace.
  842. *
  843. ENTRAT = ZERO
  844. DO 1114 p = N+1, N+M
  845. BIG1 = ( ( RWORK(p) / XSC )**2 ) * TEMP1
  846. IF ( BIG1 .NE. ZERO ) ENTRAT = ENTRAT + BIG1 * ALOG(BIG1)
  847. 1114 CONTINUE
  848. ENTRAT = - ENTRAT / ALOG(FLOAT(M))
  849. *
  850. * Analyze the entropies and decide A or A^*. Smaller entropy
  851. * usually means better input for the algorithm.
  852. *
  853. TRANSP = ( ENTRAT .LT. ENTRA )
  854. TRANSP = .TRUE.
  855. *
  856. * If A^* is better than A, take the adjoint of A.
  857. *
  858. IF ( TRANSP ) THEN
  859. * In an optimal implementation, this trivial transpose
  860. * should be replaced with faster transpose.
  861. DO 1115 p = 1, N - 1
  862. A(p,p) = CONJG(A(p,p))
  863. DO 1116 q = p + 1, N
  864. CTEMP = CONJG(A(q,p))
  865. A(q,p) = CONJG(A(p,q))
  866. A(p,q) = CTEMP
  867. 1116 CONTINUE
  868. 1115 CONTINUE
  869. A(N,N) = CONJG(A(N,N))
  870. DO 1117 p = 1, N
  871. RWORK(M+N+p) = SVA(p)
  872. SVA(p) = RWORK(N+p)
  873. * previously computed row 2-norms are now column 2-norms
  874. * of the transposed matrix
  875. 1117 CONTINUE
  876. TEMP1 = AAPP
  877. AAPP = AATMAX
  878. AATMAX = TEMP1
  879. TEMP1 = AAQQ
  880. AAQQ = AATMIN
  881. AATMIN = TEMP1
  882. KILL = LSVEC
  883. LSVEC = RSVEC
  884. RSVEC = KILL
  885. IF ( LSVEC ) N1 = N
  886. *
  887. ROWPIV = .TRUE.
  888. END IF
  889. *
  890. END IF
  891. * END IF L2TRAN
  892. *
  893. * Scale the matrix so that its maximal singular value remains less
  894. * than SQRT(BIG) -- the matrix is scaled so that its maximal column
  895. * has Euclidean norm equal to SQRT(BIG/N). The only reason to keep
  896. * SQRT(BIG) instead of BIG is the fact that CGEJSV uses LAPACK and
  897. * BLAS routines that, in some implementations, are not capable of
  898. * working in the full interval [SFMIN,BIG] and that they may provoke
  899. * overflows in the intermediate results. If the singular values spread
  900. * from SFMIN to BIG, then CGESVJ will compute them. So, in that case,
  901. * one should use CGESVJ instead of CGEJSV.
  902. *
  903. BIG1 = SQRT( BIG )
  904. TEMP1 = SQRT( BIG / FLOAT(N) )
  905. *
  906. CALL CLASCL( 'G', 0, 0, AAPP, TEMP1, N, 1, SVA, N, IERR )
  907. IF ( AAQQ .GT. (AAPP * SFMIN) ) THEN
  908. AAQQ = ( AAQQ / AAPP ) * TEMP1
  909. ELSE
  910. AAQQ = ( AAQQ * TEMP1 ) / AAPP
  911. END IF
  912. TEMP1 = TEMP1 * SCALEM
  913. CALL CLASCL( 'G', 0, 0, AAPP, TEMP1, M, N, A, LDA, IERR )
  914. *
  915. * To undo scaling at the end of this procedure, multiply the
  916. * computed singular values with USCAL2 / USCAL1.
  917. *
  918. USCAL1 = TEMP1
  919. USCAL2 = AAPP
  920. *
  921. IF ( L2KILL ) THEN
  922. * L2KILL enforces computation of nonzero singular values in
  923. * the restricted range of condition number of the initial A,
  924. * sigma_max(A) / sigma_min(A) approx. SQRT(BIG)/SQRT(SFMIN).
  925. XSC = SQRT( SFMIN )
  926. ELSE
  927. XSC = SMALL
  928. *
  929. * Now, if the condition number of A is too big,
  930. * sigma_max(A) / sigma_min(A) .GT. SQRT(BIG/N) * EPSLN / SFMIN,
  931. * as a precaution measure, the full SVD is computed using CGESVJ
  932. * with accumulated Jacobi rotations. This provides numerically
  933. * more robust computation, at the cost of slightly increased run
  934. * time. Depending on the concrete implementation of BLAS and LAPACK
  935. * (i.e. how they behave in presence of extreme ill-conditioning) the
  936. * implementor may decide to remove this switch.
  937. IF ( ( AAQQ.LT.SQRT(SFMIN) ) .AND. LSVEC .AND. RSVEC ) THEN
  938. JRACC = .TRUE.
  939. END IF
  940. *
  941. END IF
  942. IF ( AAQQ .LT. XSC ) THEN
  943. DO 700 p = 1, N
  944. IF ( SVA(p) .LT. XSC ) THEN
  945. CALL CLASET( 'A', M, 1, CZERO, CZERO, A(1,p), LDA )
  946. SVA(p) = ZERO
  947. END IF
  948. 700 CONTINUE
  949. END IF
  950. *
  951. * Preconditioning using QR factorization with pivoting
  952. *
  953. IF ( ROWPIV ) THEN
  954. * Optional row permutation (Bjoerck row pivoting):
  955. * A result by Cox and Higham shows that the Bjoerck's
  956. * row pivoting combined with standard column pivoting
  957. * has similar effect as Powell-Reid complete pivoting.
  958. * The ell-infinity norms of A are made nonincreasing.
  959. DO 1952 p = 1, M - 1
  960. q = ISAMAX( M-p+1, RWORK(M+N+p), 1 ) + p - 1
  961. IWORK(2*N+p) = q
  962. IF ( p .NE. q ) THEN
  963. TEMP1 = RWORK(M+N+p)
  964. RWORK(M+N+p) = RWORK(M+N+q)
  965. RWORK(M+N+q) = TEMP1
  966. END IF
  967. 1952 CONTINUE
  968. CALL CLASWP( N, A, LDA, 1, M-1, IWORK(2*N+1), 1 )
  969. END IF
  970. *
  971. * End of the preparation phase (scaling, optional sorting and
  972. * transposing, optional flushing of small columns).
  973. *
  974. * Preconditioning
  975. *
  976. * If the full SVD is needed, the right singular vectors are computed
  977. * from a matrix equation, and for that we need theoretical analysis
  978. * of the Businger-Golub pivoting. So we use CGEQP3 as the first RR QRF.
  979. * In all other cases the first RR QRF can be chosen by other criteria
  980. * (eg speed by replacing global with restricted window pivoting, such
  981. * as in xGEQPX from TOMS # 782). Good results will be obtained using
  982. * xGEQPX with properly (!) chosen numerical parameters.
  983. * Any improvement of CGEQP3 improves overal performance of CGEJSV.
  984. *
  985. * A * P1 = Q1 * [ R1^* 0]^*:
  986. DO 1963 p = 1, N
  987. * .. all columns are free columns
  988. IWORK(p) = 0
  989. 1963 CONTINUE
  990. CALL CGEQP3( M, N, A, LDA, IWORK, CWORK, CWORK(N+1), LWORK-N,
  991. $ RWORK, IERR )
  992. *
  993. * The upper triangular matrix R1 from the first QRF is inspected for
  994. * rank deficiency and possibilities for deflation, or possible
  995. * ill-conditioning. Depending on the user specified flag L2RANK,
  996. * the procedure explores possibilities to reduce the numerical
  997. * rank by inspecting the computed upper triangular factor. If
  998. * L2RANK or L2ABER are up, then CGEJSV will compute the SVD of
  999. * A + dA, where ||dA|| <= f(M,N)*EPSLN.
  1000. *
  1001. NR = 1
  1002. IF ( L2ABER ) THEN
  1003. * Standard absolute error bound suffices. All sigma_i with
  1004. * sigma_i < N*EPSLN*||A|| are flushed to zero. This is an
  1005. * agressive enforcement of lower numerical rank by introducing a
  1006. * backward error of the order of N*EPSLN*||A||.
  1007. TEMP1 = SQRT(FLOAT(N))*EPSLN
  1008. DO 3001 p = 2, N
  1009. IF ( ABS(A(p,p)) .GE. (TEMP1*ABS(A(1,1))) ) THEN
  1010. NR = NR + 1
  1011. ELSE
  1012. GO TO 3002
  1013. END IF
  1014. 3001 CONTINUE
  1015. 3002 CONTINUE
  1016. ELSE IF ( L2RANK ) THEN
  1017. * .. similarly as above, only slightly more gentle (less agressive).
  1018. * Sudden drop on the diagonal of R1 is used as the criterion for
  1019. * close-to-rank-defficient.
  1020. TEMP1 = SQRT(SFMIN)
  1021. DO 3401 p = 2, N
  1022. IF ( ( ABS(A(p,p)) .LT. (EPSLN*ABS(A(p-1,p-1))) ) .OR.
  1023. $ ( ABS(A(p,p)) .LT. SMALL ) .OR.
  1024. $ ( L2KILL .AND. (ABS(A(p,p)) .LT. TEMP1) ) ) GO TO 3402
  1025. NR = NR + 1
  1026. 3401 CONTINUE
  1027. 3402 CONTINUE
  1028. *
  1029. ELSE
  1030. * The goal is high relative accuracy. However, if the matrix
  1031. * has high scaled condition number the relative accuracy is in
  1032. * general not feasible. Later on, a condition number estimator
  1033. * will be deployed to estimate the scaled condition number.
  1034. * Here we just remove the underflowed part of the triangular
  1035. * factor. This prevents the situation in which the code is
  1036. * working hard to get the accuracy not warranted by the data.
  1037. TEMP1 = SQRT(SFMIN)
  1038. DO 3301 p = 2, N
  1039. IF ( ( ABS(A(p,p)) .LT. SMALL ) .OR.
  1040. $ ( L2KILL .AND. (ABS(A(p,p)) .LT. TEMP1) ) ) GO TO 3302
  1041. NR = NR + 1
  1042. 3301 CONTINUE
  1043. 3302 CONTINUE
  1044. *
  1045. END IF
  1046. *
  1047. ALMORT = .FALSE.
  1048. IF ( NR .EQ. N ) THEN
  1049. MAXPRJ = ONE
  1050. DO 3051 p = 2, N
  1051. TEMP1 = ABS(A(p,p)) / SVA(IWORK(p))
  1052. MAXPRJ = AMIN1( MAXPRJ, TEMP1 )
  1053. 3051 CONTINUE
  1054. IF ( MAXPRJ**2 .GE. ONE - FLOAT(N)*EPSLN ) ALMORT = .TRUE.
  1055. END IF
  1056. *
  1057. *
  1058. SCONDA = - ONE
  1059. CONDR1 = - ONE
  1060. CONDR2 = - ONE
  1061. *
  1062. IF ( ERREST ) THEN
  1063. IF ( N .EQ. NR ) THEN
  1064. IF ( RSVEC ) THEN
  1065. * .. V is available as workspace
  1066. CALL CLACPY( 'U', N, N, A, LDA, V, LDV )
  1067. DO 3053 p = 1, N
  1068. TEMP1 = SVA(IWORK(p))
  1069. CALL CSSCAL( p, ONE/TEMP1, V(1,p), 1 )
  1070. 3053 CONTINUE
  1071. CALL CPOCON( 'U', N, V, LDV, ONE, TEMP1,
  1072. $ CWORK(N+1), RWORK, IERR )
  1073. *
  1074. ELSE IF ( LSVEC ) THEN
  1075. * .. U is available as workspace
  1076. CALL CLACPY( 'U', N, N, A, LDA, U, LDU )
  1077. DO 3054 p = 1, N
  1078. TEMP1 = SVA(IWORK(p))
  1079. CALL CSSCAL( p, ONE/TEMP1, U(1,p), 1 )
  1080. 3054 CONTINUE
  1081. CALL CPOCON( 'U', N, U, LDU, ONE, TEMP1,
  1082. $ CWORK(N+1), RWORK, IERR )
  1083. ELSE
  1084. CALL CLACPY( 'U', N, N, A, LDA, CWORK(N+1), N )
  1085. DO 3052 p = 1, N
  1086. TEMP1 = SVA(IWORK(p))
  1087. CALL CSSCAL( p, ONE/TEMP1, CWORK(N+(p-1)*N+1), 1 )
  1088. 3052 CONTINUE
  1089. * .. the columns of R are scaled to have unit Euclidean lengths.
  1090. CALL CPOCON( 'U', N, CWORK(N+1), N, ONE, TEMP1,
  1091. $ CWORK(N+N*N+1), RWORK, IERR )
  1092. *
  1093. END IF
  1094. SCONDA = ONE / SQRT(TEMP1)
  1095. * SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1).
  1096. * N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA
  1097. ELSE
  1098. SCONDA = - ONE
  1099. END IF
  1100. END IF
  1101. *
  1102. L2PERT = L2PERT .AND. ( ABS( A(1,1)/A(NR,NR) ) .GT. SQRT(BIG1) )
  1103. * If there is no violent scaling, artificial perturbation is not needed.
  1104. *
  1105. * Phase 3:
  1106. *
  1107. IF ( .NOT. ( RSVEC .OR. LSVEC ) ) THEN
  1108. *
  1109. * Singular Values only
  1110. *
  1111. * .. transpose A(1:NR,1:N)
  1112. DO 1946 p = 1, MIN0( N-1, NR )
  1113. CALL CCOPY( N-p, A(p,p+1), LDA, A(p+1,p), 1 )
  1114. CALL CLACGV( N-p+1, A(p,p), 1 )
  1115. 1946 CONTINUE
  1116. IF ( NR .EQ. N ) A(N,N) = CONJG(A(N,N))
  1117. *
  1118. * The following two DO-loops introduce small relative perturbation
  1119. * into the strict upper triangle of the lower triangular matrix.
  1120. * Small entries below the main diagonal are also changed.
  1121. * This modification is useful if the computing environment does not
  1122. * provide/allow FLUSH TO ZERO underflow, for it prevents many
  1123. * annoying denormalized numbers in case of strongly scaled matrices.
  1124. * The perturbation is structured so that it does not introduce any
  1125. * new perturbation of the singular values, and it does not destroy
  1126. * the job done by the preconditioner.
  1127. * The licence for this perturbation is in the variable L2PERT, which
  1128. * should be .FALSE. if FLUSH TO ZERO underflow is active.
  1129. *
  1130. IF ( .NOT. ALMORT ) THEN
  1131. *
  1132. IF ( L2PERT ) THEN
  1133. * XSC = SQRT(SMALL)
  1134. XSC = EPSLN / FLOAT(N)
  1135. DO 4947 q = 1, NR
  1136. CTEMP = CMPLX(XSC*ABS(A(q,q)),ZERO)
  1137. DO 4949 p = 1, N
  1138. IF ( ( (p.GT.q) .AND. (ABS(A(p,q)).LE.TEMP1) )
  1139. $ .OR. ( p .LT. q ) )
  1140. * $ A(p,q) = TEMP1 * ( A(p,q) / ABS(A(p,q)) )
  1141. $ A(p,q) = CTEMP
  1142. 4949 CONTINUE
  1143. 4947 CONTINUE
  1144. ELSE
  1145. CALL CLASET( 'U', NR-1,NR-1, CZERO,CZERO, A(1,2),LDA )
  1146. END IF
  1147. *
  1148. * .. second preconditioning using the QR factorization
  1149. *
  1150. CALL CGEQRF( N,NR, A,LDA, CWORK, CWORK(N+1),LWORK-N, IERR )
  1151. *
  1152. * .. and transpose upper to lower triangular
  1153. DO 1948 p = 1, NR - 1
  1154. CALL CCOPY( NR-p, A(p,p+1), LDA, A(p+1,p), 1 )
  1155. CALL CLACGV( NR-p+1, A(p,p), 1 )
  1156. 1948 CONTINUE
  1157. *
  1158. END IF
  1159. *
  1160. * Row-cyclic Jacobi SVD algorithm with column pivoting
  1161. *
  1162. * .. again some perturbation (a "background noise") is added
  1163. * to drown denormals
  1164. IF ( L2PERT ) THEN
  1165. * XSC = SQRT(SMALL)
  1166. XSC = EPSLN / FLOAT(N)
  1167. DO 1947 q = 1, NR
  1168. CTEMP = CMPLX(XSC*ABS(A(q,q)),ZERO)
  1169. DO 1949 p = 1, NR
  1170. IF ( ( (p.GT.q) .AND. (ABS(A(p,q)).LE.TEMP1) )
  1171. $ .OR. ( p .LT. q ) )
  1172. * $ A(p,q) = TEMP1 * ( A(p,q) / ABS(A(p,q)) )
  1173. $ A(p,q) = CTEMP
  1174. 1949 CONTINUE
  1175. 1947 CONTINUE
  1176. ELSE
  1177. CALL CLASET( 'U', NR-1, NR-1, CZERO, CZERO, A(1,2), LDA )
  1178. END IF
  1179. *
  1180. * .. and one-sided Jacobi rotations are started on a lower
  1181. * triangular matrix (plus perturbation which is ignored in
  1182. * the part which destroys triangular form (confusing?!))
  1183. *
  1184. CALL CGESVJ( 'L', 'NoU', 'NoV', NR, NR, A, LDA, SVA,
  1185. $ N, V, LDV, CWORK, LWORK, RWORK, LRWORK, INFO )
  1186. *
  1187. SCALEM = RWORK(1)
  1188. NUMRANK = NINT(RWORK(2))
  1189. *
  1190. *
  1191. ELSE IF ( RSVEC .AND. ( .NOT. LSVEC ) ) THEN
  1192. *
  1193. * -> Singular Values and Right Singular Vectors <-
  1194. *
  1195. IF ( ALMORT ) THEN
  1196. *
  1197. * .. in this case NR equals N
  1198. DO 1998 p = 1, NR
  1199. CALL CCOPY( N-p+1, A(p,p), LDA, V(p,p), 1 )
  1200. CALL CLACGV( N-p+1, V(p,p), 1 )
  1201. 1998 CONTINUE
  1202. CALL CLASET( 'Upper', NR-1,NR-1, CZERO, CZERO, V(1,2), LDV )
  1203. *
  1204. CALL CGESVJ( 'L','U','N', N, NR, V,LDV, SVA, NR, A,LDA,
  1205. $ CWORK, LWORK, RWORK, LRWORK, INFO )
  1206. SCALEM = RWORK(1)
  1207. NUMRANK = NINT(RWORK(2))
  1208. ELSE
  1209. *
  1210. * .. two more QR factorizations ( one QRF is not enough, two require
  1211. * accumulated product of Jacobi rotations, three are perfect )
  1212. *
  1213. CALL CLASET( 'Lower', NR-1,NR-1, CZERO, CZERO, A(2,1), LDA )
  1214. CALL CGELQF( NR,N, A, LDA, CWORK, CWORK(N+1), LWORK-N, IERR)
  1215. CALL CLACPY( 'Lower', NR, NR, A, LDA, V, LDV )
  1216. CALL CLASET( 'Upper', NR-1,NR-1, CZERO, CZERO, V(1,2), LDV )
  1217. CALL CGEQRF( NR, NR, V, LDV, CWORK(N+1), CWORK(2*N+1),
  1218. $ LWORK-2*N, IERR )
  1219. DO 8998 p = 1, NR
  1220. CALL CCOPY( NR-p+1, V(p,p), LDV, V(p,p), 1 )
  1221. CALL CLACGV( NR-p+1, V(p,p), 1 )
  1222. 8998 CONTINUE
  1223. CALL CLASET( 'Upper', NR-1, NR-1, ZERO, ZERO, V(1,2), LDV )
  1224. *
  1225. CALL CGESVJ( 'Lower', 'U','N', NR, NR, V,LDV, SVA, NR, U,
  1226. $ LDU, CWORK(N+1), LWORK-N, RWORK, LRWORK, INFO )
  1227. SCALEM = RWORK(1)
  1228. NUMRANK = NINT(RWORK(2))
  1229. IF ( NR .LT. N ) THEN
  1230. CALL CLASET( 'A',N-NR, NR, CZERO,CZERO, V(NR+1,1), LDV )
  1231. CALL CLASET( 'A',NR, N-NR, CZERO,CZERO, V(1,NR+1), LDV )
  1232. CALL CLASET( 'A',N-NR,N-NR,CZERO,CONE, V(NR+1,NR+1),LDV )
  1233. END IF
  1234. *
  1235. CALL CUNMLQ( 'Left', 'C', N, N, NR, A, LDA, CWORK,
  1236. $ V, LDV, CWORK(N+1), LWORK-N, IERR )
  1237. *
  1238. END IF
  1239. *
  1240. DO 8991 p = 1, N
  1241. CALL CCOPY( N, V(p,1), LDV, A(IWORK(p),1), LDA )
  1242. 8991 CONTINUE
  1243. CALL CLACPY( 'All', N, N, A, LDA, V, LDV )
  1244. *
  1245. IF ( TRANSP ) THEN
  1246. CALL CLACPY( 'All', N, N, V, LDV, U, LDU )
  1247. END IF
  1248. *
  1249. ELSE IF ( LSVEC .AND. ( .NOT. RSVEC ) ) THEN
  1250. *
  1251. * .. Singular Values and Left Singular Vectors ..
  1252. *
  1253. * .. second preconditioning step to avoid need to accumulate
  1254. * Jacobi rotations in the Jacobi iterations.
  1255. DO 1965 p = 1, NR
  1256. CALL CCOPY( N-p+1, A(p,p), LDA, U(p,p), 1 )
  1257. CALL CLACGV( N-p+1, U(p,p), 1 )
  1258. 1965 CONTINUE
  1259. CALL CLASET( 'Upper', NR-1, NR-1, CZERO, CZERO, U(1,2), LDU )
  1260. *
  1261. CALL CGEQRF( N, NR, U, LDU, CWORK(N+1), CWORK(2*N+1),
  1262. $ LWORK-2*N, IERR )
  1263. *
  1264. DO 1967 p = 1, NR - 1
  1265. CALL CCOPY( NR-p, U(p,p+1), LDU, U(p+1,p), 1 )
  1266. CALL CLACGV( N-p+1, U(p,p), 1 )
  1267. 1967 CONTINUE
  1268. CALL CLASET( 'Upper', NR-1, NR-1, CZERO, CZERO, U(1,2), LDU )
  1269. *
  1270. CALL CGESVJ( 'Lower', 'U', 'N', NR,NR, U, LDU, SVA, NR, A,
  1271. $ LDA, CWORK(N+1), LWORK-N, RWORK, LRWORK, INFO )
  1272. SCALEM = RWORK(1)
  1273. NUMRANK = NINT(RWORK(2))
  1274. *
  1275. IF ( NR .LT. M ) THEN
  1276. CALL CLASET( 'A', M-NR, NR,CZERO, CZERO, U(NR+1,1), LDU )
  1277. IF ( NR .LT. N1 ) THEN
  1278. CALL CLASET( 'A',NR, N1-NR, CZERO, CZERO, U(1,NR+1),LDU )
  1279. CALL CLASET( 'A',M-NR,N1-NR,CZERO,CONE,U(NR+1,NR+1),LDU )
  1280. END IF
  1281. END IF
  1282. *
  1283. CALL CUNMQR( 'Left', 'No Tr', M, N1, N, A, LDA, CWORK, U,
  1284. $ LDU, CWORK(N+1), LWORK-N, IERR )
  1285. *
  1286. IF ( ROWPIV )
  1287. $ CALL CLASWP( N1, U, LDU, 1, M-1, IWORK(2*N+1), -1 )
  1288. *
  1289. DO 1974 p = 1, N1
  1290. XSC = ONE / SCNRM2( M, U(1,p), 1 )
  1291. CALL CSSCAL( M, XSC, U(1,p), 1 )
  1292. 1974 CONTINUE
  1293. *
  1294. IF ( TRANSP ) THEN
  1295. CALL CLACPY( 'All', N, N, U, LDU, V, LDV )
  1296. END IF
  1297. *
  1298. ELSE
  1299. *
  1300. * .. Full SVD ..
  1301. *
  1302. IF ( .NOT. JRACC ) THEN
  1303. *
  1304. IF ( .NOT. ALMORT ) THEN
  1305. *
  1306. * Second Preconditioning Step (QRF [with pivoting])
  1307. * Note that the composition of TRANSPOSE, QRF and TRANSPOSE is
  1308. * equivalent to an LQF CALL. Since in many libraries the QRF
  1309. * seems to be better optimized than the LQF, we do explicit
  1310. * transpose and use the QRF. This is subject to changes in an
  1311. * optimized implementation of CGEJSV.
  1312. *
  1313. DO 1968 p = 1, NR
  1314. CALL CCOPY( N-p+1, A(p,p), LDA, V(p,p), 1 )
  1315. CALL CLACGV( N-p+1, V(p,p), 1 )
  1316. 1968 CONTINUE
  1317. *
  1318. * .. the following two loops perturb small entries to avoid
  1319. * denormals in the second QR factorization, where they are
  1320. * as good as zeros. This is done to avoid painfully slow
  1321. * computation with denormals. The relative size of the perturbation
  1322. * is a parameter that can be changed by the implementer.
  1323. * This perturbation device will be obsolete on machines with
  1324. * properly implemented arithmetic.
  1325. * To switch it off, set L2PERT=.FALSE. To remove it from the
  1326. * code, remove the action under L2PERT=.TRUE., leave the ELSE part.
  1327. * The following two loops should be blocked and fused with the
  1328. * transposed copy above.
  1329. *
  1330. IF ( L2PERT ) THEN
  1331. XSC = SQRT(SMALL)
  1332. DO 2969 q = 1, NR
  1333. CTEMP = CMPLX(XSC*ABS( V(q,q) ),ZERO)
  1334. DO 2968 p = 1, N
  1335. IF ( ( p .GT. q ) .AND. ( ABS(V(p,q)) .LE. TEMP1 )
  1336. $ .OR. ( p .LT. q ) )
  1337. * $ V(p,q) = TEMP1 * ( V(p,q) / ABS(V(p,q)) )
  1338. $ V(p,q) = CTEMP
  1339. IF ( p .LT. q ) V(p,q) = - V(p,q)
  1340. 2968 CONTINUE
  1341. 2969 CONTINUE
  1342. ELSE
  1343. CALL CLASET( 'U', NR-1, NR-1, CZERO, CZERO, V(1,2), LDV )
  1344. END IF
  1345. *
  1346. * Estimate the row scaled condition number of R1
  1347. * (If R1 is rectangular, N > NR, then the condition number
  1348. * of the leading NR x NR submatrix is estimated.)
  1349. *
  1350. CALL CLACPY( 'L', NR, NR, V, LDV, CWORK(2*N+1), NR )
  1351. DO 3950 p = 1, NR
  1352. TEMP1 = SCNRM2(NR-p+1,CWORK(2*N+(p-1)*NR+p),1)
  1353. CALL CSSCAL(NR-p+1,ONE/TEMP1,CWORK(2*N+(p-1)*NR+p),1)
  1354. 3950 CONTINUE
  1355. CALL CPOCON('Lower',NR,CWORK(2*N+1),NR,ONE,TEMP1,
  1356. $ CWORK(2*N+NR*NR+1),RWORK,IERR)
  1357. CONDR1 = ONE / SQRT(TEMP1)
  1358. * .. here need a second oppinion on the condition number
  1359. * .. then assume worst case scenario
  1360. * R1 is OK for inverse <=> CONDR1 .LT. FLOAT(N)
  1361. * more conservative <=> CONDR1 .LT. SQRT(FLOAT(N))
  1362. *
  1363. COND_OK = SQRT(SQRT(FLOAT(NR)))
  1364. *[TP] COND_OK is a tuning parameter.
  1365. *
  1366. IF ( CONDR1 .LT. COND_OK ) THEN
  1367. * .. the second QRF without pivoting. Note: in an optimized
  1368. * implementation, this QRF should be implemented as the QRF
  1369. * of a lower triangular matrix.
  1370. * R1^* = Q2 * R2
  1371. CALL CGEQRF( N, NR, V, LDV, CWORK(N+1), CWORK(2*N+1),
  1372. $ LWORK-2*N, IERR )
  1373. *
  1374. IF ( L2PERT ) THEN
  1375. XSC = SQRT(SMALL)/EPSLN
  1376. DO 3959 p = 2, NR
  1377. DO 3958 q = 1, p - 1
  1378. CTEMP=CMPLX(XSC*AMIN1(ABS(V(p,p)),ABS(V(q,q))),
  1379. $ ZERO)
  1380. IF ( ABS(V(q,p)) .LE. TEMP1 )
  1381. * $ V(q,p) = TEMP1 * ( V(q,p) / ABS(V(q,p)) )
  1382. $ V(q,p) = CTEMP
  1383. 3958 CONTINUE
  1384. 3959 CONTINUE
  1385. END IF
  1386. *
  1387. IF ( NR .NE. N )
  1388. $ CALL CLACPY( 'A', N, NR, V, LDV, CWORK(2*N+1), N )
  1389. * .. save ...
  1390. *
  1391. * .. this transposed copy should be better than naive
  1392. DO 1969 p = 1, NR - 1
  1393. CALL CCOPY( NR-p, V(p,p+1), LDV, V(p+1,p), 1 )
  1394. CALL CLACGV(NR-p+1, V(p,p), 1 )
  1395. 1969 CONTINUE
  1396. V(NR,NR)=CONJG(V(NR,NR))
  1397. *
  1398. CONDR2 = CONDR1
  1399. *
  1400. ELSE
  1401. *
  1402. * .. ill-conditioned case: second QRF with pivoting
  1403. * Note that windowed pivoting would be equaly good
  1404. * numerically, and more run-time efficient. So, in
  1405. * an optimal implementation, the next call to CGEQP3
  1406. * should be replaced with eg. CALL CGEQPX (ACM TOMS #782)
  1407. * with properly (carefully) chosen parameters.
  1408. *
  1409. * R1^* * P2 = Q2 * R2
  1410. DO 3003 p = 1, NR
  1411. IWORK(N+p) = 0
  1412. 3003 CONTINUE
  1413. CALL CGEQP3( N, NR, V, LDV, IWORK(N+1), CWORK(N+1),
  1414. $ CWORK(2*N+1), LWORK-2*N, RWORK, IERR )
  1415. ** CALL CGEQRF( N, NR, V, LDV, CWORK(N+1), CWORK(2*N+1),
  1416. ** $ LWORK-2*N, IERR )
  1417. IF ( L2PERT ) THEN
  1418. XSC = SQRT(SMALL)
  1419. DO 3969 p = 2, NR
  1420. DO 3968 q = 1, p - 1
  1421. CTEMP=CMPLX(XSC*AMIN1(ABS(V(p,p)),ABS(V(q,q))),
  1422. $ ZERO)
  1423. IF ( ABS(V(q,p)) .LE. TEMP1 )
  1424. * $ V(q,p) = TEMP1 * ( V(q,p) / ABS(V(q,p)) )
  1425. $ V(q,p) = CTEMP
  1426. 3968 CONTINUE
  1427. 3969 CONTINUE
  1428. END IF
  1429. *
  1430. CALL CLACPY( 'A', N, NR, V, LDV, CWORK(2*N+1), N )
  1431. *
  1432. IF ( L2PERT ) THEN
  1433. XSC = SQRT(SMALL)
  1434. DO 8970 p = 2, NR
  1435. DO 8971 q = 1, p - 1
  1436. CTEMP=CMPLX(XSC*AMIN1(ABS(V(p,p)),ABS(V(q,q))),
  1437. $ ZERO)
  1438. * V(p,q) = - TEMP1*( V(q,p) / ABS(V(q,p)) )
  1439. V(p,q) = - CTEMP
  1440. 8971 CONTINUE
  1441. 8970 CONTINUE
  1442. ELSE
  1443. CALL CLASET( 'L',NR-1,NR-1,CZERO,CZERO,V(2,1),LDV )
  1444. END IF
  1445. * Now, compute R2 = L3 * Q3, the LQ factorization.
  1446. CALL CGELQF( NR, NR, V, LDV, CWORK(2*N+N*NR+1),
  1447. $ CWORK(2*N+N*NR+NR+1), LWORK-2*N-N*NR-NR, IERR )
  1448. * .. and estimate the condition number
  1449. CALL CLACPY( 'L',NR,NR,V,LDV,CWORK(2*N+N*NR+NR+1),NR )
  1450. DO 4950 p = 1, NR
  1451. TEMP1 = SCNRM2( p, CWORK(2*N+N*NR+NR+p), NR )
  1452. CALL CSSCAL( p, ONE/TEMP1, CWORK(2*N+N*NR+NR+p), NR )
  1453. 4950 CONTINUE
  1454. CALL CPOCON( 'L',NR,CWORK(2*N+N*NR+NR+1),NR,ONE,TEMP1,
  1455. $ CWORK(2*N+N*NR+NR+NR*NR+1),RWORK,IERR )
  1456. CONDR2 = ONE / SQRT(TEMP1)
  1457. *
  1458. *
  1459. IF ( CONDR2 .GE. COND_OK ) THEN
  1460. * .. save the Householder vectors used for Q3
  1461. * (this overwrittes the copy of R2, as it will not be
  1462. * needed in this branch, but it does not overwritte the
  1463. * Huseholder vectors of Q2.).
  1464. CALL CLACPY( 'U', NR, NR, V, LDV, CWORK(2*N+1), N )
  1465. * .. and the rest of the information on Q3 is in
  1466. * WORK(2*N+N*NR+1:2*N+N*NR+N)
  1467. END IF
  1468. *
  1469. END IF
  1470. *
  1471. IF ( L2PERT ) THEN
  1472. XSC = SQRT(SMALL)
  1473. DO 4968 q = 2, NR
  1474. CTEMP = XSC * V(q,q)
  1475. DO 4969 p = 1, q - 1
  1476. * V(p,q) = - SIGN( TEMP1, V(q,p) )
  1477. * V(p,q) = - TEMP1*( V(p,q) / ABS(V(p,q)) )
  1478. V(p,q) = - CTEMP
  1479. 4969 CONTINUE
  1480. 4968 CONTINUE
  1481. ELSE
  1482. CALL CLASET( 'U', NR-1,NR-1, CZERO,CZERO, V(1,2), LDV )
  1483. END IF
  1484. *
  1485. * Second preconditioning finished; continue with Jacobi SVD
  1486. * The input matrix is lower trinagular.
  1487. *
  1488. * Recover the right singular vectors as solution of a well
  1489. * conditioned triangular matrix equation.
  1490. *
  1491. IF ( CONDR1 .LT. COND_OK ) THEN
  1492. *
  1493. CALL CGESVJ( 'L','U','N',NR,NR,V,LDV,SVA,NR,U, LDU,
  1494. $ CWORK(2*N+N*NR+NR+1),LWORK-2*N-N*NR-NR,RWORK,
  1495. $ LRWORK, INFO )
  1496. SCALEM = RWORK(1)
  1497. NUMRANK = NINT(RWORK(2))
  1498. DO 3970 p = 1, NR
  1499. CALL CCOPY( NR, V(1,p), 1, U(1,p), 1 )
  1500. CALL CSSCAL( NR, SVA(p), V(1,p), 1 )
  1501. 3970 CONTINUE
  1502. * .. pick the right matrix equation and solve it
  1503. *
  1504. IF ( NR .EQ. N ) THEN
  1505. * :)) .. best case, R1 is inverted. The solution of this matrix
  1506. * equation is Q2*V2 = the product of the Jacobi rotations
  1507. * used in CGESVJ, premultiplied with the orthogonal matrix
  1508. * from the second QR factorization.
  1509. CALL CTRSM('L','U','N','N', NR,NR,CONE, A,LDA, V,LDV)
  1510. ELSE
  1511. * .. R1 is well conditioned, but non-square. Adjoint of R2
  1512. * is inverted to get the product of the Jacobi rotations
  1513. * used in CGESVJ. The Q-factor from the second QR
  1514. * factorization is then built in explicitly.
  1515. CALL CTRSM('L','U','C','N',NR,NR,CONE,CWORK(2*N+1),
  1516. $ N,V,LDV)
  1517. IF ( NR .LT. N ) THEN
  1518. CALL CLASET('A',N-NR,NR,ZERO,CZERO,V(NR+1,1),LDV)
  1519. CALL CLASET('A',NR,N-NR,ZERO,CZERO,V(1,NR+1),LDV)
  1520. CALL CLASET('A',N-NR,N-NR,ZERO,CONE,V(NR+1,NR+1),LDV)
  1521. END IF
  1522. CALL CUNMQR('L','N',N,N,NR,CWORK(2*N+1),N,CWORK(N+1),
  1523. $ V,LDV,CWORK(2*N+N*NR+NR+1),LWORK-2*N-N*NR-NR,IERR)
  1524. END IF
  1525. *
  1526. ELSE IF ( CONDR2 .LT. COND_OK ) THEN
  1527. *
  1528. * The matrix R2 is inverted. The solution of the matrix equation
  1529. * is Q3^* * V3 = the product of the Jacobi rotations (appplied to
  1530. * the lower triangular L3 from the LQ factorization of
  1531. * R2=L3*Q3), pre-multiplied with the transposed Q3.
  1532. CALL CGESVJ( 'L', 'U', 'N', NR, NR, V, LDV, SVA, NR, U,
  1533. $ LDU, CWORK(2*N+N*NR+NR+1), LWORK-2*N-N*NR-NR,
  1534. $ RWORK, LRWORK, INFO )
  1535. SCALEM = RWORK(1)
  1536. NUMRANK = NINT(RWORK(2))
  1537. DO 3870 p = 1, NR
  1538. CALL CCOPY( NR, V(1,p), 1, U(1,p), 1 )
  1539. CALL CSSCAL( NR, SVA(p), U(1,p), 1 )
  1540. 3870 CONTINUE
  1541. CALL CTRSM('L','U','N','N',NR,NR,CONE,CWORK(2*N+1),N,
  1542. $ U,LDU)
  1543. * .. apply the permutation from the second QR factorization
  1544. DO 873 q = 1, NR
  1545. DO 872 p = 1, NR
  1546. CWORK(2*N+N*NR+NR+IWORK(N+p)) = U(p,q)
  1547. 872 CONTINUE
  1548. DO 874 p = 1, NR
  1549. U(p,q) = CWORK(2*N+N*NR+NR+p)
  1550. 874 CONTINUE
  1551. 873 CONTINUE
  1552. IF ( NR .LT. N ) THEN
  1553. CALL CLASET( 'A',N-NR,NR,CZERO,CZERO,V(NR+1,1),LDV )
  1554. CALL CLASET( 'A',NR,N-NR,CZERO,CZERO,V(1,NR+1),LDV )
  1555. CALL CLASET('A',N-NR,N-NR,CZERO,CONE,V(NR+1,NR+1),LDV)
  1556. END IF
  1557. CALL CUNMQR( 'L','N',N,N,NR,CWORK(2*N+1),N,CWORK(N+1),
  1558. $ V,LDV,CWORK(2*N+N*NR+NR+1),LWORK-2*N-N*NR-NR,IERR )
  1559. ELSE
  1560. * Last line of defense.
  1561. * #:( This is a rather pathological case: no scaled condition
  1562. * improvement after two pivoted QR factorizations. Other
  1563. * possibility is that the rank revealing QR factorization
  1564. * or the condition estimator has failed, or the COND_OK
  1565. * is set very close to ONE (which is unnecessary). Normally,
  1566. * this branch should never be executed, but in rare cases of
  1567. * failure of the RRQR or condition estimator, the last line of
  1568. * defense ensures that CGEJSV completes the task.
  1569. * Compute the full SVD of L3 using CGESVJ with explicit
  1570. * accumulation of Jacobi rotations.
  1571. CALL CGESVJ( 'L', 'U', 'V', NR, NR, V, LDV, SVA, NR, U,
  1572. $ LDU, CWORK(2*N+N*NR+NR+1), LWORK-2*N-N*NR-NR,
  1573. $ RWORK, LRWORK, INFO )
  1574. SCALEM = RWORK(1)
  1575. NUMRANK = NINT(RWORK(2))
  1576. IF ( NR .LT. N ) THEN
  1577. CALL CLASET( 'A',N-NR,NR,CZERO,CZERO,V(NR+1,1),LDV )
  1578. CALL CLASET( 'A',NR,N-NR,CZERO,CZERO,V(1,NR+1),LDV )
  1579. CALL CLASET('A',N-NR,N-NR,CZERO,CONE,V(NR+1,NR+1),LDV)
  1580. END IF
  1581. CALL CUNMQR( 'L','N',N,N,NR,CWORK(2*N+1),N,CWORK(N+1),
  1582. $ V,LDV,CWORK(2*N+N*NR+NR+1),LWORK-2*N-N*NR-NR,IERR )
  1583. *
  1584. CALL CUNMLQ( 'L', 'C', NR, NR, NR, CWORK(2*N+1), N,
  1585. $ CWORK(2*N+N*NR+1), U, LDU, CWORK(2*N+N*NR+NR+1),
  1586. $ LWORK-2*N-N*NR-NR, IERR )
  1587. DO 773 q = 1, NR
  1588. DO 772 p = 1, NR
  1589. CWORK(2*N+N*NR+NR+IWORK(N+p)) = U(p,q)
  1590. 772 CONTINUE
  1591. DO 774 p = 1, NR
  1592. U(p,q) = CWORK(2*N+N*NR+NR+p)
  1593. 774 CONTINUE
  1594. 773 CONTINUE
  1595. *
  1596. END IF
  1597. *
  1598. * Permute the rows of V using the (column) permutation from the
  1599. * first QRF. Also, scale the columns to make them unit in
  1600. * Euclidean norm. This applies to all cases.
  1601. *
  1602. TEMP1 = SQRT(FLOAT(N)) * EPSLN
  1603. DO 1972 q = 1, N
  1604. DO 972 p = 1, N
  1605. CWORK(2*N+N*NR+NR+IWORK(p)) = V(p,q)
  1606. 972 CONTINUE
  1607. DO 973 p = 1, N
  1608. V(p,q) = CWORK(2*N+N*NR+NR+p)
  1609. 973 CONTINUE
  1610. XSC = ONE / SCNRM2( N, V(1,q), 1 )
  1611. IF ( (XSC .LT. (ONE-TEMP1)) .OR. (XSC .GT. (ONE+TEMP1)) )
  1612. $ CALL CSSCAL( N, XSC, V(1,q), 1 )
  1613. 1972 CONTINUE
  1614. * At this moment, V contains the right singular vectors of A.
  1615. * Next, assemble the left singular vector matrix U (M x N).
  1616. IF ( NR .LT. M ) THEN
  1617. CALL CLASET('A', M-NR, NR, CZERO, CZERO, U(NR+1,1), LDU)
  1618. IF ( NR .LT. N1 ) THEN
  1619. CALL CLASET('A',NR,N1-NR,CZERO,CZERO,U(1,NR+1),LDU)
  1620. CALL CLASET('A',M-NR,N1-NR,CZERO,CONE,
  1621. $ U(NR+1,NR+1),LDU)
  1622. END IF
  1623. END IF
  1624. *
  1625. * The Q matrix from the first QRF is built into the left singular
  1626. * matrix U. This applies to all cases.
  1627. *
  1628. CALL CUNMQR( 'Left', 'No_Tr', M, N1, N, A, LDA, CWORK, U,
  1629. $ LDU, CWORK(N+1), LWORK-N, IERR )
  1630. * The columns of U are normalized. The cost is O(M*N) flops.
  1631. TEMP1 = SQRT(FLOAT(M)) * EPSLN
  1632. DO 1973 p = 1, NR
  1633. XSC = ONE / SCNRM2( M, U(1,p), 1 )
  1634. IF ( (XSC .LT. (ONE-TEMP1)) .OR. (XSC .GT. (ONE+TEMP1)) )
  1635. $ CALL CSSCAL( M, XSC, U(1,p), 1 )
  1636. 1973 CONTINUE
  1637. *
  1638. * If the initial QRF is computed with row pivoting, the left
  1639. * singular vectors must be adjusted.
  1640. *
  1641. IF ( ROWPIV )
  1642. $ CALL CLASWP( N1, U, LDU, 1, M-1, IWORK(2*N+1), -1 )
  1643. *
  1644. ELSE
  1645. *
  1646. * .. the initial matrix A has almost orthogonal columns and
  1647. * the second QRF is not needed
  1648. *
  1649. CALL CLACPY( 'Upper', N, N, A, LDA, CWORK(N+1), N )
  1650. IF ( L2PERT ) THEN
  1651. XSC = SQRT(SMALL)
  1652. DO 5970 p = 2, N
  1653. CTEMP = XSC * CWORK( N + (p-1)*N + p )
  1654. DO 5971 q = 1, p - 1
  1655. * CWORK(N+(q-1)*N+p)=-TEMP1 * ( CWORK(N+(p-1)*N+q) /
  1656. * $ ABS(CWORK(N+(p-1)*N+q)) )
  1657. CWORK(N+(q-1)*N+p)=-CTEMP
  1658. 5971 CONTINUE
  1659. 5970 CONTINUE
  1660. ELSE
  1661. CALL CLASET( 'Lower',N-1,N-1,CZERO,CZERO,CWORK(N+2),N )
  1662. END IF
  1663. *
  1664. CALL CGESVJ( 'Upper', 'U', 'N', N, N, CWORK(N+1), N, SVA,
  1665. $ N, U, LDU, CWORK(N+N*N+1), LWORK-N-N*N, RWORK, LRWORK,
  1666. $ INFO )
  1667. *
  1668. SCALEM = RWORK(1)
  1669. NUMRANK = NINT(RWORK(2))
  1670. DO 6970 p = 1, N
  1671. CALL CCOPY( N, CWORK(N+(p-1)*N+1), 1, U(1,p), 1 )
  1672. CALL CSSCAL( N, SVA(p), CWORK(N+(p-1)*N+1), 1 )
  1673. 6970 CONTINUE
  1674. *
  1675. CALL CTRSM( 'Left', 'Upper', 'NoTrans', 'No UD', N, N,
  1676. $ CONE, A, LDA, CWORK(N+1), N )
  1677. DO 6972 p = 1, N
  1678. CALL CCOPY( N, CWORK(N+p), N, V(IWORK(p),1), LDV )
  1679. 6972 CONTINUE
  1680. TEMP1 = SQRT(FLOAT(N))*EPSLN
  1681. DO 6971 p = 1, N
  1682. XSC = ONE / SCNRM2( N, V(1,p), 1 )
  1683. IF ( (XSC .LT. (ONE-TEMP1)) .OR. (XSC .GT. (ONE+TEMP1)) )
  1684. $ CALL CSSCAL( N, XSC, V(1,p), 1 )
  1685. 6971 CONTINUE
  1686. *
  1687. * Assemble the left singular vector matrix U (M x N).
  1688. *
  1689. IF ( N .LT. M ) THEN
  1690. CALL CLASET( 'A', M-N, N, CZERO, CZERO, U(N+1,1), LDU )
  1691. IF ( N .LT. N1 ) THEN
  1692. CALL CLASET('A',N, N1-N, CZERO, CZERO, U(1,N+1),LDU)
  1693. CALL CLASET( 'A',M-N,N1-N, CZERO, CONE,U(N+1,N+1),LDU)
  1694. END IF
  1695. END IF
  1696. CALL CUNMQR( 'Left', 'No Tr', M, N1, N, A, LDA, CWORK, U,
  1697. $ LDU, CWORK(N+1), LWORK-N, IERR )
  1698. TEMP1 = SQRT(FLOAT(M))*EPSLN
  1699. DO 6973 p = 1, N1
  1700. XSC = ONE / SCNRM2( M, U(1,p), 1 )
  1701. IF ( (XSC .LT. (ONE-TEMP1)) .OR. (XSC .GT. (ONE+TEMP1)) )
  1702. $ CALL CSSCAL( M, XSC, U(1,p), 1 )
  1703. 6973 CONTINUE
  1704. *
  1705. IF ( ROWPIV )
  1706. $ CALL CLASWP( N1, U, LDU, 1, M-1, IWORK(2*N+1), -1 )
  1707. *
  1708. END IF
  1709. *
  1710. * end of the >> almost orthogonal case << in the full SVD
  1711. *
  1712. ELSE
  1713. *
  1714. * This branch deploys a preconditioned Jacobi SVD with explicitly
  1715. * accumulated rotations. It is included as optional, mainly for
  1716. * experimental purposes. It does perfom well, and can also be used.
  1717. * In this implementation, this branch will be automatically activated
  1718. * if the condition number sigma_max(A) / sigma_min(A) is predicted
  1719. * to be greater than the overflow threshold. This is because the
  1720. * a posteriori computation of the singular vectors assumes robust
  1721. * implementation of BLAS and some LAPACK procedures, capable of working
  1722. * in presence of extreme values. Since that is not always the case, ...
  1723. *
  1724. DO 7968 p = 1, NR
  1725. CALL CCOPY( N-p+1, A(p,p), LDA, V(p,p), 1 )
  1726. CALL CLACGV( N-p+1, V(p,p), 1 )
  1727. 7968 CONTINUE
  1728. *
  1729. IF ( L2PERT ) THEN
  1730. XSC = SQRT(SMALL/EPSLN)
  1731. DO 5969 q = 1, NR
  1732. CTEMP = CMPLX(XSC*ABS( V(q,q) ),ZERO)
  1733. DO 5968 p = 1, N
  1734. IF ( ( p .GT. q ) .AND. ( ABS(V(p,q)) .LE. TEMP1 )
  1735. $ .OR. ( p .LT. q ) )
  1736. * $ V(p,q) = TEMP1 * ( V(p,q) / ABS(V(p,q)) )
  1737. $ V(p,q) = CTEMP
  1738. IF ( p .LT. q ) V(p,q) = - V(p,q)
  1739. 5968 CONTINUE
  1740. 5969 CONTINUE
  1741. ELSE
  1742. CALL CLASET( 'U', NR-1, NR-1, CZERO, CZERO, V(1,2), LDV )
  1743. END IF
  1744. CALL CGEQRF( N, NR, V, LDV, CWORK(N+1), CWORK(2*N+1),
  1745. $ LWORK-2*N, IERR )
  1746. CALL CLACPY( 'L', N, NR, V, LDV, CWORK(2*N+1), N )
  1747. *
  1748. DO 7969 p = 1, NR
  1749. CALL CCOPY( NR-p+1, V(p,p), LDV, U(p,p), 1 )
  1750. CALL CLACGV( NR-p+1, U(p,p), 1 )
  1751. 7969 CONTINUE
  1752. IF ( L2PERT ) THEN
  1753. XSC = SQRT(SMALL/EPSLN)
  1754. DO 9970 q = 2, NR
  1755. DO 9971 p = 1, q - 1
  1756. CTEMP = CMPLX(XSC * AMIN1(ABS(U(p,p)),ABS(U(q,q))),
  1757. $ ZERO)
  1758. * U(p,q) = - TEMP1 * ( U(q,p) / ABS(U(q,p)) )
  1759. U(p,q) = - CTEMP
  1760. 9971 CONTINUE
  1761. 9970 CONTINUE
  1762. ELSE
  1763. CALL CLASET('U', NR-1, NR-1, CZERO, CZERO, U(1,2), LDU )
  1764. END IF
  1765. CALL CGESVJ( 'L', 'U', 'V', NR, NR, U, LDU, SVA,
  1766. $ N, V, LDV, CWORK(2*N+N*NR+1), LWORK-2*N-N*NR,
  1767. $ RWORK, LRWORK, INFO )
  1768. SCALEM = RWORK(1)
  1769. NUMRANK = NINT(RWORK(2))
  1770. IF ( NR .LT. N ) THEN
  1771. CALL CLASET( 'A',N-NR,NR,ZERO,ZERO,V(NR+1,1),LDV )
  1772. CALL CLASET( 'A',NR,N-NR,ZERO,ZERO,V(1,NR+1),LDV )
  1773. CALL CLASET( 'A',N-NR,N-NR,ZERO,ONE,V(NR+1,NR+1),LDV )
  1774. END IF
  1775. CALL CUNMQR( 'L','N',N,N,NR,CWORK(2*N+1),N,CWORK(N+1),
  1776. $ V,LDV,CWORK(2*N+N*NR+NR+1),LWORK-2*N-N*NR-NR,IERR )
  1777. *
  1778. * Permute the rows of V using the (column) permutation from the
  1779. * first QRF. Also, scale the columns to make them unit in
  1780. * Euclidean norm. This applies to all cases.
  1781. *
  1782. TEMP1 = SQRT(FLOAT(N)) * EPSLN
  1783. DO 7972 q = 1, N
  1784. DO 8972 p = 1, N
  1785. CWORK(2*N+N*NR+NR+IWORK(p)) = V(p,q)
  1786. 8972 CONTINUE
  1787. DO 8973 p = 1, N
  1788. V(p,q) = CWORK(2*N+N*NR+NR+p)
  1789. 8973 CONTINUE
  1790. XSC = ONE / SCNRM2( N, V(1,q), 1 )
  1791. IF ( (XSC .LT. (ONE-TEMP1)) .OR. (XSC .GT. (ONE+TEMP1)) )
  1792. $ CALL CSSCAL( N, XSC, V(1,q), 1 )
  1793. 7972 CONTINUE
  1794. *
  1795. * At this moment, V contains the right singular vectors of A.
  1796. * Next, assemble the left singular vector matrix U (M x N).
  1797. *
  1798. IF ( NR .LT. M ) THEN
  1799. CALL CLASET( 'A', M-NR, NR, CZERO, CZERO, U(NR+1,1), LDU )
  1800. IF ( NR .LT. N1 ) THEN
  1801. CALL CLASET('A',NR, N1-NR, CZERO, CZERO, U(1,NR+1),LDU)
  1802. CALL CLASET('A',M-NR,N1-NR, CZERO, CONE,U(NR+1,NR+1),LDU)
  1803. END IF
  1804. END IF
  1805. *
  1806. CALL CUNMQR( 'Left', 'No Tr', M, N1, N, A, LDA, CWORK, U,
  1807. $ LDU, CWORK(N+1), LWORK-N, IERR )
  1808. *
  1809. IF ( ROWPIV )
  1810. $ CALL CLASWP( N1, U, LDU, 1, M-1, IWORK(2*N+1), -1 )
  1811. *
  1812. *
  1813. END IF
  1814. IF ( TRANSP ) THEN
  1815. * .. swap U and V because the procedure worked on A^*
  1816. DO 6974 p = 1, N
  1817. CALL CSWAP( N, U(1,p), 1, V(1,p), 1 )
  1818. 6974 CONTINUE
  1819. END IF
  1820. *
  1821. END IF
  1822. * end of the full SVD
  1823. *
  1824. * Undo scaling, if necessary (and possible)
  1825. *
  1826. IF ( USCAL2 .LE. (BIG/SVA(1))*USCAL1 ) THEN
  1827. CALL CLASCL( 'G', 0, 0, USCAL1, USCAL2, NR, 1, SVA, N, IERR )
  1828. USCAL1 = ONE
  1829. USCAL2 = ONE
  1830. END IF
  1831. *
  1832. IF ( NR .LT. N ) THEN
  1833. DO 3004 p = NR+1, N
  1834. SVA(p) = ZERO
  1835. 3004 CONTINUE
  1836. END IF
  1837. *
  1838. RWORK(1) = USCAL2 * SCALEM
  1839. RWORK(2) = USCAL1
  1840. IF ( ERREST ) RWORK(3) = SCONDA
  1841. IF ( LSVEC .AND. RSVEC ) THEN
  1842. RWORK(4) = CONDR1
  1843. RWORK(5) = CONDR2
  1844. END IF
  1845. IF ( L2TRAN ) THEN
  1846. RWORK(6) = ENTRA
  1847. RWORK(7) = ENTRAT
  1848. END IF
  1849. *
  1850. IWORK(1) = NR
  1851. IWORK(2) = NUMRANK
  1852. IWORK(3) = WARNING
  1853. *
  1854. RETURN
  1855. * ..
  1856. * .. END OF CGEJSV
  1857. * ..
  1858. END
  1859. *