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