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.

zggsvd.c 24 kB

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