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.

sgejsv.c 94 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <complex.h>
  6. #ifdef complex
  7. #undef complex
  8. #endif
  9. #ifdef I
  10. #undef I
  11. #endif
  12. #if defined(_WIN64)
  13. typedef long long BLASLONG;
  14. typedef unsigned long long BLASULONG;
  15. #else
  16. typedef long BLASLONG;
  17. typedef unsigned long BLASULONG;
  18. #endif
  19. #ifdef LAPACK_ILP64
  20. typedef BLASLONG blasint;
  21. #if defined(_WIN64)
  22. #define blasabs(x) llabs(x)
  23. #else
  24. #define blasabs(x) labs(x)
  25. #endif
  26. #else
  27. typedef int blasint;
  28. #define blasabs(x) abs(x)
  29. #endif
  30. typedef blasint integer;
  31. typedef unsigned int uinteger;
  32. typedef char *address;
  33. typedef short int shortint;
  34. typedef float real;
  35. typedef double doublereal;
  36. typedef struct { real r, i; } complex;
  37. typedef struct { doublereal r, i; } doublecomplex;
  38. #ifdef _MSC_VER
  39. static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
  40. static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
  41. static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
  42. static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
  43. #else
  44. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  45. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  46. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  47. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  48. #endif
  49. #define pCf(z) (*_pCf(z))
  50. #define pCd(z) (*_pCd(z))
  51. typedef int logical;
  52. typedef short int shortlogical;
  53. typedef char logical1;
  54. typedef char integer1;
  55. #define TRUE_ (1)
  56. #define FALSE_ (0)
  57. /* Extern is for use with -E */
  58. #ifndef Extern
  59. #define Extern extern
  60. #endif
  61. /* I/O stuff */
  62. typedef int flag;
  63. typedef int ftnlen;
  64. typedef int ftnint;
  65. /*external read, write*/
  66. typedef struct
  67. { flag cierr;
  68. ftnint ciunit;
  69. flag ciend;
  70. char *cifmt;
  71. ftnint cirec;
  72. } cilist;
  73. /*internal read, write*/
  74. typedef struct
  75. { flag icierr;
  76. char *iciunit;
  77. flag iciend;
  78. char *icifmt;
  79. ftnint icirlen;
  80. ftnint icirnum;
  81. } icilist;
  82. /*open*/
  83. typedef struct
  84. { flag oerr;
  85. ftnint ounit;
  86. char *ofnm;
  87. ftnlen ofnmlen;
  88. char *osta;
  89. char *oacc;
  90. char *ofm;
  91. ftnint orl;
  92. char *oblnk;
  93. } olist;
  94. /*close*/
  95. typedef struct
  96. { flag cerr;
  97. ftnint cunit;
  98. char *csta;
  99. } cllist;
  100. /*rewind, backspace, endfile*/
  101. typedef struct
  102. { flag aerr;
  103. ftnint aunit;
  104. } alist;
  105. /* inquire */
  106. typedef struct
  107. { flag inerr;
  108. ftnint inunit;
  109. char *infile;
  110. ftnlen infilen;
  111. ftnint *inex; /*parameters in standard's order*/
  112. ftnint *inopen;
  113. ftnint *innum;
  114. ftnint *innamed;
  115. char *inname;
  116. ftnlen innamlen;
  117. char *inacc;
  118. ftnlen inacclen;
  119. char *inseq;
  120. ftnlen inseqlen;
  121. char *indir;
  122. ftnlen indirlen;
  123. char *infmt;
  124. ftnlen infmtlen;
  125. char *inform;
  126. ftnint informlen;
  127. char *inunf;
  128. ftnlen inunflen;
  129. ftnint *inrecl;
  130. ftnint *innrec;
  131. char *inblank;
  132. ftnlen inblanklen;
  133. } inlist;
  134. #define VOID void
  135. union Multitype { /* for multiple entry points */
  136. integer1 g;
  137. shortint h;
  138. integer i;
  139. /* longint j; */
  140. real r;
  141. doublereal d;
  142. complex c;
  143. doublecomplex z;
  144. };
  145. typedef union Multitype Multitype;
  146. struct Vardesc { /* for Namelist */
  147. char *name;
  148. char *addr;
  149. ftnlen *dims;
  150. int type;
  151. };
  152. typedef struct Vardesc Vardesc;
  153. struct Namelist {
  154. char *name;
  155. Vardesc **vars;
  156. int nvars;
  157. };
  158. typedef struct Namelist Namelist;
  159. #define abs(x) ((x) >= 0 ? (x) : -(x))
  160. #define dabs(x) (fabs(x))
  161. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  162. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  163. #define dmin(a,b) (f2cmin(a,b))
  164. #define dmax(a,b) (f2cmax(a,b))
  165. #define bit_test(a,b) ((a) >> (b) & 1)
  166. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  167. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  168. #define abort_() { sig_die("Fortran abort routine called", 1); }
  169. #define c_abs(z) (cabsf(Cf(z)))
  170. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  171. #ifdef _MSC_VER
  172. #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
  173. #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
  174. #else
  175. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  176. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  177. #endif
  178. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  179. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  180. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  181. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  182. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  183. #define d_abs(x) (fabs(*(x)))
  184. #define d_acos(x) (acos(*(x)))
  185. #define d_asin(x) (asin(*(x)))
  186. #define d_atan(x) (atan(*(x)))
  187. #define d_atn2(x, y) (atan2(*(x),*(y)))
  188. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  189. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  190. #define d_cos(x) (cos(*(x)))
  191. #define d_cosh(x) (cosh(*(x)))
  192. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  193. #define d_exp(x) (exp(*(x)))
  194. #define d_imag(z) (cimag(Cd(z)))
  195. #define r_imag(z) (cimagf(Cf(z)))
  196. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  198. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  200. #define d_log(x) (log(*(x)))
  201. #define d_mod(x, y) (fmod(*(x), *(y)))
  202. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  203. #define d_nint(x) u_nint(*(x))
  204. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  205. #define d_sign(a,b) u_sign(*(a),*(b))
  206. #define r_sign(a,b) u_sign(*(a),*(b))
  207. #define d_sin(x) (sin(*(x)))
  208. #define d_sinh(x) (sinh(*(x)))
  209. #define d_sqrt(x) (sqrt(*(x)))
  210. #define d_tan(x) (tan(*(x)))
  211. #define d_tanh(x) (tanh(*(x)))
  212. #define i_abs(x) abs(*(x))
  213. #define i_dnnt(x) ((integer)u_nint(*(x)))
  214. #define i_len(s, n) (n)
  215. #define i_nint(x) ((integer)u_nint(*(x)))
  216. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  217. #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
  218. #define pow_si(B,E) spow_ui(*(B),*(E))
  219. #define pow_ri(B,E) spow_ui(*(B),*(E))
  220. #define pow_di(B,E) dpow_ui(*(B),*(E))
  221. #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
  222. #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
  223. #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
  224. #define s_cat(lpp, rpp, rnp, np, llp) { ftnlen i, nc, ll; char *f__rp, *lp; ll = (llp); lp = (lpp); for(i=0; i < (int)*(np); ++i) { nc = ll; if((rnp)[i] < nc) nc = (rnp)[i]; ll -= nc; f__rp = (rpp)[i]; while(--nc >= 0) *lp++ = *(f__rp)++; } while(--ll >= 0) *lp++ = ' '; }
  225. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  226. #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
  227. #define sig_die(s, kill) { exit(1); }
  228. #define s_stop(s, n) {exit(0);}
  229. static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
  230. #define z_abs(z) (cabs(Cd(z)))
  231. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  232. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  233. #define myexit_() break;
  234. #define mycycle() continue;
  235. #define myceiling(w) {ceil(w)}
  236. #define myhuge(w) {HUGE_VAL}
  237. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  238. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  239. /* procedure parameter types for -A and -C++ */
  240. #define F2C_proc_par_types 1
  241. #ifdef __cplusplus
  242. typedef logical (*L_fp)(...);
  243. #else
  244. typedef logical (*L_fp)();
  245. #endif
  246. static float spow_ui(float x, integer n) {
  247. float pow=1.0; unsigned long int u;
  248. if(n != 0) {
  249. if(n < 0) n = -n, x = 1/x;
  250. for(u = n; ; ) {
  251. if(u & 01) pow *= x;
  252. if(u >>= 1) x *= x;
  253. else break;
  254. }
  255. }
  256. return pow;
  257. }
  258. static double dpow_ui(double x, integer n) {
  259. double pow=1.0; unsigned long int u;
  260. if(n != 0) {
  261. if(n < 0) n = -n, x = 1/x;
  262. for(u = n; ; ) {
  263. if(u & 01) pow *= x;
  264. if(u >>= 1) x *= x;
  265. else break;
  266. }
  267. }
  268. return pow;
  269. }
  270. #ifdef _MSC_VER
  271. static _Fcomplex cpow_ui(complex x, integer n) {
  272. complex pow={1.0,0.0}; unsigned long int u;
  273. if(n != 0) {
  274. if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
  275. for(u = n; ; ) {
  276. if(u & 01) pow.r *= x.r, pow.i *= x.i;
  277. if(u >>= 1) x.r *= x.r, x.i *= x.i;
  278. else break;
  279. }
  280. }
  281. _Fcomplex p={pow.r, pow.i};
  282. return p;
  283. }
  284. #else
  285. static _Complex float cpow_ui(_Complex float x, integer n) {
  286. _Complex float pow=1.0; unsigned long int u;
  287. if(n != 0) {
  288. if(n < 0) n = -n, x = 1/x;
  289. for(u = n; ; ) {
  290. if(u & 01) pow *= x;
  291. if(u >>= 1) x *= x;
  292. else break;
  293. }
  294. }
  295. return pow;
  296. }
  297. #endif
  298. #ifdef _MSC_VER
  299. static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
  300. _Dcomplex pow={1.0,0.0}; unsigned long int u;
  301. if(n != 0) {
  302. if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
  303. for(u = n; ; ) {
  304. if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
  305. if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
  306. else break;
  307. }
  308. }
  309. _Dcomplex p = {pow._Val[0], pow._Val[1]};
  310. return p;
  311. }
  312. #else
  313. static _Complex double zpow_ui(_Complex double x, integer n) {
  314. _Complex double pow=1.0; unsigned long int u;
  315. if(n != 0) {
  316. if(n < 0) n = -n, x = 1/x;
  317. for(u = n; ; ) {
  318. if(u & 01) pow *= x;
  319. if(u >>= 1) x *= x;
  320. else break;
  321. }
  322. }
  323. return pow;
  324. }
  325. #endif
  326. static integer pow_ii(integer x, integer n) {
  327. integer pow; unsigned long int u;
  328. if (n <= 0) {
  329. if (n == 0 || x == 1) pow = 1;
  330. else if (x != -1) pow = x == 0 ? 1/x : 0;
  331. else n = -n;
  332. }
  333. if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
  334. u = n;
  335. for(pow = 1; ; ) {
  336. if(u & 01) pow *= x;
  337. if(u >>= 1) x *= x;
  338. else break;
  339. }
  340. }
  341. return pow;
  342. }
  343. static integer dmaxloc_(double *w, integer s, integer e, integer *n)
  344. {
  345. double m; integer i, mi;
  346. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  347. if (w[i-1]>m) mi=i ,m=w[i-1];
  348. return mi-s+1;
  349. }
  350. static integer smaxloc_(float *w, integer s, integer e, integer *n)
  351. {
  352. float m; integer i, mi;
  353. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  354. if (w[i-1]>m) mi=i ,m=w[i-1];
  355. return mi-s+1;
  356. }
  357. static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  358. integer n = *n_, incx = *incx_, incy = *incy_, i;
  359. #ifdef _MSC_VER
  360. _Fcomplex zdotc = {0.0, 0.0};
  361. if (incx == 1 && incy == 1) {
  362. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  363. zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
  364. zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
  365. }
  366. } else {
  367. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  368. zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
  369. zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
  370. }
  371. }
  372. pCf(z) = zdotc;
  373. }
  374. #else
  375. _Complex float zdotc = 0.0;
  376. if (incx == 1 && incy == 1) {
  377. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  378. zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
  379. }
  380. } else {
  381. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  382. zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
  383. }
  384. }
  385. pCf(z) = zdotc;
  386. }
  387. #endif
  388. static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  389. integer n = *n_, incx = *incx_, incy = *incy_, i;
  390. #ifdef _MSC_VER
  391. _Dcomplex zdotc = {0.0, 0.0};
  392. if (incx == 1 && incy == 1) {
  393. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  394. zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
  395. zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
  396. }
  397. } else {
  398. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  399. zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
  400. zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
  401. }
  402. }
  403. pCd(z) = zdotc;
  404. }
  405. #else
  406. _Complex double zdotc = 0.0;
  407. if (incx == 1 && incy == 1) {
  408. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  409. zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
  410. }
  411. } else {
  412. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  413. zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
  414. }
  415. }
  416. pCd(z) = zdotc;
  417. }
  418. #endif
  419. static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  420. integer n = *n_, incx = *incx_, incy = *incy_, i;
  421. #ifdef _MSC_VER
  422. _Fcomplex zdotc = {0.0, 0.0};
  423. if (incx == 1 && incy == 1) {
  424. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  425. zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
  426. zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
  427. }
  428. } else {
  429. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  430. zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
  431. zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
  432. }
  433. }
  434. pCf(z) = zdotc;
  435. }
  436. #else
  437. _Complex float zdotc = 0.0;
  438. if (incx == 1 && incy == 1) {
  439. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  440. zdotc += Cf(&x[i]) * Cf(&y[i]);
  441. }
  442. } else {
  443. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  444. zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
  445. }
  446. }
  447. pCf(z) = zdotc;
  448. }
  449. #endif
  450. static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  451. integer n = *n_, incx = *incx_, incy = *incy_, i;
  452. #ifdef _MSC_VER
  453. _Dcomplex zdotc = {0.0, 0.0};
  454. if (incx == 1 && incy == 1) {
  455. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  456. zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
  457. zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
  458. }
  459. } else {
  460. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  461. zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
  462. zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
  463. }
  464. }
  465. pCd(z) = zdotc;
  466. }
  467. #else
  468. _Complex double zdotc = 0.0;
  469. if (incx == 1 && incy == 1) {
  470. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  471. zdotc += Cd(&x[i]) * Cd(&y[i]);
  472. }
  473. } else {
  474. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  475. zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
  476. }
  477. }
  478. pCd(z) = zdotc;
  479. }
  480. #endif
  481. /* -- translated by f2c (version 20000121).
  482. You must link the resulting object file with the libraries:
  483. -lf2c -lm (in that order)
  484. */
  485. /* Table of constant values */
  486. static integer c__1 = 1;
  487. static real c_b34 = 0.f;
  488. static real c_b35 = 1.f;
  489. static integer c__0 = 0;
  490. static integer c_n1 = -1;
  491. /* > \brief \b SGEJSV */
  492. /* =========== DOCUMENTATION =========== */
  493. /* Online html documentation available at */
  494. /* http://www.netlib.org/lapack/explore-html/ */
  495. /* > \htmlonly */
  496. /* > Download SGEJSV + dependencies */
  497. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgejsv.
  498. f"> */
  499. /* > [TGZ]</a> */
  500. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgejsv.
  501. f"> */
  502. /* > [ZIP]</a> */
  503. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgejsv.
  504. f"> */
  505. /* > [TXT]</a> */
  506. /* > \endhtmlonly */
  507. /* Definition: */
  508. /* =========== */
  509. /* SUBROUTINE SGEJSV( JOBA, JOBU, JOBV, JOBR, JOBT, JOBP, */
  510. /* M, N, A, LDA, SVA, U, LDU, V, LDV, */
  511. /* WORK, LWORK, IWORK, INFO ) */
  512. /* IMPLICIT NONE */
  513. /* INTEGER INFO, LDA, LDU, LDV, LWORK, M, N */
  514. /* REAL A( LDA, * ), SVA( N ), U( LDU, * ), V( LDV, * ), */
  515. /* $ WORK( LWORK ) */
  516. /* INTEGER IWORK( * ) */
  517. /* CHARACTER*1 JOBA, JOBP, JOBR, JOBT, JOBU, JOBV */
  518. /* > \par Purpose: */
  519. /* ============= */
  520. /* > */
  521. /* > \verbatim */
  522. /* > */
  523. /* > SGEJSV computes the singular value decomposition (SVD) of a real M-by-N */
  524. /* > matrix [A], where M >= N. The SVD of [A] is written as */
  525. /* > */
  526. /* > [A] = [U] * [SIGMA] * [V]^t, */
  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) orthonormal matrix, and */
  530. /* > [V] is an N-by-N orthogonal 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. /* > SGEJSV can sometimes compute tiny singular values and their singular vectors much */
  536. /* > more accurately than other SVD routines, see below under Further Details. */
  537. /* > \endverbatim */
  538. /* Arguments: */
  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=D*B. If A has heavily weighted */
  566. /* > rows, then using this condition number gives too pessimistic */
  567. /* > error bound. */
  568. /* > = 'A': Small singular values are the noise and the matrix is treated */
  569. /* > as numerically rank deficient. The error in the computed */
  570. /* > singular values is bounded by f(m,n)*epsilon*||A||. */
  571. /* > The computed SVD A = U * S * V^t restores A up to */
  572. /* > f(m,n)*epsilon*||A||. */
  573. /* > This gives the procedure the licence to discard (set to zero) */
  574. /* > all singular values below N*epsilon*||A||. */
  575. /* > = 'R': Similar as in 'A'. Rank revealing property of the initial */
  576. /* > QR factorization is used do reveal (using triangular factor) */
  577. /* > a gap sigma_{r+1} < epsilon * sigma_r in which case the */
  578. /* > numerical RANK is declared to be r. The SVD is computed with */
  579. /* > absolute error bounds, but more accurately than with 'A'. */
  580. /* > \endverbatim */
  581. /* > */
  582. /* > \param[in] JOBU */
  583. /* > \verbatim */
  584. /* > JOBU is CHARACTER*1 */
  585. /* > Specifies whether to compute the columns of U: */
  586. /* > = 'U': N columns of U are returned in the array U. */
  587. /* > = 'F': full set of M left sing. vectors is returned in the array U. */
  588. /* > = 'W': U may be used as workspace of length M*N. See the description */
  589. /* > of U. */
  590. /* > = 'N': U is not computed. */
  591. /* > \endverbatim */
  592. /* > */
  593. /* > \param[in] JOBV */
  594. /* > \verbatim */
  595. /* > JOBV is CHARACTER*1 */
  596. /* > Specifies whether to compute the matrix V: */
  597. /* > = 'V': N columns of V are returned in the array V; Jacobi rotations */
  598. /* > are not explicitly accumulated. */
  599. /* > = 'J': N columns of V are returned in the array V, but they are */
  600. /* > computed as the product of Jacobi rotations. This option is */
  601. /* > allowed only if JOBU .NE. 'N', i.e. in computing the full SVD. */
  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 SGESVJ. */
  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 SGESVJ. */
  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^t seems to be better with respect to convergence. */
  633. /* > If the matrix is not square, JOBT is ignored. This is subject to */
  634. /* > changes in the future. */
  635. /* > The decision is based on two values of entropy over the adjoint */
  636. /* > orbit of A^t * A. See the descriptions of WORK(6) and WORK(7). */
  637. /* > = 'T': transpose if entropy test indicates possibly faster */
  638. /* > convergence of Jacobi process if A^t is taken as input. If A is */
  639. /* > replaced with A^t, then the row pivoting is included automatically. */
  640. /* > = 'N': do not speculate. */
  641. /* > This option can be used to compute only the singular values, or the */
  642. /* > full SVD (U, SIGMA and V). For only one set of singular vectors */
  643. /* > (U or V), the caller should provide both U and V, as one of the */
  644. /* > matrices is used as workspace if the matrix A is transposed. */
  645. /* > The implementer can easily remove this constraint and make the */
  646. /* > code more complicated. See the descriptions of U and V. */
  647. /* > \endverbatim */
  648. /* > */
  649. /* > \param[in] JOBP */
  650. /* > \verbatim */
  651. /* > JOBP is CHARACTER*1 */
  652. /* > Issues the licence to introduce structured perturbations to drown */
  653. /* > denormalized numbers. This licence should be active if the */
  654. /* > denormals are poorly implemented, causing slow computation, */
  655. /* > especially in cases of fast convergence (!). For details see [1,2]. */
  656. /* > For the sake of simplicity, this perturbations are included only */
  657. /* > when the full SVD or only the singular values are requested. The */
  658. /* > implementer/user can easily add the perturbation for the cases of */
  659. /* > computing one set of singular vectors. */
  660. /* > = 'P': introduce perturbation */
  661. /* > = 'N': do not perturb */
  662. /* > \endverbatim */
  663. /* > */
  664. /* > \param[in] M */
  665. /* > \verbatim */
  666. /* > M is INTEGER */
  667. /* > The number of rows of the input matrix A. M >= 0. */
  668. /* > \endverbatim */
  669. /* > */
  670. /* > \param[in] N */
  671. /* > \verbatim */
  672. /* > N is INTEGER */
  673. /* > The number of columns of the input matrix A. M >= N >= 0. */
  674. /* > \endverbatim */
  675. /* > */
  676. /* > \param[in,out] A */
  677. /* > \verbatim */
  678. /* > A is REAL array, dimension (LDA,N) */
  679. /* > On entry, the M-by-N matrix A. */
  680. /* > \endverbatim */
  681. /* > */
  682. /* > \param[in] LDA */
  683. /* > \verbatim */
  684. /* > LDA is INTEGER */
  685. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  686. /* > \endverbatim */
  687. /* > */
  688. /* > \param[out] SVA */
  689. /* > \verbatim */
  690. /* > SVA is REAL array, dimension (N) */
  691. /* > On exit, */
  692. /* > - For WORK(1)/WORK(2) = ONE: The singular values of A. During the */
  693. /* > computation SVA contains Euclidean column norms of the */
  694. /* > iterated matrices in the array A. */
  695. /* > - For WORK(1) .NE. WORK(2): The singular values of A are */
  696. /* > (WORK(1)/WORK(2)) * SVA(1:N). This factored form is used if */
  697. /* > sigma_max(A) overflows or if small singular values have been */
  698. /* > saved from underflow by scaling the input matrix A. */
  699. /* > - If JOBR='R' then some of the singular values may be returned */
  700. /* > as exact zeros obtained by "set to zero" because they are */
  701. /* > below the numerical rank threshold or are denormalized numbers. */
  702. /* > \endverbatim */
  703. /* > */
  704. /* > \param[out] U */
  705. /* > \verbatim */
  706. /* > U is REAL array, dimension ( LDU, N ) */
  707. /* > If JOBU = 'U', then U contains on exit the M-by-N matrix of */
  708. /* > the left singular vectors. */
  709. /* > If JOBU = 'F', then U contains on exit the M-by-M matrix of */
  710. /* > the left singular vectors, including an ONB */
  711. /* > of the orthogonal complement of the Range(A). */
  712. /* > If JOBU = 'W' .AND. (JOBV = 'V' .AND. JOBT = 'T' .AND. M = N), */
  713. /* > then U is used as workspace if the procedure */
  714. /* > replaces A with A^t. In that case, [V] is computed */
  715. /* > in U as left singular vectors of A^t and then */
  716. /* > copied back to the V array. This 'W' option is just */
  717. /* > a reminder to the caller that in this case U is */
  718. /* > reserved as workspace of length N*N. */
  719. /* > If JOBU = 'N' U is not referenced, unless JOBT='T'. */
  720. /* > \endverbatim */
  721. /* > */
  722. /* > \param[in] LDU */
  723. /* > \verbatim */
  724. /* > LDU is INTEGER */
  725. /* > The leading dimension of the array U, LDU >= 1. */
  726. /* > IF JOBU = 'U' or 'F' or 'W', then LDU >= M. */
  727. /* > \endverbatim */
  728. /* > */
  729. /* > \param[out] V */
  730. /* > \verbatim */
  731. /* > V is REAL array, dimension ( LDV, N ) */
  732. /* > If JOBV = 'V', 'J' then V contains on exit the N-by-N matrix of */
  733. /* > the right singular vectors; */
  734. /* > If JOBV = 'W', AND (JOBU = 'U' AND JOBT = 'T' AND M = N), */
  735. /* > then V is used as workspace if the pprocedure */
  736. /* > replaces A with A^t. In that case, [U] is computed */
  737. /* > in V as right singular vectors of A^t and then */
  738. /* > copied back to the U array. This 'W' option is just */
  739. /* > a reminder to the caller that in this case V is */
  740. /* > reserved as workspace of length N*N. */
  741. /* > If JOBV = 'N' V is not referenced, unless JOBT='T'. */
  742. /* > \endverbatim */
  743. /* > */
  744. /* > \param[in] LDV */
  745. /* > \verbatim */
  746. /* > LDV is INTEGER */
  747. /* > The leading dimension of the array V, LDV >= 1. */
  748. /* > If JOBV = 'V' or 'J' or 'W', then LDV >= N. */
  749. /* > \endverbatim */
  750. /* > */
  751. /* > \param[out] WORK */
  752. /* > \verbatim */
  753. /* > WORK is REAL array, dimension (LWORK) */
  754. /* > On exit, */
  755. /* > WORK(1) = SCALE = WORK(2) / WORK(1) is the scaling factor such */
  756. /* > that SCALE*SVA(1:N) are the computed singular values */
  757. /* > of A. (See the description of SVA().) */
  758. /* > WORK(2) = See the description of WORK(1). */
  759. /* > WORK(3) = SCONDA is an estimate for the condition number of */
  760. /* > column equilibrated A. (If JOBA = 'E' or 'G') */
  761. /* > SCONDA is an estimate of SQRT(||(R^t * R)^(-1)||_1). */
  762. /* > It is computed using SPOCON. It holds */
  763. /* > N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
  764. /* > where R is the triangular factor from the QRF of A. */
  765. /* > However, if R is truncated and the numerical rank is */
  766. /* > determined to be strictly smaller than N, SCONDA is */
  767. /* > returned as -1, thus indicating that the smallest */
  768. /* > singular values might be lost. */
  769. /* > */
  770. /* > If full SVD is needed, the following two condition numbers are */
  771. /* > useful for the analysis of the algorithm. They are provied for */
  772. /* > a developer/implementer who is familiar with the details of */
  773. /* > the method. */
  774. /* > */
  775. /* > WORK(4) = an estimate of the scaled condition number of the */
  776. /* > triangular factor in the first QR factorization. */
  777. /* > WORK(5) = an estimate of the scaled condition number of the */
  778. /* > triangular factor in the second QR factorization. */
  779. /* > The following two parameters are computed if JOBT = 'T'. */
  780. /* > They are provided for a developer/implementer who is familiar */
  781. /* > with the details of the method. */
  782. /* > */
  783. /* > WORK(6) = the entropy of A^t*A :: this is the Shannon entropy */
  784. /* > of diag(A^t*A) / Trace(A^t*A) taken as point in the */
  785. /* > probability simplex. */
  786. /* > WORK(7) = the entropy of A*A^t. */
  787. /* > \endverbatim */
  788. /* > */
  789. /* > \param[in] LWORK */
  790. /* > \verbatim */
  791. /* > LWORK is INTEGER */
  792. /* > Length of WORK to confirm proper allocation of work space. */
  793. /* > LWORK depends on the job: */
  794. /* > */
  795. /* > If only SIGMA is needed ( JOBU = 'N', JOBV = 'N' ) and */
  796. /* > -> .. no scaled condition estimate required (JOBE = 'N'): */
  797. /* > LWORK >= f2cmax(2*M+N,4*N+1,7). This is the minimal requirement. */
  798. /* > ->> For optimal performance (blocked code) the optimal value */
  799. /* > is LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB,7). Here NB is the optimal */
  800. /* > block size for DGEQP3 and DGEQRF. */
  801. /* > In general, optimal LWORK is computed as */
  802. /* > LWORK >= f2cmax(2*M+N,N+LWORK(DGEQP3),N+LWORK(DGEQRF), 7). */
  803. /* > -> .. an estimate of the scaled condition number of A is */
  804. /* > required (JOBA='E', 'G'). In this case, LWORK is the maximum */
  805. /* > of the above and N*N+4*N, i.e. LWORK >= f2cmax(2*M+N,N*N+4*N,7). */
  806. /* > ->> For optimal performance (blocked code) the optimal value */
  807. /* > is LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB, N*N+4*N, 7). */
  808. /* > In general, the optimal length LWORK is computed as */
  809. /* > LWORK >= f2cmax(2*M+N,N+LWORK(DGEQP3),N+LWORK(DGEQRF), */
  810. /* > N+N*N+LWORK(DPOCON),7). */
  811. /* > */
  812. /* > If SIGMA and the right singular vectors are needed (JOBV = 'V'), */
  813. /* > -> the minimal requirement is LWORK >= f2cmax(2*M+N,4*N+1,7). */
  814. /* > -> For optimal performance, LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB,7), */
  815. /* > where NB is the optimal block size for DGEQP3, DGEQRF, DGELQ, */
  816. /* > DORMLQ. In general, the optimal length LWORK is computed as */
  817. /* > LWORK >= f2cmax(2*M+N,N+LWORK(DGEQP3), N+LWORK(DPOCON), */
  818. /* > N+LWORK(DGELQ), 2*N+LWORK(DGEQRF), N+LWORK(DORMLQ)). */
  819. /* > */
  820. /* > If SIGMA and the left singular vectors are needed */
  821. /* > -> the minimal requirement is LWORK >= f2cmax(2*M+N,4*N+1,7). */
  822. /* > -> For optimal performance: */
  823. /* > if JOBU = 'U' :: LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB,7), */
  824. /* > if JOBU = 'F' :: LWORK >= f2cmax(2*M+N,3*N+(N+1)*NB,N+M*NB,7), */
  825. /* > where NB is the optimal block size for DGEQP3, DGEQRF, DORMQR. */
  826. /* > In general, the optimal length LWORK is computed as */
  827. /* > LWORK >= f2cmax(2*M+N,N+LWORK(DGEQP3),N+LWORK(DPOCON), */
  828. /* > 2*N+LWORK(DGEQRF), N+LWORK(DORMQR)). */
  829. /* > Here LWORK(DORMQR) equals N*NB (for JOBU = 'U') or */
  830. /* > M*NB (for JOBU = 'F'). */
  831. /* > */
  832. /* > If the full SVD is needed: (JOBU = 'U' or JOBU = 'F') and */
  833. /* > -> if JOBV = 'V' */
  834. /* > the minimal requirement is LWORK >= f2cmax(2*M+N,6*N+2*N*N). */
  835. /* > -> if JOBV = 'J' the minimal requirement is */
  836. /* > LWORK >= f2cmax(2*M+N, 4*N+N*N,2*N+N*N+6). */
  837. /* > -> For optimal performance, LWORK should be additionally */
  838. /* > larger than N+M*NB, where NB is the optimal block size */
  839. /* > for DORMQR. */
  840. /* > \endverbatim */
  841. /* > */
  842. /* > \param[out] IWORK */
  843. /* > \verbatim */
  844. /* > IWORK is INTEGER array, dimension (M+3*N). */
  845. /* > On exit, */
  846. /* > IWORK(1) = the numerical rank determined after the initial */
  847. /* > QR factorization with pivoting. See the descriptions */
  848. /* > of JOBA and JOBR. */
  849. /* > IWORK(2) = the number of the computed nonzero singular values */
  850. /* > IWORK(3) = if nonzero, a warning message: */
  851. /* > If IWORK(3) = 1 then some of the column norms of A */
  852. /* > were denormalized floats. The requested high accuracy */
  853. /* > is not warranted by the data. */
  854. /* > \endverbatim */
  855. /* > */
  856. /* > \param[out] INFO */
  857. /* > \verbatim */
  858. /* > INFO is INTEGER */
  859. /* > < 0: if INFO = -i, then the i-th argument had an illegal value. */
  860. /* > = 0: successful exit; */
  861. /* > > 0: SGEJSV did not converge in the maximal allowed number */
  862. /* > of sweeps. The computed values may be inaccurate. */
  863. /* > \endverbatim */
  864. /* Authors: */
  865. /* ======== */
  866. /* > \author Univ. of Tennessee */
  867. /* > \author Univ. of California Berkeley */
  868. /* > \author Univ. of Colorado Denver */
  869. /* > \author NAG Ltd. */
  870. /* > \date June 2016 */
  871. /* > \ingroup realGEsing */
  872. /* > \par Further Details: */
  873. /* ===================== */
  874. /* > */
  875. /* > \verbatim */
  876. /* > */
  877. /* > SGEJSV implements a preconditioned Jacobi SVD algorithm. It uses SGEQP3, */
  878. /* > SGEQRF, and SGELQF as preprocessors and preconditioners. Optionally, an */
  879. /* > additional row pivoting can be used as a preprocessor, which in some */
  880. /* > cases results in much higher accuracy. An example is matrix A with the */
  881. /* > structure A = D1 * C * D2, where D1, D2 are arbitrarily ill-conditioned */
  882. /* > diagonal matrices and C is well-conditioned matrix. In that case, complete */
  883. /* > pivoting in the first QR factorizations provides accuracy dependent on the */
  884. /* > condition number of C, and independent of D1, D2. Such higher accuracy is */
  885. /* > not completely understood theoretically, but it works well in practice. */
  886. /* > Further, if A can be written as A = B*D, with well-conditioned B and some */
  887. /* > diagonal D, then the high accuracy is guaranteed, both theoretically and */
  888. /* > in software, independent of D. For more details see [1], [2]. */
  889. /* > The computational range for the singular values can be the full range */
  890. /* > ( UNDERFLOW,OVERFLOW ), provided that the machine arithmetic and the BLAS */
  891. /* > & LAPACK routines called by SGEJSV are implemented to work in that range. */
  892. /* > If that is not the case, then the restriction for safe computation with */
  893. /* > the singular values in the range of normalized IEEE numbers is that the */
  894. /* > spectral condition number kappa(A)=sigma_max(A)/sigma_min(A) does not */
  895. /* > overflow. This code (SGEJSV) is best used in this restricted range, */
  896. /* > meaning that singular values of magnitude below ||A||_2 / SLAMCH('O') are */
  897. /* > returned as zeros. See JOBR for details on this. */
  898. /* > Further, this implementation is somewhat slower than the one described */
  899. /* > in [1,2] due to replacement of some non-LAPACK components, and because */
  900. /* > the choice of some tuning parameters in the iterative part (SGESVJ) is */
  901. /* > left to the implementer on a particular machine. */
  902. /* > The rank revealing QR factorization (in this code: SGEQP3) should be */
  903. /* > implemented as in [3]. We have a new version of SGEQP3 under development */
  904. /* > that is more robust than the current one in LAPACK, with a cleaner cut in */
  905. /* > rank deficient cases. It will be available in the SIGMA library [4]. */
  906. /* > If M is much larger than N, it is obvious that the initial QRF with */
  907. /* > column pivoting can be preprocessed by the QRF without pivoting. That */
  908. /* > well known trick is not used in SGEJSV because in some cases heavy row */
  909. /* > weighting can be treated with complete pivoting. The overhead in cases */
  910. /* > M much larger than N is then only due to pivoting, but the benefits in */
  911. /* > terms of accuracy have prevailed. The implementer/user can incorporate */
  912. /* > this extra QRF step easily. The implementer can also improve data movement */
  913. /* > (matrix transpose, matrix copy, matrix transposed copy) - this */
  914. /* > implementation of SGEJSV uses only the simplest, naive data movement. */
  915. /* > \endverbatim */
  916. /* > \par Contributors: */
  917. /* ================== */
  918. /* > */
  919. /* > Zlatko Drmac (Zagreb, Croatia) and Kresimir Veselic (Hagen, Germany) */
  920. /* > \par References: */
  921. /* ================ */
  922. /* > */
  923. /* > \verbatim */
  924. /* > */
  925. /* > [1] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I. */
  926. /* > SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342. */
  927. /* > LAPACK Working note 169. */
  928. /* > [2] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II. */
  929. /* > SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362. */
  930. /* > LAPACK Working note 170. */
  931. /* > [3] Z. Drmac and Z. Bujanovic: On the failure of rank-revealing QR */
  932. /* > factorization software - a case study. */
  933. /* > ACM Trans. Math. Softw. Vol. 35, No 2 (2008), pp. 1-28. */
  934. /* > LAPACK Working note 176. */
  935. /* > [4] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV, */
  936. /* > QSVD, (H,K)-SVD computations. */
  937. /* > Department of Mathematics, University of Zagreb, 2008. */
  938. /* > \endverbatim */
  939. /* > \par Bugs, examples and comments: */
  940. /* ================================= */
  941. /* > */
  942. /* > Please report all bugs and send interesting examples and/or comments to */
  943. /* > drmac@math.hr. Thank you. */
  944. /* > */
  945. /* ===================================================================== */
  946. /* Subroutine */ int sgejsv_(char *joba, char *jobu, char *jobv, char *jobr,
  947. char *jobt, char *jobp, integer *m, integer *n, real *a, integer *lda,
  948. real *sva, real *u, integer *ldu, real *v, integer *ldv, real *work,
  949. integer *lwork, integer *iwork, integer *info)
  950. {
  951. /* System generated locals */
  952. integer a_dim1, a_offset, u_dim1, u_offset, v_dim1, v_offset, i__1, i__2,
  953. i__3, i__4, i__5, i__6, i__7, i__8, i__9, i__10, i__11, i__12;
  954. real r__1, r__2, r__3, r__4;
  955. /* Local variables */
  956. logical defr;
  957. real aapp, aaqq;
  958. logical kill;
  959. integer ierr;
  960. real temp1;
  961. extern real snrm2_(integer *, real *, integer *);
  962. integer p, q;
  963. logical jracc;
  964. extern logical lsame_(char *, char *);
  965. extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
  966. real small, entra, sfmin;
  967. logical lsvec;
  968. real epsln;
  969. logical rsvec;
  970. extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *,
  971. integer *);
  972. integer n1;
  973. extern /* Subroutine */ int sswap_(integer *, real *, integer *, real *,
  974. integer *);
  975. logical l2aber;
  976. extern /* Subroutine */ int strsm_(char *, char *, char *, char *,
  977. integer *, integer *, real *, real *, integer *, real *, integer *
  978. );
  979. real condr1, condr2, uscal1, uscal2;
  980. logical l2kill, l2rank, l2tran;
  981. extern /* Subroutine */ int sgeqp3_(integer *, integer *, real *, integer
  982. *, integer *, real *, real *, integer *, integer *);
  983. logical l2pert;
  984. integer nr;
  985. real scalem, sconda;
  986. logical goscal;
  987. real aatmin;
  988. extern real slamch_(char *);
  989. real aatmax;
  990. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  991. logical noscal;
  992. extern /* Subroutine */ int sgelqf_(integer *, integer *, real *, integer
  993. *, real *, real *, integer *, integer *);
  994. extern integer isamax_(integer *, real *, integer *);
  995. extern /* Subroutine */ int slascl_(char *, integer *, integer *, real *,
  996. real *, integer *, integer *, real *, integer *, integer *), sgeqrf_(integer *, integer *, real *, integer *, real *,
  997. real *, integer *, integer *), slacpy_(char *, integer *, integer
  998. *, real *, integer *, real *, integer *), slaset_(char *,
  999. integer *, integer *, real *, real *, real *, integer *);
  1000. real entrat;
  1001. logical almort;
  1002. real maxprj;
  1003. extern /* Subroutine */ int spocon_(char *, integer *, real *, integer *,
  1004. real *, real *, real *, integer *, integer *);
  1005. logical errest;
  1006. extern /* Subroutine */ int sgesvj_(char *, char *, char *, integer *,
  1007. integer *, real *, integer *, real *, integer *, real *, integer *
  1008. , real *, integer *, integer *), slassq_(
  1009. integer *, real *, integer *, real *, real *);
  1010. logical transp;
  1011. extern /* Subroutine */ int slaswp_(integer *, real *, integer *, integer
  1012. *, integer *, integer *, integer *), sorgqr_(integer *, integer *,
  1013. integer *, real *, integer *, real *, real *, integer *, integer
  1014. *), sormlq_(char *, char *, integer *, integer *, integer *, real
  1015. *, integer *, real *, real *, integer *, real *, integer *,
  1016. integer *), sormqr_(char *, char *, integer *,
  1017. integer *, integer *, real *, integer *, real *, real *, integer *
  1018. , real *, integer *, integer *);
  1019. logical rowpiv;
  1020. real big, cond_ok__, xsc, big1;
  1021. integer warning, numrank;
  1022. /* -- LAPACK computational routine (version 3.7.1) -- */
  1023. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  1024. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  1025. /* June 2016 */
  1026. /* =========================================================================== */
  1027. /* Test the input arguments */
  1028. /* Parameter adjustments */
  1029. --sva;
  1030. a_dim1 = *lda;
  1031. a_offset = 1 + a_dim1 * 1;
  1032. a -= a_offset;
  1033. u_dim1 = *ldu;
  1034. u_offset = 1 + u_dim1 * 1;
  1035. u -= u_offset;
  1036. v_dim1 = *ldv;
  1037. v_offset = 1 + v_dim1 * 1;
  1038. v -= v_offset;
  1039. --work;
  1040. --iwork;
  1041. /* Function Body */
  1042. lsvec = lsame_(jobu, "U") || lsame_(jobu, "F");
  1043. jracc = lsame_(jobv, "J");
  1044. rsvec = lsame_(jobv, "V") || jracc;
  1045. rowpiv = lsame_(joba, "F") || lsame_(joba, "G");
  1046. l2rank = lsame_(joba, "R");
  1047. l2aber = lsame_(joba, "A");
  1048. errest = lsame_(joba, "E") || lsame_(joba, "G");
  1049. l2tran = lsame_(jobt, "T");
  1050. l2kill = lsame_(jobr, "R");
  1051. defr = lsame_(jobr, "N");
  1052. l2pert = lsame_(jobp, "P");
  1053. if (! (rowpiv || l2rank || l2aber || errest || lsame_(joba, "C"))) {
  1054. *info = -1;
  1055. } else if (! (lsvec || lsame_(jobu, "N") || lsame_(
  1056. jobu, "W"))) {
  1057. *info = -2;
  1058. } else if (! (rsvec || lsame_(jobv, "N") || lsame_(
  1059. jobv, "W")) || jracc && ! lsvec) {
  1060. *info = -3;
  1061. } else if (! (l2kill || defr)) {
  1062. *info = -4;
  1063. } else if (! (l2tran || lsame_(jobt, "N"))) {
  1064. *info = -5;
  1065. } else if (! (l2pert || lsame_(jobp, "N"))) {
  1066. *info = -6;
  1067. } else if (*m < 0) {
  1068. *info = -7;
  1069. } else if (*n < 0 || *n > *m) {
  1070. *info = -8;
  1071. } else if (*lda < *m) {
  1072. *info = -10;
  1073. } else if (lsvec && *ldu < *m) {
  1074. *info = -13;
  1075. } else if (rsvec && *ldv < *n) {
  1076. *info = -15;
  1077. } else /* if(complicated condition) */ {
  1078. /* Computing MAX */
  1079. i__1 = 7, i__2 = (*n << 2) + 1, i__1 = f2cmax(i__1,i__2), i__2 = (*m <<
  1080. 1) + *n;
  1081. /* Computing MAX */
  1082. i__3 = 7, i__4 = (*n << 2) + *n * *n, i__3 = f2cmax(i__3,i__4), i__4 = (*
  1083. m << 1) + *n;
  1084. /* Computing MAX */
  1085. i__5 = 7, i__6 = (*m << 1) + *n, i__5 = f2cmax(i__5,i__6), i__6 = (*n <<
  1086. 2) + 1;
  1087. /* Computing MAX */
  1088. i__7 = 7, i__8 = (*m << 1) + *n, i__7 = f2cmax(i__7,i__8), i__8 = (*n <<
  1089. 2) + 1;
  1090. /* Computing MAX */
  1091. i__9 = (*m << 1) + *n, i__10 = *n * 6 + (*n << 1) * *n;
  1092. /* Computing MAX */
  1093. i__11 = (*m << 1) + *n, i__12 = (*n << 2) + *n * *n, i__11 = f2cmax(
  1094. i__11,i__12), i__12 = (*n << 1) + *n * *n + 6;
  1095. if (! (lsvec || rsvec || errest) && *lwork < f2cmax(i__1,i__2) || ! (
  1096. lsvec || rsvec) && errest && *lwork < f2cmax(i__3,i__4) || lsvec
  1097. && ! rsvec && *lwork < f2cmax(i__5,i__6) || rsvec && ! lsvec && *
  1098. lwork < f2cmax(i__7,i__8) || lsvec && rsvec && ! jracc && *lwork
  1099. < f2cmax(i__9,i__10) || lsvec && rsvec && jracc && *lwork < f2cmax(
  1100. i__11,i__12)) {
  1101. *info = -17;
  1102. } else {
  1103. /* #:) */
  1104. *info = 0;
  1105. }
  1106. }
  1107. if (*info != 0) {
  1108. /* #:( */
  1109. i__1 = -(*info);
  1110. xerbla_("SGEJSV", &i__1, (ftnlen)6);
  1111. return 0;
  1112. }
  1113. /* Quick return for void matrix (Y3K safe) */
  1114. /* #:) */
  1115. if (*m == 0 || *n == 0) {
  1116. iwork[1] = 0;
  1117. iwork[2] = 0;
  1118. iwork[3] = 0;
  1119. work[1] = 0.f;
  1120. work[2] = 0.f;
  1121. work[3] = 0.f;
  1122. work[4] = 0.f;
  1123. work[5] = 0.f;
  1124. work[6] = 0.f;
  1125. work[7] = 0.f;
  1126. return 0;
  1127. }
  1128. /* Determine whether the matrix U should be M x N or M x M */
  1129. if (lsvec) {
  1130. n1 = *n;
  1131. if (lsame_(jobu, "F")) {
  1132. n1 = *m;
  1133. }
  1134. }
  1135. /* Set numerical parameters */
  1136. /* ! NOTE: Make sure SLAMCH() does not fail on the target architecture. */
  1137. epsln = slamch_("Epsilon");
  1138. sfmin = slamch_("SafeMinimum");
  1139. small = sfmin / epsln;
  1140. big = slamch_("O");
  1141. /* BIG = ONE / SFMIN */
  1142. /* Initialize SVA(1:N) = diag( ||A e_i||_2 )_1^N */
  1143. /* (!) If necessary, scale SVA() to protect the largest norm from */
  1144. /* overflow. It is possible that this scaling pushes the smallest */
  1145. /* column norm left from the underflow threshold (extreme case). */
  1146. scalem = 1.f / sqrt((real) (*m) * (real) (*n));
  1147. noscal = TRUE_;
  1148. goscal = TRUE_;
  1149. i__1 = *n;
  1150. for (p = 1; p <= i__1; ++p) {
  1151. aapp = 0.f;
  1152. aaqq = 1.f;
  1153. slassq_(m, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
  1154. if (aapp > big) {
  1155. *info = -9;
  1156. i__2 = -(*info);
  1157. xerbla_("SGEJSV", &i__2, (ftnlen)6);
  1158. return 0;
  1159. }
  1160. aaqq = sqrt(aaqq);
  1161. if (aapp < big / aaqq && noscal) {
  1162. sva[p] = aapp * aaqq;
  1163. } else {
  1164. noscal = FALSE_;
  1165. sva[p] = aapp * (aaqq * scalem);
  1166. if (goscal) {
  1167. goscal = FALSE_;
  1168. i__2 = p - 1;
  1169. sscal_(&i__2, &scalem, &sva[1], &c__1);
  1170. }
  1171. }
  1172. /* L1874: */
  1173. }
  1174. if (noscal) {
  1175. scalem = 1.f;
  1176. }
  1177. aapp = 0.f;
  1178. aaqq = big;
  1179. i__1 = *n;
  1180. for (p = 1; p <= i__1; ++p) {
  1181. /* Computing MAX */
  1182. r__1 = aapp, r__2 = sva[p];
  1183. aapp = f2cmax(r__1,r__2);
  1184. if (sva[p] != 0.f) {
  1185. /* Computing MIN */
  1186. r__1 = aaqq, r__2 = sva[p];
  1187. aaqq = f2cmin(r__1,r__2);
  1188. }
  1189. /* L4781: */
  1190. }
  1191. /* Quick return for zero M x N matrix */
  1192. /* #:) */
  1193. if (aapp == 0.f) {
  1194. if (lsvec) {
  1195. slaset_("G", m, &n1, &c_b34, &c_b35, &u[u_offset], ldu)
  1196. ;
  1197. }
  1198. if (rsvec) {
  1199. slaset_("G", n, n, &c_b34, &c_b35, &v[v_offset], ldv);
  1200. }
  1201. work[1] = 1.f;
  1202. work[2] = 1.f;
  1203. if (errest) {
  1204. work[3] = 1.f;
  1205. }
  1206. if (lsvec && rsvec) {
  1207. work[4] = 1.f;
  1208. work[5] = 1.f;
  1209. }
  1210. if (l2tran) {
  1211. work[6] = 0.f;
  1212. work[7] = 0.f;
  1213. }
  1214. iwork[1] = 0;
  1215. iwork[2] = 0;
  1216. iwork[3] = 0;
  1217. return 0;
  1218. }
  1219. /* Issue warning if denormalized column norms detected. Override the */
  1220. /* high relative accuracy request. Issue licence to kill columns */
  1221. /* (set them to zero) whose norm is less than sigma_max / BIG (roughly). */
  1222. /* #:( */
  1223. warning = 0;
  1224. if (aaqq <= sfmin) {
  1225. l2rank = TRUE_;
  1226. l2kill = TRUE_;
  1227. warning = 1;
  1228. }
  1229. /* Quick return for one-column matrix */
  1230. /* #:) */
  1231. if (*n == 1) {
  1232. if (lsvec) {
  1233. slascl_("G", &c__0, &c__0, &sva[1], &scalem, m, &c__1, &a[a_dim1
  1234. + 1], lda, &ierr);
  1235. slacpy_("A", m, &c__1, &a[a_offset], lda, &u[u_offset], ldu);
  1236. /* computing all M left singular vectors of the M x 1 matrix */
  1237. if (n1 != *n) {
  1238. i__1 = *lwork - *n;
  1239. sgeqrf_(m, n, &u[u_offset], ldu, &work[1], &work[*n + 1], &
  1240. i__1, &ierr);
  1241. i__1 = *lwork - *n;
  1242. sorgqr_(m, &n1, &c__1, &u[u_offset], ldu, &work[1], &work[*n
  1243. + 1], &i__1, &ierr);
  1244. scopy_(m, &a[a_dim1 + 1], &c__1, &u[u_dim1 + 1], &c__1);
  1245. }
  1246. }
  1247. if (rsvec) {
  1248. v[v_dim1 + 1] = 1.f;
  1249. }
  1250. if (sva[1] < big * scalem) {
  1251. sva[1] /= scalem;
  1252. scalem = 1.f;
  1253. }
  1254. work[1] = 1.f / scalem;
  1255. work[2] = 1.f;
  1256. if (sva[1] != 0.f) {
  1257. iwork[1] = 1;
  1258. if (sva[1] / scalem >= sfmin) {
  1259. iwork[2] = 1;
  1260. } else {
  1261. iwork[2] = 0;
  1262. }
  1263. } else {
  1264. iwork[1] = 0;
  1265. iwork[2] = 0;
  1266. }
  1267. iwork[3] = 0;
  1268. if (errest) {
  1269. work[3] = 1.f;
  1270. }
  1271. if (lsvec && rsvec) {
  1272. work[4] = 1.f;
  1273. work[5] = 1.f;
  1274. }
  1275. if (l2tran) {
  1276. work[6] = 0.f;
  1277. work[7] = 0.f;
  1278. }
  1279. return 0;
  1280. }
  1281. transp = FALSE_;
  1282. l2tran = l2tran && *m == *n;
  1283. aatmax = -1.f;
  1284. aatmin = big;
  1285. if (rowpiv || l2tran) {
  1286. /* Compute the row norms, needed to determine row pivoting sequence */
  1287. /* (in the case of heavily row weighted A, row pivoting is strongly */
  1288. /* advised) and to collect information needed to compare the */
  1289. /* structures of A * A^t and A^t * A (in the case L2TRAN.EQ..TRUE.). */
  1290. if (l2tran) {
  1291. i__1 = *m;
  1292. for (p = 1; p <= i__1; ++p) {
  1293. xsc = 0.f;
  1294. temp1 = 1.f;
  1295. slassq_(n, &a[p + a_dim1], lda, &xsc, &temp1);
  1296. /* SLASSQ gets both the ell_2 and the ell_infinity norm */
  1297. /* in one pass through the vector */
  1298. work[*m + *n + p] = xsc * scalem;
  1299. work[*n + p] = xsc * (scalem * sqrt(temp1));
  1300. /* Computing MAX */
  1301. r__1 = aatmax, r__2 = work[*n + p];
  1302. aatmax = f2cmax(r__1,r__2);
  1303. if (work[*n + p] != 0.f) {
  1304. /* Computing MIN */
  1305. r__1 = aatmin, r__2 = work[*n + p];
  1306. aatmin = f2cmin(r__1,r__2);
  1307. }
  1308. /* L1950: */
  1309. }
  1310. } else {
  1311. i__1 = *m;
  1312. for (p = 1; p <= i__1; ++p) {
  1313. work[*m + *n + p] = scalem * (r__1 = a[p + isamax_(n, &a[p +
  1314. a_dim1], lda) * a_dim1], abs(r__1));
  1315. /* Computing MAX */
  1316. r__1 = aatmax, r__2 = work[*m + *n + p];
  1317. aatmax = f2cmax(r__1,r__2);
  1318. /* Computing MIN */
  1319. r__1 = aatmin, r__2 = work[*m + *n + p];
  1320. aatmin = f2cmin(r__1,r__2);
  1321. /* L1904: */
  1322. }
  1323. }
  1324. }
  1325. /* For square matrix A try to determine whether A^t would be better */
  1326. /* input for the preconditioned Jacobi SVD, with faster convergence. */
  1327. /* The decision is based on an O(N) function of the vector of column */
  1328. /* and row norms of A, based on the Shannon entropy. This should give */
  1329. /* the right choice in most cases when the difference actually matters. */
  1330. /* It may fail and pick the slower converging side. */
  1331. entra = 0.f;
  1332. entrat = 0.f;
  1333. if (l2tran) {
  1334. xsc = 0.f;
  1335. temp1 = 1.f;
  1336. slassq_(n, &sva[1], &c__1, &xsc, &temp1);
  1337. temp1 = 1.f / temp1;
  1338. entra = 0.f;
  1339. i__1 = *n;
  1340. for (p = 1; p <= i__1; ++p) {
  1341. /* Computing 2nd power */
  1342. r__1 = sva[p] / xsc;
  1343. big1 = r__1 * r__1 * temp1;
  1344. if (big1 != 0.f) {
  1345. entra += big1 * log(big1);
  1346. }
  1347. /* L1113: */
  1348. }
  1349. entra = -entra / log((real) (*n));
  1350. /* Now, SVA().^2/Trace(A^t * A) is a point in the probability simplex. */
  1351. /* It is derived from the diagonal of A^t * A. Do the same with the */
  1352. /* diagonal of A * A^t, compute the entropy of the corresponding */
  1353. /* probability distribution. Note that A * A^t and A^t * A have the */
  1354. /* same trace. */
  1355. entrat = 0.f;
  1356. i__1 = *n + *m;
  1357. for (p = *n + 1; p <= i__1; ++p) {
  1358. /* Computing 2nd power */
  1359. r__1 = work[p] / xsc;
  1360. big1 = r__1 * r__1 * temp1;
  1361. if (big1 != 0.f) {
  1362. entrat += big1 * log(big1);
  1363. }
  1364. /* L1114: */
  1365. }
  1366. entrat = -entrat / log((real) (*m));
  1367. /* Analyze the entropies and decide A or A^t. Smaller entropy */
  1368. /* usually means better input for the algorithm. */
  1369. transp = entrat < entra;
  1370. /* If A^t is better than A, transpose A. */
  1371. if (transp) {
  1372. /* In an optimal implementation, this trivial transpose */
  1373. /* should be replaced with faster transpose. */
  1374. i__1 = *n - 1;
  1375. for (p = 1; p <= i__1; ++p) {
  1376. i__2 = *n;
  1377. for (q = p + 1; q <= i__2; ++q) {
  1378. temp1 = a[q + p * a_dim1];
  1379. a[q + p * a_dim1] = a[p + q * a_dim1];
  1380. a[p + q * a_dim1] = temp1;
  1381. /* L1116: */
  1382. }
  1383. /* L1115: */
  1384. }
  1385. i__1 = *n;
  1386. for (p = 1; p <= i__1; ++p) {
  1387. work[*m + *n + p] = sva[p];
  1388. sva[p] = work[*n + p];
  1389. /* L1117: */
  1390. }
  1391. temp1 = aapp;
  1392. aapp = aatmax;
  1393. aatmax = temp1;
  1394. temp1 = aaqq;
  1395. aaqq = aatmin;
  1396. aatmin = temp1;
  1397. kill = lsvec;
  1398. lsvec = rsvec;
  1399. rsvec = kill;
  1400. if (lsvec) {
  1401. n1 = *n;
  1402. }
  1403. rowpiv = TRUE_;
  1404. }
  1405. }
  1406. /* END IF L2TRAN */
  1407. /* Scale the matrix so that its maximal singular value remains less */
  1408. /* than SQRT(BIG) -- the matrix is scaled so that its maximal column */
  1409. /* has Euclidean norm equal to SQRT(BIG/N). The only reason to keep */
  1410. /* SQRT(BIG) instead of BIG is the fact that SGEJSV uses LAPACK and */
  1411. /* BLAS routines that, in some implementations, are not capable of */
  1412. /* working in the full interval [SFMIN,BIG] and that they may provoke */
  1413. /* overflows in the intermediate results. If the singular values spread */
  1414. /* from SFMIN to BIG, then SGESVJ will compute them. So, in that case, */
  1415. /* one should use SGESVJ instead of SGEJSV. */
  1416. big1 = sqrt(big);
  1417. temp1 = sqrt(big / (real) (*n));
  1418. slascl_("G", &c__0, &c__0, &aapp, &temp1, n, &c__1, &sva[1], n, &ierr);
  1419. if (aaqq > aapp * sfmin) {
  1420. aaqq = aaqq / aapp * temp1;
  1421. } else {
  1422. aaqq = aaqq * temp1 / aapp;
  1423. }
  1424. temp1 *= scalem;
  1425. slascl_("G", &c__0, &c__0, &aapp, &temp1, m, n, &a[a_offset], lda, &ierr);
  1426. /* To undo scaling at the end of this procedure, multiply the */
  1427. /* computed singular values with USCAL2 / USCAL1. */
  1428. uscal1 = temp1;
  1429. uscal2 = aapp;
  1430. if (l2kill) {
  1431. /* L2KILL enforces computation of nonzero singular values in */
  1432. /* the restricted range of condition number of the initial A, */
  1433. /* sigma_max(A) / sigma_min(A) approx. SQRT(BIG)/SQRT(SFMIN). */
  1434. xsc = sqrt(sfmin);
  1435. } else {
  1436. xsc = small;
  1437. /* Now, if the condition number of A is too big, */
  1438. /* sigma_max(A) / sigma_min(A) .GT. SQRT(BIG/N) * EPSLN / SFMIN, */
  1439. /* as a precaution measure, the full SVD is computed using SGESVJ */
  1440. /* with accumulated Jacobi rotations. This provides numerically */
  1441. /* more robust computation, at the cost of slightly increased run */
  1442. /* time. Depending on the concrete implementation of BLAS and LAPACK */
  1443. /* (i.e. how they behave in presence of extreme ill-conditioning) the */
  1444. /* implementor may decide to remove this switch. */
  1445. if (aaqq < sqrt(sfmin) && lsvec && rsvec) {
  1446. jracc = TRUE_;
  1447. }
  1448. }
  1449. if (aaqq < xsc) {
  1450. i__1 = *n;
  1451. for (p = 1; p <= i__1; ++p) {
  1452. if (sva[p] < xsc) {
  1453. slaset_("A", m, &c__1, &c_b34, &c_b34, &a[p * a_dim1 + 1],
  1454. lda);
  1455. sva[p] = 0.f;
  1456. }
  1457. /* L700: */
  1458. }
  1459. }
  1460. /* Preconditioning using QR factorization with pivoting */
  1461. if (rowpiv) {
  1462. /* Optional row permutation (Bjoerck row pivoting): */
  1463. /* A result by Cox and Higham shows that the Bjoerck's */
  1464. /* row pivoting combined with standard column pivoting */
  1465. /* has similar effect as Powell-Reid complete pivoting. */
  1466. /* The ell-infinity norms of A are made nonincreasing. */
  1467. i__1 = *m - 1;
  1468. for (p = 1; p <= i__1; ++p) {
  1469. i__2 = *m - p + 1;
  1470. q = isamax_(&i__2, &work[*m + *n + p], &c__1) + p - 1;
  1471. iwork[(*n << 1) + p] = q;
  1472. if (p != q) {
  1473. temp1 = work[*m + *n + p];
  1474. work[*m + *n + p] = work[*m + *n + q];
  1475. work[*m + *n + q] = temp1;
  1476. }
  1477. /* L1952: */
  1478. }
  1479. i__1 = *m - 1;
  1480. slaswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[(*n << 1) + 1], &
  1481. c__1);
  1482. }
  1483. /* End of the preparation phase (scaling, optional sorting and */
  1484. /* transposing, optional flushing of small columns). */
  1485. /* Preconditioning */
  1486. /* If the full SVD is needed, the right singular vectors are computed */
  1487. /* from a matrix equation, and for that we need theoretical analysis */
  1488. /* of the Businger-Golub pivoting. So we use SGEQP3 as the first RR QRF. */
  1489. /* In all other cases the first RR QRF can be chosen by other criteria */
  1490. /* (eg speed by replacing global with restricted window pivoting, such */
  1491. /* as in SGEQPX from TOMS # 782). Good results will be obtained using */
  1492. /* SGEQPX with properly (!) chosen numerical parameters. */
  1493. /* Any improvement of SGEQP3 improves overal performance of SGEJSV. */
  1494. /* A * P1 = Q1 * [ R1^t 0]^t: */
  1495. i__1 = *n;
  1496. for (p = 1; p <= i__1; ++p) {
  1497. iwork[p] = 0;
  1498. /* L1963: */
  1499. }
  1500. i__1 = *lwork - *n;
  1501. sgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &work[1], &work[*n + 1], &
  1502. i__1, &ierr);
  1503. /* The upper triangular matrix R1 from the first QRF is inspected for */
  1504. /* rank deficiency and possibilities for deflation, or possible */
  1505. /* ill-conditioning. Depending on the user specified flag L2RANK, */
  1506. /* the procedure explores possibilities to reduce the numerical */
  1507. /* rank by inspecting the computed upper triangular factor. If */
  1508. /* L2RANK or L2ABER are up, then SGEJSV will compute the SVD of */
  1509. /* A + dA, where ||dA|| <= f(M,N)*EPSLN. */
  1510. nr = 1;
  1511. if (l2aber) {
  1512. /* Standard absolute error bound suffices. All sigma_i with */
  1513. /* sigma_i < N*EPSLN*||A|| are flushed to zero. This is an */
  1514. /* aggressive enforcement of lower numerical rank by introducing a */
  1515. /* backward error of the order of N*EPSLN*||A||. */
  1516. temp1 = sqrt((real) (*n)) * epsln;
  1517. i__1 = *n;
  1518. for (p = 2; p <= i__1; ++p) {
  1519. if ((r__2 = a[p + p * a_dim1], abs(r__2)) >= temp1 * (r__1 = a[
  1520. a_dim1 + 1], abs(r__1))) {
  1521. ++nr;
  1522. } else {
  1523. goto L3002;
  1524. }
  1525. /* L3001: */
  1526. }
  1527. L3002:
  1528. ;
  1529. } else if (l2rank) {
  1530. /* Sudden drop on the diagonal of R1 is used as the criterion for */
  1531. /* close-to-rank-deficient. */
  1532. temp1 = sqrt(sfmin);
  1533. i__1 = *n;
  1534. for (p = 2; p <= i__1; ++p) {
  1535. if ((r__2 = a[p + p * a_dim1], abs(r__2)) < epsln * (r__1 = a[p -
  1536. 1 + (p - 1) * a_dim1], abs(r__1)) || (r__3 = a[p + p *
  1537. a_dim1], abs(r__3)) < small || l2kill && (r__4 = a[p + p *
  1538. a_dim1], abs(r__4)) < temp1) {
  1539. goto L3402;
  1540. }
  1541. ++nr;
  1542. /* L3401: */
  1543. }
  1544. L3402:
  1545. ;
  1546. } else {
  1547. /* The goal is high relative accuracy. However, if the matrix */
  1548. /* has high scaled condition number the relative accuracy is in */
  1549. /* general not feasible. Later on, a condition number estimator */
  1550. /* will be deployed to estimate the scaled condition number. */
  1551. /* Here we just remove the underflowed part of the triangular */
  1552. /* factor. This prevents the situation in which the code is */
  1553. /* working hard to get the accuracy not warranted by the data. */
  1554. temp1 = sqrt(sfmin);
  1555. i__1 = *n;
  1556. for (p = 2; p <= i__1; ++p) {
  1557. if ((r__1 = a[p + p * a_dim1], abs(r__1)) < small || l2kill && (
  1558. r__2 = a[p + p * a_dim1], abs(r__2)) < temp1) {
  1559. goto L3302;
  1560. }
  1561. ++nr;
  1562. /* L3301: */
  1563. }
  1564. L3302:
  1565. ;
  1566. }
  1567. almort = FALSE_;
  1568. if (nr == *n) {
  1569. maxprj = 1.f;
  1570. i__1 = *n;
  1571. for (p = 2; p <= i__1; ++p) {
  1572. temp1 = (r__1 = a[p + p * a_dim1], abs(r__1)) / sva[iwork[p]];
  1573. maxprj = f2cmin(maxprj,temp1);
  1574. /* L3051: */
  1575. }
  1576. /* Computing 2nd power */
  1577. r__1 = maxprj;
  1578. if (r__1 * r__1 >= 1.f - (real) (*n) * epsln) {
  1579. almort = TRUE_;
  1580. }
  1581. }
  1582. sconda = -1.f;
  1583. condr1 = -1.f;
  1584. condr2 = -1.f;
  1585. if (errest) {
  1586. if (*n == nr) {
  1587. if (rsvec) {
  1588. slacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
  1589. i__1 = *n;
  1590. for (p = 1; p <= i__1; ++p) {
  1591. temp1 = sva[iwork[p]];
  1592. r__1 = 1.f / temp1;
  1593. sscal_(&p, &r__1, &v[p * v_dim1 + 1], &c__1);
  1594. /* L3053: */
  1595. }
  1596. spocon_("U", n, &v[v_offset], ldv, &c_b35, &temp1, &work[*n +
  1597. 1], &iwork[(*n << 1) + *m + 1], &ierr);
  1598. } else if (lsvec) {
  1599. slacpy_("U", n, n, &a[a_offset], lda, &u[u_offset], ldu);
  1600. i__1 = *n;
  1601. for (p = 1; p <= i__1; ++p) {
  1602. temp1 = sva[iwork[p]];
  1603. r__1 = 1.f / temp1;
  1604. sscal_(&p, &r__1, &u[p * u_dim1 + 1], &c__1);
  1605. /* L3054: */
  1606. }
  1607. spocon_("U", n, &u[u_offset], ldu, &c_b35, &temp1, &work[*n +
  1608. 1], &iwork[(*n << 1) + *m + 1], &ierr);
  1609. } else {
  1610. slacpy_("U", n, n, &a[a_offset], lda, &work[*n + 1], n);
  1611. i__1 = *n;
  1612. for (p = 1; p <= i__1; ++p) {
  1613. temp1 = sva[iwork[p]];
  1614. r__1 = 1.f / temp1;
  1615. sscal_(&p, &r__1, &work[*n + (p - 1) * *n + 1], &c__1);
  1616. /* L3052: */
  1617. }
  1618. spocon_("U", n, &work[*n + 1], n, &c_b35, &temp1, &work[*n + *
  1619. n * *n + 1], &iwork[(*n << 1) + *m + 1], &ierr);
  1620. }
  1621. sconda = 1.f / sqrt(temp1);
  1622. /* SCONDA is an estimate of SQRT(||(R^t * R)^(-1)||_1). */
  1623. /* N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
  1624. } else {
  1625. sconda = -1.f;
  1626. }
  1627. }
  1628. l2pert = l2pert && (r__1 = a[a_dim1 + 1] / a[nr + nr * a_dim1], abs(r__1))
  1629. > sqrt(big1);
  1630. /* If there is no violent scaling, artificial perturbation is not needed. */
  1631. /* Phase 3: */
  1632. if (! (rsvec || lsvec)) {
  1633. /* Singular Values only */
  1634. /* Computing MIN */
  1635. i__2 = *n - 1;
  1636. i__1 = f2cmin(i__2,nr);
  1637. for (p = 1; p <= i__1; ++p) {
  1638. i__2 = *n - p;
  1639. scopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p *
  1640. a_dim1], &c__1);
  1641. /* L1946: */
  1642. }
  1643. /* The following two DO-loops introduce small relative perturbation */
  1644. /* into the strict upper triangle of the lower triangular matrix. */
  1645. /* Small entries below the main diagonal are also changed. */
  1646. /* This modification is useful if the computing environment does not */
  1647. /* provide/allow FLUSH TO ZERO underflow, for it prevents many */
  1648. /* annoying denormalized numbers in case of strongly scaled matrices. */
  1649. /* The perturbation is structured so that it does not introduce any */
  1650. /* new perturbation of the singular values, and it does not destroy */
  1651. /* the job done by the preconditioner. */
  1652. /* The licence for this perturbation is in the variable L2PERT, which */
  1653. /* should be .FALSE. if FLUSH TO ZERO underflow is active. */
  1654. if (! almort) {
  1655. if (l2pert) {
  1656. /* XSC = SQRT(SMALL) */
  1657. xsc = epsln / (real) (*n);
  1658. i__1 = nr;
  1659. for (q = 1; q <= i__1; ++q) {
  1660. temp1 = xsc * (r__1 = a[q + q * a_dim1], abs(r__1));
  1661. i__2 = *n;
  1662. for (p = 1; p <= i__2; ++p) {
  1663. if (p > q && (r__1 = a[p + q * a_dim1], abs(r__1)) <=
  1664. temp1 || p < q) {
  1665. a[p + q * a_dim1] = r_sign(&temp1, &a[p + q *
  1666. a_dim1]);
  1667. }
  1668. /* L4949: */
  1669. }
  1670. /* L4947: */
  1671. }
  1672. } else {
  1673. i__1 = nr - 1;
  1674. i__2 = nr - 1;
  1675. slaset_("U", &i__1, &i__2, &c_b34, &c_b34, &a[(a_dim1 << 1) +
  1676. 1], lda);
  1677. }
  1678. i__1 = *lwork - *n;
  1679. sgeqrf_(n, &nr, &a[a_offset], lda, &work[1], &work[*n + 1], &i__1,
  1680. &ierr);
  1681. i__1 = nr - 1;
  1682. for (p = 1; p <= i__1; ++p) {
  1683. i__2 = nr - p;
  1684. scopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p *
  1685. a_dim1], &c__1);
  1686. /* L1948: */
  1687. }
  1688. }
  1689. /* Row-cyclic Jacobi SVD algorithm with column pivoting */
  1690. /* to drown denormals */
  1691. if (l2pert) {
  1692. /* XSC = SQRT(SMALL) */
  1693. xsc = epsln / (real) (*n);
  1694. i__1 = nr;
  1695. for (q = 1; q <= i__1; ++q) {
  1696. temp1 = xsc * (r__1 = a[q + q * a_dim1], abs(r__1));
  1697. i__2 = nr;
  1698. for (p = 1; p <= i__2; ++p) {
  1699. if (p > q && (r__1 = a[p + q * a_dim1], abs(r__1)) <=
  1700. temp1 || p < q) {
  1701. a[p + q * a_dim1] = r_sign(&temp1, &a[p + q * a_dim1])
  1702. ;
  1703. }
  1704. /* L1949: */
  1705. }
  1706. /* L1947: */
  1707. }
  1708. } else {
  1709. i__1 = nr - 1;
  1710. i__2 = nr - 1;
  1711. slaset_("U", &i__1, &i__2, &c_b34, &c_b34, &a[(a_dim1 << 1) + 1],
  1712. lda);
  1713. }
  1714. /* triangular matrix (plus perturbation which is ignored in */
  1715. /* the part which destroys triangular form (confusing?!)) */
  1716. sgesvj_("L", "NoU", "NoV", &nr, &nr, &a[a_offset], lda, &sva[1], n, &
  1717. v[v_offset], ldv, &work[1], lwork, info);
  1718. scalem = work[1];
  1719. numrank = i_nint(&work[2]);
  1720. } else if (rsvec && ! lsvec) {
  1721. /* -> Singular Values and Right Singular Vectors <- */
  1722. if (almort) {
  1723. i__1 = nr;
  1724. for (p = 1; p <= i__1; ++p) {
  1725. i__2 = *n - p + 1;
  1726. scopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
  1727. c__1);
  1728. /* L1998: */
  1729. }
  1730. i__1 = nr - 1;
  1731. i__2 = nr - 1;
  1732. slaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) +
  1733. 1], ldv);
  1734. sgesvj_("L", "U", "N", n, &nr, &v[v_offset], ldv, &sva[1], &nr, &
  1735. a[a_offset], lda, &work[1], lwork, info);
  1736. scalem = work[1];
  1737. numrank = i_nint(&work[2]);
  1738. } else {
  1739. /* accumulated product of Jacobi rotations, three are perfect ) */
  1740. i__1 = nr - 1;
  1741. i__2 = nr - 1;
  1742. slaset_("Lower", &i__1, &i__2, &c_b34, &c_b34, &a[a_dim1 + 2],
  1743. lda);
  1744. i__1 = *lwork - *n;
  1745. sgelqf_(&nr, n, &a[a_offset], lda, &work[1], &work[*n + 1], &i__1,
  1746. &ierr);
  1747. slacpy_("Lower", &nr, &nr, &a[a_offset], lda, &v[v_offset], ldv);
  1748. i__1 = nr - 1;
  1749. i__2 = nr - 1;
  1750. slaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) +
  1751. 1], ldv);
  1752. i__1 = *lwork - (*n << 1);
  1753. sgeqrf_(&nr, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*n <<
  1754. 1) + 1], &i__1, &ierr);
  1755. i__1 = nr;
  1756. for (p = 1; p <= i__1; ++p) {
  1757. i__2 = nr - p + 1;
  1758. scopy_(&i__2, &v[p + p * v_dim1], ldv, &v[p + p * v_dim1], &
  1759. c__1);
  1760. /* L8998: */
  1761. }
  1762. i__1 = nr - 1;
  1763. i__2 = nr - 1;
  1764. slaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) +
  1765. 1], ldv);
  1766. i__1 = *lwork - *n;
  1767. sgesvj_("Lower", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[1], &
  1768. nr, &u[u_offset], ldu, &work[*n + 1], &i__1, info);
  1769. scalem = work[*n + 1];
  1770. numrank = i_nint(&work[*n + 2]);
  1771. if (nr < *n) {
  1772. i__1 = *n - nr;
  1773. slaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + v_dim1],
  1774. ldv);
  1775. i__1 = *n - nr;
  1776. slaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) * v_dim1
  1777. + 1], ldv);
  1778. i__1 = *n - nr;
  1779. i__2 = *n - nr;
  1780. slaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 + (nr +
  1781. 1) * v_dim1], ldv);
  1782. }
  1783. i__1 = *lwork - *n;
  1784. sormlq_("Left", "Transpose", n, n, &nr, &a[a_offset], lda, &work[
  1785. 1], &v[v_offset], ldv, &work[*n + 1], &i__1, &ierr);
  1786. }
  1787. i__1 = *n;
  1788. for (p = 1; p <= i__1; ++p) {
  1789. scopy_(n, &v[p + v_dim1], ldv, &a[iwork[p] + a_dim1], lda);
  1790. /* L8991: */
  1791. }
  1792. slacpy_("All", n, n, &a[a_offset], lda, &v[v_offset], ldv);
  1793. if (transp) {
  1794. slacpy_("All", n, n, &v[v_offset], ldv, &u[u_offset], ldu);
  1795. }
  1796. } else if (lsvec && ! rsvec) {
  1797. /* Jacobi rotations in the Jacobi iterations. */
  1798. i__1 = nr;
  1799. for (p = 1; p <= i__1; ++p) {
  1800. i__2 = *n - p + 1;
  1801. scopy_(&i__2, &a[p + p * a_dim1], lda, &u[p + p * u_dim1], &c__1);
  1802. /* L1965: */
  1803. }
  1804. i__1 = nr - 1;
  1805. i__2 = nr - 1;
  1806. slaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 1],
  1807. ldu);
  1808. i__1 = *lwork - (*n << 1);
  1809. sgeqrf_(n, &nr, &u[u_offset], ldu, &work[*n + 1], &work[(*n << 1) + 1]
  1810. , &i__1, &ierr);
  1811. i__1 = nr - 1;
  1812. for (p = 1; p <= i__1; ++p) {
  1813. i__2 = nr - p;
  1814. scopy_(&i__2, &u[p + (p + 1) * u_dim1], ldu, &u[p + 1 + p *
  1815. u_dim1], &c__1);
  1816. /* L1967: */
  1817. }
  1818. i__1 = nr - 1;
  1819. i__2 = nr - 1;
  1820. slaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 1],
  1821. ldu);
  1822. i__1 = *lwork - *n;
  1823. sgesvj_("Lower", "U", "N", &nr, &nr, &u[u_offset], ldu, &sva[1], &nr,
  1824. &a[a_offset], lda, &work[*n + 1], &i__1, info);
  1825. scalem = work[*n + 1];
  1826. numrank = i_nint(&work[*n + 2]);
  1827. if (nr < *m) {
  1828. i__1 = *m - nr;
  1829. slaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 + u_dim1], ldu);
  1830. if (nr < n1) {
  1831. i__1 = n1 - nr;
  1832. slaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) * u_dim1
  1833. + 1], ldu);
  1834. i__1 = *m - nr;
  1835. i__2 = n1 - nr;
  1836. slaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 + (nr +
  1837. 1) * u_dim1], ldu);
  1838. }
  1839. }
  1840. i__1 = *lwork - *n;
  1841. sormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[1], &u[
  1842. u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  1843. if (rowpiv) {
  1844. i__1 = *m - 1;
  1845. slaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n << 1) +
  1846. 1], &c_n1);
  1847. }
  1848. i__1 = n1;
  1849. for (p = 1; p <= i__1; ++p) {
  1850. xsc = 1.f / snrm2_(m, &u[p * u_dim1 + 1], &c__1);
  1851. sscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  1852. /* L1974: */
  1853. }
  1854. if (transp) {
  1855. slacpy_("All", n, n, &u[u_offset], ldu, &v[v_offset], ldv);
  1856. }
  1857. } else {
  1858. if (! jracc) {
  1859. if (! almort) {
  1860. /* Second Preconditioning Step (QRF [with pivoting]) */
  1861. /* Note that the composition of TRANSPOSE, QRF and TRANSPOSE is */
  1862. /* equivalent to an LQF CALL. Since in many libraries the QRF */
  1863. /* seems to be better optimized than the LQF, we do explicit */
  1864. /* transpose and use the QRF. This is subject to changes in an */
  1865. /* optimized implementation of SGEJSV. */
  1866. i__1 = nr;
  1867. for (p = 1; p <= i__1; ++p) {
  1868. i__2 = *n - p + 1;
  1869. scopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1],
  1870. &c__1);
  1871. /* L1968: */
  1872. }
  1873. /* denormals in the second QR factorization, where they are */
  1874. /* as good as zeros. This is done to avoid painfully slow */
  1875. /* computation with denormals. The relative size of the perturbation */
  1876. /* is a parameter that can be changed by the implementer. */
  1877. /* This perturbation device will be obsolete on machines with */
  1878. /* properly implemented arithmetic. */
  1879. /* To switch it off, set L2PERT=.FALSE. To remove it from the */
  1880. /* code, remove the action under L2PERT=.TRUE., leave the ELSE part. */
  1881. /* The following two loops should be blocked and fused with the */
  1882. /* transposed copy above. */
  1883. if (l2pert) {
  1884. xsc = sqrt(small);
  1885. i__1 = nr;
  1886. for (q = 1; q <= i__1; ++q) {
  1887. temp1 = xsc * (r__1 = v[q + q * v_dim1], abs(r__1));
  1888. i__2 = *n;
  1889. for (p = 1; p <= i__2; ++p) {
  1890. if (p > q && (r__1 = v[p + q * v_dim1], abs(r__1))
  1891. <= temp1 || p < q) {
  1892. v[p + q * v_dim1] = r_sign(&temp1, &v[p + q *
  1893. v_dim1]);
  1894. }
  1895. if (p < q) {
  1896. v[p + q * v_dim1] = -v[p + q * v_dim1];
  1897. }
  1898. /* L2968: */
  1899. }
  1900. /* L2969: */
  1901. }
  1902. } else {
  1903. i__1 = nr - 1;
  1904. i__2 = nr - 1;
  1905. slaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 <<
  1906. 1) + 1], ldv);
  1907. }
  1908. /* Estimate the row scaled condition number of R1 */
  1909. /* (If R1 is rectangular, N > NR, then the condition number */
  1910. /* of the leading NR x NR submatrix is estimated.) */
  1911. slacpy_("L", &nr, &nr, &v[v_offset], ldv, &work[(*n << 1) + 1]
  1912. , &nr);
  1913. i__1 = nr;
  1914. for (p = 1; p <= i__1; ++p) {
  1915. i__2 = nr - p + 1;
  1916. temp1 = snrm2_(&i__2, &work[(*n << 1) + (p - 1) * nr + p],
  1917. &c__1);
  1918. i__2 = nr - p + 1;
  1919. r__1 = 1.f / temp1;
  1920. sscal_(&i__2, &r__1, &work[(*n << 1) + (p - 1) * nr + p],
  1921. &c__1);
  1922. /* L3950: */
  1923. }
  1924. spocon_("Lower", &nr, &work[(*n << 1) + 1], &nr, &c_b35, &
  1925. temp1, &work[(*n << 1) + nr * nr + 1], &iwork[*m + (*
  1926. n << 1) + 1], &ierr);
  1927. condr1 = 1.f / sqrt(temp1);
  1928. /* R1 is OK for inverse <=> CONDR1 .LT. FLOAT(N) */
  1929. /* more conservative <=> CONDR1 .LT. SQRT(FLOAT(N)) */
  1930. cond_ok__ = sqrt((real) nr);
  1931. /* [TP] COND_OK is a tuning parameter. */
  1932. if (condr1 < cond_ok__) {
  1933. /* implementation, this QRF should be implemented as the QRF */
  1934. /* of a lower triangular matrix. */
  1935. /* R1^t = Q2 * R2 */
  1936. i__1 = *lwork - (*n << 1);
  1937. sgeqrf_(n, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*
  1938. n << 1) + 1], &i__1, &ierr);
  1939. if (l2pert) {
  1940. xsc = sqrt(small) / epsln;
  1941. i__1 = nr;
  1942. for (p = 2; p <= i__1; ++p) {
  1943. i__2 = p - 1;
  1944. for (q = 1; q <= i__2; ++q) {
  1945. /* Computing MIN */
  1946. r__3 = (r__1 = v[p + p * v_dim1], abs(r__1)),
  1947. r__4 = (r__2 = v[q + q * v_dim1], abs(
  1948. r__2));
  1949. temp1 = xsc * f2cmin(r__3,r__4);
  1950. if ((r__1 = v[q + p * v_dim1], abs(r__1)) <=
  1951. temp1) {
  1952. v[q + p * v_dim1] = r_sign(&temp1, &v[q +
  1953. p * v_dim1]);
  1954. }
  1955. /* L3958: */
  1956. }
  1957. /* L3959: */
  1958. }
  1959. }
  1960. if (nr != *n) {
  1961. slacpy_("A", n, &nr, &v[v_offset], ldv, &work[(*n <<
  1962. 1) + 1], n);
  1963. }
  1964. i__1 = nr - 1;
  1965. for (p = 1; p <= i__1; ++p) {
  1966. i__2 = nr - p;
  1967. scopy_(&i__2, &v[p + (p + 1) * v_dim1], ldv, &v[p + 1
  1968. + p * v_dim1], &c__1);
  1969. /* L1969: */
  1970. }
  1971. condr2 = condr1;
  1972. } else {
  1973. /* Note that windowed pivoting would be equally good */
  1974. /* numerically, and more run-time efficient. So, in */
  1975. /* an optimal implementation, the next call to SGEQP3 */
  1976. /* should be replaced with eg. CALL SGEQPX (ACM TOMS #782) */
  1977. /* with properly (carefully) chosen parameters. */
  1978. /* R1^t * P2 = Q2 * R2 */
  1979. i__1 = nr;
  1980. for (p = 1; p <= i__1; ++p) {
  1981. iwork[*n + p] = 0;
  1982. /* L3003: */
  1983. }
  1984. i__1 = *lwork - (*n << 1);
  1985. sgeqp3_(n, &nr, &v[v_offset], ldv, &iwork[*n + 1], &work[*
  1986. n + 1], &work[(*n << 1) + 1], &i__1, &ierr);
  1987. /* * CALL SGEQRF( N, NR, V, LDV, WORK(N+1), WORK(2*N+1), */
  1988. /* * $ LWORK-2*N, IERR ) */
  1989. if (l2pert) {
  1990. xsc = sqrt(small);
  1991. i__1 = nr;
  1992. for (p = 2; p <= i__1; ++p) {
  1993. i__2 = p - 1;
  1994. for (q = 1; q <= i__2; ++q) {
  1995. /* Computing MIN */
  1996. r__3 = (r__1 = v[p + p * v_dim1], abs(r__1)),
  1997. r__4 = (r__2 = v[q + q * v_dim1], abs(
  1998. r__2));
  1999. temp1 = xsc * f2cmin(r__3,r__4);
  2000. if ((r__1 = v[q + p * v_dim1], abs(r__1)) <=
  2001. temp1) {
  2002. v[q + p * v_dim1] = r_sign(&temp1, &v[q +
  2003. p * v_dim1]);
  2004. }
  2005. /* L3968: */
  2006. }
  2007. /* L3969: */
  2008. }
  2009. }
  2010. slacpy_("A", n, &nr, &v[v_offset], ldv, &work[(*n << 1) +
  2011. 1], n);
  2012. if (l2pert) {
  2013. xsc = sqrt(small);
  2014. i__1 = nr;
  2015. for (p = 2; p <= i__1; ++p) {
  2016. i__2 = p - 1;
  2017. for (q = 1; q <= i__2; ++q) {
  2018. /* Computing MIN */
  2019. r__3 = (r__1 = v[p + p * v_dim1], abs(r__1)),
  2020. r__4 = (r__2 = v[q + q * v_dim1], abs(
  2021. r__2));
  2022. temp1 = xsc * f2cmin(r__3,r__4);
  2023. v[p + q * v_dim1] = -r_sign(&temp1, &v[q + p *
  2024. v_dim1]);
  2025. /* L8971: */
  2026. }
  2027. /* L8970: */
  2028. }
  2029. } else {
  2030. i__1 = nr - 1;
  2031. i__2 = nr - 1;
  2032. slaset_("L", &i__1, &i__2, &c_b34, &c_b34, &v[v_dim1
  2033. + 2], ldv);
  2034. }
  2035. /* Now, compute R2 = L3 * Q3, the LQ factorization. */
  2036. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2037. sgelqf_(&nr, &nr, &v[v_offset], ldv, &work[(*n << 1) + *n
  2038. * nr + 1], &work[(*n << 1) + *n * nr + nr + 1], &
  2039. i__1, &ierr);
  2040. slacpy_("L", &nr, &nr, &v[v_offset], ldv, &work[(*n << 1)
  2041. + *n * nr + nr + 1], &nr);
  2042. i__1 = nr;
  2043. for (p = 1; p <= i__1; ++p) {
  2044. temp1 = snrm2_(&p, &work[(*n << 1) + *n * nr + nr + p]
  2045. , &nr);
  2046. r__1 = 1.f / temp1;
  2047. sscal_(&p, &r__1, &work[(*n << 1) + *n * nr + nr + p],
  2048. &nr);
  2049. /* L4950: */
  2050. }
  2051. spocon_("L", &nr, &work[(*n << 1) + *n * nr + nr + 1], &
  2052. nr, &c_b35, &temp1, &work[(*n << 1) + *n * nr +
  2053. nr + nr * nr + 1], &iwork[*m + (*n << 1) + 1], &
  2054. ierr);
  2055. condr2 = 1.f / sqrt(temp1);
  2056. if (condr2 >= cond_ok__) {
  2057. /* (this overwrites the copy of R2, as it will not be */
  2058. /* needed in this branch, but it does not overwritte the */
  2059. /* Huseholder vectors of Q2.). */
  2060. slacpy_("U", &nr, &nr, &v[v_offset], ldv, &work[(*n <<
  2061. 1) + 1], n);
  2062. /* WORK(2*N+N*NR+1:2*N+N*NR+N) */
  2063. }
  2064. }
  2065. if (l2pert) {
  2066. xsc = sqrt(small);
  2067. i__1 = nr;
  2068. for (q = 2; q <= i__1; ++q) {
  2069. temp1 = xsc * v[q + q * v_dim1];
  2070. i__2 = q - 1;
  2071. for (p = 1; p <= i__2; ++p) {
  2072. /* V(p,q) = - SIGN( TEMP1, V(q,p) ) */
  2073. v[p + q * v_dim1] = -r_sign(&temp1, &v[p + q *
  2074. v_dim1]);
  2075. /* L4969: */
  2076. }
  2077. /* L4968: */
  2078. }
  2079. } else {
  2080. i__1 = nr - 1;
  2081. i__2 = nr - 1;
  2082. slaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 <<
  2083. 1) + 1], ldv);
  2084. }
  2085. /* Second preconditioning finished; continue with Jacobi SVD */
  2086. /* The input matrix is lower trinagular. */
  2087. /* Recover the right singular vectors as solution of a well */
  2088. /* conditioned triangular matrix equation. */
  2089. if (condr1 < cond_ok__) {
  2090. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2091. sgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
  2092. 1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
  2093. nr + nr + 1], &i__1, info);
  2094. scalem = work[(*n << 1) + *n * nr + nr + 1];
  2095. numrank = i_nint(&work[(*n << 1) + *n * nr + nr + 2]);
  2096. i__1 = nr;
  2097. for (p = 1; p <= i__1; ++p) {
  2098. scopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1
  2099. + 1], &c__1);
  2100. sscal_(&nr, &sva[p], &v[p * v_dim1 + 1], &c__1);
  2101. /* L3970: */
  2102. }
  2103. if (nr == *n) {
  2104. /* :)) .. best case, R1 is inverted. The solution of this matrix */
  2105. /* equation is Q2*V2 = the product of the Jacobi rotations */
  2106. /* used in SGESVJ, premultiplied with the orthogonal matrix */
  2107. /* from the second QR factorization. */
  2108. strsm_("L", "U", "N", "N", &nr, &nr, &c_b35, &a[
  2109. a_offset], lda, &v[v_offset], ldv);
  2110. } else {
  2111. /* is inverted to get the product of the Jacobi rotations */
  2112. /* used in SGESVJ. The Q-factor from the second QR */
  2113. /* factorization is then built in explicitly. */
  2114. strsm_("L", "U", "T", "N", &nr, &nr, &c_b35, &work[(*
  2115. n << 1) + 1], n, &v[v_offset], ldv);
  2116. if (nr < *n) {
  2117. i__1 = *n - nr;
  2118. slaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr +
  2119. 1 + v_dim1], ldv);
  2120. i__1 = *n - nr;
  2121. slaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr +
  2122. 1) * v_dim1 + 1], ldv);
  2123. i__1 = *n - nr;
  2124. i__2 = *n - nr;
  2125. slaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr
  2126. + 1 + (nr + 1) * v_dim1], ldv);
  2127. }
  2128. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2129. sormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n,
  2130. &work[*n + 1], &v[v_offset], ldv, &work[(*n <<
  2131. 1) + *n * nr + nr + 1], &i__1, &ierr);
  2132. }
  2133. } else if (condr2 < cond_ok__) {
  2134. /* :) .. the input matrix A is very likely a relative of */
  2135. /* the Kahan matrix :) */
  2136. /* The matrix R2 is inverted. The solution of the matrix equation */
  2137. /* is Q3^T*V3 = the product of the Jacobi rotations (appplied to */
  2138. /* the lower triangular L3 from the LQ factorization of */
  2139. /* R2=L3*Q3), pre-multiplied with the transposed Q3. */
  2140. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2141. sgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
  2142. 1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
  2143. nr + nr + 1], &i__1, info);
  2144. scalem = work[(*n << 1) + *n * nr + nr + 1];
  2145. numrank = i_nint(&work[(*n << 1) + *n * nr + nr + 2]);
  2146. i__1 = nr;
  2147. for (p = 1; p <= i__1; ++p) {
  2148. scopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1
  2149. + 1], &c__1);
  2150. sscal_(&nr, &sva[p], &u[p * u_dim1 + 1], &c__1);
  2151. /* L3870: */
  2152. }
  2153. strsm_("L", "U", "N", "N", &nr, &nr, &c_b35, &work[(*n <<
  2154. 1) + 1], n, &u[u_offset], ldu);
  2155. i__1 = nr;
  2156. for (q = 1; q <= i__1; ++q) {
  2157. i__2 = nr;
  2158. for (p = 1; p <= i__2; ++p) {
  2159. work[(*n << 1) + *n * nr + nr + iwork[*n + p]] =
  2160. u[p + q * u_dim1];
  2161. /* L872: */
  2162. }
  2163. i__2 = nr;
  2164. for (p = 1; p <= i__2; ++p) {
  2165. u[p + q * u_dim1] = work[(*n << 1) + *n * nr + nr
  2166. + p];
  2167. /* L874: */
  2168. }
  2169. /* L873: */
  2170. }
  2171. if (nr < *n) {
  2172. i__1 = *n - nr;
  2173. slaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 +
  2174. v_dim1], ldv);
  2175. i__1 = *n - nr;
  2176. slaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) *
  2177. v_dim1 + 1], ldv);
  2178. i__1 = *n - nr;
  2179. i__2 = *n - nr;
  2180. slaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1
  2181. + (nr + 1) * v_dim1], ldv);
  2182. }
  2183. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2184. sormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &
  2185. work[*n + 1], &v[v_offset], ldv, &work[(*n << 1)
  2186. + *n * nr + nr + 1], &i__1, &ierr);
  2187. } else {
  2188. /* Last line of defense. */
  2189. /* #:( This is a rather pathological case: no scaled condition */
  2190. /* improvement after two pivoted QR factorizations. Other */
  2191. /* possibility is that the rank revealing QR factorization */
  2192. /* or the condition estimator has failed, or the COND_OK */
  2193. /* is set very close to ONE (which is unnecessary). Normally, */
  2194. /* this branch should never be executed, but in rare cases of */
  2195. /* failure of the RRQR or condition estimator, the last line of */
  2196. /* defense ensures that SGEJSV completes the task. */
  2197. /* Compute the full SVD of L3 using SGESVJ with explicit */
  2198. /* accumulation of Jacobi rotations. */
  2199. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2200. sgesvj_("L", "U", "V", &nr, &nr, &v[v_offset], ldv, &sva[
  2201. 1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
  2202. nr + nr + 1], &i__1, info);
  2203. scalem = work[(*n << 1) + *n * nr + nr + 1];
  2204. numrank = i_nint(&work[(*n << 1) + *n * nr + nr + 2]);
  2205. if (nr < *n) {
  2206. i__1 = *n - nr;
  2207. slaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 +
  2208. v_dim1], ldv);
  2209. i__1 = *n - nr;
  2210. slaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) *
  2211. v_dim1 + 1], ldv);
  2212. i__1 = *n - nr;
  2213. i__2 = *n - nr;
  2214. slaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1
  2215. + (nr + 1) * v_dim1], ldv);
  2216. }
  2217. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2218. sormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &
  2219. work[*n + 1], &v[v_offset], ldv, &work[(*n << 1)
  2220. + *n * nr + nr + 1], &i__1, &ierr);
  2221. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2222. sormlq_("L", "T", &nr, &nr, &nr, &work[(*n << 1) + 1], n,
  2223. &work[(*n << 1) + *n * nr + 1], &u[u_offset], ldu,
  2224. &work[(*n << 1) + *n * nr + nr + 1], &i__1, &
  2225. ierr);
  2226. i__1 = nr;
  2227. for (q = 1; q <= i__1; ++q) {
  2228. i__2 = nr;
  2229. for (p = 1; p <= i__2; ++p) {
  2230. work[(*n << 1) + *n * nr + nr + iwork[*n + p]] =
  2231. u[p + q * u_dim1];
  2232. /* L772: */
  2233. }
  2234. i__2 = nr;
  2235. for (p = 1; p <= i__2; ++p) {
  2236. u[p + q * u_dim1] = work[(*n << 1) + *n * nr + nr
  2237. + p];
  2238. /* L774: */
  2239. }
  2240. /* L773: */
  2241. }
  2242. }
  2243. /* Permute the rows of V using the (column) permutation from the */
  2244. /* first QRF. Also, scale the columns to make them unit in */
  2245. /* Euclidean norm. This applies to all cases. */
  2246. temp1 = sqrt((real) (*n)) * epsln;
  2247. i__1 = *n;
  2248. for (q = 1; q <= i__1; ++q) {
  2249. i__2 = *n;
  2250. for (p = 1; p <= i__2; ++p) {
  2251. work[(*n << 1) + *n * nr + nr + iwork[p]] = v[p + q *
  2252. v_dim1];
  2253. /* L972: */
  2254. }
  2255. i__2 = *n;
  2256. for (p = 1; p <= i__2; ++p) {
  2257. v[p + q * v_dim1] = work[(*n << 1) + *n * nr + nr + p]
  2258. ;
  2259. /* L973: */
  2260. }
  2261. xsc = 1.f / snrm2_(n, &v[q * v_dim1 + 1], &c__1);
  2262. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  2263. sscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
  2264. }
  2265. /* L1972: */
  2266. }
  2267. /* At this moment, V contains the right singular vectors of A. */
  2268. /* Next, assemble the left singular vector matrix U (M x N). */
  2269. if (nr < *m) {
  2270. i__1 = *m - nr;
  2271. slaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 +
  2272. u_dim1], ldu);
  2273. if (nr < n1) {
  2274. i__1 = n1 - nr;
  2275. slaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) *
  2276. u_dim1 + 1], ldu);
  2277. i__1 = *m - nr;
  2278. i__2 = n1 - nr;
  2279. slaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1
  2280. + (nr + 1) * u_dim1], ldu);
  2281. }
  2282. }
  2283. /* The Q matrix from the first QRF is built into the left singular */
  2284. /* matrix U. This applies to all cases. */
  2285. i__1 = *lwork - *n;
  2286. sormqr_("Left", "No_Tr", m, &n1, n, &a[a_offset], lda, &work[
  2287. 1], &u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  2288. /* The columns of U are normalized. The cost is O(M*N) flops. */
  2289. temp1 = sqrt((real) (*m)) * epsln;
  2290. i__1 = nr;
  2291. for (p = 1; p <= i__1; ++p) {
  2292. xsc = 1.f / snrm2_(m, &u[p * u_dim1 + 1], &c__1);
  2293. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  2294. sscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  2295. }
  2296. /* L1973: */
  2297. }
  2298. /* If the initial QRF is computed with row pivoting, the left */
  2299. /* singular vectors must be adjusted. */
  2300. if (rowpiv) {
  2301. i__1 = *m - 1;
  2302. slaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n
  2303. << 1) + 1], &c_n1);
  2304. }
  2305. } else {
  2306. /* the second QRF is not needed */
  2307. slacpy_("Upper", n, n, &a[a_offset], lda, &work[*n + 1], n);
  2308. if (l2pert) {
  2309. xsc = sqrt(small);
  2310. i__1 = *n;
  2311. for (p = 2; p <= i__1; ++p) {
  2312. temp1 = xsc * work[*n + (p - 1) * *n + p];
  2313. i__2 = p - 1;
  2314. for (q = 1; q <= i__2; ++q) {
  2315. work[*n + (q - 1) * *n + p] = -r_sign(&temp1, &
  2316. work[*n + (p - 1) * *n + q]);
  2317. /* L5971: */
  2318. }
  2319. /* L5970: */
  2320. }
  2321. } else {
  2322. i__1 = *n - 1;
  2323. i__2 = *n - 1;
  2324. slaset_("Lower", &i__1, &i__2, &c_b34, &c_b34, &work[*n +
  2325. 2], n);
  2326. }
  2327. i__1 = *lwork - *n - *n * *n;
  2328. sgesvj_("Upper", "U", "N", n, n, &work[*n + 1], n, &sva[1], n,
  2329. &u[u_offset], ldu, &work[*n + *n * *n + 1], &i__1,
  2330. info);
  2331. scalem = work[*n + *n * *n + 1];
  2332. numrank = i_nint(&work[*n + *n * *n + 2]);
  2333. i__1 = *n;
  2334. for (p = 1; p <= i__1; ++p) {
  2335. scopy_(n, &work[*n + (p - 1) * *n + 1], &c__1, &u[p *
  2336. u_dim1 + 1], &c__1);
  2337. sscal_(n, &sva[p], &work[*n + (p - 1) * *n + 1], &c__1);
  2338. /* L6970: */
  2339. }
  2340. strsm_("Left", "Upper", "NoTrans", "No UD", n, n, &c_b35, &a[
  2341. a_offset], lda, &work[*n + 1], n);
  2342. i__1 = *n;
  2343. for (p = 1; p <= i__1; ++p) {
  2344. scopy_(n, &work[*n + p], n, &v[iwork[p] + v_dim1], ldv);
  2345. /* L6972: */
  2346. }
  2347. temp1 = sqrt((real) (*n)) * epsln;
  2348. i__1 = *n;
  2349. for (p = 1; p <= i__1; ++p) {
  2350. xsc = 1.f / snrm2_(n, &v[p * v_dim1 + 1], &c__1);
  2351. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  2352. sscal_(n, &xsc, &v[p * v_dim1 + 1], &c__1);
  2353. }
  2354. /* L6971: */
  2355. }
  2356. /* Assemble the left singular vector matrix U (M x N). */
  2357. if (*n < *m) {
  2358. i__1 = *m - *n;
  2359. slaset_("A", &i__1, n, &c_b34, &c_b34, &u[*n + 1 + u_dim1]
  2360. , ldu);
  2361. if (*n < n1) {
  2362. i__1 = n1 - *n;
  2363. slaset_("A", n, &i__1, &c_b34, &c_b34, &u[(*n + 1) *
  2364. u_dim1 + 1], ldu);
  2365. i__1 = *m - *n;
  2366. i__2 = n1 - *n;
  2367. slaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[*n + 1
  2368. + (*n + 1) * u_dim1], ldu);
  2369. }
  2370. }
  2371. i__1 = *lwork - *n;
  2372. sormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[
  2373. 1], &u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  2374. temp1 = sqrt((real) (*m)) * epsln;
  2375. i__1 = n1;
  2376. for (p = 1; p <= i__1; ++p) {
  2377. xsc = 1.f / snrm2_(m, &u[p * u_dim1 + 1], &c__1);
  2378. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  2379. sscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  2380. }
  2381. /* L6973: */
  2382. }
  2383. if (rowpiv) {
  2384. i__1 = *m - 1;
  2385. slaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n
  2386. << 1) + 1], &c_n1);
  2387. }
  2388. }
  2389. /* end of the >> almost orthogonal case << in the full SVD */
  2390. } else {
  2391. /* This branch deploys a preconditioned Jacobi SVD with explicitly */
  2392. /* accumulated rotations. It is included as optional, mainly for */
  2393. /* experimental purposes. It does perform well, and can also be used. */
  2394. /* In this implementation, this branch will be automatically activated */
  2395. /* if the condition number sigma_max(A) / sigma_min(A) is predicted */
  2396. /* to be greater than the overflow threshold. This is because the */
  2397. /* a posteriori computation of the singular vectors assumes robust */
  2398. /* implementation of BLAS and some LAPACK procedures, capable of working */
  2399. /* in presence of extreme values. Since that is not always the case, ... */
  2400. i__1 = nr;
  2401. for (p = 1; p <= i__1; ++p) {
  2402. i__2 = *n - p + 1;
  2403. scopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
  2404. c__1);
  2405. /* L7968: */
  2406. }
  2407. if (l2pert) {
  2408. xsc = sqrt(small / epsln);
  2409. i__1 = nr;
  2410. for (q = 1; q <= i__1; ++q) {
  2411. temp1 = xsc * (r__1 = v[q + q * v_dim1], abs(r__1));
  2412. i__2 = *n;
  2413. for (p = 1; p <= i__2; ++p) {
  2414. if (p > q && (r__1 = v[p + q * v_dim1], abs(r__1)) <=
  2415. temp1 || p < q) {
  2416. v[p + q * v_dim1] = r_sign(&temp1, &v[p + q *
  2417. v_dim1]);
  2418. }
  2419. if (p < q) {
  2420. v[p + q * v_dim1] = -v[p + q * v_dim1];
  2421. }
  2422. /* L5968: */
  2423. }
  2424. /* L5969: */
  2425. }
  2426. } else {
  2427. i__1 = nr - 1;
  2428. i__2 = nr - 1;
  2429. slaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) +
  2430. 1], ldv);
  2431. }
  2432. i__1 = *lwork - (*n << 1);
  2433. sgeqrf_(n, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*n << 1)
  2434. + 1], &i__1, &ierr);
  2435. slacpy_("L", n, &nr, &v[v_offset], ldv, &work[(*n << 1) + 1], n);
  2436. i__1 = nr;
  2437. for (p = 1; p <= i__1; ++p) {
  2438. i__2 = nr - p + 1;
  2439. scopy_(&i__2, &v[p + p * v_dim1], ldv, &u[p + p * u_dim1], &
  2440. c__1);
  2441. /* L7969: */
  2442. }
  2443. if (l2pert) {
  2444. xsc = sqrt(small / epsln);
  2445. i__1 = nr;
  2446. for (q = 2; q <= i__1; ++q) {
  2447. i__2 = q - 1;
  2448. for (p = 1; p <= i__2; ++p) {
  2449. /* Computing MIN */
  2450. r__3 = (r__1 = u[p + p * u_dim1], abs(r__1)), r__4 = (
  2451. r__2 = u[q + q * u_dim1], abs(r__2));
  2452. temp1 = xsc * f2cmin(r__3,r__4);
  2453. u[p + q * u_dim1] = -r_sign(&temp1, &u[q + p * u_dim1]
  2454. );
  2455. /* L9971: */
  2456. }
  2457. /* L9970: */
  2458. }
  2459. } else {
  2460. i__1 = nr - 1;
  2461. i__2 = nr - 1;
  2462. slaset_("U", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) +
  2463. 1], ldu);
  2464. }
  2465. i__1 = *lwork - (*n << 1) - *n * nr;
  2466. sgesvj_("L", "U", "V", &nr, &nr, &u[u_offset], ldu, &sva[1], n, &
  2467. v[v_offset], ldv, &work[(*n << 1) + *n * nr + 1], &i__1,
  2468. info);
  2469. scalem = work[(*n << 1) + *n * nr + 1];
  2470. numrank = i_nint(&work[(*n << 1) + *n * nr + 2]);
  2471. if (nr < *n) {
  2472. i__1 = *n - nr;
  2473. slaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + v_dim1],
  2474. ldv);
  2475. i__1 = *n - nr;
  2476. slaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) * v_dim1
  2477. + 1], ldv);
  2478. i__1 = *n - nr;
  2479. i__2 = *n - nr;
  2480. slaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 + (nr +
  2481. 1) * v_dim1], ldv);
  2482. }
  2483. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2484. sormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &work[*n +
  2485. 1], &v[v_offset], ldv, &work[(*n << 1) + *n * nr + nr + 1]
  2486. , &i__1, &ierr);
  2487. /* Permute the rows of V using the (column) permutation from the */
  2488. /* first QRF. Also, scale the columns to make them unit in */
  2489. /* Euclidean norm. This applies to all cases. */
  2490. temp1 = sqrt((real) (*n)) * epsln;
  2491. i__1 = *n;
  2492. for (q = 1; q <= i__1; ++q) {
  2493. i__2 = *n;
  2494. for (p = 1; p <= i__2; ++p) {
  2495. work[(*n << 1) + *n * nr + nr + iwork[p]] = v[p + q *
  2496. v_dim1];
  2497. /* L8972: */
  2498. }
  2499. i__2 = *n;
  2500. for (p = 1; p <= i__2; ++p) {
  2501. v[p + q * v_dim1] = work[(*n << 1) + *n * nr + nr + p];
  2502. /* L8973: */
  2503. }
  2504. xsc = 1.f / snrm2_(n, &v[q * v_dim1 + 1], &c__1);
  2505. if (xsc < 1.f - temp1 || xsc > temp1 + 1.f) {
  2506. sscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
  2507. }
  2508. /* L7972: */
  2509. }
  2510. /* At this moment, V contains the right singular vectors of A. */
  2511. /* Next, assemble the left singular vector matrix U (M x N). */
  2512. if (nr < *m) {
  2513. i__1 = *m - nr;
  2514. slaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 + u_dim1],
  2515. ldu);
  2516. if (nr < n1) {
  2517. i__1 = n1 - nr;
  2518. slaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) *
  2519. u_dim1 + 1], ldu);
  2520. i__1 = *m - nr;
  2521. i__2 = n1 - nr;
  2522. slaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 + (
  2523. nr + 1) * u_dim1], ldu);
  2524. }
  2525. }
  2526. i__1 = *lwork - *n;
  2527. sormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[1], &
  2528. u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  2529. if (rowpiv) {
  2530. i__1 = *m - 1;
  2531. slaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n << 1)
  2532. + 1], &c_n1);
  2533. }
  2534. }
  2535. if (transp) {
  2536. i__1 = *n;
  2537. for (p = 1; p <= i__1; ++p) {
  2538. sswap_(n, &u[p * u_dim1 + 1], &c__1, &v[p * v_dim1 + 1], &
  2539. c__1);
  2540. /* L6974: */
  2541. }
  2542. }
  2543. }
  2544. /* end of the full SVD */
  2545. /* Undo scaling, if necessary (and possible) */
  2546. if (uscal2 <= big / sva[1] * uscal1) {
  2547. slascl_("G", &c__0, &c__0, &uscal1, &uscal2, &nr, &c__1, &sva[1], n, &
  2548. ierr);
  2549. uscal1 = 1.f;
  2550. uscal2 = 1.f;
  2551. }
  2552. if (nr < *n) {
  2553. i__1 = *n;
  2554. for (p = nr + 1; p <= i__1; ++p) {
  2555. sva[p] = 0.f;
  2556. /* L3004: */
  2557. }
  2558. }
  2559. work[1] = uscal2 * scalem;
  2560. work[2] = uscal1;
  2561. if (errest) {
  2562. work[3] = sconda;
  2563. }
  2564. if (lsvec && rsvec) {
  2565. work[4] = condr1;
  2566. work[5] = condr2;
  2567. }
  2568. if (l2tran) {
  2569. work[6] = entra;
  2570. work[7] = entrat;
  2571. }
  2572. iwork[1] = nr;
  2573. iwork[2] = numrank;
  2574. iwork[3] = warning;
  2575. return 0;
  2576. } /* sgejsv_ */