dgglse.c
Go to the documentation of this file.
00001 /* dgglse.f -- translated by f2c (version 20061008).
00002    You must link the resulting object file with libf2c:
00003         on Microsoft Windows system, link with libf2c.lib;
00004         on Linux or Unix systems, link with .../path/to/libf2c.a -lm
00005         or, if you install libf2c.a in a standard place, with -lf2c -lm
00006         -- in that order, at the end of the command line, as in
00007                 cc *.o -lf2c -lm
00008         Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
00009 
00010                 http://www.netlib.org/f2c/libf2c.zip
00011 */
00012 
00013 #include "f2c.h"
00014 #include "blaswrap.h"
00015 
00016 /* Table of constant values */
00017 
00018 static integer c__1 = 1;
00019 static integer c_n1 = -1;
00020 static doublereal c_b31 = -1.;
00021 static doublereal c_b33 = 1.;
00022 
00023 /* Subroutine */ int dgglse_(integer *m, integer *n, integer *p, doublereal *
00024         a, integer *lda, doublereal *b, integer *ldb, doublereal *c__, 
00025         doublereal *d__, doublereal *x, doublereal *work, integer *lwork, 
00026         integer *info)
00027 {
00028     /* System generated locals */
00029     integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2;
00030 
00031     /* Local variables */
00032     integer nb, mn, nr, nb1, nb2, nb3, nb4, lopt;
00033     extern /* Subroutine */ int dgemv_(char *, integer *, integer *, 
00034             doublereal *, doublereal *, integer *, doublereal *, integer *, 
00035             doublereal *, doublereal *, integer *), dcopy_(integer *, 
00036             doublereal *, integer *, doublereal *, integer *), daxpy_(integer 
00037             *, doublereal *, doublereal *, integer *, doublereal *, integer *)
00038             , dtrmv_(char *, char *, char *, integer *, doublereal *, integer 
00039             *, doublereal *, integer *), dggrqf_(
00040             integer *, integer *, integer *, doublereal *, integer *, 
00041             doublereal *, doublereal *, integer *, doublereal *, doublereal *, 
00042              integer *, integer *), xerbla_(char *, integer *);
00043     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
00044             integer *, integer *);
00045     integer lwkmin;
00046     extern /* Subroutine */ int dormqr_(char *, char *, integer *, integer *, 
00047             integer *, doublereal *, integer *, doublereal *, doublereal *, 
00048             integer *, doublereal *, integer *, integer *), 
00049             dormrq_(char *, char *, integer *, integer *, integer *, 
00050             doublereal *, integer *, doublereal *, doublereal *, integer *, 
00051             doublereal *, integer *, integer *);
00052     integer lwkopt;
00053     logical lquery;
00054     extern /* Subroutine */ int dtrtrs_(char *, char *, char *, integer *, 
00055             integer *, doublereal *, integer *, doublereal *, integer *, 
00056             integer *);
00057 
00058 
00059 /*  -- LAPACK driver routine (version 3.2) -- */
00060 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00061 /*     November 2006 */
00062 
00063 /*     .. Scalar Arguments .. */
00064 /*     .. */
00065 /*     .. Array Arguments .. */
00066 /*     .. */
00067 
00068 /*  Purpose */
00069 /*  ======= */
00070 
00071 /*  DGGLSE solves the linear equality-constrained least squares (LSE) */
00072 /*  problem: */
00073 
00074 /*          minimize || c - A*x ||_2   subject to   B*x = d */
00075 
00076 /*  where A is an M-by-N matrix, B is a P-by-N matrix, c is a given */
00077 /*  M-vector, and d is a given P-vector. It is assumed that */
00078 /*  P <= N <= M+P, and */
00079 
00080 /*           rank(B) = P and  rank( (A) ) = N. */
00081 /*                                ( (B) ) */
00082 
00083 /*  These conditions ensure that the LSE problem has a unique solution, */
00084 /*  which is obtained using a generalized RQ factorization of the */
00085 /*  matrices (B, A) given by */
00086 
00087 /*     B = (0 R)*Q,   A = Z*T*Q. */
00088 
00089 /*  Arguments */
00090 /*  ========= */
00091 
00092 /*  M       (input) INTEGER */
00093 /*          The number of rows of the matrix A.  M >= 0. */
00094 
00095 /*  N       (input) INTEGER */
00096 /*          The number of columns of the matrices A and B. N >= 0. */
00097 
00098 /*  P       (input) INTEGER */
00099 /*          The number of rows of the matrix B. 0 <= P <= N <= M+P. */
00100 
00101 /*  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
00102 /*          On entry, the M-by-N matrix A. */
00103 /*          On exit, the elements on and above the diagonal of the array */
00104 /*          contain the min(M,N)-by-N upper trapezoidal matrix T. */
00105 
00106 /*  LDA     (input) INTEGER */
00107 /*          The leading dimension of the array A. LDA >= max(1,M). */
00108 
00109 /*  B       (input/output) DOUBLE PRECISION array, dimension (LDB,N) */
00110 /*          On entry, the P-by-N matrix B. */
00111 /*          On exit, the upper triangle of the subarray B(1:P,N-P+1:N) */
00112 /*          contains the P-by-P upper triangular matrix R. */
00113 
00114 /*  LDB     (input) INTEGER */
00115 /*          The leading dimension of the array B. LDB >= max(1,P). */
00116 
00117 /*  C       (input/output) DOUBLE PRECISION array, dimension (M) */
00118 /*          On entry, C contains the right hand side vector for the */
00119 /*          least squares part of the LSE problem. */
00120 /*          On exit, the residual sum of squares for the solution */
00121 /*          is given by the sum of squares of elements N-P+1 to M of */
00122 /*          vector C. */
00123 
00124 /*  D       (input/output) DOUBLE PRECISION array, dimension (P) */
00125 /*          On entry, D contains the right hand side vector for the */
00126 /*          constrained equation. */
00127 /*          On exit, D is destroyed. */
00128 
00129 /*  X       (output) DOUBLE PRECISION array, dimension (N) */
00130 /*          On exit, X is the solution of the LSE problem. */
00131 
00132 /*  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
00133 /*          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
00134 
00135 /*  LWORK   (input) INTEGER */
00136 /*          The dimension of the array WORK. LWORK >= max(1,M+N+P). */
00137 /*          For optimum performance LWORK >= P+min(M,N)+max(M,N)*NB, */
00138 /*          where NB is an upper bound for the optimal blocksizes for */
00139 /*          DGEQRF, SGERQF, DORMQR and SORMRQ. */
00140 
00141 /*          If LWORK = -1, then a workspace query is assumed; the routine */
00142 /*          only calculates the optimal size of the WORK array, returns */
00143 /*          this value as the first entry of the WORK array, and no error */
00144 /*          message related to LWORK is issued by XERBLA. */
00145 
00146 /*  INFO    (output) INTEGER */
00147 /*          = 0:  successful exit. */
00148 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
00149 /*          = 1:  the upper triangular factor R associated with B in the */
00150 /*                generalized RQ factorization of the pair (B, A) is */
00151 /*                singular, so that rank(B) < P; the least squares */
00152 /*                solution could not be computed. */
00153 /*          = 2:  the (N-P) by (N-P) part of the upper trapezoidal factor */
00154 /*                T associated with A in the generalized RQ factorization */
00155 /*                of the pair (B, A) is singular, so that */
00156 /*                rank( (A) ) < N; the least squares solution could not */
00157 /*                    ( (B) ) */
00158 /*                be computed. */
00159 
00160 /*  ===================================================================== */
00161 
00162 /*     .. Parameters .. */
00163 /*     .. */
00164 /*     .. Local Scalars .. */
00165 /*     .. */
00166 /*     .. External Subroutines .. */
00167 /*     .. */
00168 /*     .. External Functions .. */
00169 /*     .. */
00170 /*     .. Intrinsic Functions .. */
00171 /*     .. */
00172 /*     .. Executable Statements .. */
00173 
00174 /*     Test the input parameters */
00175 
00176     /* Parameter adjustments */
00177     a_dim1 = *lda;
00178     a_offset = 1 + a_dim1;
00179     a -= a_offset;
00180     b_dim1 = *ldb;
00181     b_offset = 1 + b_dim1;
00182     b -= b_offset;
00183     --c__;
00184     --d__;
00185     --x;
00186     --work;
00187 
00188     /* Function Body */
00189     *info = 0;
00190     mn = min(*m,*n);
00191     lquery = *lwork == -1;
00192     if (*m < 0) {
00193         *info = -1;
00194     } else if (*n < 0) {
00195         *info = -2;
00196     } else if (*p < 0 || *p > *n || *p < *n - *m) {
00197         *info = -3;
00198     } else if (*lda < max(1,*m)) {
00199         *info = -5;
00200     } else if (*ldb < max(1,*p)) {
00201         *info = -7;
00202     }
00203 
00204 /*     Calculate workspace */
00205 
00206     if (*info == 0) {
00207         if (*n == 0) {
00208             lwkmin = 1;
00209             lwkopt = 1;
00210         } else {
00211             nb1 = ilaenv_(&c__1, "DGEQRF", " ", m, n, &c_n1, &c_n1);
00212             nb2 = ilaenv_(&c__1, "DGERQF", " ", m, n, &c_n1, &c_n1);
00213             nb3 = ilaenv_(&c__1, "DORMQR", " ", m, n, p, &c_n1);
00214             nb4 = ilaenv_(&c__1, "DORMRQ", " ", m, n, p, &c_n1);
00215 /* Computing MAX */
00216             i__1 = max(nb1,nb2), i__1 = max(i__1,nb3);
00217             nb = max(i__1,nb4);
00218             lwkmin = *m + *n + *p;
00219             lwkopt = *p + mn + max(*m,*n) * nb;
00220         }
00221         work[1] = (doublereal) lwkopt;
00222 
00223         if (*lwork < lwkmin && ! lquery) {
00224             *info = -12;
00225         }
00226     }
00227 
00228     if (*info != 0) {
00229         i__1 = -(*info);
00230         xerbla_("DGGLSE", &i__1);
00231         return 0;
00232     } else if (lquery) {
00233         return 0;
00234     }
00235 
00236 /*     Quick return if possible */
00237 
00238     if (*n == 0) {
00239         return 0;
00240     }
00241 
00242 /*     Compute the GRQ factorization of matrices B and A: */
00243 
00244 /*            B*Q' = (  0  T12 ) P   Z'*A*Q' = ( R11 R12 ) N-P */
00245 /*                     N-P  P                  (  0  R22 ) M+P-N */
00246 /*                                               N-P  P */
00247 
00248 /*     where T12 and R11 are upper triangular, and Q and Z are */
00249 /*     orthogonal. */
00250 
00251     i__1 = *lwork - *p - mn;
00252     dggrqf_(p, m, n, &b[b_offset], ldb, &work[1], &a[a_offset], lda, &work[*p 
00253             + 1], &work[*p + mn + 1], &i__1, info);
00254     lopt = (integer) work[*p + mn + 1];
00255 
00256 /*     Update c = Z'*c = ( c1 ) N-P */
00257 /*                       ( c2 ) M+P-N */
00258 
00259     i__1 = max(1,*m);
00260     i__2 = *lwork - *p - mn;
00261     dormqr_("Left", "Transpose", m, &c__1, &mn, &a[a_offset], lda, &work[*p + 
00262             1], &c__[1], &i__1, &work[*p + mn + 1], &i__2, info);
00263 /* Computing MAX */
00264     i__1 = lopt, i__2 = (integer) work[*p + mn + 1];
00265     lopt = max(i__1,i__2);
00266 
00267 /*     Solve T12*x2 = d for x2 */
00268 
00269     if (*p > 0) {
00270         dtrtrs_("Upper", "No transpose", "Non-unit", p, &c__1, &b[(*n - *p + 
00271                 1) * b_dim1 + 1], ldb, &d__[1], p, info);
00272 
00273         if (*info > 0) {
00274             *info = 1;
00275             return 0;
00276         }
00277 
00278 /*        Put the solution in X */
00279 
00280         dcopy_(p, &d__[1], &c__1, &x[*n - *p + 1], &c__1);
00281 
00282 /*        Update c1 */
00283 
00284         i__1 = *n - *p;
00285         dgemv_("No transpose", &i__1, p, &c_b31, &a[(*n - *p + 1) * a_dim1 + 
00286                 1], lda, &d__[1], &c__1, &c_b33, &c__[1], &c__1);
00287     }
00288 
00289 /*     Solve R11*x1 = c1 for x1 */
00290 
00291     if (*n > *p) {
00292         i__1 = *n - *p;
00293         i__2 = *n - *p;
00294         dtrtrs_("Upper", "No transpose", "Non-unit", &i__1, &c__1, &a[
00295                 a_offset], lda, &c__[1], &i__2, info);
00296 
00297         if (*info > 0) {
00298             *info = 2;
00299             return 0;
00300         }
00301 
00302 /*        Put the solutions in X */
00303 
00304         i__1 = *n - *p;
00305         dcopy_(&i__1, &c__[1], &c__1, &x[1], &c__1);
00306     }
00307 
00308 /*     Compute the residual vector: */
00309 
00310     if (*m < *n) {
00311         nr = *m + *p - *n;
00312         if (nr > 0) {
00313             i__1 = *n - *m;
00314             dgemv_("No transpose", &nr, &i__1, &c_b31, &a[*n - *p + 1 + (*m + 
00315                     1) * a_dim1], lda, &d__[nr + 1], &c__1, &c_b33, &c__[*n - 
00316                     *p + 1], &c__1);
00317         }
00318     } else {
00319         nr = *p;
00320     }
00321     if (nr > 0) {
00322         dtrmv_("Upper", "No transpose", "Non unit", &nr, &a[*n - *p + 1 + (*n 
00323                 - *p + 1) * a_dim1], lda, &d__[1], &c__1);
00324         daxpy_(&nr, &c_b31, &d__[1], &c__1, &c__[*n - *p + 1], &c__1);
00325     }
00326 
00327 /*     Backward transformation x = Q'*x */
00328 
00329     i__1 = *lwork - *p - mn;
00330     dormrq_("Left", "Transpose", n, &c__1, p, &b[b_offset], ldb, &work[1], &x[
00331             1], n, &work[*p + mn + 1], &i__1, info);
00332 /* Computing MAX */
00333     i__1 = lopt, i__2 = (integer) work[*p + mn + 1];
00334     work[1] = (doublereal) (*p + mn + max(i__1,i__2));
00335 
00336     return 0;
00337 
00338 /*     End of DGGLSE */
00339 
00340 } /* dgglse_ */


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:55:45