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.

cgels.c 28 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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 complex c_b1 = {0.f,0.f};
  381. static integer c__1 = 1;
  382. static integer c_n1 = -1;
  383. static integer c__0 = 0;
  384. /* > \brief <b> CGELS solves overdetermined or underdetermined systems for GE matrices</b> */
  385. /* =========== DOCUMENTATION =========== */
  386. /* Online html documentation available at */
  387. /* http://www.netlib.org/lapack/explore-html/ */
  388. /* > \htmlonly */
  389. /* > Download CGELS + dependencies */
  390. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgels.f
  391. "> */
  392. /* > [TGZ]</a> */
  393. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgels.f
  394. "> */
  395. /* > [ZIP]</a> */
  396. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgels.f
  397. "> */
  398. /* > [TXT]</a> */
  399. /* > \endhtmlonly */
  400. /* Definition: */
  401. /* =========== */
  402. /* SUBROUTINE CGELS( TRANS, M, N, NRHS, A, LDA, B, LDB, WORK, LWORK, */
  403. /* INFO ) */
  404. /* CHARACTER TRANS */
  405. /* INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS */
  406. /* COMPLEX A( LDA, * ), B( LDB, * ), WORK( * ) */
  407. /* > \par Purpose: */
  408. /* ============= */
  409. /* > */
  410. /* > \verbatim */
  411. /* > */
  412. /* > CGELS solves overdetermined or underdetermined complex linear systems */
  413. /* > involving an M-by-N matrix A, or its conjugate-transpose, using a QR */
  414. /* > or LQ factorization of A. It is assumed that A has full rank. */
  415. /* > */
  416. /* > The following options are provided: */
  417. /* > */
  418. /* > 1. If TRANS = 'N' and m >= n: find the least squares solution of */
  419. /* > an overdetermined system, i.e., solve the least squares problem */
  420. /* > minimize || B - A*X ||. */
  421. /* > */
  422. /* > 2. If TRANS = 'N' and m < n: find the minimum norm solution of */
  423. /* > an underdetermined system A * X = B. */
  424. /* > */
  425. /* > 3. If TRANS = 'C' and m >= n: find the minimum norm solution of */
  426. /* > an underdetermined system A**H * X = B. */
  427. /* > */
  428. /* > 4. If TRANS = 'C' and m < n: find the least squares solution of */
  429. /* > an overdetermined system, i.e., solve the least squares problem */
  430. /* > minimize || B - A**H * X ||. */
  431. /* > */
  432. /* > Several right hand side vectors b and solution vectors x can be */
  433. /* > handled in a single call; they are stored as the columns of the */
  434. /* > M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
  435. /* > matrix X. */
  436. /* > \endverbatim */
  437. /* Arguments: */
  438. /* ========== */
  439. /* > \param[in] TRANS */
  440. /* > \verbatim */
  441. /* > TRANS is CHARACTER*1 */
  442. /* > = 'N': the linear system involves A; */
  443. /* > = 'C': the linear system involves A**H. */
  444. /* > \endverbatim */
  445. /* > */
  446. /* > \param[in] M */
  447. /* > \verbatim */
  448. /* > M is INTEGER */
  449. /* > The number of rows of the matrix A. M >= 0. */
  450. /* > \endverbatim */
  451. /* > */
  452. /* > \param[in] N */
  453. /* > \verbatim */
  454. /* > N is INTEGER */
  455. /* > The number of columns of the matrix A. N >= 0. */
  456. /* > \endverbatim */
  457. /* > */
  458. /* > \param[in] NRHS */
  459. /* > \verbatim */
  460. /* > NRHS is INTEGER */
  461. /* > The number of right hand sides, i.e., the number of */
  462. /* > columns of the matrices B and X. NRHS >= 0. */
  463. /* > \endverbatim */
  464. /* > */
  465. /* > \param[in,out] A */
  466. /* > \verbatim */
  467. /* > A is COMPLEX array, dimension (LDA,N) */
  468. /* > On entry, the M-by-N matrix A. */
  469. /* > if M >= N, A is overwritten by details of its QR */
  470. /* > factorization as returned by CGEQRF; */
  471. /* > if M < N, A is overwritten by details of its LQ */
  472. /* > factorization as returned by CGELQF. */
  473. /* > \endverbatim */
  474. /* > */
  475. /* > \param[in] LDA */
  476. /* > \verbatim */
  477. /* > LDA is INTEGER */
  478. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  479. /* > \endverbatim */
  480. /* > */
  481. /* > \param[in,out] B */
  482. /* > \verbatim */
  483. /* > B is COMPLEX array, dimension (LDB,NRHS) */
  484. /* > On entry, the matrix B of right hand side vectors, stored */
  485. /* > columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS */
  486. /* > if TRANS = 'C'. */
  487. /* > On exit, if INFO = 0, B is overwritten by the solution */
  488. /* > vectors, stored columnwise: */
  489. /* > if TRANS = 'N' and m >= n, rows 1 to n of B contain the least */
  490. /* > squares solution vectors; the residual sum of squares for the */
  491. /* > solution in each column is given by the sum of squares of the */
  492. /* > modulus of elements N+1 to M in that column; */
  493. /* > if TRANS = 'N' and m < n, rows 1 to N of B contain the */
  494. /* > minimum norm solution vectors; */
  495. /* > if TRANS = 'C' and m >= n, rows 1 to M of B contain the */
  496. /* > minimum norm solution vectors; */
  497. /* > if TRANS = 'C' and m < n, rows 1 to M of B contain the */
  498. /* > least squares solution vectors; the residual sum of squares */
  499. /* > for the solution in each column is given by the sum of */
  500. /* > squares of the modulus of elements M+1 to N in that column. */
  501. /* > \endverbatim */
  502. /* > */
  503. /* > \param[in] LDB */
  504. /* > \verbatim */
  505. /* > LDB is INTEGER */
  506. /* > The leading dimension of the array B. LDB >= MAX(1,M,N). */
  507. /* > \endverbatim */
  508. /* > */
  509. /* > \param[out] WORK */
  510. /* > \verbatim */
  511. /* > WORK is COMPLEX array, dimension (MAX(1,LWORK)) */
  512. /* > On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  513. /* > \endverbatim */
  514. /* > */
  515. /* > \param[in] LWORK */
  516. /* > \verbatim */
  517. /* > LWORK is INTEGER */
  518. /* > The dimension of the array WORK. */
  519. /* > LWORK >= f2cmax( 1, MN + f2cmax( MN, NRHS ) ). */
  520. /* > For optimal performance, */
  521. /* > LWORK >= f2cmax( 1, MN + f2cmax( MN, NRHS )*NB ). */
  522. /* > where MN = f2cmin(M,N) and NB is the optimum block size. */
  523. /* > */
  524. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  525. /* > only calculates the optimal size of the WORK array, returns */
  526. /* > this value as the first entry of the WORK array, and no error */
  527. /* > message related to LWORK is issued by XERBLA. */
  528. /* > \endverbatim */
  529. /* > */
  530. /* > \param[out] INFO */
  531. /* > \verbatim */
  532. /* > INFO is INTEGER */
  533. /* > = 0: successful exit */
  534. /* > < 0: if INFO = -i, the i-th argument had an illegal value */
  535. /* > > 0: if INFO = i, the i-th diagonal element of the */
  536. /* > triangular factor of A is zero, so that A does not have */
  537. /* > full rank; the least squares solution could not be */
  538. /* > computed. */
  539. /* > \endverbatim */
  540. /* Authors: */
  541. /* ======== */
  542. /* > \author Univ. of Tennessee */
  543. /* > \author Univ. of California Berkeley */
  544. /* > \author Univ. of Colorado Denver */
  545. /* > \author NAG Ltd. */
  546. /* > \date December 2016 */
  547. /* > \ingroup complexGEsolve */
  548. /* ===================================================================== */
  549. /* Subroutine */ int cgels_(char *trans, integer *m, integer *n, integer *
  550. nrhs, complex *a, integer *lda, complex *b, integer *ldb, complex *
  551. work, integer *lwork, integer *info)
  552. {
  553. /* System generated locals */
  554. integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2, i__3;
  555. real r__1;
  556. /* Local variables */
  557. real anrm, bnrm;
  558. integer brow;
  559. logical tpsd;
  560. integer i__, j, iascl, ibscl;
  561. extern logical lsame_(char *, char *);
  562. integer wsize;
  563. real rwork[1];
  564. integer nb;
  565. extern /* Subroutine */ int slabad_(real *, real *);
  566. extern real clange_(char *, integer *, integer *, complex *, integer *,
  567. real *);
  568. integer mn;
  569. extern /* Subroutine */ int cgelqf_(integer *, integer *, complex *,
  570. integer *, complex *, complex *, integer *, integer *), clascl_(
  571. char *, integer *, integer *, real *, real *, integer *, integer *
  572. , complex *, integer *, integer *);
  573. extern real slamch_(char *);
  574. extern /* Subroutine */ int cgeqrf_(integer *, integer *, complex *,
  575. integer *, complex *, complex *, integer *, integer *), claset_(
  576. char *, integer *, integer *, complex *, complex *, complex *,
  577. integer *), xerbla_(char *, integer *, ftnlen);
  578. extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
  579. integer *, integer *, ftnlen, ftnlen);
  580. integer scllen;
  581. real bignum;
  582. extern /* Subroutine */ int cunmlq_(char *, char *, integer *, integer *,
  583. integer *, complex *, integer *, complex *, complex *, integer *,
  584. complex *, integer *, integer *), cunmqr_(char *,
  585. char *, integer *, integer *, integer *, complex *, integer *,
  586. complex *, complex *, integer *, complex *, integer *, integer *);
  587. real smlnum;
  588. logical lquery;
  589. extern /* Subroutine */ int ctrtrs_(char *, char *, char *, integer *,
  590. integer *, complex *, integer *, complex *, integer *, integer *);
  591. /* -- LAPACK driver routine (version 3.7.0) -- */
  592. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  593. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  594. /* December 2016 */
  595. /* ===================================================================== */
  596. /* Test the input arguments. */
  597. /* Parameter adjustments */
  598. a_dim1 = *lda;
  599. a_offset = 1 + a_dim1 * 1;
  600. a -= a_offset;
  601. b_dim1 = *ldb;
  602. b_offset = 1 + b_dim1 * 1;
  603. b -= b_offset;
  604. --work;
  605. /* Function Body */
  606. *info = 0;
  607. mn = f2cmin(*m,*n);
  608. lquery = *lwork == -1;
  609. if (! (lsame_(trans, "N") || lsame_(trans, "C"))) {
  610. *info = -1;
  611. } else if (*m < 0) {
  612. *info = -2;
  613. } else if (*n < 0) {
  614. *info = -3;
  615. } else if (*nrhs < 0) {
  616. *info = -4;
  617. } else if (*lda < f2cmax(1,*m)) {
  618. *info = -6;
  619. } else /* if(complicated condition) */ {
  620. /* Computing MAX */
  621. i__1 = f2cmax(1,*m);
  622. if (*ldb < f2cmax(i__1,*n)) {
  623. *info = -8;
  624. } else /* if(complicated condition) */ {
  625. /* Computing MAX */
  626. i__1 = 1, i__2 = mn + f2cmax(mn,*nrhs);
  627. if (*lwork < f2cmax(i__1,i__2) && ! lquery) {
  628. *info = -10;
  629. }
  630. }
  631. }
  632. /* Figure out optimal block size */
  633. if (*info == 0 || *info == -10) {
  634. tpsd = TRUE_;
  635. if (lsame_(trans, "N")) {
  636. tpsd = FALSE_;
  637. }
  638. if (*m >= *n) {
  639. nb = ilaenv_(&c__1, "CGEQRF", " ", m, n, &c_n1, &c_n1, (ftnlen)6,
  640. (ftnlen)1);
  641. if (tpsd) {
  642. /* Computing MAX */
  643. i__1 = nb, i__2 = ilaenv_(&c__1, "CUNMQR", "LN", m, nrhs, n, &
  644. c_n1, (ftnlen)6, (ftnlen)2);
  645. nb = f2cmax(i__1,i__2);
  646. } else {
  647. /* Computing MAX */
  648. i__1 = nb, i__2 = ilaenv_(&c__1, "CUNMQR", "LC", m, nrhs, n, &
  649. c_n1, (ftnlen)6, (ftnlen)2);
  650. nb = f2cmax(i__1,i__2);
  651. }
  652. } else {
  653. nb = ilaenv_(&c__1, "CGELQF", " ", m, n, &c_n1, &c_n1, (ftnlen)6,
  654. (ftnlen)1);
  655. if (tpsd) {
  656. /* Computing MAX */
  657. i__1 = nb, i__2 = ilaenv_(&c__1, "CUNMLQ", "LC", n, nrhs, m, &
  658. c_n1, (ftnlen)6, (ftnlen)2);
  659. nb = f2cmax(i__1,i__2);
  660. } else {
  661. /* Computing MAX */
  662. i__1 = nb, i__2 = ilaenv_(&c__1, "CUNMLQ", "LN", n, nrhs, m, &
  663. c_n1, (ftnlen)6, (ftnlen)2);
  664. nb = f2cmax(i__1,i__2);
  665. }
  666. }
  667. /* Computing MAX */
  668. i__1 = 1, i__2 = mn + f2cmax(mn,*nrhs) * nb;
  669. wsize = f2cmax(i__1,i__2);
  670. r__1 = (real) wsize;
  671. work[1].r = r__1, work[1].i = 0.f;
  672. }
  673. if (*info != 0) {
  674. i__1 = -(*info);
  675. xerbla_("CGELS ", &i__1, (ftnlen)6);
  676. return 0;
  677. } else if (lquery) {
  678. return 0;
  679. }
  680. /* Quick return if possible */
  681. /* Computing MIN */
  682. i__1 = f2cmin(*m,*n);
  683. if (f2cmin(i__1,*nrhs) == 0) {
  684. i__1 = f2cmax(*m,*n);
  685. claset_("Full", &i__1, nrhs, &c_b1, &c_b1, &b[b_offset], ldb);
  686. return 0;
  687. }
  688. /* Get machine parameters */
  689. smlnum = slamch_("S") / slamch_("P");
  690. bignum = 1.f / smlnum;
  691. slabad_(&smlnum, &bignum);
  692. /* Scale A, B if f2cmax element outside range [SMLNUM,BIGNUM] */
  693. anrm = clange_("M", m, n, &a[a_offset], lda, rwork);
  694. iascl = 0;
  695. if (anrm > 0.f && anrm < smlnum) {
  696. /* Scale matrix norm up to SMLNUM */
  697. clascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
  698. info);
  699. iascl = 1;
  700. } else if (anrm > bignum) {
  701. /* Scale matrix norm down to BIGNUM */
  702. clascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
  703. info);
  704. iascl = 2;
  705. } else if (anrm == 0.f) {
  706. /* Matrix all zero. Return zero solution. */
  707. i__1 = f2cmax(*m,*n);
  708. claset_("F", &i__1, nrhs, &c_b1, &c_b1, &b[b_offset], ldb);
  709. goto L50;
  710. }
  711. brow = *m;
  712. if (tpsd) {
  713. brow = *n;
  714. }
  715. bnrm = clange_("M", &brow, nrhs, &b[b_offset], ldb, rwork);
  716. ibscl = 0;
  717. if (bnrm > 0.f && bnrm < smlnum) {
  718. /* Scale matrix norm up to SMLNUM */
  719. clascl_("G", &c__0, &c__0, &bnrm, &smlnum, &brow, nrhs, &b[b_offset],
  720. ldb, info);
  721. ibscl = 1;
  722. } else if (bnrm > bignum) {
  723. /* Scale matrix norm down to BIGNUM */
  724. clascl_("G", &c__0, &c__0, &bnrm, &bignum, &brow, nrhs, &b[b_offset],
  725. ldb, info);
  726. ibscl = 2;
  727. }
  728. if (*m >= *n) {
  729. /* compute QR factorization of A */
  730. i__1 = *lwork - mn;
  731. cgeqrf_(m, n, &a[a_offset], lda, &work[1], &work[mn + 1], &i__1, info)
  732. ;
  733. /* workspace at least N, optimally N*NB */
  734. if (! tpsd) {
  735. /* Least-Squares Problem f2cmin || A * X - B || */
  736. /* B(1:M,1:NRHS) := Q**H * B(1:M,1:NRHS) */
  737. i__1 = *lwork - mn;
  738. cunmqr_("Left", "Conjugate transpose", m, nrhs, n, &a[a_offset],
  739. lda, &work[1], &b[b_offset], ldb, &work[mn + 1], &i__1,
  740. info);
  741. /* workspace at least NRHS, optimally NRHS*NB */
  742. /* B(1:N,1:NRHS) := inv(R) * B(1:N,1:NRHS) */
  743. ctrtrs_("Upper", "No transpose", "Non-unit", n, nrhs, &a[a_offset]
  744. , lda, &b[b_offset], ldb, info);
  745. if (*info > 0) {
  746. return 0;
  747. }
  748. scllen = *n;
  749. } else {
  750. /* Underdetermined system of equations A**T * X = B */
  751. /* B(1:N,1:NRHS) := inv(R**H) * B(1:N,1:NRHS) */
  752. ctrtrs_("Upper", "Conjugate transpose", "Non-unit", n, nrhs, &a[
  753. a_offset], lda, &b[b_offset], ldb, info);
  754. if (*info > 0) {
  755. return 0;
  756. }
  757. /* B(N+1:M,1:NRHS) = ZERO */
  758. i__1 = *nrhs;
  759. for (j = 1; j <= i__1; ++j) {
  760. i__2 = *m;
  761. for (i__ = *n + 1; i__ <= i__2; ++i__) {
  762. i__3 = i__ + j * b_dim1;
  763. b[i__3].r = 0.f, b[i__3].i = 0.f;
  764. /* L10: */
  765. }
  766. /* L20: */
  767. }
  768. /* B(1:M,1:NRHS) := Q(1:N,:) * B(1:N,1:NRHS) */
  769. i__1 = *lwork - mn;
  770. cunmqr_("Left", "No transpose", m, nrhs, n, &a[a_offset], lda, &
  771. work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
  772. /* workspace at least NRHS, optimally NRHS*NB */
  773. scllen = *m;
  774. }
  775. } else {
  776. /* Compute LQ factorization of A */
  777. i__1 = *lwork - mn;
  778. cgelqf_(m, n, &a[a_offset], lda, &work[1], &work[mn + 1], &i__1, info)
  779. ;
  780. /* workspace at least M, optimally M*NB. */
  781. if (! tpsd) {
  782. /* underdetermined system of equations A * X = B */
  783. /* B(1:M,1:NRHS) := inv(L) * B(1:M,1:NRHS) */
  784. ctrtrs_("Lower", "No transpose", "Non-unit", m, nrhs, &a[a_offset]
  785. , lda, &b[b_offset], ldb, info);
  786. if (*info > 0) {
  787. return 0;
  788. }
  789. /* B(M+1:N,1:NRHS) = 0 */
  790. i__1 = *nrhs;
  791. for (j = 1; j <= i__1; ++j) {
  792. i__2 = *n;
  793. for (i__ = *m + 1; i__ <= i__2; ++i__) {
  794. i__3 = i__ + j * b_dim1;
  795. b[i__3].r = 0.f, b[i__3].i = 0.f;
  796. /* L30: */
  797. }
  798. /* L40: */
  799. }
  800. /* B(1:N,1:NRHS) := Q(1:N,:)**H * B(1:M,1:NRHS) */
  801. i__1 = *lwork - mn;
  802. cunmlq_("Left", "Conjugate transpose", n, nrhs, m, &a[a_offset],
  803. lda, &work[1], &b[b_offset], ldb, &work[mn + 1], &i__1,
  804. info);
  805. /* workspace at least NRHS, optimally NRHS*NB */
  806. scllen = *n;
  807. } else {
  808. /* overdetermined system f2cmin || A**H * X - B || */
  809. /* B(1:N,1:NRHS) := Q * B(1:N,1:NRHS) */
  810. i__1 = *lwork - mn;
  811. cunmlq_("Left", "No transpose", n, nrhs, m, &a[a_offset], lda, &
  812. work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
  813. /* workspace at least NRHS, optimally NRHS*NB */
  814. /* B(1:M,1:NRHS) := inv(L**H) * B(1:M,1:NRHS) */
  815. ctrtrs_("Lower", "Conjugate transpose", "Non-unit", m, nrhs, &a[
  816. a_offset], lda, &b[b_offset], ldb, info);
  817. if (*info > 0) {
  818. return 0;
  819. }
  820. scllen = *m;
  821. }
  822. }
  823. /* Undo scaling */
  824. if (iascl == 1) {
  825. clascl_("G", &c__0, &c__0, &anrm, &smlnum, &scllen, nrhs, &b[b_offset]
  826. , ldb, info);
  827. } else if (iascl == 2) {
  828. clascl_("G", &c__0, &c__0, &anrm, &bignum, &scllen, nrhs, &b[b_offset]
  829. , ldb, info);
  830. }
  831. if (ibscl == 1) {
  832. clascl_("G", &c__0, &c__0, &smlnum, &bnrm, &scllen, nrhs, &b[b_offset]
  833. , ldb, info);
  834. } else if (ibscl == 2) {
  835. clascl_("G", &c__0, &c__0, &bignum, &bnrm, &scllen, nrhs, &b[b_offset]
  836. , ldb, info);
  837. }
  838. L50:
  839. r__1 = (real) wsize;
  840. work[1].r = r__1, work[1].i = 0.f;
  841. return 0;
  842. /* End of CGELS */
  843. } /* cgels_ */