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


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