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

sgejsv.c 94 kB

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