You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cgejsv.c 122 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553
  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]/df(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 complex c_b1 = {0.f,0.f};
  487. static complex c_b2 = {1.f,0.f};
  488. static integer c_n1 = -1;
  489. static integer c__1 = 1;
  490. static integer c__0 = 0;
  491. static real c_b141 = 1.f;
  492. static logical c_false = FALSE_;
  493. /* > \brief \b CGEJSV */
  494. /* =========== DOCUMENTATION =========== */
  495. /* Online html documentation available at */
  496. /* http://www.netlib.org/lapack/explore-html/ */
  497. /* > \htmlonly */
  498. /* > Download CGEJSV + dependencies */
  499. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgejsv.
  500. f"> */
  501. /* > [TGZ]</a> */
  502. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgejsv.
  503. f"> */
  504. /* > [ZIP]</a> */
  505. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgejsv.
  506. f"> */
  507. /* > [TXT]</a> */
  508. /* > \endhtmlonly */
  509. /* Definition: */
  510. /* =========== */
  511. /* SUBROUTINE CGEJSV( 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 A( LDA, * ), U( LDU, * ), V( LDV, * ), CWORK( LWORK ) */
  517. /* REAL 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. /* > CGEJSV 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=SLAMCH('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=SLAMCH('S'), EPSLN=SLAMCH('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 CGESVJ. */
  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 CGESVJ. */
  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 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 REAL 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 array, dimension ( LDU, N ) or ( LDU, M ) */
  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 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 array, dimension (MAX(2,LWORK)) */
  757. /* > If the call to CGEJSV 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 CGEQP3 and CGEQRF. */
  774. /* > In general, optimal LWORK is computed as */
  775. /* > LWORK >= f2cmax(N+LWORK(CGEQP3),N+LWORK(CGEQRF), LWORK(CGESVJ)). */
  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(CGEQP3),N+LWORK(CGEQRF), LWORK(CGESVJ), */
  783. /* > N*N+LWORK(CPOCON)). */
  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 CGEQP3, CGEQRF, CGELQ, */
  791. /* > CUNMLQ. In general, the optimal length LWORK is computed as */
  792. /* > LWORK >= f2cmax(N+LWORK(CGEQP3), N+LWORK(CGESVJ), */
  793. /* > N+LWORK(CGELQF), 2*N+LWORK(CGEQRF), N+LWORK(CUNMLQ)). */
  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 CGEQP3, CGEQRF, CGELQ, */
  800. /* > CUNMLQ. In general, the optimal length LWORK is computed as */
  801. /* > LWORK >= f2cmax(N+LWORK(CGEQP3), LWORK(CPOCON), N+LWORK(CGESVJ), */
  802. /* > N+LWORK(CGELQF), 2*N+LWORK(CGEQRF), N+LWORK(CUNMLQ)). */
  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 CGEQP3, CGEQRF, CUNMQR. */
  809. /* > In general, the optimal length LWORK is computed as */
  810. /* > LWORK >= f2cmax(N+LWORK(CGEQP3), 2*N+LWORK(CGEQRF), N+LWORK(CUNMQR)). */
  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 CGEQP3, CGEQRF, CUNMQR. */
  817. /* > In general, the optimal length LWORK is computed as */
  818. /* > LWORK >= f2cmax(N+LWORK(CGEQP3),N+LWORK(CPOCON), */
  819. /* > 2*N+LWORK(CGEQRF), N+LWORK(CUNMQR)). */
  820. /* > */
  821. /* > 4. If the full SVD is needed: (JOBU = 'U' or JOBU = 'F') and */
  822. /* > 4.1. if JOBV = 'V' */
  823. /* > the minimal requirement is LWORK >= 5*N+2*N*N. */
  824. /* > 4.2. if JOBV = 'J' the minimal requirement is */
  825. /* > LWORK >= 4*N+N*N. */
  826. /* > In both cases, the allocated CWORK can accommodate blocked runs */
  827. /* > of CGEQP3, CGEQRF, CGELQF, CUNMQR, CUNMLQ. */
  828. /* > */
  829. /* > If the call to CGEJSV is a workspace query (indicated by LWORK=-1 or */
  830. /* > LRWORK=-1), then on exit CWORK(1) contains the optimal and CWORK(2) contains the */
  831. /* > minimal length of CWORK for the job parameters used in the call. */
  832. /* > \endverbatim */
  833. /* > */
  834. /* > \param[out] RWORK */
  835. /* > \verbatim */
  836. /* > RWORK is REAL array, dimension (MAX(7,LWORK)) */
  837. /* > On exit, */
  838. /* > RWORK(1) = Determines the scaling factor SCALE = RWORK(2) / RWORK(1) */
  839. /* > such that SCALE*SVA(1:N) are the computed singular values */
  840. /* > of A. (See the description of SVA().) */
  841. /* > RWORK(2) = See the description of RWORK(1). */
  842. /* > RWORK(3) = SCONDA is an estimate for the condition number of */
  843. /* > column equilibrated A. (If JOBA = 'E' or 'G') */
  844. /* > SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1). */
  845. /* > It is computed using SPOCON. It holds */
  846. /* > N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
  847. /* > where R is the triangular factor from the QRF of A. */
  848. /* > However, if R is truncated and the numerical rank is */
  849. /* > determined to be strictly smaller than N, SCONDA is */
  850. /* > returned as -1, thus indicating that the smallest */
  851. /* > singular values might be lost. */
  852. /* > */
  853. /* > If full SVD is needed, the following two condition numbers are */
  854. /* > useful for the analysis of the algorithm. They are provied for */
  855. /* > a developer/implementer who is familiar with the details of */
  856. /* > the method. */
  857. /* > */
  858. /* > RWORK(4) = an estimate of the scaled condition number of the */
  859. /* > triangular factor in the first QR factorization. */
  860. /* > RWORK(5) = an estimate of the scaled condition number of the */
  861. /* > triangular factor in the second QR factorization. */
  862. /* > The following two parameters are computed if JOBT = 'T'. */
  863. /* > They are provided for a developer/implementer who is familiar */
  864. /* > with the details of the method. */
  865. /* > RWORK(6) = the entropy of A^* * A :: this is the Shannon entropy */
  866. /* > of diag(A^* * A) / Trace(A^* * A) taken as point in the */
  867. /* > probability simplex. */
  868. /* > RWORK(7) = the entropy of A * A^*. (See the description of RWORK(6).) */
  869. /* > If the call to CGEJSV is a workspace query (indicated by LWORK=-1 or */
  870. /* > LRWORK=-1), then on exit RWORK(1) contains the required length of */
  871. /* > RWORK for the job parameters used in the call. */
  872. /* > \endverbatim */
  873. /* > */
  874. /* > \param[in] LRWORK */
  875. /* > \verbatim */
  876. /* > LRWORK is INTEGER */
  877. /* > Length of RWORK to confirm proper allocation of workspace. */
  878. /* > LRWORK depends on the job: */
  879. /* > */
  880. /* > 1. If only the singular values are requested i.e. if */
  881. /* > LSAME(JOBU,'N') .AND. LSAME(JOBV,'N') */
  882. /* > then: */
  883. /* > 1.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
  884. /* > then: LRWORK = f2cmax( 7, 2 * M ). */
  885. /* > 1.2. Otherwise, LRWORK = f2cmax( 7, N ). */
  886. /* > 2. If singular values with the right singular vectors are requested */
  887. /* > i.e. if */
  888. /* > (LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) .AND. */
  889. /* > .NOT.(LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) */
  890. /* > then: */
  891. /* > 2.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
  892. /* > then LRWORK = f2cmax( 7, 2 * M ). */
  893. /* > 2.2. Otherwise, LRWORK = f2cmax( 7, N ). */
  894. /* > 3. If singular values with the left singular vectors are requested, i.e. if */
  895. /* > (LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) .AND. */
  896. /* > .NOT.(LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) */
  897. /* > then: */
  898. /* > 3.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
  899. /* > then LRWORK = f2cmax( 7, 2 * M ). */
  900. /* > 3.2. Otherwise, LRWORK = f2cmax( 7, N ). */
  901. /* > 4. If singular values with both the left and the right singular vectors */
  902. /* > are requested, i.e. if */
  903. /* > (LSAME(JOBU,'U').OR.LSAME(JOBU,'F')) .AND. */
  904. /* > (LSAME(JOBV,'V').OR.LSAME(JOBV,'J')) */
  905. /* > then: */
  906. /* > 4.1. If LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G'), */
  907. /* > then LRWORK = f2cmax( 7, 2 * M ). */
  908. /* > 4.2. Otherwise, LRWORK = f2cmax( 7, N ). */
  909. /* > */
  910. /* > If, on entry, LRWORK = -1 or LWORK=-1, a workspace query is assumed and */
  911. /* > the length of RWORK is returned in RWORK(1). */
  912. /* > \endverbatim */
  913. /* > */
  914. /* > \param[out] IWORK */
  915. /* > \verbatim */
  916. /* > IWORK is INTEGER array, of dimension at least 4, that further depends */
  917. /* > on the job: */
  918. /* > */
  919. /* > 1. If only the singular values are requested then: */
  920. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  921. /* > then the length of IWORK is N+M; otherwise the length of IWORK is N. */
  922. /* > 2. If the singular values and the right singular vectors are requested then: */
  923. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  924. /* > then the length of IWORK is N+M; otherwise the length of IWORK is N. */
  925. /* > 3. If the singular values and the left singular vectors are requested then: */
  926. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  927. /* > then the length of IWORK is N+M; otherwise the length of IWORK is N. */
  928. /* > 4. If the singular values with both the left and the right singular vectors */
  929. /* > are requested, then: */
  930. /* > 4.1. If LSAME(JOBV,'J') the length of IWORK is determined as follows: */
  931. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  932. /* > then the length of IWORK is N+M; otherwise the length of IWORK is N. */
  933. /* > 4.2. If LSAME(JOBV,'V') the length of IWORK is determined as follows: */
  934. /* > If ( LSAME(JOBT,'T') .OR. LSAME(JOBA,'F') .OR. LSAME(JOBA,'G') ) */
  935. /* > then the length of IWORK is 2*N+M; otherwise the length of IWORK is 2*N. */
  936. /* > */
  937. /* > On exit, */
  938. /* > IWORK(1) = the numerical rank determined after the initial */
  939. /* > QR factorization with pivoting. See the descriptions */
  940. /* > of JOBA and JOBR. */
  941. /* > IWORK(2) = the number of the computed nonzero singular values */
  942. /* > IWORK(3) = if nonzero, a warning message: */
  943. /* > If IWORK(3) = 1 then some of the column norms of A */
  944. /* > were denormalized floats. The requested high accuracy */
  945. /* > is not warranted by the data. */
  946. /* > IWORK(4) = 1 or -1. If IWORK(4) = 1, then the procedure used A^* to */
  947. /* > do the job as specified by the JOB parameters. */
  948. /* > If the call to CGEJSV is a workspace query (indicated by LWORK = -1 and */
  949. /* > LRWORK = -1), then on exit IWORK(1) contains the required length of */
  950. /* > IWORK for the job parameters used in the call. */
  951. /* > \endverbatim */
  952. /* > */
  953. /* > \param[out] INFO */
  954. /* > \verbatim */
  955. /* > INFO is INTEGER */
  956. /* > < 0: if INFO = -i, then the i-th argument had an illegal value. */
  957. /* > = 0: successful exit; */
  958. /* > > 0: CGEJSV did not converge in the maximal allowed number */
  959. /* > of sweeps. The computed values may be inaccurate. */
  960. /* > \endverbatim */
  961. /* Authors: */
  962. /* ======== */
  963. /* > \author Univ. of Tennessee */
  964. /* > \author Univ. of California Berkeley */
  965. /* > \author Univ. of Colorado Denver */
  966. /* > \author NAG Ltd. */
  967. /* > \date June 2016 */
  968. /* > \ingroup complexGEsing */
  969. /* > \par Further Details: */
  970. /* ===================== */
  971. /* > */
  972. /* > \verbatim */
  973. /* > CGEJSV implements a preconditioned Jacobi SVD algorithm. It uses CGEQP3, */
  974. /* > CGEQRF, and CGELQF 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 CGEJSV 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 (CGEJSV) is best used in this restricted range, */
  992. /* > meaning that singular values of magnitude below ||A||_2 / SLAMCH('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 (CGESVJ) is */
  997. /* > left to the implementer on a particular machine. */
  998. /* > The rank revealing QR factorization (in this code: CGEQP3) should be */
  999. /* > implemented as in [3]. We have a new version of CGEQP3 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 CGEJSV 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 CGEJSV uses only the simplest, naive data movement. */
  1011. /* > \endverbatim */
  1012. /* > \par Contributor: */
  1013. /* ================== */
  1014. /* > */
  1015. /* > Zlatko Drmac (Zagreb, Croatia) */
  1016. /* > \par References: */
  1017. /* ================ */
  1018. /* > */
  1019. /* > \verbatim */
  1020. /* > */
  1021. /* > [1] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I. */
  1022. /* > SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342. */
  1023. /* > LAPACK Working note 169. */
  1024. /* > [2] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II. */
  1025. /* > SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362. */
  1026. /* > LAPACK Working note 170. */
  1027. /* > [3] Z. Drmac and Z. Bujanovic: On the failure of rank-revealing QR */
  1028. /* > factorization software - a case study. */
  1029. /* > ACM Trans. Math. Softw. Vol. 35, No 2 (2008), pp. 1-28. */
  1030. /* > LAPACK Working note 176. */
  1031. /* > [4] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV, */
  1032. /* > QSVD, (H,K)-SVD computations. */
  1033. /* > Department of Mathematics, University of Zagreb, 2008, 2016. */
  1034. /* > \endverbatim */
  1035. /* > \par Bugs, examples and comments: */
  1036. /* ================================= */
  1037. /* > */
  1038. /* > Please report all bugs and send interesting examples and/or comments to */
  1039. /* > drmac@math.hr. Thank you. */
  1040. /* > */
  1041. /* ===================================================================== */
  1042. /* Subroutine */ void cgejsv_(char *joba, char *jobu, char *jobv, char *jobr,
  1043. char *jobt, char *jobp, integer *m, integer *n, complex *a, integer *
  1044. lda, real *sva, complex *u, integer *ldu, complex *v, integer *ldv,
  1045. complex *cwork, integer *lwork, real *rwork, integer *lrwork, integer
  1046. *iwork, integer *info)
  1047. {
  1048. /* System generated locals */
  1049. integer a_dim1, a_offset, u_dim1, u_offset, v_dim1, v_offset, i__1, i__2,
  1050. i__3, i__4, i__5, i__6, i__7, i__8, i__9, i__10, i__11;
  1051. real r__1, r__2, r__3;
  1052. complex q__1;
  1053. /* Local variables */
  1054. integer lwrk_cunmqr__;
  1055. logical defr;
  1056. real aapp, aaqq;
  1057. logical kill;
  1058. integer ierr, lwrk_cgeqp3n__;
  1059. real temp1;
  1060. integer lwunmqrm, lwrk_cgesvju__, lwrk_cgesvjv__, lwqp3, lwrk_cunmqrm__,
  1061. p, q;
  1062. logical jracc;
  1063. extern logical lsame_(char *, char *);
  1064. extern /* Subroutine */ void sscal_(integer *, real *, real *, integer *);
  1065. complex ctemp;
  1066. real entra, small;
  1067. integer iwoff;
  1068. real sfmin;
  1069. logical lsvec;
  1070. extern /* Subroutine */ void ccopy_(integer *, complex *, integer *,
  1071. complex *, integer *), cswap_(integer *, complex *, integer *,
  1072. complex *, integer *);
  1073. real epsln;
  1074. logical rsvec;
  1075. integer lwcon, lwlqf;
  1076. extern /* Subroutine */ void ctrsm_(char *, char *, char *, char *,
  1077. integer *, integer *, complex *, complex *, integer *, complex *,
  1078. integer *);
  1079. integer lwqrf, n1;
  1080. logical l2aber;
  1081. extern /* Subroutine */ void cgeqp3_(integer *, integer *, complex *,
  1082. integer *, integer *, complex *, complex *, integer *, real *,
  1083. integer *);
  1084. real condr1, condr2, uscal1, uscal2;
  1085. logical l2kill, l2rank, l2tran;
  1086. extern real scnrm2_(integer *, complex *, integer *);
  1087. logical l2pert;
  1088. integer lrwqp3;
  1089. extern /* Subroutine */ void clacgv_(integer *, complex *, integer *);
  1090. integer nr;
  1091. extern /* Subroutine */ void cgelqf_(integer *, integer *, complex *,
  1092. integer *, complex *, complex *, integer *, integer *);
  1093. extern integer icamax_(integer *, complex *, integer *);
  1094. extern /* Subroutine */ void clascl_(char *, integer *, integer *, real *,
  1095. real *, integer *, integer *, complex *, integer *, integer *);
  1096. real scalem, sconda;
  1097. logical goscal;
  1098. real aatmin;
  1099. extern real slamch_(char *);
  1100. real aatmax;
  1101. extern /* Subroutine */ void cgeqrf_(integer *, integer *, complex *,
  1102. integer *, complex *, complex *, integer *, integer *), clacpy_(
  1103. char *, integer *, integer *, complex *, integer *, complex *,
  1104. integer *), clapmr_(logical *, integer *, integer *,
  1105. complex *, integer *, integer *);
  1106. logical noscal;
  1107. extern /* Subroutine */ void claset_(char *, integer *, integer *, complex
  1108. *, complex *, complex *, integer *);
  1109. extern integer isamax_(integer *, real *, integer *);
  1110. extern /* Subroutine */ void slascl_(char *, integer *, integer *, real *,
  1111. real *, integer *, integer *, real *, integer *, integer *), cpocon_(char *, integer *, complex *, integer *, real *,
  1112. real *, complex *, real *, integer *), csscal_(integer *,
  1113. real *, complex *, integer *), classq_(integer *, complex *,
  1114. integer *, real *, real *);
  1115. extern int xerbla_(char *, integer *, ftnlen);
  1116. extern void cgesvj_(char *, char *, char *, integer *, integer *, complex *,
  1117. integer *, real *, integer *, complex *, integer *, complex *,
  1118. integer *, real *, integer *, integer *);
  1119. extern int claswp_(integer *, complex *, integer *, integer *, integer *,
  1120. integer *, integer *);
  1121. real entrat;
  1122. logical almort;
  1123. complex cdummy[1];
  1124. extern /* Subroutine */ void cungqr_(integer *, integer *, integer *,
  1125. complex *, integer *, complex *, complex *, integer *, integer *);
  1126. real maxprj;
  1127. extern /* Subroutine */ void cunmlq_(char *, char *, integer *, integer *,
  1128. integer *, complex *, integer *, complex *, complex *, integer *,
  1129. complex *, integer *, integer *);
  1130. logical errest;
  1131. integer lrwcon;
  1132. extern /* Subroutine */ void slassq_(integer *, real *, integer *, real *,
  1133. real *);
  1134. logical transp;
  1135. integer minwrk, lwsvdj;
  1136. extern /* Subroutine */ void cunmqr_(char *, char *, integer *, integer *,
  1137. integer *, complex *, integer *, complex *, complex *, integer *,
  1138. complex *, integer *, integer *);
  1139. real rdummy[1];
  1140. logical lquery, rowpiv;
  1141. integer optwrk;
  1142. real big;
  1143. integer lwrk_cgeqp3__;
  1144. real cond_ok__, xsc, big1;
  1145. integer warning, numrank, lwrk_cgelqf__, miniwrk, lwrk_cgeqrf__, minrwrk,
  1146. lrwsvdj, lwunmlq, lwsvdjv, lwrk_cgesvj__, lwunmqr, lwrk_cunmlq__;
  1147. /* -- LAPACK computational routine (version 3.7.1) -- */
  1148. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  1149. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  1150. /* June 2017 */
  1151. /* =========================================================================== */
  1152. /* Test the input arguments */
  1153. /* Parameter adjustments */
  1154. --sva;
  1155. a_dim1 = *lda;
  1156. a_offset = 1 + a_dim1 * 1;
  1157. a -= a_offset;
  1158. u_dim1 = *ldu;
  1159. u_offset = 1 + u_dim1 * 1;
  1160. u -= u_offset;
  1161. v_dim1 = *ldv;
  1162. v_offset = 1 + v_dim1 * 1;
  1163. v -= v_offset;
  1164. --cwork;
  1165. --rwork;
  1166. --iwork;
  1167. /* Function Body */
  1168. lsvec = lsame_(jobu, "U") || lsame_(jobu, "F");
  1169. jracc = lsame_(jobv, "J");
  1170. rsvec = lsame_(jobv, "V") || jracc;
  1171. rowpiv = lsame_(joba, "F") || lsame_(joba, "G");
  1172. l2rank = lsame_(joba, "R");
  1173. l2aber = lsame_(joba, "A");
  1174. errest = lsame_(joba, "E") || lsame_(joba, "G");
  1175. l2tran = lsame_(jobt, "T") && *m == *n;
  1176. l2kill = lsame_(jobr, "R");
  1177. defr = lsame_(jobr, "N");
  1178. l2pert = lsame_(jobp, "P");
  1179. lquery = *lwork == -1 || *lrwork == -1;
  1180. if (! (rowpiv || l2rank || l2aber || errest || lsame_(joba, "C"))) {
  1181. *info = -1;
  1182. } else if (! (lsvec || lsame_(jobu, "N") || lsame_(
  1183. jobu, "W") && rsvec && l2tran)) {
  1184. *info = -2;
  1185. } else if (! (rsvec || lsame_(jobv, "N") || lsame_(
  1186. jobv, "W") && lsvec && l2tran)) {
  1187. *info = -3;
  1188. } else if (! (l2kill || defr)) {
  1189. *info = -4;
  1190. } else if (! (lsame_(jobt, "T") || lsame_(jobt,
  1191. "N"))) {
  1192. *info = -5;
  1193. } else if (! (l2pert || lsame_(jobp, "N"))) {
  1194. *info = -6;
  1195. } else if (*m < 0) {
  1196. *info = -7;
  1197. } else if (*n < 0 || *n > *m) {
  1198. *info = -8;
  1199. } else if (*lda < *m) {
  1200. *info = -10;
  1201. } else if (lsvec && *ldu < *m) {
  1202. *info = -13;
  1203. } else if (rsvec && *ldv < *n) {
  1204. *info = -15;
  1205. } else {
  1206. /* #:) */
  1207. *info = 0;
  1208. }
  1209. if (*info == 0) {
  1210. /* [[The expressions for computing the minimal and the optimal */
  1211. /* values of LCWORK, LRWORK are written with a lot of redundancy and */
  1212. /* can be simplified. However, this verbose form is useful for */
  1213. /* maintenance and modifications of the code.]] */
  1214. /* CGEQRF of an N x N matrix, CGELQF of an N x N matrix, */
  1215. /* CUNMLQ for computing N x N matrix, CUNMQR for computing N x N */
  1216. /* matrix, CUNMQR for computing M x N matrix, respectively. */
  1217. lwqp3 = *n + 1;
  1218. lwqrf = f2cmax(1,*n);
  1219. lwlqf = f2cmax(1,*n);
  1220. lwunmlq = f2cmax(1,*n);
  1221. lwunmqr = f2cmax(1,*n);
  1222. lwunmqrm = f2cmax(1,*m);
  1223. lwcon = *n << 1;
  1224. /* without and with explicit accumulation of Jacobi rotations */
  1225. /* Computing MAX */
  1226. i__1 = *n << 1;
  1227. lwsvdj = f2cmax(i__1,1);
  1228. /* Computing MAX */
  1229. i__1 = *n << 1;
  1230. lwsvdjv = f2cmax(i__1,1);
  1231. lrwqp3 = *n << 1;
  1232. lrwcon = *n;
  1233. lrwsvdj = *n;
  1234. if (lquery) {
  1235. cgeqp3_(m, n, &a[a_offset], lda, &iwork[1], cdummy, cdummy, &c_n1,
  1236. rdummy, &ierr);
  1237. lwrk_cgeqp3__ = cdummy[0].r;
  1238. cgeqrf_(n, n, &a[a_offset], lda, cdummy, cdummy, &c_n1, &ierr);
  1239. lwrk_cgeqrf__ = cdummy[0].r;
  1240. cgelqf_(n, n, &a[a_offset], lda, cdummy, cdummy, &c_n1, &ierr);
  1241. lwrk_cgelqf__ = cdummy[0].r;
  1242. }
  1243. minwrk = 2;
  1244. optwrk = 2;
  1245. miniwrk = *n;
  1246. if (! (lsvec || rsvec)) {
  1247. /* only the singular values are requested */
  1248. if (errest) {
  1249. /* Computing MAX */
  1250. /* Computing 2nd power */
  1251. i__3 = *n;
  1252. i__1 = *n + lwqp3, i__2 = i__3 * i__3 + lwcon, i__1 = f2cmax(
  1253. i__1,i__2), i__2 = *n + lwqrf, i__1 = f2cmax(i__1,i__2);
  1254. minwrk = f2cmax(i__1,lwsvdj);
  1255. } else {
  1256. /* Computing MAX */
  1257. i__1 = *n + lwqp3, i__2 = *n + lwqrf, i__1 = f2cmax(i__1,i__2);
  1258. minwrk = f2cmax(i__1,lwsvdj);
  1259. }
  1260. if (lquery) {
  1261. cgesvj_("L", "N", "N", n, n, &a[a_offset], lda, &sva[1], n, &
  1262. v[v_offset], ldv, cdummy, &c_n1, rdummy, &c_n1, &ierr);
  1263. lwrk_cgesvj__ = cdummy[0].r;
  1264. if (errest) {
  1265. /* Computing MAX */
  1266. /* Computing 2nd power */
  1267. i__3 = *n;
  1268. i__1 = *n + lwrk_cgeqp3__, i__2 = i__3 * i__3 + lwcon,
  1269. i__1 = f2cmax(i__1,i__2), i__2 = *n + lwrk_cgeqrf__,
  1270. i__1 = f2cmax(i__1,i__2);
  1271. optwrk = f2cmax(i__1,lwrk_cgesvj__);
  1272. } else {
  1273. /* Computing MAX */
  1274. i__1 = *n + lwrk_cgeqp3__, i__2 = *n + lwrk_cgeqrf__,
  1275. i__1 = f2cmax(i__1,i__2);
  1276. optwrk = f2cmax(i__1,lwrk_cgesvj__);
  1277. }
  1278. }
  1279. if (l2tran || rowpiv) {
  1280. if (errest) {
  1281. /* Computing MAX */
  1282. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1283. f2cmax(i__1,lrwqp3), i__1 = f2cmax(i__1,lrwcon);
  1284. minrwrk = f2cmax(i__1,lrwsvdj);
  1285. } else {
  1286. /* Computing MAX */
  1287. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1288. f2cmax(i__1,lrwqp3);
  1289. minrwrk = f2cmax(i__1,lrwsvdj);
  1290. }
  1291. } else {
  1292. if (errest) {
  1293. /* Computing MAX */
  1294. i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwcon);
  1295. minrwrk = f2cmax(i__1,lrwsvdj);
  1296. } else {
  1297. /* Computing MAX */
  1298. i__1 = f2cmax(7,lrwqp3);
  1299. minrwrk = f2cmax(i__1,lrwsvdj);
  1300. }
  1301. }
  1302. if (rowpiv || l2tran) {
  1303. miniwrk += *m;
  1304. }
  1305. } else if (rsvec && ! lsvec) {
  1306. /* singular values and the right singular vectors are requested */
  1307. if (errest) {
  1308. /* Computing MAX */
  1309. i__1 = *n + lwqp3, i__1 = f2cmax(i__1,lwcon), i__1 = f2cmax(i__1,
  1310. lwsvdj), i__2 = *n + lwlqf, i__1 = f2cmax(i__1,i__2),
  1311. i__2 = (*n << 1) + lwqrf, i__1 = f2cmax(i__1,i__2), i__2
  1312. = *n + lwsvdj, i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1313. lwunmlq;
  1314. minwrk = f2cmax(i__1,i__2);
  1315. } else {
  1316. /* Computing MAX */
  1317. i__1 = *n + lwqp3, i__1 = f2cmax(i__1,lwsvdj), i__2 = *n + lwlqf,
  1318. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqrf,
  1319. i__1 = f2cmax(i__1,i__2), i__2 = *n + lwsvdj, i__1 = f2cmax(
  1320. i__1,i__2), i__2 = *n + lwunmlq;
  1321. minwrk = f2cmax(i__1,i__2);
  1322. }
  1323. if (lquery) {
  1324. cgesvj_("L", "U", "N", n, n, &u[u_offset], ldu, &sva[1], n, &
  1325. a[a_offset], lda, cdummy, &c_n1, rdummy, &c_n1, &ierr);
  1326. lwrk_cgesvj__ = cdummy[0].r;
  1327. cunmlq_("L", "C", n, n, n, &a[a_offset], lda, cdummy, &v[
  1328. v_offset], ldv, cdummy, &c_n1, &ierr);
  1329. lwrk_cunmlq__ = cdummy[0].r;
  1330. if (errest) {
  1331. /* Computing MAX */
  1332. i__1 = *n + lwrk_cgeqp3__, i__1 = f2cmax(i__1,lwcon), i__1 =
  1333. f2cmax(i__1,lwrk_cgesvj__), i__2 = *n +
  1334. lwrk_cgelqf__, i__1 = f2cmax(i__1,i__2), i__2 = (*n
  1335. << 1) + lwrk_cgeqrf__, i__1 = f2cmax(i__1,i__2),
  1336. i__2 = *n + lwrk_cgesvj__, i__1 = f2cmax(i__1,i__2),
  1337. i__2 = *n + lwrk_cunmlq__;
  1338. optwrk = f2cmax(i__1,i__2);
  1339. } else {
  1340. /* Computing MAX */
  1341. i__1 = *n + lwrk_cgeqp3__, i__1 = f2cmax(i__1,lwrk_cgesvj__),
  1342. i__2 = *n + lwrk_cgelqf__, i__1 = f2cmax(i__1,i__2),
  1343. i__2 = (*n << 1) + lwrk_cgeqrf__, i__1 = f2cmax(
  1344. i__1,i__2), i__2 = *n + lwrk_cgesvj__, i__1 = f2cmax(
  1345. i__1,i__2), i__2 = *n + lwrk_cunmlq__;
  1346. optwrk = f2cmax(i__1,i__2);
  1347. }
  1348. }
  1349. if (l2tran || rowpiv) {
  1350. if (errest) {
  1351. /* Computing MAX */
  1352. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1353. f2cmax(i__1,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1354. minrwrk = f2cmax(i__1,lrwcon);
  1355. } else {
  1356. /* Computing MAX */
  1357. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1358. f2cmax(i__1,lrwqp3);
  1359. minrwrk = f2cmax(i__1,lrwsvdj);
  1360. }
  1361. } else {
  1362. if (errest) {
  1363. /* Computing MAX */
  1364. i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1365. minrwrk = f2cmax(i__1,lrwcon);
  1366. } else {
  1367. /* Computing MAX */
  1368. i__1 = f2cmax(7,lrwqp3);
  1369. minrwrk = f2cmax(i__1,lrwsvdj);
  1370. }
  1371. }
  1372. if (rowpiv || l2tran) {
  1373. miniwrk += *m;
  1374. }
  1375. } else if (lsvec && ! rsvec) {
  1376. /* singular values and the left singular vectors are requested */
  1377. if (errest) {
  1378. /* Computing MAX */
  1379. i__1 = f2cmax(lwqp3,lwcon), i__2 = *n + lwqrf, i__1 = f2cmax(i__1,
  1380. i__2), i__1 = f2cmax(i__1,lwsvdj);
  1381. minwrk = *n + f2cmax(i__1,lwunmqrm);
  1382. } else {
  1383. /* Computing MAX */
  1384. i__1 = lwqp3, i__2 = *n + lwqrf, i__1 = f2cmax(i__1,i__2), i__1 =
  1385. f2cmax(i__1,lwsvdj);
  1386. minwrk = *n + f2cmax(i__1,lwunmqrm);
  1387. }
  1388. if (lquery) {
  1389. cgesvj_("L", "U", "N", n, n, &u[u_offset], ldu, &sva[1], n, &
  1390. a[a_offset], lda, cdummy, &c_n1, rdummy, &c_n1, &ierr);
  1391. lwrk_cgesvj__ = cdummy[0].r;
  1392. cunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
  1393. u_offset], ldu, cdummy, &c_n1, &ierr);
  1394. lwrk_cunmqrm__ = cdummy[0].r;
  1395. if (errest) {
  1396. /* Computing MAX */
  1397. i__1 = f2cmax(lwrk_cgeqp3__,lwcon), i__2 = *n +
  1398. lwrk_cgeqrf__, i__1 = f2cmax(i__1,i__2), i__1 = f2cmax(
  1399. i__1,lwrk_cgesvj__);
  1400. optwrk = *n + f2cmax(i__1,lwrk_cunmqrm__);
  1401. } else {
  1402. /* Computing MAX */
  1403. i__1 = lwrk_cgeqp3__, i__2 = *n + lwrk_cgeqrf__, i__1 =
  1404. f2cmax(i__1,i__2), i__1 = f2cmax(i__1,lwrk_cgesvj__);
  1405. optwrk = *n + f2cmax(i__1,lwrk_cunmqrm__);
  1406. }
  1407. }
  1408. if (l2tran || rowpiv) {
  1409. if (errest) {
  1410. /* Computing MAX */
  1411. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1412. f2cmax(i__1,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1413. minrwrk = f2cmax(i__1,lrwcon);
  1414. } else {
  1415. /* Computing MAX */
  1416. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 =
  1417. f2cmax(i__1,lrwqp3);
  1418. minrwrk = f2cmax(i__1,lrwsvdj);
  1419. }
  1420. } else {
  1421. if (errest) {
  1422. /* Computing MAX */
  1423. i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1424. minrwrk = f2cmax(i__1,lrwcon);
  1425. } else {
  1426. /* Computing MAX */
  1427. i__1 = f2cmax(7,lrwqp3);
  1428. minrwrk = f2cmax(i__1,lrwsvdj);
  1429. }
  1430. }
  1431. if (rowpiv || l2tran) {
  1432. miniwrk += *m;
  1433. }
  1434. } else {
  1435. /* full SVD is requested */
  1436. if (! jracc) {
  1437. if (errest) {
  1438. /* Computing MAX */
  1439. /* Computing 2nd power */
  1440. i__3 = *n;
  1441. /* Computing 2nd power */
  1442. i__4 = *n;
  1443. /* Computing 2nd power */
  1444. i__5 = *n;
  1445. /* Computing 2nd power */
  1446. i__6 = *n;
  1447. /* Computing 2nd power */
  1448. i__7 = *n;
  1449. /* Computing 2nd power */
  1450. i__8 = *n;
  1451. /* Computing 2nd power */
  1452. i__9 = *n;
  1453. /* Computing 2nd power */
  1454. i__10 = *n;
  1455. /* Computing 2nd power */
  1456. i__11 = *n;
  1457. i__1 = *n + lwqp3, i__2 = *n + lwcon, i__1 = f2cmax(i__1,
  1458. i__2), i__2 = (*n << 1) + i__3 * i__3 + lwcon,
  1459. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqrf,
  1460. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqp3,
  1461. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1462. i__4 + *n + lwlqf, i__1 = f2cmax(i__1,i__2), i__2 = (
  1463. *n << 1) + i__5 * i__5 + *n + i__6 * i__6 + lwcon,
  1464. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__7 *
  1465. i__7 + *n + lwsvdj, i__1 = f2cmax(i__1,i__2), i__2 =
  1466. (*n << 1) + i__8 * i__8 + *n + lwsvdjv, i__1 =
  1467. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__9 * i__9 + *
  1468. n + lwunmqr, i__1 = f2cmax(i__1,i__2), i__2 = (*n <<
  1469. 1) + i__10 * i__10 + *n + lwunmlq, i__1 = f2cmax(
  1470. i__1,i__2), i__2 = *n + i__11 * i__11 + lwsvdj,
  1471. i__1 = f2cmax(i__1,i__2), i__2 = *n + lwunmqrm;
  1472. minwrk = f2cmax(i__1,i__2);
  1473. } else {
  1474. /* Computing MAX */
  1475. /* Computing 2nd power */
  1476. i__3 = *n;
  1477. /* Computing 2nd power */
  1478. i__4 = *n;
  1479. /* Computing 2nd power */
  1480. i__5 = *n;
  1481. /* Computing 2nd power */
  1482. i__6 = *n;
  1483. /* Computing 2nd power */
  1484. i__7 = *n;
  1485. /* Computing 2nd power */
  1486. i__8 = *n;
  1487. /* Computing 2nd power */
  1488. i__9 = *n;
  1489. /* Computing 2nd power */
  1490. i__10 = *n;
  1491. /* Computing 2nd power */
  1492. i__11 = *n;
  1493. i__1 = *n + lwqp3, i__2 = (*n << 1) + i__3 * i__3 + lwcon,
  1494. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqrf,
  1495. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + lwqp3,
  1496. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1497. i__4 + *n + lwlqf, i__1 = f2cmax(i__1,i__2), i__2 = (
  1498. *n << 1) + i__5 * i__5 + *n + i__6 * i__6 + lwcon,
  1499. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__7 *
  1500. i__7 + *n + lwsvdj, i__1 = f2cmax(i__1,i__2), i__2 =
  1501. (*n << 1) + i__8 * i__8 + *n + lwsvdjv, i__1 =
  1502. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__9 * i__9 + *
  1503. n + lwunmqr, i__1 = f2cmax(i__1,i__2), i__2 = (*n <<
  1504. 1) + i__10 * i__10 + *n + lwunmlq, i__1 = f2cmax(
  1505. i__1,i__2), i__2 = *n + i__11 * i__11 + lwsvdj,
  1506. i__1 = f2cmax(i__1,i__2), i__2 = *n + lwunmqrm;
  1507. minwrk = f2cmax(i__1,i__2);
  1508. }
  1509. miniwrk += *n;
  1510. if (rowpiv || l2tran) {
  1511. miniwrk += *m;
  1512. }
  1513. } else {
  1514. if (errest) {
  1515. /* Computing MAX */
  1516. /* Computing 2nd power */
  1517. i__3 = *n;
  1518. /* Computing 2nd power */
  1519. i__4 = *n;
  1520. i__1 = *n + lwqp3, i__2 = *n + lwcon, i__1 = f2cmax(i__1,
  1521. i__2), i__2 = (*n << 1) + lwqrf, i__1 = f2cmax(i__1,
  1522. i__2), i__2 = (*n << 1) + i__3 * i__3 + lwsvdjv,
  1523. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1524. i__4 + *n + lwunmqr, i__1 = f2cmax(i__1,i__2), i__2 =
  1525. *n + lwunmqrm;
  1526. minwrk = f2cmax(i__1,i__2);
  1527. } else {
  1528. /* Computing MAX */
  1529. /* Computing 2nd power */
  1530. i__3 = *n;
  1531. /* Computing 2nd power */
  1532. i__4 = *n;
  1533. i__1 = *n + lwqp3, i__2 = (*n << 1) + lwqrf, i__1 = f2cmax(
  1534. i__1,i__2), i__2 = (*n << 1) + i__3 * i__3 +
  1535. lwsvdjv, i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1)
  1536. + i__4 * i__4 + *n + lwunmqr, i__1 = f2cmax(i__1,
  1537. i__2), i__2 = *n + lwunmqrm;
  1538. minwrk = f2cmax(i__1,i__2);
  1539. }
  1540. if (rowpiv || l2tran) {
  1541. miniwrk += *m;
  1542. }
  1543. }
  1544. if (lquery) {
  1545. cunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
  1546. u_offset], ldu, cdummy, &c_n1, &ierr);
  1547. lwrk_cunmqrm__ = cdummy[0].r;
  1548. cunmqr_("L", "N", n, n, n, &a[a_offset], lda, cdummy, &u[
  1549. u_offset], ldu, cdummy, &c_n1, &ierr);
  1550. lwrk_cunmqr__ = cdummy[0].r;
  1551. if (! jracc) {
  1552. cgeqp3_(n, n, &a[a_offset], lda, &iwork[1], cdummy,
  1553. cdummy, &c_n1, rdummy, &ierr);
  1554. lwrk_cgeqp3n__ = cdummy[0].r;
  1555. cgesvj_("L", "U", "N", n, n, &u[u_offset], ldu, &sva[1],
  1556. n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
  1557. c_n1, &ierr);
  1558. lwrk_cgesvj__ = cdummy[0].r;
  1559. cgesvj_("U", "U", "N", n, n, &u[u_offset], ldu, &sva[1],
  1560. n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
  1561. c_n1, &ierr);
  1562. lwrk_cgesvju__ = cdummy[0].r;
  1563. cgesvj_("L", "U", "V", n, n, &u[u_offset], ldu, &sva[1],
  1564. n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
  1565. c_n1, &ierr);
  1566. lwrk_cgesvjv__ = cdummy[0].r;
  1567. cunmlq_("L", "C", n, n, n, &a[a_offset], lda, cdummy, &v[
  1568. v_offset], ldv, cdummy, &c_n1, &ierr);
  1569. lwrk_cunmlq__ = cdummy[0].r;
  1570. if (errest) {
  1571. /* Computing MAX */
  1572. /* Computing 2nd power */
  1573. i__3 = *n;
  1574. /* Computing 2nd power */
  1575. i__4 = *n;
  1576. /* Computing 2nd power */
  1577. i__5 = *n;
  1578. /* Computing 2nd power */
  1579. i__6 = *n;
  1580. /* Computing 2nd power */
  1581. i__7 = *n;
  1582. /* Computing 2nd power */
  1583. i__8 = *n;
  1584. /* Computing 2nd power */
  1585. i__9 = *n;
  1586. /* Computing 2nd power */
  1587. i__10 = *n;
  1588. /* Computing 2nd power */
  1589. i__11 = *n;
  1590. i__1 = *n + lwrk_cgeqp3__, i__2 = *n + lwcon, i__1 =
  1591. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__3 *
  1592. i__3 + lwcon, i__1 = f2cmax(i__1,i__2), i__2 = (*
  1593. n << 1) + lwrk_cgeqrf__, i__1 = f2cmax(i__1,i__2)
  1594. , i__2 = (*n << 1) + lwrk_cgeqp3n__, i__1 =
  1595. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1596. i__4 + *n + lwrk_cgelqf__, i__1 = f2cmax(i__1,
  1597. i__2), i__2 = (*n << 1) + i__5 * i__5 + *n +
  1598. i__6 * i__6 + lwcon, i__1 = f2cmax(i__1,i__2),
  1599. i__2 = (*n << 1) + i__7 * i__7 + *n +
  1600. lwrk_cgesvj__, i__1 = f2cmax(i__1,i__2), i__2 = (
  1601. *n << 1) + i__8 * i__8 + *n + lwrk_cgesvjv__,
  1602. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) +
  1603. i__9 * i__9 + *n + lwrk_cunmqr__, i__1 = f2cmax(
  1604. i__1,i__2), i__2 = (*n << 1) + i__10 * i__10
  1605. + *n + lwrk_cunmlq__, i__1 = f2cmax(i__1,i__2),
  1606. i__2 = *n + i__11 * i__11 + lwrk_cgesvju__,
  1607. i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1608. lwrk_cunmqrm__;
  1609. optwrk = f2cmax(i__1,i__2);
  1610. } else {
  1611. /* Computing MAX */
  1612. /* Computing 2nd power */
  1613. i__3 = *n;
  1614. /* Computing 2nd power */
  1615. i__4 = *n;
  1616. /* Computing 2nd power */
  1617. i__5 = *n;
  1618. /* Computing 2nd power */
  1619. i__6 = *n;
  1620. /* Computing 2nd power */
  1621. i__7 = *n;
  1622. /* Computing 2nd power */
  1623. i__8 = *n;
  1624. /* Computing 2nd power */
  1625. i__9 = *n;
  1626. /* Computing 2nd power */
  1627. i__10 = *n;
  1628. /* Computing 2nd power */
  1629. i__11 = *n;
  1630. i__1 = *n + lwrk_cgeqp3__, i__2 = (*n << 1) + i__3 *
  1631. i__3 + lwcon, i__1 = f2cmax(i__1,i__2), i__2 = (*
  1632. n << 1) + lwrk_cgeqrf__, i__1 = f2cmax(i__1,i__2)
  1633. , i__2 = (*n << 1) + lwrk_cgeqp3n__, i__1 =
  1634. f2cmax(i__1,i__2), i__2 = (*n << 1) + i__4 *
  1635. i__4 + *n + lwrk_cgelqf__, i__1 = f2cmax(i__1,
  1636. i__2), i__2 = (*n << 1) + i__5 * i__5 + *n +
  1637. i__6 * i__6 + lwcon, i__1 = f2cmax(i__1,i__2),
  1638. i__2 = (*n << 1) + i__7 * i__7 + *n +
  1639. lwrk_cgesvj__, i__1 = f2cmax(i__1,i__2), i__2 = (
  1640. *n << 1) + i__8 * i__8 + *n + lwrk_cgesvjv__,
  1641. i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1) +
  1642. i__9 * i__9 + *n + lwrk_cunmqr__, i__1 = f2cmax(
  1643. i__1,i__2), i__2 = (*n << 1) + i__10 * i__10
  1644. + *n + lwrk_cunmlq__, i__1 = f2cmax(i__1,i__2),
  1645. i__2 = *n + i__11 * i__11 + lwrk_cgesvju__,
  1646. i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1647. lwrk_cunmqrm__;
  1648. optwrk = f2cmax(i__1,i__2);
  1649. }
  1650. } else {
  1651. cgesvj_("L", "U", "V", n, n, &u[u_offset], ldu, &sva[1],
  1652. n, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
  1653. c_n1, &ierr);
  1654. lwrk_cgesvjv__ = cdummy[0].r;
  1655. cunmqr_("L", "N", n, n, n, cdummy, n, cdummy, &v[v_offset]
  1656. , ldv, cdummy, &c_n1, &ierr)
  1657. ;
  1658. lwrk_cunmqr__ = cdummy[0].r;
  1659. cunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
  1660. u_offset], ldu, cdummy, &c_n1, &ierr);
  1661. lwrk_cunmqrm__ = cdummy[0].r;
  1662. if (errest) {
  1663. /* Computing MAX */
  1664. /* Computing 2nd power */
  1665. i__3 = *n;
  1666. /* Computing 2nd power */
  1667. i__4 = *n;
  1668. /* Computing 2nd power */
  1669. i__5 = *n;
  1670. i__1 = *n + lwrk_cgeqp3__, i__2 = *n + lwcon, i__1 =
  1671. f2cmax(i__1,i__2), i__2 = (*n << 1) +
  1672. lwrk_cgeqrf__, i__1 = f2cmax(i__1,i__2), i__2 = (
  1673. *n << 1) + i__3 * i__3, i__1 = f2cmax(i__1,i__2),
  1674. i__2 = (*n << 1) + i__4 * i__4 +
  1675. lwrk_cgesvjv__, i__1 = f2cmax(i__1,i__2), i__2 =
  1676. (*n << 1) + i__5 * i__5 + *n + lwrk_cunmqr__,
  1677. i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1678. lwrk_cunmqrm__;
  1679. optwrk = f2cmax(i__1,i__2);
  1680. } else {
  1681. /* Computing MAX */
  1682. /* Computing 2nd power */
  1683. i__3 = *n;
  1684. /* Computing 2nd power */
  1685. i__4 = *n;
  1686. /* Computing 2nd power */
  1687. i__5 = *n;
  1688. i__1 = *n + lwrk_cgeqp3__, i__2 = (*n << 1) +
  1689. lwrk_cgeqrf__, i__1 = f2cmax(i__1,i__2), i__2 = (
  1690. *n << 1) + i__3 * i__3, i__1 = f2cmax(i__1,i__2),
  1691. i__2 = (*n << 1) + i__4 * i__4 +
  1692. lwrk_cgesvjv__, i__1 = f2cmax(i__1,i__2), i__2 =
  1693. (*n << 1) + i__5 * i__5 + *n + lwrk_cunmqr__,
  1694. i__1 = f2cmax(i__1,i__2), i__2 = *n +
  1695. lwrk_cunmqrm__;
  1696. optwrk = f2cmax(i__1,i__2);
  1697. }
  1698. }
  1699. }
  1700. if (l2tran || rowpiv) {
  1701. /* Computing MAX */
  1702. i__1 = 7, i__2 = *m << 1, i__1 = f2cmax(i__1,i__2), i__1 = f2cmax(
  1703. i__1,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1704. minrwrk = f2cmax(i__1,lrwcon);
  1705. } else {
  1706. /* Computing MAX */
  1707. i__1 = f2cmax(7,lrwqp3), i__1 = f2cmax(i__1,lrwsvdj);
  1708. minrwrk = f2cmax(i__1,lrwcon);
  1709. }
  1710. }
  1711. minwrk = f2cmax(2,minwrk);
  1712. optwrk = f2cmax(optwrk,minwrk);
  1713. if (*lwork < minwrk && ! lquery) {
  1714. *info = -17;
  1715. }
  1716. if (*lrwork < minrwrk && ! lquery) {
  1717. *info = -19;
  1718. }
  1719. }
  1720. if (*info != 0) {
  1721. /* #:( */
  1722. i__1 = -(*info);
  1723. xerbla_("CGEJSV", &i__1, (ftnlen)6);
  1724. return;
  1725. } else if (lquery) {
  1726. cwork[1].r = (real) optwrk, cwork[1].i = 0.f;
  1727. cwork[2].r = (real) minwrk, cwork[2].i = 0.f;
  1728. rwork[1] = (real) minrwrk;
  1729. iwork[1] = f2cmax(4,miniwrk);
  1730. return;
  1731. }
  1732. /* Quick return for void matrix (Y3K safe) */
  1733. /* #:) */
  1734. if (*m == 0 || *n == 0) {
  1735. iwork[1] = 0;
  1736. iwork[2] = 0;
  1737. iwork[3] = 0;
  1738. iwork[4] = 0;
  1739. rwork[1] = 0.f;
  1740. rwork[2] = 0.f;
  1741. rwork[3] = 0.f;
  1742. rwork[4] = 0.f;
  1743. rwork[5] = 0.f;
  1744. rwork[6] = 0.f;
  1745. rwork[7] = 0.f;
  1746. return;
  1747. }
  1748. /* Determine whether the matrix U should be M x N or M x M */
  1749. if (lsvec) {
  1750. n1 = *n;
  1751. if (lsame_(jobu, "F")) {
  1752. n1 = *m;
  1753. }
  1754. }
  1755. /* Set numerical parameters */
  1756. /* ! NOTE: Make sure SLAMCH() does not fail on the target architecture. */
  1757. epsln = slamch_("Epsilon");
  1758. sfmin = slamch_("SafeMinimum");
  1759. small = sfmin / epsln;
  1760. big = slamch_("O");
  1761. /* BIG = ONE / SFMIN */
  1762. /* Initialize SVA(1:N) = diag( ||A e_i||_2 )_1^N */
  1763. /* (!) If necessary, scale SVA() to protect the largest norm from */
  1764. /* overflow. It is possible that this scaling pushes the smallest */
  1765. /* column norm left from the underflow threshold (extreme case). */
  1766. scalem = 1.f / sqrt((real) (*m) * (real) (*n));
  1767. noscal = TRUE_;
  1768. goscal = TRUE_;
  1769. i__1 = *n;
  1770. for (p = 1; p <= i__1; ++p) {
  1771. aapp = 0.f;
  1772. aaqq = 1.f;
  1773. classq_(m, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
  1774. if (aapp > big) {
  1775. *info = -9;
  1776. i__2 = -(*info);
  1777. xerbla_("CGEJSV", &i__2, (ftnlen)6);
  1778. return;
  1779. }
  1780. aaqq = sqrt(aaqq);
  1781. if (aapp < big / aaqq && noscal) {
  1782. sva[p] = aapp * aaqq;
  1783. } else {
  1784. noscal = FALSE_;
  1785. sva[p] = aapp * (aaqq * scalem);
  1786. if (goscal) {
  1787. goscal = FALSE_;
  1788. i__2 = p - 1;
  1789. sscal_(&i__2, &scalem, &sva[1], &c__1);
  1790. }
  1791. }
  1792. /* L1874: */
  1793. }
  1794. if (noscal) {
  1795. scalem = 1.f;
  1796. }
  1797. aapp = 0.f;
  1798. aaqq = big;
  1799. i__1 = *n;
  1800. for (p = 1; p <= i__1; ++p) {
  1801. /* Computing MAX */
  1802. r__1 = aapp, r__2 = sva[p];
  1803. aapp = f2cmax(r__1,r__2);
  1804. if (sva[p] != 0.f) {
  1805. /* Computing MIN */
  1806. r__1 = aaqq, r__2 = sva[p];
  1807. aaqq = f2cmin(r__1,r__2);
  1808. }
  1809. /* L4781: */
  1810. }
  1811. /* Quick return for zero M x N matrix */
  1812. /* #:) */
  1813. if (aapp == 0.f) {
  1814. if (lsvec) {
  1815. claset_("G", m, &n1, &c_b1, &c_b2, &u[u_offset], ldu);
  1816. }
  1817. if (rsvec) {
  1818. claset_("G", n, n, &c_b1, &c_b2, &v[v_offset], ldv);
  1819. }
  1820. rwork[1] = 1.f;
  1821. rwork[2] = 1.f;
  1822. if (errest) {
  1823. rwork[3] = 1.f;
  1824. }
  1825. if (lsvec && rsvec) {
  1826. rwork[4] = 1.f;
  1827. rwork[5] = 1.f;
  1828. }
  1829. if (l2tran) {
  1830. rwork[6] = 0.f;
  1831. rwork[7] = 0.f;
  1832. }
  1833. iwork[1] = 0;
  1834. iwork[2] = 0;
  1835. iwork[3] = 0;
  1836. iwork[4] = -1;
  1837. return;
  1838. }
  1839. /* Issue warning if denormalized column norms detected. Override the */
  1840. /* high relative accuracy request. Issue licence to kill nonzero columns */
  1841. /* (set them to zero) whose norm is less than sigma_max / BIG (roughly). */
  1842. /* #:( */
  1843. warning = 0;
  1844. if (aaqq <= sfmin) {
  1845. l2rank = TRUE_;
  1846. l2kill = TRUE_;
  1847. warning = 1;
  1848. }
  1849. /* Quick return for one-column matrix */
  1850. /* #:) */
  1851. if (*n == 1) {
  1852. if (lsvec) {
  1853. clascl_("G", &c__0, &c__0, &sva[1], &scalem, m, &c__1, &a[a_dim1
  1854. + 1], lda, &ierr);
  1855. clacpy_("A", m, &c__1, &a[a_offset], lda, &u[u_offset], ldu);
  1856. /* computing all M left singular vectors of the M x 1 matrix */
  1857. if (n1 != *n) {
  1858. i__1 = *lwork - *n;
  1859. cgeqrf_(m, n, &u[u_offset], ldu, &cwork[1], &cwork[*n + 1], &
  1860. i__1, &ierr);
  1861. i__1 = *lwork - *n;
  1862. cungqr_(m, &n1, &c__1, &u[u_offset], ldu, &cwork[1], &cwork[*
  1863. n + 1], &i__1, &ierr);
  1864. ccopy_(m, &a[a_dim1 + 1], &c__1, &u[u_dim1 + 1], &c__1);
  1865. }
  1866. }
  1867. if (rsvec) {
  1868. i__1 = v_dim1 + 1;
  1869. v[i__1].r = 1.f, v[i__1].i = 0.f;
  1870. }
  1871. if (sva[1] < big * scalem) {
  1872. sva[1] /= scalem;
  1873. scalem = 1.f;
  1874. }
  1875. rwork[1] = 1.f / scalem;
  1876. rwork[2] = 1.f;
  1877. if (sva[1] != 0.f) {
  1878. iwork[1] = 1;
  1879. if (sva[1] / scalem >= sfmin) {
  1880. iwork[2] = 1;
  1881. } else {
  1882. iwork[2] = 0;
  1883. }
  1884. } else {
  1885. iwork[1] = 0;
  1886. iwork[2] = 0;
  1887. }
  1888. iwork[3] = 0;
  1889. iwork[4] = -1;
  1890. if (errest) {
  1891. rwork[3] = 1.f;
  1892. }
  1893. if (lsvec && rsvec) {
  1894. rwork[4] = 1.f;
  1895. rwork[5] = 1.f;
  1896. }
  1897. if (l2tran) {
  1898. rwork[6] = 0.f;
  1899. rwork[7] = 0.f;
  1900. }
  1901. return;
  1902. }
  1903. transp = FALSE_;
  1904. aatmax = -1.f;
  1905. aatmin = big;
  1906. if (rowpiv || l2tran) {
  1907. /* Compute the row norms, needed to determine row pivoting sequence */
  1908. /* (in the case of heavily row weighted A, row pivoting is strongly */
  1909. /* advised) and to collect information needed to compare the */
  1910. /* structures of A * A^* and A^* * A (in the case L2TRAN.EQ..TRUE.). */
  1911. if (l2tran) {
  1912. i__1 = *m;
  1913. for (p = 1; p <= i__1; ++p) {
  1914. xsc = 0.f;
  1915. temp1 = 1.f;
  1916. classq_(n, &a[p + a_dim1], lda, &xsc, &temp1);
  1917. /* CLASSQ gets both the ell_2 and the ell_infinity norm */
  1918. /* in one pass through the vector */
  1919. rwork[*m + p] = xsc * scalem;
  1920. rwork[p] = xsc * (scalem * sqrt(temp1));
  1921. /* Computing MAX */
  1922. r__1 = aatmax, r__2 = rwork[p];
  1923. aatmax = f2cmax(r__1,r__2);
  1924. if (rwork[p] != 0.f) {
  1925. /* Computing MIN */
  1926. r__1 = aatmin, r__2 = rwork[p];
  1927. aatmin = f2cmin(r__1,r__2);
  1928. }
  1929. /* L1950: */
  1930. }
  1931. } else {
  1932. i__1 = *m;
  1933. for (p = 1; p <= i__1; ++p) {
  1934. rwork[*m + p] = scalem * c_abs(&a[p + icamax_(n, &a[p +
  1935. a_dim1], lda) * a_dim1]);
  1936. /* Computing MAX */
  1937. r__1 = aatmax, r__2 = rwork[*m + p];
  1938. aatmax = f2cmax(r__1,r__2);
  1939. /* Computing MIN */
  1940. r__1 = aatmin, r__2 = rwork[*m + p];
  1941. aatmin = f2cmin(r__1,r__2);
  1942. /* L1904: */
  1943. }
  1944. }
  1945. }
  1946. /* For square matrix A try to determine whether A^* would be better */
  1947. /* input for the preconditioned Jacobi SVD, with faster convergence. */
  1948. /* The decision is based on an O(N) function of the vector of column */
  1949. /* and row norms of A, based on the Shannon entropy. This should give */
  1950. /* the right choice in most cases when the difference actually matters. */
  1951. /* It may fail and pick the slower converging side. */
  1952. entra = 0.f;
  1953. entrat = 0.f;
  1954. if (l2tran) {
  1955. xsc = 0.f;
  1956. temp1 = 1.f;
  1957. slassq_(n, &sva[1], &c__1, &xsc, &temp1);
  1958. temp1 = 1.f / temp1;
  1959. entra = 0.f;
  1960. i__1 = *n;
  1961. for (p = 1; p <= i__1; ++p) {
  1962. /* Computing 2nd power */
  1963. r__1 = sva[p] / xsc;
  1964. big1 = r__1 * r__1 * temp1;
  1965. if (big1 != 0.f) {
  1966. entra += big1 * log(big1);
  1967. }
  1968. /* L1113: */
  1969. }
  1970. entra = -entra / log((real) (*n));
  1971. /* Now, SVA().^2/Trace(A^* * A) is a point in the probability simplex. */
  1972. /* It is derived from the diagonal of A^* * A. Do the same with the */
  1973. /* diagonal of A * A^*, compute the entropy of the corresponding */
  1974. /* probability distribution. Note that A * A^* and A^* * A have the */
  1975. /* same trace. */
  1976. entrat = 0.f;
  1977. i__1 = *m;
  1978. for (p = 1; p <= i__1; ++p) {
  1979. /* Computing 2nd power */
  1980. r__1 = rwork[p] / xsc;
  1981. big1 = r__1 * r__1 * temp1;
  1982. if (big1 != 0.f) {
  1983. entrat += big1 * log(big1);
  1984. }
  1985. /* L1114: */
  1986. }
  1987. entrat = -entrat / log((real) (*m));
  1988. /* Analyze the entropies and decide A or A^*. Smaller entropy */
  1989. /* usually means better input for the algorithm. */
  1990. transp = entrat < entra;
  1991. /* If A^* is better than A, take the adjoint of A. This is allowed */
  1992. /* only for square matrices, M=N. */
  1993. if (transp) {
  1994. /* In an optimal implementation, this trivial transpose */
  1995. /* should be replaced with faster transpose. */
  1996. i__1 = *n - 1;
  1997. for (p = 1; p <= i__1; ++p) {
  1998. i__2 = p + p * a_dim1;
  1999. r_cnjg(&q__1, &a[p + p * a_dim1]);
  2000. a[i__2].r = q__1.r, a[i__2].i = q__1.i;
  2001. i__2 = *n;
  2002. for (q = p + 1; q <= i__2; ++q) {
  2003. r_cnjg(&q__1, &a[q + p * a_dim1]);
  2004. ctemp.r = q__1.r, ctemp.i = q__1.i;
  2005. i__3 = q + p * a_dim1;
  2006. r_cnjg(&q__1, &a[p + q * a_dim1]);
  2007. a[i__3].r = q__1.r, a[i__3].i = q__1.i;
  2008. i__3 = p + q * a_dim1;
  2009. a[i__3].r = ctemp.r, a[i__3].i = ctemp.i;
  2010. /* L1116: */
  2011. }
  2012. /* L1115: */
  2013. }
  2014. i__1 = *n + *n * a_dim1;
  2015. r_cnjg(&q__1, &a[*n + *n * a_dim1]);
  2016. a[i__1].r = q__1.r, a[i__1].i = q__1.i;
  2017. i__1 = *n;
  2018. for (p = 1; p <= i__1; ++p) {
  2019. rwork[*m + p] = sva[p];
  2020. sva[p] = rwork[p];
  2021. /* previously computed row 2-norms are now column 2-norms */
  2022. /* of the transposed matrix */
  2023. /* L1117: */
  2024. }
  2025. temp1 = aapp;
  2026. aapp = aatmax;
  2027. aatmax = temp1;
  2028. temp1 = aaqq;
  2029. aaqq = aatmin;
  2030. aatmin = temp1;
  2031. kill = lsvec;
  2032. lsvec = rsvec;
  2033. rsvec = kill;
  2034. if (lsvec) {
  2035. n1 = *n;
  2036. }
  2037. rowpiv = TRUE_;
  2038. }
  2039. }
  2040. /* END IF L2TRAN */
  2041. /* Scale the matrix so that its maximal singular value remains less */
  2042. /* than SQRT(BIG) -- the matrix is scaled so that its maximal column */
  2043. /* has Euclidean norm equal to SQRT(BIG/N). The only reason to keep */
  2044. /* SQRT(BIG) instead of BIG is the fact that CGEJSV uses LAPACK and */
  2045. /* BLAS routines that, in some implementations, are not capable of */
  2046. /* working in the full interval [SFMIN,BIG] and that they may provoke */
  2047. /* overflows in the intermediate results. If the singular values spread */
  2048. /* from SFMIN to BIG, then CGESVJ will compute them. So, in that case, */
  2049. /* one should use CGESVJ instead of CGEJSV. */
  2050. big1 = sqrt(big);
  2051. temp1 = sqrt(big / (real) (*n));
  2052. /* >> for future updates: allow bigger range, i.e. the largest column */
  2053. /* will be allowed up to BIG/N and CGESVJ will do the rest. However, for */
  2054. /* this all other (LAPACK) components must allow such a range. */
  2055. /* TEMP1 = BIG/REAL(N) */
  2056. /* TEMP1 = BIG * EPSLN this should 'almost' work with current LAPACK components */
  2057. slascl_("G", &c__0, &c__0, &aapp, &temp1, n, &c__1, &sva[1], n, &ierr);
  2058. if (aaqq > aapp * sfmin) {
  2059. aaqq = aaqq / aapp * temp1;
  2060. } else {
  2061. aaqq = aaqq * temp1 / aapp;
  2062. }
  2063. temp1 *= scalem;
  2064. clascl_("G", &c__0, &c__0, &aapp, &temp1, m, n, &a[a_offset], lda, &ierr);
  2065. /* To undo scaling at the end of this procedure, multiply the */
  2066. /* computed singular values with USCAL2 / USCAL1. */
  2067. uscal1 = temp1;
  2068. uscal2 = aapp;
  2069. if (l2kill) {
  2070. /* L2KILL enforces computation of nonzero singular values in */
  2071. /* the restricted range of condition number of the initial A, */
  2072. /* sigma_max(A) / sigma_min(A) approx. SQRT(BIG)/SQRT(SFMIN). */
  2073. xsc = sqrt(sfmin);
  2074. } else {
  2075. xsc = small;
  2076. /* Now, if the condition number of A is too big, */
  2077. /* sigma_max(A) / sigma_min(A) .GT. SQRT(BIG/N) * EPSLN / SFMIN, */
  2078. /* as a precaution measure, the full SVD is computed using CGESVJ */
  2079. /* with accumulated Jacobi rotations. This provides numerically */
  2080. /* more robust computation, at the cost of slightly increased run */
  2081. /* time. Depending on the concrete implementation of BLAS and LAPACK */
  2082. /* (i.e. how they behave in presence of extreme ill-conditioning) the */
  2083. /* implementor may decide to remove this switch. */
  2084. if (aaqq < sqrt(sfmin) && lsvec && rsvec) {
  2085. jracc = TRUE_;
  2086. }
  2087. }
  2088. if (aaqq < xsc) {
  2089. i__1 = *n;
  2090. for (p = 1; p <= i__1; ++p) {
  2091. if (sva[p] < xsc) {
  2092. claset_("A", m, &c__1, &c_b1, &c_b1, &a[p * a_dim1 + 1], lda);
  2093. sva[p] = 0.f;
  2094. }
  2095. /* L700: */
  2096. }
  2097. }
  2098. /* Preconditioning using QR factorization with pivoting */
  2099. if (rowpiv) {
  2100. /* Optional row permutation (Bjoerck row pivoting): */
  2101. /* A result by Cox and Higham shows that the Bjoerck's */
  2102. /* row pivoting combined with standard column pivoting */
  2103. /* has similar effect as Powell-Reid complete pivoting. */
  2104. /* The ell-infinity norms of A are made nonincreasing. */
  2105. if (lsvec && rsvec && ! jracc) {
  2106. iwoff = *n << 1;
  2107. } else {
  2108. iwoff = *n;
  2109. }
  2110. i__1 = *m - 1;
  2111. for (p = 1; p <= i__1; ++p) {
  2112. i__2 = *m - p + 1;
  2113. q = isamax_(&i__2, &rwork[*m + p], &c__1) + p - 1;
  2114. iwork[iwoff + p] = q;
  2115. if (p != q) {
  2116. temp1 = rwork[*m + p];
  2117. rwork[*m + p] = rwork[*m + q];
  2118. rwork[*m + q] = temp1;
  2119. }
  2120. /* L1952: */
  2121. }
  2122. i__1 = *m - 1;
  2123. claswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[iwoff + 1], &c__1);
  2124. }
  2125. /* End of the preparation phase (scaling, optional sorting and */
  2126. /* transposing, optional flushing of small columns). */
  2127. /* Preconditioning */
  2128. /* If the full SVD is needed, the right singular vectors are computed */
  2129. /* from a matrix equation, and for that we need theoretical analysis */
  2130. /* of the Businger-Golub pivoting. So we use CGEQP3 as the first RR QRF. */
  2131. /* In all other cases the first RR QRF can be chosen by other criteria */
  2132. /* (eg speed by replacing global with restricted window pivoting, such */
  2133. /* as in xGEQPX from TOMS # 782). Good results will be obtained using */
  2134. /* xGEQPX with properly (!) chosen numerical parameters. */
  2135. /* Any improvement of CGEQP3 improves overal performance of CGEJSV. */
  2136. /* A * P1 = Q1 * [ R1^* 0]^*: */
  2137. i__1 = *n;
  2138. for (p = 1; p <= i__1; ++p) {
  2139. iwork[p] = 0;
  2140. /* L1963: */
  2141. }
  2142. i__1 = *lwork - *n;
  2143. cgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &cwork[1], &cwork[*n + 1], &
  2144. i__1, &rwork[1], &ierr);
  2145. /* The upper triangular matrix R1 from the first QRF is inspected for */
  2146. /* rank deficiency and possibilities for deflation, or possible */
  2147. /* ill-conditioning. Depending on the user specified flag L2RANK, */
  2148. /* the procedure explores possibilities to reduce the numerical */
  2149. /* rank by inspecting the computed upper triangular factor. If */
  2150. /* L2RANK or L2ABER are up, then CGEJSV will compute the SVD of */
  2151. /* A + dA, where ||dA|| <= f(M,N)*EPSLN. */
  2152. nr = 1;
  2153. if (l2aber) {
  2154. /* Standard absolute error bound suffices. All sigma_i with */
  2155. /* sigma_i < N*EPSLN*||A|| are flushed to zero. This is an */
  2156. /* aggressive enforcement of lower numerical rank by introducing a */
  2157. /* backward error of the order of N*EPSLN*||A||. */
  2158. temp1 = sqrt((real) (*n)) * epsln;
  2159. i__1 = *n;
  2160. for (p = 2; p <= i__1; ++p) {
  2161. if (c_abs(&a[p + p * a_dim1]) >= temp1 * c_abs(&a[a_dim1 + 1])) {
  2162. ++nr;
  2163. } else {
  2164. goto L3002;
  2165. }
  2166. /* L3001: */
  2167. }
  2168. L3002:
  2169. ;
  2170. } else if (l2rank) {
  2171. /* Sudden drop on the diagonal of R1 is used as the criterion for */
  2172. /* close-to-rank-deficient. */
  2173. temp1 = sqrt(sfmin);
  2174. i__1 = *n;
  2175. for (p = 2; p <= i__1; ++p) {
  2176. if (c_abs(&a[p + p * a_dim1]) < epsln * c_abs(&a[p - 1 + (p - 1) *
  2177. a_dim1]) || c_abs(&a[p + p * a_dim1]) < small || l2kill
  2178. && c_abs(&a[p + p * a_dim1]) < temp1) {
  2179. goto L3402;
  2180. }
  2181. ++nr;
  2182. /* L3401: */
  2183. }
  2184. L3402:
  2185. ;
  2186. } else {
  2187. /* The goal is high relative accuracy. However, if the matrix */
  2188. /* has high scaled condition number the relative accuracy is in */
  2189. /* general not feasible. Later on, a condition number estimator */
  2190. /* will be deployed to estimate the scaled condition number. */
  2191. /* Here we just remove the underflowed part of the triangular */
  2192. /* factor. This prevents the situation in which the code is */
  2193. /* working hard to get the accuracy not warranted by the data. */
  2194. temp1 = sqrt(sfmin);
  2195. i__1 = *n;
  2196. for (p = 2; p <= i__1; ++p) {
  2197. if (c_abs(&a[p + p * a_dim1]) < small || l2kill && c_abs(&a[p + p
  2198. * a_dim1]) < temp1) {
  2199. goto L3302;
  2200. }
  2201. ++nr;
  2202. /* L3301: */
  2203. }
  2204. L3302:
  2205. ;
  2206. }
  2207. almort = FALSE_;
  2208. if (nr == *n) {
  2209. maxprj = 1.f;
  2210. i__1 = *n;
  2211. for (p = 2; p <= i__1; ++p) {
  2212. temp1 = c_abs(&a[p + p * a_dim1]) / sva[iwork[p]];
  2213. maxprj = f2cmin(maxprj,temp1);
  2214. /* L3051: */
  2215. }
  2216. /* Computing 2nd power */
  2217. r__1 = maxprj;
  2218. if (r__1 * r__1 >= 1.f - (real) (*n) * epsln) {
  2219. almort = TRUE_;
  2220. }
  2221. }
  2222. sconda = -1.f;
  2223. condr1 = -1.f;
  2224. condr2 = -1.f;
  2225. if (errest) {
  2226. if (*n == nr) {
  2227. if (rsvec) {
  2228. clacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
  2229. i__1 = *n;
  2230. for (p = 1; p <= i__1; ++p) {
  2231. temp1 = sva[iwork[p]];
  2232. r__1 = 1.f / temp1;
  2233. csscal_(&p, &r__1, &v[p * v_dim1 + 1], &c__1);
  2234. /* L3053: */
  2235. }
  2236. if (lsvec) {
  2237. cpocon_("U", n, &v[v_offset], ldv, &c_b141, &temp1, &
  2238. cwork[*n + 1], &rwork[1], &ierr);
  2239. } else {
  2240. cpocon_("U", n, &v[v_offset], ldv, &c_b141, &temp1, &
  2241. cwork[1], &rwork[1], &ierr);
  2242. }
  2243. } else if (lsvec) {
  2244. clacpy_("U", n, n, &a[a_offset], lda, &u[u_offset], ldu);
  2245. i__1 = *n;
  2246. for (p = 1; p <= i__1; ++p) {
  2247. temp1 = sva[iwork[p]];
  2248. r__1 = 1.f / temp1;
  2249. csscal_(&p, &r__1, &u[p * u_dim1 + 1], &c__1);
  2250. /* L3054: */
  2251. }
  2252. cpocon_("U", n, &u[u_offset], ldu, &c_b141, &temp1, &cwork[*n
  2253. + 1], &rwork[1], &ierr);
  2254. } else {
  2255. clacpy_("U", n, n, &a[a_offset], lda, &cwork[1], n)
  2256. ;
  2257. /* [] CALL CLACPY( 'U', N, N, A, LDA, CWORK(N+1), N ) */
  2258. /* Change: here index shifted by N to the left, CWORK(1:N) */
  2259. /* not needed for SIGMA only computation */
  2260. i__1 = *n;
  2261. for (p = 1; p <= i__1; ++p) {
  2262. temp1 = sva[iwork[p]];
  2263. /* [] CALL CSSCAL( p, ONE/TEMP1, CWORK(N+(p-1)*N+1), 1 ) */
  2264. r__1 = 1.f / temp1;
  2265. csscal_(&p, &r__1, &cwork[(p - 1) * *n + 1], &c__1);
  2266. /* L3052: */
  2267. }
  2268. /* [] CALL CPOCON( 'U', N, CWORK(N+1), N, ONE, TEMP1, */
  2269. /* [] $ CWORK(N+N*N+1), RWORK, IERR ) */
  2270. cpocon_("U", n, &cwork[1], n, &c_b141, &temp1, &cwork[*n * *n
  2271. + 1], &rwork[1], &ierr);
  2272. }
  2273. if (temp1 != 0.f) {
  2274. sconda = 1.f / sqrt(temp1);
  2275. } else {
  2276. sconda = -1.f;
  2277. }
  2278. /* SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1). */
  2279. /* N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
  2280. } else {
  2281. sconda = -1.f;
  2282. }
  2283. }
  2284. c_div(&q__1, &a[a_dim1 + 1], &a[nr + nr * a_dim1]);
  2285. l2pert = l2pert && c_abs(&q__1) > sqrt(big1);
  2286. /* If there is no violent scaling, artificial perturbation is not needed. */
  2287. /* Phase 3: */
  2288. if (! (rsvec || lsvec)) {
  2289. /* Singular Values only */
  2290. /* Computing MIN */
  2291. i__2 = *n - 1;
  2292. i__1 = f2cmin(i__2,nr);
  2293. for (p = 1; p <= i__1; ++p) {
  2294. i__2 = *n - p;
  2295. ccopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p *
  2296. a_dim1], &c__1);
  2297. i__2 = *n - p + 1;
  2298. clacgv_(&i__2, &a[p + p * a_dim1], &c__1);
  2299. /* L1946: */
  2300. }
  2301. if (nr == *n) {
  2302. i__1 = *n + *n * a_dim1;
  2303. r_cnjg(&q__1, &a[*n + *n * a_dim1]);
  2304. a[i__1].r = q__1.r, a[i__1].i = q__1.i;
  2305. }
  2306. /* The following two DO-loops introduce small relative perturbation */
  2307. /* into the strict upper triangle of the lower triangular matrix. */
  2308. /* Small entries below the main diagonal are also changed. */
  2309. /* This modification is useful if the computing environment does not */
  2310. /* provide/allow FLUSH TO ZERO underflow, for it prevents many */
  2311. /* annoying denormalized numbers in case of strongly scaled matrices. */
  2312. /* The perturbation is structured so that it does not introduce any */
  2313. /* new perturbation of the singular values, and it does not destroy */
  2314. /* the job done by the preconditioner. */
  2315. /* The licence for this perturbation is in the variable L2PERT, which */
  2316. /* should be .FALSE. if FLUSH TO ZERO underflow is active. */
  2317. if (! almort) {
  2318. if (l2pert) {
  2319. /* XSC = SQRT(SMALL) */
  2320. xsc = epsln / (real) (*n);
  2321. i__1 = nr;
  2322. for (q = 1; q <= i__1; ++q) {
  2323. r__1 = xsc * c_abs(&a[q + q * a_dim1]);
  2324. q__1.r = r__1, q__1.i = 0.f;
  2325. ctemp.r = q__1.r, ctemp.i = q__1.i;
  2326. i__2 = *n;
  2327. for (p = 1; p <= i__2; ++p) {
  2328. if (p > q && c_abs(&a[p + q * a_dim1]) <= temp1 || p <
  2329. q) {
  2330. i__3 = p + q * a_dim1;
  2331. a[i__3].r = ctemp.r, a[i__3].i = ctemp.i;
  2332. }
  2333. /* $ A(p,q) = TEMP1 * ( A(p,q) / ABS(A(p,q)) ) */
  2334. /* L4949: */
  2335. }
  2336. /* L4947: */
  2337. }
  2338. } else {
  2339. i__1 = nr - 1;
  2340. i__2 = nr - 1;
  2341. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &a[(a_dim1 << 1) + 1]
  2342. , lda);
  2343. }
  2344. i__1 = *lwork - *n;
  2345. cgeqrf_(n, &nr, &a[a_offset], lda, &cwork[1], &cwork[*n + 1], &
  2346. i__1, &ierr);
  2347. i__1 = nr - 1;
  2348. for (p = 1; p <= i__1; ++p) {
  2349. i__2 = nr - p;
  2350. ccopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p *
  2351. a_dim1], &c__1);
  2352. i__2 = nr - p + 1;
  2353. clacgv_(&i__2, &a[p + p * a_dim1], &c__1);
  2354. /* L1948: */
  2355. }
  2356. }
  2357. /* Row-cyclic Jacobi SVD algorithm with column pivoting */
  2358. /* to drown denormals */
  2359. if (l2pert) {
  2360. /* XSC = SQRT(SMALL) */
  2361. xsc = epsln / (real) (*n);
  2362. i__1 = nr;
  2363. for (q = 1; q <= i__1; ++q) {
  2364. r__1 = xsc * c_abs(&a[q + q * a_dim1]);
  2365. q__1.r = r__1, q__1.i = 0.f;
  2366. ctemp.r = q__1.r, ctemp.i = q__1.i;
  2367. i__2 = nr;
  2368. for (p = 1; p <= i__2; ++p) {
  2369. if (p > q && c_abs(&a[p + q * a_dim1]) <= temp1 || p < q)
  2370. {
  2371. i__3 = p + q * a_dim1;
  2372. a[i__3].r = ctemp.r, a[i__3].i = ctemp.i;
  2373. }
  2374. /* $ A(p,q) = TEMP1 * ( A(p,q) / ABS(A(p,q)) ) */
  2375. /* L1949: */
  2376. }
  2377. /* L1947: */
  2378. }
  2379. } else {
  2380. i__1 = nr - 1;
  2381. i__2 = nr - 1;
  2382. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &a[(a_dim1 << 1) + 1],
  2383. lda);
  2384. }
  2385. /* triangular matrix (plus perturbation which is ignored in */
  2386. /* the part which destroys triangular form (confusing?!)) */
  2387. cgesvj_("L", "N", "N", &nr, &nr, &a[a_offset], lda, &sva[1], n, &v[
  2388. v_offset], ldv, &cwork[1], lwork, &rwork[1], lrwork, info);
  2389. scalem = rwork[1];
  2390. numrank = i_nint(&rwork[2]);
  2391. } else if (rsvec && ! lsvec && ! jracc || jracc && ! lsvec && nr != *n) {
  2392. /* -> Singular Values and Right Singular Vectors <- */
  2393. if (almort) {
  2394. i__1 = nr;
  2395. for (p = 1; p <= i__1; ++p) {
  2396. i__2 = *n - p + 1;
  2397. ccopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
  2398. c__1);
  2399. i__2 = *n - p + 1;
  2400. clacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  2401. /* L1998: */
  2402. }
  2403. i__1 = nr - 1;
  2404. i__2 = nr - 1;
  2405. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1],
  2406. ldv);
  2407. cgesvj_("L", "U", "N", n, &nr, &v[v_offset], ldv, &sva[1], &nr, &
  2408. a[a_offset], lda, &cwork[1], lwork, &rwork[1], lrwork,
  2409. info);
  2410. scalem = rwork[1];
  2411. numrank = i_nint(&rwork[2]);
  2412. } else {
  2413. /* accumulated product of Jacobi rotations, three are perfect ) */
  2414. i__1 = nr - 1;
  2415. i__2 = nr - 1;
  2416. claset_("L", &i__1, &i__2, &c_b1, &c_b1, &a[a_dim1 + 2], lda);
  2417. i__1 = *lwork - *n;
  2418. cgelqf_(&nr, n, &a[a_offset], lda, &cwork[1], &cwork[*n + 1], &
  2419. i__1, &ierr);
  2420. clacpy_("L", &nr, &nr, &a[a_offset], lda, &v[v_offset], ldv);
  2421. i__1 = nr - 1;
  2422. i__2 = nr - 1;
  2423. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1],
  2424. ldv);
  2425. i__1 = *lwork - (*n << 1);
  2426. cgeqrf_(&nr, &nr, &v[v_offset], ldv, &cwork[*n + 1], &cwork[(*n <<
  2427. 1) + 1], &i__1, &ierr);
  2428. i__1 = nr;
  2429. for (p = 1; p <= i__1; ++p) {
  2430. i__2 = nr - p + 1;
  2431. ccopy_(&i__2, &v[p + p * v_dim1], ldv, &v[p + p * v_dim1], &
  2432. c__1);
  2433. i__2 = nr - p + 1;
  2434. clacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  2435. /* L8998: */
  2436. }
  2437. i__1 = nr - 1;
  2438. i__2 = nr - 1;
  2439. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1],
  2440. ldv);
  2441. i__1 = *lwork - *n;
  2442. cgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[1], &nr,
  2443. &u[u_offset], ldu, &cwork[*n + 1], &i__1, &rwork[1],
  2444. lrwork, info);
  2445. scalem = rwork[1];
  2446. numrank = i_nint(&rwork[2]);
  2447. if (nr < *n) {
  2448. i__1 = *n - nr;
  2449. claset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + v_dim1],
  2450. ldv);
  2451. i__1 = *n - nr;
  2452. claset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * v_dim1 +
  2453. 1], ldv);
  2454. i__1 = *n - nr;
  2455. i__2 = *n - nr;
  2456. claset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (nr + 1)
  2457. * v_dim1], ldv);
  2458. }
  2459. i__1 = *lwork - *n;
  2460. cunmlq_("L", "C", n, n, &nr, &a[a_offset], lda, &cwork[1], &v[
  2461. v_offset], ldv, &cwork[*n + 1], &i__1, &ierr);
  2462. }
  2463. /* DO 8991 p = 1, N */
  2464. /* CALL CCOPY( N, V(p,1), LDV, A(IWORK(p),1), LDA ) */
  2465. /* 8991 CONTINUE */
  2466. /* CALL CLACPY( 'All', N, N, A, LDA, V, LDV ) */
  2467. clapmr_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  2468. if (transp) {
  2469. clacpy_("A", n, n, &v[v_offset], ldv, &u[u_offset], ldu);
  2470. }
  2471. } else if (jracc && ! lsvec && nr == *n) {
  2472. i__1 = *n - 1;
  2473. i__2 = *n - 1;
  2474. claset_("L", &i__1, &i__2, &c_b1, &c_b1, &a[a_dim1 + 2], lda);
  2475. cgesvj_("U", "N", "V", n, n, &a[a_offset], lda, &sva[1], n, &v[
  2476. v_offset], ldv, &cwork[1], lwork, &rwork[1], lrwork, info);
  2477. scalem = rwork[1];
  2478. numrank = i_nint(&rwork[2]);
  2479. clapmr_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  2480. } else if (lsvec && ! rsvec) {
  2481. /* Jacobi rotations in the Jacobi iterations. */
  2482. i__1 = nr;
  2483. for (p = 1; p <= i__1; ++p) {
  2484. i__2 = *n - p + 1;
  2485. ccopy_(&i__2, &a[p + p * a_dim1], lda, &u[p + p * u_dim1], &c__1);
  2486. i__2 = *n - p + 1;
  2487. clacgv_(&i__2, &u[p + p * u_dim1], &c__1);
  2488. /* L1965: */
  2489. }
  2490. i__1 = nr - 1;
  2491. i__2 = nr - 1;
  2492. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1], ldu);
  2493. i__1 = *lwork - (*n << 1);
  2494. cgeqrf_(n, &nr, &u[u_offset], ldu, &cwork[*n + 1], &cwork[(*n << 1) +
  2495. 1], &i__1, &ierr);
  2496. i__1 = nr - 1;
  2497. for (p = 1; p <= i__1; ++p) {
  2498. i__2 = nr - p;
  2499. ccopy_(&i__2, &u[p + (p + 1) * u_dim1], ldu, &u[p + 1 + p *
  2500. u_dim1], &c__1);
  2501. i__2 = *n - p + 1;
  2502. clacgv_(&i__2, &u[p + p * u_dim1], &c__1);
  2503. /* L1967: */
  2504. }
  2505. i__1 = nr - 1;
  2506. i__2 = nr - 1;
  2507. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1], ldu);
  2508. i__1 = *lwork - *n;
  2509. cgesvj_("L", "U", "N", &nr, &nr, &u[u_offset], ldu, &sva[1], &nr, &a[
  2510. a_offset], lda, &cwork[*n + 1], &i__1, &rwork[1], lrwork,
  2511. info);
  2512. scalem = rwork[1];
  2513. numrank = i_nint(&rwork[2]);
  2514. if (nr < *m) {
  2515. i__1 = *m - nr;
  2516. claset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1], ldu);
  2517. if (nr < n1) {
  2518. i__1 = n1 - nr;
  2519. claset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) * u_dim1 +
  2520. 1], ldu);
  2521. i__1 = *m - nr;
  2522. i__2 = n1 - nr;
  2523. claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (nr + 1)
  2524. * u_dim1], ldu);
  2525. }
  2526. }
  2527. i__1 = *lwork - *n;
  2528. cunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
  2529. u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
  2530. if (rowpiv) {
  2531. i__1 = *m - 1;
  2532. claswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[iwoff + 1], &
  2533. c_n1);
  2534. }
  2535. i__1 = n1;
  2536. for (p = 1; p <= i__1; ++p) {
  2537. xsc = 1.f / scnrm2_(m, &u[p * u_dim1 + 1], &c__1);
  2538. csscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  2539. /* L1974: */
  2540. }
  2541. if (transp) {
  2542. clacpy_("A", n, n, &u[u_offset], ldu, &v[v_offset], ldv);
  2543. }
  2544. } else {
  2545. if (! jracc) {
  2546. if (! almort) {
  2547. /* Second Preconditioning Step (QRF [with pivoting]) */
  2548. /* Note that the composition of TRANSPOSE, QRF and TRANSPOSE is */
  2549. /* equivalent to an LQF CALL. Since in many libraries the QRF */
  2550. /* seems to be better optimized than the LQF, we do explicit */
  2551. /* transpose and use the QRF. This is subject to changes in an */
  2552. /* optimized implementation of CGEJSV. */
  2553. i__1 = nr;
  2554. for (p = 1; p <= i__1; ++p) {
  2555. i__2 = *n - p + 1;
  2556. ccopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1],
  2557. &c__1);
  2558. i__2 = *n - p + 1;
  2559. clacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  2560. /* L1968: */
  2561. }
  2562. /* denormals in the second QR factorization, where they are */
  2563. /* as good as zeros. This is done to avoid painfully slow */
  2564. /* computation with denormals. The relative size of the perturbation */
  2565. /* is a parameter that can be changed by the implementer. */
  2566. /* This perturbation device will be obsolete on machines with */
  2567. /* properly implemented arithmetic. */
  2568. /* To switch it off, set L2PERT=.FALSE. To remove it from the */
  2569. /* code, remove the action under L2PERT=.TRUE., leave the ELSE part. */
  2570. /* The following two loops should be blocked and fused with the */
  2571. /* transposed copy above. */
  2572. if (l2pert) {
  2573. xsc = sqrt(small);
  2574. i__1 = nr;
  2575. for (q = 1; q <= i__1; ++q) {
  2576. r__1 = xsc * c_abs(&v[q + q * v_dim1]);
  2577. q__1.r = r__1, q__1.i = 0.f;
  2578. ctemp.r = q__1.r, ctemp.i = q__1.i;
  2579. i__2 = *n;
  2580. for (p = 1; p <= i__2; ++p) {
  2581. if (p > q && c_abs(&v[p + q * v_dim1]) <= temp1 ||
  2582. p < q) {
  2583. i__3 = p + q * v_dim1;
  2584. v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
  2585. }
  2586. /* $ V(p,q) = TEMP1 * ( V(p,q) / ABS(V(p,q)) ) */
  2587. if (p < q) {
  2588. i__3 = p + q * v_dim1;
  2589. i__4 = p + q * v_dim1;
  2590. q__1.r = -v[i__4].r, q__1.i = -v[i__4].i;
  2591. v[i__3].r = q__1.r, v[i__3].i = q__1.i;
  2592. }
  2593. /* L2968: */
  2594. }
  2595. /* L2969: */
  2596. }
  2597. } else {
  2598. i__1 = nr - 1;
  2599. i__2 = nr - 1;
  2600. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1)
  2601. + 1], ldv);
  2602. }
  2603. /* Estimate the row scaled condition number of R1 */
  2604. /* (If R1 is rectangular, N > NR, then the condition number */
  2605. /* of the leading NR x NR submatrix is estimated.) */
  2606. clacpy_("L", &nr, &nr, &v[v_offset], ldv, &cwork[(*n << 1) +
  2607. 1], &nr);
  2608. i__1 = nr;
  2609. for (p = 1; p <= i__1; ++p) {
  2610. i__2 = nr - p + 1;
  2611. temp1 = scnrm2_(&i__2, &cwork[(*n << 1) + (p - 1) * nr +
  2612. p], &c__1);
  2613. i__2 = nr - p + 1;
  2614. r__1 = 1.f / temp1;
  2615. csscal_(&i__2, &r__1, &cwork[(*n << 1) + (p - 1) * nr + p]
  2616. , &c__1);
  2617. /* L3950: */
  2618. }
  2619. cpocon_("L", &nr, &cwork[(*n << 1) + 1], &nr, &c_b141, &temp1,
  2620. &cwork[(*n << 1) + nr * nr + 1], &rwork[1], &ierr);
  2621. condr1 = 1.f / sqrt(temp1);
  2622. /* R1 is OK for inverse <=> CONDR1 .LT. REAL(N) */
  2623. /* more conservative <=> CONDR1 .LT. SQRT(REAL(N)) */
  2624. cond_ok__ = sqrt(sqrt((real) nr));
  2625. /* [TP] COND_OK is a tuning parameter. */
  2626. if (condr1 < cond_ok__) {
  2627. /* implementation, this QRF should be implemented as the QRF */
  2628. /* of a lower triangular matrix. */
  2629. /* R1^* = Q2 * R2 */
  2630. i__1 = *lwork - (*n << 1);
  2631. cgeqrf_(n, &nr, &v[v_offset], ldv, &cwork[*n + 1], &cwork[
  2632. (*n << 1) + 1], &i__1, &ierr);
  2633. if (l2pert) {
  2634. xsc = sqrt(small) / epsln;
  2635. i__1 = nr;
  2636. for (p = 2; p <= i__1; ++p) {
  2637. i__2 = p - 1;
  2638. for (q = 1; q <= i__2; ++q) {
  2639. /* Computing MIN */
  2640. r__2 = c_abs(&v[p + p * v_dim1]), r__3 =
  2641. c_abs(&v[q + q * v_dim1]);
  2642. r__1 = xsc * f2cmin(r__2,r__3);
  2643. q__1.r = r__1, q__1.i = 0.f;
  2644. ctemp.r = q__1.r, ctemp.i = q__1.i;
  2645. if (c_abs(&v[q + p * v_dim1]) <= temp1) {
  2646. i__3 = q + p * v_dim1;
  2647. v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
  2648. }
  2649. /* $ V(q,p) = TEMP1 * ( V(q,p) / ABS(V(q,p)) ) */
  2650. /* L3958: */
  2651. }
  2652. /* L3959: */
  2653. }
  2654. }
  2655. if (nr != *n) {
  2656. clacpy_("A", n, &nr, &v[v_offset], ldv, &cwork[(*n <<
  2657. 1) + 1], n);
  2658. }
  2659. i__1 = nr - 1;
  2660. for (p = 1; p <= i__1; ++p) {
  2661. i__2 = nr - p;
  2662. ccopy_(&i__2, &v[p + (p + 1) * v_dim1], ldv, &v[p + 1
  2663. + p * v_dim1], &c__1);
  2664. i__2 = nr - p + 1;
  2665. clacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  2666. /* L1969: */
  2667. }
  2668. i__1 = nr + nr * v_dim1;
  2669. r_cnjg(&q__1, &v[nr + nr * v_dim1]);
  2670. v[i__1].r = q__1.r, v[i__1].i = q__1.i;
  2671. condr2 = condr1;
  2672. } else {
  2673. /* Note that windowed pivoting would be equally good */
  2674. /* numerically, and more run-time efficient. So, in */
  2675. /* an optimal implementation, the next call to CGEQP3 */
  2676. /* should be replaced with eg. CALL CGEQPX (ACM TOMS #782) */
  2677. /* with properly (carefully) chosen parameters. */
  2678. /* R1^* * P2 = Q2 * R2 */
  2679. i__1 = nr;
  2680. for (p = 1; p <= i__1; ++p) {
  2681. iwork[*n + p] = 0;
  2682. /* L3003: */
  2683. }
  2684. i__1 = *lwork - (*n << 1);
  2685. cgeqp3_(n, &nr, &v[v_offset], ldv, &iwork[*n + 1], &cwork[
  2686. *n + 1], &cwork[(*n << 1) + 1], &i__1, &rwork[1],
  2687. &ierr);
  2688. /* * CALL CGEQRF( N, NR, V, LDV, CWORK(N+1), CWORK(2*N+1), */
  2689. /* * $ LWORK-2*N, IERR ) */
  2690. if (l2pert) {
  2691. xsc = sqrt(small);
  2692. i__1 = nr;
  2693. for (p = 2; p <= i__1; ++p) {
  2694. i__2 = p - 1;
  2695. for (q = 1; q <= i__2; ++q) {
  2696. /* Computing MIN */
  2697. r__2 = c_abs(&v[p + p * v_dim1]), r__3 =
  2698. c_abs(&v[q + q * v_dim1]);
  2699. r__1 = xsc * f2cmin(r__2,r__3);
  2700. q__1.r = r__1, q__1.i = 0.f;
  2701. ctemp.r = q__1.r, ctemp.i = q__1.i;
  2702. if (c_abs(&v[q + p * v_dim1]) <= temp1) {
  2703. i__3 = q + p * v_dim1;
  2704. v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
  2705. }
  2706. /* $ V(q,p) = TEMP1 * ( V(q,p) / ABS(V(q,p)) ) */
  2707. /* L3968: */
  2708. }
  2709. /* L3969: */
  2710. }
  2711. }
  2712. clacpy_("A", n, &nr, &v[v_offset], ldv, &cwork[(*n << 1)
  2713. + 1], n);
  2714. if (l2pert) {
  2715. xsc = sqrt(small);
  2716. i__1 = nr;
  2717. for (p = 2; p <= i__1; ++p) {
  2718. i__2 = p - 1;
  2719. for (q = 1; q <= i__2; ++q) {
  2720. /* Computing MIN */
  2721. r__2 = c_abs(&v[p + p * v_dim1]), r__3 =
  2722. c_abs(&v[q + q * v_dim1]);
  2723. r__1 = xsc * f2cmin(r__2,r__3);
  2724. q__1.r = r__1, q__1.i = 0.f;
  2725. ctemp.r = q__1.r, ctemp.i = q__1.i;
  2726. /* V(p,q) = - TEMP1*( V(q,p) / ABS(V(q,p)) ) */
  2727. i__3 = p + q * v_dim1;
  2728. q__1.r = -ctemp.r, q__1.i = -ctemp.i;
  2729. v[i__3].r = q__1.r, v[i__3].i = q__1.i;
  2730. /* L8971: */
  2731. }
  2732. /* L8970: */
  2733. }
  2734. } else {
  2735. i__1 = nr - 1;
  2736. i__2 = nr - 1;
  2737. claset_("L", &i__1, &i__2, &c_b1, &c_b1, &v[v_dim1 +
  2738. 2], ldv);
  2739. }
  2740. /* Now, compute R2 = L3 * Q3, the LQ factorization. */
  2741. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2742. cgelqf_(&nr, &nr, &v[v_offset], ldv, &cwork[(*n << 1) + *
  2743. n * nr + 1], &cwork[(*n << 1) + *n * nr + nr + 1],
  2744. &i__1, &ierr);
  2745. clacpy_("L", &nr, &nr, &v[v_offset], ldv, &cwork[(*n << 1)
  2746. + *n * nr + nr + 1], &nr);
  2747. i__1 = nr;
  2748. for (p = 1; p <= i__1; ++p) {
  2749. temp1 = scnrm2_(&p, &cwork[(*n << 1) + *n * nr + nr +
  2750. p], &nr);
  2751. r__1 = 1.f / temp1;
  2752. csscal_(&p, &r__1, &cwork[(*n << 1) + *n * nr + nr +
  2753. p], &nr);
  2754. /* L4950: */
  2755. }
  2756. cpocon_("L", &nr, &cwork[(*n << 1) + *n * nr + nr + 1], &
  2757. nr, &c_b141, &temp1, &cwork[(*n << 1) + *n * nr +
  2758. nr + nr * nr + 1], &rwork[1], &ierr);
  2759. condr2 = 1.f / sqrt(temp1);
  2760. if (condr2 >= cond_ok__) {
  2761. /* (this overwrites the copy of R2, as it will not be */
  2762. /* needed in this branch, but it does not overwritte the */
  2763. /* Huseholder vectors of Q2.). */
  2764. clacpy_("U", &nr, &nr, &v[v_offset], ldv, &cwork[(*n
  2765. << 1) + 1], n);
  2766. /* WORK(2*N+N*NR+1:2*N+N*NR+N) */
  2767. }
  2768. }
  2769. if (l2pert) {
  2770. xsc = sqrt(small);
  2771. i__1 = nr;
  2772. for (q = 2; q <= i__1; ++q) {
  2773. i__2 = q + q * v_dim1;
  2774. q__1.r = xsc * v[i__2].r, q__1.i = xsc * v[i__2].i;
  2775. ctemp.r = q__1.r, ctemp.i = q__1.i;
  2776. i__2 = q - 1;
  2777. for (p = 1; p <= i__2; ++p) {
  2778. /* V(p,q) = - TEMP1*( V(p,q) / ABS(V(p,q)) ) */
  2779. i__3 = p + q * v_dim1;
  2780. q__1.r = -ctemp.r, q__1.i = -ctemp.i;
  2781. v[i__3].r = q__1.r, v[i__3].i = q__1.i;
  2782. /* L4969: */
  2783. }
  2784. /* L4968: */
  2785. }
  2786. } else {
  2787. i__1 = nr - 1;
  2788. i__2 = nr - 1;
  2789. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1)
  2790. + 1], ldv);
  2791. }
  2792. /* Second preconditioning finished; continue with Jacobi SVD */
  2793. /* The input matrix is lower trinagular. */
  2794. /* Recover the right singular vectors as solution of a well */
  2795. /* conditioned triangular matrix equation. */
  2796. if (condr1 < cond_ok__) {
  2797. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2798. cgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
  2799. 1], &nr, &u[u_offset], ldu, &cwork[(*n << 1) + *n
  2800. * nr + nr + 1], &i__1, &rwork[1], lrwork, info);
  2801. scalem = rwork[1];
  2802. numrank = i_nint(&rwork[2]);
  2803. i__1 = nr;
  2804. for (p = 1; p <= i__1; ++p) {
  2805. ccopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1
  2806. + 1], &c__1);
  2807. csscal_(&nr, &sva[p], &v[p * v_dim1 + 1], &c__1);
  2808. /* L3970: */
  2809. }
  2810. if (nr == *n) {
  2811. /* :)) .. best case, R1 is inverted. The solution of this matrix */
  2812. /* equation is Q2*V2 = the product of the Jacobi rotations */
  2813. /* used in CGESVJ, premultiplied with the orthogonal matrix */
  2814. /* from the second QR factorization. */
  2815. ctrsm_("L", "U", "N", "N", &nr, &nr, &c_b2, &a[
  2816. a_offset], lda, &v[v_offset], ldv);
  2817. } else {
  2818. /* is inverted to get the product of the Jacobi rotations */
  2819. /* used in CGESVJ. The Q-factor from the second QR */
  2820. /* factorization is then built in explicitly. */
  2821. ctrsm_("L", "U", "C", "N", &nr, &nr, &c_b2, &cwork[(*
  2822. n << 1) + 1], n, &v[v_offset], ldv);
  2823. if (nr < *n) {
  2824. i__1 = *n - nr;
  2825. claset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1
  2826. + v_dim1], ldv);
  2827. i__1 = *n - nr;
  2828. claset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1)
  2829. * v_dim1 + 1], ldv);
  2830. i__1 = *n - nr;
  2831. i__2 = *n - nr;
  2832. claset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr +
  2833. 1 + (nr + 1) * v_dim1], ldv);
  2834. }
  2835. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2836. cunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n,
  2837. &cwork[*n + 1], &v[v_offset], ldv, &cwork[(*
  2838. n << 1) + *n * nr + nr + 1], &i__1, &ierr);
  2839. }
  2840. } else if (condr2 < cond_ok__) {
  2841. /* The matrix R2 is inverted. The solution of the matrix equation */
  2842. /* is Q3^* * V3 = the product of the Jacobi rotations (appplied to */
  2843. /* the lower triangular L3 from the LQ factorization of */
  2844. /* R2=L3*Q3), pre-multiplied with the transposed Q3. */
  2845. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2846. cgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
  2847. 1], &nr, &u[u_offset], ldu, &cwork[(*n << 1) + *n
  2848. * nr + nr + 1], &i__1, &rwork[1], lrwork, info);
  2849. scalem = rwork[1];
  2850. numrank = i_nint(&rwork[2]);
  2851. i__1 = nr;
  2852. for (p = 1; p <= i__1; ++p) {
  2853. ccopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1
  2854. + 1], &c__1);
  2855. csscal_(&nr, &sva[p], &u[p * u_dim1 + 1], &c__1);
  2856. /* L3870: */
  2857. }
  2858. ctrsm_("L", "U", "N", "N", &nr, &nr, &c_b2, &cwork[(*n <<
  2859. 1) + 1], n, &u[u_offset], ldu);
  2860. i__1 = nr;
  2861. for (q = 1; q <= i__1; ++q) {
  2862. i__2 = nr;
  2863. for (p = 1; p <= i__2; ++p) {
  2864. i__3 = (*n << 1) + *n * nr + nr + iwork[*n + p];
  2865. i__4 = p + q * u_dim1;
  2866. cwork[i__3].r = u[i__4].r, cwork[i__3].i = u[i__4]
  2867. .i;
  2868. /* L872: */
  2869. }
  2870. i__2 = nr;
  2871. for (p = 1; p <= i__2; ++p) {
  2872. i__3 = p + q * u_dim1;
  2873. i__4 = (*n << 1) + *n * nr + nr + p;
  2874. u[i__3].r = cwork[i__4].r, u[i__3].i = cwork[i__4]
  2875. .i;
  2876. /* L874: */
  2877. }
  2878. /* L873: */
  2879. }
  2880. if (nr < *n) {
  2881. i__1 = *n - nr;
  2882. claset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 +
  2883. v_dim1], ldv);
  2884. i__1 = *n - nr;
  2885. claset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) *
  2886. v_dim1 + 1], ldv);
  2887. i__1 = *n - nr;
  2888. i__2 = *n - nr;
  2889. claset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (
  2890. nr + 1) * v_dim1], ldv);
  2891. }
  2892. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2893. cunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n, &
  2894. cwork[*n + 1], &v[v_offset], ldv, &cwork[(*n << 1)
  2895. + *n * nr + nr + 1], &i__1, &ierr);
  2896. } else {
  2897. /* Last line of defense. */
  2898. /* #:( This is a rather pathological case: no scaled condition */
  2899. /* improvement after two pivoted QR factorizations. Other */
  2900. /* possibility is that the rank revealing QR factorization */
  2901. /* or the condition estimator has failed, or the COND_OK */
  2902. /* is set very close to ONE (which is unnecessary). Normally, */
  2903. /* this branch should never be executed, but in rare cases of */
  2904. /* failure of the RRQR or condition estimator, the last line of */
  2905. /* defense ensures that CGEJSV completes the task. */
  2906. /* Compute the full SVD of L3 using CGESVJ with explicit */
  2907. /* accumulation of Jacobi rotations. */
  2908. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2909. cgesvj_("L", "U", "V", &nr, &nr, &v[v_offset], ldv, &sva[
  2910. 1], &nr, &u[u_offset], ldu, &cwork[(*n << 1) + *n
  2911. * nr + nr + 1], &i__1, &rwork[1], lrwork, info);
  2912. scalem = rwork[1];
  2913. numrank = i_nint(&rwork[2]);
  2914. if (nr < *n) {
  2915. i__1 = *n - nr;
  2916. claset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 +
  2917. v_dim1], ldv);
  2918. i__1 = *n - nr;
  2919. claset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) *
  2920. v_dim1 + 1], ldv);
  2921. i__1 = *n - nr;
  2922. i__2 = *n - nr;
  2923. claset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (
  2924. nr + 1) * v_dim1], ldv);
  2925. }
  2926. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2927. cunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n, &
  2928. cwork[*n + 1], &v[v_offset], ldv, &cwork[(*n << 1)
  2929. + *n * nr + nr + 1], &i__1, &ierr);
  2930. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2931. cunmlq_("L", "C", &nr, &nr, &nr, &cwork[(*n << 1) + 1], n,
  2932. &cwork[(*n << 1) + *n * nr + 1], &u[u_offset],
  2933. ldu, &cwork[(*n << 1) + *n * nr + nr + 1], &i__1,
  2934. &ierr);
  2935. i__1 = nr;
  2936. for (q = 1; q <= i__1; ++q) {
  2937. i__2 = nr;
  2938. for (p = 1; p <= i__2; ++p) {
  2939. i__3 = (*n << 1) + *n * nr + nr + iwork[*n + p];
  2940. i__4 = p + q * u_dim1;
  2941. cwork[i__3].r = u[i__4].r, cwork[i__3].i = u[i__4]
  2942. .i;
  2943. /* L772: */
  2944. }
  2945. i__2 = nr;
  2946. for (p = 1; p <= i__2; ++p) {
  2947. i__3 = p + q * u_dim1;
  2948. i__4 = (*n << 1) + *n * nr + nr + p;
  2949. u[i__3].r = cwork[i__4].r, u[i__3].i = cwork[i__4]
  2950. .i;
  2951. /* L774: */
  2952. }
  2953. /* L773: */
  2954. }
  2955. }
  2956. /* Permute the rows of V using the (column) permutation from the */
  2957. /* first QRF. Also, scale the columns to make them unit in */
  2958. /* Euclidean norm. This applies to all cases. */
  2959. temp1 = sqrt((real) (*n)) * epsln;
  2960. i__1 = *n;
  2961. for (q = 1; q <= i__1; ++q) {
  2962. i__2 = *n;
  2963. for (p = 1; p <= i__2; ++p) {
  2964. i__3 = (*n << 1) + *n * nr + nr + iwork[p];
  2965. i__4 = p + q * v_dim1;
  2966. cwork[i__3].r = v[i__4].r, cwork[i__3].i = v[i__4].i;
  2967. /* L972: */
  2968. }
  2969. i__2 = *n;
  2970. for (p = 1; p <= i__2; ++p) {
  2971. i__3 = p + q * v_dim1;
  2972. i__4 = (*n << 1) + *n * nr + nr + p;
  2973. v[i__3].r = cwork[i__4].r, v[i__3].i = cwork[i__4].i;
  2974. /* L973: */
  2975. }
  2976. xsc = 1.f / scnrm2_(n, &v[q * v_dim1 + 1], &c__1);
  2977. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  2978. csscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
  2979. }
  2980. /* L1972: */
  2981. }
  2982. /* At this moment, V contains the right singular vectors of A. */
  2983. /* Next, assemble the left singular vector matrix U (M x N). */
  2984. if (nr < *m) {
  2985. i__1 = *m - nr;
  2986. claset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1]
  2987. , ldu);
  2988. if (nr < n1) {
  2989. i__1 = n1 - nr;
  2990. claset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) *
  2991. u_dim1 + 1], ldu);
  2992. i__1 = *m - nr;
  2993. i__2 = n1 - nr;
  2994. claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (
  2995. nr + 1) * u_dim1], ldu);
  2996. }
  2997. }
  2998. /* The Q matrix from the first QRF is built into the left singular */
  2999. /* matrix U. This applies to all cases. */
  3000. i__1 = *lwork - *n;
  3001. cunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
  3002. u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
  3003. /* The columns of U are normalized. The cost is O(M*N) flops. */
  3004. temp1 = sqrt((real) (*m)) * epsln;
  3005. i__1 = nr;
  3006. for (p = 1; p <= i__1; ++p) {
  3007. xsc = 1.f / scnrm2_(m, &u[p * u_dim1 + 1], &c__1);
  3008. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  3009. csscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  3010. }
  3011. /* L1973: */
  3012. }
  3013. /* If the initial QRF is computed with row pivoting, the left */
  3014. /* singular vectors must be adjusted. */
  3015. if (rowpiv) {
  3016. i__1 = *m - 1;
  3017. claswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[
  3018. iwoff + 1], &c_n1);
  3019. }
  3020. } else {
  3021. /* the second QRF is not needed */
  3022. clacpy_("U", n, n, &a[a_offset], lda, &cwork[*n + 1], n);
  3023. if (l2pert) {
  3024. xsc = sqrt(small);
  3025. i__1 = *n;
  3026. for (p = 2; p <= i__1; ++p) {
  3027. i__2 = *n + (p - 1) * *n + p;
  3028. q__1.r = xsc * cwork[i__2].r, q__1.i = xsc * cwork[
  3029. i__2].i;
  3030. ctemp.r = q__1.r, ctemp.i = q__1.i;
  3031. i__2 = p - 1;
  3032. for (q = 1; q <= i__2; ++q) {
  3033. /* CWORK(N+(q-1)*N+p)=-TEMP1 * ( CWORK(N+(p-1)*N+q) / */
  3034. /* $ ABS(CWORK(N+(p-1)*N+q)) ) */
  3035. i__3 = *n + (q - 1) * *n + p;
  3036. q__1.r = -ctemp.r, q__1.i = -ctemp.i;
  3037. cwork[i__3].r = q__1.r, cwork[i__3].i = q__1.i;
  3038. /* L5971: */
  3039. }
  3040. /* L5970: */
  3041. }
  3042. } else {
  3043. i__1 = *n - 1;
  3044. i__2 = *n - 1;
  3045. claset_("L", &i__1, &i__2, &c_b1, &c_b1, &cwork[*n + 2],
  3046. n);
  3047. }
  3048. i__1 = *lwork - *n - *n * *n;
  3049. cgesvj_("U", "U", "N", n, n, &cwork[*n + 1], n, &sva[1], n, &
  3050. u[u_offset], ldu, &cwork[*n + *n * *n + 1], &i__1, &
  3051. rwork[1], lrwork, info);
  3052. scalem = rwork[1];
  3053. numrank = i_nint(&rwork[2]);
  3054. i__1 = *n;
  3055. for (p = 1; p <= i__1; ++p) {
  3056. ccopy_(n, &cwork[*n + (p - 1) * *n + 1], &c__1, &u[p *
  3057. u_dim1 + 1], &c__1);
  3058. csscal_(n, &sva[p], &cwork[*n + (p - 1) * *n + 1], &c__1);
  3059. /* L6970: */
  3060. }
  3061. ctrsm_("L", "U", "N", "N", n, n, &c_b2, &a[a_offset], lda, &
  3062. cwork[*n + 1], n);
  3063. i__1 = *n;
  3064. for (p = 1; p <= i__1; ++p) {
  3065. ccopy_(n, &cwork[*n + p], n, &v[iwork[p] + v_dim1], ldv);
  3066. /* L6972: */
  3067. }
  3068. temp1 = sqrt((real) (*n)) * epsln;
  3069. i__1 = *n;
  3070. for (p = 1; p <= i__1; ++p) {
  3071. xsc = 1.f / scnrm2_(n, &v[p * v_dim1 + 1], &c__1);
  3072. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  3073. csscal_(n, &xsc, &v[p * v_dim1 + 1], &c__1);
  3074. }
  3075. /* L6971: */
  3076. }
  3077. /* Assemble the left singular vector matrix U (M x N). */
  3078. if (*n < *m) {
  3079. i__1 = *m - *n;
  3080. claset_("A", &i__1, n, &c_b1, &c_b1, &u[*n + 1 + u_dim1],
  3081. ldu);
  3082. if (*n < n1) {
  3083. i__1 = n1 - *n;
  3084. claset_("A", n, &i__1, &c_b1, &c_b1, &u[(*n + 1) *
  3085. u_dim1 + 1], ldu);
  3086. i__1 = *m - *n;
  3087. i__2 = n1 - *n;
  3088. claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[*n + 1 + (
  3089. *n + 1) * u_dim1], ldu);
  3090. }
  3091. }
  3092. i__1 = *lwork - *n;
  3093. cunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
  3094. u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
  3095. temp1 = sqrt((real) (*m)) * epsln;
  3096. i__1 = n1;
  3097. for (p = 1; p <= i__1; ++p) {
  3098. xsc = 1.f / scnrm2_(m, &u[p * u_dim1 + 1], &c__1);
  3099. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  3100. csscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  3101. }
  3102. /* L6973: */
  3103. }
  3104. if (rowpiv) {
  3105. i__1 = *m - 1;
  3106. claswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[
  3107. iwoff + 1], &c_n1);
  3108. }
  3109. }
  3110. /* end of the >> almost orthogonal case << in the full SVD */
  3111. } else {
  3112. /* This branch deploys a preconditioned Jacobi SVD with explicitly */
  3113. /* accumulated rotations. It is included as optional, mainly for */
  3114. /* experimental purposes. It does perform well, and can also be used. */
  3115. /* In this implementation, this branch will be automatically activated */
  3116. /* if the condition number sigma_max(A) / sigma_min(A) is predicted */
  3117. /* to be greater than the overflow threshold. This is because the */
  3118. /* a posteriori computation of the singular vectors assumes robust */
  3119. /* implementation of BLAS and some LAPACK procedures, capable of working */
  3120. /* in presence of extreme values, e.g. when the singular values spread from */
  3121. /* the underflow to the overflow threshold. */
  3122. i__1 = nr;
  3123. for (p = 1; p <= i__1; ++p) {
  3124. i__2 = *n - p + 1;
  3125. ccopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
  3126. c__1);
  3127. i__2 = *n - p + 1;
  3128. clacgv_(&i__2, &v[p + p * v_dim1], &c__1);
  3129. /* L7968: */
  3130. }
  3131. if (l2pert) {
  3132. xsc = sqrt(small / epsln);
  3133. i__1 = nr;
  3134. for (q = 1; q <= i__1; ++q) {
  3135. r__1 = xsc * c_abs(&v[q + q * v_dim1]);
  3136. q__1.r = r__1, q__1.i = 0.f;
  3137. ctemp.r = q__1.r, ctemp.i = q__1.i;
  3138. i__2 = *n;
  3139. for (p = 1; p <= i__2; ++p) {
  3140. if (p > q && c_abs(&v[p + q * v_dim1]) <= temp1 || p <
  3141. q) {
  3142. i__3 = p + q * v_dim1;
  3143. v[i__3].r = ctemp.r, v[i__3].i = ctemp.i;
  3144. }
  3145. /* $ V(p,q) = TEMP1 * ( V(p,q) / ABS(V(p,q)) ) */
  3146. if (p < q) {
  3147. i__3 = p + q * v_dim1;
  3148. i__4 = p + q * v_dim1;
  3149. q__1.r = -v[i__4].r, q__1.i = -v[i__4].i;
  3150. v[i__3].r = q__1.r, v[i__3].i = q__1.i;
  3151. }
  3152. /* L5968: */
  3153. }
  3154. /* L5969: */
  3155. }
  3156. } else {
  3157. i__1 = nr - 1;
  3158. i__2 = nr - 1;
  3159. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1]
  3160. , ldv);
  3161. }
  3162. i__1 = *lwork - (*n << 1);
  3163. cgeqrf_(n, &nr, &v[v_offset], ldv, &cwork[*n + 1], &cwork[(*n <<
  3164. 1) + 1], &i__1, &ierr);
  3165. clacpy_("L", n, &nr, &v[v_offset], ldv, &cwork[(*n << 1) + 1], n);
  3166. i__1 = nr;
  3167. for (p = 1; p <= i__1; ++p) {
  3168. i__2 = nr - p + 1;
  3169. ccopy_(&i__2, &v[p + p * v_dim1], ldv, &u[p + p * u_dim1], &
  3170. c__1);
  3171. i__2 = nr - p + 1;
  3172. clacgv_(&i__2, &u[p + p * u_dim1], &c__1);
  3173. /* L7969: */
  3174. }
  3175. if (l2pert) {
  3176. xsc = sqrt(small / epsln);
  3177. i__1 = nr;
  3178. for (q = 2; q <= i__1; ++q) {
  3179. i__2 = q - 1;
  3180. for (p = 1; p <= i__2; ++p) {
  3181. /* Computing MIN */
  3182. r__2 = c_abs(&u[p + p * u_dim1]), r__3 = c_abs(&u[q +
  3183. q * u_dim1]);
  3184. r__1 = xsc * f2cmin(r__2,r__3);
  3185. q__1.r = r__1, q__1.i = 0.f;
  3186. ctemp.r = q__1.r, ctemp.i = q__1.i;
  3187. /* U(p,q) = - TEMP1 * ( U(q,p) / ABS(U(q,p)) ) */
  3188. i__3 = p + q * u_dim1;
  3189. q__1.r = -ctemp.r, q__1.i = -ctemp.i;
  3190. u[i__3].r = q__1.r, u[i__3].i = q__1.i;
  3191. /* L9971: */
  3192. }
  3193. /* L9970: */
  3194. }
  3195. } else {
  3196. i__1 = nr - 1;
  3197. i__2 = nr - 1;
  3198. claset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1]
  3199. , ldu);
  3200. }
  3201. i__1 = *lwork - (*n << 1) - *n * nr;
  3202. cgesvj_("L", "U", "V", &nr, &nr, &u[u_offset], ldu, &sva[1], n, &
  3203. v[v_offset], ldv, &cwork[(*n << 1) + *n * nr + 1], &i__1,
  3204. &rwork[1], lrwork, info);
  3205. scalem = rwork[1];
  3206. numrank = i_nint(&rwork[2]);
  3207. if (nr < *n) {
  3208. i__1 = *n - nr;
  3209. claset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + v_dim1],
  3210. ldv);
  3211. i__1 = *n - nr;
  3212. claset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * v_dim1 +
  3213. 1], ldv);
  3214. i__1 = *n - nr;
  3215. i__2 = *n - nr;
  3216. claset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (nr + 1)
  3217. * v_dim1], ldv);
  3218. }
  3219. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  3220. cunmqr_("L", "N", n, n, &nr, &cwork[(*n << 1) + 1], n, &cwork[*n
  3221. + 1], &v[v_offset], ldv, &cwork[(*n << 1) + *n * nr + nr
  3222. + 1], &i__1, &ierr);
  3223. /* Permute the rows of V using the (column) permutation from the */
  3224. /* first QRF. Also, scale the columns to make them unit in */
  3225. /* Euclidean norm. This applies to all cases. */
  3226. temp1 = sqrt((real) (*n)) * epsln;
  3227. i__1 = *n;
  3228. for (q = 1; q <= i__1; ++q) {
  3229. i__2 = *n;
  3230. for (p = 1; p <= i__2; ++p) {
  3231. i__3 = (*n << 1) + *n * nr + nr + iwork[p];
  3232. i__4 = p + q * v_dim1;
  3233. cwork[i__3].r = v[i__4].r, cwork[i__3].i = v[i__4].i;
  3234. /* L8972: */
  3235. }
  3236. i__2 = *n;
  3237. for (p = 1; p <= i__2; ++p) {
  3238. i__3 = p + q * v_dim1;
  3239. i__4 = (*n << 1) + *n * nr + nr + p;
  3240. v[i__3].r = cwork[i__4].r, v[i__3].i = cwork[i__4].i;
  3241. /* L8973: */
  3242. }
  3243. xsc = 1.f / scnrm2_(n, &v[q * v_dim1 + 1], &c__1);
  3244. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  3245. csscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
  3246. }
  3247. /* L7972: */
  3248. }
  3249. /* At this moment, V contains the right singular vectors of A. */
  3250. /* Next, assemble the left singular vector matrix U (M x N). */
  3251. if (nr < *m) {
  3252. i__1 = *m - nr;
  3253. claset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1],
  3254. ldu);
  3255. if (nr < n1) {
  3256. i__1 = n1 - nr;
  3257. claset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) *
  3258. u_dim1 + 1], ldu);
  3259. i__1 = *m - nr;
  3260. i__2 = n1 - nr;
  3261. claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (nr
  3262. + 1) * u_dim1], ldu);
  3263. }
  3264. }
  3265. i__1 = *lwork - *n;
  3266. cunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
  3267. u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
  3268. if (rowpiv) {
  3269. i__1 = *m - 1;
  3270. claswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[iwoff +
  3271. 1], &c_n1);
  3272. }
  3273. }
  3274. if (transp) {
  3275. i__1 = *n;
  3276. for (p = 1; p <= i__1; ++p) {
  3277. cswap_(n, &u[p * u_dim1 + 1], &c__1, &v[p * v_dim1 + 1], &
  3278. c__1);
  3279. /* L6974: */
  3280. }
  3281. }
  3282. }
  3283. /* end of the full SVD */
  3284. /* Undo scaling, if necessary (and possible) */
  3285. if (uscal2 <= big / sva[1] * uscal1) {
  3286. slascl_("G", &c__0, &c__0, &uscal1, &uscal2, &nr, &c__1, &sva[1], n, &
  3287. ierr);
  3288. uscal1 = 1.f;
  3289. uscal2 = 1.f;
  3290. }
  3291. if (nr < *n) {
  3292. i__1 = *n;
  3293. for (p = nr + 1; p <= i__1; ++p) {
  3294. sva[p] = 0.f;
  3295. /* L3004: */
  3296. }
  3297. }
  3298. rwork[1] = uscal2 * scalem;
  3299. rwork[2] = uscal1;
  3300. if (errest) {
  3301. rwork[3] = sconda;
  3302. }
  3303. if (lsvec && rsvec) {
  3304. rwork[4] = condr1;
  3305. rwork[5] = condr2;
  3306. }
  3307. if (l2tran) {
  3308. rwork[6] = entra;
  3309. rwork[7] = entrat;
  3310. }
  3311. iwork[1] = nr;
  3312. iwork[2] = numrank;
  3313. iwork[3] = warning;
  3314. if (transp) {
  3315. iwork[4] = 1;
  3316. } else {
  3317. iwork[4] = -1;
  3318. }
  3319. return;
  3320. } /* cgejsv_ */