dggglm.c
Go to the documentation of this file.
00001 /* dggglm.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_b32 = -1.;
00021 static doublereal c_b34 = 1.;
00022 
00023 /* Subroutine */ int dggglm_(integer *n, integer *m, integer *p, doublereal *
00024         a, integer *lda, doublereal *b, integer *ldb, doublereal *d__, 
00025         doublereal *x, doublereal *y, 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, i__3, i__4;
00030 
00031     /* Local variables */
00032     integer i__, nb, np, 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 *), dggqrf_(
00037             integer *, integer *, integer *, doublereal *, integer *, 
00038             doublereal *, doublereal *, integer *, doublereal *, doublereal *, 
00039              integer *, integer *), xerbla_(char *, integer *);
00040     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
00041             integer *, integer *);
00042     integer lwkmin;
00043     extern /* Subroutine */ int dormqr_(char *, char *, integer *, integer *, 
00044             integer *, doublereal *, integer *, doublereal *, doublereal *, 
00045             integer *, doublereal *, integer *, integer *), 
00046             dormrq_(char *, char *, integer *, integer *, integer *, 
00047             doublereal *, integer *, doublereal *, doublereal *, integer *, 
00048             doublereal *, integer *, integer *);
00049     integer lwkopt;
00050     logical lquery;
00051     extern /* Subroutine */ int dtrtrs_(char *, char *, char *, integer *, 
00052             integer *, doublereal *, integer *, doublereal *, integer *, 
00053             integer *);
00054 
00055 
00056 /*  -- LAPACK driver routine (version 3.2) -- */
00057 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00058 /*     November 2006 */
00059 
00060 /*     .. Scalar Arguments .. */
00061 /*     .. */
00062 /*     .. Array Arguments .. */
00063 /*     .. */
00064 
00065 /*  Purpose */
00066 /*  ======= */
00067 
00068 /*  DGGGLM solves a general Gauss-Markov linear model (GLM) problem: */
00069 
00070 /*          minimize || y ||_2   subject to   d = A*x + B*y */
00071 /*              x */
00072 
00073 /*  where A is an N-by-M matrix, B is an N-by-P matrix, and d is a */
00074 /*  given N-vector. It is assumed that M <= N <= M+P, and */
00075 
00076 /*             rank(A) = M    and    rank( A B ) = N. */
00077 
00078 /*  Under these assumptions, the constrained equation is always */
00079 /*  consistent, and there is a unique solution x and a minimal 2-norm */
00080 /*  solution y, which is obtained using a generalized QR factorization */
00081 /*  of the matrices (A, B) given by */
00082 
00083 /*     A = Q*(R),   B = Q*T*Z. */
00084 /*           (0) */
00085 
00086 /*  In particular, if matrix B is square nonsingular, then the problem */
00087 /*  GLM is equivalent to the following weighted linear least squares */
00088 /*  problem */
00089 
00090 /*               minimize || inv(B)*(d-A*x) ||_2 */
00091 /*                   x */
00092 
00093 /*  where inv(B) denotes the inverse of B. */
00094 
00095 /*  Arguments */
00096 /*  ========= */
00097 
00098 /*  N       (input) INTEGER */
00099 /*          The number of rows of the matrices A and B.  N >= 0. */
00100 
00101 /*  M       (input) INTEGER */
00102 /*          The number of columns of the matrix A.  0 <= M <= N. */
00103 
00104 /*  P       (input) INTEGER */
00105 /*          The number of columns of the matrix B.  P >= N-M. */
00106 
00107 /*  A       (input/output) DOUBLE PRECISION array, dimension (LDA,M) */
00108 /*          On entry, the N-by-M matrix A. */
00109 /*          On exit, the upper triangular part of the array A contains */
00110 /*          the M-by-M upper triangular matrix R. */
00111 
00112 /*  LDA     (input) INTEGER */
00113 /*          The leading dimension of the array A. LDA >= max(1,N). */
00114 
00115 /*  B       (input/output) DOUBLE PRECISION array, dimension (LDB,P) */
00116 /*          On entry, the N-by-P matrix B. */
00117 /*          On exit, if N <= P, the upper triangle of the subarray */
00118 /*          B(1:N,P-N+1:P) contains the N-by-N upper triangular matrix T; */
00119 /*          if N > P, the elements on and above the (N-P)th subdiagonal */
00120 /*          contain the N-by-P upper trapezoidal matrix T. */
00121 
00122 /*  LDB     (input) INTEGER */
00123 /*          The leading dimension of the array B. LDB >= max(1,N). */
00124 
00125 /*  D       (input/output) DOUBLE PRECISION array, dimension (N) */
00126 /*          On entry, D is the left hand side of the GLM equation. */
00127 /*          On exit, D is destroyed. */
00128 
00129 /*  X       (output) DOUBLE PRECISION array, dimension (M) */
00130 /*  Y       (output) DOUBLE PRECISION array, dimension (P) */
00131 /*          On exit, X and Y are the solutions of the GLM problem. */
00132 
00133 /*  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
00134 /*          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
00135 
00136 /*  LWORK   (input) INTEGER */
00137 /*          The dimension of the array WORK. LWORK >= max(1,N+M+P). */
00138 /*          For optimum performance, LWORK >= M+min(N,P)+max(N,P)*NB, */
00139 /*          where NB is an upper bound for the optimal blocksizes for */
00140 /*          DGEQRF, SGERQF, DORMQR and SORMRQ. */
00141 
00142 /*          If LWORK = -1, then a workspace query is assumed; the routine */
00143 /*          only calculates the optimal size of the WORK array, returns */
00144 /*          this value as the first entry of the WORK array, and no error */
00145 /*          message related to LWORK is issued by XERBLA. */
00146 
00147 /*  INFO    (output) INTEGER */
00148 /*          = 0:  successful exit. */
00149 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
00150 /*          = 1:  the upper triangular factor R associated with A in the */
00151 /*                generalized QR factorization of the pair (A, B) is */
00152 /*                singular, so that rank(A) < M; the least squares */
00153 /*                solution could not be computed. */
00154 /*          = 2:  the bottom (N-M) by (N-M) part of the upper trapezoidal */
00155 /*                factor T associated with B in the generalized QR */
00156 /*                factorization of the pair (A, B) is singular, so that */
00157 /*                rank( A B ) < N; the least squares solution could not */
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     --d__;
00184     --x;
00185     --y;
00186     --work;
00187 
00188     /* Function Body */
00189     *info = 0;
00190     np = min(*n,*p);
00191     lquery = *lwork == -1;
00192     if (*n < 0) {
00193         *info = -1;
00194     } else if (*m < 0 || *m > *n) {
00195         *info = -2;
00196     } else if (*p < 0 || *p < *n - *m) {
00197         *info = -3;
00198     } else if (*lda < max(1,*n)) {
00199         *info = -5;
00200     } else if (*ldb < max(1,*n)) {
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", " ", n, m, &c_n1, &c_n1);
00212             nb2 = ilaenv_(&c__1, "DGERQF", " ", n, m, &c_n1, &c_n1);
00213             nb3 = ilaenv_(&c__1, "DORMQR", " ", n, m, p, &c_n1);
00214             nb4 = ilaenv_(&c__1, "DORMRQ", " ", n, m, 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 = *m + np + max(*n,*p) * 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_("DGGGLM", &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 GQR factorization of matrices A and B: */
00243 
00244 /*            Q'*A = ( R11 ) M,    Q'*B*Z' = ( T11   T12 ) M */
00245 /*                   (  0  ) N-M             (  0    T22 ) N-M */
00246 /*                      M                     M+P-N  N-M */
00247 
00248 /*     where R11 and T22 are upper triangular, and Q and Z are */
00249 /*     orthogonal. */
00250 
00251     i__1 = *lwork - *m - np;
00252     dggqrf_(n, m, p, &a[a_offset], lda, &work[1], &b[b_offset], ldb, &work[*m 
00253             + 1], &work[*m + np + 1], &i__1, info);
00254     lopt = (integer) work[*m + np + 1];
00255 
00256 /*     Update left-hand-side vector d = Q'*d = ( d1 ) M */
00257 /*                                             ( d2 ) N-M */
00258 
00259     i__1 = max(1,*n);
00260     i__2 = *lwork - *m - np;
00261     dormqr_("Left", "Transpose", n, &c__1, m, &a[a_offset], lda, &work[1], &
00262             d__[1], &i__1, &work[*m + np + 1], &i__2, info);
00263 /* Computing MAX */
00264     i__1 = lopt, i__2 = (integer) work[*m + np + 1];
00265     lopt = max(i__1,i__2);
00266 
00267 /*     Solve T22*y2 = d2 for y2 */
00268 
00269     if (*n > *m) {
00270         i__1 = *n - *m;
00271         i__2 = *n - *m;
00272         dtrtrs_("Upper", "No transpose", "Non unit", &i__1, &c__1, &b[*m + 1 
00273                 + (*m + *p - *n + 1) * b_dim1], ldb, &d__[*m + 1], &i__2, 
00274                 info);
00275 
00276         if (*info > 0) {
00277             *info = 1;
00278             return 0;
00279         }
00280 
00281         i__1 = *n - *m;
00282         dcopy_(&i__1, &d__[*m + 1], &c__1, &y[*m + *p - *n + 1], &c__1);
00283     }
00284 
00285 /*     Set y1 = 0 */
00286 
00287     i__1 = *m + *p - *n;
00288     for (i__ = 1; i__ <= i__1; ++i__) {
00289         y[i__] = 0.;
00290 /* L10: */
00291     }
00292 
00293 /*     Update d1 = d1 - T12*y2 */
00294 
00295     i__1 = *n - *m;
00296     dgemv_("No transpose", m, &i__1, &c_b32, &b[(*m + *p - *n + 1) * b_dim1 + 
00297             1], ldb, &y[*m + *p - *n + 1], &c__1, &c_b34, &d__[1], &c__1);
00298 
00299 /*     Solve triangular system: R11*x = d1 */
00300 
00301     if (*m > 0) {
00302         dtrtrs_("Upper", "No Transpose", "Non unit", m, &c__1, &a[a_offset], 
00303                 lda, &d__[1], m, info);
00304 
00305         if (*info > 0) {
00306             *info = 2;
00307             return 0;
00308         }
00309 
00310 /*        Copy D to X */
00311 
00312         dcopy_(m, &d__[1], &c__1, &x[1], &c__1);
00313     }
00314 
00315 /*     Backward transformation y = Z'*y */
00316 
00317 /* Computing MAX */
00318     i__1 = 1, i__2 = *n - *p + 1;
00319     i__3 = max(1,*p);
00320     i__4 = *lwork - *m - np;
00321     dormrq_("Left", "Transpose", p, &c__1, &np, &b[max(i__1, i__2)+ b_dim1], 
00322             ldb, &work[*m + 1], &y[1], &i__3, &work[*m + np + 1], &i__4, info);
00323 /* Computing MAX */
00324     i__1 = lopt, i__2 = (integer) work[*m + np + 1];
00325     work[1] = (doublereal) (*m + np + max(i__1,i__2));
00326 
00327     return 0;
00328 
00329 /*     End of DGGGLM */
00330 
00331 } /* dggglm_ */


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