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


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