dgelsy.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* dgelsy.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__1 = 1;
  15. static integer c_n1 = -1;
  16. static integer c__0 = 0;
  17. static doublereal c_b31 = 0.;
  18. static integer c__2 = 2;
  19. static doublereal c_b54 = 1.;
  20. /* Subroutine */ int _starpu_dgelsy_(integer *m, integer *n, integer *nrhs,
  21. doublereal *a, integer *lda, doublereal *b, integer *ldb, integer *
  22. jpvt, doublereal *rcond, integer *rank, doublereal *work, integer *
  23. lwork, integer *info)
  24. {
  25. /* System generated locals */
  26. integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2;
  27. doublereal d__1, d__2;
  28. /* Local variables */
  29. integer i__, j;
  30. doublereal c1, c2, s1, s2;
  31. integer nb, mn, nb1, nb2, nb3, nb4;
  32. doublereal anrm, bnrm, smin, smax;
  33. integer iascl, ibscl;
  34. extern /* Subroutine */ int _starpu_dcopy_(integer *, doublereal *, integer *,
  35. doublereal *, integer *);
  36. integer ismin, ismax;
  37. extern /* Subroutine */ int _starpu_dtrsm_(char *, char *, char *, char *,
  38. integer *, integer *, doublereal *, doublereal *, integer *,
  39. doublereal *, integer *), _starpu_dlaic1_(
  40. integer *, integer *, doublereal *, doublereal *, doublereal *,
  41. doublereal *, doublereal *, doublereal *, doublereal *);
  42. doublereal wsize;
  43. extern /* Subroutine */ int _starpu_dgeqp3_(integer *, integer *, doublereal *,
  44. integer *, integer *, doublereal *, doublereal *, integer *,
  45. integer *), _starpu_dlabad_(doublereal *, doublereal *);
  46. extern doublereal _starpu_dlamch_(char *), _starpu_dlange_(char *, integer *,
  47. integer *, doublereal *, integer *, doublereal *);
  48. extern /* Subroutine */ int _starpu_dlascl_(char *, integer *, integer *,
  49. doublereal *, doublereal *, integer *, integer *, doublereal *,
  50. integer *, integer *), _starpu_dlaset_(char *, integer *, integer
  51. *, doublereal *, doublereal *, doublereal *, integer *),
  52. _starpu_xerbla_(char *, integer *);
  53. extern integer _starpu_ilaenv_(integer *, char *, char *, integer *, integer *,
  54. integer *, integer *);
  55. doublereal bignum;
  56. integer lwkmin;
  57. extern /* Subroutine */ int _starpu_dormqr_(char *, char *, integer *, integer *,
  58. integer *, doublereal *, integer *, doublereal *, doublereal *,
  59. integer *, doublereal *, integer *, integer *);
  60. doublereal sminpr, smaxpr, smlnum;
  61. extern /* Subroutine */ int _starpu_dormrz_(char *, char *, integer *, integer *,
  62. integer *, integer *, doublereal *, integer *, doublereal *,
  63. doublereal *, integer *, doublereal *, integer *, integer *);
  64. integer lwkopt;
  65. logical lquery;
  66. extern /* Subroutine */ int _starpu_dtzrzf_(integer *, integer *, doublereal *,
  67. integer *, doublereal *, doublereal *, integer *, integer *);
  68. /* -- LAPACK driver routine (version 3.2) -- */
  69. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  70. /* November 2006 */
  71. /* .. Scalar Arguments .. */
  72. /* .. */
  73. /* .. Array Arguments .. */
  74. /* .. */
  75. /* Purpose */
  76. /* ======= */
  77. /* DGELSY computes the minimum-norm solution to a real linear least */
  78. /* squares problem: */
  79. /* minimize || A * X - B || */
  80. /* using a complete orthogonal factorization of A. A is an M-by-N */
  81. /* matrix which may be rank-deficient. */
  82. /* Several right hand side vectors b and solution vectors x can be */
  83. /* handled in a single call; they are stored as the columns of the */
  84. /* M-by-NRHS right hand side matrix B and the N-by-NRHS solution */
  85. /* matrix X. */
  86. /* The routine first computes a QR factorization with column pivoting: */
  87. /* A * P = Q * [ R11 R12 ] */
  88. /* [ 0 R22 ] */
  89. /* with R11 defined as the largest leading submatrix whose estimated */
  90. /* condition number is less than 1/RCOND. The order of R11, RANK, */
  91. /* is the effective rank of A. */
  92. /* Then, R22 is considered to be negligible, and R12 is annihilated */
  93. /* by orthogonal transformations from the right, arriving at the */
  94. /* complete orthogonal factorization: */
  95. /* A * P = Q * [ T11 0 ] * Z */
  96. /* [ 0 0 ] */
  97. /* The minimum-norm solution is then */
  98. /* X = P * Z' [ inv(T11)*Q1'*B ] */
  99. /* [ 0 ] */
  100. /* where Q1 consists of the first RANK columns of Q. */
  101. /* This routine is basically identical to the original xGELSX except */
  102. /* three differences: */
  103. /* o The call to the subroutine xGEQPF has been substituted by the */
  104. /* the call to the subroutine xGEQP3. This subroutine is a Blas-3 */
  105. /* version of the QR factorization with column pivoting. */
  106. /* o Matrix B (the right hand side) is updated with Blas-3. */
  107. /* o The permutation of matrix B (the right hand side) is faster and */
  108. /* more simple. */
  109. /* Arguments */
  110. /* ========= */
  111. /* M (input) INTEGER */
  112. /* The number of rows of the matrix A. M >= 0. */
  113. /* N (input) INTEGER */
  114. /* The number of columns of the matrix A. N >= 0. */
  115. /* NRHS (input) INTEGER */
  116. /* The number of right hand sides, i.e., the number of */
  117. /* columns of matrices B and X. NRHS >= 0. */
  118. /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
  119. /* On entry, the M-by-N matrix A. */
  120. /* On exit, A has been overwritten by details of its */
  121. /* complete orthogonal factorization. */
  122. /* LDA (input) INTEGER */
  123. /* The leading dimension of the array A. LDA >= max(1,M). */
  124. /* B (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  125. /* On entry, the M-by-NRHS right hand side matrix B. */
  126. /* On exit, the N-by-NRHS solution matrix X. */
  127. /* LDB (input) INTEGER */
  128. /* The leading dimension of the array B. LDB >= max(1,M,N). */
  129. /* JPVT (input/output) INTEGER array, dimension (N) */
  130. /* On entry, if JPVT(i) .ne. 0, the i-th column of A is permuted */
  131. /* to the front of AP, otherwise column i is a free column. */
  132. /* On exit, if JPVT(i) = k, then the i-th column of AP */
  133. /* was the k-th column of A. */
  134. /* RCOND (input) DOUBLE PRECISION */
  135. /* RCOND is used to determine the effective rank of A, which */
  136. /* is defined as the order of the largest leading triangular */
  137. /* submatrix R11 in the QR factorization with pivoting of A, */
  138. /* whose estimated condition number < 1/RCOND. */
  139. /* RANK (output) INTEGER */
  140. /* The effective rank of A, i.e., the order of the submatrix */
  141. /* R11. This is the same as the order of the submatrix T11 */
  142. /* in the complete orthogonal factorization of A. */
  143. /* WORK (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
  144. /* On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  145. /* LWORK (input) INTEGER */
  146. /* The dimension of the array WORK. */
  147. /* The unblocked strategy requires that: */
  148. /* LWORK >= MAX( MN+3*N+1, 2*MN+NRHS ), */
  149. /* where MN = min( M, N ). */
  150. /* The block algorithm requires that: */
  151. /* LWORK >= MAX( MN+2*N+NB*(N+1), 2*MN+NB*NRHS ), */
  152. /* where NB is an upper bound on the blocksize returned */
  153. /* by ILAENV for the routines DGEQP3, DTZRZF, STZRQF, DORMQR, */
  154. /* and DORMRZ. */
  155. /* If LWORK = -1, then a workspace query is assumed; the routine */
  156. /* only calculates the optimal size of the WORK array, returns */
  157. /* this value as the first entry of the WORK array, and no error */
  158. /* message related to LWORK is issued by XERBLA. */
  159. /* INFO (output) INTEGER */
  160. /* = 0: successful exit */
  161. /* < 0: If INFO = -i, the i-th argument had an illegal value. */
  162. /* Further Details */
  163. /* =============== */
  164. /* Based on contributions by */
  165. /* A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA */
  166. /* E. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain */
  167. /* G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain */
  168. /* ===================================================================== */
  169. /* .. Parameters .. */
  170. /* .. */
  171. /* .. Local Scalars .. */
  172. /* .. */
  173. /* .. External Functions .. */
  174. /* .. */
  175. /* .. External Subroutines .. */
  176. /* .. */
  177. /* .. Intrinsic Functions .. */
  178. /* .. */
  179. /* .. Executable Statements .. */
  180. /* Parameter adjustments */
  181. a_dim1 = *lda;
  182. a_offset = 1 + a_dim1;
  183. a -= a_offset;
  184. b_dim1 = *ldb;
  185. b_offset = 1 + b_dim1;
  186. b -= b_offset;
  187. --jpvt;
  188. --work;
  189. /* Function Body */
  190. mn = min(*m,*n);
  191. ismin = mn + 1;
  192. ismax = (mn << 1) + 1;
  193. /* Test the input arguments. */
  194. *info = 0;
  195. lquery = *lwork == -1;
  196. if (*m < 0) {
  197. *info = -1;
  198. } else if (*n < 0) {
  199. *info = -2;
  200. } else if (*nrhs < 0) {
  201. *info = -3;
  202. } else if (*lda < max(1,*m)) {
  203. *info = -5;
  204. } else /* if(complicated condition) */ {
  205. /* Computing MAX */
  206. i__1 = max(1,*m);
  207. if (*ldb < max(i__1,*n)) {
  208. *info = -7;
  209. }
  210. }
  211. /* Figure out optimal block size */
  212. if (*info == 0) {
  213. if (mn == 0 || *nrhs == 0) {
  214. lwkmin = 1;
  215. lwkopt = 1;
  216. } else {
  217. nb1 = _starpu_ilaenv_(&c__1, "DGEQRF", " ", m, n, &c_n1, &c_n1);
  218. nb2 = _starpu_ilaenv_(&c__1, "DGERQF", " ", m, n, &c_n1, &c_n1);
  219. nb3 = _starpu_ilaenv_(&c__1, "DORMQR", " ", m, n, nrhs, &c_n1);
  220. nb4 = _starpu_ilaenv_(&c__1, "DORMRQ", " ", m, n, nrhs, &c_n1);
  221. /* Computing MAX */
  222. i__1 = max(nb1,nb2), i__1 = max(i__1,nb3);
  223. nb = max(i__1,nb4);
  224. /* Computing MAX */
  225. i__1 = mn << 1, i__2 = *n + 1, i__1 = max(i__1,i__2), i__2 = mn +
  226. *nrhs;
  227. lwkmin = mn + max(i__1,i__2);
  228. /* Computing MAX */
  229. i__1 = lwkmin, i__2 = mn + (*n << 1) + nb * (*n + 1), i__1 = max(
  230. i__1,i__2), i__2 = (mn << 1) + nb * *nrhs;
  231. lwkopt = max(i__1,i__2);
  232. }
  233. work[1] = (doublereal) lwkopt;
  234. if (*lwork < lwkmin && ! lquery) {
  235. *info = -12;
  236. }
  237. }
  238. if (*info != 0) {
  239. i__1 = -(*info);
  240. _starpu_xerbla_("DGELSY", &i__1);
  241. return 0;
  242. } else if (lquery) {
  243. return 0;
  244. }
  245. /* Quick return if possible */
  246. if (mn == 0 || *nrhs == 0) {
  247. *rank = 0;
  248. return 0;
  249. }
  250. /* Get machine parameters */
  251. smlnum = _starpu_dlamch_("S") / _starpu_dlamch_("P");
  252. bignum = 1. / smlnum;
  253. _starpu_dlabad_(&smlnum, &bignum);
  254. /* Scale A, B if max entries outside range [SMLNUM,BIGNUM] */
  255. anrm = _starpu_dlange_("M", m, n, &a[a_offset], lda, &work[1]);
  256. iascl = 0;
  257. if (anrm > 0. && anrm < smlnum) {
  258. /* Scale matrix norm up to SMLNUM */
  259. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
  260. info);
  261. iascl = 1;
  262. } else if (anrm > bignum) {
  263. /* Scale matrix norm down to BIGNUM */
  264. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
  265. info);
  266. iascl = 2;
  267. } else if (anrm == 0.) {
  268. /* Matrix all zero. Return zero solution. */
  269. i__1 = max(*m,*n);
  270. _starpu_dlaset_("F", &i__1, nrhs, &c_b31, &c_b31, &b[b_offset], ldb);
  271. *rank = 0;
  272. goto L70;
  273. }
  274. bnrm = _starpu_dlange_("M", m, nrhs, &b[b_offset], ldb, &work[1]);
  275. ibscl = 0;
  276. if (bnrm > 0. && bnrm < smlnum) {
  277. /* Scale matrix norm up to SMLNUM */
  278. _starpu_dlascl_("G", &c__0, &c__0, &bnrm, &smlnum, m, nrhs, &b[b_offset], ldb,
  279. info);
  280. ibscl = 1;
  281. } else if (bnrm > bignum) {
  282. /* Scale matrix norm down to BIGNUM */
  283. _starpu_dlascl_("G", &c__0, &c__0, &bnrm, &bignum, m, nrhs, &b[b_offset], ldb,
  284. info);
  285. ibscl = 2;
  286. }
  287. /* Compute QR factorization with column pivoting of A: */
  288. /* A * P = Q * R */
  289. i__1 = *lwork - mn;
  290. _starpu_dgeqp3_(m, n, &a[a_offset], lda, &jpvt[1], &work[1], &work[mn + 1], &i__1,
  291. info);
  292. wsize = mn + work[mn + 1];
  293. /* workspace: MN+2*N+NB*(N+1). */
  294. /* Details of Householder rotations stored in WORK(1:MN). */
  295. /* Determine RANK using incremental condition estimation */
  296. work[ismin] = 1.;
  297. work[ismax] = 1.;
  298. smax = (d__1 = a[a_dim1 + 1], abs(d__1));
  299. smin = smax;
  300. if ((d__1 = a[a_dim1 + 1], abs(d__1)) == 0.) {
  301. *rank = 0;
  302. i__1 = max(*m,*n);
  303. _starpu_dlaset_("F", &i__1, nrhs, &c_b31, &c_b31, &b[b_offset], ldb);
  304. goto L70;
  305. } else {
  306. *rank = 1;
  307. }
  308. L10:
  309. if (*rank < mn) {
  310. i__ = *rank + 1;
  311. _starpu_dlaic1_(&c__2, rank, &work[ismin], &smin, &a[i__ * a_dim1 + 1], &a[
  312. i__ + i__ * a_dim1], &sminpr, &s1, &c1);
  313. _starpu_dlaic1_(&c__1, rank, &work[ismax], &smax, &a[i__ * a_dim1 + 1], &a[
  314. i__ + i__ * a_dim1], &smaxpr, &s2, &c2);
  315. if (smaxpr * *rcond <= sminpr) {
  316. i__1 = *rank;
  317. for (i__ = 1; i__ <= i__1; ++i__) {
  318. work[ismin + i__ - 1] = s1 * work[ismin + i__ - 1];
  319. work[ismax + i__ - 1] = s2 * work[ismax + i__ - 1];
  320. /* L20: */
  321. }
  322. work[ismin + *rank] = c1;
  323. work[ismax + *rank] = c2;
  324. smin = sminpr;
  325. smax = smaxpr;
  326. ++(*rank);
  327. goto L10;
  328. }
  329. }
  330. /* workspace: 3*MN. */
  331. /* Logically partition R = [ R11 R12 ] */
  332. /* [ 0 R22 ] */
  333. /* where R11 = R(1:RANK,1:RANK) */
  334. /* [R11,R12] = [ T11, 0 ] * Y */
  335. if (*rank < *n) {
  336. i__1 = *lwork - (mn << 1);
  337. _starpu_dtzrzf_(rank, n, &a[a_offset], lda, &work[mn + 1], &work[(mn << 1) +
  338. 1], &i__1, info);
  339. }
  340. /* workspace: 2*MN. */
  341. /* Details of Householder rotations stored in WORK(MN+1:2*MN) */
  342. /* B(1:M,1:NRHS) := Q' * B(1:M,1:NRHS) */
  343. i__1 = *lwork - (mn << 1);
  344. _starpu_dormqr_("Left", "Transpose", m, nrhs, &mn, &a[a_offset], lda, &work[1], &
  345. b[b_offset], ldb, &work[(mn << 1) + 1], &i__1, info);
  346. /* Computing MAX */
  347. d__1 = wsize, d__2 = (mn << 1) + work[(mn << 1) + 1];
  348. wsize = max(d__1,d__2);
  349. /* workspace: 2*MN+NB*NRHS. */
  350. /* B(1:RANK,1:NRHS) := inv(T11) * B(1:RANK,1:NRHS) */
  351. _starpu_dtrsm_("Left", "Upper", "No transpose", "Non-unit", rank, nrhs, &c_b54, &
  352. a[a_offset], lda, &b[b_offset], ldb);
  353. i__1 = *nrhs;
  354. for (j = 1; j <= i__1; ++j) {
  355. i__2 = *n;
  356. for (i__ = *rank + 1; i__ <= i__2; ++i__) {
  357. b[i__ + j * b_dim1] = 0.;
  358. /* L30: */
  359. }
  360. /* L40: */
  361. }
  362. /* B(1:N,1:NRHS) := Y' * B(1:N,1:NRHS) */
  363. if (*rank < *n) {
  364. i__1 = *n - *rank;
  365. i__2 = *lwork - (mn << 1);
  366. _starpu_dormrz_("Left", "Transpose", n, nrhs, rank, &i__1, &a[a_offset], lda,
  367. &work[mn + 1], &b[b_offset], ldb, &work[(mn << 1) + 1], &i__2,
  368. info);
  369. }
  370. /* workspace: 2*MN+NRHS. */
  371. /* B(1:N,1:NRHS) := P * B(1:N,1:NRHS) */
  372. i__1 = *nrhs;
  373. for (j = 1; j <= i__1; ++j) {
  374. i__2 = *n;
  375. for (i__ = 1; i__ <= i__2; ++i__) {
  376. work[jpvt[i__]] = b[i__ + j * b_dim1];
  377. /* L50: */
  378. }
  379. _starpu_dcopy_(n, &work[1], &c__1, &b[j * b_dim1 + 1], &c__1);
  380. /* L60: */
  381. }
  382. /* workspace: N. */
  383. /* Undo scaling */
  384. if (iascl == 1) {
  385. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &smlnum, n, nrhs, &b[b_offset], ldb,
  386. info);
  387. _starpu_dlascl_("U", &c__0, &c__0, &smlnum, &anrm, rank, rank, &a[a_offset],
  388. lda, info);
  389. } else if (iascl == 2) {
  390. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &bignum, n, nrhs, &b[b_offset], ldb,
  391. info);
  392. _starpu_dlascl_("U", &c__0, &c__0, &bignum, &anrm, rank, rank, &a[a_offset],
  393. lda, info);
  394. }
  395. if (ibscl == 1) {
  396. _starpu_dlascl_("G", &c__0, &c__0, &smlnum, &bnrm, n, nrhs, &b[b_offset], ldb,
  397. info);
  398. } else if (ibscl == 2) {
  399. _starpu_dlascl_("G", &c__0, &c__0, &bignum, &bnrm, n, nrhs, &b[b_offset], ldb,
  400. info);
  401. }
  402. L70:
  403. work[1] = (doublereal) lwkopt;
  404. return 0;
  405. /* End of DGELSY */
  406. } /* _starpu_dgelsy_ */