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.

sgelsd.c 37 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /* f2c.h -- Standard Fortran to C header file */
  2. /** barf [ba:rf] 2. "He suggested using FORTRAN, and everybody barfed."
  3. - From The Shogakukan DICTIONARY OF NEW ENGLISH (Second edition) */
  4. #ifndef F2C_INCLUDE
  5. #define F2C_INCLUDE
  6. #include <math.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <complex.h>
  11. #ifdef complex
  12. #undef complex
  13. #endif
  14. #ifdef I
  15. #undef I
  16. #endif
  17. #if defined(_WIN64)
  18. typedef long long BLASLONG;
  19. typedef unsigned long long BLASULONG;
  20. #else
  21. typedef long BLASLONG;
  22. typedef unsigned long BLASULONG;
  23. #endif
  24. #ifdef LAPACK_ILP64
  25. typedef BLASLONG blasint;
  26. #if defined(_WIN64)
  27. #define blasabs(x) llabs(x)
  28. #else
  29. #define blasabs(x) labs(x)
  30. #endif
  31. #else
  32. typedef int blasint;
  33. #define blasabs(x) abs(x)
  34. #endif
  35. typedef blasint integer;
  36. typedef unsigned int uinteger;
  37. typedef char *address;
  38. typedef short int shortint;
  39. typedef float real;
  40. typedef double doublereal;
  41. typedef struct { real r, i; } complex;
  42. typedef struct { doublereal r, i; } doublecomplex;
  43. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  44. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  45. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  46. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  47. #define pCf(z) (*_pCf(z))
  48. #define pCd(z) (*_pCd(z))
  49. typedef int logical;
  50. typedef short int shortlogical;
  51. typedef char logical1;
  52. typedef char integer1;
  53. #define TRUE_ (1)
  54. #define FALSE_ (0)
  55. /* Extern is for use with -E */
  56. #ifndef Extern
  57. #define Extern extern
  58. #endif
  59. /* I/O stuff */
  60. typedef int flag;
  61. typedef int ftnlen;
  62. typedef int ftnint;
  63. /*external read, write*/
  64. typedef struct
  65. { flag cierr;
  66. ftnint ciunit;
  67. flag ciend;
  68. char *cifmt;
  69. ftnint cirec;
  70. } cilist;
  71. /*internal read, write*/
  72. typedef struct
  73. { flag icierr;
  74. char *iciunit;
  75. flag iciend;
  76. char *icifmt;
  77. ftnint icirlen;
  78. ftnint icirnum;
  79. } icilist;
  80. /*open*/
  81. typedef struct
  82. { flag oerr;
  83. ftnint ounit;
  84. char *ofnm;
  85. ftnlen ofnmlen;
  86. char *osta;
  87. char *oacc;
  88. char *ofm;
  89. ftnint orl;
  90. char *oblnk;
  91. } olist;
  92. /*close*/
  93. typedef struct
  94. { flag cerr;
  95. ftnint cunit;
  96. char *csta;
  97. } cllist;
  98. /*rewind, backspace, endfile*/
  99. typedef struct
  100. { flag aerr;
  101. ftnint aunit;
  102. } alist;
  103. /* inquire */
  104. typedef struct
  105. { flag inerr;
  106. ftnint inunit;
  107. char *infile;
  108. ftnlen infilen;
  109. ftnint *inex; /*parameters in standard's order*/
  110. ftnint *inopen;
  111. ftnint *innum;
  112. ftnint *innamed;
  113. char *inname;
  114. ftnlen innamlen;
  115. char *inacc;
  116. ftnlen inacclen;
  117. char *inseq;
  118. ftnlen inseqlen;
  119. char *indir;
  120. ftnlen indirlen;
  121. char *infmt;
  122. ftnlen infmtlen;
  123. char *inform;
  124. ftnint informlen;
  125. char *inunf;
  126. ftnlen inunflen;
  127. ftnint *inrecl;
  128. ftnint *innrec;
  129. char *inblank;
  130. ftnlen inblanklen;
  131. } inlist;
  132. #define VOID void
  133. union Multitype { /* for multiple entry points */
  134. integer1 g;
  135. shortint h;
  136. integer i;
  137. /* longint j; */
  138. real r;
  139. doublereal d;
  140. complex c;
  141. doublecomplex z;
  142. };
  143. typedef union Multitype Multitype;
  144. struct Vardesc { /* for Namelist */
  145. char *name;
  146. char *addr;
  147. ftnlen *dims;
  148. int type;
  149. };
  150. typedef struct Vardesc Vardesc;
  151. struct Namelist {
  152. char *name;
  153. Vardesc **vars;
  154. int nvars;
  155. };
  156. typedef struct Namelist Namelist;
  157. #define abs(x) ((x) >= 0 ? (x) : -(x))
  158. #define dabs(x) (fabs(x))
  159. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  160. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  161. #define dmin(a,b) (f2cmin(a,b))
  162. #define dmax(a,b) (f2cmax(a,b))
  163. #define bit_test(a,b) ((a) >> (b) & 1)
  164. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  165. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  166. #define abort_() { sig_die("Fortran abort routine called", 1); }
  167. #define c_abs(z) (cabsf(Cf(z)))
  168. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  169. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  170. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  171. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  172. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  173. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  174. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  175. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  176. #define d_abs(x) (fabs(*(x)))
  177. #define d_acos(x) (acos(*(x)))
  178. #define d_asin(x) (asin(*(x)))
  179. #define d_atan(x) (atan(*(x)))
  180. #define d_atn2(x, y) (atan2(*(x),*(y)))
  181. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  182. #define r_cnjg(R, Z) { pCf(R) = conj(Cf(Z)); }
  183. #define d_cos(x) (cos(*(x)))
  184. #define d_cosh(x) (cosh(*(x)))
  185. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  186. #define d_exp(x) (exp(*(x)))
  187. #define d_imag(z) (cimag(Cd(z)))
  188. #define r_imag(z) (cimag(Cf(z)))
  189. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  190. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  191. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  192. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  193. #define d_log(x) (log(*(x)))
  194. #define d_mod(x, y) (fmod(*(x), *(y)))
  195. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  196. #define d_nint(x) u_nint(*(x))
  197. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  198. #define d_sign(a,b) u_sign(*(a),*(b))
  199. #define r_sign(a,b) u_sign(*(a),*(b))
  200. #define d_sin(x) (sin(*(x)))
  201. #define d_sinh(x) (sinh(*(x)))
  202. #define d_sqrt(x) (sqrt(*(x)))
  203. #define d_tan(x) (tan(*(x)))
  204. #define d_tanh(x) (tanh(*(x)))
  205. #define i_abs(x) abs(*(x))
  206. #define i_dnnt(x) ((integer)u_nint(*(x)))
  207. #define i_len(s, n) (n)
  208. #define i_nint(x) ((integer)u_nint(*(x)))
  209. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  210. #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
  211. #define pow_si(B,E) spow_ui(*(B),*(E))
  212. #define pow_ri(B,E) spow_ui(*(B),*(E))
  213. #define pow_di(B,E) dpow_ui(*(B),*(E))
  214. #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
  215. #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
  216. #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
  217. #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++ = ' '; }
  218. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  219. #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]; }
  220. #define sig_die(s, kill) { exit(1); }
  221. #define s_stop(s, n) {exit(0);}
  222. static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
  223. #define z_abs(z) (cabs(Cd(z)))
  224. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  225. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  226. #define myexit_() break;
  227. #define mycycle() continue;
  228. #define myceiling(w) {ceil(w)}
  229. #define myhuge(w) {HUGE_VAL}
  230. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  231. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  232. /* procedure parameter types for -A and -C++ */
  233. #define F2C_proc_par_types 1
  234. #ifdef __cplusplus
  235. typedef logical (*L_fp)(...);
  236. #else
  237. typedef logical (*L_fp)();
  238. #endif
  239. static float spow_ui(float x, integer n) {
  240. float pow=1.0; unsigned long int u;
  241. if(n != 0) {
  242. if(n < 0) n = -n, x = 1/x;
  243. for(u = n; ; ) {
  244. if(u & 01) pow *= x;
  245. if(u >>= 1) x *= x;
  246. else break;
  247. }
  248. }
  249. return pow;
  250. }
  251. static double dpow_ui(double x, integer n) {
  252. double pow=1.0; unsigned long int u;
  253. if(n != 0) {
  254. if(n < 0) n = -n, x = 1/x;
  255. for(u = n; ; ) {
  256. if(u & 01) pow *= x;
  257. if(u >>= 1) x *= x;
  258. else break;
  259. }
  260. }
  261. return pow;
  262. }
  263. static _Complex float cpow_ui(_Complex float x, integer n) {
  264. _Complex float pow=1.0; unsigned long int u;
  265. if(n != 0) {
  266. if(n < 0) n = -n, x = 1/x;
  267. for(u = n; ; ) {
  268. if(u & 01) pow *= x;
  269. if(u >>= 1) x *= x;
  270. else break;
  271. }
  272. }
  273. return pow;
  274. }
  275. static _Complex double zpow_ui(_Complex double x, integer n) {
  276. _Complex double pow=1.0; unsigned long int u;
  277. if(n != 0) {
  278. if(n < 0) n = -n, x = 1/x;
  279. for(u = n; ; ) {
  280. if(u & 01) pow *= x;
  281. if(u >>= 1) x *= x;
  282. else break;
  283. }
  284. }
  285. return pow;
  286. }
  287. static integer pow_ii(integer x, integer n) {
  288. integer pow; unsigned long int u;
  289. if (n <= 0) {
  290. if (n == 0 || x == 1) pow = 1;
  291. else if (x != -1) pow = x == 0 ? 1/x : 0;
  292. else n = -n;
  293. }
  294. if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
  295. u = n;
  296. for(pow = 1; ; ) {
  297. if(u & 01) pow *= x;
  298. if(u >>= 1) x *= x;
  299. else break;
  300. }
  301. }
  302. return pow;
  303. }
  304. static integer dmaxloc_(double *w, integer s, integer e, integer *n)
  305. {
  306. double m; integer i, mi;
  307. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  308. if (w[i-1]>m) mi=i ,m=w[i-1];
  309. return mi-s+1;
  310. }
  311. static integer smaxloc_(float *w, integer s, integer e, integer *n)
  312. {
  313. float m; integer i, mi;
  314. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  315. if (w[i-1]>m) mi=i ,m=w[i-1];
  316. return mi-s+1;
  317. }
  318. static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  319. integer n = *n_, incx = *incx_, incy = *incy_, i;
  320. _Complex float zdotc = 0.0;
  321. if (incx == 1 && incy == 1) {
  322. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  323. zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
  324. }
  325. } else {
  326. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  327. zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
  328. }
  329. }
  330. pCf(z) = zdotc;
  331. }
  332. static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  333. integer n = *n_, incx = *incx_, incy = *incy_, i;
  334. _Complex double zdotc = 0.0;
  335. if (incx == 1 && incy == 1) {
  336. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  337. zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
  338. }
  339. } else {
  340. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  341. zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
  342. }
  343. }
  344. pCd(z) = zdotc;
  345. }
  346. static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  347. integer n = *n_, incx = *incx_, incy = *incy_, i;
  348. _Complex float zdotc = 0.0;
  349. if (incx == 1 && incy == 1) {
  350. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  351. zdotc += Cf(&x[i]) * Cf(&y[i]);
  352. }
  353. } else {
  354. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  355. zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
  356. }
  357. }
  358. pCf(z) = zdotc;
  359. }
  360. static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  361. integer n = *n_, incx = *incx_, incy = *incy_, i;
  362. _Complex double zdotc = 0.0;
  363. if (incx == 1 && incy == 1) {
  364. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  365. zdotc += Cd(&x[i]) * Cd(&y[i]);
  366. }
  367. } else {
  368. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  369. zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
  370. }
  371. }
  372. pCd(z) = zdotc;
  373. }
  374. #endif
  375. /* -- translated by f2c (version 20000121).
  376. You must link the resulting object file with the libraries:
  377. -lf2c -lm (in that order)
  378. */
  379. /* Table of constant values */
  380. static integer c__9 = 9;
  381. static integer c__0 = 0;
  382. static integer c__6 = 6;
  383. static integer c_n1 = -1;
  384. static integer c__1 = 1;
  385. static real c_b81 = 0.f;
  386. /* > \brief <b> SGELSD computes the minimum-norm solution to a linear least squares problem for GE matrices</b
  387. > */
  388. /* =========== DOCUMENTATION =========== */
  389. /* Online html documentation available at */
  390. /* http://www.netlib.org/lapack/explore-html/ */
  391. /* > \htmlonly */
  392. /* > Download SGELSD + dependencies */
  393. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgelsd.
  394. f"> */
  395. /* > [TGZ]</a> */
  396. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgelsd.
  397. f"> */
  398. /* > [ZIP]</a> */
  399. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgelsd.
  400. f"> */
  401. /* > [TXT]</a> */
  402. /* > \endhtmlonly */
  403. /* Definition: */
  404. /* =========== */
  405. /* SUBROUTINE SGELSD( M, N, NRHS, A, LDA, B, LDB, S, RCOND, */
  406. /* RANK, WORK, LWORK, IWORK, INFO ) */
  407. /* INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK */
  408. /* REAL RCOND */
  409. /* INTEGER IWORK( * ) */
  410. /* REAL A( LDA, * ), B( LDB, * ), S( * ), WORK( * ) */
  411. /* > \par Purpose: */
  412. /* ============= */
  413. /* > */
  414. /* > \verbatim */
  415. /* > */
  416. /* > SGELSD computes the minimum-norm solution to a real linear least */
  417. /* > squares problem: */
  418. /* > minimize 2-norm(| b - A*x |) */
  419. /* > using the singular value decomposition (SVD) of A. A is an M-by-N */
  420. /* > matrix which may be rank-deficient. */
  421. /* > */
  422. /* > Several right hand side vectors b and solution vectors x can be */
  423. /* > handled in a single call; they are stored as the columns of the */
  424. /* > M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
  425. /* > matrix X. */
  426. /* > */
  427. /* > The problem is solved in three steps: */
  428. /* > (1) Reduce the coefficient matrix A to bidiagonal form with */
  429. /* > Householder transformations, reducing the original problem */
  430. /* > into a "bidiagonal least squares problem" (BLS) */
  431. /* > (2) Solve the BLS using a divide and conquer approach. */
  432. /* > (3) Apply back all the Householder transformations to solve */
  433. /* > the original least squares problem. */
  434. /* > */
  435. /* > The effective rank of A is determined by treating as zero those */
  436. /* > singular values which are less than RCOND times the largest singular */
  437. /* > value. */
  438. /* > */
  439. /* > The divide and conquer algorithm makes very mild assumptions about */
  440. /* > floating point arithmetic. It will work on machines with a guard */
  441. /* > digit in add/subtract, or on those binary machines without guard */
  442. /* > digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or */
  443. /* > Cray-2. It could conceivably fail on hexadecimal or decimal machines */
  444. /* > without guard digits, but we know of none. */
  445. /* > \endverbatim */
  446. /* Arguments: */
  447. /* ========== */
  448. /* > \param[in] M */
  449. /* > \verbatim */
  450. /* > M is INTEGER */
  451. /* > The number of rows of A. M >= 0. */
  452. /* > \endverbatim */
  453. /* > */
  454. /* > \param[in] N */
  455. /* > \verbatim */
  456. /* > N is INTEGER */
  457. /* > The number of columns of A. N >= 0. */
  458. /* > \endverbatim */
  459. /* > */
  460. /* > \param[in] NRHS */
  461. /* > \verbatim */
  462. /* > NRHS is INTEGER */
  463. /* > The number of right hand sides, i.e., the number of columns */
  464. /* > of the matrices B and X. NRHS >= 0. */
  465. /* > \endverbatim */
  466. /* > */
  467. /* > \param[in,out] A */
  468. /* > \verbatim */
  469. /* > A is REAL array, dimension (LDA,N) */
  470. /* > On entry, the M-by-N matrix A. */
  471. /* > On exit, A has been destroyed. */
  472. /* > \endverbatim */
  473. /* > */
  474. /* > \param[in] LDA */
  475. /* > \verbatim */
  476. /* > LDA is INTEGER */
  477. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  478. /* > \endverbatim */
  479. /* > */
  480. /* > \param[in,out] B */
  481. /* > \verbatim */
  482. /* > B is REAL array, dimension (LDB,NRHS) */
  483. /* > On entry, the M-by-NRHS right hand side matrix B. */
  484. /* > On exit, B is overwritten by the N-by-NRHS solution */
  485. /* > matrix X. If m >= n and RANK = n, the residual */
  486. /* > sum-of-squares for the solution in the i-th column is given */
  487. /* > by the sum of squares of elements n+1:m in that column. */
  488. /* > \endverbatim */
  489. /* > */
  490. /* > \param[in] LDB */
  491. /* > \verbatim */
  492. /* > LDB is INTEGER */
  493. /* > The leading dimension of the array B. LDB >= f2cmax(1,f2cmax(M,N)). */
  494. /* > \endverbatim */
  495. /* > */
  496. /* > \param[out] S */
  497. /* > \verbatim */
  498. /* > S is REAL array, dimension (f2cmin(M,N)) */
  499. /* > The singular values of A in decreasing order. */
  500. /* > The condition number of A in the 2-norm = S(1)/S(f2cmin(m,n)). */
  501. /* > \endverbatim */
  502. /* > */
  503. /* > \param[in] RCOND */
  504. /* > \verbatim */
  505. /* > RCOND is REAL */
  506. /* > RCOND is used to determine the effective rank of A. */
  507. /* > Singular values S(i) <= RCOND*S(1) are treated as zero. */
  508. /* > If RCOND < 0, machine precision is used instead. */
  509. /* > \endverbatim */
  510. /* > */
  511. /* > \param[out] RANK */
  512. /* > \verbatim */
  513. /* > RANK is INTEGER */
  514. /* > The effective rank of A, i.e., the number of singular values */
  515. /* > which are greater than RCOND*S(1). */
  516. /* > \endverbatim */
  517. /* > */
  518. /* > \param[out] WORK */
  519. /* > \verbatim */
  520. /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
  521. /* > On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  522. /* > \endverbatim */
  523. /* > */
  524. /* > \param[in] LWORK */
  525. /* > \verbatim */
  526. /* > LWORK is INTEGER */
  527. /* > The dimension of the array WORK. LWORK must be at least 1. */
  528. /* > The exact minimum amount of workspace needed depends on M, */
  529. /* > N and NRHS. As long as LWORK is at least */
  530. /* > 12*N + 2*N*SMLSIZ + 8*N*NLVL + N*NRHS + (SMLSIZ+1)**2, */
  531. /* > if M is greater than or equal to N or */
  532. /* > 12*M + 2*M*SMLSIZ + 8*M*NLVL + M*NRHS + (SMLSIZ+1)**2, */
  533. /* > if M is less than N, the code will execute correctly. */
  534. /* > SMLSIZ is returned by ILAENV and is equal to the maximum */
  535. /* > size of the subproblems at the bottom of the computation */
  536. /* > tree (usually about 25), and */
  537. /* > NLVL = MAX( 0, INT( LOG_2( MIN( M,N )/(SMLSIZ+1) ) ) + 1 ) */
  538. /* > For good performance, LWORK should generally be larger. */
  539. /* > */
  540. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  541. /* > only calculates the optimal size of the array WORK and the */
  542. /* > minimum size of the array IWORK, and returns these values as */
  543. /* > the first entries of the WORK and IWORK arrays, and no error */
  544. /* > message related to LWORK is issued by XERBLA. */
  545. /* > \endverbatim */
  546. /* > */
  547. /* > \param[out] IWORK */
  548. /* > \verbatim */
  549. /* > IWORK is INTEGER array, dimension (MAX(1,LIWORK)) */
  550. /* > LIWORK >= f2cmax(1, 3*MINMN*NLVL + 11*MINMN), */
  551. /* > where MINMN = MIN( M,N ). */
  552. /* > On exit, if INFO = 0, IWORK(1) returns the minimum LIWORK. */
  553. /* > \endverbatim */
  554. /* > */
  555. /* > \param[out] INFO */
  556. /* > \verbatim */
  557. /* > INFO is INTEGER */
  558. /* > = 0: successful exit */
  559. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  560. /* > > 0: the algorithm for computing the SVD failed to converge; */
  561. /* > if INFO = i, i off-diagonal elements of an intermediate */
  562. /* > bidiagonal form did not converge to zero. */
  563. /* > \endverbatim */
  564. /* Authors: */
  565. /* ======== */
  566. /* > \author Univ. of Tennessee */
  567. /* > \author Univ. of California Berkeley */
  568. /* > \author Univ. of Colorado Denver */
  569. /* > \author NAG Ltd. */
  570. /* > \date June 2017 */
  571. /* > \ingroup realGEsolve */
  572. /* > \par Contributors: */
  573. /* ================== */
  574. /* > */
  575. /* > Ming Gu and Ren-Cang Li, Computer Science Division, University of */
  576. /* > California at Berkeley, USA \n */
  577. /* > Osni Marques, LBNL/NERSC, USA \n */
  578. /* ===================================================================== */
  579. /* Subroutine */ int sgelsd_(integer *m, integer *n, integer *nrhs, real *a,
  580. integer *lda, real *b, integer *ldb, real *s, real *rcond, integer *
  581. rank, real *work, integer *lwork, integer *iwork, integer *info)
  582. {
  583. /* System generated locals */
  584. integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2, i__3, i__4;
  585. /* Local variables */
  586. real anrm, bnrm;
  587. integer itau, nlvl, iascl, ibscl;
  588. real sfmin;
  589. integer minmn, maxmn, itaup, itauq, mnthr, nwork, ie, il;
  590. extern /* Subroutine */ int slabad_(real *, real *);
  591. integer mm;
  592. extern /* Subroutine */ int sgebrd_(integer *, integer *, real *, integer
  593. *, real *, real *, real *, real *, real *, integer *, integer *);
  594. extern real slamch_(char *), slange_(char *, integer *, integer *,
  595. real *, integer *, real *);
  596. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  597. extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
  598. integer *, integer *, ftnlen, ftnlen);
  599. real bignum;
  600. extern /* Subroutine */ int sgelqf_(integer *, integer *, real *, integer
  601. *, real *, real *, integer *, integer *), slalsd_(char *, integer
  602. *, integer *, integer *, real *, real *, real *, integer *, real *
  603. , integer *, real *, integer *, integer *), slascl_(char *
  604. , integer *, integer *, real *, real *, integer *, integer *,
  605. real *, integer *, integer *);
  606. integer wlalsd;
  607. extern /* Subroutine */ int sgeqrf_(integer *, integer *, real *, integer
  608. *, real *, real *, integer *, integer *), slacpy_(char *, integer
  609. *, integer *, real *, integer *, real *, integer *),
  610. slaset_(char *, integer *, integer *, real *, real *, real *,
  611. integer *);
  612. integer ldwork;
  613. extern /* Subroutine */ int sormbr_(char *, char *, char *, integer *,
  614. integer *, integer *, real *, integer *, real *, real *, integer *
  615. , real *, integer *, integer *);
  616. integer liwork, minwrk, maxwrk;
  617. real smlnum;
  618. extern /* Subroutine */ int sormlq_(char *, char *, integer *, integer *,
  619. integer *, real *, integer *, real *, real *, integer *, real *,
  620. integer *, integer *);
  621. logical lquery;
  622. integer smlsiz;
  623. extern /* Subroutine */ int sormqr_(char *, char *, integer *, integer *,
  624. integer *, real *, integer *, real *, real *, integer *, real *,
  625. integer *, integer *);
  626. real eps;
  627. /* -- LAPACK driver routine (version 3.7.1) -- */
  628. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  629. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  630. /* June 2017 */
  631. /* ===================================================================== */
  632. /* Test the input arguments. */
  633. /* Parameter adjustments */
  634. a_dim1 = *lda;
  635. a_offset = 1 + a_dim1;
  636. a -= a_offset;
  637. b_dim1 = *ldb;
  638. b_offset = 1 + b_dim1;
  639. b -= b_offset;
  640. --s;
  641. --work;
  642. --iwork;
  643. fprintf(stdout,"start of SGELSD\n");
  644. /* Function Body */
  645. *info = 0;
  646. minmn = f2cmin(*m,*n);
  647. maxmn = f2cmax(*m,*n);
  648. lquery = *lwork == -1;
  649. if (*m < 0) {
  650. *info = -1;
  651. } else if (*n < 0) {
  652. *info = -2;
  653. } else if (*nrhs < 0) {
  654. *info = -3;
  655. } else if (*lda < f2cmax(1,*m)) {
  656. *info = -5;
  657. } else if (*ldb < f2cmax(1,maxmn)) {
  658. *info = -7;
  659. }
  660. /* Compute workspace. */
  661. /* (Note: Comments in the code beginning "Workspace:" describe the */
  662. /* minimal amount of workspace needed at that point in the code, */
  663. /* as well as the preferred amount for good performance. */
  664. /* NB refers to the optimal block size for the immediately */
  665. /* following subroutine, as returned by ILAENV.) */
  666. if (*info == 0) {
  667. minwrk = 1;
  668. maxwrk = 1;
  669. liwork = 1;
  670. if (minmn > 0) {
  671. smlsiz = ilaenv_(&c__9, "SGELSD", " ", &c__0, &c__0, &c__0, &c__0,
  672. (ftnlen)6, (ftnlen)1);
  673. mnthr = ilaenv_(&c__6, "SGELSD", " ", m, n, nrhs, &c_n1, (ftnlen)
  674. 6, (ftnlen)1);
  675. /* Computing MAX */
  676. i__1 = (integer) (logf((real) minmn / (real) (smlsiz + 1)) / logf(
  677. 2.f)) + 1;
  678. nlvl = f2cmax(i__1,0);
  679. liwork = minmn * 3 * nlvl + minmn * 11;
  680. mm = *m;
  681. if (*m >= *n && *m >= mnthr) {
  682. /* Path 1a - overdetermined, with many more rows than */
  683. /* columns. */
  684. mm = *n;
  685. /* Computing MAX */
  686. i__1 = maxwrk, i__2 = *n + *n * ilaenv_(&c__1, "SGEQRF",
  687. " ", m, n, &c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
  688. maxwrk = f2cmax(i__1,i__2);
  689. /* Computing MAX */
  690. i__1 = maxwrk, i__2 = *n + *nrhs * ilaenv_(&c__1, "SORMQR",
  691. "LT", m, nrhs, n, &c_n1, (ftnlen)6, (ftnlen)2);
  692. maxwrk = f2cmax(i__1,i__2);
  693. }
  694. if (*m >= *n) {
  695. /* Path 1 - overdetermined or exactly determined. */
  696. /* Computing MAX */
  697. i__1 = maxwrk, i__2 = *n * 3 + (mm + *n) * ilaenv_(&c__1,
  698. "SGEBRD", " ", &mm, n, &c_n1, &c_n1, (ftnlen)6, (
  699. ftnlen)1);
  700. maxwrk = f2cmax(i__1,i__2);
  701. /* Computing MAX */
  702. i__1 = maxwrk, i__2 = *n * 3 + *nrhs * ilaenv_(&c__1, "SORMBR"
  703. , "QLT", &mm, nrhs, n, &c_n1, (ftnlen)6, (ftnlen)3);
  704. maxwrk = f2cmax(i__1,i__2);
  705. /* Computing MAX */
  706. i__1 = maxwrk, i__2 = *n * 3 + (*n - 1) * ilaenv_(&c__1,
  707. "SORMBR", "PLN", n, nrhs, n, &c_n1, (ftnlen)6, (
  708. ftnlen)3);
  709. maxwrk = f2cmax(i__1,i__2);
  710. /* Computing 2nd power */
  711. i__1 = smlsiz + 1;
  712. wlalsd = *n * 9 + (*n << 1) * smlsiz + (*n << 3) * nlvl + *n *
  713. *nrhs + i__1 * i__1;
  714. /* Computing MAX */
  715. i__1 = maxwrk, i__2 = *n * 3 + wlalsd;
  716. maxwrk = f2cmax(i__1,i__2);
  717. /* Computing MAX */
  718. i__1 = *n * 3 + mm, i__2 = *n * 3 + *nrhs, i__1 = f2cmax(i__1,
  719. i__2), i__2 = *n * 3 + wlalsd;
  720. minwrk = f2cmax(i__1,i__2);
  721. }
  722. if (*n > *m) {
  723. /* Computing 2nd power */
  724. i__1 = smlsiz + 1;
  725. wlalsd = *m * 9 + (*m << 1) * smlsiz + (*m << 3) * nlvl + *m *
  726. *nrhs + i__1 * i__1;
  727. if (*n >= mnthr) {
  728. /* Path 2a - underdetermined, with many more columns */
  729. /* than rows. */
  730. maxwrk = *m + *m * ilaenv_(&c__1, "SGELQF", " ", m, n, &
  731. c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
  732. /* Computing MAX */
  733. i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + (*m << 1) *
  734. ilaenv_(&c__1, "SGEBRD", " ", m, m, &c_n1, &c_n1,
  735. (ftnlen)6, (ftnlen)1);
  736. maxwrk = f2cmax(i__1,i__2);
  737. /* Computing MAX */
  738. i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + *nrhs *
  739. ilaenv_(&c__1, "SORMBR", "QLT", m, nrhs, m, &c_n1,
  740. (ftnlen)6, (ftnlen)3);
  741. maxwrk = f2cmax(i__1,i__2);
  742. /* Computing MAX */
  743. i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + (*m - 1) *
  744. ilaenv_(&c__1, "SORMBR", "PLN", m, nrhs, m, &c_n1,
  745. (ftnlen)6, (ftnlen)3);
  746. maxwrk = f2cmax(i__1,i__2);
  747. if (*nrhs > 1) {
  748. /* Computing MAX */
  749. i__1 = maxwrk, i__2 = *m * *m + *m + *m * *nrhs;
  750. maxwrk = f2cmax(i__1,i__2);
  751. } else {
  752. /* Computing MAX */
  753. i__1 = maxwrk, i__2 = *m * *m + (*m << 1);
  754. maxwrk = f2cmax(i__1,i__2);
  755. }
  756. /* Computing MAX */
  757. i__1 = maxwrk, i__2 = *m + *nrhs * ilaenv_(&c__1, "SORMLQ"
  758. , "LT", n, nrhs, m, &c_n1, (ftnlen)6, (ftnlen)2);
  759. maxwrk = f2cmax(i__1,i__2);
  760. /* Computing MAX */
  761. i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + wlalsd;
  762. maxwrk = f2cmax(i__1,i__2);
  763. /* XXX: Ensure the Path 2a case below is triggered. The workspace */
  764. /* calculation should use queries for all routines eventually. */
  765. /* Computing MAX */
  766. /* Computing MAX */
  767. i__3 = *m, i__4 = (*m << 1) - 4, i__3 = f2cmax(i__3,i__4),
  768. i__3 = f2cmax(i__3,*nrhs), i__4 = *n - *m * 3;
  769. i__1 = maxwrk, i__2 = (*m << 2) + *m * *m + f2cmax(i__3,i__4)
  770. ;
  771. maxwrk = f2cmax(i__1,i__2);
  772. } else {
  773. /* Path 2 - remaining underdetermined cases. */
  774. maxwrk = *m * 3 + (*n + *m) * ilaenv_(&c__1, "SGEBRD",
  775. " ", m, n, &c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
  776. /* Computing MAX */
  777. i__1 = maxwrk, i__2 = *m * 3 + *nrhs * ilaenv_(&c__1,
  778. "SORMBR", "QLT", m, nrhs, n, &c_n1, (ftnlen)6, (
  779. ftnlen)3);
  780. maxwrk = f2cmax(i__1,i__2);
  781. /* Computing MAX */
  782. i__1 = maxwrk, i__2 = *m * 3 + *m * ilaenv_(&c__1, "SORM"
  783. "BR", "PLN", n, nrhs, m, &c_n1, (ftnlen)6, (ftnlen)
  784. 3);
  785. maxwrk = f2cmax(i__1,i__2);
  786. /* Computing MAX */
  787. i__1 = maxwrk, i__2 = *m * 3 + wlalsd;
  788. maxwrk = f2cmax(i__1,i__2);
  789. }
  790. /* Computing MAX */
  791. i__1 = *m * 3 + *nrhs, i__2 = *m * 3 + *m, i__1 = f2cmax(i__1,
  792. i__2), i__2 = *m * 3 + wlalsd;
  793. minwrk = f2cmax(i__1,i__2);
  794. }
  795. }
  796. minwrk = f2cmin(minwrk,maxwrk);
  797. work[1] = (real) maxwrk;
  798. iwork[1] = liwork;
  799. if (*lwork < minwrk && ! lquery) {
  800. *info = -12;
  801. }
  802. }
  803. if (*info != 0) {
  804. i__1 = -(*info);
  805. xerbla_("SGELSD", &i__1, (ftnlen)6);
  806. return 0;
  807. } else if (lquery) {
  808. return 0;
  809. }
  810. /* Quick return if possible. */
  811. if (*m == 0 || *n == 0) {
  812. fprintf(stdout,"SGELSD quickreturn rank=0\n");
  813. *rank = 0;
  814. return 0;
  815. }
  816. /* Get machine parameters. */
  817. eps = slamch_("P");
  818. sfmin = slamch_("S");
  819. smlnum = sfmin / eps;
  820. bignum = 1.f / smlnum;
  821. // FILE *bla=fopen("/tmp/bla","w");
  822. //fprintf(bla,"SGELSD eps=%g sfmin=%g smlnum=%g bignum=%g\n",eps,sfmin,smlnum,bignum);
  823. //fclose(bla);
  824. slabad_(&smlnum, &bignum);
  825. /* Scale A if f2cmax entry outside range [SMLNUM,BIGNUM]. */
  826. anrm = slange_("M", m, n, &a[a_offset], lda, &work[1]);
  827. iascl = 0;
  828. if (anrm > 0.f && anrm < smlnum) {
  829. /* Scale matrix norm up to SMLNUM. */
  830. fprintf(stdout,"scaling A up to SML\n");
  831. slascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
  832. info);
  833. iascl = 1;
  834. } else if (anrm > bignum) {
  835. /* Scale matrix norm down to BIGNUM. */
  836. fprintf(stdout,"scaling A down to BIG\n");
  837. slascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
  838. info);
  839. iascl = 2;
  840. } else if (anrm == 0.f) {
  841. /* Matrix all zero. Return zero solution. */
  842. fprintf(stdout,"A is zero soln\n");
  843. i__1 = f2cmax(*m,*n);
  844. slaset_("F", &i__1, nrhs, &c_b81, &c_b81, &b[b_offset], ldb);
  845. slaset_("F", &minmn, &c__1, &c_b81, &c_b81, &s[1], &c__1);
  846. *rank = 0;
  847. goto L10;
  848. }
  849. /* Scale B if f2cmax entry outside range [SMLNUM,BIGNUM]. */
  850. bnrm = slange_("M", m, nrhs, &b[b_offset], ldb, &work[1]);
  851. ibscl = 0;
  852. if (bnrm > 0.f && bnrm < smlnum) {
  853. /* Scale matrix norm up to SMLNUM. */
  854. fprintf(stdout,"scaling B up to SML\n");
  855. slascl_("G", &c__0, &c__0, &bnrm, &smlnum, m, nrhs, &b[b_offset], ldb,
  856. info);
  857. ibscl = 1;
  858. } else if (bnrm > bignum) {
  859. /* Scale matrix norm down to BIGNUM. */
  860. fprintf(stdout,"scaling B down to BIG\n");
  861. slascl_("G", &c__0, &c__0, &bnrm, &bignum, m, nrhs, &b[b_offset], ldb,
  862. info);
  863. ibscl = 2;
  864. }
  865. /* If M < N make sure certain entries of B are zero. */
  866. if (*m < *n) {
  867. i__1 = *n - *m;
  868. fprintf(stdout,"zeroing parts of B \n");
  869. slaset_("F", &i__1, nrhs, &c_b81, &c_b81, &b[*m + 1 + b_dim1], ldb);
  870. }
  871. /* Overdetermined case. */
  872. if (*m >= *n) {
  873. fprintf(stdout,"overdetermined, path 1 \n");
  874. /* Path 1 - overdetermined or exactly determined. */
  875. mm = *m;
  876. if (*m >= mnthr) {
  877. /* Path 1a - overdetermined, with many more rows than columns. */
  878. fprintf(stdout,"overdetermined, path 1a \n");
  879. mm = *n;
  880. itau = 1;
  881. nwork = itau + *n;
  882. /* Compute A=Q*R. */
  883. /* (Workspace: need 2*N, prefer N+N*NB) */
  884. i__1 = *lwork - nwork + 1;
  885. sgeqrf_(m, n, &a[a_offset], lda, &work[itau], &work[nwork], &i__1,
  886. info);
  887. /* Multiply B by transpose(Q). */
  888. /* (Workspace: need N+NRHS, prefer N+NRHS*NB) */
  889. i__1 = *lwork - nwork + 1;
  890. sormqr_("L", "T", m, nrhs, n, &a[a_offset], lda, &work[itau], &b[
  891. b_offset], ldb, &work[nwork], &i__1, info);
  892. /* Zero out below R. */
  893. if (*n > 1) {
  894. i__1 = *n - 1;
  895. i__2 = *n - 1;
  896. slaset_("L", &i__1, &i__2, &c_b81, &c_b81, &a[a_dim1 + 2],
  897. lda);
  898. }
  899. }
  900. ie = 1;
  901. itauq = ie + *n;
  902. itaup = itauq + *n;
  903. nwork = itaup + *n;
  904. /* Bidiagonalize R in A. */
  905. /* (Workspace: need 3*N+MM, prefer 3*N+(MM+N)*NB) */
  906. i__1 = *lwork - nwork + 1;
  907. sgebrd_(&mm, n, &a[a_offset], lda, &s[1], &work[ie], &work[itauq], &
  908. work[itaup], &work[nwork], &i__1, info);
  909. /* Multiply B by transpose of left bidiagonalizing vectors of R. */
  910. /* (Workspace: need 3*N+NRHS, prefer 3*N+NRHS*NB) */
  911. i__1 = *lwork - nwork + 1;
  912. sormbr_("Q", "L", "T", &mm, nrhs, n, &a[a_offset], lda, &work[itauq],
  913. &b[b_offset], ldb, &work[nwork], &i__1, info);
  914. /* Solve the bidiagonal least squares problem. */
  915. slalsd_("U", &smlsiz, n, nrhs, &s[1], &work[ie], &b[b_offset], ldb,
  916. rcond, rank, &work[nwork], &iwork[1], info);
  917. if (*info != 0) {
  918. fprintf(stdout,"info !=0 nach slalsd\n");
  919. goto L10;
  920. }
  921. /* Multiply B by right bidiagonalizing vectors of R. */
  922. i__1 = *lwork - nwork + 1;
  923. sormbr_("P", "L", "N", n, nrhs, n, &a[a_offset], lda, &work[itaup], &
  924. b[b_offset], ldb, &work[nwork], &i__1, info);
  925. } else /* if(complicated condition) */ {
  926. fprintf(stdout,"not overdetermined \n");
  927. /* Computing MAX */
  928. i__1 = *m, i__2 = (*m << 1) - 4, i__1 = f2cmax(i__1,i__2), i__1 = f2cmax(
  929. i__1,*nrhs), i__2 = *n - *m * 3, i__1 = f2cmax(i__1,i__2);
  930. if (*n >= mnthr && *lwork >= (*m << 2) + *m * *m + f2cmax(i__1,wlalsd)) {
  931. /* Path 2a - underdetermined, with many more columns than rows */
  932. /* and sufficient workspace for an efficient algorithm. */
  933. fprintf(stdout,"not overdetermined, path 2a\n");
  934. ldwork = *m;
  935. /* Computing MAX */
  936. /* Computing MAX */
  937. i__3 = *m, i__4 = (*m << 1) - 4, i__3 = f2cmax(i__3,i__4), i__3 =
  938. f2cmax(i__3,*nrhs), i__4 = *n - *m * 3;
  939. i__1 = (*m << 2) + *m * *lda + f2cmax(i__3,i__4), i__2 = *m * *lda +
  940. *m + *m * *nrhs, i__1 = f2cmax(i__1,i__2), i__2 = (*m << 2)
  941. + *m * *lda + wlalsd;
  942. if (*lwork >= f2cmax(i__1,i__2)) {
  943. ldwork = *lda;
  944. }
  945. itau = 1;
  946. nwork = *m + 1;
  947. /* Compute A=L*Q. */
  948. /* (Workspace: need 2*M, prefer M+M*NB) */
  949. i__1 = *lwork - nwork + 1;
  950. sgelqf_(m, n, &a[a_offset], lda, &work[itau], &work[nwork], &i__1,
  951. info);
  952. il = nwork;
  953. /* Copy L to WORK(IL), zeroing out above its diagonal. */
  954. slacpy_("L", m, m, &a[a_offset], lda, &work[il], &ldwork);
  955. i__1 = *m - 1;
  956. i__2 = *m - 1;
  957. slaset_("U", &i__1, &i__2, &c_b81, &c_b81, &work[il + ldwork], &
  958. ldwork);
  959. ie = il + ldwork * *m;
  960. itauq = ie + *m;
  961. itaup = itauq + *m;
  962. nwork = itaup + *m;
  963. /* Bidiagonalize L in WORK(IL). */
  964. /* (Workspace: need M*M+5*M, prefer M*M+4*M+2*M*NB) */
  965. i__1 = *lwork - nwork + 1;
  966. sgebrd_(m, m, &work[il], &ldwork, &s[1], &work[ie], &work[itauq],
  967. &work[itaup], &work[nwork], &i__1, info);
  968. /* Multiply B by transpose of left bidiagonalizing vectors of L. */
  969. /* (Workspace: need M*M+4*M+NRHS, prefer M*M+4*M+NRHS*NB) */
  970. i__1 = *lwork - nwork + 1;
  971. sormbr_("Q", "L", "T", m, nrhs, m, &work[il], &ldwork, &work[
  972. itauq], &b[b_offset], ldb, &work[nwork], &i__1, info);
  973. /* Solve the bidiagonal least squares problem. */
  974. slalsd_("U", &smlsiz, m, nrhs, &s[1], &work[ie], &b[b_offset],
  975. ldb, rcond, rank, &work[nwork], &iwork[1], info);
  976. if (*info != 0) {
  977. goto L10;
  978. }
  979. /* Multiply B by right bidiagonalizing vectors of L. */
  980. i__1 = *lwork - nwork + 1;
  981. sormbr_("P", "L", "N", m, nrhs, m, &work[il], &ldwork, &work[
  982. itaup], &b[b_offset], ldb, &work[nwork], &i__1, info);
  983. /* Zero out below first M rows of B. */
  984. i__1 = *n - *m;
  985. slaset_("F", &i__1, nrhs, &c_b81, &c_b81, &b[*m + 1 + b_dim1],
  986. ldb);
  987. nwork = itau + *m;
  988. /* Multiply transpose(Q) by B. */
  989. /* (Workspace: need M+NRHS, prefer M+NRHS*NB) */
  990. i__1 = *lwork - nwork + 1;
  991. sormlq_("L", "T", n, nrhs, m, &a[a_offset], lda, &work[itau], &b[
  992. b_offset], ldb, &work[nwork], &i__1, info);
  993. } else {
  994. /* Path 2 - remaining underdetermined cases. */
  995. fprintf(stdout,"other underdetermined, path 2");
  996. ie = 1;
  997. itauq = ie + *m;
  998. itaup = itauq + *m;
  999. nwork = itaup + *m;
  1000. /* Bidiagonalize A. */
  1001. /* (Workspace: need 3*M+N, prefer 3*M+(M+N)*NB) */
  1002. i__1 = *lwork - nwork + 1;
  1003. sgebrd_(m, n, &a[a_offset], lda, &s[1], &work[ie], &work[itauq], &
  1004. work[itaup], &work[nwork], &i__1, info);
  1005. /* Multiply B by transpose of left bidiagonalizing vectors. */
  1006. /* (Workspace: need 3*M+NRHS, prefer 3*M+NRHS*NB) */
  1007. i__1 = *lwork - nwork + 1;
  1008. sormbr_("Q", "L", "T", m, nrhs, n, &a[a_offset], lda, &work[itauq]
  1009. , &b[b_offset], ldb, &work[nwork], &i__1, info);
  1010. /* Solve the bidiagonal least squares problem. */
  1011. slalsd_("L", &smlsiz, m, nrhs, &s[1], &work[ie], &b[b_offset],
  1012. ldb, rcond, rank, &work[nwork], &iwork[1], info);
  1013. if (*info != 0) {
  1014. goto L10;
  1015. }
  1016. /* Multiply B by right bidiagonalizing vectors of A. */
  1017. i__1 = *lwork - nwork + 1;
  1018. sormbr_("P", "L", "N", n, nrhs, m, &a[a_offset], lda, &work[itaup]
  1019. , &b[b_offset], ldb, &work[nwork], &i__1, info);
  1020. }
  1021. }
  1022. /* Undo scaling. */
  1023. if (iascl == 1) {
  1024. fprintf(stdout," unscaling a1\n");
  1025. slascl_("G", &c__0, &c__0, &anrm, &smlnum, n, nrhs, &b[b_offset], ldb,
  1026. info);
  1027. slascl_("G", &c__0, &c__0, &smlnum, &anrm, &minmn, &c__1, &s[1], &
  1028. minmn, info);
  1029. } else if (iascl == 2) {
  1030. fprintf(stdout," unscaling a2\n");
  1031. slascl_("G", &c__0, &c__0, &anrm, &bignum, n, nrhs, &b[b_offset], ldb,
  1032. info);
  1033. slascl_("G", &c__0, &c__0, &bignum, &anrm, &minmn, &c__1, &s[1], &
  1034. minmn, info);
  1035. }
  1036. if (ibscl == 1) {
  1037. fprintf(stdout," unscaling b1\n");
  1038. slascl_("G", &c__0, &c__0, &smlnum, &bnrm, n, nrhs, &b[b_offset], ldb,
  1039. info);
  1040. } else if (ibscl == 2) {
  1041. fprintf(stdout," unscaling b2\n");
  1042. slascl_("G", &c__0, &c__0, &bignum, &bnrm, n, nrhs, &b[b_offset], ldb,
  1043. info);
  1044. }
  1045. L10:
  1046. work[1] = (real) maxwrk;
  1047. iwork[1] = liwork;
  1048. fprintf(stdout, "end of SGELSD\n");
  1049. return 0;
  1050. /* End of SGELSD */
  1051. } /* sgelsd_ */