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.

zgejsv.f 77 kB

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