dgels.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* dgels.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 doublereal c_b33 = 0.;
  17. static integer c__0 = 0;
  18. /* Subroutine */ int _starpu_dgels_(char *trans, integer *m, integer *n, integer *
  19. nrhs, doublereal *a, integer *lda, doublereal *b, integer *ldb,
  20. doublereal *work, integer *lwork, integer *info)
  21. {
  22. /* System generated locals */
  23. integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2;
  24. /* Local variables */
  25. integer i__, j, nb, mn;
  26. doublereal anrm, bnrm;
  27. integer brow;
  28. logical tpsd;
  29. integer iascl, ibscl;
  30. extern logical _starpu_lsame_(char *, char *);
  31. integer wsize;
  32. doublereal rwork[1];
  33. extern /* Subroutine */ int _starpu_dlabad_(doublereal *, doublereal *);
  34. extern doublereal _starpu_dlamch_(char *), _starpu_dlange_(char *, integer *,
  35. integer *, doublereal *, integer *, doublereal *);
  36. extern /* Subroutine */ int _starpu_dgelqf_(integer *, integer *, doublereal *,
  37. integer *, doublereal *, doublereal *, integer *, integer *),
  38. _starpu_dlascl_(char *, integer *, integer *, doublereal *, doublereal *,
  39. integer *, integer *, doublereal *, integer *, integer *),
  40. _starpu_dgeqrf_(integer *, integer *, doublereal *, integer *,
  41. doublereal *, doublereal *, integer *, integer *), _starpu_dlaset_(char *,
  42. integer *, integer *, doublereal *, doublereal *, doublereal *,
  43. integer *), _starpu_xerbla_(char *, integer *);
  44. extern integer _starpu_ilaenv_(integer *, char *, char *, integer *, integer *,
  45. integer *, integer *);
  46. integer scllen;
  47. doublereal bignum;
  48. extern /* Subroutine */ int _starpu_dormlq_(char *, char *, integer *, integer *,
  49. integer *, doublereal *, integer *, doublereal *, doublereal *,
  50. integer *, doublereal *, integer *, integer *),
  51. _starpu_dormqr_(char *, char *, integer *, integer *, integer *,
  52. doublereal *, integer *, doublereal *, doublereal *, integer *,
  53. doublereal *, integer *, integer *);
  54. doublereal smlnum;
  55. logical lquery;
  56. extern /* Subroutine */ int _starpu_dtrtrs_(char *, char *, char *, integer *,
  57. integer *, doublereal *, integer *, doublereal *, integer *,
  58. integer *);
  59. /* -- LAPACK driver routine (version 3.2) -- */
  60. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  61. /* November 2006 */
  62. /* .. Scalar Arguments .. */
  63. /* .. */
  64. /* .. Array Arguments .. */
  65. /* .. */
  66. /* Purpose */
  67. /* ======= */
  68. /* DGELS solves overdetermined or underdetermined real linear systems */
  69. /* involving an M-by-N matrix A, or its transpose, using a QR or LQ */
  70. /* factorization of A. It is assumed that A has full rank. */
  71. /* The following options are provided: */
  72. /* 1. If TRANS = 'N' and m >= n: find the least squares solution of */
  73. /* an overdetermined system, i.e., solve the least squares problem */
  74. /* minimize || B - A*X ||. */
  75. /* 2. If TRANS = 'N' and m < n: find the minimum norm solution of */
  76. /* an underdetermined system A * X = B. */
  77. /* 3. If TRANS = 'T' and m >= n: find the minimum norm solution of */
  78. /* an undetermined system A**T * X = B. */
  79. /* 4. If TRANS = 'T' and m < n: find the least squares solution of */
  80. /* an overdetermined system, i.e., solve the least squares problem */
  81. /* minimize || B - A**T * X ||. */
  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. /* Arguments */
  87. /* ========= */
  88. /* TRANS (input) CHARACTER*1 */
  89. /* = 'N': the linear system involves A; */
  90. /* = 'T': the linear system involves A**T. */
  91. /* M (input) INTEGER */
  92. /* The number of rows of the matrix A. M >= 0. */
  93. /* N (input) INTEGER */
  94. /* The number of columns of the matrix A. N >= 0. */
  95. /* NRHS (input) INTEGER */
  96. /* The number of right hand sides, i.e., the number of */
  97. /* columns of the matrices B and X. NRHS >=0. */
  98. /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
  99. /* On entry, the M-by-N matrix A. */
  100. /* On exit, */
  101. /* if M >= N, A is overwritten by details of its QR */
  102. /* factorization as returned by DGEQRF; */
  103. /* if M < N, A is overwritten by details of its LQ */
  104. /* factorization as returned by DGELQF. */
  105. /* LDA (input) INTEGER */
  106. /* The leading dimension of the array A. LDA >= max(1,M). */
  107. /* B (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  108. /* On entry, the matrix B of right hand side vectors, stored */
  109. /* columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS */
  110. /* if TRANS = 'T'. */
  111. /* On exit, if INFO = 0, B is overwritten by the solution */
  112. /* vectors, stored columnwise: */
  113. /* if TRANS = 'N' and m >= n, rows 1 to n of B contain the least */
  114. /* squares solution vectors; the residual sum of squares for the */
  115. /* solution in each column is given by the sum of squares of */
  116. /* elements N+1 to M in that column; */
  117. /* if TRANS = 'N' and m < n, rows 1 to N of B contain the */
  118. /* minimum norm solution vectors; */
  119. /* if TRANS = 'T' and m >= n, rows 1 to M of B contain the */
  120. /* minimum norm solution vectors; */
  121. /* if TRANS = 'T' and m < n, rows 1 to M of B contain the */
  122. /* least squares solution vectors; the residual sum of squares */
  123. /* for the solution in each column is given by the sum of */
  124. /* squares of elements M+1 to N in that column. */
  125. /* LDB (input) INTEGER */
  126. /* The leading dimension of the array B. LDB >= MAX(1,M,N). */
  127. /* WORK (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
  128. /* On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  129. /* LWORK (input) INTEGER */
  130. /* The dimension of the array WORK. */
  131. /* LWORK >= max( 1, MN + max( MN, NRHS ) ). */
  132. /* For optimal performance, */
  133. /* LWORK >= max( 1, MN + max( MN, NRHS )*NB ). */
  134. /* where MN = min(M,N) and NB is the optimum block size. */
  135. /* If LWORK = -1, then a workspace query is assumed; the routine */
  136. /* only calculates the optimal size of the WORK array, returns */
  137. /* this value as the first entry of the WORK array, and no error */
  138. /* message related to LWORK is issued by XERBLA. */
  139. /* INFO (output) INTEGER */
  140. /* = 0: successful exit */
  141. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  142. /* > 0: if INFO = i, the i-th diagonal element of the */
  143. /* triangular factor of A is zero, so that A does not have */
  144. /* full rank; the least squares solution could not be */
  145. /* computed. */
  146. /* ===================================================================== */
  147. /* .. Parameters .. */
  148. /* .. */
  149. /* .. Local Scalars .. */
  150. /* .. */
  151. /* .. Local Arrays .. */
  152. /* .. */
  153. /* .. External Functions .. */
  154. /* .. */
  155. /* .. External Subroutines .. */
  156. /* .. */
  157. /* .. Intrinsic Functions .. */
  158. /* .. */
  159. /* .. Executable Statements .. */
  160. /* Test the input arguments. */
  161. /* Parameter adjustments */
  162. a_dim1 = *lda;
  163. a_offset = 1 + a_dim1;
  164. a -= a_offset;
  165. b_dim1 = *ldb;
  166. b_offset = 1 + b_dim1;
  167. b -= b_offset;
  168. --work;
  169. /* Function Body */
  170. *info = 0;
  171. mn = min(*m,*n);
  172. lquery = *lwork == -1;
  173. if (! (_starpu_lsame_(trans, "N") || _starpu_lsame_(trans, "T"))) {
  174. *info = -1;
  175. } else if (*m < 0) {
  176. *info = -2;
  177. } else if (*n < 0) {
  178. *info = -3;
  179. } else if (*nrhs < 0) {
  180. *info = -4;
  181. } else if (*lda < max(1,*m)) {
  182. *info = -6;
  183. } else /* if(complicated condition) */ {
  184. /* Computing MAX */
  185. i__1 = max(1,*m);
  186. if (*ldb < max(i__1,*n)) {
  187. *info = -8;
  188. } else /* if(complicated condition) */ {
  189. /* Computing MAX */
  190. i__1 = 1, i__2 = mn + max(mn,*nrhs);
  191. if (*lwork < max(i__1,i__2) && ! lquery) {
  192. *info = -10;
  193. }
  194. }
  195. }
  196. /* Figure out optimal block size */
  197. if (*info == 0 || *info == -10) {
  198. tpsd = TRUE_;
  199. if (_starpu_lsame_(trans, "N")) {
  200. tpsd = FALSE_;
  201. }
  202. if (*m >= *n) {
  203. nb = _starpu_ilaenv_(&c__1, "DGEQRF", " ", m, n, &c_n1, &c_n1);
  204. if (tpsd) {
  205. /* Computing MAX */
  206. i__1 = nb, i__2 = _starpu_ilaenv_(&c__1, "DORMQR", "LN", m, nrhs, n, &
  207. c_n1);
  208. nb = max(i__1,i__2);
  209. } else {
  210. /* Computing MAX */
  211. i__1 = nb, i__2 = _starpu_ilaenv_(&c__1, "DORMQR", "LT", m, nrhs, n, &
  212. c_n1);
  213. nb = max(i__1,i__2);
  214. }
  215. } else {
  216. nb = _starpu_ilaenv_(&c__1, "DGELQF", " ", m, n, &c_n1, &c_n1);
  217. if (tpsd) {
  218. /* Computing MAX */
  219. i__1 = nb, i__2 = _starpu_ilaenv_(&c__1, "DORMLQ", "LT", n, nrhs, m, &
  220. c_n1);
  221. nb = max(i__1,i__2);
  222. } else {
  223. /* Computing MAX */
  224. i__1 = nb, i__2 = _starpu_ilaenv_(&c__1, "DORMLQ", "LN", n, nrhs, m, &
  225. c_n1);
  226. nb = max(i__1,i__2);
  227. }
  228. }
  229. /* Computing MAX */
  230. i__1 = 1, i__2 = mn + max(mn,*nrhs) * nb;
  231. wsize = max(i__1,i__2);
  232. work[1] = (doublereal) wsize;
  233. }
  234. if (*info != 0) {
  235. i__1 = -(*info);
  236. _starpu_xerbla_("DGELS ", &i__1);
  237. return 0;
  238. } else if (lquery) {
  239. return 0;
  240. }
  241. /* Quick return if possible */
  242. /* Computing MIN */
  243. i__1 = min(*m,*n);
  244. if (min(i__1,*nrhs) == 0) {
  245. i__1 = max(*m,*n);
  246. _starpu_dlaset_("Full", &i__1, nrhs, &c_b33, &c_b33, &b[b_offset], ldb);
  247. return 0;
  248. }
  249. /* Get machine parameters */
  250. smlnum = _starpu_dlamch_("S") / _starpu_dlamch_("P");
  251. bignum = 1. / smlnum;
  252. _starpu_dlabad_(&smlnum, &bignum);
  253. /* Scale A, B if max element outside range [SMLNUM,BIGNUM] */
  254. anrm = _starpu_dlange_("M", m, n, &a[a_offset], lda, rwork);
  255. iascl = 0;
  256. if (anrm > 0. && anrm < smlnum) {
  257. /* Scale matrix norm up to SMLNUM */
  258. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
  259. info);
  260. iascl = 1;
  261. } else if (anrm > bignum) {
  262. /* Scale matrix norm down to BIGNUM */
  263. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
  264. info);
  265. iascl = 2;
  266. } else if (anrm == 0.) {
  267. /* Matrix all zero. Return zero solution. */
  268. i__1 = max(*m,*n);
  269. _starpu_dlaset_("F", &i__1, nrhs, &c_b33, &c_b33, &b[b_offset], ldb);
  270. goto L50;
  271. }
  272. brow = *m;
  273. if (tpsd) {
  274. brow = *n;
  275. }
  276. bnrm = _starpu_dlange_("M", &brow, nrhs, &b[b_offset], ldb, rwork);
  277. ibscl = 0;
  278. if (bnrm > 0. && bnrm < smlnum) {
  279. /* Scale matrix norm up to SMLNUM */
  280. _starpu_dlascl_("G", &c__0, &c__0, &bnrm, &smlnum, &brow, nrhs, &b[b_offset],
  281. ldb, info);
  282. ibscl = 1;
  283. } else if (bnrm > bignum) {
  284. /* Scale matrix norm down to BIGNUM */
  285. _starpu_dlascl_("G", &c__0, &c__0, &bnrm, &bignum, &brow, nrhs, &b[b_offset],
  286. ldb, info);
  287. ibscl = 2;
  288. }
  289. if (*m >= *n) {
  290. /* compute QR factorization of A */
  291. i__1 = *lwork - mn;
  292. _starpu_dgeqrf_(m, n, &a[a_offset], lda, &work[1], &work[mn + 1], &i__1, info)
  293. ;
  294. /* workspace at least N, optimally N*NB */
  295. if (! tpsd) {
  296. /* Least-Squares Problem min || A * X - B || */
  297. /* B(1:M,1:NRHS) := Q' * B(1:M,1:NRHS) */
  298. i__1 = *lwork - mn;
  299. _starpu_dormqr_("Left", "Transpose", m, nrhs, n, &a[a_offset], lda, &work[
  300. 1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
  301. /* workspace at least NRHS, optimally NRHS*NB */
  302. /* B(1:N,1:NRHS) := inv(R) * B(1:N,1:NRHS) */
  303. _starpu_dtrtrs_("Upper", "No transpose", "Non-unit", n, nrhs, &a[a_offset]
  304. , lda, &b[b_offset], ldb, info);
  305. if (*info > 0) {
  306. return 0;
  307. }
  308. scllen = *n;
  309. } else {
  310. /* Overdetermined system of equations A' * X = B */
  311. /* B(1:N,1:NRHS) := inv(R') * B(1:N,1:NRHS) */
  312. _starpu_dtrtrs_("Upper", "Transpose", "Non-unit", n, nrhs, &a[a_offset],
  313. lda, &b[b_offset], ldb, info);
  314. if (*info > 0) {
  315. return 0;
  316. }
  317. /* B(N+1:M,1:NRHS) = ZERO */
  318. i__1 = *nrhs;
  319. for (j = 1; j <= i__1; ++j) {
  320. i__2 = *m;
  321. for (i__ = *n + 1; i__ <= i__2; ++i__) {
  322. b[i__ + j * b_dim1] = 0.;
  323. /* L10: */
  324. }
  325. /* L20: */
  326. }
  327. /* B(1:M,1:NRHS) := Q(1:N,:) * B(1:N,1:NRHS) */
  328. i__1 = *lwork - mn;
  329. _starpu_dormqr_("Left", "No transpose", m, nrhs, n, &a[a_offset], lda, &
  330. work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
  331. /* workspace at least NRHS, optimally NRHS*NB */
  332. scllen = *m;
  333. }
  334. } else {
  335. /* Compute LQ factorization of A */
  336. i__1 = *lwork - mn;
  337. _starpu_dgelqf_(m, n, &a[a_offset], lda, &work[1], &work[mn + 1], &i__1, info)
  338. ;
  339. /* workspace at least M, optimally M*NB. */
  340. if (! tpsd) {
  341. /* underdetermined system of equations A * X = B */
  342. /* B(1:M,1:NRHS) := inv(L) * B(1:M,1:NRHS) */
  343. _starpu_dtrtrs_("Lower", "No transpose", "Non-unit", m, nrhs, &a[a_offset]
  344. , lda, &b[b_offset], ldb, info);
  345. if (*info > 0) {
  346. return 0;
  347. }
  348. /* B(M+1:N,1:NRHS) = 0 */
  349. i__1 = *nrhs;
  350. for (j = 1; j <= i__1; ++j) {
  351. i__2 = *n;
  352. for (i__ = *m + 1; i__ <= i__2; ++i__) {
  353. b[i__ + j * b_dim1] = 0.;
  354. /* L30: */
  355. }
  356. /* L40: */
  357. }
  358. /* B(1:N,1:NRHS) := Q(1:N,:)' * B(1:M,1:NRHS) */
  359. i__1 = *lwork - mn;
  360. _starpu_dormlq_("Left", "Transpose", n, nrhs, m, &a[a_offset], lda, &work[
  361. 1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
  362. /* workspace at least NRHS, optimally NRHS*NB */
  363. scllen = *n;
  364. } else {
  365. /* overdetermined system min || A' * X - B || */
  366. /* B(1:N,1:NRHS) := Q * B(1:N,1:NRHS) */
  367. i__1 = *lwork - mn;
  368. _starpu_dormlq_("Left", "No transpose", n, nrhs, m, &a[a_offset], lda, &
  369. work[1], &b[b_offset], ldb, &work[mn + 1], &i__1, info);
  370. /* workspace at least NRHS, optimally NRHS*NB */
  371. /* B(1:M,1:NRHS) := inv(L') * B(1:M,1:NRHS) */
  372. _starpu_dtrtrs_("Lower", "Transpose", "Non-unit", m, nrhs, &a[a_offset],
  373. lda, &b[b_offset], ldb, info);
  374. if (*info > 0) {
  375. return 0;
  376. }
  377. scllen = *m;
  378. }
  379. }
  380. /* Undo scaling */
  381. if (iascl == 1) {
  382. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &smlnum, &scllen, nrhs, &b[b_offset]
  383. , ldb, info);
  384. } else if (iascl == 2) {
  385. _starpu_dlascl_("G", &c__0, &c__0, &anrm, &bignum, &scllen, nrhs, &b[b_offset]
  386. , ldb, info);
  387. }
  388. if (ibscl == 1) {
  389. _starpu_dlascl_("G", &c__0, &c__0, &smlnum, &bnrm, &scllen, nrhs, &b[b_offset]
  390. , ldb, info);
  391. } else if (ibscl == 2) {
  392. _starpu_dlascl_("G", &c__0, &c__0, &bignum, &bnrm, &scllen, nrhs, &b[b_offset]
  393. , ldb, info);
  394. }
  395. L50:
  396. work[1] = (doublereal) wsize;
  397. return 0;
  398. /* End of DGELS */
  399. } /* _starpu_dgels_ */