dgglse.c 11 KB

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