sggsvd.c
Go to the documentation of this file.
00001 /* sggsvd.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 
00020 /* Subroutine */ int sggsvd_(char *jobu, char *jobv, char *jobq, integer *m, 
00021         integer *n, integer *p, integer *k, integer *l, real *a, integer *lda, 
00022          real *b, integer *ldb, real *alpha, real *beta, real *u, integer *
00023         ldu, real *v, integer *ldv, real *q, integer *ldq, real *work, 
00024         integer *iwork, integer *info)
00025 {
00026     /* System generated locals */
00027     integer a_dim1, a_offset, b_dim1, b_offset, q_dim1, q_offset, u_dim1, 
00028             u_offset, v_dim1, v_offset, i__1, i__2;
00029 
00030     /* Local variables */
00031     integer i__, j;
00032     real ulp;
00033     integer ibnd;
00034     real tola;
00035     integer isub;
00036     real tolb, unfl, temp, smax;
00037     extern logical lsame_(char *, char *);
00038     real anorm, bnorm;
00039     logical wantq;
00040     extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *, 
00041             integer *);
00042     logical wantu, wantv;
00043     extern doublereal slamch_(char *), slange_(char *, integer *, 
00044             integer *, real *, integer *, real *);
00045     integer ncycle;
00046     extern /* Subroutine */ int xerbla_(char *, integer *), stgsja_(
00047             char *, char *, char *, integer *, integer *, integer *, integer *
00048 , integer *, real *, integer *, real *, integer *, real *, real *, 
00049              real *, real *, real *, integer *, real *, integer *, real *, 
00050             integer *, real *, integer *, integer *), 
00051             sggsvp_(char *, char *, char *, integer *, integer *, integer *, 
00052             real *, integer *, real *, integer *, real *, real *, integer *, 
00053             integer *, real *, integer *, real *, integer *, real *, integer *
00054 , integer *, real *, real *, integer *);
00055 
00056 
00057 /*  -- LAPACK driver routine (version 3.2) -- */
00058 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00059 /*     November 2006 */
00060 
00061 /*     .. Scalar Arguments .. */
00062 /*     .. */
00063 /*     .. Array Arguments .. */
00064 /*     .. */
00065 
00066 /*  Purpose */
00067 /*  ======= */
00068 
00069 /*  SGGSVD computes the generalized singular value decomposition (GSVD) */
00070 /*  of an M-by-N real matrix A and P-by-N real matrix B: */
00071 
00072 /*      U'*A*Q = D1*( 0 R ),    V'*B*Q = D2*( 0 R ) */
00073 
00074 /*  where U, V and Q are orthogonal matrices, and Z' is the transpose */
00075 /*  of Z.  Let K+L = the effective numerical rank of the matrix (A',B')', */
00076 /*  then R is a K+L-by-K+L nonsingular upper triangular matrix, D1 and */
00077 /*  D2 are M-by-(K+L) and P-by-(K+L) "diagonal" matrices and of the */
00078 /*  following structures, respectively: */
00079 
00080 /*  If M-K-L >= 0, */
00081 
00082 /*                      K  L */
00083 /*         D1 =     K ( I  0 ) */
00084 /*                  L ( 0  C ) */
00085 /*              M-K-L ( 0  0 ) */
00086 
00087 /*                    K  L */
00088 /*         D2 =   L ( 0  S ) */
00089 /*              P-L ( 0  0 ) */
00090 
00091 /*                  N-K-L  K    L */
00092 /*    ( 0 R ) = K (  0   R11  R12 ) */
00093 /*              L (  0    0   R22 ) */
00094 
00095 /*  where */
00096 
00097 /*    C = diag( ALPHA(K+1), ... , ALPHA(K+L) ), */
00098 /*    S = diag( BETA(K+1),  ... , BETA(K+L) ), */
00099 /*    C**2 + S**2 = I. */
00100 
00101 /*    R is stored in A(1:K+L,N-K-L+1:N) on exit. */
00102 
00103 /*  If M-K-L < 0, */
00104 
00105 /*                    K M-K K+L-M */
00106 /*         D1 =   K ( I  0    0   ) */
00107 /*              M-K ( 0  C    0   ) */
00108 
00109 /*                      K M-K K+L-M */
00110 /*         D2 =   M-K ( 0  S    0  ) */
00111 /*              K+L-M ( 0  0    I  ) */
00112 /*                P-L ( 0  0    0  ) */
00113 
00114 /*                     N-K-L  K   M-K  K+L-M */
00115 /*    ( 0 R ) =     K ( 0    R11  R12  R13  ) */
00116 /*                M-K ( 0     0   R22  R23  ) */
00117 /*              K+L-M ( 0     0    0   R33  ) */
00118 
00119 /*  where */
00120 
00121 /*    C = diag( ALPHA(K+1), ... , ALPHA(M) ), */
00122 /*    S = diag( BETA(K+1),  ... , BETA(M) ), */
00123 /*    C**2 + S**2 = I. */
00124 
00125 /*    (R11 R12 R13 ) is stored in A(1:M, N-K-L+1:N), and R33 is stored */
00126 /*    ( 0  R22 R23 ) */
00127 /*    in B(M-K+1:L,N+M-K-L+1:N) on exit. */
00128 
00129 /*  The routine computes C, S, R, and optionally the orthogonal */
00130 /*  transformation matrices U, V and Q. */
00131 
00132 /*  In particular, if B is an N-by-N nonsingular matrix, then the GSVD of */
00133 /*  A and B implicitly gives the SVD of A*inv(B): */
00134 /*                       A*inv(B) = U*(D1*inv(D2))*V'. */
00135 /*  If ( A',B')' has orthonormal columns, then the GSVD of A and B is */
00136 /*  also equal to the CS decomposition of A and B. Furthermore, the GSVD */
00137 /*  can be used to derive the solution of the eigenvalue problem: */
00138 /*                       A'*A x = lambda* B'*B x. */
00139 /*  In some literature, the GSVD of A and B is presented in the form */
00140 /*                   U'*A*X = ( 0 D1 ),   V'*B*X = ( 0 D2 ) */
00141 /*  where U and V are orthogonal and X is nonsingular, D1 and D2 are */
00142 /*  ``diagonal''.  The former GSVD form can be converted to the latter */
00143 /*  form by taking the nonsingular matrix X as */
00144 
00145 /*                       X = Q*( I   0    ) */
00146 /*                             ( 0 inv(R) ). */
00147 
00148 /*  Arguments */
00149 /*  ========= */
00150 
00151 /*  JOBU    (input) CHARACTER*1 */
00152 /*          = 'U':  Orthogonal matrix U is computed; */
00153 /*          = 'N':  U is not computed. */
00154 
00155 /*  JOBV    (input) CHARACTER*1 */
00156 /*          = 'V':  Orthogonal matrix V is computed; */
00157 /*          = 'N':  V is not computed. */
00158 
00159 /*  JOBQ    (input) CHARACTER*1 */
00160 /*          = 'Q':  Orthogonal matrix Q is computed; */
00161 /*          = 'N':  Q is not computed. */
00162 
00163 /*  M       (input) INTEGER */
00164 /*          The number of rows of the matrix A.  M >= 0. */
00165 
00166 /*  N       (input) INTEGER */
00167 /*          The number of columns of the matrices A and B.  N >= 0. */
00168 
00169 /*  P       (input) INTEGER */
00170 /*          The number of rows of the matrix B.  P >= 0. */
00171 
00172 /*  K       (output) INTEGER */
00173 /*  L       (output) INTEGER */
00174 /*          On exit, K and L specify the dimension of the subblocks */
00175 /*          described in the Purpose section. */
00176 /*          K + L = effective numerical rank of (A',B')'. */
00177 
00178 /*  A       (input/output) REAL array, dimension (LDA,N) */
00179 /*          On entry, the M-by-N matrix A. */
00180 /*          On exit, A contains the triangular matrix R, or part of R. */
00181 /*          See Purpose for details. */
00182 
00183 /*  LDA     (input) INTEGER */
00184 /*          The leading dimension of the array A. LDA >= max(1,M). */
00185 
00186 /*  B       (input/output) REAL array, dimension (LDB,N) */
00187 /*          On entry, the P-by-N matrix B. */
00188 /*          On exit, B contains the triangular matrix R if M-K-L < 0. */
00189 /*          See Purpose for details. */
00190 
00191 /*  LDB     (input) INTEGER */
00192 /*          The leading dimension of the array B. LDB >= max(1,P). */
00193 
00194 /*  ALPHA   (output) REAL array, dimension (N) */
00195 /*  BETA    (output) REAL array, dimension (N) */
00196 /*          On exit, ALPHA and BETA contain the generalized singular */
00197 /*          value pairs of A and B; */
00198 /*            ALPHA(1:K) = 1, */
00199 /*            BETA(1:K)  = 0, */
00200 /*          and if M-K-L >= 0, */
00201 /*            ALPHA(K+1:K+L) = C, */
00202 /*            BETA(K+1:K+L)  = S, */
00203 /*          or if M-K-L < 0, */
00204 /*            ALPHA(K+1:M)=C, ALPHA(M+1:K+L)=0 */
00205 /*            BETA(K+1:M) =S, BETA(M+1:K+L) =1 */
00206 /*          and */
00207 /*            ALPHA(K+L+1:N) = 0 */
00208 /*            BETA(K+L+1:N)  = 0 */
00209 
00210 /*  U       (output) REAL array, dimension (LDU,M) */
00211 /*          If JOBU = 'U', U contains the M-by-M orthogonal matrix U. */
00212 /*          If JOBU = 'N', U is not referenced. */
00213 
00214 /*  LDU     (input) INTEGER */
00215 /*          The leading dimension of the array U. LDU >= max(1,M) if */
00216 /*          JOBU = 'U'; LDU >= 1 otherwise. */
00217 
00218 /*  V       (output) REAL array, dimension (LDV,P) */
00219 /*          If JOBV = 'V', V contains the P-by-P orthogonal matrix V. */
00220 /*          If JOBV = 'N', V is not referenced. */
00221 
00222 /*  LDV     (input) INTEGER */
00223 /*          The leading dimension of the array V. LDV >= max(1,P) if */
00224 /*          JOBV = 'V'; LDV >= 1 otherwise. */
00225 
00226 /*  Q       (output) REAL array, dimension (LDQ,N) */
00227 /*          If JOBQ = 'Q', Q contains the N-by-N orthogonal matrix Q. */
00228 /*          If JOBQ = 'N', Q is not referenced. */
00229 
00230 /*  LDQ     (input) INTEGER */
00231 /*          The leading dimension of the array Q. LDQ >= max(1,N) if */
00232 /*          JOBQ = 'Q'; LDQ >= 1 otherwise. */
00233 
00234 /*  WORK    (workspace) REAL array, */
00235 /*                      dimension (max(3*N,M,P)+N) */
00236 
00237 /*  IWORK   (workspace/output) INTEGER array, dimension (N) */
00238 /*          On exit, IWORK stores the sorting information. More */
00239 /*          precisely, the following loop will sort ALPHA */
00240 /*             for I = K+1, min(M,K+L) */
00241 /*                 swap ALPHA(I) and ALPHA(IWORK(I)) */
00242 /*             endfor */
00243 /*          such that ALPHA(1) >= ALPHA(2) >= ... >= ALPHA(N). */
00244 
00245 /*  INFO    (output) INTEGER */
00246 /*          = 0:  successful exit */
00247 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
00248 /*          > 0:  if INFO = 1, the Jacobi-type procedure failed to */
00249 /*                converge.  For further details, see subroutine STGSJA. */
00250 
00251 /*  Internal Parameters */
00252 /*  =================== */
00253 
00254 /*  TOLA    REAL */
00255 /*  TOLB    REAL */
00256 /*          TOLA and TOLB are the thresholds to determine the effective */
00257 /*          rank of (A',B')'. Generally, they are set to */
00258 /*                   TOLA = MAX(M,N)*norm(A)*MACHEPS, */
00259 /*                   TOLB = MAX(P,N)*norm(B)*MACHEPS. */
00260 /*          The size of TOLA and TOLB may affect the size of backward */
00261 /*          errors of the decomposition. */
00262 
00263 /*  Further Details */
00264 /*  =============== */
00265 
00266 /*  2-96 Based on modifications by */
00267 /*     Ming Gu and Huan Ren, Computer Science Division, University of */
00268 /*     California at Berkeley, USA */
00269 
00270 /*  ===================================================================== */
00271 
00272 /*     .. Local Scalars .. */
00273 /*     .. */
00274 /*     .. External Functions .. */
00275 /*     .. */
00276 /*     .. External Subroutines .. */
00277 /*     .. */
00278 /*     .. Intrinsic Functions .. */
00279 /*     .. */
00280 /*     .. Executable Statements .. */
00281 
00282 /*     Test the input parameters */
00283 
00284     /* Parameter adjustments */
00285     a_dim1 = *lda;
00286     a_offset = 1 + a_dim1;
00287     a -= a_offset;
00288     b_dim1 = *ldb;
00289     b_offset = 1 + b_dim1;
00290     b -= b_offset;
00291     --alpha;
00292     --beta;
00293     u_dim1 = *ldu;
00294     u_offset = 1 + u_dim1;
00295     u -= u_offset;
00296     v_dim1 = *ldv;
00297     v_offset = 1 + v_dim1;
00298     v -= v_offset;
00299     q_dim1 = *ldq;
00300     q_offset = 1 + q_dim1;
00301     q -= q_offset;
00302     --work;
00303     --iwork;
00304 
00305     /* Function Body */
00306     wantu = lsame_(jobu, "U");
00307     wantv = lsame_(jobv, "V");
00308     wantq = lsame_(jobq, "Q");
00309 
00310     *info = 0;
00311     if (! (wantu || lsame_(jobu, "N"))) {
00312         *info = -1;
00313     } else if (! (wantv || lsame_(jobv, "N"))) {
00314         *info = -2;
00315     } else if (! (wantq || lsame_(jobq, "N"))) {
00316         *info = -3;
00317     } else if (*m < 0) {
00318         *info = -4;
00319     } else if (*n < 0) {
00320         *info = -5;
00321     } else if (*p < 0) {
00322         *info = -6;
00323     } else if (*lda < max(1,*m)) {
00324         *info = -10;
00325     } else if (*ldb < max(1,*p)) {
00326         *info = -12;
00327     } else if (*ldu < 1 || wantu && *ldu < *m) {
00328         *info = -16;
00329     } else if (*ldv < 1 || wantv && *ldv < *p) {
00330         *info = -18;
00331     } else if (*ldq < 1 || wantq && *ldq < *n) {
00332         *info = -20;
00333     }
00334     if (*info != 0) {
00335         i__1 = -(*info);
00336         xerbla_("SGGSVD", &i__1);
00337         return 0;
00338     }
00339 
00340 /*     Compute the Frobenius norm of matrices A and B */
00341 
00342     anorm = slange_("1", m, n, &a[a_offset], lda, &work[1]);
00343     bnorm = slange_("1", p, n, &b[b_offset], ldb, &work[1]);
00344 
00345 /*     Get machine precision and set up threshold for determining */
00346 /*     the effective numerical rank of the matrices A and B. */
00347 
00348     ulp = slamch_("Precision");
00349     unfl = slamch_("Safe Minimum");
00350     tola = max(*m,*n) * dmax(anorm,unfl) * ulp;
00351     tolb = max(*p,*n) * dmax(bnorm,unfl) * ulp;
00352 
00353 /*     Preprocessing */
00354 
00355     sggsvp_(jobu, jobv, jobq, m, p, n, &a[a_offset], lda, &b[b_offset], ldb, &
00356             tola, &tolb, k, l, &u[u_offset], ldu, &v[v_offset], ldv, &q[
00357             q_offset], ldq, &iwork[1], &work[1], &work[*n + 1], info);
00358 
00359 /*     Compute the GSVD of two upper "triangular" matrices */
00360 
00361     stgsja_(jobu, jobv, jobq, m, p, n, k, l, &a[a_offset], lda, &b[b_offset], 
00362             ldb, &tola, &tolb, &alpha[1], &beta[1], &u[u_offset], ldu, &v[
00363             v_offset], ldv, &q[q_offset], ldq, &work[1], &ncycle, info);
00364 
00365 /*     Sort the singular values and store the pivot indices in IWORK */
00366 /*     Copy ALPHA to WORK, then sort ALPHA in WORK */
00367 
00368     scopy_(n, &alpha[1], &c__1, &work[1], &c__1);
00369 /* Computing MIN */
00370     i__1 = *l, i__2 = *m - *k;
00371     ibnd = min(i__1,i__2);
00372     i__1 = ibnd;
00373     for (i__ = 1; i__ <= i__1; ++i__) {
00374 
00375 /*        Scan for largest ALPHA(K+I) */
00376 
00377         isub = i__;
00378         smax = work[*k + i__];
00379         i__2 = ibnd;
00380         for (j = i__ + 1; j <= i__2; ++j) {
00381             temp = work[*k + j];
00382             if (temp > smax) {
00383                 isub = j;
00384                 smax = temp;
00385             }
00386 /* L10: */
00387         }
00388         if (isub != i__) {
00389             work[*k + isub] = work[*k + i__];
00390             work[*k + i__] = smax;
00391             iwork[*k + i__] = *k + isub;
00392         } else {
00393             iwork[*k + i__] = *k + i__;
00394         }
00395 /* L20: */
00396     }
00397 
00398     return 0;
00399 
00400 /*     End of SGGSVD */
00401 
00402 } /* sggsvd_ */


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