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.

sggsvd3.c 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 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++ = ' '; }
  217. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  218. #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]; }
  219. #define sig_die(s, kill) { exit(1); }
  220. #define s_stop(s, n) {exit(0);}
  221. #define z_abs(z) (cabs(Cd(z)))
  222. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  223. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  224. #define myexit_() break;
  225. #define mycycle() continue;
  226. #define myceiling(w) {ceil(w)}
  227. #define myhuge(w) {HUGE_VAL}
  228. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  229. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  230. /* -- translated by f2c (version 20000121).
  231. You must link the resulting object file with the libraries:
  232. -lf2c -lm (in that order)
  233. */
  234. /* Table of constant values */
  235. static integer c_n1 = -1;
  236. static integer c__1 = 1;
  237. /* > \brief <b> SGGSVD3 computes the singular value decomposition (SVD) for OTHER matrices</b> */
  238. /* =========== DOCUMENTATION =========== */
  239. /* Online html documentation available at */
  240. /* http://www.netlib.org/lapack/explore-html/ */
  241. /* > \htmlonly */
  242. /* > Download SGGSVD3 + dependencies */
  243. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sggsvd3
  244. .f"> */
  245. /* > [TGZ]</a> */
  246. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sggsvd3
  247. .f"> */
  248. /* > [ZIP]</a> */
  249. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sggsvd3
  250. .f"> */
  251. /* > [TXT]</a> */
  252. /* > \endhtmlonly */
  253. /* Definition: */
  254. /* =========== */
  255. /* SUBROUTINE SGGSVD3( JOBU, JOBV, JOBQ, M, N, P, K, L, A, LDA, B, */
  256. /* LDB, ALPHA, BETA, U, LDU, V, LDV, Q, LDQ, WORK, */
  257. /* LWORK, IWORK, INFO ) */
  258. /* CHARACTER JOBQ, JOBU, JOBV */
  259. /* INTEGER INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N, P, LWORK */
  260. /* INTEGER IWORK( * ) */
  261. /* REAL A( LDA, * ), ALPHA( * ), B( LDB, * ), */
  262. /* $ BETA( * ), Q( LDQ, * ), U( LDU, * ), */
  263. /* $ V( LDV, * ), WORK( * ) */
  264. /* > \par Purpose: */
  265. /* ============= */
  266. /* > */
  267. /* > \verbatim */
  268. /* > */
  269. /* > SGGSVD3 computes the generalized singular value decomposition (GSVD) */
  270. /* > of an M-by-N real matrix A and P-by-N real matrix B: */
  271. /* > */
  272. /* > U**T*A*Q = D1*( 0 R ), V**T*B*Q = D2*( 0 R ) */
  273. /* > */
  274. /* > where U, V and Q are orthogonal matrices. */
  275. /* > Let K+L = the effective numerical rank of the matrix (A**T,B**T)**T, */
  276. /* > then R is a K+L-by-K+L nonsingular upper triangular matrix, D1 and */
  277. /* > D2 are M-by-(K+L) and P-by-(K+L) "diagonal" matrices and of the */
  278. /* > following structures, respectively: */
  279. /* > */
  280. /* > If M-K-L >= 0, */
  281. /* > */
  282. /* > K L */
  283. /* > D1 = K ( I 0 ) */
  284. /* > L ( 0 C ) */
  285. /* > M-K-L ( 0 0 ) */
  286. /* > */
  287. /* > K L */
  288. /* > D2 = L ( 0 S ) */
  289. /* > P-L ( 0 0 ) */
  290. /* > */
  291. /* > N-K-L K L */
  292. /* > ( 0 R ) = K ( 0 R11 R12 ) */
  293. /* > L ( 0 0 R22 ) */
  294. /* > */
  295. /* > where */
  296. /* > */
  297. /* > C = diag( ALPHA(K+1), ... , ALPHA(K+L) ), */
  298. /* > S = diag( BETA(K+1), ... , BETA(K+L) ), */
  299. /* > C**2 + S**2 = I. */
  300. /* > */
  301. /* > R is stored in A(1:K+L,N-K-L+1:N) on exit. */
  302. /* > */
  303. /* > If M-K-L < 0, */
  304. /* > */
  305. /* > K M-K K+L-M */
  306. /* > D1 = K ( I 0 0 ) */
  307. /* > M-K ( 0 C 0 ) */
  308. /* > */
  309. /* > K M-K K+L-M */
  310. /* > D2 = M-K ( 0 S 0 ) */
  311. /* > K+L-M ( 0 0 I ) */
  312. /* > P-L ( 0 0 0 ) */
  313. /* > */
  314. /* > N-K-L K M-K K+L-M */
  315. /* > ( 0 R ) = K ( 0 R11 R12 R13 ) */
  316. /* > M-K ( 0 0 R22 R23 ) */
  317. /* > K+L-M ( 0 0 0 R33 ) */
  318. /* > */
  319. /* > where */
  320. /* > */
  321. /* > C = diag( ALPHA(K+1), ... , ALPHA(M) ), */
  322. /* > S = diag( BETA(K+1), ... , BETA(M) ), */
  323. /* > C**2 + S**2 = I. */
  324. /* > */
  325. /* > (R11 R12 R13 ) is stored in A(1:M, N-K-L+1:N), and R33 is stored */
  326. /* > ( 0 R22 R23 ) */
  327. /* > in B(M-K+1:L,N+M-K-L+1:N) on exit. */
  328. /* > */
  329. /* > The routine computes C, S, R, and optionally the orthogonal */
  330. /* > transformation matrices U, V and Q. */
  331. /* > */
  332. /* > In particular, if B is an N-by-N nonsingular matrix, then the GSVD of */
  333. /* > A and B implicitly gives the SVD of A*inv(B): */
  334. /* > A*inv(B) = U*(D1*inv(D2))*V**T. */
  335. /* > If ( A**T,B**T)**T has orthonormal columns, then the GSVD of A and B is */
  336. /* > also equal to the CS decomposition of A and B. Furthermore, the GSVD */
  337. /* > can be used to derive the solution of the eigenvalue problem: */
  338. /* > A**T*A x = lambda* B**T*B x. */
  339. /* > In some literature, the GSVD of A and B is presented in the form */
  340. /* > U**T*A*X = ( 0 D1 ), V**T*B*X = ( 0 D2 ) */
  341. /* > where U and V are orthogonal and X is nonsingular, D1 and D2 are */
  342. /* > ``diagonal''. The former GSVD form can be converted to the latter */
  343. /* > form by taking the nonsingular matrix X as */
  344. /* > */
  345. /* > X = Q*( I 0 ) */
  346. /* > ( 0 inv(R) ). */
  347. /* > \endverbatim */
  348. /* Arguments: */
  349. /* ========== */
  350. /* > \param[in] JOBU */
  351. /* > \verbatim */
  352. /* > JOBU is CHARACTER*1 */
  353. /* > = 'U': Orthogonal matrix U is computed; */
  354. /* > = 'N': U is not computed. */
  355. /* > \endverbatim */
  356. /* > */
  357. /* > \param[in] JOBV */
  358. /* > \verbatim */
  359. /* > JOBV is CHARACTER*1 */
  360. /* > = 'V': Orthogonal matrix V is computed; */
  361. /* > = 'N': V is not computed. */
  362. /* > \endverbatim */
  363. /* > */
  364. /* > \param[in] JOBQ */
  365. /* > \verbatim */
  366. /* > JOBQ is CHARACTER*1 */
  367. /* > = 'Q': Orthogonal matrix Q is computed; */
  368. /* > = 'N': Q is not computed. */
  369. /* > \endverbatim */
  370. /* > */
  371. /* > \param[in] M */
  372. /* > \verbatim */
  373. /* > M is INTEGER */
  374. /* > The number of rows of the matrix A. M >= 0. */
  375. /* > \endverbatim */
  376. /* > */
  377. /* > \param[in] N */
  378. /* > \verbatim */
  379. /* > N is INTEGER */
  380. /* > The number of columns of the matrices A and B. N >= 0. */
  381. /* > \endverbatim */
  382. /* > */
  383. /* > \param[in] P */
  384. /* > \verbatim */
  385. /* > P is INTEGER */
  386. /* > The number of rows of the matrix B. P >= 0. */
  387. /* > \endverbatim */
  388. /* > */
  389. /* > \param[out] K */
  390. /* > \verbatim */
  391. /* > K is INTEGER */
  392. /* > \endverbatim */
  393. /* > */
  394. /* > \param[out] L */
  395. /* > \verbatim */
  396. /* > L is INTEGER */
  397. /* > */
  398. /* > On exit, K and L specify the dimension of the subblocks */
  399. /* > described in Purpose. */
  400. /* > K + L = effective numerical rank of (A**T,B**T)**T. */
  401. /* > \endverbatim */
  402. /* > */
  403. /* > \param[in,out] A */
  404. /* > \verbatim */
  405. /* > A is REAL array, dimension (LDA,N) */
  406. /* > On entry, the M-by-N matrix A. */
  407. /* > On exit, A contains the triangular matrix R, or part of R. */
  408. /* > See Purpose for details. */
  409. /* > \endverbatim */
  410. /* > */
  411. /* > \param[in] LDA */
  412. /* > \verbatim */
  413. /* > LDA is INTEGER */
  414. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  415. /* > \endverbatim */
  416. /* > */
  417. /* > \param[in,out] B */
  418. /* > \verbatim */
  419. /* > B is REAL array, dimension (LDB,N) */
  420. /* > On entry, the P-by-N matrix B. */
  421. /* > On exit, B contains the triangular matrix R if M-K-L < 0. */
  422. /* > See Purpose for details. */
  423. /* > \endverbatim */
  424. /* > */
  425. /* > \param[in] LDB */
  426. /* > \verbatim */
  427. /* > LDB is INTEGER */
  428. /* > The leading dimension of the array B. LDB >= f2cmax(1,P). */
  429. /* > \endverbatim */
  430. /* > */
  431. /* > \param[out] ALPHA */
  432. /* > \verbatim */
  433. /* > ALPHA is REAL array, dimension (N) */
  434. /* > \endverbatim */
  435. /* > */
  436. /* > \param[out] BETA */
  437. /* > \verbatim */
  438. /* > BETA is REAL array, dimension (N) */
  439. /* > */
  440. /* > On exit, ALPHA and BETA contain the generalized singular */
  441. /* > value pairs of A and B; */
  442. /* > ALPHA(1:K) = 1, */
  443. /* > BETA(1:K) = 0, */
  444. /* > and if M-K-L >= 0, */
  445. /* > ALPHA(K+1:K+L) = C, */
  446. /* > BETA(K+1:K+L) = S, */
  447. /* > or if M-K-L < 0, */
  448. /* > ALPHA(K+1:M)=C, ALPHA(M+1:K+L)=0 */
  449. /* > BETA(K+1:M) =S, BETA(M+1:K+L) =1 */
  450. /* > and */
  451. /* > ALPHA(K+L+1:N) = 0 */
  452. /* > BETA(K+L+1:N) = 0 */
  453. /* > \endverbatim */
  454. /* > */
  455. /* > \param[out] U */
  456. /* > \verbatim */
  457. /* > U is REAL array, dimension (LDU,M) */
  458. /* > If JOBU = 'U', U contains the M-by-M orthogonal matrix U. */
  459. /* > If JOBU = 'N', U is not referenced. */
  460. /* > \endverbatim */
  461. /* > */
  462. /* > \param[in] LDU */
  463. /* > \verbatim */
  464. /* > LDU is INTEGER */
  465. /* > The leading dimension of the array U. LDU >= f2cmax(1,M) if */
  466. /* > JOBU = 'U'; LDU >= 1 otherwise. */
  467. /* > \endverbatim */
  468. /* > */
  469. /* > \param[out] V */
  470. /* > \verbatim */
  471. /* > V is REAL array, dimension (LDV,P) */
  472. /* > If JOBV = 'V', V contains the P-by-P orthogonal matrix V. */
  473. /* > If JOBV = 'N', V is not referenced. */
  474. /* > \endverbatim */
  475. /* > */
  476. /* > \param[in] LDV */
  477. /* > \verbatim */
  478. /* > LDV is INTEGER */
  479. /* > The leading dimension of the array V. LDV >= f2cmax(1,P) if */
  480. /* > JOBV = 'V'; LDV >= 1 otherwise. */
  481. /* > \endverbatim */
  482. /* > */
  483. /* > \param[out] Q */
  484. /* > \verbatim */
  485. /* > Q is REAL array, dimension (LDQ,N) */
  486. /* > If JOBQ = 'Q', Q contains the N-by-N orthogonal matrix Q. */
  487. /* > If JOBQ = 'N', Q is not referenced. */
  488. /* > \endverbatim */
  489. /* > */
  490. /* > \param[in] LDQ */
  491. /* > \verbatim */
  492. /* > LDQ is INTEGER */
  493. /* > The leading dimension of the array Q. LDQ >= f2cmax(1,N) if */
  494. /* > JOBQ = 'Q'; LDQ >= 1 otherwise. */
  495. /* > \endverbatim */
  496. /* > */
  497. /* > \param[out] WORK */
  498. /* > \verbatim */
  499. /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
  500. /* > On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  501. /* > \endverbatim */
  502. /* > */
  503. /* > \param[in] LWORK */
  504. /* > \verbatim */
  505. /* > LWORK is INTEGER */
  506. /* > The dimension of the array WORK. */
  507. /* > */
  508. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  509. /* > only calculates the optimal size of the WORK array, returns */
  510. /* > this value as the first entry of the WORK array, and no error */
  511. /* > message related to LWORK is issued by XERBLA. */
  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 STGSJA. */
  532. /* > \endverbatim */
  533. /* > \par Internal Parameters: */
  534. /* ========================= */
  535. /* > */
  536. /* > \verbatim */
  537. /* > TOLA REAL */
  538. /* > TOLB REAL */
  539. /* > TOLA and TOLB are the thresholds to determine the effective */
  540. /* > rank of (A**T,B**T)**T. Generally, they are set to */
  541. /* > TOLA = MAX(M,N)*norm(A)*MACHEPS, */
  542. /* > TOLB = MAX(P,N)*norm(B)*MACHEPS. */
  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 August 2015 */
  553. /* > \ingroup realGEsing */
  554. /* > \par Contributors: */
  555. /* ================== */
  556. /* > */
  557. /* > Ming Gu and Huan Ren, Computer Science Division, University of */
  558. /* > California at Berkeley, USA */
  559. /* > */
  560. /* > \par Further Details: */
  561. /* ===================== */
  562. /* > */
  563. /* > SGGSVD3 replaces the deprecated subroutine SGGSVD. */
  564. /* > */
  565. /* ===================================================================== */
  566. /* Subroutine */ void sggsvd3_(char *jobu, char *jobv, char *jobq, integer *m,
  567. integer *n, integer *p, integer *k, integer *l, real *a, integer *lda,
  568. real *b, integer *ldb, real *alpha, real *beta, real *u, integer *
  569. ldu, real *v, integer *ldv, real *q, integer *ldq, real *work,
  570. integer *lwork, integer *iwork, integer *info)
  571. {
  572. /* System generated locals */
  573. integer a_dim1, a_offset, b_dim1, b_offset, q_dim1, q_offset, u_dim1,
  574. u_offset, v_dim1, v_offset, i__1, i__2;
  575. /* Local variables */
  576. integer ibnd;
  577. real tola;
  578. integer isub;
  579. real tolb, unfl, temp, smax;
  580. integer ncallmycycle, i__, j;
  581. extern logical lsame_(char *, char *);
  582. real anorm, bnorm;
  583. logical wantq;
  584. extern /* Subroutine */ void scopy_(integer *, real *, integer *, real *,
  585. integer *);
  586. logical wantu, wantv;
  587. extern real slamch_(char *), slange_(char *, integer *, integer *,
  588. real *, integer *, real *);
  589. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  590. extern void stgsja_(
  591. char *, char *, char *, integer *, integer *, integer *, integer *
  592. , integer *, real *, integer *, real *, integer *, real *, real *,
  593. real *, real *, real *, integer *, real *, integer *, real *,
  594. integer *, real *, integer *, integer *);
  595. integer lwkopt;
  596. logical lquery;
  597. extern /* Subroutine */ void sggsvp3_(char *, char *, char *, integer *,
  598. integer *, integer *, real *, integer *, real *, integer *, real *
  599. , real *, integer *, integer *, real *, integer *, real *,
  600. integer *, real *, integer *, integer *, real *, real *, integer *
  601. , integer *);
  602. real ulp;
  603. /* -- LAPACK driver routine (version 3.7.0) -- */
  604. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  605. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  606. /* August 2015 */
  607. /* ===================================================================== */
  608. /* Decode and test the input parameters */
  609. /* Parameter adjustments */
  610. a_dim1 = *lda;
  611. a_offset = 1 + a_dim1 * 1;
  612. a -= a_offset;
  613. b_dim1 = *ldb;
  614. b_offset = 1 + b_dim1 * 1;
  615. b -= b_offset;
  616. --alpha;
  617. --beta;
  618. u_dim1 = *ldu;
  619. u_offset = 1 + u_dim1 * 1;
  620. u -= u_offset;
  621. v_dim1 = *ldv;
  622. v_offset = 1 + v_dim1 * 1;
  623. v -= v_offset;
  624. q_dim1 = *ldq;
  625. q_offset = 1 + q_dim1 * 1;
  626. q -= q_offset;
  627. --work;
  628. --iwork;
  629. /* Function Body */
  630. wantu = lsame_(jobu, "U");
  631. wantv = lsame_(jobv, "V");
  632. wantq = lsame_(jobq, "Q");
  633. lquery = *lwork == -1;
  634. lwkopt = 1;
  635. /* Test the input arguments */
  636. *info = 0;
  637. if (! (wantu || lsame_(jobu, "N"))) {
  638. *info = -1;
  639. } else if (! (wantv || lsame_(jobv, "N"))) {
  640. *info = -2;
  641. } else if (! (wantq || lsame_(jobq, "N"))) {
  642. *info = -3;
  643. } else if (*m < 0) {
  644. *info = -4;
  645. } else if (*n < 0) {
  646. *info = -5;
  647. } else if (*p < 0) {
  648. *info = -6;
  649. } else if (*lda < f2cmax(1,*m)) {
  650. *info = -10;
  651. } else if (*ldb < f2cmax(1,*p)) {
  652. *info = -12;
  653. } else if (*ldu < 1 || wantu && *ldu < *m) {
  654. *info = -16;
  655. } else if (*ldv < 1 || wantv && *ldv < *p) {
  656. *info = -18;
  657. } else if (*ldq < 1 || wantq && *ldq < *n) {
  658. *info = -20;
  659. } else if (*lwork < 1 && ! lquery) {
  660. *info = -24;
  661. }
  662. /* Compute workspace */
  663. if (*info == 0) {
  664. sggsvp3_(jobu, jobv, jobq, m, p, n, &a[a_offset], lda, &b[b_offset],
  665. ldb, &tola, &tolb, k, l, &u[u_offset], ldu, &v[v_offset], ldv,
  666. &q[q_offset], ldq, &iwork[1], &work[1], &work[1], &c_n1,
  667. info);
  668. lwkopt = *n + (integer) work[1];
  669. /* Computing MAX */
  670. i__1 = *n << 1;
  671. lwkopt = f2cmax(i__1,lwkopt);
  672. lwkopt = f2cmax(1,lwkopt);
  673. work[1] = (real) lwkopt;
  674. }
  675. if (*info != 0) {
  676. i__1 = -(*info);
  677. xerbla_("SGGSVD3", &i__1, (ftnlen)7);
  678. return;
  679. }
  680. if (lquery) {
  681. return;
  682. }
  683. /* Compute the Frobenius norm of matrices A and B */
  684. anorm = slange_("1", m, n, &a[a_offset], lda, &work[1]);
  685. bnorm = slange_("1", p, n, &b[b_offset], ldb, &work[1]);
  686. /* Get machine precision and set up threshold for determining */
  687. /* the effective numerical rank of the matrices A and B. */
  688. ulp = slamch_("Precision");
  689. unfl = slamch_("Safe Minimum");
  690. tola = f2cmax(*m,*n) * f2cmax(anorm,unfl) * ulp;
  691. tolb = f2cmax(*p,*n) * f2cmax(bnorm,unfl) * ulp;
  692. /* Preprocessing */
  693. i__1 = *lwork - *n;
  694. sggsvp3_(jobu, jobv, jobq, m, p, n, &a[a_offset], lda, &b[b_offset], ldb,
  695. &tola, &tolb, k, l, &u[u_offset], ldu, &v[v_offset], ldv, &q[
  696. q_offset], ldq, &iwork[1], &work[1], &work[*n + 1], &i__1, info);
  697. /* Compute the GSVD of two upper "triangular" matrices */
  698. stgsja_(jobu, jobv, jobq, m, p, n, k, l, &a[a_offset], lda, &b[b_offset],
  699. ldb, &tola, &tolb, &alpha[1], &beta[1], &u[u_offset], ldu, &v[
  700. v_offset], ldv, &q[q_offset], ldq, &work[1], &ncallmycycle, info);
  701. /* Sort the singular values and store the pivot indices in IWORK */
  702. /* Copy ALPHA to WORK, then sort ALPHA in WORK */
  703. scopy_(n, &alpha[1], &c__1, &work[1], &c__1);
  704. /* Computing MIN */
  705. i__1 = *l, i__2 = *m - *k;
  706. ibnd = f2cmin(i__1,i__2);
  707. i__1 = ibnd;
  708. for (i__ = 1; i__ <= i__1; ++i__) {
  709. /* Scan for largest ALPHA(K+I) */
  710. isub = i__;
  711. smax = work[*k + i__];
  712. i__2 = ibnd;
  713. for (j = i__ + 1; j <= i__2; ++j) {
  714. temp = work[*k + j];
  715. if (temp > smax) {
  716. isub = j;
  717. smax = temp;
  718. }
  719. /* L10: */
  720. }
  721. if (isub != i__) {
  722. work[*k + isub] = work[*k + i__];
  723. work[*k + i__] = smax;
  724. iwork[*k + i__] = *k + isub;
  725. } else {
  726. iwork[*k + i__] = *k + i__;
  727. }
  728. /* L20: */
  729. }
  730. work[1] = (real) lwkopt;
  731. return;
  732. /* End of SGGSVD3 */
  733. } /* sggsvd3_ */