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


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:56:08