dgelsx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* dgelsx.f -- translated by f2c (version 20061008).
  2. You must link the resulting object file with libf2c:
  3. on Microsoft Windows system, link with libf2c.lib;
  4. on Linux or Unix systems, link with .../path/to/libf2c.a -lm
  5. or, if you install libf2c.a in a standard place, with -lf2c -lm
  6. -- in that order, at the end of the command line, as in
  7. cc *.o -lf2c -lm
  8. Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
  9. http://www.netlib.org/f2c/libf2c.zip
  10. */
  11. #include "f2c.h"
  12. #include "blaswrap.h"
  13. /* Table of constant values */
  14. static integer c__0 = 0;
  15. static doublereal c_b13 = 0.;
  16. static integer c__2 = 2;
  17. static integer c__1 = 1;
  18. static doublereal c_b36 = 1.;
  19. /* Subroutine */ int _starpu_dgelsx_(integer *m, integer *n, integer *nrhs,
  20. doublereal *a, integer *lda, doublereal *b, integer *ldb, integer *
  21. jpvt, doublereal *rcond, integer *rank, doublereal *work, integer *
  22. info)
  23. {
  24. /* System generated locals */
  25. integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2;
  26. doublereal d__1;
  27. /* Local variables */
  28. integer i__, j, k;
  29. doublereal c1, c2, s1, s2, t1, t2;
  30. integer mn;
  31. doublereal anrm, bnrm, smin, smax;
  32. integer iascl, ibscl, ismin, ismax;
  33. extern /* Subroutine */ int _starpu_dtrsm_(char *, char *, char *, char *,
  34. integer *, integer *, doublereal *, doublereal *, integer *,
  35. doublereal *, integer *), _starpu_dlaic1_(
  36. integer *, integer *, doublereal *, doublereal *, doublereal *,
  37. doublereal *, doublereal *, doublereal *, doublereal *), _starpu_dorm2r_(
  38. char *, char *, integer *, integer *, integer *, doublereal *,
  39. integer *, doublereal *, doublereal *, integer *, doublereal *,
  40. integer *), _starpu_dlabad_(doublereal *, doublereal *);
  41. extern doublereal _starpu_dlamch_(char *), _starpu_dlange_(char *, integer *,
  42. integer *, doublereal *, integer *, doublereal *);
  43. extern /* Subroutine */ int _starpu_dlascl_(char *, integer *, integer *,
  44. doublereal *, doublereal *, integer *, integer *, doublereal *,
  45. integer *, integer *), _starpu_dgeqpf_(integer *, integer *,
  46. doublereal *, integer *, integer *, doublereal *, doublereal *,
  47. integer *), _starpu_dlaset_(char *, integer *, integer *, doublereal *,
  48. doublereal *, doublereal *, integer *), _starpu_xerbla_(char *,
  49. integer *);
  50. doublereal bignum;
  51. extern /* Subroutine */ int _starpu_dlatzm_(char *, integer *, integer *,
  52. doublereal *, integer *, doublereal *, doublereal *, doublereal *,
  53. integer *, doublereal *);
  54. doublereal sminpr, smaxpr, smlnum;
  55. extern /* Subroutine */ int _starpu_dtzrqf_(integer *, integer *, doublereal *,
  56. integer *, doublereal *, integer *);
  57. /* -- LAPACK driver routine (version 3.2) -- */
  58. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  59. /* November 2006 */
  60. /* .. Scalar Arguments .. */
  61. /* .. */
  62. /* .. Array Arguments .. */
  63. /* .. */
  64. /* Purpose */
  65. /* ======= */
  66. /* This routine is deprecated and has been replaced by routine DGELSY. */
  67. /* DGELSX computes the minimum-norm solution to a real linear least */
  68. /* squares problem: */
  69. /* minimize || A * X - B || */
  70. /* using a complete orthogonal factorization of A. A is an M-by-N */
  71. /* matrix which may be rank-deficient. */
  72. /* Several right hand side vectors b and solution vectors x can be */
  73. /* handled in a single call; they are stored as the columns of the */
  74. /* M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
  75. /* matrix X. */
  76. /* The routine first computes a QR factorization with column pivoting: */
  77. /* A * P = Q * [ R11 R12 ] */
  78. /* [ 0 R22 ] */
  79. /* with R11 defined as the largest leading submatrix whose estimated */
  80. /* condition number is less than 1/RCOND. The order of R11, RANK, */
  81. /* is the effective rank of A. */
  82. /* Then, R22 is considered to be negligible, and R12 is annihilated */
  83. /* by orthogonal transformations from the right, arriving at the */
  84. /* complete orthogonal factorization: */
  85. /* A * P = Q * [ T11 0 ] * Z */
  86. /* [ 0 0 ] */
  87. /* The minimum-norm solution is then */
  88. /* X = P * Z' [ inv(T11)*Q1'*B ] */
  89. /* [ 0 ] */
  90. /* where Q1 consists of the first RANK columns of Q. */
  91. /* Arguments */
  92. /* ========= */
  93. /* M (input) INTEGER */
  94. /* The number of rows of the matrix A. M >= 0. */
  95. /* N (input) INTEGER */
  96. /* The number of columns of the matrix A. N >= 0. */
  97. /* NRHS (input) INTEGER */
  98. /* The number of right hand sides, i.e., the number of */
  99. /* columns of matrices B and X. NRHS >= 0. */
  100. /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
  101. /* On entry, the M-by-N matrix A. */
  102. /* On exit, A has been overwritten by details of its */
  103. /* complete orthogonal factorization. */
  104. /* LDA (input) INTEGER */
  105. /* The leading dimension of the array A. LDA >= max(1,M). */
  106. /* B (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  107. /* On entry, the M-by-NRHS right hand side matrix B. */
  108. /* On exit, the N-by-NRHS solution matrix X. */
  109. /* If m >= n and RANK = n, the residual sum-of-squares for */
  110. /* the solution in the i-th column is given by the sum of */
  111. /* squares of elements N+1:M in that column. */
  112. /* LDB (input) INTEGER */
  113. /* The leading dimension of the array B. LDB >= max(1,M,N). */
  114. /* JPVT (input/output) INTEGER array, dimension (N) */
  115. /* On entry, if JPVT(i) .ne. 0, the i-th column of A is an */
  116. /* initial column, otherwise it is a free column. Before */
  117. /* the QR factorization of A, all initial columns are */
  118. /* permuted to the leading positions; only the remaining */
  119. /* free columns are moved as a result of column pivoting */
  120. /* during the factorization. */
  121. /* On exit, if JPVT(i) = k, then the i-th column of A*P */
  122. /* was the k-th column of A. */
  123. /* RCOND (input) DOUBLE PRECISION */
  124. /* RCOND is used to determine the effective rank of A, which */
  125. /* is defined as the order of the largest leading triangular */
  126. /* submatrix R11 in the QR factorization with pivoting of A, */
  127. /* whose estimated condition number < 1/RCOND. */
  128. /* RANK (output) INTEGER */
  129. /* The effective rank of A, i.e., the order of the submatrix */
  130. /* R11. This is the same as the order of the submatrix T11 */
  131. /* in the complete orthogonal factorization of A. */
  132. /* WORK (workspace) DOUBLE PRECISION array, dimension */
  133. /* (max( min(M,N)+3*N, 2*min(M,N)+NRHS )), */
  134. /* INFO (output) INTEGER */
  135. /* = 0: successful exit */
  136. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  137. /* ===================================================================== */
  138. /* .. Parameters .. */
  139. /* .. */
  140. /* .. Local Scalars .. */
  141. /* .. */
  142. /* .. External Functions .. */
  143. /* .. */
  144. /* .. External Subroutines .. */
  145. /* .. */
  146. /* .. Intrinsic Functions .. */
  147. /* .. */
  148. /* .. Executable Statements .. */
  149. /* Parameter adjustments */
  150. a_dim1 = *lda;
  151. a_offset = 1 + a_dim1;
  152. a -= a_offset;
  153. b_dim1 = *ldb;
  154. b_offset = 1 + b_dim1;
  155. b -= b_offset;
  156. --jpvt;
  157. --work;
  158. /* Function Body */
  159. mn = min(*m,*n);
  160. ismin = mn + 1;
  161. ismax = (mn << 1) + 1;
  162. /* Test the input arguments. */
  163. *info = 0;
  164. if (*m < 0) {
  165. *info = -1;
  166. } else if (*n < 0) {
  167. *info = -2;
  168. } else if (*nrhs < 0) {
  169. *info = -3;
  170. } else if (*lda < max(1,*m)) {
  171. *info = -5;
  172. } else /* if(complicated condition) */ {
  173. /* Computing MAX */
  174. i__1 = max(1,*m);
  175. if (*ldb < max(i__1,*n)) {
  176. *info = -7;
  177. }
  178. }
  179. if (*info != 0) {
  180. i__1 = -(*info);
  181. _starpu_xerbla_("DGELSX", &i__1);
  182. return 0;
  183. }
  184. /* Quick return if possible */
  185. /* Computing MIN */
  186. i__1 = min(*m,*n);
  187. if (min(i__1,*nrhs) == 0) {
  188. *rank = 0;
  189. return 0;
  190. }
  191. /* Get machine parameters */
  192. smlnum = _starpu_dlamch_("S") / _starpu_dlamch_("P");
  193. bignum = 1. / smlnum;
  194. _starpu_dlabad_(&smlnum, &bignum);
  195. /* Scale A, B if max elements outside range [SMLNUM,BIGNUM] */
  196. anrm = _starpu_dlange_("M", m, n, &a[a_offset], lda, &work[1]);
  197. iascl = 0;
  198. if (anrm > 0. && anrm < smlnum) {
  199. /* Scale matrix norm up to SMLNUM */
  200. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
  201. info);
  202. iascl = 1;
  203. } else if (anrm > bignum) {
  204. /* Scale matrix norm down to BIGNUM */
  205. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
  206. info);
  207. iascl = 2;
  208. } else if (anrm == 0.) {
  209. /* Matrix all zero. Return zero solution. */
  210. i__1 = max(*m,*n);
  211. _starpu_dlaset_("F", &i__1, nrhs, &c_b13, &c_b13, &b[b_offset], ldb);
  212. *rank = 0;
  213. goto L100;
  214. }
  215. bnrm = _starpu_dlange_("M", m, nrhs, &b[b_offset], ldb, &work[1]);
  216. ibscl = 0;
  217. if (bnrm > 0. && bnrm < smlnum) {
  218. /* Scale matrix norm up to SMLNUM */
  219. _starpu_dlascl_("G", &c__0, &c__0, &bnrm, &smlnum, m, nrhs, &b[b_offset], ldb,
  220. info);
  221. ibscl = 1;
  222. } else if (bnrm > bignum) {
  223. /* Scale matrix norm down to BIGNUM */
  224. _starpu_dlascl_("G", &c__0, &c__0, &bnrm, &bignum, m, nrhs, &b[b_offset], ldb,
  225. info);
  226. ibscl = 2;
  227. }
  228. /* Compute QR factorization with column pivoting of A: */
  229. /* A * P = Q * R */
  230. _starpu_dgeqpf_(m, n, &a[a_offset], lda, &jpvt[1], &work[1], &work[mn + 1], info);
  231. /* workspace 3*N. Details of Householder rotations stored */
  232. /* in WORK(1:MN). */
  233. /* Determine RANK using incremental condition estimation */
  234. work[ismin] = 1.;
  235. work[ismax] = 1.;
  236. smax = (d__1 = a[a_dim1 + 1], abs(d__1));
  237. smin = smax;
  238. if ((d__1 = a[a_dim1 + 1], abs(d__1)) == 0.) {
  239. *rank = 0;
  240. i__1 = max(*m,*n);
  241. _starpu_dlaset_("F", &i__1, nrhs, &c_b13, &c_b13, &b[b_offset], ldb);
  242. goto L100;
  243. } else {
  244. *rank = 1;
  245. }
  246. L10:
  247. if (*rank < mn) {
  248. i__ = *rank + 1;
  249. _starpu_dlaic1_(&c__2, rank, &work[ismin], &smin, &a[i__ * a_dim1 + 1], &a[
  250. i__ + i__ * a_dim1], &sminpr, &s1, &c1);
  251. _starpu_dlaic1_(&c__1, rank, &work[ismax], &smax, &a[i__ * a_dim1 + 1], &a[
  252. i__ + i__ * a_dim1], &smaxpr, &s2, &c2);
  253. if (smaxpr * *rcond <= sminpr) {
  254. i__1 = *rank;
  255. for (i__ = 1; i__ <= i__1; ++i__) {
  256. work[ismin + i__ - 1] = s1 * work[ismin + i__ - 1];
  257. work[ismax + i__ - 1] = s2 * work[ismax + i__ - 1];
  258. /* L20: */
  259. }
  260. work[ismin + *rank] = c1;
  261. work[ismax + *rank] = c2;
  262. smin = sminpr;
  263. smax = smaxpr;
  264. ++(*rank);
  265. goto L10;
  266. }
  267. }
  268. /* Logically partition R = [ R11 R12 ] */
  269. /* [ 0 R22 ] */
  270. /* where R11 = R(1:RANK,1:RANK) */
  271. /* [R11,R12] = [ T11, 0 ] * Y */
  272. if (*rank < *n) {
  273. _starpu_dtzrqf_(rank, n, &a[a_offset], lda, &work[mn + 1], info);
  274. }
  275. /* Details of Householder rotations stored in WORK(MN+1:2*MN) */
  276. /* B(1:M,1:NRHS) := Q' * B(1:M,1:NRHS) */
  277. _starpu_dorm2r_("Left", "Transpose", m, nrhs, &mn, &a[a_offset], lda, &work[1], &
  278. b[b_offset], ldb, &work[(mn << 1) + 1], info);
  279. /* workspace NRHS */
  280. /* B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS) */
  281. _starpu_dtrsm_("Left", "Upper", "No transpose", "Non-unit", rank, nrhs, &c_b36, &
  282. a[a_offset], lda, &b[b_offset], ldb);
  283. i__1 = *n;
  284. for (i__ = *rank + 1; i__ <= i__1; ++i__) {
  285. i__2 = *nrhs;
  286. for (j = 1; j <= i__2; ++j) {
  287. b[i__ + j * b_dim1] = 0.;
  288. /* L30: */
  289. }
  290. /* L40: */
  291. }
  292. /* B(1:N,1:NRHS) := Y' * B(1:N,1:NRHS) */
  293. if (*rank < *n) {
  294. i__1 = *rank;
  295. for (i__ = 1; i__ <= i__1; ++i__) {
  296. i__2 = *n - *rank + 1;
  297. _starpu_dlatzm_("Left", &i__2, nrhs, &a[i__ + (*rank + 1) * a_dim1], lda,
  298. &work[mn + i__], &b[i__ + b_dim1], &b[*rank + 1 + b_dim1],
  299. ldb, &work[(mn << 1) + 1]);
  300. /* L50: */
  301. }
  302. }
  303. /* workspace NRHS */
  304. /* B(1:N,1:NRHS) := P * B(1:N,1:NRHS) */
  305. i__1 = *nrhs;
  306. for (j = 1; j <= i__1; ++j) {
  307. i__2 = *n;
  308. for (i__ = 1; i__ <= i__2; ++i__) {
  309. work[(mn << 1) + i__] = 1.;
  310. /* L60: */
  311. }
  312. i__2 = *n;
  313. for (i__ = 1; i__ <= i__2; ++i__) {
  314. if (work[(mn << 1) + i__] == 1.) {
  315. if (jpvt[i__] != i__) {
  316. k = i__;
  317. t1 = b[k + j * b_dim1];
  318. t2 = b[jpvt[k] + j * b_dim1];
  319. L70:
  320. b[jpvt[k] + j * b_dim1] = t1;
  321. work[(mn << 1) + k] = 0.;
  322. t1 = t2;
  323. k = jpvt[k];
  324. t2 = b[jpvt[k] + j * b_dim1];
  325. if (jpvt[k] != i__) {
  326. goto L70;
  327. }
  328. b[i__ + j * b_dim1] = t1;
  329. work[(mn << 1) + k] = 0.;
  330. }
  331. }
  332. /* L80: */
  333. }
  334. /* L90: */
  335. }
  336. /* Undo scaling */
  337. if (iascl == 1) {
  338. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &smlnum, n, nrhs, &b[b_offset], ldb,
  339. info);
  340. _starpu_dlascl_("U", &c__0, &c__0, &smlnum, &anrm, rank, rank, &a[a_offset],
  341. lda, info);
  342. } else if (iascl == 2) {
  343. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &bignum, n, nrhs, &b[b_offset], ldb,
  344. info);
  345. _starpu_dlascl_("U", &c__0, &c__0, &bignum, &anrm, rank, rank, &a[a_offset],
  346. lda, info);
  347. }
  348. if (ibscl == 1) {
  349. _starpu_dlascl_("G", &c__0, &c__0, &smlnum, &bnrm, n, nrhs, &b[b_offset], ldb,
  350. info);
  351. } else if (ibscl == 2) {
  352. _starpu_dlascl_("G", &c__0, &c__0, &bignum, &bnrm, n, nrhs, &b[b_offset], ldb,
  353. info);
  354. }
  355. L100:
  356. return 0;
  357. /* End of DGELSX */
  358. } /* _starpu_dgelsx_ */