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.

sgelsy.c 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <complex.h>
  6. #ifdef complex
  7. #undef complex
  8. #endif
  9. #ifdef I
  10. #undef I
  11. #endif
  12. #if defined(_WIN64)
  13. typedef long long BLASLONG;
  14. typedef unsigned long long BLASULONG;
  15. #else
  16. typedef long BLASLONG;
  17. typedef unsigned long BLASULONG;
  18. #endif
  19. #ifdef LAPACK_ILP64
  20. typedef BLASLONG blasint;
  21. #if defined(_WIN64)
  22. #define blasabs(x) llabs(x)
  23. #else
  24. #define blasabs(x) labs(x)
  25. #endif
  26. #else
  27. typedef int blasint;
  28. #define blasabs(x) abs(x)
  29. #endif
  30. typedef blasint integer;
  31. typedef unsigned int uinteger;
  32. typedef char *address;
  33. typedef short int shortint;
  34. typedef float real;
  35. typedef double doublereal;
  36. typedef struct { real r, i; } complex;
  37. typedef struct { doublereal r, i; } doublecomplex;
  38. #ifdef _MSC_VER
  39. static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
  40. static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
  41. static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
  42. static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
  43. #else
  44. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  45. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  46. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  47. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  48. #endif
  49. #define pCf(z) (*_pCf(z))
  50. #define pCd(z) (*_pCd(z))
  51. typedef blasint logical;
  52. typedef char logical1;
  53. typedef char integer1;
  54. #define TRUE_ (1)
  55. #define FALSE_ (0)
  56. /* Extern is for use with -E */
  57. #ifndef Extern
  58. #define Extern extern
  59. #endif
  60. /* I/O stuff */
  61. typedef int flag;
  62. typedef int ftnlen;
  63. typedef int ftnint;
  64. /*external read, write*/
  65. typedef struct
  66. { flag cierr;
  67. ftnint ciunit;
  68. flag ciend;
  69. char *cifmt;
  70. ftnint cirec;
  71. } cilist;
  72. /*internal read, write*/
  73. typedef struct
  74. { flag icierr;
  75. char *iciunit;
  76. flag iciend;
  77. char *icifmt;
  78. ftnint icirlen;
  79. ftnint icirnum;
  80. } icilist;
  81. /*open*/
  82. typedef struct
  83. { flag oerr;
  84. ftnint ounit;
  85. char *ofnm;
  86. ftnlen ofnmlen;
  87. char *osta;
  88. char *oacc;
  89. char *ofm;
  90. ftnint orl;
  91. char *oblnk;
  92. } olist;
  93. /*close*/
  94. typedef struct
  95. { flag cerr;
  96. ftnint cunit;
  97. char *csta;
  98. } cllist;
  99. /*rewind, backspace, endfile*/
  100. typedef struct
  101. { flag aerr;
  102. ftnint aunit;
  103. } alist;
  104. /* inquire */
  105. typedef struct
  106. { flag inerr;
  107. ftnint inunit;
  108. char *infile;
  109. ftnlen infilen;
  110. ftnint *inex; /*parameters in standard's order*/
  111. ftnint *inopen;
  112. ftnint *innum;
  113. ftnint *innamed;
  114. char *inname;
  115. ftnlen innamlen;
  116. char *inacc;
  117. ftnlen inacclen;
  118. char *inseq;
  119. ftnlen inseqlen;
  120. char *indir;
  121. ftnlen indirlen;
  122. char *infmt;
  123. ftnlen infmtlen;
  124. char *inform;
  125. ftnint informlen;
  126. char *inunf;
  127. ftnlen inunflen;
  128. ftnint *inrecl;
  129. ftnint *innrec;
  130. char *inblank;
  131. ftnlen inblanklen;
  132. } inlist;
  133. #define VOID void
  134. union Multitype { /* for multiple entry points */
  135. integer1 g;
  136. shortint h;
  137. integer i;
  138. /* longint j; */
  139. real r;
  140. doublereal d;
  141. complex c;
  142. doublecomplex z;
  143. };
  144. typedef union Multitype Multitype;
  145. struct Vardesc { /* for Namelist */
  146. char *name;
  147. char *addr;
  148. ftnlen *dims;
  149. int type;
  150. };
  151. typedef struct Vardesc Vardesc;
  152. struct Namelist {
  153. char *name;
  154. Vardesc **vars;
  155. int nvars;
  156. };
  157. typedef struct Namelist Namelist;
  158. #define abs(x) ((x) >= 0 ? (x) : -(x))
  159. #define dabs(x) (fabs(x))
  160. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  161. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  162. #define dmin(a,b) (f2cmin(a,b))
  163. #define dmax(a,b) (f2cmax(a,b))
  164. #define bit_test(a,b) ((a) >> (b) & 1)
  165. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  166. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  167. #define abort_() { sig_die("Fortran abort routine called", 1); }
  168. #define c_abs(z) (cabsf(Cf(z)))
  169. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  170. #ifdef _MSC_VER
  171. #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
  172. #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
  173. #else
  174. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  175. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  176. #endif
  177. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  178. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  179. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  180. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  181. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  182. #define d_abs(x) (fabs(*(x)))
  183. #define d_acos(x) (acos(*(x)))
  184. #define d_asin(x) (asin(*(x)))
  185. #define d_atan(x) (atan(*(x)))
  186. #define d_atn2(x, y) (atan2(*(x),*(y)))
  187. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  188. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  189. #define d_cos(x) (cos(*(x)))
  190. #define d_cosh(x) (cosh(*(x)))
  191. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  192. #define d_exp(x) (exp(*(x)))
  193. #define d_imag(z) (cimag(Cd(z)))
  194. #define r_imag(z) (cimagf(Cf(z)))
  195. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  196. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  198. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define d_log(x) (log(*(x)))
  200. #define d_mod(x, y) (fmod(*(x), *(y)))
  201. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  202. #define d_nint(x) u_nint(*(x))
  203. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  204. #define d_sign(a,b) u_sign(*(a),*(b))
  205. #define r_sign(a,b) u_sign(*(a),*(b))
  206. #define d_sin(x) (sin(*(x)))
  207. #define d_sinh(x) (sinh(*(x)))
  208. #define d_sqrt(x) (sqrt(*(x)))
  209. #define d_tan(x) (tan(*(x)))
  210. #define d_tanh(x) (tanh(*(x)))
  211. #define i_abs(x) abs(*(x))
  212. #define i_dnnt(x) ((integer)u_nint(*(x)))
  213. #define i_len(s, n) (n)
  214. #define i_nint(x) ((integer)u_nint(*(x)))
  215. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  216. #define 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++ = ' '; }
  217. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  218. #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]; }
  219. #define sig_die(s, kill) { exit(1); }
  220. #define s_stop(s, n) {exit(0);}
  221. #define z_abs(z) (cabs(Cd(z)))
  222. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  223. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  224. #define myexit_() break;
  225. #define mycycle() continue;
  226. #define myceiling(w) {ceil(w)}
  227. #define myhuge(w) {HUGE_VAL}
  228. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  229. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  230. /* -- translated by f2c (version 20000121).
  231. You must link the resulting object file with the libraries:
  232. -lf2c -lm (in that order)
  233. */
  234. /* Table of constant values */
  235. static integer c__1 = 1;
  236. static integer c_n1 = -1;
  237. static integer c__0 = 0;
  238. static real c_b31 = 0.f;
  239. static integer c__2 = 2;
  240. static real c_b54 = 1.f;
  241. /* > \brief <b> SGELSY solves overdetermined or underdetermined systems for GE matrices</b> */
  242. /* =========== DOCUMENTATION =========== */
  243. /* Online html documentation available at */
  244. /* http://www.netlib.org/lapack/explore-html/ */
  245. /* > \htmlonly */
  246. /* > Download SGELSY + dependencies */
  247. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgelsy.
  248. f"> */
  249. /* > [TGZ]</a> */
  250. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgelsy.
  251. f"> */
  252. /* > [ZIP]</a> */
  253. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgelsy.
  254. f"> */
  255. /* > [TXT]</a> */
  256. /* > \endhtmlonly */
  257. /* Definition: */
  258. /* =========== */
  259. /* SUBROUTINE SGELSY( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK, */
  260. /* WORK, LWORK, INFO ) */
  261. /* INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK */
  262. /* REAL RCOND */
  263. /* INTEGER JPVT( * ) */
  264. /* REAL A( LDA, * ), B( LDB, * ), WORK( * ) */
  265. /* > \par Purpose: */
  266. /* ============= */
  267. /* > */
  268. /* > \verbatim */
  269. /* > */
  270. /* > SGELSY computes the minimum-norm solution to a real linear least */
  271. /* > squares problem: */
  272. /* > minimize || A * X - B || */
  273. /* > using a complete orthogonal factorization of A. A is an M-by-N */
  274. /* > matrix which may be rank-deficient. */
  275. /* > */
  276. /* > Several right hand side vectors b and solution vectors x can be */
  277. /* > handled in a single call; they are stored as the columns of the */
  278. /* > M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
  279. /* > matrix X. */
  280. /* > */
  281. /* > The routine first computes a QR factorization with column pivoting: */
  282. /* > A * P = Q * [ R11 R12 ] */
  283. /* > [ 0 R22 ] */
  284. /* > with R11 defined as the largest leading submatrix whose estimated */
  285. /* > condition number is less than 1/RCOND. The order of R11, RANK, */
  286. /* > is the effective rank of A. */
  287. /* > */
  288. /* > Then, R22 is considered to be negligible, and R12 is annihilated */
  289. /* > by orthogonal transformations from the right, arriving at the */
  290. /* > complete orthogonal factorization: */
  291. /* > A * P = Q * [ T11 0 ] * Z */
  292. /* > [ 0 0 ] */
  293. /* > The minimum-norm solution is then */
  294. /* > X = P * Z**T [ inv(T11)*Q1**T*B ] */
  295. /* > [ 0 ] */
  296. /* > where Q1 consists of the first RANK columns of Q. */
  297. /* > */
  298. /* > This routine is basically identical to the original xGELSX except */
  299. /* > three differences: */
  300. /* > o The call to the subroutine xGEQPF has been substituted by the */
  301. /* > the call to the subroutine xGEQP3. This subroutine is a Blas-3 */
  302. /* > version of the QR factorization with column pivoting. */
  303. /* > o Matrix B (the right hand side) is updated with Blas-3. */
  304. /* > o The permutation of matrix B (the right hand side) is faster and */
  305. /* > more simple. */
  306. /* > \endverbatim */
  307. /* Arguments: */
  308. /* ========== */
  309. /* > \param[in] M */
  310. /* > \verbatim */
  311. /* > M is INTEGER */
  312. /* > The number of rows of the matrix A. M >= 0. */
  313. /* > \endverbatim */
  314. /* > */
  315. /* > \param[in] N */
  316. /* > \verbatim */
  317. /* > N is INTEGER */
  318. /* > The number of columns of the matrix A. N >= 0. */
  319. /* > \endverbatim */
  320. /* > */
  321. /* > \param[in] NRHS */
  322. /* > \verbatim */
  323. /* > NRHS is INTEGER */
  324. /* > The number of right hand sides, i.e., the number of */
  325. /* > columns of matrices B and X. NRHS >= 0. */
  326. /* > \endverbatim */
  327. /* > */
  328. /* > \param[in,out] A */
  329. /* > \verbatim */
  330. /* > A is REAL array, dimension (LDA,N) */
  331. /* > On entry, the M-by-N matrix A. */
  332. /* > On exit, A has been overwritten by details of its */
  333. /* > complete orthogonal factorization. */
  334. /* > \endverbatim */
  335. /* > */
  336. /* > \param[in] LDA */
  337. /* > \verbatim */
  338. /* > LDA is INTEGER */
  339. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  340. /* > \endverbatim */
  341. /* > */
  342. /* > \param[in,out] B */
  343. /* > \verbatim */
  344. /* > B is REAL array, dimension (LDB,NRHS) */
  345. /* > On entry, the M-by-NRHS right hand side matrix B. */
  346. /* > On exit, the N-by-NRHS solution matrix X. */
  347. /* > \endverbatim */
  348. /* > */
  349. /* > \param[in] LDB */
  350. /* > \verbatim */
  351. /* > LDB is INTEGER */
  352. /* > The leading dimension of the array B. LDB >= f2cmax(1,M,N). */
  353. /* > \endverbatim */
  354. /* > */
  355. /* > \param[in,out] JPVT */
  356. /* > \verbatim */
  357. /* > JPVT is INTEGER array, dimension (N) */
  358. /* > On entry, if JPVT(i) .ne. 0, the i-th column of A is permuted */
  359. /* > to the front of AP, otherwise column i is a free column. */
  360. /* > On exit, if JPVT(i) = k, then the i-th column of AP */
  361. /* > was the k-th column of A. */
  362. /* > \endverbatim */
  363. /* > */
  364. /* > \param[in] RCOND */
  365. /* > \verbatim */
  366. /* > RCOND is REAL */
  367. /* > RCOND is used to determine the effective rank of A, which */
  368. /* > is defined as the order of the largest leading triangular */
  369. /* > submatrix R11 in the QR factorization with pivoting of A, */
  370. /* > whose estimated condition number < 1/RCOND. */
  371. /* > \endverbatim */
  372. /* > */
  373. /* > \param[out] RANK */
  374. /* > \verbatim */
  375. /* > RANK is INTEGER */
  376. /* > The effective rank of A, i.e., the order of the submatrix */
  377. /* > R11. This is the same as the order of the submatrix T11 */
  378. /* > in the complete orthogonal factorization of A. */
  379. /* > \endverbatim */
  380. /* > */
  381. /* > \param[out] WORK */
  382. /* > \verbatim */
  383. /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
  384. /* > On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  385. /* > \endverbatim */
  386. /* > */
  387. /* > \param[in] LWORK */
  388. /* > \verbatim */
  389. /* > LWORK is INTEGER */
  390. /* > The dimension of the array WORK. */
  391. /* > The unblocked strategy requires that: */
  392. /* > LWORK >= MAX( MN+3*N+1, 2*MN+NRHS ), */
  393. /* > where MN = f2cmin( M, N ). */
  394. /* > The block algorithm requires that: */
  395. /* > LWORK >= MAX( MN+2*N+NB*(N+1), 2*MN+NB*NRHS ), */
  396. /* > where NB is an upper bound on the blocksize returned */
  397. /* > by ILAENV for the routines SGEQP3, STZRZF, STZRQF, SORMQR, */
  398. /* > and SORMRZ. */
  399. /* > */
  400. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  401. /* > only calculates the optimal size of the WORK array, returns */
  402. /* > this value as the first entry of the WORK array, and no error */
  403. /* > message related to LWORK is issued by XERBLA. */
  404. /* > \endverbatim */
  405. /* > */
  406. /* > \param[out] INFO */
  407. /* > \verbatim */
  408. /* > INFO is INTEGER */
  409. /* > = 0: successful exit */
  410. /* > < 0: If INFO = -i, the i-th argument had an illegal value. */
  411. /* > \endverbatim */
  412. /* Authors: */
  413. /* ======== */
  414. /* > \author Univ. of Tennessee */
  415. /* > \author Univ. of California Berkeley */
  416. /* > \author Univ. of Colorado Denver */
  417. /* > \author NAG Ltd. */
  418. /* > \date December 2016 */
  419. /* > \ingroup realGEsolve */
  420. /* > \par Contributors: */
  421. /* ================== */
  422. /* > */
  423. /* > A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA \n */
  424. /* > E. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain \n */
  425. /* > G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain \n */
  426. /* > */
  427. /* ===================================================================== */
  428. /* Subroutine */ void sgelsy_(integer *m, integer *n, integer *nrhs, real *a,
  429. integer *lda, real *b, integer *ldb, integer *jpvt, real *rcond,
  430. integer *rank, real *work, integer *lwork, integer *info)
  431. {
  432. /* System generated locals */
  433. integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2;
  434. real r__1, r__2;
  435. /* Local variables */
  436. real anrm, bnrm, smin, smax;
  437. integer i__, j, iascl, ibscl, ismin, ismax;
  438. real c1, c2;
  439. extern /* Subroutine */ void scopy_(integer *, real *, integer *, real *,
  440. integer *);
  441. real wsize, s1, s2;
  442. extern /* Subroutine */ void strsm_(char *, char *, char *, char *,
  443. integer *, integer *, real *, real *, integer *, real *, integer *
  444. ), slaic1_(integer *, integer *,
  445. real *, real *, real *, real *, real *, real *, real *), sgeqp3_(
  446. integer *, integer *, real *, integer *, integer *, real *, real *
  447. , integer *, integer *);
  448. integer nb;
  449. extern /* Subroutine */ void slabad_(real *, real *);
  450. integer mn;
  451. extern real slamch_(char *), slange_(char *, integer *, integer *,
  452. real *, integer *, real *);
  453. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  454. extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
  455. integer *, integer *, ftnlen, ftnlen);
  456. real bignum;
  457. extern /* Subroutine */ void slascl_(char *, integer *, integer *, real *,
  458. real *, integer *, integer *, real *, integer *, integer *), slaset_(char *, integer *, integer *, real *, real *,
  459. real *, integer *);
  460. integer lwkmin, nb1, nb2, nb3, nb4;
  461. real sminpr, smaxpr, smlnum;
  462. integer lwkopt;
  463. logical lquery;
  464. extern /* Subroutine */ void sormqr_(char *, char *, integer *, integer *,
  465. integer *, real *, integer *, real *, real *, integer *, real *,
  466. integer *, integer *), sormrz_(char *, char *,
  467. integer *, integer *, integer *, integer *, real *, integer *,
  468. real *, real *, integer *, real *, integer *, integer *), stzrzf_(integer *, integer *, real *, integer *, real *,
  469. real *, integer *, integer *);
  470. /* -- LAPACK driver routine (version 3.7.0) -- */
  471. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  472. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  473. /* December 2016 */
  474. /* ===================================================================== */
  475. /* Parameter adjustments */
  476. a_dim1 = *lda;
  477. a_offset = 1 + a_dim1 * 1;
  478. a -= a_offset;
  479. b_dim1 = *ldb;
  480. b_offset = 1 + b_dim1 * 1;
  481. b -= b_offset;
  482. --jpvt;
  483. --work;
  484. /* Function Body */
  485. mn = f2cmin(*m,*n);
  486. ismin = mn + 1;
  487. ismax = (mn << 1) + 1;
  488. /* Test the input arguments. */
  489. *info = 0;
  490. lquery = *lwork == -1;
  491. if (*m < 0) {
  492. *info = -1;
  493. } else if (*n < 0) {
  494. *info = -2;
  495. } else if (*nrhs < 0) {
  496. *info = -3;
  497. } else if (*lda < f2cmax(1,*m)) {
  498. *info = -5;
  499. } else /* if(complicated condition) */ {
  500. /* Computing MAX */
  501. i__1 = f2cmax(1,*m);
  502. if (*ldb < f2cmax(i__1,*n)) {
  503. *info = -7;
  504. }
  505. }
  506. /* Figure out optimal block size */
  507. if (*info == 0) {
  508. if (mn == 0 || *nrhs == 0) {
  509. lwkmin = 1;
  510. lwkopt = 1;
  511. } else {
  512. nb1 = ilaenv_(&c__1, "SGEQRF", " ", m, n, &c_n1, &c_n1, (ftnlen)6,
  513. (ftnlen)1);
  514. nb2 = ilaenv_(&c__1, "SGERQF", " ", m, n, &c_n1, &c_n1, (ftnlen)6,
  515. (ftnlen)1);
  516. nb3 = ilaenv_(&c__1, "SORMQR", " ", m, n, nrhs, &c_n1, (ftnlen)6,
  517. (ftnlen)1);
  518. nb4 = ilaenv_(&c__1, "SORMRQ", " ", m, n, nrhs, &c_n1, (ftnlen)6,
  519. (ftnlen)1);
  520. /* Computing MAX */
  521. i__1 = f2cmax(nb1,nb2), i__1 = f2cmax(i__1,nb3);
  522. nb = f2cmax(i__1,nb4);
  523. /* Computing MAX */
  524. i__1 = mn << 1, i__2 = *n + 1, i__1 = f2cmax(i__1,i__2), i__2 = mn +
  525. *nrhs;
  526. lwkmin = mn + f2cmax(i__1,i__2);
  527. /* Computing MAX */
  528. i__1 = lwkmin, i__2 = mn + (*n << 1) + nb * (*n + 1), i__1 = f2cmax(
  529. i__1,i__2), i__2 = (mn << 1) + nb * *nrhs;
  530. lwkopt = f2cmax(i__1,i__2);
  531. }
  532. work[1] = (real) lwkopt;
  533. if (*lwork < lwkmin && ! lquery) {
  534. *info = -12;
  535. }
  536. }
  537. if (*info != 0) {
  538. i__1 = -(*info);
  539. xerbla_("SGELSY", &i__1, (ftnlen)6);
  540. return;
  541. } else if (lquery) {
  542. return;
  543. }
  544. /* Quick return if possible */
  545. if (mn == 0 || *nrhs == 0) {
  546. *rank = 0;
  547. return;
  548. }
  549. /* Get machine parameters */
  550. smlnum = slamch_("S") / slamch_("P");
  551. bignum = 1.f / smlnum;
  552. slabad_(&smlnum, &bignum);
  553. /* Scale A, B if f2cmax entries outside range [SMLNUM,BIGNUM] */
  554. anrm = slange_("M", m, n, &a[a_offset], lda, &work[1]);
  555. iascl = 0;
  556. if (anrm > 0.f && anrm < smlnum) {
  557. /* Scale matrix norm up to SMLNUM */
  558. slascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
  559. info);
  560. iascl = 1;
  561. } else if (anrm > bignum) {
  562. /* Scale matrix norm down to BIGNUM */
  563. slascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
  564. info);
  565. iascl = 2;
  566. } else if (anrm == 0.f) {
  567. /* Matrix all zero. Return zero solution. */
  568. i__1 = f2cmax(*m,*n);
  569. slaset_("F", &i__1, nrhs, &c_b31, &c_b31, &b[b_offset], ldb);
  570. *rank = 0;
  571. goto L70;
  572. }
  573. bnrm = slange_("M", m, nrhs, &b[b_offset], ldb, &work[1]);
  574. ibscl = 0;
  575. if (bnrm > 0.f && bnrm < smlnum) {
  576. /* Scale matrix norm up to SMLNUM */
  577. slascl_("G", &c__0, &c__0, &bnrm, &smlnum, m, nrhs, &b[b_offset], ldb,
  578. info);
  579. ibscl = 1;
  580. } else if (bnrm > bignum) {
  581. /* Scale matrix norm down to BIGNUM */
  582. slascl_("G", &c__0, &c__0, &bnrm, &bignum, m, nrhs, &b[b_offset], ldb,
  583. info);
  584. ibscl = 2;
  585. }
  586. /* Compute QR factorization with column pivoting of A: */
  587. /* A * P = Q * R */
  588. i__1 = *lwork - mn;
  589. sgeqp3_(m, n, &a[a_offset], lda, &jpvt[1], &work[1], &work[mn + 1], &i__1,
  590. info);
  591. wsize = mn + work[mn + 1];
  592. /* workspace: MN+2*N+NB*(N+1). */
  593. /* Details of Householder rotations stored in WORK(1:MN). */
  594. /* Determine RANK using incremental condition estimation */
  595. work[ismin] = 1.f;
  596. work[ismax] = 1.f;
  597. smax = (r__1 = a[a_dim1 + 1], abs(r__1));
  598. smin = smax;
  599. if ((r__1 = a[a_dim1 + 1], abs(r__1)) == 0.f) {
  600. *rank = 0;
  601. i__1 = f2cmax(*m,*n);
  602. slaset_("F", &i__1, nrhs, &c_b31, &c_b31, &b[b_offset], ldb);
  603. goto L70;
  604. } else {
  605. *rank = 1;
  606. }
  607. L10:
  608. if (*rank < mn) {
  609. i__ = *rank + 1;
  610. slaic1_(&c__2, rank, &work[ismin], &smin, &a[i__ * a_dim1 + 1], &a[
  611. i__ + i__ * a_dim1], &sminpr, &s1, &c1);
  612. slaic1_(&c__1, rank, &work[ismax], &smax, &a[i__ * a_dim1 + 1], &a[
  613. i__ + i__ * a_dim1], &smaxpr, &s2, &c2);
  614. if (smaxpr * *rcond <= sminpr) {
  615. i__1 = *rank;
  616. for (i__ = 1; i__ <= i__1; ++i__) {
  617. work[ismin + i__ - 1] = s1 * work[ismin + i__ - 1];
  618. work[ismax + i__ - 1] = s2 * work[ismax + i__ - 1];
  619. /* L20: */
  620. }
  621. work[ismin + *rank] = c1;
  622. work[ismax + *rank] = c2;
  623. smin = sminpr;
  624. smax = smaxpr;
  625. ++(*rank);
  626. goto L10;
  627. }
  628. }
  629. /* workspace: 3*MN. */
  630. /* Logically partition R = [ R11 R12 ] */
  631. /* [ 0 R22 ] */
  632. /* where R11 = R(1:RANK,1:RANK) */
  633. /* [R11,R12] = [ T11, 0 ] * Y */
  634. if (*rank < *n) {
  635. i__1 = *lwork - (mn << 1);
  636. stzrzf_(rank, n, &a[a_offset], lda, &work[mn + 1], &work[(mn << 1) +
  637. 1], &i__1, info);
  638. }
  639. /* workspace: 2*MN. */
  640. /* Details of Householder rotations stored in WORK(MN+1:2*MN) */
  641. /* B(1:M,1:NRHS) := Q**T * B(1:M,1:NRHS) */
  642. i__1 = *lwork - (mn << 1);
  643. sormqr_("Left", "Transpose", m, nrhs, &mn, &a[a_offset], lda, &work[1], &
  644. b[b_offset], ldb, &work[(mn << 1) + 1], &i__1, info);
  645. /* Computing MAX */
  646. r__1 = wsize, r__2 = (mn << 1) + work[(mn << 1) + 1];
  647. wsize = f2cmax(r__1,r__2);
  648. /* workspace: 2*MN+NB*NRHS. */
  649. /* B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS) */
  650. strsm_("Left", "Upper", "No transpose", "Non-unit", rank, nrhs, &c_b54, &
  651. a[a_offset], lda, &b[b_offset], ldb);
  652. i__1 = *nrhs;
  653. for (j = 1; j <= i__1; ++j) {
  654. i__2 = *n;
  655. for (i__ = *rank + 1; i__ <= i__2; ++i__) {
  656. b[i__ + j * b_dim1] = 0.f;
  657. /* L30: */
  658. }
  659. /* L40: */
  660. }
  661. /* B(1:N,1:NRHS) := Y**T * B(1:N,1:NRHS) */
  662. if (*rank < *n) {
  663. i__1 = *n - *rank;
  664. i__2 = *lwork - (mn << 1);
  665. sormrz_("Left", "Transpose", n, nrhs, rank, &i__1, &a[a_offset], lda,
  666. &work[mn + 1], &b[b_offset], ldb, &work[(mn << 1) + 1], &i__2,
  667. info);
  668. }
  669. /* workspace: 2*MN+NRHS. */
  670. /* B(1:N,1:NRHS) := P * B(1:N,1:NRHS) */
  671. i__1 = *nrhs;
  672. for (j = 1; j <= i__1; ++j) {
  673. i__2 = *n;
  674. for (i__ = 1; i__ <= i__2; ++i__) {
  675. work[jpvt[i__]] = b[i__ + j * b_dim1];
  676. /* L50: */
  677. }
  678. scopy_(n, &work[1], &c__1, &b[j * b_dim1 + 1], &c__1);
  679. /* L60: */
  680. }
  681. /* workspace: N. */
  682. /* Undo scaling */
  683. if (iascl == 1) {
  684. slascl_("G", &c__0, &c__0, &anrm, &smlnum, n, nrhs, &b[b_offset], ldb,
  685. info);
  686. slascl_("U", &c__0, &c__0, &smlnum, &anrm, rank, rank, &a[a_offset],
  687. lda, info);
  688. } else if (iascl == 2) {
  689. slascl_("G", &c__0, &c__0, &anrm, &bignum, n, nrhs, &b[b_offset], ldb,
  690. info);
  691. slascl_("U", &c__0, &c__0, &bignum, &anrm, rank, rank, &a[a_offset],
  692. lda, info);
  693. }
  694. if (ibscl == 1) {
  695. slascl_("G", &c__0, &c__0, &smlnum, &bnrm, n, nrhs, &b[b_offset], ldb,
  696. info);
  697. } else if (ibscl == 2) {
  698. slascl_("G", &c__0, &c__0, &bignum, &bnrm, n, nrhs, &b[b_offset], ldb,
  699. info);
  700. }
  701. L70:
  702. work[1] = (real) lwkopt;
  703. return;
  704. /* End of SGELSY */
  705. } /* sgelsy_ */