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.

sgesvdq.c 72 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248
  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_n1 = -1;
  487. static integer c__1 = 1;
  488. static real c_b72 = 0.f;
  489. static real c_b76 = 1.f;
  490. static integer c__0 = 0;
  491. static logical c_false = FALSE_;
  492. /* > \brief <b> SGESVDQ computes the singular value decomposition (SVD) with a QR-Preconditioned QR SVD Method
  493. for GE matrices</b> */
  494. /* =========== DOCUMENTATION =========== */
  495. /* Online html documentation available at */
  496. /* http://www.netlib.org/lapack/explore-html/ */
  497. /* > \htmlonly */
  498. /* > Download SGESVDQ + dependencies */
  499. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgesvdq
  500. .f"> */
  501. /* > [TGZ]</a> */
  502. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgesvdq
  503. .f"> */
  504. /* > [ZIP]</a> */
  505. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgesvdq
  506. .f"> */
  507. /* > [TXT]</a> */
  508. /* > \endhtmlonly */
  509. /* Definition: */
  510. /* =========== */
  511. /* SUBROUTINE SGESVDQ( JOBA, JOBP, JOBR, JOBU, JOBV, M, N, A, LDA, */
  512. /* S, U, LDU, V, LDV, NUMRANK, IWORK, LIWORK, */
  513. /* WORK, LWORK, RWORK, LRWORK, INFO ) */
  514. /* IMPLICIT NONE */
  515. /* CHARACTER JOBA, JOBP, JOBR, JOBU, JOBV */
  516. /* INTEGER M, N, LDA, LDU, LDV, NUMRANK, LIWORK, LWORK, LRWORK, */
  517. /* INFO */
  518. /* REAL A( LDA, * ), U( LDU, * ), V( LDV, * ), WORK( * ) */
  519. /* REAL S( * ), RWORK( * ) */
  520. /* INTEGER IWORK( * ) */
  521. /* > \par Purpose: */
  522. /* ============= */
  523. /* > */
  524. /* > \verbatim */
  525. /* > */
  526. /* > SGESVDQ computes the singular value decomposition (SVD) of a real */
  527. /* > M-by-N matrix A, where M >= N. The SVD of A is written as */
  528. /* > [++] [xx] [x0] [xx] */
  529. /* > A = U * SIGMA * V^*, [++] = [xx] * [ox] * [xx] */
  530. /* > [++] [xx] */
  531. /* > where SIGMA is an N-by-N diagonal matrix, U is an M-by-N orthonormal */
  532. /* > matrix, and V is an N-by-N orthogonal matrix. The diagonal elements */
  533. /* > of SIGMA are the singular values of A. The columns of U and V are the */
  534. /* > left and the right singular vectors of A, respectively. */
  535. /* > \endverbatim */
  536. /* Arguments: */
  537. /* ========== */
  538. /* > \param[in] JOBA */
  539. /* > \verbatim */
  540. /* > JOBA is CHARACTER*1 */
  541. /* > Specifies the level of accuracy in the computed SVD */
  542. /* > = 'A' The requested accuracy corresponds to having the backward */
  543. /* > error bounded by || delta A ||_F <= f(m,n) * EPS * || A ||_F, */
  544. /* > where EPS = SLAMCH('Epsilon'). This authorises CGESVDQ to */
  545. /* > truncate the computed triangular factor in a rank revealing */
  546. /* > QR factorization whenever the truncated part is below the */
  547. /* > threshold of the order of EPS * ||A||_F. This is aggressive */
  548. /* > truncation level. */
  549. /* > = 'M' Similarly as with 'A', but the truncation is more gentle: it */
  550. /* > is allowed only when there is a drop on the diagonal of the */
  551. /* > triangular factor in the QR factorization. This is medium */
  552. /* > truncation level. */
  553. /* > = 'H' High accuracy requested. No numerical rank determination based */
  554. /* > on the rank revealing QR factorization is attempted. */
  555. /* > = 'E' Same as 'H', and in addition the condition number of column */
  556. /* > scaled A is estimated and returned in RWORK(1). */
  557. /* > N^(-1/4)*RWORK(1) <= ||pinv(A_scaled)||_2 <= N^(1/4)*RWORK(1) */
  558. /* > \endverbatim */
  559. /* > */
  560. /* > \param[in] JOBP */
  561. /* > \verbatim */
  562. /* > JOBP is CHARACTER*1 */
  563. /* > = 'P' The rows of A are ordered in decreasing order with respect to */
  564. /* > ||A(i,:)||_\infty. This enhances numerical accuracy at the cost */
  565. /* > of extra data movement. Recommended for numerical robustness. */
  566. /* > = 'N' No row pivoting. */
  567. /* > \endverbatim */
  568. /* > */
  569. /* > \param[in] JOBR */
  570. /* > \verbatim */
  571. /* > JOBR is CHARACTER*1 */
  572. /* > = 'T' After the initial pivoted QR factorization, SGESVD is applied to */
  573. /* > the transposed R**T of the computed triangular factor R. This involves */
  574. /* > some extra data movement (matrix transpositions). Useful for */
  575. /* > experiments, research and development. */
  576. /* > = 'N' The triangular factor R is given as input to SGESVD. This may be */
  577. /* > preferred as it involves less data movement. */
  578. /* > \endverbatim */
  579. /* > */
  580. /* > \param[in] JOBU */
  581. /* > \verbatim */
  582. /* > JOBU is CHARACTER*1 */
  583. /* > = 'A' All M left singular vectors are computed and returned in the */
  584. /* > matrix U. See the description of U. */
  585. /* > = 'S' or 'U' N = f2cmin(M,N) left singular vectors are computed and returned */
  586. /* > in the matrix U. See the description of U. */
  587. /* > = 'R' Numerical rank NUMRANK is determined and only NUMRANK left singular */
  588. /* > vectors are computed and returned in the matrix U. */
  589. /* > = 'F' The N left singular vectors are returned in factored form as the */
  590. /* > product of the Q factor from the initial QR factorization and the */
  591. /* > N left singular vectors of (R**T , 0)**T. If row pivoting is used, */
  592. /* > then the necessary information on the row pivoting is stored in */
  593. /* > IWORK(N+1:N+M-1). */
  594. /* > = 'N' The left singular vectors are not computed. */
  595. /* > \endverbatim */
  596. /* > */
  597. /* > \param[in] JOBV */
  598. /* > \verbatim */
  599. /* > JOBV is CHARACTER*1 */
  600. /* > = 'A', 'V' All N right singular vectors are computed and returned in */
  601. /* > the matrix V. */
  602. /* > = 'R' Numerical rank NUMRANK is determined and only NUMRANK right singular */
  603. /* > vectors are computed and returned in the matrix V. This option is */
  604. /* > allowed only if JOBU = 'R' or JOBU = 'N'; otherwise it is illegal. */
  605. /* > = 'N' The right singular vectors are not computed. */
  606. /* > \endverbatim */
  607. /* > */
  608. /* > \param[in] M */
  609. /* > \verbatim */
  610. /* > M is INTEGER */
  611. /* > The number of rows of the input matrix A. M >= 0. */
  612. /* > \endverbatim */
  613. /* > */
  614. /* > \param[in] N */
  615. /* > \verbatim */
  616. /* > N is INTEGER */
  617. /* > The number of columns of the input matrix A. M >= N >= 0. */
  618. /* > \endverbatim */
  619. /* > */
  620. /* > \param[in,out] A */
  621. /* > \verbatim */
  622. /* > A is REAL array of dimensions LDA x N */
  623. /* > On entry, the input matrix A. */
  624. /* > On exit, if JOBU .NE. 'N' or JOBV .NE. 'N', the lower triangle of A contains */
  625. /* > the Householder vectors as stored by SGEQP3. If JOBU = 'F', these Householder */
  626. /* > vectors together with WORK(1:N) can be used to restore the Q factors from */
  627. /* > the initial pivoted QR factorization of A. See the description of U. */
  628. /* > \endverbatim */
  629. /* > */
  630. /* > \param[in] LDA */
  631. /* > \verbatim */
  632. /* > LDA is INTEGER. */
  633. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  634. /* > \endverbatim */
  635. /* > */
  636. /* > \param[out] S */
  637. /* > \verbatim */
  638. /* > S is REAL array of dimension N. */
  639. /* > The singular values of A, ordered so that S(i) >= S(i+1). */
  640. /* > \endverbatim */
  641. /* > */
  642. /* > \param[out] U */
  643. /* > \verbatim */
  644. /* > U is REAL array, dimension */
  645. /* > LDU x M if JOBU = 'A'; see the description of LDU. In this case, */
  646. /* > on exit, U contains the M left singular vectors. */
  647. /* > LDU x N if JOBU = 'S', 'U', 'R' ; see the description of LDU. In this */
  648. /* > case, U contains the leading N or the leading NUMRANK left singular vectors. */
  649. /* > LDU x N if JOBU = 'F' ; see the description of LDU. In this case U */
  650. /* > contains N x N orthogonal matrix that can be used to form the left */
  651. /* > singular vectors. */
  652. /* > If JOBU = 'N', U is not referenced. */
  653. /* > \endverbatim */
  654. /* > */
  655. /* > \param[in] LDU */
  656. /* > \verbatim */
  657. /* > LDU is INTEGER. */
  658. /* > The leading dimension of the array U. */
  659. /* > If JOBU = 'A', 'S', 'U', 'R', LDU >= f2cmax(1,M). */
  660. /* > If JOBU = 'F', LDU >= f2cmax(1,N). */
  661. /* > Otherwise, LDU >= 1. */
  662. /* > \endverbatim */
  663. /* > */
  664. /* > \param[out] V */
  665. /* > \verbatim */
  666. /* > V is REAL array, dimension */
  667. /* > LDV x N if JOBV = 'A', 'V', 'R' or if JOBA = 'E' . */
  668. /* > If JOBV = 'A', or 'V', V contains the N-by-N orthogonal matrix V**T; */
  669. /* > If JOBV = 'R', V contains the first NUMRANK rows of V**T (the right */
  670. /* > singular vectors, stored rowwise, of the NUMRANK largest singular values). */
  671. /* > If JOBV = 'N' and JOBA = 'E', V is used as a workspace. */
  672. /* > If JOBV = 'N', and JOBA.NE.'E', V is not referenced. */
  673. /* > \endverbatim */
  674. /* > */
  675. /* > \param[in] LDV */
  676. /* > \verbatim */
  677. /* > LDV is INTEGER */
  678. /* > The leading dimension of the array V. */
  679. /* > If JOBV = 'A', 'V', 'R', or JOBA = 'E', LDV >= f2cmax(1,N). */
  680. /* > Otherwise, LDV >= 1. */
  681. /* > \endverbatim */
  682. /* > */
  683. /* > \param[out] NUMRANK */
  684. /* > \verbatim */
  685. /* > NUMRANK is INTEGER */
  686. /* > NUMRANK is the numerical rank first determined after the rank */
  687. /* > revealing QR factorization, following the strategy specified by the */
  688. /* > value of JOBA. If JOBV = 'R' and JOBU = 'R', only NUMRANK */
  689. /* > leading singular values and vectors are then requested in the call */
  690. /* > of SGESVD. The final value of NUMRANK might be further reduced if */
  691. /* > some singular values are computed as zeros. */
  692. /* > \endverbatim */
  693. /* > */
  694. /* > \param[out] IWORK */
  695. /* > \verbatim */
  696. /* > IWORK is INTEGER array, dimension (f2cmax(1, LIWORK)). */
  697. /* > On exit, IWORK(1:N) contains column pivoting permutation of the */
  698. /* > rank revealing QR factorization. */
  699. /* > If JOBP = 'P', IWORK(N+1:N+M-1) contains the indices of the sequence */
  700. /* > of row swaps used in row pivoting. These can be used to restore the */
  701. /* > left singular vectors in the case JOBU = 'F'. */
  702. /* > */
  703. /* > If LIWORK, LWORK, or LRWORK = -1, then on exit, if INFO = 0, */
  704. /* > LIWORK(1) returns the minimal LIWORK. */
  705. /* > \endverbatim */
  706. /* > */
  707. /* > \param[in] LIWORK */
  708. /* > \verbatim */
  709. /* > LIWORK is INTEGER */
  710. /* > The dimension of the array IWORK. */
  711. /* > LIWORK >= N + M - 1, if JOBP = 'P' and JOBA .NE. 'E'; */
  712. /* > LIWORK >= N if JOBP = 'N' and JOBA .NE. 'E'; */
  713. /* > LIWORK >= N + M - 1 + N, if JOBP = 'P' and JOBA = 'E'; */
  714. /* > LIWORK >= N + N if JOBP = 'N' and JOBA = 'E'. */
  715. /* > If LIWORK = -1, then a workspace query is assumed; the routine */
  716. /* > only calculates and returns the optimal and minimal sizes */
  717. /* > for the WORK, IWORK, and RWORK arrays, and no error */
  718. /* > message related to LWORK is issued by XERBLA. */
  719. /* > \endverbatim */
  720. /* > */
  721. /* > \param[out] WORK */
  722. /* > \verbatim */
  723. /* > WORK is REAL array, dimension (f2cmax(2, LWORK)), used as a workspace. */
  724. /* > On exit, if, on entry, LWORK.NE.-1, WORK(1:N) contains parameters */
  725. /* > needed to recover the Q factor from the QR factorization computed by */
  726. /* > SGEQP3. */
  727. /* > */
  728. /* > If LIWORK, LWORK, or LRWORK = -1, then on exit, if INFO = 0, */
  729. /* > WORK(1) returns the optimal LWORK, and */
  730. /* > WORK(2) returns the minimal LWORK. */
  731. /* > \endverbatim */
  732. /* > */
  733. /* > \param[in,out] LWORK */
  734. /* > \verbatim */
  735. /* > LWORK is INTEGER */
  736. /* > The dimension of the array WORK. It is determined as follows: */
  737. /* > Let LWQP3 = 3*N+1, LWCON = 3*N, and let */
  738. /* > LWORQ = { MAX( N, 1 ), if JOBU = 'R', 'S', or 'U' */
  739. /* > { MAX( M, 1 ), if JOBU = 'A' */
  740. /* > LWSVD = MAX( 5*N, 1 ) */
  741. /* > LWLQF = MAX( N/2, 1 ), LWSVD2 = MAX( 5*(N/2), 1 ), LWORLQ = MAX( N, 1 ), */
  742. /* > LWQRF = MAX( N/2, 1 ), LWORQ2 = MAX( N, 1 ) */
  743. /* > Then the minimal value of LWORK is: */
  744. /* > = MAX( N + LWQP3, LWSVD ) if only the singular values are needed; */
  745. /* > = MAX( N + LWQP3, LWCON, LWSVD ) if only the singular values are needed, */
  746. /* > and a scaled condition estimate requested; */
  747. /* > */
  748. /* > = N + MAX( LWQP3, LWSVD, LWORQ ) if the singular values and the left */
  749. /* > singular vectors are requested; */
  750. /* > = N + MAX( LWQP3, LWCON, LWSVD, LWORQ ) if the singular values and the left */
  751. /* > singular vectors are requested, and also */
  752. /* > a scaled condition estimate requested; */
  753. /* > */
  754. /* > = N + MAX( LWQP3, LWSVD ) if the singular values and the right */
  755. /* > singular vectors are requested; */
  756. /* > = N + MAX( LWQP3, LWCON, LWSVD ) if the singular values and the right */
  757. /* > singular vectors are requested, and also */
  758. /* > a scaled condition etimate requested; */
  759. /* > */
  760. /* > = N + MAX( LWQP3, LWSVD, LWORQ ) if the full SVD is requested with JOBV = 'R'; */
  761. /* > independent of JOBR; */
  762. /* > = N + MAX( LWQP3, LWCON, LWSVD, LWORQ ) if the full SVD is requested, */
  763. /* > JOBV = 'R' and, also a scaled condition */
  764. /* > estimate requested; independent of JOBR; */
  765. /* > = MAX( N + MAX( LWQP3, LWSVD, LWORQ ), */
  766. /* > N + MAX( LWQP3, N/2+LWLQF, N/2+LWSVD2, N/2+LWORLQ, LWORQ) ) if the */
  767. /* > full SVD is requested with JOBV = 'A' or 'V', and */
  768. /* > JOBR ='N' */
  769. /* > = MAX( N + MAX( LWQP3, LWCON, LWSVD, LWORQ ), */
  770. /* > N + MAX( LWQP3, LWCON, N/2+LWLQF, N/2+LWSVD2, N/2+LWORLQ, LWORQ ) ) */
  771. /* > if the full SVD is requested with JOBV = 'A' or 'V', and */
  772. /* > JOBR ='N', and also a scaled condition number estimate */
  773. /* > requested. */
  774. /* > = MAX( N + MAX( LWQP3, LWSVD, LWORQ ), */
  775. /* > N + MAX( LWQP3, N/2+LWQRF, N/2+LWSVD2, N/2+LWORQ2, LWORQ ) ) if the */
  776. /* > full SVD is requested with JOBV = 'A', 'V', and JOBR ='T' */
  777. /* > = MAX( N + MAX( LWQP3, LWCON, LWSVD, LWORQ ), */
  778. /* > N + MAX( LWQP3, LWCON, N/2+LWQRF, N/2+LWSVD2, N/2+LWORQ2, LWORQ ) ) */
  779. /* > if the full SVD is requested with JOBV = 'A' or 'V', and */
  780. /* > JOBR ='T', and also a scaled condition number estimate */
  781. /* > requested. */
  782. /* > Finally, LWORK must be at least two: LWORK = MAX( 2, LWORK ). */
  783. /* > */
  784. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  785. /* > only calculates and returns the optimal and minimal sizes */
  786. /* > for the WORK, IWORK, and RWORK arrays, and no error */
  787. /* > message related to LWORK is issued by XERBLA. */
  788. /* > \endverbatim */
  789. /* > */
  790. /* > \param[out] RWORK */
  791. /* > \verbatim */
  792. /* > RWORK is REAL array, dimension (f2cmax(1, LRWORK)). */
  793. /* > On exit, */
  794. /* > 1. If JOBA = 'E', RWORK(1) contains an estimate of the condition */
  795. /* > number of column scaled A. If A = C * D where D is diagonal and C */
  796. /* > has unit columns in the Euclidean norm, then, assuming full column rank, */
  797. /* > N^(-1/4) * RWORK(1) <= ||pinv(C)||_2 <= N^(1/4) * RWORK(1). */
  798. /* > Otherwise, RWORK(1) = -1. */
  799. /* > 2. RWORK(2) contains the number of singular values computed as */
  800. /* > exact zeros in SGESVD applied to the upper triangular or trapeziodal */
  801. /* > R (from the initial QR factorization). In case of early exit (no call to */
  802. /* > SGESVD, such as in the case of zero matrix) RWORK(2) = -1. */
  803. /* > */
  804. /* > If LIWORK, LWORK, or LRWORK = -1, then on exit, if INFO = 0, */
  805. /* > RWORK(1) returns the minimal LRWORK. */
  806. /* > \endverbatim */
  807. /* > */
  808. /* > \param[in] LRWORK */
  809. /* > \verbatim */
  810. /* > LRWORK is INTEGER. */
  811. /* > The dimension of the array RWORK. */
  812. /* > If JOBP ='P', then LRWORK >= MAX(2, M). */
  813. /* > Otherwise, LRWORK >= 2 */
  814. /* > If LRWORK = -1, then a workspace query is assumed; the routine */
  815. /* > only calculates and returns the optimal and minimal sizes */
  816. /* > for the WORK, IWORK, and RWORK arrays, and no error */
  817. /* > message related to LWORK is issued by XERBLA. */
  818. /* > \endverbatim */
  819. /* > */
  820. /* > \param[out] INFO */
  821. /* > \verbatim */
  822. /* > INFO is INTEGER */
  823. /* > = 0: successful exit. */
  824. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  825. /* > > 0: if SBDSQR did not converge, INFO specifies how many superdiagonals */
  826. /* > of an intermediate bidiagonal form B (computed in SGESVD) did not */
  827. /* > converge to zero. */
  828. /* > \endverbatim */
  829. /* > \par Further Details: */
  830. /* ======================== */
  831. /* > */
  832. /* > \verbatim */
  833. /* > */
  834. /* > 1. The data movement (matrix transpose) is coded using simple nested */
  835. /* > DO-loops because BLAS and LAPACK do not provide corresponding subroutines. */
  836. /* > Those DO-loops are easily identified in this source code - by the CONTINUE */
  837. /* > statements labeled with 11**. In an optimized version of this code, the */
  838. /* > nested DO loops should be replaced with calls to an optimized subroutine. */
  839. /* > 2. This code scales A by 1/SQRT(M) if the largest ABS(A(i,j)) could cause */
  840. /* > column norm overflow. This is the minial precaution and it is left to the */
  841. /* > SVD routine (CGESVD) to do its own preemptive scaling if potential over- */
  842. /* > or underflows are detected. To avoid repeated scanning of the array A, */
  843. /* > an optimal implementation would do all necessary scaling before calling */
  844. /* > CGESVD and the scaling in CGESVD can be switched off. */
  845. /* > 3. Other comments related to code optimization are given in comments in the */
  846. /* > code, enlosed in [[double brackets]]. */
  847. /* > \endverbatim */
  848. /* > \par Bugs, examples and comments */
  849. /* =========================== */
  850. /* > \verbatim */
  851. /* > Please report all bugs and send interesting examples and/or comments to */
  852. /* > drmac@math.hr. Thank you. */
  853. /* > \endverbatim */
  854. /* > \par References */
  855. /* =============== */
  856. /* > \verbatim */
  857. /* > [1] Zlatko Drmac, Algorithm 977: A QR-Preconditioned QR SVD Method for */
  858. /* > Computing the SVD with High Accuracy. ACM Trans. Math. Softw. */
  859. /* > 44(1): 11:1-11:30 (2017) */
  860. /* > */
  861. /* > SIGMA library, xGESVDQ section updated February 2016. */
  862. /* > Developed and coded by Zlatko Drmac, Department of Mathematics */
  863. /* > University of Zagreb, Croatia, drmac@math.hr */
  864. /* > \endverbatim */
  865. /* > \par Contributors: */
  866. /* ================== */
  867. /* > */
  868. /* > \verbatim */
  869. /* > Developed and coded by Zlatko Drmac, Department of Mathematics */
  870. /* > University of Zagreb, Croatia, drmac@math.hr */
  871. /* > \endverbatim */
  872. /* Authors: */
  873. /* ======== */
  874. /* > \author Univ. of Tennessee */
  875. /* > \author Univ. of California Berkeley */
  876. /* > \author Univ. of Colorado Denver */
  877. /* > \author NAG Ltd. */
  878. /* > \date November 2018 */
  879. /* > \ingroup realGEsing */
  880. /* ===================================================================== */
  881. /* Subroutine */ int sgesvdq_(char *joba, char *jobp, char *jobr, char *jobu,
  882. char *jobv, integer *m, integer *n, real *a, integer *lda, real *s,
  883. real *u, integer *ldu, real *v, integer *ldv, integer *numrank,
  884. integer *iwork, integer *liwork, real *work, integer *lwork, real *
  885. rwork, integer *lrwork, integer *info)
  886. {
  887. /* System generated locals */
  888. integer a_dim1, a_offset, u_dim1, u_offset, v_dim1, v_offset, i__1, i__2;
  889. real r__1, r__2, r__3;
  890. /* Local variables */
  891. integer lwrk_sormlq__, lwrk_sormqr__, ierr, lwrk_sgesvd2__;
  892. real rtmp;
  893. integer lwrk_sormqr2__, optratio;
  894. logical lsvc0;
  895. extern real snrm2_(integer *, real *, integer *);
  896. logical accla;
  897. integer lwqp3;
  898. logical acclh, acclm;
  899. integer p, q;
  900. logical conda;
  901. extern logical lsame_(char *, char *);
  902. extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
  903. integer iwoff;
  904. logical lsvec;
  905. real sfmin, epsln;
  906. integer lwcon;
  907. logical rsvec;
  908. integer lwlqf, lwqrf, n1, lwsvd;
  909. logical dntwu, dntwv, wntua;
  910. integer lworq;
  911. logical wntuf, wntva, wntur, wntus, wntvr;
  912. extern /* Subroutine */ int sgeqp3_(integer *, integer *, real *, integer
  913. *, integer *, real *, real *, integer *, integer *);
  914. integer lwsvd2, lworq2, nr;
  915. real sconda;
  916. extern real slamch_(char *), slange_(char *, integer *, integer *,
  917. real *, integer *, real *);
  918. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen), sgelqf_(
  919. integer *, integer *, real *, integer *, real *, real *, integer *
  920. , integer *), slascl_(char *, integer *, integer *, real *, real *
  921. , integer *, integer *, real *, integer *, integer *);
  922. extern integer isamax_(integer *, real *, integer *);
  923. extern /* Subroutine */ int sgeqrf_(integer *, integer *, real *, integer
  924. *, real *, real *, integer *, integer *), sgesvd_(char *, char *,
  925. integer *, integer *, real *, integer *, real *, real *, integer *
  926. , real *, integer *, real *, integer *, integer *)
  927. , slacpy_(char *, integer *, integer *, real *, integer *, real *,
  928. integer *), slaset_(char *, integer *, integer *, real *,
  929. real *, real *, integer *), slapmt_(logical *, integer *,
  930. integer *, real *, integer *, integer *), spocon_(char *,
  931. integer *, real *, integer *, real *, real *, real *, integer *,
  932. integer *);
  933. integer minwrk;
  934. logical rtrans;
  935. extern /* Subroutine */ int slaswp_(integer *, real *, integer *, integer
  936. *, integer *, integer *, integer *);
  937. real rdummy[1];
  938. extern /* Subroutine */ int sormlq_(char *, char *, integer *, integer *,
  939. integer *, real *, integer *, real *, real *, integer *, real *,
  940. integer *, integer *);
  941. logical lquery;
  942. integer lwunlq;
  943. extern /* Subroutine */ int sormqr_(char *, char *, integer *, integer *,
  944. integer *, real *, integer *, real *, real *, integer *, real *,
  945. integer *, integer *);
  946. integer optwrk;
  947. logical rowprm;
  948. real big;
  949. integer minwrk2;
  950. logical ascaled;
  951. integer optwrk2, lwrk_sgeqp3__, iminwrk, rminwrk, lwrk_sgelqf__,
  952. lwrk_sgeqrf__, lwrk_sgesvd__;
  953. /* ===================================================================== */
  954. /* Test the input arguments */
  955. /* Parameter adjustments */
  956. a_dim1 = *lda;
  957. a_offset = 1 + a_dim1 * 1;
  958. a -= a_offset;
  959. --s;
  960. u_dim1 = *ldu;
  961. u_offset = 1 + u_dim1 * 1;
  962. u -= u_offset;
  963. v_dim1 = *ldv;
  964. v_offset = 1 + v_dim1 * 1;
  965. v -= v_offset;
  966. --iwork;
  967. --work;
  968. --rwork;
  969. /* Function Body */
  970. wntus = lsame_(jobu, "S") || lsame_(jobu, "U");
  971. wntur = lsame_(jobu, "R");
  972. wntua = lsame_(jobu, "A");
  973. wntuf = lsame_(jobu, "F");
  974. lsvc0 = wntus || wntur || wntua;
  975. lsvec = lsvc0 || wntuf;
  976. dntwu = lsame_(jobu, "N");
  977. wntvr = lsame_(jobv, "R");
  978. wntva = lsame_(jobv, "A") || lsame_(jobv, "V");
  979. rsvec = wntvr || wntva;
  980. dntwv = lsame_(jobv, "N");
  981. accla = lsame_(joba, "A");
  982. acclm = lsame_(joba, "M");
  983. conda = lsame_(joba, "E");
  984. acclh = lsame_(joba, "H") || conda;
  985. rowprm = lsame_(jobp, "P");
  986. rtrans = lsame_(jobr, "T");
  987. if (rowprm) {
  988. if (conda) {
  989. /* Computing MAX */
  990. i__1 = 1, i__2 = *n + *m - 1 + *n;
  991. iminwrk = f2cmax(i__1,i__2);
  992. } else {
  993. /* Computing MAX */
  994. i__1 = 1, i__2 = *n + *m - 1;
  995. iminwrk = f2cmax(i__1,i__2);
  996. }
  997. rminwrk = f2cmax(2,*m);
  998. } else {
  999. if (conda) {
  1000. /* Computing MAX */
  1001. i__1 = 1, i__2 = *n + *n;
  1002. iminwrk = f2cmax(i__1,i__2);
  1003. } else {
  1004. iminwrk = f2cmax(1,*n);
  1005. }
  1006. rminwrk = 2;
  1007. }
  1008. lquery = *liwork == -1 || *lwork == -1 || *lrwork == -1;
  1009. *info = 0;
  1010. if (! (accla || acclm || acclh)) {
  1011. *info = -1;
  1012. } else if (! (rowprm || lsame_(jobp, "N"))) {
  1013. *info = -2;
  1014. } else if (! (rtrans || lsame_(jobr, "N"))) {
  1015. *info = -3;
  1016. } else if (! (lsvec || dntwu)) {
  1017. *info = -4;
  1018. } else if (wntur && wntva) {
  1019. *info = -5;
  1020. } else if (! (rsvec || dntwv)) {
  1021. *info = -5;
  1022. } else if (*m < 0) {
  1023. *info = -6;
  1024. } else if (*n < 0 || *n > *m) {
  1025. *info = -7;
  1026. } else if (*lda < f2cmax(1,*m)) {
  1027. *info = -9;
  1028. } else if (*ldu < 1 || lsvc0 && *ldu < *m || wntuf && *ldu < *n) {
  1029. *info = -12;
  1030. } else if (*ldv < 1 || rsvec && *ldv < *n || conda && *ldv < *n) {
  1031. *info = -14;
  1032. } else if (*liwork < iminwrk && ! lquery) {
  1033. *info = -17;
  1034. }
  1035. if (*info == 0) {
  1036. /* [[The expressions for computing the minimal and the optimal */
  1037. /* values of LWORK are written with a lot of redundancy and */
  1038. /* can be simplified. However, this detailed form is easier for */
  1039. /* maintenance and modifications of the code.]] */
  1040. lwqp3 = *n * 3 + 1;
  1041. if (wntus || wntur) {
  1042. lworq = f2cmax(*n,1);
  1043. } else if (wntua) {
  1044. lworq = f2cmax(*m,1);
  1045. }
  1046. lwcon = *n * 3;
  1047. /* Computing MAX */
  1048. i__1 = *n * 5;
  1049. lwsvd = f2cmax(i__1,1);
  1050. if (lquery) {
  1051. sgeqp3_(m, n, &a[a_offset], lda, &iwork[1], rdummy, rdummy, &c_n1,
  1052. &ierr);
  1053. lwrk_sgeqp3__ = (integer) rdummy[0];
  1054. if (wntus || wntur) {
  1055. sormqr_("L", "N", m, n, n, &a[a_offset], lda, rdummy, &u[
  1056. u_offset], ldu, rdummy, &c_n1, &ierr);
  1057. lwrk_sormqr__ = (integer) rdummy[0];
  1058. } else if (wntua) {
  1059. sormqr_("L", "N", m, m, n, &a[a_offset], lda, rdummy, &u[
  1060. u_offset], ldu, rdummy, &c_n1, &ierr);
  1061. lwrk_sormqr__ = (integer) rdummy[0];
  1062. } else {
  1063. lwrk_sormqr__ = 0;
  1064. }
  1065. }
  1066. minwrk = 2;
  1067. optwrk = 2;
  1068. if (! (lsvec || rsvec)) {
  1069. /* only the singular values are requested */
  1070. if (conda) {
  1071. /* Computing MAX */
  1072. i__1 = *n + lwqp3, i__1 = f2cmax(i__1,lwcon);
  1073. minwrk = f2cmax(i__1,lwsvd);
  1074. } else {
  1075. /* Computing MAX */
  1076. i__1 = *n + lwqp3;
  1077. minwrk = f2cmax(i__1,lwsvd);
  1078. }
  1079. if (lquery) {
  1080. sgesvd_("N", "N", n, n, &a[a_offset], lda, &s[1], &u[u_offset]
  1081. , ldu, &v[v_offset], ldv, rdummy, &c_n1, &ierr);
  1082. lwrk_sgesvd__ = (integer) rdummy[0];
  1083. if (conda) {
  1084. /* Computing MAX */
  1085. i__1 = *n + lwrk_sgeqp3__, i__2 = *n + lwcon, i__1 = f2cmax(
  1086. i__1,i__2);
  1087. optwrk = f2cmax(i__1,lwrk_sgesvd__);
  1088. } else {
  1089. /* Computing MAX */
  1090. i__1 = *n + lwrk_sgeqp3__;
  1091. optwrk = f2cmax(i__1,lwrk_sgesvd__);
  1092. }
  1093. }
  1094. } else if (lsvec && ! rsvec) {
  1095. /* singular values and the left singular vectors are requested */
  1096. if (conda) {
  1097. /* Computing MAX */
  1098. i__1 = f2cmax(lwqp3,lwcon), i__1 = f2cmax(i__1,lwsvd);
  1099. minwrk = *n + f2cmax(i__1,lworq);
  1100. } else {
  1101. /* Computing MAX */
  1102. i__1 = f2cmax(lwqp3,lwsvd);
  1103. minwrk = *n + f2cmax(i__1,lworq);
  1104. }
  1105. if (lquery) {
  1106. if (rtrans) {
  1107. sgesvd_("N", "O", n, n, &a[a_offset], lda, &s[1], &u[
  1108. u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1,
  1109. &ierr);
  1110. } else {
  1111. sgesvd_("O", "N", n, n, &a[a_offset], lda, &s[1], &u[
  1112. u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1,
  1113. &ierr);
  1114. }
  1115. lwrk_sgesvd__ = (integer) rdummy[0];
  1116. if (conda) {
  1117. /* Computing MAX */
  1118. i__1 = f2cmax(lwrk_sgeqp3__,lwcon), i__1 = f2cmax(i__1,
  1119. lwrk_sgesvd__);
  1120. optwrk = *n + f2cmax(i__1,lwrk_sormqr__);
  1121. } else {
  1122. /* Computing MAX */
  1123. i__1 = f2cmax(lwrk_sgeqp3__,lwrk_sgesvd__);
  1124. optwrk = *n + f2cmax(i__1,lwrk_sormqr__);
  1125. }
  1126. }
  1127. } else if (rsvec && ! lsvec) {
  1128. /* singular values and the right singular vectors are requested */
  1129. if (conda) {
  1130. /* Computing MAX */
  1131. i__1 = f2cmax(lwqp3,lwcon);
  1132. minwrk = *n + f2cmax(i__1,lwsvd);
  1133. } else {
  1134. minwrk = *n + f2cmax(lwqp3,lwsvd);
  1135. }
  1136. if (lquery) {
  1137. if (rtrans) {
  1138. sgesvd_("O", "N", n, n, &a[a_offset], lda, &s[1], &u[
  1139. u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1,
  1140. &ierr);
  1141. } else {
  1142. sgesvd_("N", "O", n, n, &a[a_offset], lda, &s[1], &u[
  1143. u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1,
  1144. &ierr);
  1145. }
  1146. lwrk_sgesvd__ = (integer) rdummy[0];
  1147. if (conda) {
  1148. /* Computing MAX */
  1149. i__1 = f2cmax(lwrk_sgeqp3__,lwcon);
  1150. optwrk = *n + f2cmax(i__1,lwrk_sgesvd__);
  1151. } else {
  1152. optwrk = *n + f2cmax(lwrk_sgeqp3__,lwrk_sgesvd__);
  1153. }
  1154. }
  1155. } else {
  1156. /* full SVD is requested */
  1157. if (rtrans) {
  1158. /* Computing MAX */
  1159. i__1 = f2cmax(lwqp3,lwsvd);
  1160. minwrk = f2cmax(i__1,lworq);
  1161. if (conda) {
  1162. minwrk = f2cmax(minwrk,lwcon);
  1163. }
  1164. minwrk += *n;
  1165. if (wntva) {
  1166. /* Computing MAX */
  1167. i__1 = *n / 2;
  1168. lwqrf = f2cmax(i__1,1);
  1169. /* Computing MAX */
  1170. i__1 = *n / 2 * 5;
  1171. lwsvd2 = f2cmax(i__1,1);
  1172. lworq2 = f2cmax(*n,1);
  1173. /* Computing MAX */
  1174. i__1 = lwqp3, i__2 = *n / 2 + lwqrf, i__1 = f2cmax(i__1,i__2)
  1175. , i__2 = *n / 2 + lwsvd2, i__1 = f2cmax(i__1,i__2),
  1176. i__2 = *n / 2 + lworq2, i__1 = f2cmax(i__1,i__2);
  1177. minwrk2 = f2cmax(i__1,lworq);
  1178. if (conda) {
  1179. minwrk2 = f2cmax(minwrk2,lwcon);
  1180. }
  1181. minwrk2 = *n + minwrk2;
  1182. minwrk = f2cmax(minwrk,minwrk2);
  1183. }
  1184. } else {
  1185. /* Computing MAX */
  1186. i__1 = f2cmax(lwqp3,lwsvd);
  1187. minwrk = f2cmax(i__1,lworq);
  1188. if (conda) {
  1189. minwrk = f2cmax(minwrk,lwcon);
  1190. }
  1191. minwrk += *n;
  1192. if (wntva) {
  1193. /* Computing MAX */
  1194. i__1 = *n / 2;
  1195. lwlqf = f2cmax(i__1,1);
  1196. /* Computing MAX */
  1197. i__1 = *n / 2 * 5;
  1198. lwsvd2 = f2cmax(i__1,1);
  1199. lwunlq = f2cmax(*n,1);
  1200. /* Computing MAX */
  1201. i__1 = lwqp3, i__2 = *n / 2 + lwlqf, i__1 = f2cmax(i__1,i__2)
  1202. , i__2 = *n / 2 + lwsvd2, i__1 = f2cmax(i__1,i__2),
  1203. i__2 = *n / 2 + lwunlq, i__1 = f2cmax(i__1,i__2);
  1204. minwrk2 = f2cmax(i__1,lworq);
  1205. if (conda) {
  1206. minwrk2 = f2cmax(minwrk2,lwcon);
  1207. }
  1208. minwrk2 = *n + minwrk2;
  1209. minwrk = f2cmax(minwrk,minwrk2);
  1210. }
  1211. }
  1212. if (lquery) {
  1213. if (rtrans) {
  1214. sgesvd_("O", "A", n, n, &a[a_offset], lda, &s[1], &u[
  1215. u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1,
  1216. &ierr);
  1217. lwrk_sgesvd__ = (integer) rdummy[0];
  1218. /* Computing MAX */
  1219. i__1 = f2cmax(lwrk_sgeqp3__,lwrk_sgesvd__);
  1220. optwrk = f2cmax(i__1,lwrk_sormqr__);
  1221. if (conda) {
  1222. optwrk = f2cmax(optwrk,lwcon);
  1223. }
  1224. optwrk = *n + optwrk;
  1225. if (wntva) {
  1226. i__1 = *n / 2;
  1227. sgeqrf_(n, &i__1, &u[u_offset], ldu, rdummy, rdummy, &
  1228. c_n1, &ierr);
  1229. lwrk_sgeqrf__ = (integer) rdummy[0];
  1230. i__1 = *n / 2;
  1231. i__2 = *n / 2;
  1232. sgesvd_("S", "O", &i__1, &i__2, &v[v_offset], ldv, &s[
  1233. 1], &u[u_offset], ldu, &v[v_offset], ldv,
  1234. rdummy, &c_n1, &ierr);
  1235. lwrk_sgesvd2__ = (integer) rdummy[0];
  1236. i__1 = *n / 2;
  1237. sormqr_("R", "C", n, n, &i__1, &u[u_offset], ldu,
  1238. rdummy, &v[v_offset], ldv, rdummy, &c_n1, &
  1239. ierr);
  1240. lwrk_sormqr2__ = (integer) rdummy[0];
  1241. /* Computing MAX */
  1242. i__1 = lwrk_sgeqp3__, i__2 = *n / 2 + lwrk_sgeqrf__,
  1243. i__1 = f2cmax(i__1,i__2), i__2 = *n / 2 +
  1244. lwrk_sgesvd2__, i__1 = f2cmax(i__1,i__2), i__2 =
  1245. *n / 2 + lwrk_sormqr2__;
  1246. optwrk2 = f2cmax(i__1,i__2);
  1247. if (conda) {
  1248. optwrk2 = f2cmax(optwrk2,lwcon);
  1249. }
  1250. optwrk2 = *n + optwrk2;
  1251. optwrk = f2cmax(optwrk,optwrk2);
  1252. }
  1253. } else {
  1254. sgesvd_("S", "O", n, n, &a[a_offset], lda, &s[1], &u[
  1255. u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1,
  1256. &ierr);
  1257. lwrk_sgesvd__ = (integer) rdummy[0];
  1258. /* Computing MAX */
  1259. i__1 = f2cmax(lwrk_sgeqp3__,lwrk_sgesvd__);
  1260. optwrk = f2cmax(i__1,lwrk_sormqr__);
  1261. if (conda) {
  1262. optwrk = f2cmax(optwrk,lwcon);
  1263. }
  1264. optwrk = *n + optwrk;
  1265. if (wntva) {
  1266. i__1 = *n / 2;
  1267. sgelqf_(&i__1, n, &u[u_offset], ldu, rdummy, rdummy, &
  1268. c_n1, &ierr);
  1269. lwrk_sgelqf__ = (integer) rdummy[0];
  1270. i__1 = *n / 2;
  1271. i__2 = *n / 2;
  1272. sgesvd_("S", "O", &i__1, &i__2, &v[v_offset], ldv, &s[
  1273. 1], &u[u_offset], ldu, &v[v_offset], ldv,
  1274. rdummy, &c_n1, &ierr);
  1275. lwrk_sgesvd2__ = (integer) rdummy[0];
  1276. i__1 = *n / 2;
  1277. sormlq_("R", "N", n, n, &i__1, &u[u_offset], ldu,
  1278. rdummy, &v[v_offset], ldv, rdummy, &c_n1, &
  1279. ierr);
  1280. lwrk_sormlq__ = (integer) rdummy[0];
  1281. /* Computing MAX */
  1282. i__1 = lwrk_sgeqp3__, i__2 = *n / 2 + lwrk_sgelqf__,
  1283. i__1 = f2cmax(i__1,i__2), i__2 = *n / 2 +
  1284. lwrk_sgesvd2__, i__1 = f2cmax(i__1,i__2), i__2 =
  1285. *n / 2 + lwrk_sormlq__;
  1286. optwrk2 = f2cmax(i__1,i__2);
  1287. if (conda) {
  1288. optwrk2 = f2cmax(optwrk2,lwcon);
  1289. }
  1290. optwrk2 = *n + optwrk2;
  1291. optwrk = f2cmax(optwrk,optwrk2);
  1292. }
  1293. }
  1294. }
  1295. }
  1296. minwrk = f2cmax(2,minwrk);
  1297. optwrk = f2cmax(2,optwrk);
  1298. if (*lwork < minwrk && ! lquery) {
  1299. *info = -19;
  1300. }
  1301. }
  1302. if (*info == 0 && *lrwork < rminwrk && ! lquery) {
  1303. *info = -21;
  1304. }
  1305. if (*info != 0) {
  1306. i__1 = -(*info);
  1307. xerbla_("SGESVDQ", &i__1, (ftnlen)7);
  1308. return 0;
  1309. } else if (lquery) {
  1310. /* Return optimal workspace */
  1311. iwork[1] = iminwrk;
  1312. work[1] = (real) optwrk;
  1313. work[2] = (real) minwrk;
  1314. rwork[1] = (real) rminwrk;
  1315. return 0;
  1316. }
  1317. /* Quick return if the matrix is void. */
  1318. if (*m == 0 || *n == 0) {
  1319. return 0;
  1320. }
  1321. big = slamch_("O");
  1322. ascaled = FALSE_;
  1323. iwoff = 1;
  1324. if (rowprm) {
  1325. iwoff = *m;
  1326. /* ell-infinity norm - this enhances numerical robustness in */
  1327. /* the case of differently scaled rows. */
  1328. i__1 = *m;
  1329. for (p = 1; p <= i__1; ++p) {
  1330. /* RWORK(p) = ABS( A(p,ICAMAX(N,A(p,1),LDA)) ) */
  1331. /* [[SLANGE will return NaN if an entry of the p-th row is Nan]] */
  1332. rwork[p] = slange_("M", &c__1, n, &a[p + a_dim1], lda, rdummy);
  1333. if (rwork[p] != rwork[p] || rwork[p] * 0.f != 0.f) {
  1334. *info = -8;
  1335. i__2 = -(*info);
  1336. xerbla_("SGESVDQ", &i__2, (ftnlen)7);
  1337. return 0;
  1338. }
  1339. /* L1904: */
  1340. }
  1341. i__1 = *m - 1;
  1342. for (p = 1; p <= i__1; ++p) {
  1343. i__2 = *m - p + 1;
  1344. q = isamax_(&i__2, &rwork[p], &c__1) + p - 1;
  1345. iwork[*n + p] = q;
  1346. if (p != q) {
  1347. rtmp = rwork[p];
  1348. rwork[p] = rwork[q];
  1349. rwork[q] = rtmp;
  1350. }
  1351. /* L1952: */
  1352. }
  1353. if (rwork[1] == 0.f) {
  1354. /* Quick return: A is the M x N zero matrix. */
  1355. *numrank = 0;
  1356. slaset_("G", n, &c__1, &c_b72, &c_b72, &s[1], n);
  1357. if (wntus) {
  1358. slaset_("G", m, n, &c_b72, &c_b76, &u[u_offset], ldu);
  1359. }
  1360. if (wntua) {
  1361. slaset_("G", m, m, &c_b72, &c_b76, &u[u_offset], ldu);
  1362. }
  1363. if (wntva) {
  1364. slaset_("G", n, n, &c_b72, &c_b76, &v[v_offset], ldv);
  1365. }
  1366. if (wntuf) {
  1367. slaset_("G", n, &c__1, &c_b72, &c_b72, &work[1], n)
  1368. ;
  1369. slaset_("G", m, n, &c_b72, &c_b76, &u[u_offset], ldu);
  1370. }
  1371. i__1 = *n;
  1372. for (p = 1; p <= i__1; ++p) {
  1373. iwork[p] = p;
  1374. /* L5001: */
  1375. }
  1376. if (rowprm) {
  1377. i__1 = *n + *m - 1;
  1378. for (p = *n + 1; p <= i__1; ++p) {
  1379. iwork[p] = p - *n;
  1380. /* L5002: */
  1381. }
  1382. }
  1383. if (conda) {
  1384. rwork[1] = -1.f;
  1385. }
  1386. rwork[2] = -1.f;
  1387. return 0;
  1388. }
  1389. if (rwork[1] > big / sqrt((real) (*m))) {
  1390. /* matrix by 1/sqrt(M) if too large entry detected */
  1391. r__1 = sqrt((real) (*m));
  1392. slascl_("G", &c__0, &c__0, &r__1, &c_b76, m, n, &a[a_offset], lda,
  1393. &ierr);
  1394. ascaled = TRUE_;
  1395. }
  1396. i__1 = *m - 1;
  1397. slaswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[*n + 1], &c__1);
  1398. }
  1399. /* norms overflows during the QR factorization. The SVD procedure should */
  1400. /* have its own scaling to save the singular values from overflows and */
  1401. /* underflows. That depends on the SVD procedure. */
  1402. if (! rowprm) {
  1403. rtmp = slange_("M", m, n, &a[a_offset], lda, rdummy);
  1404. if (rtmp != rtmp || rtmp * 0.f != 0.f) {
  1405. *info = -8;
  1406. i__1 = -(*info);
  1407. xerbla_("SGESVDQ", &i__1, (ftnlen)7);
  1408. return 0;
  1409. }
  1410. if (rtmp > big / sqrt((real) (*m))) {
  1411. /* matrix by 1/sqrt(M) if too large entry detected */
  1412. r__1 = sqrt((real) (*m));
  1413. slascl_("G", &c__0, &c__0, &r__1, &c_b76, m, n, &a[a_offset], lda,
  1414. &ierr);
  1415. ascaled = TRUE_;
  1416. }
  1417. }
  1418. /* A * P = Q * [ R ] */
  1419. /* [ 0 ] */
  1420. i__1 = *n;
  1421. for (p = 1; p <= i__1; ++p) {
  1422. iwork[p] = 0;
  1423. /* L1963: */
  1424. }
  1425. i__1 = *lwork - *n;
  1426. sgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &work[1], &work[*n + 1], &
  1427. i__1, &ierr);
  1428. /* If the user requested accuracy level allows truncation in the */
  1429. /* computed upper triangular factor, the matrix R is examined and, */
  1430. /* if possible, replaced with its leading upper trapezoidal part. */
  1431. epsln = slamch_("E");
  1432. sfmin = slamch_("S");
  1433. /* SMALL = SFMIN / EPSLN */
  1434. nr = *n;
  1435. if (accla) {
  1436. /* Standard absolute error bound suffices. All sigma_i with */
  1437. /* sigma_i < N*EPS*||A||_F are flushed to zero. This is an */
  1438. /* aggressive enforcement of lower numerical rank by introducing a */
  1439. /* backward error of the order of N*EPS*||A||_F. */
  1440. nr = 1;
  1441. rtmp = sqrt((real) (*n)) * epsln;
  1442. i__1 = *n;
  1443. for (p = 2; p <= i__1; ++p) {
  1444. if ((r__2 = a[p + p * a_dim1], abs(r__2)) < rtmp * (r__1 = a[
  1445. a_dim1 + 1], abs(r__1))) {
  1446. goto L3002;
  1447. }
  1448. ++nr;
  1449. /* L3001: */
  1450. }
  1451. L3002:
  1452. ;
  1453. } else if (acclm) {
  1454. /* Sudden drop on the diagonal of R is used as the criterion for being */
  1455. /* close-to-rank-deficient. The threshold is set to EPSLN=SLAMCH('E'). */
  1456. /* [[This can be made more flexible by replacing this hard-coded value */
  1457. /* with a user specified threshold.]] Also, the values that underflow */
  1458. /* will be truncated. */
  1459. nr = 1;
  1460. i__1 = *n;
  1461. for (p = 2; p <= i__1; ++p) {
  1462. if ((r__2 = a[p + p * a_dim1], abs(r__2)) < epsln * (r__1 = a[p -
  1463. 1 + (p - 1) * a_dim1], abs(r__1)) || (r__3 = a[p + p *
  1464. a_dim1], abs(r__3)) < sfmin) {
  1465. goto L3402;
  1466. }
  1467. ++nr;
  1468. /* L3401: */
  1469. }
  1470. L3402:
  1471. ;
  1472. } else {
  1473. /* obvious case of zero pivots. */
  1474. /* R(i,i)=0 => R(i:N,i:N)=0. */
  1475. nr = 1;
  1476. i__1 = *n;
  1477. for (p = 2; p <= i__1; ++p) {
  1478. if ((r__1 = a[p + p * a_dim1], abs(r__1)) == 0.f) {
  1479. goto L3502;
  1480. }
  1481. ++nr;
  1482. /* L3501: */
  1483. }
  1484. L3502:
  1485. if (conda) {
  1486. /* Estimate the scaled condition number of A. Use the fact that it is */
  1487. /* the same as the scaled condition number of R. */
  1488. slacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
  1489. /* Only the leading NR x NR submatrix of the triangular factor */
  1490. /* is considered. Only if NR=N will this give a reliable error */
  1491. /* bound. However, even for NR < N, this can be used on an */
  1492. /* expert level and obtain useful information in the sense of */
  1493. /* perturbation theory. */
  1494. i__1 = nr;
  1495. for (p = 1; p <= i__1; ++p) {
  1496. rtmp = snrm2_(&p, &v[p * v_dim1 + 1], &c__1);
  1497. r__1 = 1.f / rtmp;
  1498. sscal_(&p, &r__1, &v[p * v_dim1 + 1], &c__1);
  1499. /* L3053: */
  1500. }
  1501. if (! (lsvec || rsvec)) {
  1502. spocon_("U", &nr, &v[v_offset], ldv, &c_b76, &rtmp, &work[1],
  1503. &iwork[*n + iwoff], &ierr);
  1504. } else {
  1505. spocon_("U", &nr, &v[v_offset], ldv, &c_b76, &rtmp, &work[*n
  1506. + 1], &iwork[*n + iwoff], &ierr);
  1507. }
  1508. sconda = 1.f / sqrt(rtmp);
  1509. /* For NR=N, SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1), */
  1510. /* N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
  1511. /* See the reference [1] for more details. */
  1512. }
  1513. }
  1514. if (wntur) {
  1515. n1 = nr;
  1516. } else if (wntus || wntuf) {
  1517. n1 = *n;
  1518. } else if (wntua) {
  1519. n1 = *m;
  1520. }
  1521. if (! (rsvec || lsvec)) {
  1522. /* ....................................................................... */
  1523. /* ....................................................................... */
  1524. if (rtrans) {
  1525. /* the upper triangle of [A] to zero. */
  1526. i__1 = f2cmin(*n,nr);
  1527. for (p = 1; p <= i__1; ++p) {
  1528. i__2 = *n;
  1529. for (q = p + 1; q <= i__2; ++q) {
  1530. a[q + p * a_dim1] = a[p + q * a_dim1];
  1531. if (q <= nr) {
  1532. a[p + q * a_dim1] = 0.f;
  1533. }
  1534. /* L1147: */
  1535. }
  1536. /* L1146: */
  1537. }
  1538. sgesvd_("N", "N", n, &nr, &a[a_offset], lda, &s[1], &u[u_offset],
  1539. ldu, &v[v_offset], ldv, &work[1], lwork, info);
  1540. } else {
  1541. if (nr > 1) {
  1542. i__1 = nr - 1;
  1543. i__2 = nr - 1;
  1544. slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &a[a_dim1 + 2],
  1545. lda);
  1546. }
  1547. sgesvd_("N", "N", &nr, n, &a[a_offset], lda, &s[1], &u[u_offset],
  1548. ldu, &v[v_offset], ldv, &work[1], lwork, info);
  1549. }
  1550. } else if (lsvec && ! rsvec) {
  1551. /* ....................................................................... */
  1552. /* ......................................................................."""""""" */
  1553. if (rtrans) {
  1554. /* vectors of R */
  1555. i__1 = nr;
  1556. for (p = 1; p <= i__1; ++p) {
  1557. i__2 = *n;
  1558. for (q = p; q <= i__2; ++q) {
  1559. u[q + p * u_dim1] = a[p + q * a_dim1];
  1560. /* L1193: */
  1561. }
  1562. /* L1192: */
  1563. }
  1564. if (nr > 1) {
  1565. i__1 = nr - 1;
  1566. i__2 = nr - 1;
  1567. slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &u[(u_dim1 << 1) +
  1568. 1], ldu);
  1569. }
  1570. /* vectors overwrite [U](1:NR,1:NR) as transposed. These */
  1571. /* will be pre-multiplied by Q to build the left singular vectors of A. */
  1572. i__1 = *lwork - *n;
  1573. sgesvd_("N", "O", n, &nr, &u[u_offset], ldu, &s[1], &u[u_offset],
  1574. ldu, &u[u_offset], ldu, &work[*n + 1], &i__1, info);
  1575. i__1 = nr;
  1576. for (p = 1; p <= i__1; ++p) {
  1577. i__2 = nr;
  1578. for (q = p + 1; q <= i__2; ++q) {
  1579. rtmp = u[q + p * u_dim1];
  1580. u[q + p * u_dim1] = u[p + q * u_dim1];
  1581. u[p + q * u_dim1] = rtmp;
  1582. /* L1120: */
  1583. }
  1584. /* L1119: */
  1585. }
  1586. } else {
  1587. slacpy_("U", &nr, n, &a[a_offset], lda, &u[u_offset], ldu);
  1588. if (nr > 1) {
  1589. i__1 = nr - 1;
  1590. i__2 = nr - 1;
  1591. slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &u[u_dim1 + 2],
  1592. ldu);
  1593. }
  1594. /* vectors overwrite [U](1:NR,1:NR) */
  1595. i__1 = *lwork - *n;
  1596. sgesvd_("O", "N", &nr, n, &u[u_offset], ldu, &s[1], &u[u_offset],
  1597. ldu, &v[v_offset], ldv, &work[*n + 1], &i__1, info);
  1598. /* R. These will be pre-multiplied by Q to build the left singular */
  1599. /* vectors of A. */
  1600. }
  1601. /* (M x NR) or (M x N) or (M x M). */
  1602. if (nr < *m && ! wntuf) {
  1603. i__1 = *m - nr;
  1604. slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 + u_dim1], ldu);
  1605. if (nr < n1) {
  1606. i__1 = n1 - nr;
  1607. slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr + 1) * u_dim1
  1608. + 1], ldu);
  1609. i__1 = *m - nr;
  1610. i__2 = n1 - nr;
  1611. slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr + 1 + (nr +
  1612. 1) * u_dim1], ldu);
  1613. }
  1614. }
  1615. /* The Q matrix from the first QRF is built into the left singular */
  1616. /* vectors matrix U. */
  1617. if (! wntuf) {
  1618. i__1 = *lwork - *n;
  1619. sormqr_("L", "N", m, &n1, n, &a[a_offset], lda, &work[1], &u[
  1620. u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  1621. }
  1622. if (rowprm && ! wntuf) {
  1623. i__1 = *m - 1;
  1624. slaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[*n + 1], &
  1625. c_n1);
  1626. }
  1627. } else if (rsvec && ! lsvec) {
  1628. /* ....................................................................... */
  1629. /* ....................................................................... */
  1630. if (rtrans) {
  1631. i__1 = nr;
  1632. for (p = 1; p <= i__1; ++p) {
  1633. i__2 = *n;
  1634. for (q = p; q <= i__2; ++q) {
  1635. v[q + p * v_dim1] = a[p + q * a_dim1];
  1636. /* L1166: */
  1637. }
  1638. /* L1165: */
  1639. }
  1640. if (nr > 1) {
  1641. i__1 = nr - 1;
  1642. i__2 = nr - 1;
  1643. slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1 << 1) +
  1644. 1], ldv);
  1645. }
  1646. /* vectors not computed */
  1647. if (wntvr || nr == *n) {
  1648. i__1 = *lwork - *n;
  1649. sgesvd_("O", "N", n, &nr, &v[v_offset], ldv, &s[1], &u[
  1650. u_offset], ldu, &u[u_offset], ldu, &work[*n + 1], &
  1651. i__1, info);
  1652. i__1 = nr;
  1653. for (p = 1; p <= i__1; ++p) {
  1654. i__2 = nr;
  1655. for (q = p + 1; q <= i__2; ++q) {
  1656. rtmp = v[q + p * v_dim1];
  1657. v[q + p * v_dim1] = v[p + q * v_dim1];
  1658. v[p + q * v_dim1] = rtmp;
  1659. /* L1122: */
  1660. }
  1661. /* L1121: */
  1662. }
  1663. if (nr < *n) {
  1664. i__1 = nr;
  1665. for (p = 1; p <= i__1; ++p) {
  1666. i__2 = *n;
  1667. for (q = nr + 1; q <= i__2; ++q) {
  1668. v[p + q * v_dim1] = v[q + p * v_dim1];
  1669. /* L1104: */
  1670. }
  1671. /* L1103: */
  1672. }
  1673. }
  1674. slapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
  1675. } else {
  1676. /* [!] This is simple implementation that augments [V](1:N,1:NR) */
  1677. /* by padding a zero block. In the case NR << N, a more efficient */
  1678. /* way is to first use the QR factorization. For more details */
  1679. /* how to implement this, see the " FULL SVD " branch. */
  1680. i__1 = *n - nr;
  1681. slaset_("G", n, &i__1, &c_b72, &c_b72, &v[(nr + 1) * v_dim1 +
  1682. 1], ldv);
  1683. i__1 = *lwork - *n;
  1684. sgesvd_("O", "N", n, n, &v[v_offset], ldv, &s[1], &u[u_offset]
  1685. , ldu, &u[u_offset], ldu, &work[*n + 1], &i__1, info);
  1686. i__1 = *n;
  1687. for (p = 1; p <= i__1; ++p) {
  1688. i__2 = *n;
  1689. for (q = p + 1; q <= i__2; ++q) {
  1690. rtmp = v[q + p * v_dim1];
  1691. v[q + p * v_dim1] = v[p + q * v_dim1];
  1692. v[p + q * v_dim1] = rtmp;
  1693. /* L1124: */
  1694. }
  1695. /* L1123: */
  1696. }
  1697. slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  1698. }
  1699. } else {
  1700. slacpy_("U", &nr, n, &a[a_offset], lda, &v[v_offset], ldv);
  1701. if (nr > 1) {
  1702. i__1 = nr - 1;
  1703. i__2 = nr - 1;
  1704. slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &v[v_dim1 + 2],
  1705. ldv);
  1706. }
  1707. /* vectors stored in U(1:NR,1:NR) */
  1708. if (wntvr || nr == *n) {
  1709. i__1 = *lwork - *n;
  1710. sgesvd_("N", "O", &nr, n, &v[v_offset], ldv, &s[1], &u[
  1711. u_offset], ldu, &v[v_offset], ldv, &work[*n + 1], &
  1712. i__1, info);
  1713. slapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
  1714. } else {
  1715. /* [!] This is simple implementation that augments [V](1:NR,1:N) */
  1716. /* by padding a zero block. In the case NR << N, a more efficient */
  1717. /* way is to first use the LQ factorization. For more details */
  1718. /* how to implement this, see the " FULL SVD " branch. */
  1719. i__1 = *n - nr;
  1720. slaset_("G", &i__1, n, &c_b72, &c_b72, &v[nr + 1 + v_dim1],
  1721. ldv);
  1722. i__1 = *lwork - *n;
  1723. sgesvd_("N", "O", n, n, &v[v_offset], ldv, &s[1], &u[u_offset]
  1724. , ldu, &v[v_offset], ldv, &work[*n + 1], &i__1, info);
  1725. slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  1726. }
  1727. /* vectors of A. */
  1728. }
  1729. } else {
  1730. /* ....................................................................... */
  1731. /* ....................................................................... */
  1732. if (rtrans) {
  1733. if (wntvr || nr == *n) {
  1734. /* vectors of R**T */
  1735. i__1 = nr;
  1736. for (p = 1; p <= i__1; ++p) {
  1737. i__2 = *n;
  1738. for (q = p; q <= i__2; ++q) {
  1739. v[q + p * v_dim1] = a[p + q * a_dim1];
  1740. /* L1169: */
  1741. }
  1742. /* L1168: */
  1743. }
  1744. if (nr > 1) {
  1745. i__1 = nr - 1;
  1746. i__2 = nr - 1;
  1747. slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1 <<
  1748. 1) + 1], ldv);
  1749. }
  1750. /* singular vectors of R**T stored in [U](1:NR,1:NR) as transposed */
  1751. i__1 = *lwork - *n;
  1752. sgesvd_("O", "A", n, &nr, &v[v_offset], ldv, &s[1], &v[
  1753. v_offset], ldv, &u[u_offset], ldu, &work[*n + 1], &
  1754. i__1, info);
  1755. i__1 = nr;
  1756. for (p = 1; p <= i__1; ++p) {
  1757. i__2 = nr;
  1758. for (q = p + 1; q <= i__2; ++q) {
  1759. rtmp = v[q + p * v_dim1];
  1760. v[q + p * v_dim1] = v[p + q * v_dim1];
  1761. v[p + q * v_dim1] = rtmp;
  1762. /* L1116: */
  1763. }
  1764. /* L1115: */
  1765. }
  1766. if (nr < *n) {
  1767. i__1 = nr;
  1768. for (p = 1; p <= i__1; ++p) {
  1769. i__2 = *n;
  1770. for (q = nr + 1; q <= i__2; ++q) {
  1771. v[p + q * v_dim1] = v[q + p * v_dim1];
  1772. /* L1102: */
  1773. }
  1774. /* L1101: */
  1775. }
  1776. }
  1777. slapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
  1778. i__1 = nr;
  1779. for (p = 1; p <= i__1; ++p) {
  1780. i__2 = nr;
  1781. for (q = p + 1; q <= i__2; ++q) {
  1782. rtmp = u[q + p * u_dim1];
  1783. u[q + p * u_dim1] = u[p + q * u_dim1];
  1784. u[p + q * u_dim1] = rtmp;
  1785. /* L1118: */
  1786. }
  1787. /* L1117: */
  1788. }
  1789. if (nr < *m && ! wntuf) {
  1790. i__1 = *m - nr;
  1791. slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 +
  1792. u_dim1], ldu);
  1793. if (nr < n1) {
  1794. i__1 = n1 - nr;
  1795. slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr + 1) *
  1796. u_dim1 + 1], ldu);
  1797. i__1 = *m - nr;
  1798. i__2 = n1 - nr;
  1799. slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr + 1
  1800. + (nr + 1) * u_dim1], ldu);
  1801. }
  1802. }
  1803. } else {
  1804. /* vectors of R**T */
  1805. /* [[The optimal ratio N/NR for using QRF instead of padding */
  1806. /* with zeros. Here hard coded to 2; it must be at least */
  1807. /* two due to work space constraints.]] */
  1808. /* OPTRATIO = ILAENV(6, 'SGESVD', 'S' // 'O', NR,N,0,0) */
  1809. /* OPTRATIO = MAX( OPTRATIO, 2 ) */
  1810. optratio = 2;
  1811. if (optratio * nr > *n) {
  1812. i__1 = nr;
  1813. for (p = 1; p <= i__1; ++p) {
  1814. i__2 = *n;
  1815. for (q = p; q <= i__2; ++q) {
  1816. v[q + p * v_dim1] = a[p + q * a_dim1];
  1817. /* L1199: */
  1818. }
  1819. /* L1198: */
  1820. }
  1821. if (nr > 1) {
  1822. i__1 = nr - 1;
  1823. i__2 = nr - 1;
  1824. slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1
  1825. << 1) + 1], ldv);
  1826. }
  1827. i__1 = *n - nr;
  1828. slaset_("A", n, &i__1, &c_b72, &c_b72, &v[(nr + 1) *
  1829. v_dim1 + 1], ldv);
  1830. i__1 = *lwork - *n;
  1831. sgesvd_("O", "A", n, n, &v[v_offset], ldv, &s[1], &v[
  1832. v_offset], ldv, &u[u_offset], ldu, &work[*n + 1],
  1833. &i__1, info);
  1834. i__1 = *n;
  1835. for (p = 1; p <= i__1; ++p) {
  1836. i__2 = *n;
  1837. for (q = p + 1; q <= i__2; ++q) {
  1838. rtmp = v[q + p * v_dim1];
  1839. v[q + p * v_dim1] = v[p + q * v_dim1];
  1840. v[p + q * v_dim1] = rtmp;
  1841. /* L1114: */
  1842. }
  1843. /* L1113: */
  1844. }
  1845. slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  1846. /* (M x N1), i.e. (M x N) or (M x M). */
  1847. i__1 = *n;
  1848. for (p = 1; p <= i__1; ++p) {
  1849. i__2 = *n;
  1850. for (q = p + 1; q <= i__2; ++q) {
  1851. rtmp = u[q + p * u_dim1];
  1852. u[q + p * u_dim1] = u[p + q * u_dim1];
  1853. u[p + q * u_dim1] = rtmp;
  1854. /* L1112: */
  1855. }
  1856. /* L1111: */
  1857. }
  1858. if (*n < *m && ! wntuf) {
  1859. i__1 = *m - *n;
  1860. slaset_("A", &i__1, n, &c_b72, &c_b72, &u[*n + 1 +
  1861. u_dim1], ldu);
  1862. if (*n < n1) {
  1863. i__1 = n1 - *n;
  1864. slaset_("A", n, &i__1, &c_b72, &c_b72, &u[(*n + 1)
  1865. * u_dim1 + 1], ldu);
  1866. i__1 = *m - *n;
  1867. i__2 = n1 - *n;
  1868. slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[*n
  1869. + 1 + (*n + 1) * u_dim1], ldu);
  1870. }
  1871. }
  1872. } else {
  1873. /* singular vectors of R */
  1874. i__1 = nr;
  1875. for (p = 1; p <= i__1; ++p) {
  1876. i__2 = *n;
  1877. for (q = p; q <= i__2; ++q) {
  1878. u[q + (nr + p) * u_dim1] = a[p + q * a_dim1];
  1879. /* L1197: */
  1880. }
  1881. /* L1196: */
  1882. }
  1883. if (nr > 1) {
  1884. i__1 = nr - 1;
  1885. i__2 = nr - 1;
  1886. slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &u[(nr + 2)
  1887. * u_dim1 + 1], ldu);
  1888. }
  1889. i__1 = *lwork - *n - nr;
  1890. sgeqrf_(n, &nr, &u[(nr + 1) * u_dim1 + 1], ldu, &work[*n
  1891. + 1], &work[*n + nr + 1], &i__1, &ierr);
  1892. i__1 = nr;
  1893. for (p = 1; p <= i__1; ++p) {
  1894. i__2 = *n;
  1895. for (q = 1; q <= i__2; ++q) {
  1896. v[q + p * v_dim1] = u[p + (nr + q) * u_dim1];
  1897. /* L1144: */
  1898. }
  1899. /* L1143: */
  1900. }
  1901. i__1 = nr - 1;
  1902. i__2 = nr - 1;
  1903. slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1 <<
  1904. 1) + 1], ldv);
  1905. i__1 = *lwork - *n - nr;
  1906. sgesvd_("S", "O", &nr, &nr, &v[v_offset], ldv, &s[1], &u[
  1907. u_offset], ldu, &v[v_offset], ldv, &work[*n + nr
  1908. + 1], &i__1, info);
  1909. i__1 = *n - nr;
  1910. slaset_("A", &i__1, &nr, &c_b72, &c_b72, &v[nr + 1 +
  1911. v_dim1], ldv);
  1912. i__1 = *n - nr;
  1913. slaset_("A", &nr, &i__1, &c_b72, &c_b72, &v[(nr + 1) *
  1914. v_dim1 + 1], ldv);
  1915. i__1 = *n - nr;
  1916. i__2 = *n - nr;
  1917. slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &v[nr + 1 + (
  1918. nr + 1) * v_dim1], ldv);
  1919. i__1 = *lwork - *n - nr;
  1920. sormqr_("R", "C", n, n, &nr, &u[(nr + 1) * u_dim1 + 1],
  1921. ldu, &work[*n + 1], &v[v_offset], ldv, &work[*n +
  1922. nr + 1], &i__1, &ierr);
  1923. slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  1924. /* (M x NR) or (M x N) or (M x M). */
  1925. if (nr < *m && ! wntuf) {
  1926. i__1 = *m - nr;
  1927. slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 +
  1928. u_dim1], ldu);
  1929. if (nr < n1) {
  1930. i__1 = n1 - nr;
  1931. slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr +
  1932. 1) * u_dim1 + 1], ldu);
  1933. i__1 = *m - nr;
  1934. i__2 = n1 - nr;
  1935. slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr
  1936. + 1 + (nr + 1) * u_dim1], ldu);
  1937. }
  1938. }
  1939. }
  1940. }
  1941. } else {
  1942. if (wntvr || nr == *n) {
  1943. slacpy_("U", &nr, n, &a[a_offset], lda, &v[v_offset], ldv);
  1944. if (nr > 1) {
  1945. i__1 = nr - 1;
  1946. i__2 = nr - 1;
  1947. slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &v[v_dim1 + 2],
  1948. ldv);
  1949. }
  1950. /* singular vectors of R stored in [U](1:NR,1:NR) */
  1951. i__1 = *lwork - *n;
  1952. sgesvd_("S", "O", &nr, n, &v[v_offset], ldv, &s[1], &u[
  1953. u_offset], ldu, &v[v_offset], ldv, &work[*n + 1], &
  1954. i__1, info);
  1955. slapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
  1956. /* (M x NR) or (M x N) or (M x M). */
  1957. if (nr < *m && ! wntuf) {
  1958. i__1 = *m - nr;
  1959. slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 +
  1960. u_dim1], ldu);
  1961. if (nr < n1) {
  1962. i__1 = n1 - nr;
  1963. slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr + 1) *
  1964. u_dim1 + 1], ldu);
  1965. i__1 = *m - nr;
  1966. i__2 = n1 - nr;
  1967. slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr + 1
  1968. + (nr + 1) * u_dim1], ldu);
  1969. }
  1970. }
  1971. } else {
  1972. /* is then N1 (N or M) */
  1973. /* [[The optimal ratio N/NR for using LQ instead of padding */
  1974. /* with zeros. Here hard coded to 2; it must be at least */
  1975. /* two due to work space constraints.]] */
  1976. /* OPTRATIO = ILAENV(6, 'SGESVD', 'S' // 'O', NR,N,0,0) */
  1977. /* OPTRATIO = MAX( OPTRATIO, 2 ) */
  1978. optratio = 2;
  1979. if (optratio * nr > *n) {
  1980. slacpy_("U", &nr, n, &a[a_offset], lda, &v[v_offset], ldv);
  1981. if (nr > 1) {
  1982. i__1 = nr - 1;
  1983. i__2 = nr - 1;
  1984. slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &v[v_dim1
  1985. + 2], ldv);
  1986. }
  1987. /* singular vectors of R stored in [U](1:NR,1:NR) */
  1988. i__1 = *n - nr;
  1989. slaset_("A", &i__1, n, &c_b72, &c_b72, &v[nr + 1 + v_dim1]
  1990. , ldv);
  1991. i__1 = *lwork - *n;
  1992. sgesvd_("S", "O", n, n, &v[v_offset], ldv, &s[1], &u[
  1993. u_offset], ldu, &v[v_offset], ldv, &work[*n + 1],
  1994. &i__1, info);
  1995. slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  1996. /* singular vectors of A. The leading N left singular vectors */
  1997. /* are in [U](1:N,1:N) */
  1998. /* (M x N1), i.e. (M x N) or (M x M). */
  1999. if (*n < *m && ! wntuf) {
  2000. i__1 = *m - *n;
  2001. slaset_("A", &i__1, n, &c_b72, &c_b72, &u[*n + 1 +
  2002. u_dim1], ldu);
  2003. if (*n < n1) {
  2004. i__1 = n1 - *n;
  2005. slaset_("A", n, &i__1, &c_b72, &c_b72, &u[(*n + 1)
  2006. * u_dim1 + 1], ldu);
  2007. i__1 = *m - *n;
  2008. i__2 = n1 - *n;
  2009. slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[*n
  2010. + 1 + (*n + 1) * u_dim1], ldu);
  2011. }
  2012. }
  2013. } else {
  2014. slacpy_("U", &nr, n, &a[a_offset], lda, &u[nr + 1 +
  2015. u_dim1], ldu);
  2016. if (nr > 1) {
  2017. i__1 = nr - 1;
  2018. i__2 = nr - 1;
  2019. slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &u[nr + 2
  2020. + u_dim1], ldu);
  2021. }
  2022. i__1 = *lwork - *n - nr;
  2023. sgelqf_(&nr, n, &u[nr + 1 + u_dim1], ldu, &work[*n + 1], &
  2024. work[*n + nr + 1], &i__1, &ierr);
  2025. slacpy_("L", &nr, &nr, &u[nr + 1 + u_dim1], ldu, &v[
  2026. v_offset], ldv);
  2027. if (nr > 1) {
  2028. i__1 = nr - 1;
  2029. i__2 = nr - 1;
  2030. slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1
  2031. << 1) + 1], ldv);
  2032. }
  2033. i__1 = *lwork - *n - nr;
  2034. sgesvd_("S", "O", &nr, &nr, &v[v_offset], ldv, &s[1], &u[
  2035. u_offset], ldu, &v[v_offset], ldv, &work[*n + nr
  2036. + 1], &i__1, info);
  2037. i__1 = *n - nr;
  2038. slaset_("A", &i__1, &nr, &c_b72, &c_b72, &v[nr + 1 +
  2039. v_dim1], ldv);
  2040. i__1 = *n - nr;
  2041. slaset_("A", &nr, &i__1, &c_b72, &c_b72, &v[(nr + 1) *
  2042. v_dim1 + 1], ldv);
  2043. i__1 = *n - nr;
  2044. i__2 = *n - nr;
  2045. slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &v[nr + 1 + (
  2046. nr + 1) * v_dim1], ldv);
  2047. i__1 = *lwork - *n - nr;
  2048. sormlq_("R", "N", n, n, &nr, &u[nr + 1 + u_dim1], ldu, &
  2049. work[*n + 1], &v[v_offset], ldv, &work[*n + nr +
  2050. 1], &i__1, &ierr);
  2051. slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
  2052. /* (M x NR) or (M x N) or (M x M). */
  2053. if (nr < *m && ! wntuf) {
  2054. i__1 = *m - nr;
  2055. slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 +
  2056. u_dim1], ldu);
  2057. if (nr < n1) {
  2058. i__1 = n1 - nr;
  2059. slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr +
  2060. 1) * u_dim1 + 1], ldu);
  2061. i__1 = *m - nr;
  2062. i__2 = n1 - nr;
  2063. slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr
  2064. + 1 + (nr + 1) * u_dim1], ldu);
  2065. }
  2066. }
  2067. }
  2068. }
  2069. }
  2070. /* The Q matrix from the first QRF is built into the left singular */
  2071. /* vectors matrix U. */
  2072. if (! wntuf) {
  2073. i__1 = *lwork - *n;
  2074. sormqr_("L", "N", m, &n1, n, &a[a_offset], lda, &work[1], &u[
  2075. u_offset], ldu, &work[*n + 1], &i__1, &ierr);
  2076. }
  2077. if (rowprm && ! wntuf) {
  2078. i__1 = *m - 1;
  2079. slaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[*n + 1], &
  2080. c_n1);
  2081. }
  2082. /* ... end of the "full SVD" branch */
  2083. }
  2084. /* Check whether some singular values are returned as zeros, e.g. */
  2085. /* due to underflow, and update the numerical rank. */
  2086. p = nr;
  2087. for (q = p; q >= 1; --q) {
  2088. if (s[q] > 0.f) {
  2089. goto L4002;
  2090. }
  2091. --nr;
  2092. /* L4001: */
  2093. }
  2094. L4002:
  2095. /* singular values are set to zero. */
  2096. if (nr < *n) {
  2097. i__1 = *n - nr;
  2098. slaset_("G", &i__1, &c__1, &c_b72, &c_b72, &s[nr + 1], n);
  2099. }
  2100. /* values. */
  2101. if (ascaled) {
  2102. r__1 = sqrt((real) (*m));
  2103. slascl_("G", &c__0, &c__0, &c_b76, &r__1, &nr, &c__1, &s[1], n, &ierr);
  2104. }
  2105. if (conda) {
  2106. rwork[1] = sconda;
  2107. }
  2108. rwork[2] = (real) (p - nr);
  2109. /* exact zeros in SGESVD() applied to the (possibly truncated) */
  2110. /* full row rank triangular (trapezoidal) factor of A. */
  2111. *numrank = nr;
  2112. return 0;
  2113. /* End of SGESVDQ */
  2114. } /* sgesvdq_ */