You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dggsvd.c 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <complex.h>
  6. #ifdef complex
  7. #undef complex
  8. #endif
  9. #ifdef I
  10. #undef I
  11. #endif
  12. #if defined(_WIN64)
  13. typedef long long BLASLONG;
  14. typedef unsigned long long BLASULONG;
  15. #else
  16. typedef long BLASLONG;
  17. typedef unsigned long BLASULONG;
  18. #endif
  19. #ifdef LAPACK_ILP64
  20. typedef BLASLONG blasint;
  21. #if defined(_WIN64)
  22. #define blasabs(x) llabs(x)
  23. #else
  24. #define blasabs(x) labs(x)
  25. #endif
  26. #else
  27. typedef int blasint;
  28. #define blasabs(x) abs(x)
  29. #endif
  30. typedef blasint integer;
  31. typedef unsigned int uinteger;
  32. typedef char *address;
  33. typedef short int shortint;
  34. typedef float real;
  35. typedef double doublereal;
  36. typedef struct { real r, i; } complex;
  37. typedef struct { doublereal r, i; } doublecomplex;
  38. #ifdef _MSC_VER
  39. static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
  40. static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
  41. static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
  42. static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
  43. #else
  44. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  45. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  46. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  47. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  48. #endif
  49. #define pCf(z) (*_pCf(z))
  50. #define pCd(z) (*_pCd(z))
  51. typedef int logical;
  52. typedef short int shortlogical;
  53. typedef char logical1;
  54. typedef char integer1;
  55. #define TRUE_ (1)
  56. #define FALSE_ (0)
  57. /* Extern is for use with -E */
  58. #ifndef Extern
  59. #define Extern extern
  60. #endif
  61. /* I/O stuff */
  62. typedef int flag;
  63. typedef int ftnlen;
  64. typedef int ftnint;
  65. /*external read, write*/
  66. typedef struct
  67. { flag cierr;
  68. ftnint ciunit;
  69. flag ciend;
  70. char *cifmt;
  71. ftnint cirec;
  72. } cilist;
  73. /*internal read, write*/
  74. typedef struct
  75. { flag icierr;
  76. char *iciunit;
  77. flag iciend;
  78. char *icifmt;
  79. ftnint icirlen;
  80. ftnint icirnum;
  81. } icilist;
  82. /*open*/
  83. typedef struct
  84. { flag oerr;
  85. ftnint ounit;
  86. char *ofnm;
  87. ftnlen ofnmlen;
  88. char *osta;
  89. char *oacc;
  90. char *ofm;
  91. ftnint orl;
  92. char *oblnk;
  93. } olist;
  94. /*close*/
  95. typedef struct
  96. { flag cerr;
  97. ftnint cunit;
  98. char *csta;
  99. } cllist;
  100. /*rewind, backspace, endfile*/
  101. typedef struct
  102. { flag aerr;
  103. ftnint aunit;
  104. } alist;
  105. /* inquire */
  106. typedef struct
  107. { flag inerr;
  108. ftnint inunit;
  109. char *infile;
  110. ftnlen infilen;
  111. ftnint *inex; /*parameters in standard's order*/
  112. ftnint *inopen;
  113. ftnint *innum;
  114. ftnint *innamed;
  115. char *inname;
  116. ftnlen innamlen;
  117. char *inacc;
  118. ftnlen inacclen;
  119. char *inseq;
  120. ftnlen inseqlen;
  121. char *indir;
  122. ftnlen indirlen;
  123. char *infmt;
  124. ftnlen infmtlen;
  125. char *inform;
  126. ftnint informlen;
  127. char *inunf;
  128. ftnlen inunflen;
  129. ftnint *inrecl;
  130. ftnint *innrec;
  131. char *inblank;
  132. ftnlen inblanklen;
  133. } inlist;
  134. #define VOID void
  135. union Multitype { /* for multiple entry points */
  136. integer1 g;
  137. shortint h;
  138. integer i;
  139. /* longint j; */
  140. real r;
  141. doublereal d;
  142. complex c;
  143. doublecomplex z;
  144. };
  145. typedef union Multitype Multitype;
  146. struct Vardesc { /* for Namelist */
  147. char *name;
  148. char *addr;
  149. ftnlen *dims;
  150. int type;
  151. };
  152. typedef struct Vardesc Vardesc;
  153. struct Namelist {
  154. char *name;
  155. Vardesc **vars;
  156. int nvars;
  157. };
  158. typedef struct Namelist Namelist;
  159. #define abs(x) ((x) >= 0 ? (x) : -(x))
  160. #define dabs(x) (fabs(x))
  161. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  162. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  163. #define dmin(a,b) (f2cmin(a,b))
  164. #define dmax(a,b) (f2cmax(a,b))
  165. #define bit_test(a,b) ((a) >> (b) & 1)
  166. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  167. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  168. #define abort_() { sig_die("Fortran abort routine called", 1); }
  169. #define c_abs(z) (cabsf(Cf(z)))
  170. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  171. #ifdef _MSC_VER
  172. #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
  173. #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
  174. #else
  175. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  176. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  177. #endif
  178. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  179. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  180. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  181. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  182. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  183. #define d_abs(x) (fabs(*(x)))
  184. #define d_acos(x) (acos(*(x)))
  185. #define d_asin(x) (asin(*(x)))
  186. #define d_atan(x) (atan(*(x)))
  187. #define d_atn2(x, y) (atan2(*(x),*(y)))
  188. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  189. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  190. #define d_cos(x) (cos(*(x)))
  191. #define d_cosh(x) (cosh(*(x)))
  192. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  193. #define d_exp(x) (exp(*(x)))
  194. #define d_imag(z) (cimag(Cd(z)))
  195. #define r_imag(z) (cimagf(Cf(z)))
  196. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  198. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  200. #define d_log(x) (log(*(x)))
  201. #define d_mod(x, y) (fmod(*(x), *(y)))
  202. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  203. #define d_nint(x) u_nint(*(x))
  204. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  205. #define d_sign(a,b) u_sign(*(a),*(b))
  206. #define r_sign(a,b) u_sign(*(a),*(b))
  207. #define d_sin(x) (sin(*(x)))
  208. #define d_sinh(x) (sinh(*(x)))
  209. #define d_sqrt(x) (sqrt(*(x)))
  210. #define d_tan(x) (tan(*(x)))
  211. #define d_tanh(x) (tanh(*(x)))
  212. #define i_abs(x) abs(*(x))
  213. #define i_dnnt(x) ((integer)u_nint(*(x)))
  214. #define i_len(s, n) (n)
  215. #define i_nint(x) ((integer)u_nint(*(x)))
  216. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  217. #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
  218. #define pow_si(B,E) spow_ui(*(B),*(E))
  219. #define pow_ri(B,E) spow_ui(*(B),*(E))
  220. #define pow_di(B,E) dpow_ui(*(B),*(E))
  221. #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
  222. #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
  223. #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
  224. #define s_cat(lpp, rpp, rnp, np, llp) { ftnlen i, nc, ll; char *f__rp, *lp; ll = (llp); lp = (lpp); for(i=0; i < (int)*(np); ++i) { nc = ll; if((rnp)[i] < nc) nc = (rnp)[i]; ll -= nc; f__rp = (rpp)[i]; while(--nc >= 0) *lp++ = *(f__rp)++; } while(--ll >= 0) *lp++ = ' '; }
  225. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  226. #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
  227. #define sig_die(s, kill) { exit(1); }
  228. #define s_stop(s, n) {exit(0);}
  229. #define z_abs(z) (cabs(Cd(z)))
  230. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  231. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  232. #define myexit_() break;
  233. #define mycycle() continue;
  234. #define myceiling(w) {ceil(w)}
  235. #define myhuge(w) {HUGE_VAL}
  236. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  237. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  238. /* procedure parameter types for -A and -C++ */
  239. #define F2C_proc_par_types 1
  240. /* -- translated by f2c (version 20000121).
  241. You must link the resulting object file with the libraries:
  242. -lf2c -lm (in that order)
  243. */
  244. /* Table of constant values */
  245. static integer c__1 = 1;
  246. /* > \brief <b> DGGSVD computes the singular value decomposition (SVD) for OTHER matrices</b> */
  247. /* =========== DOCUMENTATION =========== */
  248. /* Online html documentation available at */
  249. /* http://www.netlib.org/lapack/explore-html/ */
  250. /* > \htmlonly */
  251. /* > Download DGGSVD + dependencies */
  252. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dggsvd.
  253. f"> */
  254. /* > [TGZ]</a> */
  255. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dggsvd.
  256. f"> */
  257. /* > [ZIP]</a> */
  258. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dggsvd.
  259. f"> */
  260. /* > [TXT]</a> */
  261. /* > \endhtmlonly */
  262. /* Definition: */
  263. /* =========== */
  264. /* SUBROUTINE DGGSVD( JOBU, JOBV, JOBQ, M, N, P, K, L, A, LDA, B, */
  265. /* LDB, ALPHA, BETA, U, LDU, V, LDV, Q, LDQ, WORK, */
  266. /* IWORK, INFO ) */
  267. /* CHARACTER JOBQ, JOBU, JOBV */
  268. /* INTEGER INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N, P */
  269. /* INTEGER IWORK( * ) */
  270. /* DOUBLE PRECISION A( LDA, * ), ALPHA( * ), B( LDB, * ), */
  271. /* $ BETA( * ), Q( LDQ, * ), U( LDU, * ), */
  272. /* $ V( LDV, * ), WORK( * ) */
  273. /* > \par Purpose: */
  274. /* ============= */
  275. /* > */
  276. /* > \verbatim */
  277. /* > */
  278. /* > This routine is deprecated and has been replaced by routine DGGSVD3. */
  279. /* > */
  280. /* > DGGSVD computes the generalized singular value decomposition (GSVD) */
  281. /* > of an M-by-N real matrix A and P-by-N real matrix B: */
  282. /* > */
  283. /* > U**T*A*Q = D1*( 0 R ), V**T*B*Q = D2*( 0 R ) */
  284. /* > */
  285. /* > where U, V and Q are orthogonal matrices. */
  286. /* > Let K+L = the effective numerical rank of the matrix (A**T,B**T)**T, */
  287. /* > then R is a K+L-by-K+L nonsingular upper triangular matrix, D1 and */
  288. /* > D2 are M-by-(K+L) and P-by-(K+L) "diagonal" matrices and of the */
  289. /* > following structures, respectively: */
  290. /* > */
  291. /* > If M-K-L >= 0, */
  292. /* > */
  293. /* > K L */
  294. /* > D1 = K ( I 0 ) */
  295. /* > L ( 0 C ) */
  296. /* > M-K-L ( 0 0 ) */
  297. /* > */
  298. /* > K L */
  299. /* > D2 = L ( 0 S ) */
  300. /* > P-L ( 0 0 ) */
  301. /* > */
  302. /* > N-K-L K L */
  303. /* > ( 0 R ) = K ( 0 R11 R12 ) */
  304. /* > L ( 0 0 R22 ) */
  305. /* > */
  306. /* > where */
  307. /* > */
  308. /* > C = diag( ALPHA(K+1), ... , ALPHA(K+L) ), */
  309. /* > S = diag( BETA(K+1), ... , BETA(K+L) ), */
  310. /* > C**2 + S**2 = I. */
  311. /* > */
  312. /* > R is stored in A(1:K+L,N-K-L+1:N) on exit. */
  313. /* > */
  314. /* > If M-K-L < 0, */
  315. /* > */
  316. /* > K M-K K+L-M */
  317. /* > D1 = K ( I 0 0 ) */
  318. /* > M-K ( 0 C 0 ) */
  319. /* > */
  320. /* > K M-K K+L-M */
  321. /* > D2 = M-K ( 0 S 0 ) */
  322. /* > K+L-M ( 0 0 I ) */
  323. /* > P-L ( 0 0 0 ) */
  324. /* > */
  325. /* > N-K-L K M-K K+L-M */
  326. /* > ( 0 R ) = K ( 0 R11 R12 R13 ) */
  327. /* > M-K ( 0 0 R22 R23 ) */
  328. /* > K+L-M ( 0 0 0 R33 ) */
  329. /* > */
  330. /* > where */
  331. /* > */
  332. /* > C = diag( ALPHA(K+1), ... , ALPHA(M) ), */
  333. /* > S = diag( BETA(K+1), ... , BETA(M) ), */
  334. /* > C**2 + S**2 = I. */
  335. /* > */
  336. /* > (R11 R12 R13 ) is stored in A(1:M, N-K-L+1:N), and R33 is stored */
  337. /* > ( 0 R22 R23 ) */
  338. /* > in B(M-K+1:L,N+M-K-L+1:N) on exit. */
  339. /* > */
  340. /* > The routine computes C, S, R, and optionally the orthogonal */
  341. /* > transformation matrices U, V and Q. */
  342. /* > */
  343. /* > In particular, if B is an N-by-N nonsingular matrix, then the GSVD of */
  344. /* > A and B implicitly gives the SVD of A*inv(B): */
  345. /* > A*inv(B) = U*(D1*inv(D2))*V**T. */
  346. /* > If ( A**T,B**T)**T has orthonormal columns, then the GSVD of A and B is */
  347. /* > also equal to the CS decomposition of A and B. Furthermore, the GSVD */
  348. /* > can be used to derive the solution of the eigenvalue problem: */
  349. /* > A**T*A x = lambda* B**T*B x. */
  350. /* > In some literature, the GSVD of A and B is presented in the form */
  351. /* > U**T*A*X = ( 0 D1 ), V**T*B*X = ( 0 D2 ) */
  352. /* > where U and V are orthogonal and X is nonsingular, D1 and D2 are */
  353. /* > ``diagonal''. The former GSVD form can be converted to the latter */
  354. /* > form by taking the nonsingular matrix X as */
  355. /* > */
  356. /* > X = Q*( I 0 ) */
  357. /* > ( 0 inv(R) ). */
  358. /* > \endverbatim */
  359. /* Arguments: */
  360. /* ========== */
  361. /* > \param[in] JOBU */
  362. /* > \verbatim */
  363. /* > JOBU is CHARACTER*1 */
  364. /* > = 'U': Orthogonal matrix U is computed; */
  365. /* > = 'N': U is not computed. */
  366. /* > \endverbatim */
  367. /* > */
  368. /* > \param[in] JOBV */
  369. /* > \verbatim */
  370. /* > JOBV is CHARACTER*1 */
  371. /* > = 'V': Orthogonal matrix V is computed; */
  372. /* > = 'N': V is not computed. */
  373. /* > \endverbatim */
  374. /* > */
  375. /* > \param[in] JOBQ */
  376. /* > \verbatim */
  377. /* > JOBQ is CHARACTER*1 */
  378. /* > = 'Q': Orthogonal matrix Q is computed; */
  379. /* > = 'N': Q is not computed. */
  380. /* > \endverbatim */
  381. /* > */
  382. /* > \param[in] M */
  383. /* > \verbatim */
  384. /* > M is INTEGER */
  385. /* > The number of rows of the matrix A. M >= 0. */
  386. /* > \endverbatim */
  387. /* > */
  388. /* > \param[in] N */
  389. /* > \verbatim */
  390. /* > N is INTEGER */
  391. /* > The number of columns of the matrices A and B. N >= 0. */
  392. /* > \endverbatim */
  393. /* > */
  394. /* > \param[in] P */
  395. /* > \verbatim */
  396. /* > P is INTEGER */
  397. /* > The number of rows of the matrix B. P >= 0. */
  398. /* > \endverbatim */
  399. /* > */
  400. /* > \param[out] K */
  401. /* > \verbatim */
  402. /* > K is INTEGER */
  403. /* > \endverbatim */
  404. /* > */
  405. /* > \param[out] L */
  406. /* > \verbatim */
  407. /* > L is INTEGER */
  408. /* > */
  409. /* > On exit, K and L specify the dimension of the subblocks */
  410. /* > described in Purpose. */
  411. /* > K + L = effective numerical rank of (A**T,B**T)**T. */
  412. /* > \endverbatim */
  413. /* > */
  414. /* > \param[in,out] A */
  415. /* > \verbatim */
  416. /* > A is DOUBLE PRECISION array, dimension (LDA,N) */
  417. /* > On entry, the M-by-N matrix A. */
  418. /* > On exit, A contains the triangular matrix R, or part of R. */
  419. /* > See Purpose for details. */
  420. /* > \endverbatim */
  421. /* > */
  422. /* > \param[in] LDA */
  423. /* > \verbatim */
  424. /* > LDA is INTEGER */
  425. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  426. /* > \endverbatim */
  427. /* > */
  428. /* > \param[in,out] B */
  429. /* > \verbatim */
  430. /* > B is DOUBLE PRECISION array, dimension (LDB,N) */
  431. /* > On entry, the P-by-N matrix B. */
  432. /* > On exit, B contains the triangular matrix R if M-K-L < 0. */
  433. /* > See Purpose for details. */
  434. /* > \endverbatim */
  435. /* > */
  436. /* > \param[in] LDB */
  437. /* > \verbatim */
  438. /* > LDB is INTEGER */
  439. /* > The leading dimension of the array B. LDB >= f2cmax(1,P). */
  440. /* > \endverbatim */
  441. /* > */
  442. /* > \param[out] ALPHA */
  443. /* > \verbatim */
  444. /* > ALPHA is DOUBLE PRECISION array, dimension (N) */
  445. /* > \endverbatim */
  446. /* > */
  447. /* > \param[out] BETA */
  448. /* > \verbatim */
  449. /* > BETA is DOUBLE PRECISION array, dimension (N) */
  450. /* > */
  451. /* > On exit, ALPHA and BETA contain the generalized singular */
  452. /* > value pairs of A and B; */
  453. /* > ALPHA(1:K) = 1, */
  454. /* > BETA(1:K) = 0, */
  455. /* > and if M-K-L >= 0, */
  456. /* > ALPHA(K+1:K+L) = C, */
  457. /* > BETA(K+1:K+L) = S, */
  458. /* > or if M-K-L < 0, */
  459. /* > ALPHA(K+1:M)=C, ALPHA(M+1:K+L)=0 */
  460. /* > BETA(K+1:M) =S, BETA(M+1:K+L) =1 */
  461. /* > and */
  462. /* > ALPHA(K+L+1:N) = 0 */
  463. /* > BETA(K+L+1:N) = 0 */
  464. /* > \endverbatim */
  465. /* > */
  466. /* > \param[out] U */
  467. /* > \verbatim */
  468. /* > U is DOUBLE PRECISION array, dimension (LDU,M) */
  469. /* > If JOBU = 'U', U contains the M-by-M orthogonal matrix U. */
  470. /* > If JOBU = 'N', U is not referenced. */
  471. /* > \endverbatim */
  472. /* > */
  473. /* > \param[in] LDU */
  474. /* > \verbatim */
  475. /* > LDU is INTEGER */
  476. /* > The leading dimension of the array U. LDU >= f2cmax(1,M) if */
  477. /* > JOBU = 'U'; LDU >= 1 otherwise. */
  478. /* > \endverbatim */
  479. /* > */
  480. /* > \param[out] V */
  481. /* > \verbatim */
  482. /* > V is DOUBLE PRECISION array, dimension (LDV,P) */
  483. /* > If JOBV = 'V', V contains the P-by-P orthogonal matrix V. */
  484. /* > If JOBV = 'N', V is not referenced. */
  485. /* > \endverbatim */
  486. /* > */
  487. /* > \param[in] LDV */
  488. /* > \verbatim */
  489. /* > LDV is INTEGER */
  490. /* > The leading dimension of the array V. LDV >= f2cmax(1,P) if */
  491. /* > JOBV = 'V'; LDV >= 1 otherwise. */
  492. /* > \endverbatim */
  493. /* > */
  494. /* > \param[out] Q */
  495. /* > \verbatim */
  496. /* > Q is DOUBLE PRECISION array, dimension (LDQ,N) */
  497. /* > If JOBQ = 'Q', Q contains the N-by-N orthogonal matrix Q. */
  498. /* > If JOBQ = 'N', Q is not referenced. */
  499. /* > \endverbatim */
  500. /* > */
  501. /* > \param[in] LDQ */
  502. /* > \verbatim */
  503. /* > LDQ is INTEGER */
  504. /* > The leading dimension of the array Q. LDQ >= f2cmax(1,N) if */
  505. /* > JOBQ = 'Q'; LDQ >= 1 otherwise. */
  506. /* > \endverbatim */
  507. /* > */
  508. /* > \param[out] WORK */
  509. /* > \verbatim */
  510. /* > WORK is DOUBLE PRECISION array, */
  511. /* > dimension (f2cmax(3*N,M,P)+N) */
  512. /* > \endverbatim */
  513. /* > */
  514. /* > \param[out] IWORK */
  515. /* > \verbatim */
  516. /* > IWORK is INTEGER array, dimension (N) */
  517. /* > On exit, IWORK stores the sorting information. More */
  518. /* > precisely, the following loop will sort ALPHA */
  519. /* > for I = K+1, f2cmin(M,K+L) */
  520. /* > swap ALPHA(I) and ALPHA(IWORK(I)) */
  521. /* > endfor */
  522. /* > such that ALPHA(1) >= ALPHA(2) >= ... >= ALPHA(N). */
  523. /* > \endverbatim */
  524. /* > */
  525. /* > \param[out] INFO */
  526. /* > \verbatim */
  527. /* > INFO is INTEGER */
  528. /* > = 0: successful exit */
  529. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  530. /* > > 0: if INFO = 1, the Jacobi-type procedure failed to */
  531. /* > converge. For further details, see subroutine DTGSJA. */
  532. /* > \endverbatim */
  533. /* > \par Internal Parameters: */
  534. /* ========================= */
  535. /* > */
  536. /* > \verbatim */
  537. /* > TOLA DOUBLE PRECISION */
  538. /* > TOLB DOUBLE PRECISION */
  539. /* > TOLA and TOLB are the thresholds to determine the effective */
  540. /* > rank of (A',B')**T. Generally, they are set to */
  541. /* > TOLA = MAX(M,N)*norm(A)*MAZHEPS, */
  542. /* > TOLB = MAX(P,N)*norm(B)*MAZHEPS. */
  543. /* > The size of TOLA and TOLB may affect the size of backward */
  544. /* > errors of the decomposition. */
  545. /* > \endverbatim */
  546. /* Authors: */
  547. /* ======== */
  548. /* > \author Univ. of Tennessee */
  549. /* > \author Univ. of California Berkeley */
  550. /* > \author Univ. of Colorado Denver */
  551. /* > \author NAG Ltd. */
  552. /* > \date December 2016 */
  553. /* > \ingroup doubleOTHERsing */
  554. /* > \par Contributors: */
  555. /* ================== */
  556. /* > */
  557. /* > Ming Gu and Huan Ren, Computer Science Division, University of */
  558. /* > California at Berkeley, USA */
  559. /* > */
  560. /* ===================================================================== */
  561. /* Subroutine */ void dggsvd_(char *jobu, char *jobv, char *jobq, integer *m,
  562. integer *n, integer *p, integer *k, integer *l, doublereal *a,
  563. integer *lda, doublereal *b, integer *ldb, doublereal *alpha,
  564. doublereal *beta, doublereal *u, integer *ldu, doublereal *v, integer
  565. *ldv, doublereal *q, integer *ldq, doublereal *work, integer *iwork,
  566. integer *info)
  567. {
  568. /* System generated locals */
  569. integer a_dim1, a_offset, b_dim1, b_offset, q_dim1, q_offset, u_dim1,
  570. u_offset, v_dim1, v_offset, i__1, i__2;
  571. /* Local variables */
  572. integer ibnd;
  573. doublereal tola;
  574. integer isub;
  575. doublereal tolb, unfl, temp, smax;
  576. integer ncallmycycle, i__, j;
  577. extern logical lsame_(char *, char *);
  578. doublereal anorm, bnorm;
  579. extern /* Subroutine */ void dcopy_(integer *, doublereal *, integer *,
  580. doublereal *, integer *);
  581. logical wantq, wantu, wantv;
  582. extern doublereal dlamch_(char *), dlange_(char *, integer *,
  583. integer *, doublereal *, integer *, doublereal *);
  584. extern /* Subroutine */ void dtgsja_(char *, char *, char *, integer *,
  585. integer *, integer *, integer *, integer *, doublereal *, integer
  586. *, doublereal *, integer *, doublereal *, doublereal *,
  587. doublereal *, doublereal *, doublereal *, integer *, doublereal *,
  588. integer *, doublereal *, integer *, doublereal *, integer *,
  589. integer *);
  590. extern int xerbla_(char *, integer *, ftnlen);
  591. extern void dggsvp_(char *, char *, char *, integer *, integer *,
  592. integer *, doublereal *, integer *, doublereal *, integer *,
  593. doublereal *, doublereal *, integer *, integer *, doublereal *,
  594. integer *, doublereal *, integer *, doublereal *, integer *,
  595. integer *, doublereal *, doublereal *, integer *);
  596. doublereal ulp;
  597. /* -- LAPACK driver routine (version 3.7.0) -- */
  598. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  599. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  600. /* December 2016 */
  601. /* ===================================================================== */
  602. /* Test the input parameters */
  603. /* Parameter adjustments */
  604. a_dim1 = *lda;
  605. a_offset = 1 + a_dim1 * 1;
  606. a -= a_offset;
  607. b_dim1 = *ldb;
  608. b_offset = 1 + b_dim1 * 1;
  609. b -= b_offset;
  610. --alpha;
  611. --beta;
  612. u_dim1 = *ldu;
  613. u_offset = 1 + u_dim1 * 1;
  614. u -= u_offset;
  615. v_dim1 = *ldv;
  616. v_offset = 1 + v_dim1 * 1;
  617. v -= v_offset;
  618. q_dim1 = *ldq;
  619. q_offset = 1 + q_dim1 * 1;
  620. q -= q_offset;
  621. --work;
  622. --iwork;
  623. /* Function Body */
  624. wantu = lsame_(jobu, "U");
  625. wantv = lsame_(jobv, "V");
  626. wantq = lsame_(jobq, "Q");
  627. *info = 0;
  628. if (! (wantu || lsame_(jobu, "N"))) {
  629. *info = -1;
  630. } else if (! (wantv || lsame_(jobv, "N"))) {
  631. *info = -2;
  632. } else if (! (wantq || lsame_(jobq, "N"))) {
  633. *info = -3;
  634. } else if (*m < 0) {
  635. *info = -4;
  636. } else if (*n < 0) {
  637. *info = -5;
  638. } else if (*p < 0) {
  639. *info = -6;
  640. } else if (*lda < f2cmax(1,*m)) {
  641. *info = -10;
  642. } else if (*ldb < f2cmax(1,*p)) {
  643. *info = -12;
  644. } else if (*ldu < 1 || wantu && *ldu < *m) {
  645. *info = -16;
  646. } else if (*ldv < 1 || wantv && *ldv < *p) {
  647. *info = -18;
  648. } else if (*ldq < 1 || wantq && *ldq < *n) {
  649. *info = -20;
  650. }
  651. if (*info != 0) {
  652. i__1 = -(*info);
  653. xerbla_("DGGSVD", &i__1, 6);
  654. return;
  655. }
  656. /* Compute the Frobenius norm of matrices A and B */
  657. anorm = dlange_("1", m, n, &a[a_offset], lda, &work[1]);
  658. bnorm = dlange_("1", p, n, &b[b_offset], ldb, &work[1]);
  659. /* Get machine precision and set up threshold for determining */
  660. /* the effective numerical rank of the matrices A and B. */
  661. ulp = dlamch_("Precision");
  662. unfl = dlamch_("Safe Minimum");
  663. tola = f2cmax(*m,*n) * f2cmax(anorm,unfl) * ulp;
  664. tolb = f2cmax(*p,*n) * f2cmax(bnorm,unfl) * ulp;
  665. /* Preprocessing */
  666. dggsvp_(jobu, jobv, jobq, m, p, n, &a[a_offset], lda, &b[b_offset], ldb, &
  667. tola, &tolb, k, l, &u[u_offset], ldu, &v[v_offset], ldv, &q[
  668. q_offset], ldq, &iwork[1], &work[1], &work[*n + 1], info);
  669. /* Compute the GSVD of two upper "triangular" matrices */
  670. dtgsja_(jobu, jobv, jobq, m, p, n, k, l, &a[a_offset], lda, &b[b_offset],
  671. ldb, &tola, &tolb, &alpha[1], &beta[1], &u[u_offset], ldu, &v[
  672. v_offset], ldv, &q[q_offset], ldq, &work[1], &ncallmycycle, info);
  673. /* Sort the singular values and store the pivot indices in IWORK */
  674. /* Copy ALPHA to WORK, then sort ALPHA in WORK */
  675. dcopy_(n, &alpha[1], &c__1, &work[1], &c__1);
  676. /* Computing MIN */
  677. i__1 = *l, i__2 = *m - *k;
  678. ibnd = f2cmin(i__1,i__2);
  679. i__1 = ibnd;
  680. for (i__ = 1; i__ <= i__1; ++i__) {
  681. /* Scan for largest ALPHA(K+I) */
  682. isub = i__;
  683. smax = work[*k + i__];
  684. i__2 = ibnd;
  685. for (j = i__ + 1; j <= i__2; ++j) {
  686. temp = work[*k + j];
  687. if (temp > smax) {
  688. isub = j;
  689. smax = temp;
  690. }
  691. /* L10: */
  692. }
  693. if (isub != i__) {
  694. work[*k + isub] = work[*k + i__];
  695. work[*k + i__] = smax;
  696. iwork[*k + i__] = *k + isub;
  697. } else {
  698. iwork[*k + i__] = *k + i__;
  699. }
  700. /* L20: */
  701. }
  702. return;
  703. /* End of DGGSVD */
  704. } /* dggsvd_ */