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