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.

dgejsv.c 94 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808
  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 doublereal c_b34 = 0.;
  488. static doublereal c_b35 = 1.;
  489. static integer c__0 = 0;
  490. static integer c_n1 = -1;
  491. /* > \brief \b DGEJSV */
  492. /* =========== DOCUMENTATION =========== */
  493. /* Online html documentation available at */
  494. /* http://www.netlib.org/lapack/explore-html/ */
  495. /* > \htmlonly */
  496. /* > Download DGEJSV + dependencies */
  497. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dgejsv.
  498. f"> */
  499. /* > [TGZ]</a> */
  500. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dgejsv.
  501. f"> */
  502. /* > [ZIP]</a> */
  503. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dgejsv.
  504. f"> */
  505. /* > [TXT]</a> */
  506. /* > \endhtmlonly */
  507. /* Definition: */
  508. /* =========== */
  509. /* SUBROUTINE DGEJSV( 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. /* DOUBLE PRECISION 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. /* > DGEJSV 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. /* > DGEJSV 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 DSQRT(BIG), BIG=SLAMCH('O'), then JOBR issues */
  614. /* > the licence to kill columns of A whose norm in c*A is less than */
  615. /* > DSQRT(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 DGESVJ. */
  621. /* > = 'R': RESTRICTED range for sigma(c*A) is [DSQRT(SFMIN), DSQRT(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 DGESVJ. */
  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 DOUBLE PRECISION 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 DOUBLE PRECISION 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 DOUBLE PRECISION 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 DOUBLE PRECISION 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 DOUBLE PRECISION array, dimension (LWORK) */
  754. /* > On exit, if N > 0 .AND. M > 0 (else not referenced), */
  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 DSQRT(||(R^t * R)^(-1)||_1). */
  762. /* > It is computed using DPOCON. 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, DGELQF, */
  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(DGELQF), 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: DGEJSV 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 doubleGEsing */
  872. /* > \par Further Details: */
  873. /* ===================== */
  874. /* > */
  875. /* > \verbatim */
  876. /* > */
  877. /* > DGEJSV implements a preconditioned Jacobi SVD algorithm. It uses DGEQP3, */
  878. /* > DGEQRF, and DGELQF 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 DGEJSV 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 (DGEJSV) is best used in this restricted range, */
  896. /* > meaning that singular values of magnitude below ||A||_2 / DLAMCH('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 (DGESVJ) is */
  901. /* > left to the implementer on a particular machine. */
  902. /* > The rank revealing QR factorization (in this code: DGEQP3) should be */
  903. /* > implemented as in [3]. We have a new version of DGEQP3 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 DGEJSV 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 DGEJSV 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 */ void dgejsv_(char *joba, char *jobu, char *jobv, char *jobr,
  947. char *jobt, char *jobp, integer *m, integer *n, doublereal *a,
  948. integer *lda, doublereal *sva, doublereal *u, integer *ldu,
  949. doublereal *v, integer *ldv, doublereal *work, integer *lwork,
  950. integer *iwork, integer *info)
  951. {
  952. /* System generated locals */
  953. integer a_dim1, a_offset, u_dim1, u_offset, v_dim1, v_offset, i__1, i__2,
  954. i__3, i__4, i__5, i__6, i__7, i__8, i__9, i__10, i__11, i__12;
  955. doublereal d__1, d__2, d__3, d__4;
  956. /* Local variables */
  957. logical defr;
  958. doublereal aapp, aaqq;
  959. logical kill;
  960. integer ierr;
  961. extern doublereal dnrm2_(integer *, doublereal *, integer *);
  962. doublereal temp1;
  963. integer p, q;
  964. logical jracc;
  965. extern /* Subroutine */ void dscal_(integer *, doublereal *, doublereal *,
  966. integer *);
  967. extern logical lsame_(char *, char *);
  968. doublereal small, entra, sfmin;
  969. logical lsvec;
  970. extern /* Subroutine */ void dcopy_(integer *, doublereal *, integer *,
  971. doublereal *, integer *), dswap_(integer *, doublereal *, integer
  972. *, doublereal *, integer *);
  973. doublereal epsln;
  974. logical rsvec;
  975. extern /* Subroutine */ void dtrsm_(char *, char *, char *, char *,
  976. integer *, integer *, doublereal *, doublereal *, integer *,
  977. doublereal *, integer *);
  978. integer n1;
  979. logical l2aber;
  980. extern /* Subroutine */ void dgeqp3_(integer *, integer *, doublereal *,
  981. integer *, integer *, doublereal *, doublereal *, integer *,
  982. integer *);
  983. doublereal condr1, condr2, uscal1, uscal2;
  984. logical l2kill, l2rank, l2tran, l2pert;
  985. extern doublereal dlamch_(char *);
  986. integer nr;
  987. extern /* Subroutine */ void dgelqf_(integer *, integer *, doublereal *,
  988. integer *, doublereal *, doublereal *, integer *, integer *);
  989. extern integer idamax_(integer *, doublereal *, integer *);
  990. doublereal scalem;
  991. extern /* Subroutine */ void dlascl_(char *, integer *, integer *,
  992. doublereal *, doublereal *, integer *, integer *, doublereal *,
  993. integer *, integer *);
  994. doublereal sconda;
  995. logical goscal;
  996. doublereal aatmin;
  997. extern /* Subroutine */ void dgeqrf_(integer *, integer *, doublereal *,
  998. integer *, doublereal *, doublereal *, integer *, integer *);
  999. doublereal aatmax;
  1000. extern /* Subroutine */ void dlacpy_(char *, integer *, integer *,
  1001. doublereal *, integer *, doublereal *, integer *),
  1002. dlaset_(char *, integer *, integer *, doublereal *, doublereal *,
  1003. doublereal *, integer *);
  1004. extern int xerbla_(char *, integer *, ftnlen);
  1005. logical noscal;
  1006. extern /* Subroutine */ void dpocon_(char *, integer *, doublereal *,
  1007. integer *, doublereal *, doublereal *, doublereal *, integer *,
  1008. integer *), dgesvj_(char *, char *, char *, integer *,
  1009. integer *, doublereal *, integer *, doublereal *, integer *,
  1010. doublereal *, integer *, doublereal *, integer *, integer *), dlassq_(integer *, doublereal *, integer
  1011. *, doublereal *, doublereal *);
  1012. extern int dlaswp_(integer *, doublereal *,
  1013. integer *, integer *, integer *, integer *, integer *);
  1014. doublereal entrat;
  1015. logical almort;
  1016. extern /* Subroutine */ void dorgqr_(integer *, integer *, integer *,
  1017. doublereal *, integer *, doublereal *, doublereal *, integer *,
  1018. integer *), dormlq_(char *, char *, integer *, integer *, integer
  1019. *, doublereal *, integer *, doublereal *, doublereal *, integer *,
  1020. doublereal *, integer *, integer *);
  1021. doublereal maxprj;
  1022. logical errest;
  1023. extern /* Subroutine */ void dormqr_(char *, char *, integer *, integer *,
  1024. integer *, doublereal *, integer *, doublereal *, doublereal *,
  1025. integer *, doublereal *, integer *, integer *);
  1026. logical transp, rowpiv;
  1027. doublereal big, cond_ok__, xsc, big1;
  1028. integer warning, numrank;
  1029. /* -- LAPACK computational routine (version 3.7.1) -- */
  1030. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  1031. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  1032. /* June 2016 */
  1033. /* =========================================================================== */
  1034. /* Test the input arguments */
  1035. /* Parameter adjustments */
  1036. --sva;
  1037. a_dim1 = *lda;
  1038. a_offset = 1 + a_dim1 * 1;
  1039. a -= a_offset;
  1040. u_dim1 = *ldu;
  1041. u_offset = 1 + u_dim1 * 1;
  1042. u -= u_offset;
  1043. v_dim1 = *ldv;
  1044. v_offset = 1 + v_dim1 * 1;
  1045. v -= v_offset;
  1046. --work;
  1047. --iwork;
  1048. /* Function Body */
  1049. lsvec = lsame_(jobu, "U") || lsame_(jobu, "F");
  1050. jracc = lsame_(jobv, "J");
  1051. rsvec = lsame_(jobv, "V") || jracc;
  1052. rowpiv = lsame_(joba, "F") || lsame_(joba, "G");
  1053. l2rank = lsame_(joba, "R");
  1054. l2aber = lsame_(joba, "A");
  1055. errest = lsame_(joba, "E") || lsame_(joba, "G");
  1056. l2tran = lsame_(jobt, "T");
  1057. l2kill = lsame_(jobr, "R");
  1058. defr = lsame_(jobr, "N");
  1059. l2pert = lsame_(jobp, "P");
  1060. if (! (rowpiv || l2rank || l2aber || errest || lsame_(joba, "C"))) {
  1061. *info = -1;
  1062. } else if (! (lsvec || lsame_(jobu, "N") || lsame_(
  1063. jobu, "W"))) {
  1064. *info = -2;
  1065. } else if (! (rsvec || lsame_(jobv, "N") || lsame_(
  1066. jobv, "W")) || jracc && ! lsvec) {
  1067. *info = -3;
  1068. } else if (! (l2kill || defr)) {
  1069. *info = -4;
  1070. } else if (! (l2tran || lsame_(jobt, "N"))) {
  1071. *info = -5;
  1072. } else if (! (l2pert || lsame_(jobp, "N"))) {
  1073. *info = -6;
  1074. } else if (*m < 0) {
  1075. *info = -7;
  1076. } else if (*n < 0 || *n > *m) {
  1077. *info = -8;
  1078. } else if (*lda < *m) {
  1079. *info = -10;
  1080. } else if (lsvec && *ldu < *m) {
  1081. *info = -13;
  1082. } else if (rsvec && *ldv < *n) {
  1083. *info = -15;
  1084. } else /* if(complicated condition) */ {
  1085. /* Computing MAX */
  1086. i__1 = 7, i__2 = (*n << 2) + 1, i__1 = f2cmax(i__1,i__2), i__2 = (*m <<
  1087. 1) + *n;
  1088. /* Computing MAX */
  1089. i__3 = 7, i__4 = (*n << 2) + *n * *n, i__3 = f2cmax(i__3,i__4), i__4 = (*
  1090. m << 1) + *n;
  1091. /* Computing MAX */
  1092. i__5 = 7, i__6 = (*m << 1) + *n, i__5 = f2cmax(i__5,i__6), i__6 = (*n <<
  1093. 2) + 1;
  1094. /* Computing MAX */
  1095. i__7 = 7, i__8 = (*m << 1) + *n, i__7 = f2cmax(i__7,i__8), i__8 = (*n <<
  1096. 2) + 1;
  1097. /* Computing MAX */
  1098. i__9 = (*m << 1) + *n, i__10 = *n * 6 + (*n << 1) * *n;
  1099. /* Computing MAX */
  1100. i__11 = (*m << 1) + *n, i__12 = (*n << 2) + *n * *n, i__11 = f2cmax(
  1101. i__11,i__12), i__12 = (*n << 1) + *n * *n + 6;
  1102. if (! (lsvec || rsvec || errest) && *lwork < f2cmax(i__1,i__2) || ! (
  1103. lsvec || rsvec) && errest && *lwork < f2cmax(i__3,i__4) || lsvec
  1104. && ! rsvec && *lwork < f2cmax(i__5,i__6) || rsvec && ! lsvec && *
  1105. lwork < f2cmax(i__7,i__8) || lsvec && rsvec && ! jracc && *lwork
  1106. < f2cmax(i__9,i__10) || lsvec && rsvec && jracc && *lwork < f2cmax(
  1107. i__11,i__12)) {
  1108. *info = -17;
  1109. } else {
  1110. /* #:) */
  1111. *info = 0;
  1112. }
  1113. }
  1114. if (*info != 0) {
  1115. /* #:( */
  1116. i__1 = -(*info);
  1117. xerbla_("DGEJSV", &i__1, (ftnlen)6);
  1118. return;
  1119. }
  1120. /* Quick return for void matrix (Y3K safe) */
  1121. /* #:) */
  1122. if (*m == 0 || *n == 0) {
  1123. iwork[1] = 0;
  1124. iwork[2] = 0;
  1125. iwork[3] = 0;
  1126. work[1] = 0.;
  1127. work[2] = 0.;
  1128. work[3] = 0.;
  1129. work[4] = 0.;
  1130. work[5] = 0.;
  1131. work[6] = 0.;
  1132. work[7] = 0.;
  1133. return;
  1134. }
  1135. /* Determine whether the matrix U should be M x N or M x M */
  1136. if (lsvec) {
  1137. n1 = *n;
  1138. if (lsame_(jobu, "F")) {
  1139. n1 = *m;
  1140. }
  1141. }
  1142. /* Set numerical parameters */
  1143. /* ! NOTE: Make sure DLAMCH() does not fail on the target architecture. */
  1144. epsln = dlamch_("Epsilon");
  1145. sfmin = dlamch_("SafeMinimum");
  1146. small = sfmin / epsln;
  1147. big = dlamch_("O");
  1148. /* BIG = ONE / SFMIN */
  1149. /* Initialize SVA(1:N) = diag( ||A e_i||_2 )_1^N */
  1150. /* (!) If necessary, scale SVA() to protect the largest norm from */
  1151. /* overflow. It is possible that this scaling pushes the smallest */
  1152. /* column norm left from the underflow threshold (extreme case). */
  1153. scalem = 1. / sqrt((doublereal) (*m) * (doublereal) (*n));
  1154. noscal = TRUE_;
  1155. goscal = TRUE_;
  1156. i__1 = *n;
  1157. for (p = 1; p <= i__1; ++p) {
  1158. aapp = 0.;
  1159. aaqq = 1.;
  1160. dlassq_(m, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
  1161. if (aapp > big) {
  1162. *info = -9;
  1163. i__2 = -(*info);
  1164. xerbla_("DGEJSV", &i__2, (ftnlen)6);
  1165. return;
  1166. }
  1167. aaqq = sqrt(aaqq);
  1168. if (aapp < big / aaqq && noscal) {
  1169. sva[p] = aapp * aaqq;
  1170. } else {
  1171. noscal = FALSE_;
  1172. sva[p] = aapp * (aaqq * scalem);
  1173. if (goscal) {
  1174. goscal = FALSE_;
  1175. i__2 = p - 1;
  1176. dscal_(&i__2, &scalem, &sva[1], &c__1);
  1177. }
  1178. }
  1179. /* L1874: */
  1180. }
  1181. if (noscal) {
  1182. scalem = 1.;
  1183. }
  1184. aapp = 0.;
  1185. aaqq = big;
  1186. i__1 = *n;
  1187. for (p = 1; p <= i__1; ++p) {
  1188. /* Computing MAX */
  1189. d__1 = aapp, d__2 = sva[p];
  1190. aapp = f2cmax(d__1,d__2);
  1191. if (sva[p] != 0.) {
  1192. /* Computing MIN */
  1193. d__1 = aaqq, d__2 = sva[p];
  1194. aaqq = f2cmin(d__1,d__2);
  1195. }
  1196. /* L4781: */
  1197. }
  1198. /* Quick return for zero M x N matrix */
  1199. /* #:) */
  1200. if (aapp == 0.) {
  1201. if (lsvec) {
  1202. dlaset_("G", m, &n1, &c_b34, &c_b35, &u[u_offset], ldu)
  1203. ;
  1204. }
  1205. if (rsvec) {
  1206. dlaset_("G", n, n, &c_b34, &c_b35, &v[v_offset], ldv);
  1207. }
  1208. work[1] = 1.;
  1209. work[2] = 1.;
  1210. if (errest) {
  1211. work[3] = 1.;
  1212. }
  1213. if (lsvec && rsvec) {
  1214. work[4] = 1.;
  1215. work[5] = 1.;
  1216. }
  1217. if (l2tran) {
  1218. work[6] = 0.;
  1219. work[7] = 0.;
  1220. }
  1221. iwork[1] = 0;
  1222. iwork[2] = 0;
  1223. iwork[3] = 0;
  1224. return;
  1225. }
  1226. /* Issue warning if denormalized column norms detected. Override the */
  1227. /* high relative accuracy request. Issue licence to kill columns */
  1228. /* (set them to zero) whose norm is less than sigma_max / BIG (roughly). */
  1229. /* #:( */
  1230. warning = 0;
  1231. if (aaqq <= sfmin) {
  1232. l2rank = TRUE_;
  1233. l2kill = TRUE_;
  1234. warning = 1;
  1235. }
  1236. /* Quick return for one-column matrix */
  1237. /* #:) */
  1238. if (*n == 1) {
  1239. if (lsvec) {
  1240. dlascl_("G", &c__0, &c__0, &sva[1], &scalem, m, &c__1, &a[a_dim1
  1241. + 1], lda, &ierr);
  1242. dlacpy_("A", m, &c__1, &a[a_offset], lda, &u[u_offset], ldu);
  1243. /* computing all M left singular vectors of the M x 1 matrix */
  1244. if (n1 != *n) {
  1245. i__1 = *lwork - *n;
  1246. dgeqrf_(m, n, &u[u_offset], ldu, &work[1], &work[*n + 1], &
  1247. i__1, &ierr);
  1248. i__1 = *lwork - *n;
  1249. dorgqr_(m, &n1, &c__1, &u[u_offset], ldu, &work[1], &work[*n
  1250. + 1], &i__1, &ierr);
  1251. dcopy_(m, &a[a_dim1 + 1], &c__1, &u[u_dim1 + 1], &c__1);
  1252. }
  1253. }
  1254. if (rsvec) {
  1255. v[v_dim1 + 1] = 1.;
  1256. }
  1257. if (sva[1] < big * scalem) {
  1258. sva[1] /= scalem;
  1259. scalem = 1.;
  1260. }
  1261. work[1] = 1. / scalem;
  1262. work[2] = 1.;
  1263. if (sva[1] != 0.) {
  1264. iwork[1] = 1;
  1265. if (sva[1] / scalem >= sfmin) {
  1266. iwork[2] = 1;
  1267. } else {
  1268. iwork[2] = 0;
  1269. }
  1270. } else {
  1271. iwork[1] = 0;
  1272. iwork[2] = 0;
  1273. }
  1274. iwork[3] = 0;
  1275. if (errest) {
  1276. work[3] = 1.;
  1277. }
  1278. if (lsvec && rsvec) {
  1279. work[4] = 1.;
  1280. work[5] = 1.;
  1281. }
  1282. if (l2tran) {
  1283. work[6] = 0.;
  1284. work[7] = 0.;
  1285. }
  1286. return;
  1287. }
  1288. transp = FALSE_;
  1289. l2tran = l2tran && *m == *n;
  1290. aatmax = -1.;
  1291. aatmin = big;
  1292. if (rowpiv || l2tran) {
  1293. /* Compute the row norms, needed to determine row pivoting sequence */
  1294. /* (in the case of heavily row weighted A, row pivoting is strongly */
  1295. /* advised) and to collect information needed to compare the */
  1296. /* structures of A * A^t and A^t * A (in the case L2TRAN.EQ..TRUE.). */
  1297. if (l2tran) {
  1298. i__1 = *m;
  1299. for (p = 1; p <= i__1; ++p) {
  1300. xsc = 0.;
  1301. temp1 = 1.;
  1302. dlassq_(n, &a[p + a_dim1], lda, &xsc, &temp1);
  1303. /* DLASSQ gets both the ell_2 and the ell_infinity norm */
  1304. /* in one pass through the vector */
  1305. work[*m + *n + p] = xsc * scalem;
  1306. work[*n + p] = xsc * (scalem * sqrt(temp1));
  1307. /* Computing MAX */
  1308. d__1 = aatmax, d__2 = work[*n + p];
  1309. aatmax = f2cmax(d__1,d__2);
  1310. if (work[*n + p] != 0.) {
  1311. /* Computing MIN */
  1312. d__1 = aatmin, d__2 = work[*n + p];
  1313. aatmin = f2cmin(d__1,d__2);
  1314. }
  1315. /* L1950: */
  1316. }
  1317. } else {
  1318. i__1 = *m;
  1319. for (p = 1; p <= i__1; ++p) {
  1320. work[*m + *n + p] = scalem * (d__1 = a[p + idamax_(n, &a[p +
  1321. a_dim1], lda) * a_dim1], abs(d__1));
  1322. /* Computing MAX */
  1323. d__1 = aatmax, d__2 = work[*m + *n + p];
  1324. aatmax = f2cmax(d__1,d__2);
  1325. /* Computing MIN */
  1326. d__1 = aatmin, d__2 = work[*m + *n + p];
  1327. aatmin = f2cmin(d__1,d__2);
  1328. /* L1904: */
  1329. }
  1330. }
  1331. }
  1332. /* For square matrix A try to determine whether A^t would be better */
  1333. /* input for the preconditioned Jacobi SVD, with faster convergence. */
  1334. /* The decision is based on an O(N) function of the vector of column */
  1335. /* and row norms of A, based on the Shannon entropy. This should give */
  1336. /* the right choice in most cases when the difference actually matters. */
  1337. /* It may fail and pick the slower converging side. */
  1338. entra = 0.;
  1339. entrat = 0.;
  1340. if (l2tran) {
  1341. xsc = 0.;
  1342. temp1 = 1.;
  1343. dlassq_(n, &sva[1], &c__1, &xsc, &temp1);
  1344. temp1 = 1. / temp1;
  1345. entra = 0.;
  1346. i__1 = *n;
  1347. for (p = 1; p <= i__1; ++p) {
  1348. /* Computing 2nd power */
  1349. d__1 = sva[p] / xsc;
  1350. big1 = d__1 * d__1 * temp1;
  1351. if (big1 != 0.) {
  1352. entra += big1 * log(big1);
  1353. }
  1354. /* L1113: */
  1355. }
  1356. entra = -entra / log((doublereal) (*n));
  1357. /* Now, SVA().^2/Trace(A^t * A) is a point in the probability simplex. */
  1358. /* It is derived from the diagonal of A^t * A. Do the same with the */
  1359. /* diagonal of A * A^t, compute the entropy of the corresponding */
  1360. /* probability distribution. Note that A * A^t and A^t * A have the */
  1361. /* same trace. */
  1362. entrat = 0.;
  1363. i__1 = *n + *m;
  1364. for (p = *n + 1; p <= i__1; ++p) {
  1365. /* Computing 2nd power */
  1366. d__1 = work[p] / xsc;
  1367. big1 = d__1 * d__1 * temp1;
  1368. if (big1 != 0.) {
  1369. entrat += big1 * log(big1);
  1370. }
  1371. /* L1114: */
  1372. }
  1373. entrat = -entrat / log((doublereal) (*m));
  1374. /* Analyze the entropies and decide A or A^t. Smaller entropy */
  1375. /* usually means better input for the algorithm. */
  1376. transp = entrat < entra;
  1377. /* If A^t is better than A, transpose A. */
  1378. if (transp) {
  1379. /* In an optimal implementation, this trivial transpose */
  1380. /* should be replaced with faster transpose. */
  1381. i__1 = *n - 1;
  1382. for (p = 1; p <= i__1; ++p) {
  1383. i__2 = *n;
  1384. for (q = p + 1; q <= i__2; ++q) {
  1385. temp1 = a[q + p * a_dim1];
  1386. a[q + p * a_dim1] = a[p + q * a_dim1];
  1387. a[p + q * a_dim1] = temp1;
  1388. /* L1116: */
  1389. }
  1390. /* L1115: */
  1391. }
  1392. i__1 = *n;
  1393. for (p = 1; p <= i__1; ++p) {
  1394. work[*m + *n + p] = sva[p];
  1395. sva[p] = work[*n + p];
  1396. /* L1117: */
  1397. }
  1398. temp1 = aapp;
  1399. aapp = aatmax;
  1400. aatmax = temp1;
  1401. temp1 = aaqq;
  1402. aaqq = aatmin;
  1403. aatmin = temp1;
  1404. kill = lsvec;
  1405. lsvec = rsvec;
  1406. rsvec = kill;
  1407. if (lsvec) {
  1408. n1 = *n;
  1409. }
  1410. rowpiv = TRUE_;
  1411. }
  1412. }
  1413. /* END IF L2TRAN */
  1414. /* Scale the matrix so that its maximal singular value remains less */
  1415. /* than DSQRT(BIG) -- the matrix is scaled so that its maximal column */
  1416. /* has Euclidean norm equal to DSQRT(BIG/N). The only reason to keep */
  1417. /* DSQRT(BIG) instead of BIG is the fact that DGEJSV uses LAPACK and */
  1418. /* BLAS routines that, in some implementations, are not capable of */
  1419. /* working in the full interval [SFMIN,BIG] and that they may provoke */
  1420. /* overflows in the intermediate results. If the singular values spread */
  1421. /* from SFMIN to BIG, then DGESVJ will compute them. So, in that case, */
  1422. /* one should use DGESVJ instead of DGEJSV. */
  1423. big1 = sqrt(big);
  1424. temp1 = sqrt(big / (doublereal) (*n));
  1425. dlascl_("G", &c__0, &c__0, &aapp, &temp1, n, &c__1, &sva[1], n, &ierr);
  1426. if (aaqq > aapp * sfmin) {
  1427. aaqq = aaqq / aapp * temp1;
  1428. } else {
  1429. aaqq = aaqq * temp1 / aapp;
  1430. }
  1431. temp1 *= scalem;
  1432. dlascl_("G", &c__0, &c__0, &aapp, &temp1, m, n, &a[a_offset], lda, &ierr);
  1433. /* To undo scaling at the end of this procedure, multiply the */
  1434. /* computed singular values with USCAL2 / USCAL1. */
  1435. uscal1 = temp1;
  1436. uscal2 = aapp;
  1437. if (l2kill) {
  1438. /* L2KILL enforces computation of nonzero singular values in */
  1439. /* the restricted range of condition number of the initial A, */
  1440. /* sigma_max(A) / sigma_min(A) approx. DSQRT(BIG)/DSQRT(SFMIN). */
  1441. xsc = sqrt(sfmin);
  1442. } else {
  1443. xsc = small;
  1444. /* Now, if the condition number of A is too big, */
  1445. /* sigma_max(A) / sigma_min(A) .GT. DSQRT(BIG/N) * EPSLN / SFMIN, */
  1446. /* as a precaution measure, the full SVD is computed using DGESVJ */
  1447. /* with accumulated Jacobi rotations. This provides numerically */
  1448. /* more robust computation, at the cost of slightly increased run */
  1449. /* time. Depending on the concrete implementation of BLAS and LAPACK */
  1450. /* (i.e. how they behave in presence of extreme ill-conditioning) the */
  1451. /* implementor may decide to remove this switch. */
  1452. if (aaqq < sqrt(sfmin) && lsvec && rsvec) {
  1453. jracc = TRUE_;
  1454. }
  1455. }
  1456. if (aaqq < xsc) {
  1457. i__1 = *n;
  1458. for (p = 1; p <= i__1; ++p) {
  1459. if (sva[p] < xsc) {
  1460. dlaset_("A", m, &c__1, &c_b34, &c_b34, &a[p * a_dim1 + 1],
  1461. lda);
  1462. sva[p] = 0.;
  1463. }
  1464. /* L700: */
  1465. }
  1466. }
  1467. /* Preconditioning using QR factorization with pivoting */
  1468. if (rowpiv) {
  1469. /* Optional row permutation (Bjoerck row pivoting): */
  1470. /* A result by Cox and Higham shows that the Bjoerck's */
  1471. /* row pivoting combined with standard column pivoting */
  1472. /* has similar effect as Powell-Reid complete pivoting. */
  1473. /* The ell-infinity norms of A are made nonincreasing. */
  1474. i__1 = *m - 1;
  1475. for (p = 1; p <= i__1; ++p) {
  1476. i__2 = *m - p + 1;
  1477. q = idamax_(&i__2, &work[*m + *n + p], &c__1) + p - 1;
  1478. iwork[(*n << 1) + p] = q;
  1479. if (p != q) {
  1480. temp1 = work[*m + *n + p];
  1481. work[*m + *n + p] = work[*m + *n + q];
  1482. work[*m + *n + q] = temp1;
  1483. }
  1484. /* L1952: */
  1485. }
  1486. i__1 = *m - 1;
  1487. dlaswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[(*n << 1) + 1], &
  1488. c__1);
  1489. }
  1490. /* End of the preparation phase (scaling, optional sorting and */
  1491. /* transposing, optional flushing of small columns). */
  1492. /* Preconditioning */
  1493. /* If the full SVD is needed, the right singular vectors are computed */
  1494. /* from a matrix equation, and for that we need theoretical analysis */
  1495. /* of the Businger-Golub pivoting. So we use DGEQP3 as the first RR QRF. */
  1496. /* In all other cases the first RR QRF can be chosen by other criteria */
  1497. /* (eg speed by replacing global with restricted window pivoting, such */
  1498. /* as in SGEQPX from TOMS # 782). Good results will be obtained using */
  1499. /* SGEQPX with properly (!) chosen numerical parameters. */
  1500. /* Any improvement of DGEQP3 improves overal performance of DGEJSV. */
  1501. /* A * P1 = Q1 * [ R1^t 0]^t: */
  1502. i__1 = *n;
  1503. for (p = 1; p <= i__1; ++p) {
  1504. iwork[p] = 0;
  1505. /* L1963: */
  1506. }
  1507. i__1 = *lwork - *n;
  1508. dgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &work[1], &work[*n + 1], &
  1509. i__1, &ierr);
  1510. /* The upper triangular matrix R1 from the first QRF is inspected for */
  1511. /* rank deficiency and possibilities for deflation, or possible */
  1512. /* ill-conditioning. Depending on the user specified flag L2RANK, */
  1513. /* the procedure explores possibilities to reduce the numerical */
  1514. /* rank by inspecting the computed upper triangular factor. If */
  1515. /* L2RANK or L2ABER are up, then DGEJSV will compute the SVD of */
  1516. /* A + dA, where ||dA|| <= f(M,N)*EPSLN. */
  1517. nr = 1;
  1518. if (l2aber) {
  1519. /* Standard absolute error bound suffices. All sigma_i with */
  1520. /* sigma_i < N*EPSLN*||A|| are flushed to zero. This is an */
  1521. /* aggressive enforcement of lower numerical rank by introducing a */
  1522. /* backward error of the order of N*EPSLN*||A||. */
  1523. temp1 = sqrt((doublereal) (*n)) * epsln;
  1524. i__1 = *n;
  1525. for (p = 2; p <= i__1; ++p) {
  1526. if ((d__2 = a[p + p * a_dim1], abs(d__2)) >= temp1 * (d__1 = a[
  1527. a_dim1 + 1], abs(d__1))) {
  1528. ++nr;
  1529. } else {
  1530. goto L3002;
  1531. }
  1532. /* L3001: */
  1533. }
  1534. L3002:
  1535. ;
  1536. } else if (l2rank) {
  1537. /* Sudden drop on the diagonal of R1 is used as the criterion for */
  1538. /* close-to-rank-deficient. */
  1539. temp1 = sqrt(sfmin);
  1540. i__1 = *n;
  1541. for (p = 2; p <= i__1; ++p) {
  1542. if ((d__2 = a[p + p * a_dim1], abs(d__2)) < epsln * (d__1 = a[p -
  1543. 1 + (p - 1) * a_dim1], abs(d__1)) || (d__3 = a[p + p *
  1544. a_dim1], abs(d__3)) < small || l2kill && (d__4 = a[p + p *
  1545. a_dim1], abs(d__4)) < temp1) {
  1546. goto L3402;
  1547. }
  1548. ++nr;
  1549. /* L3401: */
  1550. }
  1551. L3402:
  1552. ;
  1553. } else {
  1554. /* The goal is high relative accuracy. However, if the matrix */
  1555. /* has high scaled condition number the relative accuracy is in */
  1556. /* general not feasible. Later on, a condition number estimator */
  1557. /* will be deployed to estimate the scaled condition number. */
  1558. /* Here we just remove the underflowed part of the triangular */
  1559. /* factor. This prevents the situation in which the code is */
  1560. /* working hard to get the accuracy not warranted by the data. */
  1561. temp1 = sqrt(sfmin);
  1562. i__1 = *n;
  1563. for (p = 2; p <= i__1; ++p) {
  1564. if ((d__1 = a[p + p * a_dim1], abs(d__1)) < small || l2kill && (
  1565. d__2 = a[p + p * a_dim1], abs(d__2)) < temp1) {
  1566. goto L3302;
  1567. }
  1568. ++nr;
  1569. /* L3301: */
  1570. }
  1571. L3302:
  1572. ;
  1573. }
  1574. almort = FALSE_;
  1575. if (nr == *n) {
  1576. maxprj = 1.;
  1577. i__1 = *n;
  1578. for (p = 2; p <= i__1; ++p) {
  1579. temp1 = (d__1 = a[p + p * a_dim1], abs(d__1)) / sva[iwork[p]];
  1580. maxprj = f2cmin(maxprj,temp1);
  1581. /* L3051: */
  1582. }
  1583. /* Computing 2nd power */
  1584. d__1 = maxprj;
  1585. if (d__1 * d__1 >= 1. - (doublereal) (*n) * epsln) {
  1586. almort = TRUE_;
  1587. }
  1588. }
  1589. sconda = -1.;
  1590. condr1 = -1.;
  1591. condr2 = -1.;
  1592. if (errest) {
  1593. if (*n == nr) {
  1594. if (rsvec) {
  1595. dlacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
  1596. i__1 = *n;
  1597. for (p = 1; p <= i__1; ++p) {
  1598. temp1 = sva[iwork[p]];
  1599. d__1 = 1. / temp1;
  1600. dscal_(&p, &d__1, &v[p * v_dim1 + 1], &c__1);
  1601. /* L3053: */
  1602. }
  1603. dpocon_("U", n, &v[v_offset], ldv, &c_b35, &temp1, &work[*n +
  1604. 1], &iwork[(*n << 1) + *m + 1], &ierr);
  1605. } else if (lsvec) {
  1606. dlacpy_("U", n, n, &a[a_offset], lda, &u[u_offset], ldu);
  1607. i__1 = *n;
  1608. for (p = 1; p <= i__1; ++p) {
  1609. temp1 = sva[iwork[p]];
  1610. d__1 = 1. / temp1;
  1611. dscal_(&p, &d__1, &u[p * u_dim1 + 1], &c__1);
  1612. /* L3054: */
  1613. }
  1614. dpocon_("U", n, &u[u_offset], ldu, &c_b35, &temp1, &work[*n +
  1615. 1], &iwork[(*n << 1) + *m + 1], &ierr);
  1616. } else {
  1617. dlacpy_("U", n, n, &a[a_offset], lda, &work[*n + 1], n);
  1618. i__1 = *n;
  1619. for (p = 1; p <= i__1; ++p) {
  1620. temp1 = sva[iwork[p]];
  1621. d__1 = 1. / temp1;
  1622. dscal_(&p, &d__1, &work[*n + (p - 1) * *n + 1], &c__1);
  1623. /* L3052: */
  1624. }
  1625. dpocon_("U", n, &work[*n + 1], n, &c_b35, &temp1, &work[*n + *
  1626. n * *n + 1], &iwork[(*n << 1) + *m + 1], &ierr);
  1627. }
  1628. sconda = 1. / sqrt(temp1);
  1629. /* SCONDA is an estimate of DSQRT(||(R^t * R)^(-1)||_1). */
  1630. /* N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
  1631. } else {
  1632. sconda = -1.;
  1633. }
  1634. }
  1635. l2pert = l2pert && (d__1 = a[a_dim1 + 1] / a[nr + nr * a_dim1], abs(d__1))
  1636. > sqrt(big1);
  1637. /* If there is no violent scaling, artificial perturbation is not needed. */
  1638. /* Phase 3: */
  1639. if (! (rsvec || lsvec)) {
  1640. /* Singular Values only */
  1641. /* Computing MIN */
  1642. i__2 = *n - 1;
  1643. i__1 = f2cmin(i__2,nr);
  1644. for (p = 1; p <= i__1; ++p) {
  1645. i__2 = *n - p;
  1646. dcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p *
  1647. a_dim1], &c__1);
  1648. /* L1946: */
  1649. }
  1650. /* The following two DO-loops introduce small relative perturbation */
  1651. /* into the strict upper triangle of the lower triangular matrix. */
  1652. /* Small entries below the main diagonal are also changed. */
  1653. /* This modification is useful if the computing environment does not */
  1654. /* provide/allow FLUSH TO ZERO underflow, for it prevents many */
  1655. /* annoying denormalized numbers in case of strongly scaled matrices. */
  1656. /* The perturbation is structured so that it does not introduce any */
  1657. /* new perturbation of the singular values, and it does not destroy */
  1658. /* the job done by the preconditioner. */
  1659. /* The licence for this perturbation is in the variable L2PERT, which */
  1660. /* should be .FALSE. if FLUSH TO ZERO underflow is active. */
  1661. if (! almort) {
  1662. if (l2pert) {
  1663. /* XSC = DSQRT(SMALL) */
  1664. xsc = epsln / (doublereal) (*n);
  1665. i__1 = nr;
  1666. for (q = 1; q <= i__1; ++q) {
  1667. temp1 = xsc * (d__1 = a[q + q * a_dim1], abs(d__1));
  1668. i__2 = *n;
  1669. for (p = 1; p <= i__2; ++p) {
  1670. if (p > q && (d__1 = a[p + q * a_dim1], abs(d__1)) <=
  1671. temp1 || p < q) {
  1672. a[p + q * a_dim1] = d_sign(&temp1, &a[p + q *
  1673. a_dim1]);
  1674. }
  1675. /* L4949: */
  1676. }
  1677. /* L4947: */
  1678. }
  1679. } else {
  1680. i__1 = nr - 1;
  1681. i__2 = nr - 1;
  1682. dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &a[(a_dim1 << 1) +
  1683. 1], lda);
  1684. }
  1685. i__1 = *lwork - *n;
  1686. dgeqrf_(n, &nr, &a[a_offset], lda, &work[1], &work[*n + 1], &i__1,
  1687. &ierr);
  1688. i__1 = nr - 1;
  1689. for (p = 1; p <= i__1; ++p) {
  1690. i__2 = nr - p;
  1691. dcopy_(&i__2, &a[p + (p + 1) * a_dim1], lda, &a[p + 1 + p *
  1692. a_dim1], &c__1);
  1693. /* L1948: */
  1694. }
  1695. }
  1696. /* Row-cyclic Jacobi SVD algorithm with column pivoting */
  1697. /* to drown denormals */
  1698. if (l2pert) {
  1699. /* XSC = DSQRT(SMALL) */
  1700. xsc = epsln / (doublereal) (*n);
  1701. i__1 = nr;
  1702. for (q = 1; q <= i__1; ++q) {
  1703. temp1 = xsc * (d__1 = a[q + q * a_dim1], abs(d__1));
  1704. i__2 = nr;
  1705. for (p = 1; p <= i__2; ++p) {
  1706. if (p > q && (d__1 = a[p + q * a_dim1], abs(d__1)) <=
  1707. temp1 || p < q) {
  1708. a[p + q * a_dim1] = d_sign(&temp1, &a[p + q * a_dim1])
  1709. ;
  1710. }
  1711. /* L1949: */
  1712. }
  1713. /* L1947: */
  1714. }
  1715. } else {
  1716. i__1 = nr - 1;
  1717. i__2 = nr - 1;
  1718. dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &a[(a_dim1 << 1) + 1],
  1719. lda);
  1720. }
  1721. /* triangular matrix (plus perturbation which is ignored in */
  1722. /* the part which destroys triangular form (confusing?!)) */
  1723. dgesvj_("L", "NoU", "NoV", &nr, &nr, &a[a_offset], lda, &sva[1], n, &
  1724. v[v_offset], ldv, &work[1], lwork, info);
  1725. scalem = work[1];
  1726. numrank = i_dnnt(&work[2]);
  1727. } else if (rsvec && ! lsvec) {
  1728. /* -> Singular Values and Right Singular Vectors <- */
  1729. if (almort) {
  1730. i__1 = nr;
  1731. for (p = 1; p <= i__1; ++p) {
  1732. i__2 = *n - p + 1;
  1733. dcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
  1734. c__1);
  1735. /* L1998: */
  1736. }
  1737. i__1 = nr - 1;
  1738. i__2 = nr - 1;
  1739. dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) +
  1740. 1], ldv);
  1741. dgesvj_("L", "U", "N", n, &nr, &v[v_offset], ldv, &sva[1], &nr, &
  1742. a[a_offset], lda, &work[1], lwork, info);
  1743. scalem = work[1];
  1744. numrank = i_dnnt(&work[2]);
  1745. } else {
  1746. /* accumulated product of Jacobi rotations, three are perfect ) */
  1747. i__1 = nr - 1;
  1748. i__2 = nr - 1;
  1749. dlaset_("Lower", &i__1, &i__2, &c_b34, &c_b34, &a[a_dim1 + 2],
  1750. lda);
  1751. i__1 = *lwork - *n;
  1752. dgelqf_(&nr, n, &a[a_offset], lda, &work[1], &work[*n + 1], &i__1,
  1753. &ierr);
  1754. dlacpy_("Lower", &nr, &nr, &a[a_offset], lda, &v[v_offset], ldv);
  1755. i__1 = nr - 1;
  1756. i__2 = nr - 1;
  1757. dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) +
  1758. 1], ldv);
  1759. i__1 = *lwork - (*n << 1);
  1760. dgeqrf_(&nr, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*n <<
  1761. 1) + 1], &i__1, &ierr);
  1762. i__1 = nr;
  1763. for (p = 1; p <= i__1; ++p) {
  1764. i__2 = nr - p + 1;
  1765. dcopy_(&i__2, &v[p + p * v_dim1], ldv, &v[p + p * v_dim1], &
  1766. c__1);
  1767. /* L8998: */
  1768. }
  1769. i__1 = nr - 1;
  1770. i__2 = nr - 1;
  1771. dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) +
  1772. 1], ldv);
  1773. dgesvj_("Lower", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[1], &
  1774. nr, &u[u_offset], ldu, &work[*n + 1], lwork, info);
  1775. scalem = work[*n + 1];
  1776. numrank = i_dnnt(&work[*n + 2]);
  1777. if (nr < *n) {
  1778. i__1 = *n - nr;
  1779. dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + v_dim1],
  1780. ldv);
  1781. i__1 = *n - nr;
  1782. dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) * v_dim1
  1783. + 1], ldv);
  1784. i__1 = *n - nr;
  1785. i__2 = *n - nr;
  1786. dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 + (nr +
  1787. 1) * v_dim1], ldv);
  1788. }
  1789. i__1 = *lwork - *n;
  1790. dormlq_("Left", "Transpose", n, n, &nr, &a[a_offset], lda, &work[
  1791. 1], &v[v_offset], ldv, &work[*n + 1], &i__1, &ierr);
  1792. }
  1793. i__1 = *n;
  1794. for (p = 1; p <= i__1; ++p) {
  1795. dcopy_(n, &v[p + v_dim1], ldv, &a[iwork[p] + a_dim1], lda);
  1796. /* L8991: */
  1797. }
  1798. dlacpy_("All", n, n, &a[a_offset], lda, &v[v_offset], ldv);
  1799. if (transp) {
  1800. dlacpy_("All", n, n, &v[v_offset], ldv, &u[u_offset], ldu);
  1801. }
  1802. } else if (lsvec && ! rsvec) {
  1803. /* Jacobi rotations in the Jacobi iterations. */
  1804. i__1 = nr;
  1805. for (p = 1; p <= i__1; ++p) {
  1806. i__2 = *n - p + 1;
  1807. dcopy_(&i__2, &a[p + p * a_dim1], lda, &u[p + p * u_dim1], &c__1);
  1808. /* L1965: */
  1809. }
  1810. i__1 = nr - 1;
  1811. i__2 = nr - 1;
  1812. dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 1],
  1813. ldu);
  1814. i__1 = *lwork - (*n << 1);
  1815. dgeqrf_(n, &nr, &u[u_offset], ldu, &work[*n + 1], &work[(*n << 1) + 1]
  1816. , &i__1, &ierr);
  1817. i__1 = nr - 1;
  1818. for (p = 1; p <= i__1; ++p) {
  1819. i__2 = nr - p;
  1820. dcopy_(&i__2, &u[p + (p + 1) * u_dim1], ldu, &u[p + 1 + p *
  1821. u_dim1], &c__1);
  1822. /* L1967: */
  1823. }
  1824. i__1 = nr - 1;
  1825. i__2 = nr - 1;
  1826. dlaset_("Upper", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) + 1],
  1827. ldu);
  1828. i__1 = *lwork - *n;
  1829. dgesvj_("Lower", "U", "N", &nr, &nr, &u[u_offset], ldu, &sva[1], &nr,
  1830. &a[a_offset], lda, &work[*n + 1], &i__1, info);
  1831. scalem = work[*n + 1];
  1832. numrank = i_dnnt(&work[*n + 2]);
  1833. if (nr < *m) {
  1834. i__1 = *m - nr;
  1835. dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 + u_dim1], ldu);
  1836. if (nr < n1) {
  1837. i__1 = n1 - nr;
  1838. dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) * u_dim1
  1839. + 1], ldu);
  1840. i__1 = *m - nr;
  1841. i__2 = n1 - nr;
  1842. dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 + (nr +
  1843. 1) * u_dim1], ldu);
  1844. }
  1845. }
  1846. i__1 = *lwork - *n;
  1847. dormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[1], &u[
  1848. u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  1849. if (rowpiv) {
  1850. i__1 = *m - 1;
  1851. dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n << 1) +
  1852. 1], &c_n1);
  1853. }
  1854. i__1 = n1;
  1855. for (p = 1; p <= i__1; ++p) {
  1856. xsc = 1. / dnrm2_(m, &u[p * u_dim1 + 1], &c__1);
  1857. dscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  1858. /* L1974: */
  1859. }
  1860. if (transp) {
  1861. dlacpy_("All", n, n, &u[u_offset], ldu, &v[v_offset], ldv);
  1862. }
  1863. } else {
  1864. if (! jracc) {
  1865. if (! almort) {
  1866. /* Second Preconditioning Step (QRF [with pivoting]) */
  1867. /* Note that the composition of TRANSPOSE, QRF and TRANSPOSE is */
  1868. /* equivalent to an LQF CALL. Since in many libraries the QRF */
  1869. /* seems to be better optimized than the LQF, we do explicit */
  1870. /* transpose and use the QRF. This is subject to changes in an */
  1871. /* optimized implementation of DGEJSV. */
  1872. i__1 = nr;
  1873. for (p = 1; p <= i__1; ++p) {
  1874. i__2 = *n - p + 1;
  1875. dcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1],
  1876. &c__1);
  1877. /* L1968: */
  1878. }
  1879. /* denormals in the second QR factorization, where they are */
  1880. /* as good as zeros. This is done to avoid painfully slow */
  1881. /* computation with denormals. The relative size of the perturbation */
  1882. /* is a parameter that can be changed by the implementer. */
  1883. /* This perturbation device will be obsolete on machines with */
  1884. /* properly implemented arithmetic. */
  1885. /* To switch it off, set L2PERT=.FALSE. To remove it from the */
  1886. /* code, remove the action under L2PERT=.TRUE., leave the ELSE part. */
  1887. /* The following two loops should be blocked and fused with the */
  1888. /* transposed copy above. */
  1889. if (l2pert) {
  1890. xsc = sqrt(small);
  1891. i__1 = nr;
  1892. for (q = 1; q <= i__1; ++q) {
  1893. temp1 = xsc * (d__1 = v[q + q * v_dim1], abs(d__1));
  1894. i__2 = *n;
  1895. for (p = 1; p <= i__2; ++p) {
  1896. if (p > q && (d__1 = v[p + q * v_dim1], abs(d__1))
  1897. <= temp1 || p < q) {
  1898. v[p + q * v_dim1] = d_sign(&temp1, &v[p + q *
  1899. v_dim1]);
  1900. }
  1901. if (p < q) {
  1902. v[p + q * v_dim1] = -v[p + q * v_dim1];
  1903. }
  1904. /* L2968: */
  1905. }
  1906. /* L2969: */
  1907. }
  1908. } else {
  1909. i__1 = nr - 1;
  1910. i__2 = nr - 1;
  1911. dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 <<
  1912. 1) + 1], ldv);
  1913. }
  1914. /* Estimate the row scaled condition number of R1 */
  1915. /* (If R1 is rectangular, N > NR, then the condition number */
  1916. /* of the leading NR x NR submatrix is estimated.) */
  1917. dlacpy_("L", &nr, &nr, &v[v_offset], ldv, &work[(*n << 1) + 1]
  1918. , &nr);
  1919. i__1 = nr;
  1920. for (p = 1; p <= i__1; ++p) {
  1921. i__2 = nr - p + 1;
  1922. temp1 = dnrm2_(&i__2, &work[(*n << 1) + (p - 1) * nr + p],
  1923. &c__1);
  1924. i__2 = nr - p + 1;
  1925. d__1 = 1. / temp1;
  1926. dscal_(&i__2, &d__1, &work[(*n << 1) + (p - 1) * nr + p],
  1927. &c__1);
  1928. /* L3950: */
  1929. }
  1930. dpocon_("Lower", &nr, &work[(*n << 1) + 1], &nr, &c_b35, &
  1931. temp1, &work[(*n << 1) + nr * nr + 1], &iwork[*m + (*
  1932. n << 1) + 1], &ierr);
  1933. condr1 = 1. / sqrt(temp1);
  1934. /* R1 is OK for inverse <=> CONDR1 .LT. DBLE(N) */
  1935. /* more conservative <=> CONDR1 .LT. DSQRT(DBLE(N)) */
  1936. cond_ok__ = sqrt((doublereal) nr);
  1937. /* [TP] COND_OK is a tuning parameter. */
  1938. if (condr1 < cond_ok__) {
  1939. /* implementation, this QRF should be implemented as the QRF */
  1940. /* of a lower triangular matrix. */
  1941. /* R1^t = Q2 * R2 */
  1942. i__1 = *lwork - (*n << 1);
  1943. dgeqrf_(n, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*
  1944. n << 1) + 1], &i__1, &ierr);
  1945. if (l2pert) {
  1946. xsc = sqrt(small) / epsln;
  1947. i__1 = nr;
  1948. for (p = 2; p <= i__1; ++p) {
  1949. i__2 = p - 1;
  1950. for (q = 1; q <= i__2; ++q) {
  1951. /* Computing MIN */
  1952. d__3 = (d__1 = v[p + p * v_dim1], abs(d__1)),
  1953. d__4 = (d__2 = v[q + q * v_dim1], abs(
  1954. d__2));
  1955. temp1 = xsc * f2cmin(d__3,d__4);
  1956. if ((d__1 = v[q + p * v_dim1], abs(d__1)) <=
  1957. temp1) {
  1958. v[q + p * v_dim1] = d_sign(&temp1, &v[q +
  1959. p * v_dim1]);
  1960. }
  1961. /* L3958: */
  1962. }
  1963. /* L3959: */
  1964. }
  1965. }
  1966. if (nr != *n) {
  1967. dlacpy_("A", n, &nr, &v[v_offset], ldv, &work[(*n <<
  1968. 1) + 1], n);
  1969. }
  1970. i__1 = nr - 1;
  1971. for (p = 1; p <= i__1; ++p) {
  1972. i__2 = nr - p;
  1973. dcopy_(&i__2, &v[p + (p + 1) * v_dim1], ldv, &v[p + 1
  1974. + p * v_dim1], &c__1);
  1975. /* L1969: */
  1976. }
  1977. condr2 = condr1;
  1978. } else {
  1979. /* Note that windowed pivoting would be equally good */
  1980. /* numerically, and more run-time efficient. So, in */
  1981. /* an optimal implementation, the next call to DGEQP3 */
  1982. /* should be replaced with eg. CALL SGEQPX (ACM TOMS #782) */
  1983. /* with properly (carefully) chosen parameters. */
  1984. /* R1^t * P2 = Q2 * R2 */
  1985. i__1 = nr;
  1986. for (p = 1; p <= i__1; ++p) {
  1987. iwork[*n + p] = 0;
  1988. /* L3003: */
  1989. }
  1990. i__1 = *lwork - (*n << 1);
  1991. dgeqp3_(n, &nr, &v[v_offset], ldv, &iwork[*n + 1], &work[*
  1992. n + 1], &work[(*n << 1) + 1], &i__1, &ierr);
  1993. /* * CALL DGEQRF( N, NR, V, LDV, WORK(N+1), WORK(2*N+1), */
  1994. /* * $ LWORK-2*N, IERR ) */
  1995. if (l2pert) {
  1996. xsc = sqrt(small);
  1997. i__1 = nr;
  1998. for (p = 2; p <= i__1; ++p) {
  1999. i__2 = p - 1;
  2000. for (q = 1; q <= i__2; ++q) {
  2001. /* Computing MIN */
  2002. d__3 = (d__1 = v[p + p * v_dim1], abs(d__1)),
  2003. d__4 = (d__2 = v[q + q * v_dim1], abs(
  2004. d__2));
  2005. temp1 = xsc * f2cmin(d__3,d__4);
  2006. if ((d__1 = v[q + p * v_dim1], abs(d__1)) <=
  2007. temp1) {
  2008. v[q + p * v_dim1] = d_sign(&temp1, &v[q +
  2009. p * v_dim1]);
  2010. }
  2011. /* L3968: */
  2012. }
  2013. /* L3969: */
  2014. }
  2015. }
  2016. dlacpy_("A", n, &nr, &v[v_offset], ldv, &work[(*n << 1) +
  2017. 1], n);
  2018. if (l2pert) {
  2019. xsc = sqrt(small);
  2020. i__1 = nr;
  2021. for (p = 2; p <= i__1; ++p) {
  2022. i__2 = p - 1;
  2023. for (q = 1; q <= i__2; ++q) {
  2024. /* Computing MIN */
  2025. d__3 = (d__1 = v[p + p * v_dim1], abs(d__1)),
  2026. d__4 = (d__2 = v[q + q * v_dim1], abs(
  2027. d__2));
  2028. temp1 = xsc * f2cmin(d__3,d__4);
  2029. v[p + q * v_dim1] = -d_sign(&temp1, &v[q + p *
  2030. v_dim1]);
  2031. /* L8971: */
  2032. }
  2033. /* L8970: */
  2034. }
  2035. } else {
  2036. i__1 = nr - 1;
  2037. i__2 = nr - 1;
  2038. dlaset_("L", &i__1, &i__2, &c_b34, &c_b34, &v[v_dim1
  2039. + 2], ldv);
  2040. }
  2041. /* Now, compute R2 = L3 * Q3, the LQ factorization. */
  2042. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2043. dgelqf_(&nr, &nr, &v[v_offset], ldv, &work[(*n << 1) + *n
  2044. * nr + 1], &work[(*n << 1) + *n * nr + nr + 1], &
  2045. i__1, &ierr);
  2046. dlacpy_("L", &nr, &nr, &v[v_offset], ldv, &work[(*n << 1)
  2047. + *n * nr + nr + 1], &nr);
  2048. i__1 = nr;
  2049. for (p = 1; p <= i__1; ++p) {
  2050. temp1 = dnrm2_(&p, &work[(*n << 1) + *n * nr + nr + p]
  2051. , &nr);
  2052. d__1 = 1. / temp1;
  2053. dscal_(&p, &d__1, &work[(*n << 1) + *n * nr + nr + p],
  2054. &nr);
  2055. /* L4950: */
  2056. }
  2057. dpocon_("L", &nr, &work[(*n << 1) + *n * nr + nr + 1], &
  2058. nr, &c_b35, &temp1, &work[(*n << 1) + *n * nr +
  2059. nr + nr * nr + 1], &iwork[*m + (*n << 1) + 1], &
  2060. ierr);
  2061. condr2 = 1. / sqrt(temp1);
  2062. if (condr2 >= cond_ok__) {
  2063. /* (this overwrites the copy of R2, as it will not be */
  2064. /* needed in this branch, but it does not overwritte the */
  2065. /* Huseholder vectors of Q2.). */
  2066. dlacpy_("U", &nr, &nr, &v[v_offset], ldv, &work[(*n <<
  2067. 1) + 1], n);
  2068. /* WORK(2*N+N*NR+1:2*N+N*NR+N) */
  2069. }
  2070. }
  2071. if (l2pert) {
  2072. xsc = sqrt(small);
  2073. i__1 = nr;
  2074. for (q = 2; q <= i__1; ++q) {
  2075. temp1 = xsc * v[q + q * v_dim1];
  2076. i__2 = q - 1;
  2077. for (p = 1; p <= i__2; ++p) {
  2078. /* V(p,q) = - DSIGN( TEMP1, V(q,p) ) */
  2079. v[p + q * v_dim1] = -d_sign(&temp1, &v[p + q *
  2080. v_dim1]);
  2081. /* L4969: */
  2082. }
  2083. /* L4968: */
  2084. }
  2085. } else {
  2086. i__1 = nr - 1;
  2087. i__2 = nr - 1;
  2088. dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 <<
  2089. 1) + 1], ldv);
  2090. }
  2091. /* Second preconditioning finished; continue with Jacobi SVD */
  2092. /* The input matrix is lower trinagular. */
  2093. /* Recover the right singular vectors as solution of a well */
  2094. /* conditioned triangular matrix equation. */
  2095. if (condr1 < cond_ok__) {
  2096. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2097. dgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
  2098. 1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
  2099. nr + nr + 1], &i__1, info);
  2100. scalem = work[(*n << 1) + *n * nr + nr + 1];
  2101. numrank = i_dnnt(&work[(*n << 1) + *n * nr + nr + 2]);
  2102. i__1 = nr;
  2103. for (p = 1; p <= i__1; ++p) {
  2104. dcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1
  2105. + 1], &c__1);
  2106. dscal_(&nr, &sva[p], &v[p * v_dim1 + 1], &c__1);
  2107. /* L3970: */
  2108. }
  2109. if (nr == *n) {
  2110. /* :)) .. best case, R1 is inverted. The solution of this matrix */
  2111. /* equation is Q2*V2 = the product of the Jacobi rotations */
  2112. /* used in DGESVJ, premultiplied with the orthogonal matrix */
  2113. /* from the second QR factorization. */
  2114. dtrsm_("L", "U", "N", "N", &nr, &nr, &c_b35, &a[
  2115. a_offset], lda, &v[v_offset], ldv);
  2116. } else {
  2117. /* is inverted to get the product of the Jacobi rotations */
  2118. /* used in DGESVJ. The Q-factor from the second QR */
  2119. /* factorization is then built in explicitly. */
  2120. dtrsm_("L", "U", "T", "N", &nr, &nr, &c_b35, &work[(*
  2121. n << 1) + 1], n, &v[v_offset], ldv);
  2122. if (nr < *n) {
  2123. i__1 = *n - nr;
  2124. dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr +
  2125. 1 + v_dim1], ldv);
  2126. i__1 = *n - nr;
  2127. dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr +
  2128. 1) * v_dim1 + 1], ldv);
  2129. i__1 = *n - nr;
  2130. i__2 = *n - nr;
  2131. dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr
  2132. + 1 + (nr + 1) * v_dim1], ldv);
  2133. }
  2134. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2135. dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n,
  2136. &work[*n + 1], &v[v_offset], ldv, &work[(*n <<
  2137. 1) + *n * nr + nr + 1], &i__1, &ierr);
  2138. }
  2139. } else if (condr2 < cond_ok__) {
  2140. /* :) .. the input matrix A is very likely a relative of */
  2141. /* the Kahan matrix :) */
  2142. /* The matrix R2 is inverted. The solution of the matrix equation */
  2143. /* is Q3^T*V3 = the product of the Jacobi rotations (appplied to */
  2144. /* the lower triangular L3 from the LQ factorization of */
  2145. /* R2=L3*Q3), pre-multiplied with the transposed Q3. */
  2146. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2147. dgesvj_("L", "U", "N", &nr, &nr, &v[v_offset], ldv, &sva[
  2148. 1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
  2149. nr + nr + 1], &i__1, info);
  2150. scalem = work[(*n << 1) + *n * nr + nr + 1];
  2151. numrank = i_dnnt(&work[(*n << 1) + *n * nr + nr + 2]);
  2152. i__1 = nr;
  2153. for (p = 1; p <= i__1; ++p) {
  2154. dcopy_(&nr, &v[p * v_dim1 + 1], &c__1, &u[p * u_dim1
  2155. + 1], &c__1);
  2156. dscal_(&nr, &sva[p], &u[p * u_dim1 + 1], &c__1);
  2157. /* L3870: */
  2158. }
  2159. dtrsm_("L", "U", "N", "N", &nr, &nr, &c_b35, &work[(*n <<
  2160. 1) + 1], n, &u[u_offset], ldu);
  2161. i__1 = nr;
  2162. for (q = 1; q <= i__1; ++q) {
  2163. i__2 = nr;
  2164. for (p = 1; p <= i__2; ++p) {
  2165. work[(*n << 1) + *n * nr + nr + iwork[*n + p]] =
  2166. u[p + q * u_dim1];
  2167. /* L872: */
  2168. }
  2169. i__2 = nr;
  2170. for (p = 1; p <= i__2; ++p) {
  2171. u[p + q * u_dim1] = work[(*n << 1) + *n * nr + nr
  2172. + p];
  2173. /* L874: */
  2174. }
  2175. /* L873: */
  2176. }
  2177. if (nr < *n) {
  2178. i__1 = *n - nr;
  2179. dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 +
  2180. v_dim1], ldv);
  2181. i__1 = *n - nr;
  2182. dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) *
  2183. v_dim1 + 1], ldv);
  2184. i__1 = *n - nr;
  2185. i__2 = *n - nr;
  2186. dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1
  2187. + (nr + 1) * v_dim1], ldv);
  2188. }
  2189. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2190. dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &
  2191. work[*n + 1], &v[v_offset], ldv, &work[(*n << 1)
  2192. + *n * nr + nr + 1], &i__1, &ierr);
  2193. } else {
  2194. /* Last line of defense. */
  2195. /* #:( This is a rather pathological case: no scaled condition */
  2196. /* improvement after two pivoted QR factorizations. Other */
  2197. /* possibility is that the rank revealing QR factorization */
  2198. /* or the condition estimator has failed, or the COND_OK */
  2199. /* is set very close to ONE (which is unnecessary). Normally, */
  2200. /* this branch should never be executed, but in rare cases of */
  2201. /* failure of the RRQR or condition estimator, the last line of */
  2202. /* defense ensures that DGEJSV completes the task. */
  2203. /* Compute the full SVD of L3 using DGESVJ with explicit */
  2204. /* accumulation of Jacobi rotations. */
  2205. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2206. dgesvj_("L", "U", "V", &nr, &nr, &v[v_offset], ldv, &sva[
  2207. 1], &nr, &u[u_offset], ldu, &work[(*n << 1) + *n *
  2208. nr + nr + 1], &i__1, info);
  2209. scalem = work[(*n << 1) + *n * nr + nr + 1];
  2210. numrank = i_dnnt(&work[(*n << 1) + *n * nr + nr + 2]);
  2211. if (nr < *n) {
  2212. i__1 = *n - nr;
  2213. dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 +
  2214. v_dim1], ldv);
  2215. i__1 = *n - nr;
  2216. dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) *
  2217. v_dim1 + 1], ldv);
  2218. i__1 = *n - nr;
  2219. i__2 = *n - nr;
  2220. dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1
  2221. + (nr + 1) * v_dim1], ldv);
  2222. }
  2223. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2224. dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &
  2225. work[*n + 1], &v[v_offset], ldv, &work[(*n << 1)
  2226. + *n * nr + nr + 1], &i__1, &ierr);
  2227. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2228. dormlq_("L", "T", &nr, &nr, &nr, &work[(*n << 1) + 1], n,
  2229. &work[(*n << 1) + *n * nr + 1], &u[u_offset], ldu,
  2230. &work[(*n << 1) + *n * nr + nr + 1], &i__1, &
  2231. ierr);
  2232. i__1 = nr;
  2233. for (q = 1; q <= i__1; ++q) {
  2234. i__2 = nr;
  2235. for (p = 1; p <= i__2; ++p) {
  2236. work[(*n << 1) + *n * nr + nr + iwork[*n + p]] =
  2237. u[p + q * u_dim1];
  2238. /* L772: */
  2239. }
  2240. i__2 = nr;
  2241. for (p = 1; p <= i__2; ++p) {
  2242. u[p + q * u_dim1] = work[(*n << 1) + *n * nr + nr
  2243. + p];
  2244. /* L774: */
  2245. }
  2246. /* L773: */
  2247. }
  2248. }
  2249. /* Permute the rows of V using the (column) permutation from the */
  2250. /* first QRF. Also, scale the columns to make them unit in */
  2251. /* Euclidean norm. This applies to all cases. */
  2252. temp1 = sqrt((doublereal) (*n)) * epsln;
  2253. i__1 = *n;
  2254. for (q = 1; q <= i__1; ++q) {
  2255. i__2 = *n;
  2256. for (p = 1; p <= i__2; ++p) {
  2257. work[(*n << 1) + *n * nr + nr + iwork[p]] = v[p + q *
  2258. v_dim1];
  2259. /* L972: */
  2260. }
  2261. i__2 = *n;
  2262. for (p = 1; p <= i__2; ++p) {
  2263. v[p + q * v_dim1] = work[(*n << 1) + *n * nr + nr + p]
  2264. ;
  2265. /* L973: */
  2266. }
  2267. xsc = 1. / dnrm2_(n, &v[q * v_dim1 + 1], &c__1);
  2268. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  2269. dscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
  2270. }
  2271. /* L1972: */
  2272. }
  2273. /* At this moment, V contains the right singular vectors of A. */
  2274. /* Next, assemble the left singular vector matrix U (M x N). */
  2275. if (nr < *m) {
  2276. i__1 = *m - nr;
  2277. dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 +
  2278. u_dim1], ldu);
  2279. if (nr < n1) {
  2280. i__1 = n1 - nr;
  2281. dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) *
  2282. u_dim1 + 1], ldu);
  2283. i__1 = *m - nr;
  2284. i__2 = n1 - nr;
  2285. dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1
  2286. + (nr + 1) * u_dim1], ldu);
  2287. }
  2288. }
  2289. /* The Q matrix from the first QRF is built into the left singular */
  2290. /* matrix U. This applies to all cases. */
  2291. i__1 = *lwork - *n;
  2292. dormqr_("Left", "No_Tr", m, &n1, n, &a[a_offset], lda, &work[
  2293. 1], &u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  2294. /* The columns of U are normalized. The cost is O(M*N) flops. */
  2295. temp1 = sqrt((doublereal) (*m)) * epsln;
  2296. i__1 = nr;
  2297. for (p = 1; p <= i__1; ++p) {
  2298. xsc = 1. / dnrm2_(m, &u[p * u_dim1 + 1], &c__1);
  2299. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  2300. dscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  2301. }
  2302. /* L1973: */
  2303. }
  2304. /* If the initial QRF is computed with row pivoting, the left */
  2305. /* singular vectors must be adjusted. */
  2306. if (rowpiv) {
  2307. i__1 = *m - 1;
  2308. dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n
  2309. << 1) + 1], &c_n1);
  2310. }
  2311. } else {
  2312. /* the second QRF is not needed */
  2313. dlacpy_("Upper", n, n, &a[a_offset], lda, &work[*n + 1], n);
  2314. if (l2pert) {
  2315. xsc = sqrt(small);
  2316. i__1 = *n;
  2317. for (p = 2; p <= i__1; ++p) {
  2318. temp1 = xsc * work[*n + (p - 1) * *n + p];
  2319. i__2 = p - 1;
  2320. for (q = 1; q <= i__2; ++q) {
  2321. work[*n + (q - 1) * *n + p] = -d_sign(&temp1, &
  2322. work[*n + (p - 1) * *n + q]);
  2323. /* L5971: */
  2324. }
  2325. /* L5970: */
  2326. }
  2327. } else {
  2328. i__1 = *n - 1;
  2329. i__2 = *n - 1;
  2330. dlaset_("Lower", &i__1, &i__2, &c_b34, &c_b34, &work[*n +
  2331. 2], n);
  2332. }
  2333. i__1 = *lwork - *n - *n * *n;
  2334. dgesvj_("Upper", "U", "N", n, n, &work[*n + 1], n, &sva[1], n,
  2335. &u[u_offset], ldu, &work[*n + *n * *n + 1], &i__1,
  2336. info);
  2337. scalem = work[*n + *n * *n + 1];
  2338. numrank = i_dnnt(&work[*n + *n * *n + 2]);
  2339. i__1 = *n;
  2340. for (p = 1; p <= i__1; ++p) {
  2341. dcopy_(n, &work[*n + (p - 1) * *n + 1], &c__1, &u[p *
  2342. u_dim1 + 1], &c__1);
  2343. dscal_(n, &sva[p], &work[*n + (p - 1) * *n + 1], &c__1);
  2344. /* L6970: */
  2345. }
  2346. dtrsm_("Left", "Upper", "NoTrans", "No UD", n, n, &c_b35, &a[
  2347. a_offset], lda, &work[*n + 1], n);
  2348. i__1 = *n;
  2349. for (p = 1; p <= i__1; ++p) {
  2350. dcopy_(n, &work[*n + p], n, &v[iwork[p] + v_dim1], ldv);
  2351. /* L6972: */
  2352. }
  2353. temp1 = sqrt((doublereal) (*n)) * epsln;
  2354. i__1 = *n;
  2355. for (p = 1; p <= i__1; ++p) {
  2356. xsc = 1. / dnrm2_(n, &v[p * v_dim1 + 1], &c__1);
  2357. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  2358. dscal_(n, &xsc, &v[p * v_dim1 + 1], &c__1);
  2359. }
  2360. /* L6971: */
  2361. }
  2362. /* Assemble the left singular vector matrix U (M x N). */
  2363. if (*n < *m) {
  2364. i__1 = *m - *n;
  2365. dlaset_("A", &i__1, n, &c_b34, &c_b34, &u[*n + 1 + u_dim1]
  2366. , ldu);
  2367. if (*n < n1) {
  2368. i__1 = n1 - *n;
  2369. dlaset_("A", n, &i__1, &c_b34, &c_b34, &u[(*n + 1) *
  2370. u_dim1 + 1], ldu);
  2371. i__1 = *m - *n;
  2372. i__2 = n1 - *n;
  2373. dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[*n + 1
  2374. + (*n + 1) * u_dim1], ldu);
  2375. }
  2376. }
  2377. i__1 = *lwork - *n;
  2378. dormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[
  2379. 1], &u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  2380. temp1 = sqrt((doublereal) (*m)) * epsln;
  2381. i__1 = n1;
  2382. for (p = 1; p <= i__1; ++p) {
  2383. xsc = 1. / dnrm2_(m, &u[p * u_dim1 + 1], &c__1);
  2384. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  2385. dscal_(m, &xsc, &u[p * u_dim1 + 1], &c__1);
  2386. }
  2387. /* L6973: */
  2388. }
  2389. if (rowpiv) {
  2390. i__1 = *m - 1;
  2391. dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n
  2392. << 1) + 1], &c_n1);
  2393. }
  2394. }
  2395. /* end of the >> almost orthogonal case << in the full SVD */
  2396. } else {
  2397. /* This branch deploys a preconditioned Jacobi SVD with explicitly */
  2398. /* accumulated rotations. It is included as optional, mainly for */
  2399. /* experimental purposes. It does perform well, and can also be used. */
  2400. /* In this implementation, this branch will be automatically activated */
  2401. /* if the condition number sigma_max(A) / sigma_min(A) is predicted */
  2402. /* to be greater than the overflow threshold. This is because the */
  2403. /* a posteriori computation of the singular vectors assumes robust */
  2404. /* implementation of BLAS and some LAPACK procedures, capable of working */
  2405. /* in presence of extreme values. Since that is not always the case, ... */
  2406. i__1 = nr;
  2407. for (p = 1; p <= i__1; ++p) {
  2408. i__2 = *n - p + 1;
  2409. dcopy_(&i__2, &a[p + p * a_dim1], lda, &v[p + p * v_dim1], &
  2410. c__1);
  2411. /* L7968: */
  2412. }
  2413. if (l2pert) {
  2414. xsc = sqrt(small / epsln);
  2415. i__1 = nr;
  2416. for (q = 1; q <= i__1; ++q) {
  2417. temp1 = xsc * (d__1 = v[q + q * v_dim1], abs(d__1));
  2418. i__2 = *n;
  2419. for (p = 1; p <= i__2; ++p) {
  2420. if (p > q && (d__1 = v[p + q * v_dim1], abs(d__1)) <=
  2421. temp1 || p < q) {
  2422. v[p + q * v_dim1] = d_sign(&temp1, &v[p + q *
  2423. v_dim1]);
  2424. }
  2425. if (p < q) {
  2426. v[p + q * v_dim1] = -v[p + q * v_dim1];
  2427. }
  2428. /* L5968: */
  2429. }
  2430. /* L5969: */
  2431. }
  2432. } else {
  2433. i__1 = nr - 1;
  2434. i__2 = nr - 1;
  2435. dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &v[(v_dim1 << 1) +
  2436. 1], ldv);
  2437. }
  2438. i__1 = *lwork - (*n << 1);
  2439. dgeqrf_(n, &nr, &v[v_offset], ldv, &work[*n + 1], &work[(*n << 1)
  2440. + 1], &i__1, &ierr);
  2441. dlacpy_("L", n, &nr, &v[v_offset], ldv, &work[(*n << 1) + 1], n);
  2442. i__1 = nr;
  2443. for (p = 1; p <= i__1; ++p) {
  2444. i__2 = nr - p + 1;
  2445. dcopy_(&i__2, &v[p + p * v_dim1], ldv, &u[p + p * u_dim1], &
  2446. c__1);
  2447. /* L7969: */
  2448. }
  2449. if (l2pert) {
  2450. xsc = sqrt(small / epsln);
  2451. i__1 = nr;
  2452. for (q = 2; q <= i__1; ++q) {
  2453. i__2 = q - 1;
  2454. for (p = 1; p <= i__2; ++p) {
  2455. /* Computing MIN */
  2456. d__3 = (d__1 = u[p + p * u_dim1], abs(d__1)), d__4 = (
  2457. d__2 = u[q + q * u_dim1], abs(d__2));
  2458. temp1 = xsc * f2cmin(d__3,d__4);
  2459. u[p + q * u_dim1] = -d_sign(&temp1, &u[q + p * u_dim1]
  2460. );
  2461. /* L9971: */
  2462. }
  2463. /* L9970: */
  2464. }
  2465. } else {
  2466. i__1 = nr - 1;
  2467. i__2 = nr - 1;
  2468. dlaset_("U", &i__1, &i__2, &c_b34, &c_b34, &u[(u_dim1 << 1) +
  2469. 1], ldu);
  2470. }
  2471. i__1 = *lwork - (*n << 1) - *n * nr;
  2472. dgesvj_("G", "U", "V", &nr, &nr, &u[u_offset], ldu, &sva[1], n, &
  2473. v[v_offset], ldv, &work[(*n << 1) + *n * nr + 1], &i__1,
  2474. info);
  2475. scalem = work[(*n << 1) + *n * nr + 1];
  2476. numrank = i_dnnt(&work[(*n << 1) + *n * nr + 2]);
  2477. if (nr < *n) {
  2478. i__1 = *n - nr;
  2479. dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &v[nr + 1 + v_dim1],
  2480. ldv);
  2481. i__1 = *n - nr;
  2482. dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &v[(nr + 1) * v_dim1
  2483. + 1], ldv);
  2484. i__1 = *n - nr;
  2485. i__2 = *n - nr;
  2486. dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &v[nr + 1 + (nr +
  2487. 1) * v_dim1], ldv);
  2488. }
  2489. i__1 = *lwork - (*n << 1) - *n * nr - nr;
  2490. dormqr_("L", "N", n, n, &nr, &work[(*n << 1) + 1], n, &work[*n +
  2491. 1], &v[v_offset], ldv, &work[(*n << 1) + *n * nr + nr + 1]
  2492. , &i__1, &ierr);
  2493. /* Permute the rows of V using the (column) permutation from the */
  2494. /* first QRF. Also, scale the columns to make them unit in */
  2495. /* Euclidean norm. This applies to all cases. */
  2496. temp1 = sqrt((doublereal) (*n)) * epsln;
  2497. i__1 = *n;
  2498. for (q = 1; q <= i__1; ++q) {
  2499. i__2 = *n;
  2500. for (p = 1; p <= i__2; ++p) {
  2501. work[(*n << 1) + *n * nr + nr + iwork[p]] = v[p + q *
  2502. v_dim1];
  2503. /* L8972: */
  2504. }
  2505. i__2 = *n;
  2506. for (p = 1; p <= i__2; ++p) {
  2507. v[p + q * v_dim1] = work[(*n << 1) + *n * nr + nr + p];
  2508. /* L8973: */
  2509. }
  2510. xsc = 1. / dnrm2_(n, &v[q * v_dim1 + 1], &c__1);
  2511. if (xsc < 1. - temp1 || xsc > temp1 + 1.) {
  2512. dscal_(n, &xsc, &v[q * v_dim1 + 1], &c__1);
  2513. }
  2514. /* L7972: */
  2515. }
  2516. /* At this moment, V contains the right singular vectors of A. */
  2517. /* Next, assemble the left singular vector matrix U (M x N). */
  2518. if (nr < *m) {
  2519. i__1 = *m - nr;
  2520. dlaset_("A", &i__1, &nr, &c_b34, &c_b34, &u[nr + 1 + u_dim1],
  2521. ldu);
  2522. if (nr < n1) {
  2523. i__1 = n1 - nr;
  2524. dlaset_("A", &nr, &i__1, &c_b34, &c_b34, &u[(nr + 1) *
  2525. u_dim1 + 1], ldu);
  2526. i__1 = *m - nr;
  2527. i__2 = n1 - nr;
  2528. dlaset_("A", &i__1, &i__2, &c_b34, &c_b35, &u[nr + 1 + (
  2529. nr + 1) * u_dim1], ldu);
  2530. }
  2531. }
  2532. i__1 = *lwork - *n;
  2533. dormqr_("Left", "No Tr", m, &n1, n, &a[a_offset], lda, &work[1], &
  2534. u[u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  2535. if (rowpiv) {
  2536. i__1 = *m - 1;
  2537. dlaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[(*n << 1)
  2538. + 1], &c_n1);
  2539. }
  2540. }
  2541. if (transp) {
  2542. i__1 = *n;
  2543. for (p = 1; p <= i__1; ++p) {
  2544. dswap_(n, &u[p * u_dim1 + 1], &c__1, &v[p * v_dim1 + 1], &
  2545. c__1);
  2546. /* L6974: */
  2547. }
  2548. }
  2549. }
  2550. /* end of the full SVD */
  2551. /* Undo scaling, if necessary (and possible) */
  2552. if (uscal2 <= big / sva[1] * uscal1) {
  2553. dlascl_("G", &c__0, &c__0, &uscal1, &uscal2, &nr, &c__1, &sva[1], n, &
  2554. ierr);
  2555. uscal1 = 1.;
  2556. uscal2 = 1.;
  2557. }
  2558. if (nr < *n) {
  2559. i__1 = *n;
  2560. for (p = nr + 1; p <= i__1; ++p) {
  2561. sva[p] = 0.;
  2562. /* L3004: */
  2563. }
  2564. }
  2565. work[1] = uscal2 * scalem;
  2566. work[2] = uscal1;
  2567. if (errest) {
  2568. work[3] = sconda;
  2569. }
  2570. if (lsvec && rsvec) {
  2571. work[4] = condr1;
  2572. work[5] = condr2;
  2573. }
  2574. if (l2tran) {
  2575. work[6] = entra;
  2576. work[7] = entrat;
  2577. }
  2578. iwork[1] = nr;
  2579. iwork[2] = numrank;
  2580. iwork[3] = warning;
  2581. return;
  2582. } /* dgejsv_ */