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

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