dggglm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* dggglm.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_b32 = -1.;
  17. static doublereal c_b34 = 1.;
  18. /* Subroutine */ int _starpu_dggglm_(integer *n, integer *m, integer *p, doublereal *
  19. a, integer *lda, doublereal *b, integer *ldb, doublereal *d__,
  20. doublereal *x, doublereal *y, doublereal *work, integer *lwork,
  21. integer *info)
  22. {
  23. /* System generated locals */
  24. integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2, i__3, i__4;
  25. /* Local variables */
  26. integer i__, nb, np, nb1, nb2, nb3, nb4, lopt;
  27. extern /* Subroutine */ int _starpu_dgemv_(char *, integer *, integer *,
  28. doublereal *, doublereal *, integer *, doublereal *, integer *,
  29. doublereal *, doublereal *, integer *), _starpu_dcopy_(integer *,
  30. doublereal *, integer *, doublereal *, integer *), _starpu_dggqrf_(
  31. integer *, integer *, integer *, doublereal *, integer *,
  32. doublereal *, doublereal *, integer *, doublereal *, doublereal *,
  33. integer *, integer *), _starpu_xerbla_(char *, integer *);
  34. extern integer _starpu_ilaenv_(integer *, char *, char *, integer *, integer *,
  35. integer *, integer *);
  36. integer lwkmin;
  37. extern /* Subroutine */ int _starpu_dormqr_(char *, char *, integer *, integer *,
  38. integer *, doublereal *, integer *, doublereal *, doublereal *,
  39. integer *, doublereal *, integer *, integer *),
  40. _starpu_dormrq_(char *, char *, integer *, integer *, integer *,
  41. doublereal *, integer *, doublereal *, doublereal *, integer *,
  42. doublereal *, integer *, integer *);
  43. integer lwkopt;
  44. logical lquery;
  45. extern /* Subroutine */ int _starpu_dtrtrs_(char *, char *, char *, integer *,
  46. integer *, doublereal *, integer *, doublereal *, integer *,
  47. integer *);
  48. /* -- LAPACK driver routine (version 3.2) -- */
  49. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  50. /* November 2006 */
  51. /* .. Scalar Arguments .. */
  52. /* .. */
  53. /* .. Array Arguments .. */
  54. /* .. */
  55. /* Purpose */
  56. /* ======= */
  57. /* DGGGLM solves a general Gauss-Markov linear model (GLM) problem: */
  58. /* minimize || y ||_2 subject to d = A*x + B*y */
  59. /* x */
  60. /* where A is an N-by-M matrix, B is an N-by-P matrix, and d is a */
  61. /* given N-vector. It is assumed that M <= N <= M+P, and */
  62. /* rank(A) = M and rank( A B ) = N. */
  63. /* Under these assumptions, the constrained equation is always */
  64. /* consistent, and there is a unique solution x and a minimal 2-norm */
  65. /* solution y, which is obtained using a generalized QR factorization */
  66. /* of the matrices (A, B) given by */
  67. /* A = Q*(R), B = Q*T*Z. */
  68. /* (0) */
  69. /* In particular, if matrix B is square nonsingular, then the problem */
  70. /* GLM is equivalent to the following weighted linear least squares */
  71. /* problem */
  72. /* minimize || inv(B)*(d-A*x) ||_2 */
  73. /* x */
  74. /* where inv(B) denotes the inverse of B. */
  75. /* Arguments */
  76. /* ========= */
  77. /* N (input) INTEGER */
  78. /* The number of rows of the matrices A and B. N >= 0. */
  79. /* M (input) INTEGER */
  80. /* The number of columns of the matrix A. 0 <= M <= N. */
  81. /* P (input) INTEGER */
  82. /* The number of columns of the matrix B. P >= N-M. */
  83. /* A (input/output) DOUBLE PRECISION array, dimension (LDA,M) */
  84. /* On entry, the N-by-M matrix A. */
  85. /* On exit, the upper triangular part of the array A contains */
  86. /* the M-by-M upper triangular matrix R. */
  87. /* LDA (input) INTEGER */
  88. /* The leading dimension of the array A. LDA >= max(1,N). */
  89. /* B (input/output) DOUBLE PRECISION array, dimension (LDB,P) */
  90. /* On entry, the N-by-P matrix B. */
  91. /* On exit, if N <= P, the upper triangle of the subarray */
  92. /* B(1:N,P-N+1:P) contains the N-by-N upper triangular matrix T; */
  93. /* if N > P, the elements on and above the (N-P)th subdiagonal */
  94. /* contain the N-by-P upper trapezoidal matrix T. */
  95. /* LDB (input) INTEGER */
  96. /* The leading dimension of the array B. LDB >= max(1,N). */
  97. /* D (input/output) DOUBLE PRECISION array, dimension (N) */
  98. /* On entry, D is the left hand side of the GLM equation. */
  99. /* On exit, D is destroyed. */
  100. /* X (output) DOUBLE PRECISION array, dimension (M) */
  101. /* Y (output) DOUBLE PRECISION array, dimension (P) */
  102. /* On exit, X and Y are the solutions of the GLM problem. */
  103. /* WORK (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
  104. /* On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  105. /* LWORK (input) INTEGER */
  106. /* The dimension of the array WORK. LWORK >= max(1,N+M+P). */
  107. /* For optimum performance, LWORK >= M+min(N,P)+max(N,P)*NB, */
  108. /* where NB is an upper bound for the optimal blocksizes for */
  109. /* DGEQRF, SGERQF, DORMQR and SORMRQ. */
  110. /* If LWORK = -1, then a workspace query is assumed; the routine */
  111. /* only calculates the optimal size of the WORK array, returns */
  112. /* this value as the first entry of the WORK array, and no error */
  113. /* message related to LWORK is issued by XERBLA. */
  114. /* INFO (output) INTEGER */
  115. /* = 0: successful exit. */
  116. /* < 0: if INFO = -i, the i-th argument had an illegal value. */
  117. /* = 1: the upper triangular factor R associated with A in the */
  118. /* generalized QR factorization of the pair (A, B) is */
  119. /* singular, so that rank(A) < M; the least squares */
  120. /* solution could not be computed. */
  121. /* = 2: the bottom (N-M) by (N-M) part of the upper trapezoidal */
  122. /* factor T associated with B in the generalized QR */
  123. /* factorization of the pair (A, B) is singular, so that */
  124. /* rank( A B ) < N; the least squares solution could not */
  125. /* be computed. */
  126. /* =================================================================== */
  127. /* .. Parameters .. */
  128. /* .. */
  129. /* .. Local Scalars .. */
  130. /* .. */
  131. /* .. External Subroutines .. */
  132. /* .. */
  133. /* .. External Functions .. */
  134. /* .. */
  135. /* .. Intrinsic Functions .. */
  136. /* .. */
  137. /* .. Executable Statements .. */
  138. /* Test the input parameters */
  139. /* Parameter adjustments */
  140. a_dim1 = *lda;
  141. a_offset = 1 + a_dim1;
  142. a -= a_offset;
  143. b_dim1 = *ldb;
  144. b_offset = 1 + b_dim1;
  145. b -= b_offset;
  146. --d__;
  147. --x;
  148. --y;
  149. --work;
  150. /* Function Body */
  151. *info = 0;
  152. np = min(*n,*p);
  153. lquery = *lwork == -1;
  154. if (*n < 0) {
  155. *info = -1;
  156. } else if (*m < 0 || *m > *n) {
  157. *info = -2;
  158. } else if (*p < 0 || *p < *n - *m) {
  159. *info = -3;
  160. } else if (*lda < max(1,*n)) {
  161. *info = -5;
  162. } else if (*ldb < max(1,*n)) {
  163. *info = -7;
  164. }
  165. /* Calculate workspace */
  166. if (*info == 0) {
  167. if (*n == 0) {
  168. lwkmin = 1;
  169. lwkopt = 1;
  170. } else {
  171. nb1 = _starpu_ilaenv_(&c__1, "DGEQRF", " ", n, m, &c_n1, &c_n1);
  172. nb2 = _starpu_ilaenv_(&c__1, "DGERQF", " ", n, m, &c_n1, &c_n1);
  173. nb3 = _starpu_ilaenv_(&c__1, "DORMQR", " ", n, m, p, &c_n1);
  174. nb4 = _starpu_ilaenv_(&c__1, "DORMRQ", " ", n, m, p, &c_n1);
  175. /* Computing MAX */
  176. i__1 = max(nb1,nb2), i__1 = max(i__1,nb3);
  177. nb = max(i__1,nb4);
  178. lwkmin = *m + *n + *p;
  179. lwkopt = *m + np + max(*n,*p) * nb;
  180. }
  181. work[1] = (doublereal) lwkopt;
  182. if (*lwork < lwkmin && ! lquery) {
  183. *info = -12;
  184. }
  185. }
  186. if (*info != 0) {
  187. i__1 = -(*info);
  188. _starpu_xerbla_("DGGGLM", &i__1);
  189. return 0;
  190. } else if (lquery) {
  191. return 0;
  192. }
  193. /* Quick return if possible */
  194. if (*n == 0) {
  195. return 0;
  196. }
  197. /* Compute the GQR factorization of matrices A and B: */
  198. /* Q'*A = ( R11 ) M, Q'*B*Z' = ( T11 T12 ) M */
  199. /* ( 0 ) N-M ( 0 T22 ) N-M */
  200. /* M M+P-N N-M */
  201. /* where R11 and T22 are upper triangular, and Q and Z are */
  202. /* orthogonal. */
  203. i__1 = *lwork - *m - np;
  204. _starpu_dggqrf_(n, m, p, &a[a_offset], lda, &work[1], &b[b_offset], ldb, &work[*m
  205. + 1], &work[*m + np + 1], &i__1, info);
  206. lopt = (integer) work[*m + np + 1];
  207. /* Update left-hand-side vector d = Q'*d = ( d1 ) M */
  208. /* ( d2 ) N-M */
  209. i__1 = max(1,*n);
  210. i__2 = *lwork - *m - np;
  211. _starpu_dormqr_("Left", "Transpose", n, &c__1, m, &a[a_offset], lda, &work[1], &
  212. d__[1], &i__1, &work[*m + np + 1], &i__2, info);
  213. /* Computing MAX */
  214. i__1 = lopt, i__2 = (integer) work[*m + np + 1];
  215. lopt = max(i__1,i__2);
  216. /* Solve T22*y2 = d2 for y2 */
  217. if (*n > *m) {
  218. i__1 = *n - *m;
  219. i__2 = *n - *m;
  220. _starpu_dtrtrs_("Upper", "No transpose", "Non unit", &i__1, &c__1, &b[*m + 1
  221. + (*m + *p - *n + 1) * b_dim1], ldb, &d__[*m + 1], &i__2,
  222. info);
  223. if (*info > 0) {
  224. *info = 1;
  225. return 0;
  226. }
  227. i__1 = *n - *m;
  228. _starpu_dcopy_(&i__1, &d__[*m + 1], &c__1, &y[*m + *p - *n + 1], &c__1);
  229. }
  230. /* Set y1 = 0 */
  231. i__1 = *m + *p - *n;
  232. for (i__ = 1; i__ <= i__1; ++i__) {
  233. y[i__] = 0.;
  234. /* L10: */
  235. }
  236. /* Update d1 = d1 - T12*y2 */
  237. i__1 = *n - *m;
  238. _starpu_dgemv_("No transpose", m, &i__1, &c_b32, &b[(*m + *p - *n + 1) * b_dim1 +
  239. 1], ldb, &y[*m + *p - *n + 1], &c__1, &c_b34, &d__[1], &c__1);
  240. /* Solve triangular system: R11*x = d1 */
  241. if (*m > 0) {
  242. _starpu_dtrtrs_("Upper", "No Transpose", "Non unit", m, &c__1, &a[a_offset],
  243. lda, &d__[1], m, info);
  244. if (*info > 0) {
  245. *info = 2;
  246. return 0;
  247. }
  248. /* Copy D to X */
  249. _starpu_dcopy_(m, &d__[1], &c__1, &x[1], &c__1);
  250. }
  251. /* Backward transformation y = Z'*y */
  252. /* Computing MAX */
  253. i__1 = 1, i__2 = *n - *p + 1;
  254. i__3 = max(1,*p);
  255. i__4 = *lwork - *m - np;
  256. _starpu_dormrq_("Left", "Transpose", p, &c__1, &np, &b[max(i__1, i__2)+ b_dim1],
  257. ldb, &work[*m + 1], &y[1], &i__3, &work[*m + np + 1], &i__4, info);
  258. /* Computing MAX */
  259. i__1 = lopt, i__2 = (integer) work[*m + np + 1];
  260. work[1] = (doublereal) (*m + np + max(i__1,i__2));
  261. return 0;
  262. /* End of DGGGLM */
  263. } /* _starpu_dggglm_ */