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.c 123 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <complex.h>
  6. #ifdef complex
  7. #undef complex
  8. #endif
  9. #ifdef I
  10. #undef I
  11. #endif
  12. #if defined(_WIN64)
  13. typedef long long BLASLONG;
  14. typedef unsigned long long BLASULONG;
  15. #else
  16. typedef long BLASLONG;
  17. typedef unsigned long BLASULONG;
  18. #endif
  19. #ifdef LAPACK_ILP64
  20. typedef BLASLONG blasint;
  21. #if defined(_WIN64)
  22. #define blasabs(x) llabs(x)
  23. #else
  24. #define blasabs(x) labs(x)
  25. #endif
  26. #else
  27. typedef int blasint;
  28. #define blasabs(x) abs(x)
  29. #endif
  30. typedef blasint integer;
  31. typedef unsigned int uinteger;
  32. typedef char *address;
  33. typedef short int shortint;
  34. typedef float real;
  35. typedef double doublereal;
  36. typedef struct { real r, i; } complex;
  37. typedef struct { doublereal r, i; } doublecomplex;
  38. #ifdef _MSC_VER
  39. static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
  40. static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
  41. static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
  42. static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
  43. #else
  44. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  45. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  46. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  47. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  48. #endif
  49. #define pCf(z) (*_pCf(z))
  50. #define pCd(z) (*_pCd(z))
  51. typedef int logical;
  52. typedef short int shortlogical;
  53. typedef char logical1;
  54. typedef char integer1;
  55. #define TRUE_ (1)
  56. #define FALSE_ (0)
  57. /* Extern is for use with -E */
  58. #ifndef Extern
  59. #define Extern extern
  60. #endif
  61. /* I/O stuff */
  62. typedef int flag;
  63. typedef int ftnlen;
  64. typedef int ftnint;
  65. /*external read, write*/
  66. typedef struct
  67. { flag cierr;
  68. ftnint ciunit;
  69. flag ciend;
  70. char *cifmt;
  71. ftnint cirec;
  72. } cilist;
  73. /*internal read, write*/
  74. typedef struct
  75. { flag icierr;
  76. char *iciunit;
  77. flag iciend;
  78. char *icifmt;
  79. ftnint icirlen;
  80. ftnint icirnum;
  81. } icilist;
  82. /*open*/
  83. typedef struct
  84. { flag oerr;
  85. ftnint ounit;
  86. char *ofnm;
  87. ftnlen ofnmlen;
  88. char *osta;
  89. char *oacc;
  90. char *ofm;
  91. ftnint orl;
  92. char *oblnk;
  93. } olist;
  94. /*close*/
  95. typedef struct
  96. { flag cerr;
  97. ftnint cunit;
  98. char *csta;
  99. } cllist;
  100. /*rewind, backspace, endfile*/
  101. typedef struct
  102. { flag aerr;
  103. ftnint aunit;
  104. } alist;
  105. /* inquire */
  106. typedef struct
  107. { flag inerr;
  108. ftnint inunit;
  109. char *infile;
  110. ftnlen infilen;
  111. ftnint *inex; /*parameters in standard's order*/
  112. ftnint *inopen;
  113. ftnint *innum;
  114. ftnint *innamed;
  115. char *inname;
  116. ftnlen innamlen;
  117. char *inacc;
  118. ftnlen inacclen;
  119. char *inseq;
  120. ftnlen inseqlen;
  121. char *indir;
  122. ftnlen indirlen;
  123. char *infmt;
  124. ftnlen infmtlen;
  125. char *inform;
  126. ftnint informlen;
  127. char *inunf;
  128. ftnlen inunflen;
  129. ftnint *inrecl;
  130. ftnint *innrec;
  131. char *inblank;
  132. ftnlen inblanklen;
  133. } inlist;
  134. #define VOID void
  135. union Multitype { /* for multiple entry points */
  136. integer1 g;
  137. shortint h;
  138. integer i;
  139. /* longint j; */
  140. real r;
  141. doublereal d;
  142. complex c;
  143. doublecomplex z;
  144. };
  145. typedef union Multitype Multitype;
  146. struct Vardesc { /* for Namelist */
  147. char *name;
  148. char *addr;
  149. ftnlen *dims;
  150. int type;
  151. };
  152. typedef struct Vardesc Vardesc;
  153. struct Namelist {
  154. char *name;
  155. Vardesc **vars;
  156. int nvars;
  157. };
  158. typedef struct Namelist Namelist;
  159. #define abs(x) ((x) >= 0 ? (x) : -(x))
  160. #define dabs(x) (fabs(x))
  161. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  162. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  163. #define dmin(a,b) (f2cmin(a,b))
  164. #define dmax(a,b) (f2cmax(a,b))
  165. #define bit_test(a,b) ((a) >> (b) & 1)
  166. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  167. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  168. #define abort_() { sig_die("Fortran abort routine called", 1); }
  169. #define c_abs(z) (cabsf(Cf(z)))
  170. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  171. #ifdef _MSC_VER
  172. #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
  173. #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/Cd(b)._Val[1]);}
  174. #else
  175. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  176. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  177. #endif
  178. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  179. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  180. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  181. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  182. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  183. #define d_abs(x) (fabs(*(x)))
  184. #define d_acos(x) (acos(*(x)))
  185. #define d_asin(x) (asin(*(x)))
  186. #define d_atan(x) (atan(*(x)))
  187. #define d_atn2(x, y) (atan2(*(x),*(y)))
  188. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  189. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  190. #define d_cos(x) (cos(*(x)))
  191. #define d_cosh(x) (cosh(*(x)))
  192. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  193. #define d_exp(x) (exp(*(x)))
  194. #define d_imag(z) (cimag(Cd(z)))
  195. #define r_imag(z) (cimagf(Cf(z)))
  196. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  198. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  200. #define d_log(x) (log(*(x)))
  201. #define d_mod(x, y) (fmod(*(x), *(y)))
  202. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  203. #define d_nint(x) u_nint(*(x))
  204. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  205. #define d_sign(a,b) u_sign(*(a),*(b))
  206. #define r_sign(a,b) u_sign(*(a),*(b))
  207. #define d_sin(x) (sin(*(x)))
  208. #define d_sinh(x) (sinh(*(x)))
  209. #define d_sqrt(x) (sqrt(*(x)))
  210. #define d_tan(x) (tan(*(x)))
  211. #define d_tanh(x) (tanh(*(x)))
  212. #define i_abs(x) abs(*(x))
  213. #define i_dnnt(x) ((integer)u_nint(*(x)))
  214. #define i_len(s, n) (n)
  215. #define i_nint(x) ((integer)u_nint(*(x)))
  216. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  217. #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
  218. #define pow_si(B,E) spow_ui(*(B),*(E))
  219. #define pow_ri(B,E) spow_ui(*(B),*(E))
  220. #define pow_di(B,E) dpow_ui(*(B),*(E))
  221. #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
  222. #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
  223. #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
  224. #define s_cat(lpp, rpp, rnp, np, llp) { ftnlen i, nc, ll; char *f__rp, *lp; ll = (llp); lp = (lpp); for(i=0; i < (int)*(np); ++i) { nc = ll; if((rnp)[i] < nc) nc = (rnp)[i]; ll -= nc; f__rp = (rpp)[i]; while(--nc >= 0) *lp++ = *(f__rp)++; } while(--ll >= 0) *lp++ = ' '; }
  225. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  226. #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
  227. #define sig_die(s, kill) { exit(1); }
  228. #define s_stop(s, n) {exit(0);}
  229. static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
  230. #define z_abs(z) (cabs(Cd(z)))
  231. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  232. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  233. #define myexit_() break;
  234. #define mycycle() continue;
  235. #define myceiling(w) {ceil(w)}
  236. #define myhuge(w) {HUGE_VAL}
  237. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  238. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  239. /* procedure parameter types for -A and -C++ */
  240. #define F2C_proc_par_types 1
  241. #ifdef __cplusplus
  242. typedef logical (*L_fp)(...);
  243. #else
  244. typedef logical (*L_fp)();
  245. #endif
  246. static float spow_ui(float x, integer n) {
  247. float pow=1.0; unsigned long int u;
  248. if(n != 0) {
  249. if(n < 0) n = -n, x = 1/x;
  250. for(u = n; ; ) {
  251. if(u & 01) pow *= x;
  252. if(u >>= 1) x *= x;
  253. else break;
  254. }
  255. }
  256. return pow;
  257. }
  258. static double dpow_ui(double x, integer n) {
  259. double pow=1.0; unsigned long int u;
  260. if(n != 0) {
  261. if(n < 0) n = -n, x = 1/x;
  262. for(u = n; ; ) {
  263. if(u & 01) pow *= x;
  264. if(u >>= 1) x *= x;
  265. else break;
  266. }
  267. }
  268. return pow;
  269. }
  270. #ifdef _MSC_VER
  271. static _Fcomplex cpow_ui(complex x, integer n) {
  272. complex pow={1.0,0.0}; unsigned long int u;
  273. if(n != 0) {
  274. if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
  275. for(u = n; ; ) {
  276. if(u & 01) pow.r *= x.r, pow.i *= x.i;
  277. if(u >>= 1) x.r *= x.r, x.i *= x.i;
  278. else break;
  279. }
  280. }
  281. _Fcomplex p={pow.r, pow.i};
  282. return p;
  283. }
  284. #else
  285. static _Complex float cpow_ui(_Complex float x, integer n) {
  286. _Complex float pow=1.0; unsigned long int u;
  287. if(n != 0) {
  288. if(n < 0) n = -n, x = 1/x;
  289. for(u = n; ; ) {
  290. if(u & 01) pow *= x;
  291. if(u >>= 1) x *= x;
  292. else break;
  293. }
  294. }
  295. return pow;
  296. }
  297. #endif
  298. #ifdef _MSC_VER
  299. static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
  300. _Dcomplex pow={1.0,0.0}; unsigned long int u;
  301. if(n != 0) {
  302. if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
  303. for(u = n; ; ) {
  304. if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
  305. if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
  306. else break;
  307. }
  308. }
  309. _Dcomplex p = {pow._Val[0], pow._Val[1]};
  310. return p;
  311. }
  312. #else
  313. static _Complex double zpow_ui(_Complex double x, integer n) {
  314. _Complex double pow=1.0; unsigned long int u;
  315. if(n != 0) {
  316. if(n < 0) n = -n, x = 1/x;
  317. for(u = n; ; ) {
  318. if(u & 01) pow *= x;
  319. if(u >>= 1) x *= x;
  320. else break;
  321. }
  322. }
  323. return pow;
  324. }
  325. #endif
  326. static integer pow_ii(integer x, integer n) {
  327. integer pow; unsigned long int u;
  328. if (n <= 0) {
  329. if (n == 0 || x == 1) pow = 1;
  330. else if (x != -1) pow = x == 0 ? 1/x : 0;
  331. else n = -n;
  332. }
  333. if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
  334. u = n;
  335. for(pow = 1; ; ) {
  336. if(u & 01) pow *= x;
  337. if(u >>= 1) x *= x;
  338. else break;
  339. }
  340. }
  341. return pow;
  342. }
  343. static integer dmaxloc_(double *w, integer s, integer e, integer *n)
  344. {
  345. double m; integer i, mi;
  346. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  347. if (w[i-1]>m) mi=i ,m=w[i-1];
  348. return mi-s+1;
  349. }
  350. static integer smaxloc_(float *w, integer s, integer e, integer *n)
  351. {
  352. float m; integer i, mi;
  353. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  354. if (w[i-1]>m) mi=i ,m=w[i-1];
  355. return mi-s+1;
  356. }
  357. static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  358. integer n = *n_, incx = *incx_, incy = *incy_, i;
  359. #ifdef _MSC_VER
  360. _Fcomplex zdotc = {0.0, 0.0};
  361. if (incx == 1 && incy == 1) {
  362. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  363. zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
  364. zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
  365. }
  366. } else {
  367. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  368. zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
  369. zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
  370. }
  371. }
  372. pCf(z) = zdotc;
  373. }
  374. #else
  375. _Complex float zdotc = 0.0;
  376. if (incx == 1 && incy == 1) {
  377. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  378. zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
  379. }
  380. } else {
  381. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  382. zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
  383. }
  384. }
  385. pCf(z) = zdotc;
  386. }
  387. #endif
  388. static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  389. integer n = *n_, incx = *incx_, incy = *incy_, i;
  390. #ifdef _MSC_VER
  391. _Dcomplex zdotc = {0.0, 0.0};
  392. if (incx == 1 && incy == 1) {
  393. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  394. zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
  395. zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
  396. }
  397. } else {
  398. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  399. zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
  400. zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
  401. }
  402. }
  403. pCd(z) = zdotc;
  404. }
  405. #else
  406. _Complex double zdotc = 0.0;
  407. if (incx == 1 && incy == 1) {
  408. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  409. zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
  410. }
  411. } else {
  412. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  413. zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
  414. }
  415. }
  416. pCd(z) = zdotc;
  417. }
  418. #endif
  419. static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  420. integer n = *n_, incx = *incx_, incy = *incy_, i;
  421. #ifdef _MSC_VER
  422. _Fcomplex zdotc = {0.0, 0.0};
  423. if (incx == 1 && incy == 1) {
  424. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  425. zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
  426. zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
  427. }
  428. } else {
  429. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  430. zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
  431. zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
  432. }
  433. }
  434. pCf(z) = zdotc;
  435. }
  436. #else
  437. _Complex float zdotc = 0.0;
  438. if (incx == 1 && incy == 1) {
  439. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  440. zdotc += Cf(&x[i]) * Cf(&y[i]);
  441. }
  442. } else {
  443. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  444. zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
  445. }
  446. }
  447. pCf(z) = zdotc;
  448. }
  449. #endif
  450. static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  451. integer n = *n_, incx = *incx_, incy = *incy_, i;
  452. #ifdef _MSC_VER
  453. _Dcomplex zdotc = {0.0, 0.0};
  454. if (incx == 1 && incy == 1) {
  455. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  456. zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
  457. zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
  458. }
  459. } else {
  460. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  461. zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
  462. zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
  463. }
  464. }
  465. pCd(z) = zdotc;
  466. }
  467. #else
  468. _Complex double zdotc = 0.0;
  469. if (incx == 1 && incy == 1) {
  470. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  471. zdotc += Cd(&x[i]) * Cd(&y[i]);
  472. }
  473. } else {
  474. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  475. zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
  476. }
  477. }
  478. pCd(z) = zdotc;
  479. }
  480. #endif
  481. /* -- translated by f2c (version 20000121).
  482. You must link the resulting object file with the libraries:
  483. -lf2c -lm (in that order)
  484. */
  485. /* Table of constant values */
  486. static doublecomplex c_b1 = {0.,0.};
  487. static doublecomplex c_b2 = {1.,0.};
  488. static integer c_n1 = -1;
  489. static integer c__1 = 1;
  490. static integer c__0 = 0;
  491. static doublereal c_b141 = 1.;
  492. static logical c_false = FALSE_;
  493. /* > \brief \b ZGEJSV */
  494. /* =========== DOCUMENTATION =========== */
  495. /* Online html documentation available at */
  496. /* http://www.netlib.org/lapack/explore-html/ */
  497. /* > \htmlonly */
  498. /* > Download ZGEJSV + dependencies */
  499. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zgejsv.
  500. f"> */
  501. /* > [TGZ]</a> */
  502. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zgejsv.
  503. f"> */
  504. /* > [ZIP]</a> */
  505. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zgejsv.
  506. f"> */
  507. /* > [TXT]</a> */
  508. /* > \endhtmlonly */
  509. /* Definition: */
  510. /* =========== */
  511. /* SUBROUTINE ZGEJSV( JOBA, JOBU, JOBV, JOBR, JOBT, JOBP, */
  512. /* M, N, A, LDA, SVA, U, LDU, V, LDV, */
  513. /* CWORK, LWORK, RWORK, LRWORK, IWORK, INFO ) */
  514. /* IMPLICIT NONE */
  515. /* INTEGER INFO, LDA, LDU, LDV, LWORK, M, N */
  516. /* COMPLEX*16 A( LDA, * ), U( LDU, * ), V( LDV, * ), CWORK( LWORK ) */
  517. /* DOUBLE PRECISION SVA( N ), RWORK( LRWORK ) */
  518. /* INTEGER IWORK( * ) */
  519. /* CHARACTER*1 JOBA, JOBP, JOBR, JOBT, JOBU, JOBV */
  520. /* > \par Purpose: */
  521. /* ============= */
  522. /* > */
  523. /* > \verbatim */
  524. /* > */
  525. /* > ZGEJSV computes the singular value decomposition (SVD) of a complex M-by-N */
  526. /* > matrix [A], where M >= N. The SVD of [A] is written as */
  527. /* > */
  528. /* > [A] = [U] * [SIGMA] * [V]^*, */
  529. /* > */
  530. /* > where [SIGMA] is an N-by-N (M-by-N) matrix which is zero except for its N */
  531. /* > diagonal elements, [U] is an M-by-N (or M-by-M) unitary matrix, and */
  532. /* > [V] is an N-by-N unitary matrix. The diagonal elements of [SIGMA] are */
  533. /* > the singular values of [A]. The columns of [U] and [V] are the left and */
  534. /* > the right singular vectors of [A], respectively. The matrices [U] and [V] */
  535. /* > are computed and stored in the arrays U and V, respectively. The diagonal */
  536. /* > of [SIGMA] is computed and stored in the array SVA. */
  537. /* > \endverbatim */
  538. /* > */
  539. /* > Arguments: */
  540. /* > ========== */
  541. /* > */
  542. /* > \param[in] JOBA */
  543. /* > \verbatim */
  544. /* > JOBA is CHARACTER*1 */
  545. /* > Specifies the level of accuracy: */
  546. /* > = 'C': This option works well (high relative accuracy) if A = B * D, */
  547. /* > with well-conditioned B and arbitrary diagonal matrix D. */
  548. /* > The accuracy cannot be spoiled by COLUMN scaling. The */
  549. /* > accuracy of the computed output depends on the condition of */
  550. /* > B, and the procedure aims at the best theoretical accuracy. */
  551. /* > The relative error max_{i=1:N}|d sigma_i| / sigma_i is */
  552. /* > bounded by f(M,N)*epsilon* cond(B), independent of D. */
  553. /* > The input matrix is preprocessed with the QRF with column */
  554. /* > pivoting. This initial preprocessing and preconditioning by */
  555. /* > a rank revealing QR factorization is common for all values of */
  556. /* > JOBA. Additional actions are specified as follows: */
  557. /* > = 'E': Computation as with 'C' with an additional estimate of the */
  558. /* > condition number of B. It provides a realistic error bound. */
  559. /* > = 'F': If A = D1 * C * D2 with ill-conditioned diagonal scalings */
  560. /* > D1, D2, and well-conditioned matrix C, this option gives */
  561. /* > higher accuracy than the 'C' option. If the structure of the */
  562. /* > input matrix is not known, and relative accuracy is */
  563. /* > desirable, then this option is advisable. The input matrix A */
  564. /* > is preprocessed with QR factorization with FULL (row and */
  565. /* > column) pivoting. */
  566. /* > = 'G': Computation as with 'F' with an additional estimate of the */
  567. /* > condition number of B, where A=B*D. If A has heavily weighted */
  568. /* > rows, then using this condition number gives too pessimistic */
  569. /* > error bound. */
  570. /* > = 'A': Small singular values are not well determined by the data */
  571. /* > and are considered as noisy; the matrix is treated as */
  572. /* > numerically rank deficient. The error in the computed */
  573. /* > singular values is bounded by f(m,n)*epsilon*||A||. */
  574. /* > The computed SVD A = U * S * V^* restores A up to */
  575. /* > f(m,n)*epsilon*||A||. */
  576. /* > This gives the procedure the licence to discard (set to zero) */
  577. /* > all singular values below N*epsilon*||A||. */
  578. /* > = 'R': Similar as in 'A'. Rank revealing property of the initial */
  579. /* > QR factorization is used do reveal (using triangular factor) */
  580. /* > a gap sigma_{r+1} < epsilon * sigma_r in which case the */
  581. /* > numerical RANK is declared to be r. The SVD is computed with */
  582. /* > absolute error bounds, but more accurately than with 'A'. */
  583. /* > \endverbatim */
  584. /* > */
  585. /* > \param[in] JOBU */
  586. /* > \verbatim */
  587. /* > JOBU is CHARACTER*1 */
  588. /* > Specifies whether to compute the columns of U: */
  589. /* > = 'U': N columns of U are returned in the array U. */
  590. /* > = 'F': full set of M left sing. vectors is returned in the array U. */
  591. /* > = 'W': U may be used as workspace of length M*N. See the description */
  592. /* > of U. */
  593. /* > = 'N': U is not computed. */
  594. /* > \endverbatim */
  595. /* > */
  596. /* > \param[in] JOBV */
  597. /* > \verbatim */
  598. /* > JOBV is CHARACTER*1 */
  599. /* > Specifies whether to compute the matrix V: */
  600. /* > = 'V': N columns of V are returned in the array V; Jacobi rotations */
  601. /* > are not explicitly accumulated. */
  602. /* > = 'J': N columns of V are returned in the array V, but they are */
  603. /* > computed as the product of Jacobi rotations, if JOBT = 'N'. */
  604. /* > = 'W': V may be used as workspace of length N*N. See the description */
  605. /* > of V. */
  606. /* > = 'N': V is not computed. */
  607. /* > \endverbatim */
  608. /* > */
  609. /* > \param[in] JOBR */
  610. /* > \verbatim */
  611. /* > JOBR is CHARACTER*1 */
  612. /* > Specifies the RANGE for the singular values. Issues the licence to */
  613. /* > set to zero small positive singular values if they are outside */
  614. /* > specified range. If A .NE. 0 is scaled so that the largest singular */
  615. /* > value of c*A is around SQRT(BIG), BIG=DLAMCH('O'), then JOBR issues */
  616. /* > the licence to kill columns of A whose norm in c*A is less than */
  617. /* > SQRT(SFMIN) (for JOBR = 'R'), or less than SMALL=SFMIN/EPSLN, */
  618. /* > where SFMIN=DLAMCH('S'), EPSLN=DLAMCH('E'). */
  619. /* > = 'N': Do not kill small columns of c*A. This option assumes that */
  620. /* > BLAS and QR factorizations and triangular solvers are */
  621. /* > implemented to work in that range. If the condition of A */
  622. /* > is greater than BIG, use ZGESVJ. */
  623. /* > = 'R': RESTRICTED range for sigma(c*A) is [SQRT(SFMIN), SQRT(BIG)] */
  624. /* > (roughly, as described above). This option is recommended. */
  625. /* > =========================== */
  626. /* > For computing the singular values in the FULL range [SFMIN,BIG] */
  627. /* > use ZGESVJ. */
  628. /* > \endverbatim */
  629. /* > */
  630. /* > \param[in] JOBT */
  631. /* > \verbatim */
  632. /* > JOBT is CHARACTER*1 */
  633. /* > If the matrix is square then the procedure may determine to use */
  634. /* > transposed A if A^* seems to be better with respect to convergence. */
  635. /* > If the matrix is not square, JOBT is ignored. */
  636. /* > The decision is based on two values of entropy over the adjoint */
  637. /* > orbit of A^* * A. See the descriptions of WORK(6) and WORK(7). */
  638. /* > = 'T': transpose if entropy test indicates possibly faster */
  639. /* > convergence of Jacobi process if A^* is taken as input. If A is */
  640. /* > replaced with A^*, then the row pivoting is included automatically. */
  641. /* > = 'N': do not speculate. */
  642. /* > The option 'T' can be used to compute only the singular values, or */
  643. /* > the full SVD (U, SIGMA and V). For only one set of singular vectors */
  644. /* > (U or V), the caller should provide both U and V, as one of the */
  645. /* > matrices is used as workspace if the matrix A is transposed. */
  646. /* > The implementer can easily remove this constraint and make the */
  647. /* > code more complicated. See the descriptions of U and V. */
  648. /* > In general, this option is considered experimental, and 'N'; should */
  649. /* > be preferred. This is subject to changes in the future. */
  650. /* > \endverbatim */
  651. /* > */
  652. /* > \param[in] JOBP */
  653. /* > \verbatim */
  654. /* > JOBP is CHARACTER*1 */
  655. /* > Issues the licence to introduce structured perturbations to drown */
  656. /* > denormalized numbers. This licence should be active if the */
  657. /* > denormals are poorly implemented, causing slow computation, */
  658. /* > especially in cases of fast convergence (!). For details see [1,2]. */
  659. /* > For the sake of simplicity, this perturbations are included only */
  660. /* > when the full SVD or only the singular values are requested. The */
  661. /* > implementer/user can easily add the perturbation for the cases of */
  662. /* > computing one set of singular vectors. */
  663. /* > = 'P': introduce perturbation */
  664. /* > = 'N': do not perturb */
  665. /* > \endverbatim */
  666. /* > */
  667. /* > \param[in] M */
  668. /* > \verbatim */
  669. /* > M is INTEGER */
  670. /* > The number of rows of the input matrix A. M >= 0. */
  671. /* > \endverbatim */
  672. /* > */
  673. /* > \param[in] N */
  674. /* > \verbatim */
  675. /* > N is INTEGER */
  676. /* > The number of columns of the input matrix A. M >= N >= 0. */
  677. /* > \endverbatim */
  678. /* > */
  679. /* > \param[in,out] A */
  680. /* > \verbatim */
  681. /* > A is COMPLEX*16 array, dimension (LDA,N) */
  682. /* > On entry, the M-by-N matrix A. */
  683. /* > \endverbatim */
  684. /* > */
  685. /* > \param[in] LDA */
  686. /* > \verbatim */
  687. /* > LDA is INTEGER */
  688. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  689. /* > \endverbatim */
  690. /* > */
  691. /* > \param[out] SVA */
  692. /* > \verbatim */
  693. /* > SVA is DOUBLE PRECISION array, dimension (N) */
  694. /* > On exit, */
  695. /* > - For WORK(1)/WORK(2) = ONE: The singular values of A. During the */
  696. /* > computation SVA contains Euclidean column norms of the */
  697. /* > iterated matrices in the array A. */
  698. /* > - For WORK(1) .NE. WORK(2): The singular values of A are */
  699. /* > (WORK(1)/WORK(2)) * SVA(1:N). This factored form is used if */
  700. /* > sigma_max(A) overflows or if small singular values have been */
  701. /* > saved from underflow by scaling the input matrix A. */
  702. /* > - If JOBR='R' then some of the singular values may be returned */
  703. /* > as exact zeros obtained by "set to zero" because they are */
  704. /* > below the numerical rank threshold or are denormalized numbers. */
  705. /* > \endverbatim */
  706. /* > */
  707. /* > \param[out] U */
  708. /* > \verbatim */
  709. /* > U is COMPLEX*16 array, dimension ( LDU, N ) */
  710. /* > If JOBU = 'U', then U contains on exit the M-by-N matrix of */
  711. /* > the left singular vectors. */
  712. /* > If JOBU = 'F', then U contains on exit the M-by-M matrix of */
  713. /* > the left singular vectors, including an ONB */
  714. /* > of the orthogonal complement of the Range(A). */
  715. /* > If JOBU = 'W' .AND. (JOBV = 'V' .AND. JOBT = 'T' .AND. M = N), */
  716. /* > then U is used as workspace if the procedure */
  717. /* > replaces A with A^*. In that case, [V] is computed */
  718. /* > in U as left singular vectors of A^* and then */
  719. /* > copied back to the V array. This 'W' option is just */
  720. /* > a reminder to the caller that in this case U is */
  721. /* > reserved as workspace of length N*N. */
  722. /* > If JOBU = 'N' U is not referenced, unless JOBT='T'. */
  723. /* > \endverbatim */
  724. /* > */
  725. /* > \param[in] LDU */
  726. /* > \verbatim */
  727. /* > LDU is INTEGER */
  728. /* > The leading dimension of the array U, LDU >= 1. */
  729. /* > IF JOBU = 'U' or 'F' or 'W', then LDU >= M. */
  730. /* > \endverbatim */
  731. /* > */
  732. /* > \param[out] V */
  733. /* > \verbatim */
  734. /* > V is COMPLEX*16 array, dimension ( LDV, N ) */
  735. /* > If JOBV = 'V', 'J' then V contains on exit the N-by-N matrix of */
  736. /* > the right singular vectors; */
  737. /* > If JOBV = 'W', AND (JOBU = 'U' AND JOBT = 'T' AND M = N), */
  738. /* > then V is used as workspace if the pprocedure */
  739. /* > replaces A with A^*. In that case, [U] is computed */
  740. /* > in V as right singular vectors of A^* and then */
  741. /* > copied back to the U array. This 'W' option is just */
  742. /* > a reminder to the caller that in this case V is */
  743. /* > reserved as workspace of length N*N. */
  744. /* > If JOBV = 'N' V is not referenced, unless JOBT='T'. */
  745. /* > \endverbatim */
  746. /* > */
  747. /* > \param[in] LDV */
  748. /* > \verbatim */
  749. /* > LDV is INTEGER */
  750. /* > The leading dimension of the array V, LDV >= 1. */
  751. /* > If JOBV = 'V' or 'J' or 'W', then LDV >= N. */
  752. /* > \endverbatim */
  753. /* > */
  754. /* > \param[out] CWORK */
  755. /* > \verbatim */
  756. /* > CWORK is COMPLEX*16 array, dimension (MAX(2,LWORK)) */
  757. /* > If the call to ZGEJSV is a workspace query (indicated by LWORK=-1 or */
  758. /* > LRWORK=-1), then on exit CWORK(1) contains the required length of */
  759. /* > CWORK for the job parameters used in the call. */
  760. /* > \endverbatim */
  761. /* > */
  762. /* > \param[in] LWORK */
  763. /* > \verbatim */
  764. /* > LWORK is INTEGER */
  765. /* > Length of CWORK to confirm proper allocation of workspace. */
  766. /* > LWORK depends on the job: */
  767. /* > */
  768. /* > 1. If only SIGMA is needed ( JOBU = 'N', JOBV = 'N' ) and */
  769. /* > 1.1 .. no scaled condition estimate required (JOBA.NE.'E'.AND.JOBA.NE.'G'): */
  770. /* > LWORK >= 2*N+1. This is the minimal requirement. */
  771. /* > ->> For optimal performance (blocked code) the optimal value */
  772. /* > is LWORK >= N + (N+1)*NB. Here NB is the optimal */
  773. /* > block size for ZGEQP3 and ZGEQRF. */
  774. /* > In general, optimal LWORK is computed as */
  775. /* > LWORK >= f2cmax(N+LWORK(ZGEQP3),N+LWORK(ZGEQRF), LWORK(ZGESVJ)). */
  776. /* > 1.2. .. an estimate of the scaled condition number of A is */
  777. /* > required (JOBA='E', or 'G'). In this case, LWORK the minimal */
  778. /* > requirement is LWORK >= N*N + 2*N. */
  779. /* > ->> For optimal performance (blocked code) the optimal value */
  780. /* > is LWORK >= f2cmax(N+(N+1)*NB, N*N+2*N)=N**2+2*N. */
  781. /* > In general, the optimal length LWORK is computed as */
  782. /* > LWORK >= f2cmax(N+LWORK(ZGEQP3),N+LWORK(ZGEQRF), LWORK(ZGESVJ), */
  783. /* > N*N+LWORK(ZPOCON)). */
  784. /* > 2. If SIGMA and the right singular vectors are needed (JOBV = 'V'), */
  785. /* > (JOBU = 'N') */
  786. /* > 2.1 .. no scaled condition estimate requested (JOBE = 'N'): */
  787. /* > -> the minimal requirement is LWORK >= 3*N. */
  788. /* > -> For optimal performance, */
  789. /* > LWORK >= f2cmax(N+(N+1)*NB, 2*N+N*NB)=2*N+N*NB, */
  790. /* > where NB is the optimal block size for ZGEQP3, ZGEQRF, ZGELQ, */
  791. /* > ZUNMLQ. In general, the optimal length LWORK is computed as */
  792. /* > LWORK >= f2cmax(N+LWORK(ZGEQP3), N+LWORK(ZGESVJ), */
  793. /* > N+LWORK(ZGELQF), 2*N+LWORK(ZGEQRF), N+LWORK(ZUNMLQ)). */
  794. /* > 2.2 .. an estimate of the scaled condition number of A is */
  795. /* > required (JOBA='E', or 'G'). */
  796. /* > -> the minimal requirement is LWORK >= 3*N. */
  797. /* > -> For optimal performance, */
  798. /* > LWORK >= f2cmax(N+(N+1)*NB, 2*N,2*N+N*NB)=2*N+N*NB, */
  799. /* > where NB is the optimal block size for ZGEQP3, ZGEQRF, ZGELQ, */
  800. /* > ZUNMLQ. In general, the optimal length LWORK is computed as */
  801. /* > LWORK >= f2cmax(N+LWORK(ZGEQP3), LWORK(ZPOCON), N+LWORK(ZGESVJ), */
  802. /* > N+LWORK(ZGELQF), 2*N+LWORK(ZGEQRF), N+LWORK(ZUNMLQ)). */
  803. /* > 3. If SIGMA and the left singular vectors are needed */
  804. /* > 3.1 .. no scaled condition estimate requested (JOBE = 'N'): */
  805. /* > -> the minimal requirement is LWORK >= 3*N. */
  806. /* > -> For optimal performance: */
  807. /* > if JOBU = 'U' :: LWORK >= f2cmax(3*N, N+(N+1)*NB, 2*N+N*NB)=2*N+N*NB, */
  808. /* > where NB is the optimal block size for ZGEQP3, ZGEQRF, ZUNMQR. */
  809. /* > In general, the optimal length LWORK is computed as */
  810. /* > LWORK >= f2cmax(N+LWORK(ZGEQP3), 2*N+LWORK(ZGEQRF), N+LWORK(ZUNMQR)). */
  811. /* > 3.2 .. an estimate of the scaled condition number of A is */
  812. /* > required (JOBA='E', or 'G'). */
  813. /* > -> the minimal requirement is LWORK >= 3*N. */
  814. /* > -> For optimal performance: */
  815. /* > if JOBU = 'U' :: LWORK >= f2cmax(3*N, N+(N+1)*NB, 2*N+N*NB)=2*N+N*NB, */
  816. /* > where NB is the optimal block size for ZGEQP3, ZGEQRF, ZUNMQR. */
  817. /* > In general, the optimal length LWORK is computed as */
  818. /* > LWORK >= f2cmax(N+LWORK(ZGEQP3),N+LWORK(ZPOCON), */
  819. /* > 2*N+LWORK(ZGEQRF), N+LWORK(ZUNMQR)). */
  820. /* > 4. If the full SVD is needed: (JOBU = 'U' or JOBU = 'F') and */
  821. /* > 4.1. if JOBV = 'V' */
  822. /* > the minimal requirement is LWORK >= 5*N+2*N*N. */
  823. /* > 4.2. if JOBV = 'J' the minimal requirement is */
  824. /* > LWORK >= 4*N+N*N. */
  825. /* > In both cases, the allocated CWORK can accommodate blocked runs */
  826. /* > of ZGEQP3, ZGEQRF, ZGELQF, SUNMQR, ZUNMLQ. */
  827. /* > */
  828. /* > If the call to ZGEJSV is a workspace query (indicated by LWORK=-1 or */
  829. /* > LRWORK=-1), then on exit CWORK(1) contains the optimal and CWORK(2) contains the */
  830. /* > minimal length of CWORK for the job parameters used in the call. */
  831. /* > \endverbatim */
  832. /* > */
  833. /* > \param[out] RWORK */
  834. /* > \verbatim */
  835. /* > RWORK is DOUBLE PRECISION array, dimension (MAX(7,LWORK)) */
  836. /* > On exit, */
  837. /* > RWORK(1) = Determines the scaling factor SCALE = RWORK(2) / RWORK(1) */
  838. /* > such that SCALE*SVA(1:N) are the computed singular values */
  839. /* > of A. (See the description of SVA().) */
  840. /* > RWORK(2) = See the description of RWORK(1). */
  841. /* > RWORK(3) = SCONDA is an estimate for the condition number of */
  842. /* > column equilibrated A. (If JOBA = 'E' or 'G') */
  843. /* > SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1). */
  844. /* > It is computed using SPOCON. It holds */
  845. /* > N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
  846. /* > where R is the triangular factor from the QRF of A. */
  847. /* > However, if R is truncated and the numerical rank is */
  848. /* > determined to be strictly smaller than N, SCONDA is */
  849. /* > returned as -1, thus indicating that the smallest */
  850. /* > singular values might be lost. */
  851. /* > */
  852. /* > If full SVD is needed, the following two condition numbers are */
  853. /* > useful for the analysis of the algorithm. They are provied for */
  854. /* > a developer/implementer who is familiar with the details of */
  855. /* > the method. */
  856. /* > */
  857. /* > RWORK(4) = an estimate of the scaled condition number of the */
  858. /* > triangular factor in the first QR factorization. */
  859. /* > RWORK(5) = an estimate of the scaled condition number of the */
  860. /* > triangular factor in the second QR factorization. */
  861. /* > The following two parameters are computed if JOBT = 'T'. */
  862. /* > They are provided for a developer/implementer who is familiar */
  863. /* > with the details of the method. */
  864. /* > RWORK(6) = the entropy of A^* * A :: this is the Shannon entropy */
  865. /* > of diag(A^* * A) / Trace(A^* * A) taken as point in the */
  866. /* > probability simplex. */
  867. /* > RWORK(7) = the entropy of A * A^*. (See the description of RWORK(6).) */
  868. /* > If the call to ZGEJSV is a workspace query (indicated by LWORK=-1 or */
  869. /* > LRWORK=-1), then on exit RWORK(1) contains the required length of */
  870. /* > RWORK for the job parameters used in the call. */
  871. /* > \endverbatim */
  872. /* > */
  873. /* > \param[in] LRWORK */
  874. /* > \verbatim */
  875. /* > LRWORK is INTEGER */
  876. /* > Length of RWORK to confirm proper allocation of workspace. */
  877. /* > LRWORK depends on the job: */
  878. /* > */
  879. /* > 1. If only the singular values are requested i.e. if */
  880. /* > LSAME(JOBU,'N') .AND. LSAME(JOBV,'N') */
  881. /* > then: */
  882. /* > 1.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
  883. /* > then: LRWORK = f2cmax( 7, 2 * M ). */
  884. /* > 1.2. Otherwise, LRWORK = f2cmax( 7, N ). */
  885. /* > 2. If singular values with the right singular vectors are requested */
  886. /* > i.e. if */
  887. /* > (LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) .AND. */
  888. /* > .NOT.(LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) */
  889. /* > then: */
  890. /* > 2.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
  891. /* > then LRWORK = f2cmax( 7, 2 * M ). */
  892. /* > 2.2. Otherwise, LRWORK = f2cmax( 7, N ). */
  893. /* > 3. If singular values with the left singular vectors are requested, i.e. if */
  894. /* > (LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) .AND. */
  895. /* > .NOT.(LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) */
  896. /* > then: */
  897. /* > 3.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
  898. /* > then LRWORK = f2cmax( 7, 2 * M ). */
  899. /* > 3.2. Otherwise, LRWORK = f2cmax( 7, N ). */
  900. /* > 4. If singular values with both the left and the right singular vectors */
  901. /* > are requested, i.e. if */
  902. /* > (LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) .AND. */
  903. /* > (LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) */
  904. /* > then: */
  905. /* > 4.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
  906. /* > then LRWORK = f2cmax( 7, 2 * M ). */
  907. /* > 4.2. Otherwise, LRWORK = f2cmax( 7, N ). */
  908. /* > */
  909. /* > If, on entry, LRWORK = -1 or LWORK=-1, a workspace query is assumed and */
  910. /* > the length of RWORK is returned in RWORK(1). */
  911. /* > \endverbatim */
  912. /* > */
  913. /* > \param[out] IWORK */
  914. /* > \verbatim */
  915. /* > IWORK is INTEGER array, of dimension at least 4, that further depends */
  916. /* > on the job: */
  917. /* > */
  918. /* > 1. If only the singular values are requested then: */
  919. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  920. /* > then the length of IWORK is N+M; otherwise the length of IWORK is N. */
  921. /* > 2. If the singular values and the right singular vectors are requested then: */
  922. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  923. /* > then the length of IWORK is N+M; otherwise the length of IWORK is N. */
  924. /* > 3. If the singular values and the left singular vectors are requested then: */
  925. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  926. /* > then the length of IWORK is N+M; otherwise the length of IWORK is N. */
  927. /* > 4. If the singular values with both the left and the right singular vectors */
  928. /* > are requested, then: */
  929. /* > 4.1. If LSAME(JOBV,'J') the length of IWORK is determined as follows: */
  930. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  931. /* > then the length of IWORK is N+M; otherwise the length of IWORK is N. */
  932. /* > 4.2. If LSAME(JOBV,'V') the length of IWORK is determined as follows: */
  933. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  934. /* > then the length of IWORK is 2*N+M; otherwise the length of IWORK is 2*N. */
  935. /* > */
  936. /* > On exit, */
  937. /* > IWORK(1) = the numerical rank determined after the initial */
  938. /* > QR factorization with pivoting. See the descriptions */
  939. /* > of JOBA and JOBR. */
  940. /* > IWORK(2) = the number of the computed nonzero singular values */
  941. /* > IWORK(3) = if nonzero, a warning message: */
  942. /* > If IWORK(3) = 1 then some of the column norms of A */
  943. /* > were denormalized floats. The requested high accuracy */
  944. /* > is not warranted by the data. */
  945. /* > IWORK(4) = 1 or -1. If IWORK(4) = 1, then the procedure used A^* to */
  946. /* > do the job as specified by the JOB parameters. */
  947. /* > If the call to ZGEJSV is a workspace query (indicated by LWORK = -1 or */
  948. /* > LRWORK = -1), then on exit IWORK(1) contains the required length of */
  949. /* > IWORK for the job parameters used in the call. */
  950. /* > \endverbatim */
  951. /* > */
  952. /* > \param[out] INFO */
  953. /* > \verbatim */
  954. /* > INFO is INTEGER */
  955. /* > < 0: if INFO = -i, then the i-th argument had an illegal value. */
  956. /* > = 0: successful exit; */
  957. /* > > 0: ZGEJSV did not converge in the maximal allowed number */
  958. /* > of sweeps. The computed values may be inaccurate. */
  959. /* > \endverbatim */
  960. /* Authors: */
  961. /* ======== */
  962. /* > \author Univ. of Tennessee */
  963. /* > \author Univ. of California Berkeley */
  964. /* > \author Univ. of Colorado Denver */
  965. /* > \author NAG Ltd. */
  966. /* > \date June 2016 */
  967. /* > \ingroup complex16GEsing */
  968. /* > \par Further Details: */
  969. /* ===================== */
  970. /* > */
  971. /* > \verbatim */
  972. /* > */
  973. /* > ZGEJSV implements a preconditioned Jacobi SVD algorithm. It uses ZGEQP3, */
  974. /* > ZGEQRF, and ZGELQF as preprocessors and preconditioners. Optionally, an */
  975. /* > additional row pivoting can be used as a preprocessor, which in some */
  976. /* > cases results in much higher accuracy. An example is matrix A with the */
  977. /* > structure A = D1 * C * D2, where D1, D2 are arbitrarily ill-conditioned */
  978. /* > diagonal matrices and C is well-conditioned matrix. In that case, complete */
  979. /* > pivoting in the first QR factorizations provides accuracy dependent on the */
  980. /* > condition number of C, and independent of D1, D2. Such higher accuracy is */
  981. /* > not completely understood theoretically, but it works well in practice. */
  982. /* > Further, if A can be written as A = B*D, with well-conditioned B and some */
  983. /* > diagonal D, then the high accuracy is guaranteed, both theoretically and */
  984. /* > in software, independent of D. For more details see [1], [2]. */
  985. /* > The computational range for the singular values can be the full range */
  986. /* > ( UNDERFLOW,OVERFLOW ), provided that the machine arithmetic and the BLAS */
  987. /* > & LAPACK routines called by ZGEJSV are implemented to work in that range. */
  988. /* > If that is not the case, then the restriction for safe computation with */
  989. /* > the singular values in the range of normalized IEEE numbers is that the */
  990. /* > spectral condition number kappa(A)=sigma_max(A)/sigma_min(A) does not */
  991. /* > overflow. This code (ZGEJSV) is best used in this restricted range, */
  992. /* > meaning that singular values of magnitude below ||A||_2 / DLAMCH('O') are */
  993. /* > returned as zeros. See JOBR for details on this. */
  994. /* > Further, this implementation is somewhat slower than the one described */
  995. /* > in [1,2] due to replacement of some non-LAPACK components, and because */
  996. /* > the choice of some tuning parameters in the iterative part (ZGESVJ) is */
  997. /* > left to the implementer on a particular machine. */
  998. /* > The rank revealing QR factorization (in this code: ZGEQP3) should be */
  999. /* > implemented as in [3]. We have a new version of ZGEQP3 under development */
  1000. /* > that is more robust than the current one in LAPACK, with a cleaner cut in */
  1001. /* > rank deficient cases. It will be available in the SIGMA library [4]. */
  1002. /* > If M is much larger than N, it is obvious that the initial QRF with */
  1003. /* > column pivoting can be preprocessed by the QRF without pivoting. That */
  1004. /* > well known trick is not used in ZGEJSV because in some cases heavy row */
  1005. /* > weighting can be treated with complete pivoting. The overhead in cases */
  1006. /* > M much larger than N is then only due to pivoting, but the benefits in */
  1007. /* > terms of accuracy have prevailed. The implementer/user can incorporate */
  1008. /* > this extra QRF step easily. The implementer can also improve data movement */
  1009. /* > (matrix transpose, matrix copy, matrix transposed copy) - this */
  1010. /* > implementation of ZGEJSV uses only the simplest, naive data movement. */
  1011. /* > \endverbatim */
  1012. /* > \par Contributor: */
  1013. /* ================== */
  1014. /* > */
  1015. /* > Zlatko Drmac, Department of Mathematics, Faculty of Science, */
  1016. /* > University of Zagreb (Zagreb, Croatia); drmac@math.hr */
  1017. /* > \par References: */
  1018. /* ================ */
  1019. /* > */
  1020. /* > \verbatim */
  1021. /* > */
  1022. /* > [1] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I. */
  1023. /* > SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342. */
  1024. /* > LAPACK Working note 169. */
  1025. /* > [2] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II. */
  1026. /* > SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362. */
  1027. /* > LAPACK Working note 170. */
  1028. /* > [3] Z. Drmac and Z. Bujanovic: On the failure of rank-revealing QR */
  1029. /* > factorization software - a case study. */
  1030. /* > ACM Trans. Math. Softw. Vol. 35, No 2 (2008), pp. 1-28. */
  1031. /* > LAPACK Working note 176. */
  1032. /* > [4] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV, */
  1033. /* > QSVD, (H,K)-SVD computations. */
  1034. /* > Department of Mathematics, University of Zagreb, 2008, 2016. */
  1035. /* > \endverbatim */
  1036. /* > \par Bugs, examples and comments: */
  1037. /* ================================= */
  1038. /* > */
  1039. /* > Please report all bugs and send interesting examples and/or comments to */
  1040. /* > drmac@math.hr. Thank you. */
  1041. /* > */
  1042. /* ===================================================================== */
  1043. /* Subroutine */ void zgejsv_(char *joba, char *jobu, char *jobv, char *jobr,
  1044. char *jobt, char *jobp, integer *m, integer *n, doublecomplex *a,
  1045. integer *lda, doublereal *sva, doublecomplex *u, integer *ldu,
  1046. doublecomplex *v, integer *ldv, doublecomplex *cwork, integer *lwork,
  1047. doublereal *rwork, integer *lrwork, integer *iwork, integer *info)
  1048. {
  1049. /* System generated locals */
  1050. integer a_dim1, a_offset, u_dim1, u_offset, v_dim1, v_offset, i__1, i__2,
  1051. i__3, i__4, i__5, i__6, i__7, i__8, i__9, i__10, i__11;
  1052. doublereal d__1, d__2, d__3;
  1053. doublecomplex z__1;
  1054. /* Local variables */
  1055. integer lwrk_zgesvj__;
  1056. logical defr;
  1057. doublereal aapp, aaqq;
  1058. integer lwrk_zunmlq__, lwrk_zunmqr__;
  1059. logical kill;
  1060. integer ierr, lwrk_zgeqp3n__;
  1061. doublereal temp1;
  1062. integer lwunmqrm, lwqp3, p, q;
  1063. logical jracc;
  1064. extern /* Subroutine */ void dscal_(integer *, doublereal *, doublereal *,
  1065. integer *);
  1066. integer lwrk_zgesvju__, lwrk_zgesvjv__;
  1067. extern logical lsame_(char *, char *);
  1068. integer lwrk_zunmqrm__;
  1069. doublecomplex ctemp;
  1070. doublereal entra, small;
  1071. integer iwoff;
  1072. doublereal sfmin;
  1073. logical lsvec;
  1074. doublereal epsln;
  1075. logical rsvec;
  1076. integer lwcon, lwlqf, lwqrf, n1;
  1077. extern /* Subroutine */ void zcopy_(integer *, doublecomplex *, integer *,
  1078. doublecomplex *, integer *), zswap_(integer *, doublecomplex *,
  1079. integer *, doublecomplex *, integer *);
  1080. logical l2aber;
  1081. extern /* Subroutine */ void ztrsm_(char *, char *, char *, char *,
  1082. integer *, integer *, doublecomplex *, doublecomplex *, integer *,
  1083. doublecomplex *, integer *);
  1084. doublereal condr1, condr2, uscal1, uscal2;
  1085. logical l2kill, l2rank, l2tran, l2pert;
  1086. extern /* Subroutine */ void zgeqp3_(integer *, integer *, doublecomplex *,
  1087. integer *, integer *, doublecomplex *, doublecomplex *, integer *
  1088. , doublereal *, integer *);
  1089. extern doublereal dznrm2_(integer *, doublecomplex *, integer *);
  1090. integer lrwqp3;
  1091. extern doublereal dlamch_(char *);
  1092. integer nr;
  1093. extern /* Subroutine */ void dlascl_(char *, integer *, integer *,
  1094. doublereal *, doublereal *, integer *, integer *, doublereal *,
  1095. integer *, integer *);
  1096. extern integer idamax_(integer *, doublereal *, integer *);
  1097. doublereal scalem, sconda;
  1098. logical goscal;
  1099. doublereal aatmin, aatmax;
  1100. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  1101. logical noscal;
  1102. extern /* Subroutine */ void zdscal_(integer *, doublereal *,
  1103. doublecomplex *, integer *), zlacgv_(integer *, doublecomplex *,
  1104. integer *), dlassq_(integer *, doublereal *, integer *,
  1105. doublereal *, doublereal *);
  1106. extern integer izamax_(integer *, doublecomplex *, integer *);
  1107. extern /* Subroutine */ void zgelqf_(integer *, integer *, doublecomplex *,
  1108. integer *, doublecomplex *, doublecomplex *, integer *, integer *
  1109. ), zlascl_(char *, integer *, integer *, doublereal *, doublereal
  1110. *, integer *, integer *, doublecomplex *, integer *, integer *);
  1111. doublereal entrat;
  1112. logical almort;
  1113. doublecomplex cdummy[1];
  1114. extern /* Subroutine */ void zgeqrf_(integer *, integer *, doublecomplex *,
  1115. integer *, doublecomplex *, doublecomplex *, integer *, integer *
  1116. );
  1117. doublereal maxprj;
  1118. extern /* Subroutine */ void zlacpy_(char *, integer *, integer *,
  1119. doublecomplex *, integer *, doublecomplex *, integer *),
  1120. zlaset_(char *, integer *, integer *, doublecomplex *,
  1121. doublecomplex *, doublecomplex *, integer *);
  1122. logical errest;
  1123. integer lrwcon;
  1124. extern /* Subroutine */ void zlapmr_(logical *, integer *, integer *,
  1125. doublecomplex *, integer *, integer *);
  1126. logical transp;
  1127. integer minwrk, lwsvdj;
  1128. extern /* Subroutine */ void zpocon_(char *, integer *, doublecomplex *,
  1129. integer *, doublereal *, doublereal *, doublecomplex *,
  1130. doublereal *, integer *), zgesvj_(char *, char *, char *,
  1131. integer *, integer *, doublecomplex *, integer *, doublereal *,
  1132. integer *, doublecomplex *, integer *, doublecomplex *, integer *,
  1133. doublereal *, integer *, integer *);
  1134. doublereal rdummy[1];
  1135. extern /* Subroutine */ void zlassq_(integer *, doublecomplex *, integer *,
  1136. doublereal *, doublereal *);
  1137. logical lquery;
  1138. extern /* Subroutine */ int zlaswp_(integer *, doublecomplex *, integer *,
  1139. integer *, integer *, integer *, integer *);
  1140. logical rowpiv;
  1141. integer optwrk;
  1142. extern /* Subroutine */ void zungqr_(integer *, integer *, integer *,
  1143. doublecomplex *, integer *, doublecomplex *, doublecomplex *,
  1144. integer *, integer *), zunmlq_(char *, char *, integer *, integer
  1145. *, integer *, doublecomplex *, integer *, doublecomplex *,
  1146. doublecomplex *, integer *, doublecomplex *, integer *, integer *), zunmqr_(char *, char *, integer *, integer *,
  1147. integer *, doublecomplex *, integer *, doublecomplex *,
  1148. doublecomplex *, integer *, doublecomplex *, integer *, integer *);
  1149. doublereal big, cond_ok__, xsc;
  1150. integer lwrk_zgeqp3__;
  1151. doublereal big1;
  1152. integer warning, numrank, miniwrk, minrwrk, lrwsvdj, lwunmlq, lwsvdjv,
  1153. lwunmqr, lwrk_zgelqf__, lwrk_zgeqrf__;
  1154. /* -- LAPACK computational routine (version 3.7.1) -- */
  1155. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  1156. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  1157. /* June 2017 */
  1158. /* =========================================================================== */
  1159. /* Test the input arguments */
  1160. /* Parameter adjustments */
  1161. --sva;
  1162. a_dim1 = *lda;
  1163. a_offset = 1 + a_dim1 * 1;
  1164. a -= a_offset;
  1165. u_dim1 = *ldu;
  1166. u_offset = 1 + u_dim1 * 1;
  1167. u -= u_offset;
  1168. v_dim1 = *ldv;
  1169. v_offset = 1 + v_dim1 * 1;
  1170. v -= v_offset;
  1171. --cwork;
  1172. --rwork;
  1173. --iwork;
  1174. /* Function Body */
  1175. lsvec = lsame_(jobu, "U") || lsame_(jobu, "F");
  1176. jracc = lsame_(jobv, "J");
  1177. rsvec = lsame_(jobv, "V") || jracc;
  1178. rowpiv = lsame_(joba, "F") || lsame_(joba, "G");
  1179. l2rank = lsame_(joba, "R");
  1180. l2aber = lsame_(joba, "A");
  1181. errest = lsame_(joba, "E") || lsame_(joba, "G");
  1182. l2tran = lsame_(jobt, "T") && *m == *n;
  1183. l2kill = lsame_(jobr, "R");
  1184. defr = lsame_(jobr, "N");
  1185. l2pert = lsame_(jobp, "P");
  1186. lquery = *lwork == -1 || *lrwork == -1;
  1187. if (! (rowpiv || l2rank || l2aber || errest || lsame_(joba, "C"))) {
  1188. *info = -1;
  1189. } else if (! (lsvec || lsame_(jobu, "N") || lsame_(
  1190. jobu, "W") && rsvec && l2tran)) {
  1191. *info = -2;
  1192. } else if (! (rsvec || lsame_(jobv, "N") || lsame_(
  1193. jobv, "W") && lsvec && l2tran)) {
  1194. *info = -3;
  1195. } else if (! (l2kill || defr)) {
  1196. *info = -4;
  1197. } else if (! (lsame_(jobt, "T") || lsame_(jobt,
  1198. "N"))) {
  1199. *info = -5;
  1200. } else if (! (l2pert || lsame_(jobp, "N"))) {
  1201. *info = -6;
  1202. } else if (*m < 0) {
  1203. *info = -7;
  1204. } else if (*n < 0 || *n > *m) {
  1205. *info = -8;
  1206. } else if (*lda < *m) {
  1207. *info = -10;
  1208. } else if (lsvec && *ldu < *m) {
  1209. *info = -13;
  1210. } else if (rsvec && *ldv < *n) {
  1211. *info = -15;
  1212. } else {
  1213. /* #:) */
  1214. *info = 0;
  1215. }
  1216. if (*info == 0) {
  1217. /* [[The expressions for computing the minimal and the optimal */
  1218. /* values of LCWORK, LRWORK are written with a lot of redundancy and */
  1219. /* can be simplified. However, this verbose form is useful for */
  1220. /* maintenance and modifications of the code.]] */
  1221. /* ZGEQRF of an N x N matrix, ZGELQF of an N x N matrix, */
  1222. /* ZUNMLQ for computing N x N matrix, ZUNMQR for computing N x N */
  1223. /* matrix, ZUNMQR for computing M x N matrix, respectively. */
  1224. lwqp3 = *n + 1;
  1225. lwqrf = f2cmax(1,*n);
  1226. lwlqf = f2cmax(1,*n);
  1227. lwunmlq = f2cmax(1,*n);
  1228. lwunmqr = f2cmax(1,*n);
  1229. lwunmqrm = f2cmax(1,*m);
  1230. lwcon = *n << 1;
  1231. /* without and with explicit accumulation of Jacobi rotations */
  1232. /* Computing MAX */
  1233. i__1 = *n << 1;
  1234. lwsvdj = f2cmax(i__1,1);
  1235. /* Computing MAX */
  1236. i__1 = *n << 1;
  1237. lwsvdjv = f2cmax(i__1,1);
  1238. lrwqp3 = *n << 1;
  1239. lrwcon = *n;
  1240. lrwsvdj = *n;
  1241. if (lquery) {
  1242. zgeqp3_(m, n, &a[a_offset], lda, &iwork[1], cdummy, cdummy, &c_n1,
  1243. rdummy, &ierr);
  1244. lwrk_zgeqp3__ = (integer) cdummy[0].r;
  1245. zgeqrf_(n, n, &a[a_offset], lda, cdummy, cdummy, &c_n1, &ierr);
  1246. lwrk_zgeqrf__ = (integer) cdummy[0].r;
  1247. zgelqf_(n, n, &a[a_offset], lda, cdummy, cdummy, &c_n1, &ierr);
  1248. lwrk_zgelqf__ = (integer) cdummy[0].r;
  1249. }
  1250. minwrk = 2;
  1251. optwrk = 2;
  1252. miniwrk = *n;
  1253. if (! (lsvec || rsvec)) {
  1254. /* only the singular values are requested */
  1255. if (errest) {
  1256. /* Computing MAX */
  1257. /* Computing 2nd power */
  1258. i__3 = *n;
  1259. i__1 = *n + lwqp3, i__2 = i__3 * i__3 + lwcon, i__1 = f2cmax(
  1260. i__1,i__2), i__2 = *n + lwqrf, i__1 = f2cmax(i__1,i__2);
  1261. minwrk = f2cmax(i__1,lwsvdj);
  1262. } else {
  1263. /* Computing MAX */
  1264. i__1 = *n + lwqp3, i__2 = *n + lwqrf, i__1 = f2cmax(i__1,i__2);
  1265. minwrk = f2cmax(i__1,lwsvdj);
  1266. }
  1267. if (lquery) {
  1268. zgesvj_("L", "N", "N", n, n, &a[a_offset], lda, &sva[1], n, &
  1269. v[v_offset], ldv, cdummy, &c_n1, rdummy, &c_n1, &ierr);
  1270. lwrk_zgesvj__ = (integer) cdummy[0].r;
  1271. if (errest) {
  1272. /* Computing MAX */
  1273. /* Computing 2nd power */
  1274. i__3 = *n;
  1275. i__1 = *n + lwrk_zgeqp3__, i__2 = i__3 * i__3 + lwcon,
  1276. i__1 = f2cmax(i__1,i__2), i__2 = *n + lwrk_zgeqrf__,
  1277. i__1 = f2cmax(i__1,i__2);
  1278. optwrk = f2cmax(i__1,lwrk_zgesvj__);
  1279. } else {
  1280. /* Computing MAX */
  1281. i__1 = *n + lwrk_zgeqp3__, i__2 = *n + lwrk_zgeqrf__,
  1282. i__1 = f2cmax(i__1,i__2);
  1283. optwrk = f2cmax(i__1,lwrk_zgesvj__);
  1284. }
  1285. }
  1286. if (l2tran || rowpiv) {
  1287. if (errest) {
  1288. /* Computing MAX */
  1289. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1290. f2cmax(i__1,lrwqp3), i__1 = f2cmax(i__1,lrwcon);
  1291. minrwrk = f2cmax(i__1,lrwsvdj);
  1292. } else {
  1293. /* Computing MAX */
  1294. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1295. f2cmax(i__1,lrwqp3);
  1296. minrwrk = f2cmax(i__1,lrwsvdj);
  1297. }
  1298. } else {
  1299. if (errest) {
  1300. /* Computing MAX */
  1301. i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwcon);
  1302. minrwrk = f2cmax(i__1,lrwsvdj);
  1303. } else {
  1304. /* Computing MAX */
  1305. i__1 = f2cmax(7,lrwqp3);
  1306. minrwrk = f2cmax(i__1,lrwsvdj);
  1307. }
  1308. }
  1309. if (rowpiv || l2tran) {
  1310. miniwrk += *m;
  1311. }
  1312. } else if (rsvec && ! lsvec) {
  1313. /* singular values and the right singular vectors are requested */
  1314. if (errest) {
  1315. /* Computing MAX */
  1316. i__1 = *n + lwqp3, i__1 = f2cmax(i__1,lwcon), i__1 = f2cmax(i__1,
  1317. lwsvdj), i__2 = *n + lwlqf, i__1 = f2cmax(i__1,i__2),
  1318. i__2 = (*n << 1) + lwqrf, i__1 = f2cmax(i__1,i__2), i__2
  1319. = *n + lwsvdj, i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1320. lwunmlq;
  1321. minwrk = f2cmax(i__1,i__2);
  1322. } else {
  1323. /* Computing MAX */
  1324. i__1 = *n + lwqp3, i__1 = f2cmax(i__1,lwsvdj), i__2 = *n + lwlqf,
  1325. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqrf,
  1326. i__1 = f2cmax(i__1,i__2), i__2 = *n + lwsvdj, i__1 = f2cmax(
  1327. i__1,i__2), i__2 = *n + lwunmlq;
  1328. minwrk = f2cmax(i__1,i__2);
  1329. }
  1330. if (lquery) {
  1331. zgesvj_("L", "U", "N", n, n, &u[u_offset], ldu, &sva[1], n, &
  1332. a[a_offset], lda, cdummy, &c_n1, rdummy, &c_n1, &ierr);
  1333. lwrk_zgesvj__ = (integer) cdummy[0].r;
  1334. zunmlq_("L", "C", n, n, n, &a[a_offset], lda, cdummy, &v[
  1335. v_offset], ldv, cdummy, &c_n1, &ierr);
  1336. lwrk_zunmlq__ = (integer) cdummy[0].r;
  1337. if (errest) {
  1338. /* Computing MAX */
  1339. i__1 = *n + lwrk_zgeqp3__, i__1 = f2cmax(i__1,lwcon), i__1 =
  1340. f2cmax(i__1,lwrk_zgesvj__), i__2 = *n +
  1341. lwrk_zgelqf__, i__1 = f2cmax(i__1,i__2), i__2 = (*n
  1342. << 1) + lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2),
  1343. i__2 = *n + lwrk_zgesvj__, i__1 = f2cmax(i__1,i__2),
  1344. i__2 = *n + lwrk_zunmlq__;
  1345. optwrk = f2cmax(i__1,i__2);
  1346. } else {
  1347. /* Computing MAX */
  1348. i__1 = *n + lwrk_zgeqp3__, i__1 = f2cmax(i__1,lwrk_zgesvj__),
  1349. i__2 = *n + lwrk_zgelqf__, i__1 = f2cmax(i__1,i__2),
  1350. i__2 = (*n << 1) + lwrk_zgeqrf__, i__1 = f2cmax(
  1351. i__1,i__2), i__2 = *n + lwrk_zgesvj__, i__1 = f2cmax(
  1352. i__1,i__2), i__2 = *n + lwrk_zunmlq__;
  1353. optwrk = f2cmax(i__1,i__2);
  1354. }
  1355. }
  1356. if (l2tran || rowpiv) {
  1357. if (errest) {
  1358. /* Computing MAX */
  1359. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1360. f2cmax(i__1,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1361. minrwrk = f2cmax(i__1,lrwcon);
  1362. } else {
  1363. /* Computing MAX */
  1364. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1365. f2cmax(i__1,lrwqp3);
  1366. minrwrk = f2cmax(i__1,lrwsvdj);
  1367. }
  1368. } else {
  1369. if (errest) {
  1370. /* Computing MAX */
  1371. i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1372. minrwrk = f2cmax(i__1,lrwcon);
  1373. } else {
  1374. /* Computing MAX */
  1375. i__1 = f2cmax(7,lrwqp3);
  1376. minrwrk = f2cmax(i__1,lrwsvdj);
  1377. }
  1378. }
  1379. if (rowpiv || l2tran) {
  1380. miniwrk += *m;
  1381. }
  1382. } else if (lsvec && ! rsvec) {
  1383. /* singular values and the left singular vectors are requested */
  1384. if (errest) {
  1385. /* Computing MAX */
  1386. i__1 = f2cmax(lwqp3,lwcon), i__2 = *n + lwqrf, i__1 = f2cmax(i__1,
  1387. i__2), i__1 = f2cmax(i__1,lwsvdj);
  1388. minwrk = *n + f2cmax(i__1,lwunmqrm);
  1389. } else {
  1390. /* Computing MAX */
  1391. i__1 = lwqp3, i__2 = *n + lwqrf, i__1 = f2cmax(i__1,i__2), i__1 =
  1392. f2cmax(i__1,lwsvdj);
  1393. minwrk = *n + f2cmax(i__1,lwunmqrm);
  1394. }
  1395. if (lquery) {
  1396. zgesvj_("L", "U", "N", n, n, &u[u_offset], ldu, &sva[1], n, &
  1397. a[a_offset], lda, cdummy, &c_n1, rdummy, &c_n1, &ierr);
  1398. lwrk_zgesvj__ = (integer) cdummy[0].r;
  1399. zunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
  1400. u_offset], ldu, cdummy, &c_n1, &ierr);
  1401. lwrk_zunmqrm__ = (integer) cdummy[0].r;
  1402. if (errest) {
  1403. /* Computing MAX */
  1404. i__1 = f2cmax(lwrk_zgeqp3__,lwcon), i__2 = *n +
  1405. lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2), i__1 = f2cmax(
  1406. i__1,lwrk_zgesvj__);
  1407. optwrk = *n + f2cmax(i__1,lwrk_zunmqrm__);
  1408. } else {
  1409. /* Computing MAX */
  1410. i__1 = lwrk_zgeqp3__, i__2 = *n + lwrk_zgeqrf__, i__1 =
  1411. f2cmax(i__1,i__2), i__1 = f2cmax(i__1,lwrk_zgesvj__);
  1412. optwrk = *n + f2cmax(i__1,lwrk_zunmqrm__);
  1413. }
  1414. }
  1415. if (l2tran || rowpiv) {
  1416. if (errest) {
  1417. /* Computing MAX */
  1418. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1419. f2cmax(i__1,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1420. minrwrk = f2cmax(i__1,lrwcon);
  1421. } else {
  1422. /* Computing MAX */
  1423. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1424. f2cmax(i__1,lrwqp3);
  1425. minrwrk = f2cmax(i__1,lrwsvdj);
  1426. }
  1427. } else {
  1428. if (errest) {
  1429. /* Computing MAX */
  1430. i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1431. minrwrk = f2cmax(i__1,lrwcon);
  1432. } else {
  1433. /* Computing MAX */
  1434. i__1 = f2cmax(7,lrwqp3);
  1435. minrwrk = f2cmax(i__1,lrwsvdj);
  1436. }
  1437. }
  1438. if (rowpiv || l2tran) {
  1439. miniwrk += *m;
  1440. }
  1441. } else {
  1442. /* full SVD is requested */
  1443. if (! jracc) {
  1444. if (errest) {
  1445. /* Computing MAX */
  1446. /* Computing 2nd power */
  1447. i__3 = *n;
  1448. /* Computing 2nd power */
  1449. i__4 = *n;
  1450. /* Computing 2nd power */
  1451. i__5 = *n;
  1452. /* Computing 2nd power */
  1453. i__6 = *n;
  1454. /* Computing 2nd power */
  1455. i__7 = *n;
  1456. /* Computing 2nd power */
  1457. i__8 = *n;
  1458. /* Computing 2nd power */
  1459. i__9 = *n;
  1460. /* Computing 2nd power */
  1461. i__10 = *n;
  1462. /* Computing 2nd power */
  1463. i__11 = *n;
  1464. i__1 = *n + lwqp3, i__2 = *n + lwcon, i__1 = f2cmax(i__1,
  1465. i__2), i__2 = (*n << 1) + i__3 * i__3 + lwcon,
  1466. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqrf,
  1467. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqp3,
  1468. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1469. i__4 + *n + lwlqf, i__1 = f2cmax(i__1,i__2), i__2 = (
  1470. *n << 1) + i__5 * i__5 + *n + i__6 * i__6 + lwcon,
  1471. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__7 *
  1472. i__7 + *n + lwsvdj, i__1 = f2cmax(i__1,i__2), i__2 =
  1473. (*n << 1) + i__8 * i__8 + *n + lwsvdjv, i__1 =
  1474. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__9 * i__9 + *
  1475. n + lwunmqr, i__1 = f2cmax(i__1,i__2), i__2 = (*n <<
  1476. 1) + i__10 * i__10 + *n + lwunmlq, i__1 = f2cmax(
  1477. i__1,i__2), i__2 = *n + i__11 * i__11 + lwsvdj,
  1478. i__1 = f2cmax(i__1,i__2), i__2 = *n + lwunmqrm;
  1479. minwrk = f2cmax(i__1,i__2);
  1480. } else {
  1481. /* Computing MAX */
  1482. /* Computing 2nd power */
  1483. i__3 = *n;
  1484. /* Computing 2nd power */
  1485. i__4 = *n;
  1486. /* Computing 2nd power */
  1487. i__5 = *n;
  1488. /* Computing 2nd power */
  1489. i__6 = *n;
  1490. /* Computing 2nd power */
  1491. i__7 = *n;
  1492. /* Computing 2nd power */
  1493. i__8 = *n;
  1494. /* Computing 2nd power */
  1495. i__9 = *n;
  1496. /* Computing 2nd power */
  1497. i__10 = *n;
  1498. /* Computing 2nd power */
  1499. i__11 = *n;
  1500. i__1 = *n + lwqp3, i__2 = (*n << 1) + i__3 * i__3 + lwcon,
  1501. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqrf,
  1502. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqp3,
  1503. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1504. i__4 + *n + lwlqf, i__1 = f2cmax(i__1,i__2), i__2 = (
  1505. *n << 1) + i__5 * i__5 + *n + i__6 * i__6 + lwcon,
  1506. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__7 *
  1507. i__7 + *n + lwsvdj, i__1 = f2cmax(i__1,i__2), i__2 =
  1508. (*n << 1) + i__8 * i__8 + *n + lwsvdjv, i__1 =
  1509. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__9 * i__9 + *
  1510. n + lwunmqr, i__1 = f2cmax(i__1,i__2), i__2 = (*n <<
  1511. 1) + i__10 * i__10 + *n + lwunmlq, i__1 = f2cmax(
  1512. i__1,i__2), i__2 = *n + i__11 * i__11 + lwsvdj,
  1513. i__1 = f2cmax(i__1,i__2), i__2 = *n + lwunmqrm;
  1514. minwrk = f2cmax(i__1,i__2);
  1515. }
  1516. miniwrk += *n;
  1517. if (rowpiv || l2tran) {
  1518. miniwrk += *m;
  1519. }
  1520. } else {
  1521. if (errest) {
  1522. /* Computing MAX */
  1523. /* Computing 2nd power */
  1524. i__3 = *n;
  1525. /* Computing 2nd power */
  1526. i__4 = *n;
  1527. i__1 = *n + lwqp3, i__2 = *n + lwcon, i__1 = f2cmax(i__1,
  1528. i__2), i__2 = (*n << 1) + lwqrf, i__1 = f2cmax(i__1,
  1529. i__2), i__2 = (*n << 1) + i__3 * i__3 + lwsvdjv,
  1530. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1531. i__4 + *n + lwunmqr, i__1 = f2cmax(i__1,i__2), i__2 =
  1532. *n + lwunmqrm;
  1533. minwrk = f2cmax(i__1,i__2);
  1534. } else {
  1535. /* Computing MAX */
  1536. /* Computing 2nd power */
  1537. i__3 = *n;
  1538. /* Computing 2nd power */
  1539. i__4 = *n;
  1540. i__1 = *n + lwqp3, i__2 = (*n << 1) + lwqrf, i__1 = f2cmax(
  1541. i__1,i__2), i__2 = (*n << 1) + i__3 * i__3 +
  1542. lwsvdjv, i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1)
  1543. + i__4 * i__4 + *n + lwunmqr, i__1 = f2cmax(i__1,
  1544. i__2), i__2 = *n + lwunmqrm;
  1545. minwrk = f2cmax(i__1,i__2);
  1546. }
  1547. if (rowpiv || l2tran) {
  1548. miniwrk += *m;
  1549. }
  1550. }
  1551. if (lquery) {
  1552. zunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
  1553. u_offset], ldu, cdummy, &c_n1, &ierr);
  1554. lwrk_zunmqrm__ = (integer) cdummy[0].r;
  1555. zunmqr_("L", "N", n, n, n, &a[a_offset], lda, cdummy, &u[
  1556. u_offset], ldu, cdummy, &c_n1, &ierr);
  1557. lwrk_zunmqr__ = (integer) cdummy[0].r;
  1558. if (! jracc) {
  1559. zgeqp3_(n, n, &a[a_offset], lda, &iwork[1], cdummy,
  1560. cdummy, &c_n1, rdummy, &ierr);
  1561. lwrk_zgeqp3n__ = (integer) cdummy[0].r;
  1562. zgesvj_("L", "U", "N", n, n, &u[u_offset], ldu, &sva[1],
  1563. n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
  1564. c_n1, &ierr);
  1565. lwrk_zgesvj__ = (integer) cdummy[0].r;
  1566. zgesvj_("U", "U", "N", n, n, &u[u_offset], ldu, &sva[1],
  1567. n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
  1568. c_n1, &ierr);
  1569. lwrk_zgesvju__ = (integer) cdummy[0].r;
  1570. zgesvj_("L", "U", "V", n, n, &u[u_offset], ldu, &sva[1],
  1571. n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
  1572. c_n1, &ierr);
  1573. lwrk_zgesvjv__ = (integer) cdummy[0].r;
  1574. zunmlq_("L", "C", n, n, n, &a[a_offset], lda, cdummy, &v[
  1575. v_offset], ldv, cdummy, &c_n1, &ierr);
  1576. lwrk_zunmlq__ = (integer) cdummy[0].r;
  1577. if (errest) {
  1578. /* Computing MAX */
  1579. /* Computing 2nd power */
  1580. i__3 = *n;
  1581. /* Computing 2nd power */
  1582. i__4 = *n;
  1583. /* Computing 2nd power */
  1584. i__5 = *n;
  1585. /* Computing 2nd power */
  1586. i__6 = *n;
  1587. /* Computing 2nd power */
  1588. i__7 = *n;
  1589. /* Computing 2nd power */
  1590. i__8 = *n;
  1591. /* Computing 2nd power */
  1592. i__9 = *n;
  1593. /* Computing 2nd power */
  1594. i__10 = *n;
  1595. /* Computing 2nd power */
  1596. i__11 = *n;
  1597. i__1 = *n + lwrk_zgeqp3__, i__2 = *n + lwcon, i__1 =
  1598. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__3 *
  1599. i__3 + lwcon, i__1 = f2cmax(i__1,i__2), i__2 = (*
  1600. n << 1) + lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2)
  1601. , i__2 = (*n << 1) + lwrk_zgeqp3n__, i__1 =
  1602. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1603. i__4 + *n + lwrk_zgelqf__, i__1 = f2cmax(i__1,
  1604. i__2), i__2 = (*n << 1) + i__5 * i__5 + *n +
  1605. i__6 * i__6 + lwcon, i__1 = f2cmax(i__1,i__2),
  1606. i__2 = (*n << 1) + i__7 * i__7 + *n +
  1607. lwrk_zgesvj__, i__1 = f2cmax(i__1,i__2), i__2 = (
  1608. *n << 1) + i__8 * i__8 + *n + lwrk_zgesvjv__,
  1609. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) +
  1610. i__9 * i__9 + *n + lwrk_zunmqr__, i__1 = f2cmax(
  1611. i__1,i__2), i__2 = (*n << 1) + i__10 * i__10
  1612. + *n + lwrk_zunmlq__, i__1 = f2cmax(i__1,i__2),
  1613. i__2 = *n + i__11 * i__11 + lwrk_zgesvju__,
  1614. i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1615. lwrk_zunmqrm__;
  1616. optwrk = f2cmax(i__1,i__2);
  1617. } else {
  1618. /* Computing MAX */
  1619. /* Computing 2nd power */
  1620. i__3 = *n;
  1621. /* Computing 2nd power */
  1622. i__4 = *n;
  1623. /* Computing 2nd power */
  1624. i__5 = *n;
  1625. /* Computing 2nd power */
  1626. i__6 = *n;
  1627. /* Computing 2nd power */
  1628. i__7 = *n;
  1629. /* Computing 2nd power */
  1630. i__8 = *n;
  1631. /* Computing 2nd power */
  1632. i__9 = *n;
  1633. /* Computing 2nd power */
  1634. i__10 = *n;
  1635. /* Computing 2nd power */
  1636. i__11 = *n;
  1637. i__1 = *n + lwrk_zgeqp3__, i__2 = (*n << 1) + i__3 *
  1638. i__3 + lwcon, i__1 = f2cmax(i__1,i__2), i__2 = (*
  1639. n << 1) + lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2)
  1640. , i__2 = (*n << 1) + lwrk_zgeqp3n__, i__1 =
  1641. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1642. i__4 + *n + lwrk_zgelqf__, i__1 = f2cmax(i__1,
  1643. i__2), i__2 = (*n << 1) + i__5 * i__5 + *n +
  1644. i__6 * i__6 + lwcon, i__1 = f2cmax(i__1,i__2),
  1645. i__2 = (*n << 1) + i__7 * i__7 + *n +
  1646. lwrk_zgesvj__, i__1 = f2cmax(i__1,i__2), i__2 = (
  1647. *n << 1) + i__8 * i__8 + *n + lwrk_zgesvjv__,
  1648. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) +
  1649. i__9 * i__9 + *n + lwrk_zunmqr__, i__1 = f2cmax(
  1650. i__1,i__2), i__2 = (*n << 1) + i__10 * i__10
  1651. + *n + lwrk_zunmlq__, i__1 = f2cmax(i__1,i__2),
  1652. i__2 = *n + i__11 * i__11 + lwrk_zgesvju__,
  1653. i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1654. lwrk_zunmqrm__;
  1655. optwrk = f2cmax(i__1,i__2);
  1656. }
  1657. } else {
  1658. zgesvj_("L", "U", "V", n, n, &u[u_offset], ldu, &sva[1],
  1659. n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
  1660. c_n1, &ierr);
  1661. lwrk_zgesvjv__ = (integer) cdummy[0].r;
  1662. zunmqr_("L", "N", n, n, n, cdummy, n, cdummy, &v[v_offset]
  1663. , ldv, cdummy, &c_n1, &ierr)
  1664. ;
  1665. lwrk_zunmqr__ = (integer) cdummy[0].r;
  1666. zunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
  1667. u_offset], ldu, cdummy, &c_n1, &ierr);
  1668. lwrk_zunmqrm__ = (integer) cdummy[0].r;
  1669. if (errest) {
  1670. /* Computing MAX */
  1671. /* Computing 2nd power */
  1672. i__3 = *n;
  1673. /* Computing 2nd power */
  1674. i__4 = *n;
  1675. /* Computing 2nd power */
  1676. i__5 = *n;
  1677. i__1 = *n + lwrk_zgeqp3__, i__2 = *n + lwcon, i__1 =
  1678. f2cmax(i__1,i__2), i__2 = (*n << 1) +
  1679. lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2), i__2 = (
  1680. *n << 1) + i__3 * i__3, i__1 = f2cmax(i__1,i__2),
  1681. i__2 = (*n << 1) + i__4 * i__4 +
  1682. lwrk_zgesvjv__, i__1 = f2cmax(i__1,i__2), i__2 =
  1683. (*n << 1) + i__5 * i__5 + *n + lwrk_zunmqr__,
  1684. i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1685. lwrk_zunmqrm__;
  1686. optwrk = f2cmax(i__1,i__2);
  1687. } else {
  1688. /* Computing MAX */
  1689. /* Computing 2nd power */
  1690. i__3 = *n;
  1691. /* Computing 2nd power */
  1692. i__4 = *n;
  1693. /* Computing 2nd power */
  1694. i__5 = *n;
  1695. i__1 = *n + lwrk_zgeqp3__, i__2 = (*n << 1) +
  1696. lwrk_zgeqrf__, i__1 = f2cmax(i__1,i__2), i__2 = (
  1697. *n << 1) + i__3 * i__3, i__1 = f2cmax(i__1,i__2),
  1698. i__2 = (*n << 1) + i__4 * i__4 +
  1699. lwrk_zgesvjv__, i__1 = f2cmax(i__1,i__2), i__2 =
  1700. (*n << 1) + i__5 * i__5 + *n + lwrk_zunmqr__,
  1701. i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1702. lwrk_zunmqrm__;
  1703. optwrk = f2cmax(i__1,i__2);
  1704. }
  1705. }
  1706. }
  1707. if (l2tran || rowpiv) {
  1708. /* Computing MAX */
  1709. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 = f2cmax(
  1710. i__1,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1711. minrwrk = f2cmax(i__1,lrwcon);
  1712. } else {
  1713. /* Computing MAX */
  1714. i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1715. minrwrk = f2cmax(i__1,lrwcon);
  1716. }
  1717. }
  1718. minwrk = f2cmax(2,minwrk);
  1719. optwrk = f2cmax(minwrk,optwrk);
  1720. if (*lwork < minwrk && ! lquery) {
  1721. *info = -17;
  1722. }
  1723. if (*lrwork < minrwrk && ! lquery) {
  1724. *info = -19;
  1725. }
  1726. }
  1727. if (*info != 0) {
  1728. /* #:( */
  1729. i__1 = -(*info);
  1730. xerbla_("ZGEJSV", &i__1, (ftnlen)6);
  1731. return;
  1732. } else if (lquery) {
  1733. cwork[1].r = (doublereal) optwrk, cwork[1].i = 0.;
  1734. cwork[2].r = (doublereal) minwrk, cwork[2].i = 0.;
  1735. rwork[1] = (doublereal) minrwrk;
  1736. iwork[1] = f2cmax(4,miniwrk);
  1737. return;
  1738. }
  1739. /* Quick return for void matrix (Y3K safe) */
  1740. /* #:) */
  1741. if (*m == 0 || *n == 0) {
  1742. iwork[1] = 0;
  1743. iwork[2] = 0;
  1744. iwork[3] = 0;
  1745. iwork[4] = 0;
  1746. rwork[1] = 0.;
  1747. rwork[2] = 0.;
  1748. rwork[3] = 0.;
  1749. rwork[4] = 0.;
  1750. rwork[5] = 0.;
  1751. rwork[6] = 0.;
  1752. rwork[7] = 0.;
  1753. return;
  1754. }
  1755. /* Determine whether the matrix U should be M x N or M x M */
  1756. if (lsvec) {
  1757. n1 = *n;
  1758. if (lsame_(jobu, "F")) {
  1759. n1 = *m;
  1760. }
  1761. }
  1762. /* Set numerical parameters */
  1763. /* ! NOTE: Make sure DLAMCH() does not fail on the target architecture. */
  1764. epsln = dlamch_("Epsilon");
  1765. sfmin = dlamch_("SafeMinimum");
  1766. small = sfmin / epsln;
  1767. big = dlamch_("O");
  1768. /* BIG = ONE / SFMIN */
  1769. /* Initialize SVA(1:N) = diag( ||A e_i||_2 )_1^N */
  1770. /* (!) If necessary, scale SVA() to protect the largest norm from */
  1771. /* overflow. It is possible that this scaling pushes the smallest */
  1772. /* column norm left from the underflow threshold (extreme case). */
  1773. scalem = 1. / sqrt((doublereal) (*m) * (doublereal) (*n));
  1774. noscal = TRUE_;
  1775. goscal = TRUE_;
  1776. i__1 = *n;
  1777. for (p = 1; p <= i__1; ++p) {
  1778. aapp = 0.;
  1779. aaqq = 1.;
  1780. zlassq_(m, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
  1781. if (aapp > big) {
  1782. *info = -9;
  1783. i__2 = -(*info);
  1784. xerbla_("ZGEJSV", &i__2, (ftnlen)6);
  1785. return;
  1786. }
  1787. aaqq = sqrt(aaqq);
  1788. if (aapp < big / aaqq && noscal) {
  1789. sva[p] = aapp * aaqq;
  1790. } else {
  1791. noscal = FALSE_;
  1792. sva[p] = aapp * (aaqq * scalem);
  1793. if (goscal) {
  1794. goscal = FALSE_;
  1795. i__2 = p - 1;
  1796. dscal_(&i__2, &scalem, &sva[1], &c__1);
  1797. }
  1798. }
  1799. /* L1874: */
  1800. }
  1801. if (noscal) {
  1802. scalem = 1.;
  1803. }
  1804. aapp = 0.;
  1805. aaqq = big;
  1806. i__1 = *n;
  1807. for (p = 1; p <= i__1; ++p) {
  1808. /* Computing MAX */
  1809. d__1 = aapp, d__2 = sva[p];
  1810. aapp = f2cmax(d__1,d__2);
  1811. if (sva[p] != 0.) {
  1812. /* Computing MIN */
  1813. d__1 = aaqq, d__2 = sva[p];
  1814. aaqq = f2cmin(d__1,d__2);
  1815. }
  1816. /* L4781: */
  1817. }
  1818. /* Quick return for zero M x N matrix */
  1819. /* #:) */
  1820. if (aapp == 0.) {
  1821. if (lsvec) {
  1822. zlaset_("G", m, &n1, &c_b1, &c_b2, &u[u_offset], ldu);
  1823. }
  1824. if (rsvec) {
  1825. zlaset_("G", n, n, &c_b1, &c_b2, &v[v_offset], ldv);
  1826. }
  1827. rwork[1] = 1.;
  1828. rwork[2] = 1.;
  1829. if (errest) {
  1830. rwork[3] = 1.;
  1831. }
  1832. if (lsvec && rsvec) {
  1833. rwork[4] = 1.;
  1834. rwork[5] = 1.;
  1835. }
  1836. if (l2tran) {
  1837. rwork[6] = 0.;
  1838. rwork[7] = 0.;
  1839. }
  1840. iwork[1] = 0;
  1841. iwork[2] = 0;
  1842. iwork[3] = 0;
  1843. iwork[4] = -1;
  1844. return;
  1845. }
  1846. /* Issue warning if denormalized column norms detected. Override the */
  1847. /* high relative accuracy request. Issue licence to kill nonzero columns */
  1848. /* (set them to zero) whose norm is less than sigma_max / BIG (roughly). */
  1849. /* #:( */
  1850. warning = 0;
  1851. if (aaqq <= sfmin) {
  1852. l2rank = TRUE_;
  1853. l2kill = TRUE_;
  1854. warning = 1;
  1855. }
  1856. /* Quick return for one-column matrix */
  1857. /* #:) */
  1858. if (*n == 1) {
  1859. if (lsvec) {
  1860. zlascl_("G", &c__0, &c__0, &sva[1], &scalem, m, &c__1, &a[a_dim1
  1861. + 1], lda, &ierr);
  1862. zlacpy_("A", m, &c__1, &a[a_offset], lda, &u[u_offset], ldu);
  1863. /* computing all M left singular vectors of the M x 1 matrix */
  1864. if (n1 != *n) {
  1865. i__1 = *lwork - *n;
  1866. zgeqrf_(m, n, &u[u_offset], ldu, &cwork[1], &cwork[*n + 1], &
  1867. i__1, &ierr);
  1868. i__1 = *lwork - *n;
  1869. zungqr_(m, &n1, &c__1, &u[u_offset], ldu, &cwork[1], &cwork[*
  1870. n + 1], &i__1, &ierr);
  1871. zcopy_(m, &a[a_dim1 + 1], &c__1, &u[u_dim1 + 1], &c__1);
  1872. }
  1873. }
  1874. if (rsvec) {
  1875. i__1 = v_dim1 + 1;
  1876. v[i__1].r = 1., v[i__1].i = 0.;
  1877. }
  1878. if (sva[1] < big * scalem) {
  1879. sva[1] /= scalem;
  1880. scalem = 1.;
  1881. }
  1882. rwork[1] = 1. / scalem;
  1883. rwork[2] = 1.;
  1884. if (sva[1] != 0.) {
  1885. iwork[1] = 1;
  1886. if (sva[1] / scalem >= sfmin) {
  1887. iwork[2] = 1;
  1888. } else {
  1889. iwork[2] = 0;
  1890. }
  1891. } else {
  1892. iwork[1] = 0;
  1893. iwork[2] = 0;
  1894. }
  1895. iwork[3] = 0;
  1896. iwork[4] = -1;
  1897. if (errest) {
  1898. rwork[3] = 1.;
  1899. }
  1900. if (lsvec && rsvec) {
  1901. rwork[4] = 1.;
  1902. rwork[5] = 1.;
  1903. }
  1904. if (l2tran) {
  1905. rwork[6] = 0.;
  1906. rwork[7] = 0.;
  1907. }
  1908. return;
  1909. }
  1910. transp = FALSE_;
  1911. aatmax = -1.;
  1912. aatmin = big;
  1913. if (rowpiv || l2tran) {
  1914. /* Compute the row norms, needed to determine row pivoting sequence */
  1915. /* (in the case of heavily row weighted A, row pivoting is strongly */
  1916. /* advised) and to collect information needed to compare the */
  1917. /* structures of A * A^* and A^* * A (in the case L2TRAN.EQ..TRUE.). */
  1918. if (l2tran) {
  1919. i__1 = *m;
  1920. for (p = 1; p <= i__1; ++p) {
  1921. xsc = 0.;
  1922. temp1 = 1.;
  1923. zlassq_(n, &a[p + a_dim1], lda, &xsc, &temp1);
  1924. /* ZLASSQ gets both the ell_2 and the ell_infinity norm */
  1925. /* in one pass through the vector */
  1926. rwork[*m + p] = xsc * scalem;
  1927. rwork[p] = xsc * (scalem * sqrt(temp1));
  1928. /* Computing MAX */
  1929. d__1 = aatmax, d__2 = rwork[p];
  1930. aatmax = f2cmax(d__1,d__2);
  1931. if (rwork[p] != 0.) {
  1932. /* Computing MIN */
  1933. d__1 = aatmin, d__2 = rwork[p];
  1934. aatmin = f2cmin(d__1,d__2);
  1935. }
  1936. /* L1950: */
  1937. }
  1938. } else {
  1939. i__1 = *m;
  1940. for (p = 1; p <= i__1; ++p) {
  1941. rwork[*m + p] = scalem * z_abs(&a[p + izamax_(n, &a[p +
  1942. a_dim1], lda) * a_dim1]);
  1943. /* Computing MAX */
  1944. d__1 = aatmax, d__2 = rwork[*m + p];
  1945. aatmax = f2cmax(d__1,d__2);
  1946. /* Computing MIN */
  1947. d__1 = aatmin, d__2 = rwork[*m + p];
  1948. aatmin = f2cmin(d__1,d__2);
  1949. /* L1904: */
  1950. }
  1951. }
  1952. }
  1953. /* For square matrix A try to determine whether A^* would be better */
  1954. /* input for the preconditioned Jacobi SVD, with faster convergence. */
  1955. /* The decision is based on an O(N) function of the vector of column */
  1956. /* and row norms of A, based on the Shannon entropy. This should give */
  1957. /* the right choice in most cases when the difference actually matters. */
  1958. /* It may fail and pick the slower converging side. */
  1959. entra = 0.;
  1960. entrat = 0.;
  1961. if (l2tran) {
  1962. xsc = 0.;
  1963. temp1 = 1.;
  1964. dlassq_(n, &sva[1], &c__1, &xsc, &temp1);
  1965. temp1 = 1. / temp1;
  1966. entra = 0.;
  1967. i__1 = *n;
  1968. for (p = 1; p <= i__1; ++p) {
  1969. /* Computing 2nd power */
  1970. d__1 = sva[p] / xsc;
  1971. big1 = d__1 * d__1 * temp1;
  1972. if (big1 != 0.) {
  1973. entra += big1 * log(big1);
  1974. }
  1975. /* L1113: */
  1976. }
  1977. entra = -entra / log((doublereal) (*n));
  1978. /* Now, SVA().^2/Trace(A^* * A) is a point in the probability simplex. */
  1979. /* It is derived from the diagonal of A^* * A. Do the same with the */
  1980. /* diagonal of A * A^*, compute the entropy of the corresponding */
  1981. /* probability distribution. Note that A * A^* and A^* * A have the */
  1982. /* same trace. */
  1983. entrat = 0.;
  1984. i__1 = *m;
  1985. for (p = 1; p <= i__1; ++p) {
  1986. /* Computing 2nd power */
  1987. d__1 = rwork[p] / xsc;
  1988. big1 = d__1 * d__1 * temp1;
  1989. if (big1 != 0.) {
  1990. entrat += big1 * log(big1);
  1991. }
  1992. /* L1114: */
  1993. }
  1994. entrat = -entrat / log((doublereal) (*m));
  1995. /* Analyze the entropies and decide A or A^*. Smaller entropy */
  1996. /* usually means better input for the algorithm. */
  1997. transp = entrat < entra;
  1998. /* If A^* is better than A, take the adjoint of A. This is allowed */
  1999. /* only for square matrices, M=N. */
  2000. if (transp) {
  2001. /* In an optimal implementation, this trivial transpose */
  2002. /* should be replaced with faster transpose. */
  2003. i__1 = *n - 1;
  2004. for (p = 1; p <= i__1; ++p) {
  2005. i__2 = p + p * a_dim1;
  2006. d_cnjg(&z__1, &a[p + p * a_dim1]);
  2007. a[i__2].r = z__1.r, a[i__2].i = z__1.i;
  2008. i__2 = *n;
  2009. for (q = p + 1; q <= i__2; ++q) {
  2010. d_cnjg(&z__1, &a[q + p * a_dim1]);
  2011. ctemp.r = z__1.r, ctemp.i = z__1.i;
  2012. i__3 = q + p * a_dim1;
  2013. d_cnjg(&z__1, &a[p + q * a_dim1]);
  2014. a[i__3].r = z__1.r, a[i__3].i = z__1.i;
  2015. i__3 = p + q * a_dim1;
  2016. a[i__3].r = ctemp.r, a[i__3].i = ctemp.i;
  2017. /* L1116: */
  2018. }
  2019. /* L1115: */
  2020. }
  2021. i__1 = *n + *n * a_dim1;
  2022. d_cnjg(&z__1, &a[*n + *n * a_dim1]);
  2023. a[i__1].r = z__1.r, a[i__1].i = z__1.i;
  2024. i__1 = *n;
  2025. for (p = 1; p <= i__1; ++p) {
  2026. rwork[*m + p] = sva[p];
  2027. sva[p] = rwork[p];
  2028. /* previously computed row 2-norms are now column 2-norms */
  2029. /* of the transposed matrix */
  2030. /* L1117: */
  2031. }
  2032. temp1 = aapp;
  2033. aapp = aatmax;
  2034. aatmax = temp1;
  2035. temp1 = aaqq;
  2036. aaqq = aatmin;
  2037. aatmin = temp1;
  2038. kill = lsvec;
  2039. lsvec = rsvec;
  2040. rsvec = kill;
  2041. if (lsvec) {
  2042. n1 = *n;
  2043. }
  2044. rowpiv = TRUE_;
  2045. }
  2046. }
  2047. /* END IF L2TRAN */
  2048. /* Scale the matrix so that its maximal singular value remains less */
  2049. /* than SQRT(BIG) -- the matrix is scaled so that its maximal column */
  2050. /* has Euclidean norm equal to SQRT(BIG/N). The only reason to keep */
  2051. /* SQRT(BIG) instead of BIG is the fact that ZGEJSV uses LAPACK and */
  2052. /* BLAS routines that, in some implementations, are not capable of */
  2053. /* working in the full interval [SFMIN,BIG] and that they may provoke */
  2054. /* overflows in the intermediate results. If the singular values spread */
  2055. /* from SFMIN to BIG, then ZGESVJ will compute them. So, in that case, */
  2056. /* one should use ZGESVJ instead of ZGEJSV. */
  2057. /* >> change in the April 2016 update: allow bigger range, i.e. the */
  2058. /* largest column is allowed up to BIG/N and ZGESVJ will do the rest. */
  2059. big1 = sqrt(big);
  2060. temp1 = sqrt(big / (doublereal) (*n));
  2061. /* TEMP1 = BIG/DBLE(N) */
  2062. dlascl_("G", &c__0, &c__0, &aapp, &temp1, n, &c__1, &sva[1], n, &ierr);
  2063. if (aaqq > aapp * sfmin) {
  2064. aaqq = aaqq / aapp * temp1;
  2065. } else {
  2066. aaqq = aaqq * temp1 / aapp;
  2067. }
  2068. temp1 *= scalem;
  2069. zlascl_("G", &c__0, &c__0, &aapp, &temp1, m, n, &a[a_offset], lda, &ierr);
  2070. /* To undo scaling at the end of this procedure, multiply the */
  2071. /* computed singular values with USCAL2 / USCAL1. */
  2072. uscal1 = temp1;
  2073. uscal2 = aapp;
  2074. if (l2kill) {
  2075. /* L2KILL enforces computation of nonzero singular values in */
  2076. /* the restricted range of condition number of the initial A, */
  2077. /* sigma_max(A) / sigma_min(A) approx. SQRT(BIG)/SQRT(SFMIN). */
  2078. xsc = sqrt(sfmin);
  2079. } else {
  2080. xsc = small;
  2081. /* Now, if the condition number of A is too big, */
  2082. /* sigma_max(A) / sigma_min(A) .GT. SQRT(BIG/N) * EPSLN / SFMIN, */
  2083. /* as a precaution measure, the full SVD is computed using ZGESVJ */
  2084. /* with accumulated Jacobi rotations. This provides numerically */
  2085. /* more robust computation, at the cost of slightly increased run */
  2086. /* time. Depending on the concrete implementation of BLAS and LAPACK */
  2087. /* (i.e. how they behave in presence of extreme ill-conditioning) the */
  2088. /* implementor may decide to remove this switch. */
  2089. if (aaqq < sqrt(sfmin) && lsvec && rsvec) {
  2090. jracc = TRUE_;
  2091. }
  2092. }
  2093. if (aaqq < xsc) {
  2094. i__1 = *n;
  2095. for (p = 1; p <= i__1; ++p) {
  2096. if (sva[p] < xsc) {
  2097. zlaset_("A", m, &c__1, &c_b1, &c_b1, &a[p * a_dim1 + 1], lda);
  2098. sva[p] = 0.;
  2099. }
  2100. /* L700: */
  2101. }
  2102. }
  2103. /* Preconditioning using QR factorization with pivoting */
  2104. if (rowpiv) {
  2105. /* Optional row permutation (Bjoerck row pivoting): */
  2106. /* A result by Cox and Higham shows that the Bjoerck's */
  2107. /* row pivoting combined with standard column pivoting */
  2108. /* has similar effect as Powell-Reid complete pivoting. */
  2109. /* The ell-infinity norms of A are made nonincreasing. */
  2110. if (lsvec && rsvec && ! jracc) {
  2111. iwoff = *n << 1;
  2112. } else {
  2113. iwoff = *n;
  2114. }
  2115. i__1 = *m - 1;
  2116. for (p = 1; p <= i__1; ++p) {
  2117. i__2 = *m - p + 1;
  2118. q = idamax_(&i__2, &rwork[*m + p], &c__1) + p - 1;
  2119. iwork[iwoff + p] = q;
  2120. if (p != q) {
  2121. temp1 = rwork[*m + p];
  2122. rwork[*m + p] = rwork[*m + q];
  2123. rwork[*m + q] = temp1;
  2124. }
  2125. /* L1952: */
  2126. }
  2127. i__1 = *m - 1;
  2128. zlaswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[iwoff + 1], &c__1);
  2129. }
  2130. /* End of the preparation phase (scaling, optional sorting and */
  2131. /* transposing, optional flushing of small columns). */
  2132. /* Preconditioning */
  2133. /* If the full SVD is needed, the right singular vectors are computed */
  2134. /* from a matrix equation, and for that we need theoretical analysis */
  2135. /* of the Businger-Golub pivoting. So we use ZGEQP3 as the first RR QRF. */
  2136. /* In all other cases the first RR QRF can be chosen by other criteria */
  2137. /* (eg speed by replacing global with restricted window pivoting, such */
  2138. /* as in xGEQPX from TOMS # 782). Good results will be obtained using */
  2139. /* xGEQPX with properly (!) chosen numerical parameters. */
  2140. /* Any improvement of ZGEQP3 improves overal performance of ZGEJSV. */
  2141. /* A * P1 = Q1 * [ R1^* 0]^*: */
  2142. i__1 = *n;
  2143. for (p = 1; p <= i__1; ++p) {
  2144. iwork[p] = 0;
  2145. /* L1963: */
  2146. }
  2147. i__1 = *lwork - *n;
  2148. zgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &cwork[1], &cwork[*n + 1], &
  2149. i__1, &rwork[1], &ierr);
  2150. /* The upper triangular matrix R1 from the first QRF is inspected for */
  2151. /* rank deficiency and possibilities for deflation, or possible */
  2152. /* ill-conditioning. Depending on the user specified flag L2RANK, */
  2153. /* the procedure explores possibilities to reduce the numerical */
  2154. /* rank by inspecting the computed upper triangular factor. If */
  2155. /* L2RANK or L2ABER are up, then ZGEJSV will compute the SVD of */
  2156. /* A + dA, where ||dA|| <= f(M,N)*EPSLN. */
  2157. nr = 1;
  2158. if (l2aber) {
  2159. /* Standard absolute error bound suffices. All sigma_i with */
  2160. /* sigma_i < N*EPSLN*||A|| are flushed to zero. This is an */
  2161. /* aggressive enforcement of lower numerical rank by introducing a */
  2162. /* backward error of the order of N*EPSLN*||A||. */
  2163. temp1 = sqrt((doublereal) (*n)) * epsln;
  2164. i__1 = *n;
  2165. for (p = 2; p <= i__1; ++p) {
  2166. if (z_abs(&a[p + p * a_dim1]) >= temp1 * z_abs(&a[a_dim1 + 1])) {
  2167. ++nr;
  2168. } else {
  2169. goto L3002;
  2170. }
  2171. /* L3001: */
  2172. }
  2173. L3002:
  2174. ;
  2175. } else if (l2rank) {
  2176. /* Sudden drop on the diagonal of R1 is used as the criterion for */
  2177. /* close-to-rank-deficient. */
  2178. temp1 = sqrt(sfmin);
  2179. i__1 = *n;
  2180. for (p = 2; p <= i__1; ++p) {
  2181. if (z_abs(&a[p + p * a_dim1]) < epsln * z_abs(&a[p - 1 + (p - 1) *
  2182. a_dim1]) || z_abs(&a[p + p * a_dim1]) < small || l2kill
  2183. && z_abs(&a[p + p * a_dim1]) < temp1) {
  2184. goto L3402;
  2185. }
  2186. ++nr;
  2187. /* L3401: */
  2188. }
  2189. L3402:
  2190. ;
  2191. } else {
  2192. /* The goal is high relative accuracy. However, if the matrix */
  2193. /* has high scaled condition number the relative accuracy is in */
  2194. /* general not feasible. Later on, a condition number estimator */
  2195. /* will be deployed to estimate the scaled condition number. */
  2196. /* Here we just remove the underflowed part of the triangular */
  2197. /* factor. This prevents the situation in which the code is */
  2198. /* working hard to get the accuracy not warranted by the data. */
  2199. temp1 = sqrt(sfmin);
  2200. i__1 = *n;
  2201. for (p = 2; p <= i__1; ++p) {
  2202. if (z_abs(&a[p + p * a_dim1]) < small || l2kill && z_abs(&a[p + p
  2203. * a_dim1]) < temp1) {
  2204. goto L3302;
  2205. }
  2206. ++nr;
  2207. /* L3301: */
  2208. }
  2209. L3302:
  2210. ;
  2211. }
  2212. almort = FALSE_;
  2213. if (nr == *n) {
  2214. maxprj = 1.;
  2215. i__1 = *n;
  2216. for (p = 2; p <= i__1; ++p) {
  2217. temp1 = z_abs(&a[p + p * a_dim1]) / sva[iwork[p]];
  2218. maxprj = f2cmin(maxprj,temp1);
  2219. /* L3051: */
  2220. }
  2221. /* Computing 2nd power */
  2222. d__1 = maxprj;
  2223. if (d__1 * d__1 >= 1. - (doublereal) (*n) * epsln) {
  2224. almort = TRUE_;
  2225. }
  2226. }
  2227. sconda = -1.;
  2228. condr1 = -1.;
  2229. condr2 = -1.;
  2230. if (errest) {
  2231. if (*n == nr) {
  2232. if (rsvec) {
  2233. zlacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
  2234. i__1 = *n;
  2235. for (p = 1; p <= i__1; ++p) {
  2236. temp1 = sva[iwork[p]];
  2237. d__1 = 1. / temp1;
  2238. zdscal_(&p, &d__1, &v[p * v_dim1 + 1], &c__1);
  2239. /* L3053: */
  2240. }
  2241. if (lsvec) {
  2242. zpocon_("U", n, &v[v_offset], ldv, &c_b141, &temp1, &
  2243. cwork[*n + 1], &rwork[1], &ierr);
  2244. } else {
  2245. zpocon_("U", n, &v[v_offset], ldv, &c_b141, &temp1, &
  2246. cwork[1], &rwork[1], &ierr);
  2247. }
  2248. } else if (lsvec) {
  2249. zlacpy_("U", n, n, &a[a_offset], lda, &u[u_offset], ldu);
  2250. i__1 = *n;
  2251. for (p = 1; p <= i__1; ++p) {
  2252. temp1 = sva[iwork[p]];
  2253. d__1 = 1. / temp1;
  2254. zdscal_(&p, &d__1, &u[p * u_dim1 + 1], &c__1);
  2255. /* L3054: */
  2256. }
  2257. zpocon_("U", n, &u[u_offset], ldu, &c_b141, &temp1, &cwork[*n
  2258. + 1], &rwork[1], &ierr);
  2259. } else {
  2260. zlacpy_("U", n, n, &a[a_offset], lda, &cwork[1], n)
  2261. ;
  2262. /* [] CALL ZLACPY( 'U', N, N, A, LDA, CWORK(N+1), N ) */
  2263. /* Change: here index shifted by N to the left, CWORK(1:N) */
  2264. /* not needed for SIGMA only computation */
  2265. i__1 = *n;
  2266. for (p = 1; p <= i__1; ++p) {
  2267. temp1 = sva[iwork[p]];
  2268. /* [] CALL ZDSCAL( p, ONE/TEMP1, CWORK(N+(p-1)*N+1), 1 ) */
  2269. d__1 = 1. / temp1;
  2270. zdscal_(&p, &d__1, &cwork[(p - 1) * *n + 1], &c__1);
  2271. /* L3052: */
  2272. }
  2273. /* [] CALL ZPOCON( 'U', N, CWORK(N+1), N, ONE, TEMP1, */
  2274. /* [] $ CWORK(N+N*N+1), RWORK, IERR ) */
  2275. zpocon_("U", n, &cwork[1], n, &c_b141, &temp1, &cwork[*n * *n
  2276. + 1], &rwork[1], &ierr);
  2277. }
  2278. if (temp1 != 0.) {
  2279. sconda = 1. / sqrt(temp1);
  2280. } else {
  2281. sconda = -1.;
  2282. }
  2283. /* SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1). */
  2284. /* N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
  2285. } else {
  2286. sconda = -1.;
  2287. }
  2288. }
  2289. z_div(&z__1, &a[a_dim1 + 1], &a[nr + nr * a_dim1]);
  2290. l2pert = l2pert && z_abs(&z__1) > sqrt(big1);
  2291. /* If there is no violent scaling, artificial perturbation is not needed. */
  2292. /* Phase 3: */
  2293. if (! (rsvec || lsvec)) {
  2294. /* Singular Values only */
  2295. /* Computing MIN */
  2296. i__2 = *n - 1;
  2297. i__1 = f2cmin(i__2,nr);
  2298. for (p = 1; p <= i__1; ++p) {
  2299. i__2 = *n - p;
  2300. zcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p *
  2301. a_dim1], &c__1);
  2302. i__2 = *n - p + 1;
  2303. zlacgv_(&i__2, &a[p + p * a_dim1], &c__1);
  2304. /* L1946: */
  2305. }
  2306. if (nr == *n) {
  2307. i__1 = *n + *n * a_dim1;
  2308. d_cnjg(&z__1, &a[*n + *n * a_dim1]);
  2309. a[i__1].r = z__1.r, a[i__1].i = z__1.i;
  2310. }
  2311. /* The following two DO-loops introduce small relative perturbation */
  2312. /* into the strict upper triangle of the lower triangular matrix. */
  2313. /* Small entries below the main diagonal are also changed. */
  2314. /* This modification is useful if the computing environment does not */
  2315. /* provide/allow FLUSH TO ZERO underflow, for it prevents many */
  2316. /* annoying denormalized numbers in case of strongly scaled matrices. */
  2317. /* The perturbation is structured so that it does not introduce any */
  2318. /* new perturbation of the singular values, and it does not destroy */
  2319. /* the job done by the preconditioner. */
  2320. /* The licence for this perturbation is in the variable L2PERT, which */
  2321. /* should be .FALSE. if FLUSH TO ZERO underflow is active. */
  2322. if (! almort) {
  2323. if (l2pert) {
  2324. /* XSC = SQRT(SMALL) */
  2325. xsc = epsln / (doublereal) (*n);
  2326. i__1 = nr;
  2327. for (q = 1; q <= i__1; ++q) {
  2328. d__1 = xsc * z_abs(&a[q + q * a_dim1]);
  2329. z__1.r = d__1, z__1.i = 0.;
  2330. ctemp.r = z__1.r, ctemp.i = z__1.i;
  2331. i__2 = *n;
  2332. for (p = 1; p <= i__2; ++p) {
  2333. if (p > q && z_abs(&a[p + q * a_dim1]) <= temp1 || p <
  2334. q) {
  2335. i__3 = p + q * a_dim1;
  2336. a[i__3].r = ctemp.r, a[i__3].i = ctemp.i;
  2337. }
  2338. /* $ A(p,q) = TEMP1 * ( A(p,q) / ABS(A(p,q)) ) */
  2339. /* L4949: */
  2340. }
  2341. /* L4947: */
  2342. }
  2343. } else {
  2344. i__1 = nr - 1;
  2345. i__2 = nr - 1;
  2346. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &a[(a_dim1 << 1) + 1]
  2347. , lda);
  2348. }
  2349. i__1 = *lwork - *n;
  2350. zgeqrf_(n, &nr, &a[a_offset], lda, &cwork[1], &cwork[*n + 1], &
  2351. i__1, &ierr);
  2352. i__1 = nr - 1;
  2353. for (p = 1; p <= i__1; ++p) {
  2354. i__2 = nr - p;
  2355. zcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p *
  2356. a_dim1], &c__1);
  2357. i__2 = nr - p + 1;
  2358. zlacgv_(&i__2, &a[p + p * a_dim1], &c__1);
  2359. /* L1948: */
  2360. }
  2361. }
  2362. /* Row-cyclic Jacobi SVD algorithm with column pivoting */
  2363. /* to drown denormals */
  2364. if (l2pert) {
  2365. /* XSC = SQRT(SMALL) */
  2366. xsc = epsln / (doublereal) (*n);
  2367. i__1 = nr;
  2368. for (q = 1; q <= i__1; ++q) {
  2369. d__1 = xsc * z_abs(&a[q + q * a_dim1]);
  2370. z__1.r = d__1, z__1.i = 0.;
  2371. ctemp.r = z__1.r, ctemp.i = z__1.i;
  2372. i__2 = nr;
  2373. for (p = 1; p <= i__2; ++p) {
  2374. if (p > q && z_abs(&a[p + q * a_dim1]) <= temp1 || p < q)
  2375. {
  2376. i__3 = p + q * a_dim1;
  2377. a[i__3].r = ctemp.r, a[i__3].i = ctemp.i;
  2378. }
  2379. /* $ A(p,q) = TEMP1 * ( A(p,q) / ABS(A(p,q)) ) */
  2380. /* L1949: */
  2381. }
  2382. /* L1947: */
  2383. }
  2384. } else {
  2385. i__1 = nr - 1;
  2386. i__2 = nr - 1;
  2387. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &a[(a_dim1 << 1) + 1],
  2388. lda);
  2389. }
  2390. /* triangular matrix (plus perturbation which is ignored in */
  2391. /* the part which destroys triangular form (confusing?!)) */
  2392. zgesvj_("L", "N", "N", &nr, &nr, &a[a_offset], lda, &sva[1], n, &v[
  2393. v_offset], ldv, &cwork[1], lwork, &rwork[1], lrwork, info);
  2394. scalem = rwork[1];
  2395. numrank = i_dnnt(&rwork[2]);
  2396. } else if (rsvec && ! lsvec && ! jracc || jracc && ! lsvec && nr != *n) {
  2397. /* -> Singular Values and Right Singular Vectors <- */
  2398. if (almort) {
  2399. i__1 = nr;
  2400. for (p = 1; p <= i__1; ++p) {
  2401. i__2 = *n - p + 1;
  2402. zcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
  2403. c__1);
  2404. i__2 = *n - p + 1;
  2405. zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  2406. /* L1998: */
  2407. }
  2408. i__1 = nr - 1;
  2409. i__2 = nr - 1;
  2410. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1],
  2411. ldv);
  2412. zgesvj_("L", "U", "N", n, &nr, &v[v_offset], ldv, &sva[1], &nr, &
  2413. a[a_offset], lda, &cwork[1], lwork, &rwork[1], lrwork,
  2414. info);
  2415. scalem = rwork[1];
  2416. numrank = i_dnnt(&rwork[2]);
  2417. } else {
  2418. /* accumulated product of Jacobi rotations, three are perfect ) */
  2419. i__1 = nr - 1;
  2420. i__2 = nr - 1;
  2421. zlaset_("L", &i__1, &i__2, &c_b1, &c_b1, &a[a_dim1 + 2], lda);
  2422. i__1 = *lwork - *n;
  2423. zgelqf_(&nr, n, &a[a_offset], lda, &cwork[1], &cwork[*n + 1], &
  2424. i__1, &ierr);
  2425. zlacpy_("L", &nr, &nr, &a[a_offset], lda, &v[v_offset], ldv);
  2426. i__1 = nr - 1;
  2427. i__2 = nr - 1;
  2428. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1],
  2429. ldv);
  2430. i__1 = *lwork - (*n << 1);
  2431. zgeqrf_(&nr, &nr, &v[v_offset], ldv, &cwork[*n + 1], &cwork[(*n <<
  2432. 1) + 1], &i__1, &ierr);
  2433. i__1 = nr;
  2434. for (p = 1; p <= i__1; ++p) {
  2435. i__2 = nr - p + 1;
  2436. zcopy_(&i__2, &v[p + p * v_dim1], ldv, &v[p + p * v_dim1], &
  2437. c__1);
  2438. i__2 = nr - p + 1;
  2439. zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  2440. /* L8998: */
  2441. }
  2442. i__1 = nr - 1;
  2443. i__2 = nr - 1;
  2444. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1],
  2445. ldv);
  2446. i__1 = *lwork - *n;
  2447. zgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[1], &nr,
  2448. &u[u_offset], ldu, &cwork[*n + 1], &i__1, &rwork[1],
  2449. lrwork, info);
  2450. scalem = rwork[1];
  2451. numrank = i_dnnt(&rwork[2]);
  2452. if (nr < *n) {
  2453. i__1 = *n - nr;
  2454. zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + v_dim1],
  2455. ldv);
  2456. i__1 = *n - nr;
  2457. zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * v_dim1 +
  2458. 1], ldv);
  2459. i__1 = *n - nr;
  2460. i__2 = *n - nr;
  2461. zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (nr + 1)
  2462. * v_dim1], ldv);
  2463. }
  2464. i__1 = *lwork - *n;
  2465. zunmlq_("L", "C", n, n, &nr, &a[a_offset], lda, &cwork[1], &v[
  2466. v_offset], ldv, &cwork[*n + 1], &i__1, &ierr);
  2467. }
  2468. /* DO 8991 p = 1, N */
  2469. /* CALL ZCOPY( N, V(p,1), LDV, A(IWORK(p),1), LDA ) */
  2470. /* 8991 CONTINUE */
  2471. /* CALL ZLACPY( 'All', N, N, A, LDA, V, LDV ) */
  2472. zlapmr_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  2473. if (transp) {
  2474. zlacpy_("A", n, n, &v[v_offset], ldv, &u[u_offset], ldu);
  2475. }
  2476. } else if (jracc && ! lsvec && nr == *n) {
  2477. i__1 = *n - 1;
  2478. i__2 = *n - 1;
  2479. zlaset_("L", &i__1, &i__2, &c_b1, &c_b1, &a[a_dim1 + 2], lda);
  2480. zgesvj_("U", "N", "V", n, n, &a[a_offset], lda, &sva[1], n, &v[
  2481. v_offset], ldv, &cwork[1], lwork, &rwork[1], lrwork, info);
  2482. scalem = rwork[1];
  2483. numrank = i_dnnt(&rwork[2]);
  2484. zlapmr_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  2485. } else if (lsvec && ! rsvec) {
  2486. /* Jacobi rotations in the Jacobi iterations. */
  2487. i__1 = nr;
  2488. for (p = 1; p <= i__1; ++p) {
  2489. i__2 = *n - p + 1;
  2490. zcopy_(&i__2, &a[p + p * a_dim1], lda, &u[p + p * u_dim1], &c__1);
  2491. i__2 = *n - p + 1;
  2492. zlacgv_(&i__2, &u[p + p * u_dim1], &c__1);
  2493. /* L1965: */
  2494. }
  2495. i__1 = nr - 1;
  2496. i__2 = nr - 1;
  2497. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1], ldu);
  2498. i__1 = *lwork - (*n << 1);
  2499. zgeqrf_(n, &nr, &u[u_offset], ldu, &cwork[*n + 1], &cwork[(*n << 1) +
  2500. 1], &i__1, &ierr);
  2501. i__1 = nr - 1;
  2502. for (p = 1; p <= i__1; ++p) {
  2503. i__2 = nr - p;
  2504. zcopy_(&i__2, &u[p + (p + 1) * u_dim1], ldu, &u[p + 1 + p *
  2505. u_dim1], &c__1);
  2506. i__2 = *n - p + 1;
  2507. zlacgv_(&i__2, &u[p + p * u_dim1], &c__1);
  2508. /* L1967: */
  2509. }
  2510. i__1 = nr - 1;
  2511. i__2 = nr - 1;
  2512. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1], ldu);
  2513. i__1 = *lwork - *n;
  2514. zgesvj_("L", "U", "N", &nr, &nr, &u[u_offset], ldu, &sva[1], &nr, &a[
  2515. a_offset], lda, &cwork[*n + 1], &i__1, &rwork[1], lrwork,
  2516. info);
  2517. scalem = rwork[1];
  2518. numrank = i_dnnt(&rwork[2]);
  2519. if (nr < *m) {
  2520. i__1 = *m - nr;
  2521. zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1], ldu);
  2522. if (nr < n1) {
  2523. i__1 = n1 - nr;
  2524. zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) * u_dim1 +
  2525. 1], ldu);
  2526. i__1 = *m - nr;
  2527. i__2 = n1 - nr;
  2528. zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (nr + 1)
  2529. * u_dim1], ldu);
  2530. }
  2531. }
  2532. i__1 = *lwork - *n;
  2533. zunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
  2534. u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
  2535. if (rowpiv) {
  2536. i__1 = *m - 1;
  2537. zlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[iwoff + 1], &
  2538. c_n1);
  2539. }
  2540. i__1 = n1;
  2541. for (p = 1; p <= i__1; ++p) {
  2542. xsc = 1. / dznrm2_(m, &u[p * u_dim1 + 1], &c__1);
  2543. zdscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  2544. /* L1974: */
  2545. }
  2546. if (transp) {
  2547. zlacpy_("A", n, n, &u[u_offset], ldu, &v[v_offset], ldv);
  2548. }
  2549. } else {
  2550. if (! jracc) {
  2551. if (! almort) {
  2552. /* Second Preconditioning Step (QRF [with pivoting]) */
  2553. /* Note that the composition of TRANSPOSE, QRF and TRANSPOSE is */
  2554. /* equivalent to an LQF CALL. Since in many libraries the QRF */
  2555. /* seems to be better optimized than the LQF, we do explicit */
  2556. /* transpose and use the QRF. This is subject to changes in an */
  2557. /* optimized implementation of ZGEJSV. */
  2558. i__1 = nr;
  2559. for (p = 1; p <= i__1; ++p) {
  2560. i__2 = *n - p + 1;
  2561. zcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1],
  2562. &c__1);
  2563. i__2 = *n - p + 1;
  2564. zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  2565. /* L1968: */
  2566. }
  2567. /* denormals in the second QR factorization, where they are */
  2568. /* as good as zeros. This is done to avoid painfully slow */
  2569. /* computation with denormals. The relative size of the perturbation */
  2570. /* is a parameter that can be changed by the implementer. */
  2571. /* This perturbation device will be obsolete on machines with */
  2572. /* properly implemented arithmetic. */
  2573. /* To switch it off, set L2PERT=.FALSE. To remove it from the */
  2574. /* code, remove the action under L2PERT=.TRUE., leave the ELSE part. */
  2575. /* The following two loops should be blocked and fused with the */
  2576. /* transposed copy above. */
  2577. if (l2pert) {
  2578. xsc = sqrt(small);
  2579. i__1 = nr;
  2580. for (q = 1; q <= i__1; ++q) {
  2581. d__1 = xsc * z_abs(&v[q + q * v_dim1]);
  2582. z__1.r = d__1, z__1.i = 0.;
  2583. ctemp.r = z__1.r, ctemp.i = z__1.i;
  2584. i__2 = *n;
  2585. for (p = 1; p <= i__2; ++p) {
  2586. if (p > q && z_abs(&v[p + q * v_dim1]) <= temp1 ||
  2587. p < q) {
  2588. i__3 = p + q * v_dim1;
  2589. v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
  2590. }
  2591. /* $ V(p,q) = TEMP1 * ( V(p,q) / ABS(V(p,q)) ) */
  2592. if (p < q) {
  2593. i__3 = p + q * v_dim1;
  2594. i__4 = p + q * v_dim1;
  2595. z__1.r = -v[i__4].r, z__1.i = -v[i__4].i;
  2596. v[i__3].r = z__1.r, v[i__3].i = z__1.i;
  2597. }
  2598. /* L2968: */
  2599. }
  2600. /* L2969: */
  2601. }
  2602. } else {
  2603. i__1 = nr - 1;
  2604. i__2 = nr - 1;
  2605. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1)
  2606. + 1], ldv);
  2607. }
  2608. /* Estimate the row scaled condition number of R1 */
  2609. /* (If R1 is rectangular, N > NR, then the condition number */
  2610. /* of the leading NR x NR submatrix is estimated.) */
  2611. zlacpy_("L", &nr, &nr, &v[v_offset], ldv, &cwork[(*n << 1) +
  2612. 1], &nr);
  2613. i__1 = nr;
  2614. for (p = 1; p <= i__1; ++p) {
  2615. i__2 = nr - p + 1;
  2616. temp1 = dznrm2_(&i__2, &cwork[(*n << 1) + (p - 1) * nr +
  2617. p], &c__1);
  2618. i__2 = nr - p + 1;
  2619. d__1 = 1. / temp1;
  2620. zdscal_(&i__2, &d__1, &cwork[(*n << 1) + (p - 1) * nr + p]
  2621. , &c__1);
  2622. /* L3950: */
  2623. }
  2624. zpocon_("L", &nr, &cwork[(*n << 1) + 1], &nr, &c_b141, &temp1,
  2625. &cwork[(*n << 1) + nr * nr + 1], &rwork[1], &ierr);
  2626. condr1 = 1. / sqrt(temp1);
  2627. /* R1 is OK for inverse <=> CONDR1 .LT. DBLE(N) */
  2628. /* more conservative <=> CONDR1 .LT. SQRT(DBLE(N)) */
  2629. cond_ok__ = sqrt(sqrt((doublereal) nr));
  2630. /* [TP] COND_OK is a tuning parameter. */
  2631. if (condr1 < cond_ok__) {
  2632. /* implementation, this QRF should be implemented as the QRF */
  2633. /* of a lower triangular matrix. */
  2634. /* R1^* = Q2 * R2 */
  2635. i__1 = *lwork - (*n << 1);
  2636. zgeqrf_(n, &nr, &v[v_offset], ldv, &cwork[*n + 1], &cwork[
  2637. (*n << 1) + 1], &i__1, &ierr);
  2638. if (l2pert) {
  2639. xsc = sqrt(small) / epsln;
  2640. i__1 = nr;
  2641. for (p = 2; p <= i__1; ++p) {
  2642. i__2 = p - 1;
  2643. for (q = 1; q <= i__2; ++q) {
  2644. /* Computing MIN */
  2645. d__2 = z_abs(&v[p + p * v_dim1]), d__3 =
  2646. z_abs(&v[q + q * v_dim1]);
  2647. d__1 = xsc * f2cmin(d__2,d__3);
  2648. z__1.r = d__1, z__1.i = 0.;
  2649. ctemp.r = z__1.r, ctemp.i = z__1.i;
  2650. if (z_abs(&v[q + p * v_dim1]) <= temp1) {
  2651. i__3 = q + p * v_dim1;
  2652. v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
  2653. }
  2654. /* $ V(q,p) = TEMP1 * ( V(q,p) / ABS(V(q,p)) ) */
  2655. /* L3958: */
  2656. }
  2657. /* L3959: */
  2658. }
  2659. }
  2660. if (nr != *n) {
  2661. zlacpy_("A", n, &nr, &v[v_offset], ldv, &cwork[(*n <<
  2662. 1) + 1], n);
  2663. }
  2664. i__1 = nr - 1;
  2665. for (p = 1; p <= i__1; ++p) {
  2666. i__2 = nr - p;
  2667. zcopy_(&i__2, &v[p + (p + 1) * v_dim1], ldv, &v[p + 1
  2668. + p * v_dim1], &c__1);
  2669. i__2 = nr - p + 1;
  2670. zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  2671. /* L1969: */
  2672. }
  2673. i__1 = nr + nr * v_dim1;
  2674. d_cnjg(&z__1, &v[nr + nr * v_dim1]);
  2675. v[i__1].r = z__1.r, v[i__1].i = z__1.i;
  2676. condr2 = condr1;
  2677. } else {
  2678. /* Note that windowed pivoting would be equally good */
  2679. /* numerically, and more run-time efficient. So, in */
  2680. /* an optimal implementation, the next call to ZGEQP3 */
  2681. /* should be replaced with eg. CALL ZGEQPX (ACM TOMS #782) */
  2682. /* with properly (carefully) chosen parameters. */
  2683. /* R1^* * P2 = Q2 * R2 */
  2684. i__1 = nr;
  2685. for (p = 1; p <= i__1; ++p) {
  2686. iwork[*n + p] = 0;
  2687. /* L3003: */
  2688. }
  2689. i__1 = *lwork - (*n << 1);
  2690. zgeqp3_(n, &nr, &v[v_offset], ldv, &iwork[*n + 1], &cwork[
  2691. *n + 1], &cwork[(*n << 1) + 1], &i__1, &rwork[1],
  2692. &ierr);
  2693. /* * CALL ZGEQRF( N, NR, V, LDV, CWORK(N+1), CWORK(2*N+1), */
  2694. /* * $ LWORK-2*N, IERR ) */
  2695. if (l2pert) {
  2696. xsc = sqrt(small);
  2697. i__1 = nr;
  2698. for (p = 2; p <= i__1; ++p) {
  2699. i__2 = p - 1;
  2700. for (q = 1; q <= i__2; ++q) {
  2701. /* Computing MIN */
  2702. d__2 = z_abs(&v[p + p * v_dim1]), d__3 =
  2703. z_abs(&v[q + q * v_dim1]);
  2704. d__1 = xsc * f2cmin(d__2,d__3);
  2705. z__1.r = d__1, z__1.i = 0.;
  2706. ctemp.r = z__1.r, ctemp.i = z__1.i;
  2707. if (z_abs(&v[q + p * v_dim1]) <= temp1) {
  2708. i__3 = q + p * v_dim1;
  2709. v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
  2710. }
  2711. /* $ V(q,p) = TEMP1 * ( V(q,p) / ABS(V(q,p)) ) */
  2712. /* L3968: */
  2713. }
  2714. /* L3969: */
  2715. }
  2716. }
  2717. zlacpy_("A", n, &nr, &v[v_offset], ldv, &cwork[(*n << 1)
  2718. + 1], n);
  2719. if (l2pert) {
  2720. xsc = sqrt(small);
  2721. i__1 = nr;
  2722. for (p = 2; p <= i__1; ++p) {
  2723. i__2 = p - 1;
  2724. for (q = 1; q <= i__2; ++q) {
  2725. /* Computing MIN */
  2726. d__2 = z_abs(&v[p + p * v_dim1]), d__3 =
  2727. z_abs(&v[q + q * v_dim1]);
  2728. d__1 = xsc * f2cmin(d__2,d__3);
  2729. z__1.r = d__1, z__1.i = 0.;
  2730. ctemp.r = z__1.r, ctemp.i = z__1.i;
  2731. /* V(p,q) = - TEMP1*( V(q,p) / ABS(V(q,p)) ) */
  2732. i__3 = p + q * v_dim1;
  2733. z__1.r = -ctemp.r, z__1.i = -ctemp.i;
  2734. v[i__3].r = z__1.r, v[i__3].i = z__1.i;
  2735. /* L8971: */
  2736. }
  2737. /* L8970: */
  2738. }
  2739. } else {
  2740. i__1 = nr - 1;
  2741. i__2 = nr - 1;
  2742. zlaset_("L", &i__1, &i__2, &c_b1, &c_b1, &v[v_dim1 +
  2743. 2], ldv);
  2744. }
  2745. /* Now, compute R2 = L3 * Q3, the LQ factorization. */
  2746. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2747. zgelqf_(&nr, &nr, &v[v_offset], ldv, &cwork[(*n << 1) + *
  2748. n * nr + 1], &cwork[(*n << 1) + *n * nr + nr + 1],
  2749. &i__1, &ierr);
  2750. zlacpy_("L", &nr, &nr, &v[v_offset], ldv, &cwork[(*n << 1)
  2751. + *n * nr + nr + 1], &nr);
  2752. i__1 = nr;
  2753. for (p = 1; p <= i__1; ++p) {
  2754. temp1 = dznrm2_(&p, &cwork[(*n << 1) + *n * nr + nr +
  2755. p], &nr);
  2756. d__1 = 1. / temp1;
  2757. zdscal_(&p, &d__1, &cwork[(*n << 1) + *n * nr + nr +
  2758. p], &nr);
  2759. /* L4950: */
  2760. }
  2761. zpocon_("L", &nr, &cwork[(*n << 1) + *n * nr + nr + 1], &
  2762. nr, &c_b141, &temp1, &cwork[(*n << 1) + *n * nr +
  2763. nr + nr * nr + 1], &rwork[1], &ierr);
  2764. condr2 = 1. / sqrt(temp1);
  2765. if (condr2 >= cond_ok__) {
  2766. /* (this overwrites the copy of R2, as it will not be */
  2767. /* needed in this branch, but it does not overwritte the */
  2768. /* Huseholder vectors of Q2.). */
  2769. zlacpy_("U", &nr, &nr, &v[v_offset], ldv, &cwork[(*n
  2770. << 1) + 1], n);
  2771. /* WORK(2*N+N*NR+1:2*N+N*NR+N) */
  2772. }
  2773. }
  2774. if (l2pert) {
  2775. xsc = sqrt(small);
  2776. i__1 = nr;
  2777. for (q = 2; q <= i__1; ++q) {
  2778. i__2 = q + q * v_dim1;
  2779. z__1.r = xsc * v[i__2].r, z__1.i = xsc * v[i__2].i;
  2780. ctemp.r = z__1.r, ctemp.i = z__1.i;
  2781. i__2 = q - 1;
  2782. for (p = 1; p <= i__2; ++p) {
  2783. /* V(p,q) = - TEMP1*( V(p,q) / ABS(V(p,q)) ) */
  2784. i__3 = p + q * v_dim1;
  2785. z__1.r = -ctemp.r, z__1.i = -ctemp.i;
  2786. v[i__3].r = z__1.r, v[i__3].i = z__1.i;
  2787. /* L4969: */
  2788. }
  2789. /* L4968: */
  2790. }
  2791. } else {
  2792. i__1 = nr - 1;
  2793. i__2 = nr - 1;
  2794. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1)
  2795. + 1], ldv);
  2796. }
  2797. /* Second preconditioning finished; continue with Jacobi SVD */
  2798. /* The input matrix is lower trinagular. */
  2799. /* Recover the right singular vectors as solution of a well */
  2800. /* conditioned triangular matrix equation. */
  2801. if (condr1 < cond_ok__) {
  2802. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2803. zgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
  2804. 1], &nr, &u[u_offset], ldu, &cwork[(*n << 1) + *n
  2805. * nr + nr + 1], &i__1, &rwork[1], lrwork, info);
  2806. scalem = rwork[1];
  2807. numrank = i_dnnt(&rwork[2]);
  2808. i__1 = nr;
  2809. for (p = 1; p <= i__1; ++p) {
  2810. zcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1
  2811. + 1], &c__1);
  2812. zdscal_(&nr, &sva[p], &v[p * v_dim1 + 1], &c__1);
  2813. /* L3970: */
  2814. }
  2815. if (nr == *n) {
  2816. /* :)) .. best case, R1 is inverted. The solution of this matrix */
  2817. /* equation is Q2*V2 = the product of the Jacobi rotations */
  2818. /* used in ZGESVJ, premultiplied with the orthogonal matrix */
  2819. /* from the second QR factorization. */
  2820. ztrsm_("L", "U", "N", "N", &nr, &nr, &c_b2, &a[
  2821. a_offset], lda, &v[v_offset], ldv);
  2822. } else {
  2823. /* is inverted to get the product of the Jacobi rotations */
  2824. /* used in ZGESVJ. The Q-factor from the second QR */
  2825. /* factorization is then built in explicitly. */
  2826. ztrsm_("L", "U", "C", "N", &nr, &nr, &c_b2, &cwork[(*
  2827. n << 1) + 1], n, &v[v_offset], ldv);
  2828. if (nr < *n) {
  2829. i__1 = *n - nr;
  2830. zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1
  2831. + v_dim1], ldv);
  2832. i__1 = *n - nr;
  2833. zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1)
  2834. * v_dim1 + 1], ldv);
  2835. i__1 = *n - nr;
  2836. i__2 = *n - nr;
  2837. zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr +
  2838. 1 + (nr + 1) * v_dim1], ldv);
  2839. }
  2840. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2841. zunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n,
  2842. &cwork[*n + 1], &v[v_offset], ldv, &cwork[(*
  2843. n << 1) + *n * nr + nr + 1], &i__1, &ierr);
  2844. }
  2845. } else if (condr2 < cond_ok__) {
  2846. /* The matrix R2 is inverted. The solution of the matrix equation */
  2847. /* is Q3^* * V3 = the product of the Jacobi rotations (appplied to */
  2848. /* the lower triangular L3 from the LQ factorization of */
  2849. /* R2=L3*Q3), pre-multiplied with the transposed Q3. */
  2850. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2851. zgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
  2852. 1], &nr, &u[u_offset], ldu, &cwork[(*n << 1) + *n
  2853. * nr + nr + 1], &i__1, &rwork[1], lrwork, info);
  2854. scalem = rwork[1];
  2855. numrank = i_dnnt(&rwork[2]);
  2856. i__1 = nr;
  2857. for (p = 1; p <= i__1; ++p) {
  2858. zcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1
  2859. + 1], &c__1);
  2860. zdscal_(&nr, &sva[p], &u[p * u_dim1 + 1], &c__1);
  2861. /* L3870: */
  2862. }
  2863. ztrsm_("L", "U", "N", "N", &nr, &nr, &c_b2, &cwork[(*n <<
  2864. 1) + 1], n, &u[u_offset], ldu);
  2865. i__1 = nr;
  2866. for (q = 1; q <= i__1; ++q) {
  2867. i__2 = nr;
  2868. for (p = 1; p <= i__2; ++p) {
  2869. i__3 = (*n << 1) + *n * nr + nr + iwork[*n + p];
  2870. i__4 = p + q * u_dim1;
  2871. cwork[i__3].r = u[i__4].r, cwork[i__3].i = u[i__4]
  2872. .i;
  2873. /* L872: */
  2874. }
  2875. i__2 = nr;
  2876. for (p = 1; p <= i__2; ++p) {
  2877. i__3 = p + q * u_dim1;
  2878. i__4 = (*n << 1) + *n * nr + nr + p;
  2879. u[i__3].r = cwork[i__4].r, u[i__3].i = cwork[i__4]
  2880. .i;
  2881. /* L874: */
  2882. }
  2883. /* L873: */
  2884. }
  2885. if (nr < *n) {
  2886. i__1 = *n - nr;
  2887. zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 +
  2888. v_dim1], ldv);
  2889. i__1 = *n - nr;
  2890. zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) *
  2891. v_dim1 + 1], ldv);
  2892. i__1 = *n - nr;
  2893. i__2 = *n - nr;
  2894. zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (
  2895. nr + 1) * v_dim1], ldv);
  2896. }
  2897. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2898. zunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n, &
  2899. cwork[*n + 1], &v[v_offset], ldv, &cwork[(*n << 1)
  2900. + *n * nr + nr + 1], &i__1, &ierr);
  2901. } else {
  2902. /* Last line of defense. */
  2903. /* #:( This is a rather pathological case: no scaled condition */
  2904. /* improvement after two pivoted QR factorizations. Other */
  2905. /* possibility is that the rank revealing QR factorization */
  2906. /* or the condition estimator has failed, or the COND_OK */
  2907. /* is set very close to ONE (which is unnecessary). Normally, */
  2908. /* this branch should never be executed, but in rare cases of */
  2909. /* failure of the RRQR or condition estimator, the last line of */
  2910. /* defense ensures that ZGEJSV completes the task. */
  2911. /* Compute the full SVD of L3 using ZGESVJ with explicit */
  2912. /* accumulation of Jacobi rotations. */
  2913. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2914. zgesvj_("L", "U", "V", &nr, &nr, &v[v_offset], ldv, &sva[
  2915. 1], &nr, &u[u_offset], ldu, &cwork[(*n << 1) + *n
  2916. * nr + nr + 1], &i__1, &rwork[1], lrwork, info);
  2917. scalem = rwork[1];
  2918. numrank = i_dnnt(&rwork[2]);
  2919. if (nr < *n) {
  2920. i__1 = *n - nr;
  2921. zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 +
  2922. v_dim1], ldv);
  2923. i__1 = *n - nr;
  2924. zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) *
  2925. v_dim1 + 1], ldv);
  2926. i__1 = *n - nr;
  2927. i__2 = *n - nr;
  2928. zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (
  2929. nr + 1) * v_dim1], ldv);
  2930. }
  2931. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2932. zunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n, &
  2933. cwork[*n + 1], &v[v_offset], ldv, &cwork[(*n << 1)
  2934. + *n * nr + nr + 1], &i__1, &ierr);
  2935. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2936. zunmlq_("L", "C", &nr, &nr, &nr, &cwork[(*n << 1) + 1], n,
  2937. &cwork[(*n << 1) + *n * nr + 1], &u[u_offset],
  2938. ldu, &cwork[(*n << 1) + *n * nr + nr + 1], &i__1,
  2939. &ierr);
  2940. i__1 = nr;
  2941. for (q = 1; q <= i__1; ++q) {
  2942. i__2 = nr;
  2943. for (p = 1; p <= i__2; ++p) {
  2944. i__3 = (*n << 1) + *n * nr + nr + iwork[*n + p];
  2945. i__4 = p + q * u_dim1;
  2946. cwork[i__3].r = u[i__4].r, cwork[i__3].i = u[i__4]
  2947. .i;
  2948. /* L772: */
  2949. }
  2950. i__2 = nr;
  2951. for (p = 1; p <= i__2; ++p) {
  2952. i__3 = p + q * u_dim1;
  2953. i__4 = (*n << 1) + *n * nr + nr + p;
  2954. u[i__3].r = cwork[i__4].r, u[i__3].i = cwork[i__4]
  2955. .i;
  2956. /* L774: */
  2957. }
  2958. /* L773: */
  2959. }
  2960. }
  2961. /* Permute the rows of V using the (column) permutation from the */
  2962. /* first QRF. Also, scale the columns to make them unit in */
  2963. /* Euclidean norm. This applies to all cases. */
  2964. temp1 = sqrt((doublereal) (*n)) * epsln;
  2965. i__1 = *n;
  2966. for (q = 1; q <= i__1; ++q) {
  2967. i__2 = *n;
  2968. for (p = 1; p <= i__2; ++p) {
  2969. i__3 = (*n << 1) + *n * nr + nr + iwork[p];
  2970. i__4 = p + q * v_dim1;
  2971. cwork[i__3].r = v[i__4].r, cwork[i__3].i = v[i__4].i;
  2972. /* L972: */
  2973. }
  2974. i__2 = *n;
  2975. for (p = 1; p <= i__2; ++p) {
  2976. i__3 = p + q * v_dim1;
  2977. i__4 = (*n << 1) + *n * nr + nr + p;
  2978. v[i__3].r = cwork[i__4].r, v[i__3].i = cwork[i__4].i;
  2979. /* L973: */
  2980. }
  2981. xsc = 1. / dznrm2_(n, &v[q * v_dim1 + 1], &c__1);
  2982. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  2983. zdscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
  2984. }
  2985. /* L1972: */
  2986. }
  2987. /* At this moment, V contains the right singular vectors of A. */
  2988. /* Next, assemble the left singular vector matrix U (M x N). */
  2989. if (nr < *m) {
  2990. i__1 = *m - nr;
  2991. zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1]
  2992. , ldu);
  2993. if (nr < n1) {
  2994. i__1 = n1 - nr;
  2995. zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) *
  2996. u_dim1 + 1], ldu);
  2997. i__1 = *m - nr;
  2998. i__2 = n1 - nr;
  2999. zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (
  3000. nr + 1) * u_dim1], ldu);
  3001. }
  3002. }
  3003. /* The Q matrix from the first QRF is built into the left singular */
  3004. /* matrix U. This applies to all cases. */
  3005. i__1 = *lwork - *n;
  3006. zunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
  3007. u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
  3008. /* The columns of U are normalized. The cost is O(M*N) flops. */
  3009. temp1 = sqrt((doublereal) (*m)) * epsln;
  3010. i__1 = nr;
  3011. for (p = 1; p <= i__1; ++p) {
  3012. xsc = 1. / dznrm2_(m, &u[p * u_dim1 + 1], &c__1);
  3013. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  3014. zdscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  3015. }
  3016. /* L1973: */
  3017. }
  3018. /* If the initial QRF is computed with row pivoting, the left */
  3019. /* singular vectors must be adjusted. */
  3020. if (rowpiv) {
  3021. i__1 = *m - 1;
  3022. zlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[
  3023. iwoff + 1], &c_n1);
  3024. }
  3025. } else {
  3026. /* the second QRF is not needed */
  3027. zlacpy_("U", n, n, &a[a_offset], lda, &cwork[*n + 1], n);
  3028. if (l2pert) {
  3029. xsc = sqrt(small);
  3030. i__1 = *n;
  3031. for (p = 2; p <= i__1; ++p) {
  3032. i__2 = *n + (p - 1) * *n + p;
  3033. z__1.r = xsc * cwork[i__2].r, z__1.i = xsc * cwork[
  3034. i__2].i;
  3035. ctemp.r = z__1.r, ctemp.i = z__1.i;
  3036. i__2 = p - 1;
  3037. for (q = 1; q <= i__2; ++q) {
  3038. /* CWORK(N+(q-1)*N+p)=-TEMP1 * ( CWORK(N+(p-1)*N+q) / */
  3039. /* $ ABS(CWORK(N+(p-1)*N+q)) ) */
  3040. i__3 = *n + (q - 1) * *n + p;
  3041. z__1.r = -ctemp.r, z__1.i = -ctemp.i;
  3042. cwork[i__3].r = z__1.r, cwork[i__3].i = z__1.i;
  3043. /* L5971: */
  3044. }
  3045. /* L5970: */
  3046. }
  3047. } else {
  3048. i__1 = *n - 1;
  3049. i__2 = *n - 1;
  3050. zlaset_("L", &i__1, &i__2, &c_b1, &c_b1, &cwork[*n + 2],
  3051. n);
  3052. }
  3053. i__1 = *lwork - *n - *n * *n;
  3054. zgesvj_("U", "U", "N", n, n, &cwork[*n + 1], n, &sva[1], n, &
  3055. u[u_offset], ldu, &cwork[*n + *n * *n + 1], &i__1, &
  3056. rwork[1], lrwork, info);
  3057. scalem = rwork[1];
  3058. numrank = i_dnnt(&rwork[2]);
  3059. i__1 = *n;
  3060. for (p = 1; p <= i__1; ++p) {
  3061. zcopy_(n, &cwork[*n + (p - 1) * *n + 1], &c__1, &u[p *
  3062. u_dim1 + 1], &c__1);
  3063. zdscal_(n, &sva[p], &cwork[*n + (p - 1) * *n + 1], &c__1);
  3064. /* L6970: */
  3065. }
  3066. ztrsm_("L", "U", "N", "N", n, n, &c_b2, &a[a_offset], lda, &
  3067. cwork[*n + 1], n);
  3068. i__1 = *n;
  3069. for (p = 1; p <= i__1; ++p) {
  3070. zcopy_(n, &cwork[*n + p], n, &v[iwork[p] + v_dim1], ldv);
  3071. /* L6972: */
  3072. }
  3073. temp1 = sqrt((doublereal) (*n)) * epsln;
  3074. i__1 = *n;
  3075. for (p = 1; p <= i__1; ++p) {
  3076. xsc = 1. / dznrm2_(n, &v[p * v_dim1 + 1], &c__1);
  3077. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  3078. zdscal_(n, &xsc, &v[p * v_dim1 + 1], &c__1);
  3079. }
  3080. /* L6971: */
  3081. }
  3082. /* Assemble the left singular vector matrix U (M x N). */
  3083. if (*n < *m) {
  3084. i__1 = *m - *n;
  3085. zlaset_("A", &i__1, n, &c_b1, &c_b1, &u[*n + 1 + u_dim1],
  3086. ldu);
  3087. if (*n < n1) {
  3088. i__1 = n1 - *n;
  3089. zlaset_("A", n, &i__1, &c_b1, &c_b1, &u[(*n + 1) *
  3090. u_dim1 + 1], ldu);
  3091. i__1 = *m - *n;
  3092. i__2 = n1 - *n;
  3093. zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[*n + 1 + (
  3094. *n + 1) * u_dim1], ldu);
  3095. }
  3096. }
  3097. i__1 = *lwork - *n;
  3098. zunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
  3099. u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
  3100. temp1 = sqrt((doublereal) (*m)) * epsln;
  3101. i__1 = n1;
  3102. for (p = 1; p <= i__1; ++p) {
  3103. xsc = 1. / dznrm2_(m, &u[p * u_dim1 + 1], &c__1);
  3104. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  3105. zdscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  3106. }
  3107. /* L6973: */
  3108. }
  3109. if (rowpiv) {
  3110. i__1 = *m - 1;
  3111. zlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[
  3112. iwoff + 1], &c_n1);
  3113. }
  3114. }
  3115. /* end of the >> almost orthogonal case << in the full SVD */
  3116. } else {
  3117. /* This branch deploys a preconditioned Jacobi SVD with explicitly */
  3118. /* accumulated rotations. It is included as optional, mainly for */
  3119. /* experimental purposes. It does perform well, and can also be used. */
  3120. /* In this implementation, this branch will be automatically activated */
  3121. /* if the condition number sigma_max(A) / sigma_min(A) is predicted */
  3122. /* to be greater than the overflow threshold. This is because the */
  3123. /* a posteriori computation of the singular vectors assumes robust */
  3124. /* implementation of BLAS and some LAPACK procedures, capable of working */
  3125. /* in presence of extreme values, e.g. when the singular values spread from */
  3126. /* the underflow to the overflow threshold. */
  3127. i__1 = nr;
  3128. for (p = 1; p <= i__1; ++p) {
  3129. i__2 = *n - p + 1;
  3130. zcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
  3131. c__1);
  3132. i__2 = *n - p + 1;
  3133. zlacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  3134. /* L7968: */
  3135. }
  3136. if (l2pert) {
  3137. xsc = sqrt(small / epsln);
  3138. i__1 = nr;
  3139. for (q = 1; q <= i__1; ++q) {
  3140. d__1 = xsc * z_abs(&v[q + q * v_dim1]);
  3141. z__1.r = d__1, z__1.i = 0.;
  3142. ctemp.r = z__1.r, ctemp.i = z__1.i;
  3143. i__2 = *n;
  3144. for (p = 1; p <= i__2; ++p) {
  3145. if (p > q && z_abs(&v[p + q * v_dim1]) <= temp1 || p <
  3146. q) {
  3147. i__3 = p + q * v_dim1;
  3148. v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
  3149. }
  3150. /* $ V(p,q) = TEMP1 * ( V(p,q) / ABS(V(p,q)) ) */
  3151. if (p < q) {
  3152. i__3 = p + q * v_dim1;
  3153. i__4 = p + q * v_dim1;
  3154. z__1.r = -v[i__4].r, z__1.i = -v[i__4].i;
  3155. v[i__3].r = z__1.r, v[i__3].i = z__1.i;
  3156. }
  3157. /* L5968: */
  3158. }
  3159. /* L5969: */
  3160. }
  3161. } else {
  3162. i__1 = nr - 1;
  3163. i__2 = nr - 1;
  3164. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1]
  3165. , ldv);
  3166. }
  3167. i__1 = *lwork - (*n << 1);
  3168. zgeqrf_(n, &nr, &v[v_offset], ldv, &cwork[*n + 1], &cwork[(*n <<
  3169. 1) + 1], &i__1, &ierr);
  3170. zlacpy_("L", n, &nr, &v[v_offset], ldv, &cwork[(*n << 1) + 1], n);
  3171. i__1 = nr;
  3172. for (p = 1; p <= i__1; ++p) {
  3173. i__2 = nr - p + 1;
  3174. zcopy_(&i__2, &v[p + p * v_dim1], ldv, &u[p + p * u_dim1], &
  3175. c__1);
  3176. i__2 = nr - p + 1;
  3177. zlacgv_(&i__2, &u[p + p * u_dim1], &c__1);
  3178. /* L7969: */
  3179. }
  3180. if (l2pert) {
  3181. xsc = sqrt(small / epsln);
  3182. i__1 = nr;
  3183. for (q = 2; q <= i__1; ++q) {
  3184. i__2 = q - 1;
  3185. for (p = 1; p <= i__2; ++p) {
  3186. /* Computing MIN */
  3187. d__2 = z_abs(&u[p + p * u_dim1]), d__3 = z_abs(&u[q +
  3188. q * u_dim1]);
  3189. d__1 = xsc * f2cmin(d__2,d__3);
  3190. z__1.r = d__1, z__1.i = 0.;
  3191. ctemp.r = z__1.r, ctemp.i = z__1.i;
  3192. /* U(p,q) = - TEMP1 * ( U(q,p) / ABS(U(q,p)) ) */
  3193. i__3 = p + q * u_dim1;
  3194. z__1.r = -ctemp.r, z__1.i = -ctemp.i;
  3195. u[i__3].r = z__1.r, u[i__3].i = z__1.i;
  3196. /* L9971: */
  3197. }
  3198. /* L9970: */
  3199. }
  3200. } else {
  3201. i__1 = nr - 1;
  3202. i__2 = nr - 1;
  3203. zlaset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1]
  3204. , ldu);
  3205. }
  3206. i__1 = *lwork - (*n << 1) - *n * nr;
  3207. zgesvj_("L", "U", "V", &nr, &nr, &u[u_offset], ldu, &sva[1], n, &
  3208. v[v_offset], ldv, &cwork[(*n << 1) + *n * nr + 1], &i__1,
  3209. &rwork[1], lrwork, info);
  3210. scalem = rwork[1];
  3211. numrank = i_dnnt(&rwork[2]);
  3212. if (nr < *n) {
  3213. i__1 = *n - nr;
  3214. zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + v_dim1],
  3215. ldv);
  3216. i__1 = *n - nr;
  3217. zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * v_dim1 +
  3218. 1], ldv);
  3219. i__1 = *n - nr;
  3220. i__2 = *n - nr;
  3221. zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (nr + 1)
  3222. * v_dim1], ldv);
  3223. }
  3224. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  3225. zunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n, &cwork[*n
  3226. + 1], &v[v_offset], ldv, &cwork[(*n << 1) + *n * nr + nr
  3227. + 1], &i__1, &ierr);
  3228. /* Permute the rows of V using the (column) permutation from the */
  3229. /* first QRF. Also, scale the columns to make them unit in */
  3230. /* Euclidean norm. This applies to all cases. */
  3231. temp1 = sqrt((doublereal) (*n)) * epsln;
  3232. i__1 = *n;
  3233. for (q = 1; q <= i__1; ++q) {
  3234. i__2 = *n;
  3235. for (p = 1; p <= i__2; ++p) {
  3236. i__3 = (*n << 1) + *n * nr + nr + iwork[p];
  3237. i__4 = p + q * v_dim1;
  3238. cwork[i__3].r = v[i__4].r, cwork[i__3].i = v[i__4].i;
  3239. /* L8972: */
  3240. }
  3241. i__2 = *n;
  3242. for (p = 1; p <= i__2; ++p) {
  3243. i__3 = p + q * v_dim1;
  3244. i__4 = (*n << 1) + *n * nr + nr + p;
  3245. v[i__3].r = cwork[i__4].r, v[i__3].i = cwork[i__4].i;
  3246. /* L8973: */
  3247. }
  3248. xsc = 1. / dznrm2_(n, &v[q * v_dim1 + 1], &c__1);
  3249. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  3250. zdscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
  3251. }
  3252. /* L7972: */
  3253. }
  3254. /* At this moment, V contains the right singular vectors of A. */
  3255. /* Next, assemble the left singular vector matrix U (M x N). */
  3256. if (nr < *m) {
  3257. i__1 = *m - nr;
  3258. zlaset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1],
  3259. ldu);
  3260. if (nr < n1) {
  3261. i__1 = n1 - nr;
  3262. zlaset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) *
  3263. u_dim1 + 1], ldu);
  3264. i__1 = *m - nr;
  3265. i__2 = n1 - nr;
  3266. zlaset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (nr
  3267. + 1) * u_dim1], ldu);
  3268. }
  3269. }
  3270. i__1 = *lwork - *n;
  3271. zunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
  3272. u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
  3273. if (rowpiv) {
  3274. i__1 = *m - 1;
  3275. zlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[iwoff +
  3276. 1], &c_n1);
  3277. }
  3278. }
  3279. if (transp) {
  3280. i__1 = *n;
  3281. for (p = 1; p <= i__1; ++p) {
  3282. zswap_(n, &u[p * u_dim1 + 1], &c__1, &v[p * v_dim1 + 1], &
  3283. c__1);
  3284. /* L6974: */
  3285. }
  3286. }
  3287. }
  3288. /* end of the full SVD */
  3289. /* Undo scaling, if necessary (and possible) */
  3290. if (uscal2 <= big / sva[1] * uscal1) {
  3291. dlascl_("G", &c__0, &c__0, &uscal1, &uscal2, &nr, &c__1, &sva[1], n, &
  3292. ierr);
  3293. uscal1 = 1.;
  3294. uscal2 = 1.;
  3295. }
  3296. if (nr < *n) {
  3297. i__1 = *n;
  3298. for (p = nr + 1; p <= i__1; ++p) {
  3299. sva[p] = 0.;
  3300. /* L3004: */
  3301. }
  3302. }
  3303. rwork[1] = uscal2 * scalem;
  3304. rwork[2] = uscal1;
  3305. if (errest) {
  3306. rwork[3] = sconda;
  3307. }
  3308. if (lsvec && rsvec) {
  3309. rwork[4] = condr1;
  3310. rwork[5] = condr2;
  3311. }
  3312. if (l2tran) {
  3313. rwork[6] = entra;
  3314. rwork[7] = entrat;
  3315. }
  3316. iwork[1] = nr;
  3317. iwork[2] = numrank;
  3318. iwork[3] = warning;
  3319. if (transp) {
  3320. iwork[4] = 1;
  3321. } else {
  3322. iwork[4] = -1;
  3323. }
  3324. return;
  3325. } /* zgejsv_ */