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.

sggsvp3.c 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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 real c_b14 = 0.f;
  237. static real c_b24 = 1.f;
  238. /* > \brief \b SGGSVP3 */
  239. /* =========== DOCUMENTATION =========== */
  240. /* Online html documentation available at */
  241. /* http://www.netlib.org/lapack/explore-html/ */
  242. /* > \htmlonly */
  243. /* > Download SGGSVP3 + dependencies */
  244. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sggsvp3
  245. .f"> */
  246. /* > [TGZ]</a> */
  247. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sggsvp3
  248. .f"> */
  249. /* > [ZIP]</a> */
  250. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sggsvp3
  251. .f"> */
  252. /* > [TXT]</a> */
  253. /* > \endhtmlonly */
  254. /* Definition: */
  255. /* =========== */
  256. /* SUBROUTINE SGGSVP3( JOBU, JOBV, JOBQ, M, P, N, A, LDA, B, LDB, */
  257. /* TOLA, TOLB, K, L, U, LDU, V, LDV, Q, LDQ, */
  258. /* IWORK, TAU, WORK, LWORK, INFO ) */
  259. /* CHARACTER JOBQ, JOBU, JOBV */
  260. /* INTEGER INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N, P, LWORK */
  261. /* REAL TOLA, TOLB */
  262. /* INTEGER IWORK( * ) */
  263. /* REAL A( LDA, * ), B( LDB, * ), Q( LDQ, * ), */
  264. /* $ TAU( * ), U( LDU, * ), V( LDV, * ), WORK( * ) */
  265. /* > \par Purpose: */
  266. /* ============= */
  267. /* > */
  268. /* > \verbatim */
  269. /* > */
  270. /* > SGGSVP3 computes orthogonal matrices U, V and Q such that */
  271. /* > */
  272. /* > N-K-L K L */
  273. /* > U**T*A*Q = K ( 0 A12 A13 ) if M-K-L >= 0; */
  274. /* > L ( 0 0 A23 ) */
  275. /* > M-K-L ( 0 0 0 ) */
  276. /* > */
  277. /* > N-K-L K L */
  278. /* > = K ( 0 A12 A13 ) if M-K-L < 0; */
  279. /* > M-K ( 0 0 A23 ) */
  280. /* > */
  281. /* > N-K-L K L */
  282. /* > V**T*B*Q = L ( 0 0 B13 ) */
  283. /* > P-L ( 0 0 0 ) */
  284. /* > */
  285. /* > where the K-by-K matrix A12 and L-by-L matrix B13 are nonsingular */
  286. /* > upper triangular; A23 is L-by-L upper triangular if M-K-L >= 0, */
  287. /* > otherwise A23 is (M-K)-by-L upper trapezoidal. K+L = the effective */
  288. /* > numerical rank of the (M+P)-by-N matrix (A**T,B**T)**T. */
  289. /* > */
  290. /* > This decomposition is the preprocessing step for computing the */
  291. /* > Generalized Singular Value Decomposition (GSVD), see subroutine */
  292. /* > SGGSVD3. */
  293. /* > \endverbatim */
  294. /* Arguments: */
  295. /* ========== */
  296. /* > \param[in] JOBU */
  297. /* > \verbatim */
  298. /* > JOBU is CHARACTER*1 */
  299. /* > = 'U': Orthogonal matrix U is computed; */
  300. /* > = 'N': U is not computed. */
  301. /* > \endverbatim */
  302. /* > */
  303. /* > \param[in] JOBV */
  304. /* > \verbatim */
  305. /* > JOBV is CHARACTER*1 */
  306. /* > = 'V': Orthogonal matrix V is computed; */
  307. /* > = 'N': V is not computed. */
  308. /* > \endverbatim */
  309. /* > */
  310. /* > \param[in] JOBQ */
  311. /* > \verbatim */
  312. /* > JOBQ is CHARACTER*1 */
  313. /* > = 'Q': Orthogonal matrix Q is computed; */
  314. /* > = 'N': Q is not computed. */
  315. /* > \endverbatim */
  316. /* > */
  317. /* > \param[in] M */
  318. /* > \verbatim */
  319. /* > M is INTEGER */
  320. /* > The number of rows of the matrix A. M >= 0. */
  321. /* > \endverbatim */
  322. /* > */
  323. /* > \param[in] P */
  324. /* > \verbatim */
  325. /* > P is INTEGER */
  326. /* > The number of rows of the matrix B. P >= 0. */
  327. /* > \endverbatim */
  328. /* > */
  329. /* > \param[in] N */
  330. /* > \verbatim */
  331. /* > N is INTEGER */
  332. /* > The number of columns of the matrices A and B. N >= 0. */
  333. /* > \endverbatim */
  334. /* > */
  335. /* > \param[in,out] A */
  336. /* > \verbatim */
  337. /* > A is REAL array, dimension (LDA,N) */
  338. /* > On entry, the M-by-N matrix A. */
  339. /* > On exit, A contains the triangular (or trapezoidal) matrix */
  340. /* > described in the Purpose section. */
  341. /* > \endverbatim */
  342. /* > */
  343. /* > \param[in] LDA */
  344. /* > \verbatim */
  345. /* > LDA is INTEGER */
  346. /* > The leading dimension of the array A. LDA >= f2cmax(1,M). */
  347. /* > \endverbatim */
  348. /* > */
  349. /* > \param[in,out] B */
  350. /* > \verbatim */
  351. /* > B is REAL array, dimension (LDB,N) */
  352. /* > On entry, the P-by-N matrix B. */
  353. /* > On exit, B contains the triangular matrix described in */
  354. /* > the Purpose section. */
  355. /* > \endverbatim */
  356. /* > */
  357. /* > \param[in] LDB */
  358. /* > \verbatim */
  359. /* > LDB is INTEGER */
  360. /* > The leading dimension of the array B. LDB >= f2cmax(1,P). */
  361. /* > \endverbatim */
  362. /* > */
  363. /* > \param[in] TOLA */
  364. /* > \verbatim */
  365. /* > TOLA is REAL */
  366. /* > \endverbatim */
  367. /* > */
  368. /* > \param[in] TOLB */
  369. /* > \verbatim */
  370. /* > TOLB is REAL */
  371. /* > */
  372. /* > TOLA and TOLB are the thresholds to determine the effective */
  373. /* > numerical rank of matrix B and a subblock of A. Generally, */
  374. /* > they are set to */
  375. /* > TOLA = MAX(M,N)*norm(A)*MACHEPS, */
  376. /* > TOLB = MAX(P,N)*norm(B)*MACHEPS. */
  377. /* > The size of TOLA and TOLB may affect the size of backward */
  378. /* > errors of the decomposition. */
  379. /* > \endverbatim */
  380. /* > */
  381. /* > \param[out] K */
  382. /* > \verbatim */
  383. /* > K is INTEGER */
  384. /* > \endverbatim */
  385. /* > */
  386. /* > \param[out] L */
  387. /* > \verbatim */
  388. /* > L is INTEGER */
  389. /* > */
  390. /* > On exit, K and L specify the dimension of the subblocks */
  391. /* > described in Purpose section. */
  392. /* > K + L = effective numerical rank of (A**T,B**T)**T. */
  393. /* > \endverbatim */
  394. /* > */
  395. /* > \param[out] U */
  396. /* > \verbatim */
  397. /* > U is REAL array, dimension (LDU,M) */
  398. /* > If JOBU = 'U', U contains the orthogonal matrix U. */
  399. /* > If JOBU = 'N', U is not referenced. */
  400. /* > \endverbatim */
  401. /* > */
  402. /* > \param[in] LDU */
  403. /* > \verbatim */
  404. /* > LDU is INTEGER */
  405. /* > The leading dimension of the array U. LDU >= f2cmax(1,M) if */
  406. /* > JOBU = 'U'; LDU >= 1 otherwise. */
  407. /* > \endverbatim */
  408. /* > */
  409. /* > \param[out] V */
  410. /* > \verbatim */
  411. /* > V is REAL array, dimension (LDV,P) */
  412. /* > If JOBV = 'V', V contains the orthogonal matrix V. */
  413. /* > If JOBV = 'N', V is not referenced. */
  414. /* > \endverbatim */
  415. /* > */
  416. /* > \param[in] LDV */
  417. /* > \verbatim */
  418. /* > LDV is INTEGER */
  419. /* > The leading dimension of the array V. LDV >= f2cmax(1,P) if */
  420. /* > JOBV = 'V'; LDV >= 1 otherwise. */
  421. /* > \endverbatim */
  422. /* > */
  423. /* > \param[out] Q */
  424. /* > \verbatim */
  425. /* > Q is REAL array, dimension (LDQ,N) */
  426. /* > If JOBQ = 'Q', Q contains the orthogonal matrix Q. */
  427. /* > If JOBQ = 'N', Q is not referenced. */
  428. /* > \endverbatim */
  429. /* > */
  430. /* > \param[in] LDQ */
  431. /* > \verbatim */
  432. /* > LDQ is INTEGER */
  433. /* > The leading dimension of the array Q. LDQ >= f2cmax(1,N) if */
  434. /* > JOBQ = 'Q'; LDQ >= 1 otherwise. */
  435. /* > \endverbatim */
  436. /* > */
  437. /* > \param[out] IWORK */
  438. /* > \verbatim */
  439. /* > IWORK is INTEGER array, dimension (N) */
  440. /* > \endverbatim */
  441. /* > */
  442. /* > \param[out] TAU */
  443. /* > \verbatim */
  444. /* > TAU is REAL array, dimension (N) */
  445. /* > \endverbatim */
  446. /* > */
  447. /* > \param[out] WORK */
  448. /* > \verbatim */
  449. /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
  450. /* > On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  451. /* > \endverbatim */
  452. /* > */
  453. /* > \param[in] LWORK */
  454. /* > \verbatim */
  455. /* > LWORK is INTEGER */
  456. /* > The dimension of the array WORK. */
  457. /* > */
  458. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  459. /* > only calculates the optimal size of the WORK array, returns */
  460. /* > this value as the first entry of the WORK array, and no error */
  461. /* > message related to LWORK is issued by XERBLA. */
  462. /* > \endverbatim */
  463. /* > */
  464. /* > \param[out] INFO */
  465. /* > \verbatim */
  466. /* > INFO is INTEGER */
  467. /* > = 0: successful exit */
  468. /* > < 0: if INFO = -i, the i-th argument had an illegal value. */
  469. /* > \endverbatim */
  470. /* Authors: */
  471. /* ======== */
  472. /* > \author Univ. of Tennessee */
  473. /* > \author Univ. of California Berkeley */
  474. /* > \author Univ. of Colorado Denver */
  475. /* > \author NAG Ltd. */
  476. /* > \date August 2015 */
  477. /* > \ingroup realOTHERcomputational */
  478. /* > \par Further Details: */
  479. /* ===================== */
  480. /* > */
  481. /* > \verbatim */
  482. /* > */
  483. /* > The subroutine uses LAPACK subroutine SGEQP3 for the QR factorization */
  484. /* > with column pivoting to detect the effective numerical rank of the */
  485. /* > a matrix. It may be replaced by a better rank determination strategy. */
  486. /* > */
  487. /* > SGGSVP3 replaces the deprecated subroutine SGGSVP. */
  488. /* > */
  489. /* > \endverbatim */
  490. /* > */
  491. /* ===================================================================== */
  492. /* Subroutine */ void sggsvp3_(char *jobu, char *jobv, char *jobq, integer *m,
  493. integer *p, integer *n, real *a, integer *lda, real *b, integer *ldb,
  494. real *tola, real *tolb, integer *k, integer *l, real *u, integer *ldu,
  495. real *v, integer *ldv, real *q, integer *ldq, integer *iwork, real *
  496. tau, real *work, integer *lwork, integer *info)
  497. {
  498. /* System generated locals */
  499. integer a_dim1, a_offset, b_dim1, b_offset, q_dim1, q_offset, u_dim1,
  500. u_offset, v_dim1, v_offset, i__1, i__2, i__3;
  501. real r__1;
  502. /* Local variables */
  503. integer i__, j;
  504. extern logical lsame_(char *, char *);
  505. logical wantq, wantu, wantv;
  506. extern /* Subroutine */ void sgeqp3_(integer *, integer *, real *, integer
  507. *, integer *, real *, real *, integer *, integer *), sgeqr2_(
  508. integer *, integer *, real *, integer *, real *, real *, integer *
  509. ), sgerq2_(integer *, integer *, real *, integer *, real *, real *
  510. , integer *), sorg2r_(integer *, integer *, integer *, real *,
  511. integer *, real *, real *, integer *), sorm2r_(char *, char *,
  512. integer *, integer *, integer *, real *, integer *, real *, real *
  513. , integer *, real *, integer *), sormr2_(char *,
  514. char *, integer *, integer *, integer *, real *, integer *, real *
  515. , real *, integer *, real *, integer *);
  516. extern int xerbla_(char *, integer *, ftnlen);
  517. extern void slacpy_(char *, integer *, integer *,
  518. real *, integer *, real *, integer *), slaset_(char *,
  519. integer *, integer *, real *, real *, real *, integer *),
  520. slapmt_(logical *, integer *, integer *, real *, integer *,
  521. integer *);
  522. logical forwrd;
  523. integer lwkopt;
  524. logical lquery;
  525. /* -- LAPACK computational routine (version 3.7.0) -- */
  526. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  527. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  528. /* August 2015 */
  529. /* ===================================================================== */
  530. /* Test the input parameters */
  531. /* Parameter adjustments */
  532. a_dim1 = *lda;
  533. a_offset = 1 + a_dim1 * 1;
  534. a -= a_offset;
  535. b_dim1 = *ldb;
  536. b_offset = 1 + b_dim1 * 1;
  537. b -= b_offset;
  538. u_dim1 = *ldu;
  539. u_offset = 1 + u_dim1 * 1;
  540. u -= u_offset;
  541. v_dim1 = *ldv;
  542. v_offset = 1 + v_dim1 * 1;
  543. v -= v_offset;
  544. q_dim1 = *ldq;
  545. q_offset = 1 + q_dim1 * 1;
  546. q -= q_offset;
  547. --iwork;
  548. --tau;
  549. --work;
  550. /* Function Body */
  551. wantu = lsame_(jobu, "U");
  552. wantv = lsame_(jobv, "V");
  553. wantq = lsame_(jobq, "Q");
  554. forwrd = TRUE_;
  555. lquery = *lwork == -1;
  556. lwkopt = 1;
  557. /* Test the input arguments */
  558. *info = 0;
  559. if (! (wantu || lsame_(jobu, "N"))) {
  560. *info = -1;
  561. } else if (! (wantv || lsame_(jobv, "N"))) {
  562. *info = -2;
  563. } else if (! (wantq || lsame_(jobq, "N"))) {
  564. *info = -3;
  565. } else if (*m < 0) {
  566. *info = -4;
  567. } else if (*p < 0) {
  568. *info = -5;
  569. } else if (*n < 0) {
  570. *info = -6;
  571. } else if (*lda < f2cmax(1,*m)) {
  572. *info = -8;
  573. } else if (*ldb < f2cmax(1,*p)) {
  574. *info = -10;
  575. } else if (*ldu < 1 || wantu && *ldu < *m) {
  576. *info = -16;
  577. } else if (*ldv < 1 || wantv && *ldv < *p) {
  578. *info = -18;
  579. } else if (*ldq < 1 || wantq && *ldq < *n) {
  580. *info = -20;
  581. } else if (*lwork < 1 && ! lquery) {
  582. *info = -24;
  583. }
  584. /* Compute workspace */
  585. if (*info == 0) {
  586. sgeqp3_(p, n, &b[b_offset], ldb, &iwork[1], &tau[1], &work[1], &c_n1,
  587. info);
  588. lwkopt = (integer) work[1];
  589. if (wantv) {
  590. lwkopt = f2cmax(lwkopt,*p);
  591. }
  592. /* Computing MAX */
  593. i__1 = lwkopt, i__2 = f2cmin(*n,*p);
  594. lwkopt = f2cmax(i__1,i__2);
  595. lwkopt = f2cmax(lwkopt,*m);
  596. if (wantq) {
  597. lwkopt = f2cmax(lwkopt,*n);
  598. }
  599. sgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &tau[1], &work[1], &c_n1,
  600. info);
  601. /* Computing MAX */
  602. i__1 = lwkopt, i__2 = (integer) work[1];
  603. lwkopt = f2cmax(i__1,i__2);
  604. lwkopt = f2cmax(1,lwkopt);
  605. work[1] = (real) lwkopt;
  606. }
  607. if (*info != 0) {
  608. i__1 = -(*info);
  609. xerbla_("SGGSVP3", &i__1, (ftnlen)7);
  610. return;
  611. }
  612. if (lquery) {
  613. return;
  614. }
  615. /* QR with column pivoting of B: B*P = V*( S11 S12 ) */
  616. /* ( 0 0 ) */
  617. i__1 = *n;
  618. for (i__ = 1; i__ <= i__1; ++i__) {
  619. iwork[i__] = 0;
  620. /* L10: */
  621. }
  622. sgeqp3_(p, n, &b[b_offset], ldb, &iwork[1], &tau[1], &work[1], lwork,
  623. info);
  624. /* Update A := A*P */
  625. slapmt_(&forwrd, m, n, &a[a_offset], lda, &iwork[1]);
  626. /* Determine the effective rank of matrix B. */
  627. *l = 0;
  628. i__1 = f2cmin(*p,*n);
  629. for (i__ = 1; i__ <= i__1; ++i__) {
  630. if ((r__1 = b[i__ + i__ * b_dim1], abs(r__1)) > *tolb) {
  631. ++(*l);
  632. }
  633. /* L20: */
  634. }
  635. if (wantv) {
  636. /* Copy the details of V, and form V. */
  637. slaset_("Full", p, p, &c_b14, &c_b14, &v[v_offset], ldv);
  638. if (*p > 1) {
  639. i__1 = *p - 1;
  640. slacpy_("Lower", &i__1, n, &b[b_dim1 + 2], ldb, &v[v_dim1 + 2],
  641. ldv);
  642. }
  643. i__1 = f2cmin(*p,*n);
  644. sorg2r_(p, p, &i__1, &v[v_offset], ldv, &tau[1], &work[1], info);
  645. }
  646. /* Clean up B */
  647. i__1 = *l - 1;
  648. for (j = 1; j <= i__1; ++j) {
  649. i__2 = *l;
  650. for (i__ = j + 1; i__ <= i__2; ++i__) {
  651. b[i__ + j * b_dim1] = 0.f;
  652. /* L30: */
  653. }
  654. /* L40: */
  655. }
  656. if (*p > *l) {
  657. i__1 = *p - *l;
  658. slaset_("Full", &i__1, n, &c_b14, &c_b14, &b[*l + 1 + b_dim1], ldb);
  659. }
  660. if (wantq) {
  661. /* Set Q = I and Update Q := Q*P */
  662. slaset_("Full", n, n, &c_b14, &c_b24, &q[q_offset], ldq);
  663. slapmt_(&forwrd, n, n, &q[q_offset], ldq, &iwork[1]);
  664. }
  665. if (*p >= *l && *n != *l) {
  666. /* RQ factorization of (S11 S12): ( S11 S12 ) = ( 0 S12 )*Z */
  667. sgerq2_(l, n, &b[b_offset], ldb, &tau[1], &work[1], info);
  668. /* Update A := A*Z**T */
  669. sormr2_("Right", "Transpose", m, n, l, &b[b_offset], ldb, &tau[1], &a[
  670. a_offset], lda, &work[1], info);
  671. if (wantq) {
  672. /* Update Q := Q*Z**T */
  673. sormr2_("Right", "Transpose", n, n, l, &b[b_offset], ldb, &tau[1],
  674. &q[q_offset], ldq, &work[1], info);
  675. }
  676. /* Clean up B */
  677. i__1 = *n - *l;
  678. slaset_("Full", l, &i__1, &c_b14, &c_b14, &b[b_offset], ldb);
  679. i__1 = *n;
  680. for (j = *n - *l + 1; j <= i__1; ++j) {
  681. i__2 = *l;
  682. for (i__ = j - *n + *l + 1; i__ <= i__2; ++i__) {
  683. b[i__ + j * b_dim1] = 0.f;
  684. /* L50: */
  685. }
  686. /* L60: */
  687. }
  688. }
  689. /* Let N-L L */
  690. /* A = ( A11 A12 ) M, */
  691. /* then the following does the complete QR decomposition of A11: */
  692. /* A11 = U*( 0 T12 )*P1**T */
  693. /* ( 0 0 ) */
  694. i__1 = *n - *l;
  695. for (i__ = 1; i__ <= i__1; ++i__) {
  696. iwork[i__] = 0;
  697. /* L70: */
  698. }
  699. i__1 = *n - *l;
  700. sgeqp3_(m, &i__1, &a[a_offset], lda, &iwork[1], &tau[1], &work[1], lwork,
  701. info);
  702. /* Determine the effective rank of A11 */
  703. *k = 0;
  704. /* Computing MIN */
  705. i__2 = *m, i__3 = *n - *l;
  706. i__1 = f2cmin(i__2,i__3);
  707. for (i__ = 1; i__ <= i__1; ++i__) {
  708. if ((r__1 = a[i__ + i__ * a_dim1], abs(r__1)) > *tola) {
  709. ++(*k);
  710. }
  711. /* L80: */
  712. }
  713. /* Update A12 := U**T*A12, where A12 = A( 1:M, N-L+1:N ) */
  714. /* Computing MIN */
  715. i__2 = *m, i__3 = *n - *l;
  716. i__1 = f2cmin(i__2,i__3);
  717. sorm2r_("Left", "Transpose", m, l, &i__1, &a[a_offset], lda, &tau[1], &a[(
  718. *n - *l + 1) * a_dim1 + 1], lda, &work[1], info);
  719. if (wantu) {
  720. /* Copy the details of U, and form U */
  721. slaset_("Full", m, m, &c_b14, &c_b14, &u[u_offset], ldu);
  722. if (*m > 1) {
  723. i__1 = *m - 1;
  724. i__2 = *n - *l;
  725. slacpy_("Lower", &i__1, &i__2, &a[a_dim1 + 2], lda, &u[u_dim1 + 2]
  726. , ldu);
  727. }
  728. /* Computing MIN */
  729. i__2 = *m, i__3 = *n - *l;
  730. i__1 = f2cmin(i__2,i__3);
  731. sorg2r_(m, m, &i__1, &u[u_offset], ldu, &tau[1], &work[1], info);
  732. }
  733. if (wantq) {
  734. /* Update Q( 1:N, 1:N-L ) = Q( 1:N, 1:N-L )*P1 */
  735. i__1 = *n - *l;
  736. slapmt_(&forwrd, n, &i__1, &q[q_offset], ldq, &iwork[1]);
  737. }
  738. /* Clean up A: set the strictly lower triangular part of */
  739. /* A(1:K, 1:K) = 0, and A( K+1:M, 1:N-L ) = 0. */
  740. i__1 = *k - 1;
  741. for (j = 1; j <= i__1; ++j) {
  742. i__2 = *k;
  743. for (i__ = j + 1; i__ <= i__2; ++i__) {
  744. a[i__ + j * a_dim1] = 0.f;
  745. /* L90: */
  746. }
  747. /* L100: */
  748. }
  749. if (*m > *k) {
  750. i__1 = *m - *k;
  751. i__2 = *n - *l;
  752. slaset_("Full", &i__1, &i__2, &c_b14, &c_b14, &a[*k + 1 + a_dim1],
  753. lda);
  754. }
  755. if (*n - *l > *k) {
  756. /* RQ factorization of ( T11 T12 ) = ( 0 T12 )*Z1 */
  757. i__1 = *n - *l;
  758. sgerq2_(k, &i__1, &a[a_offset], lda, &tau[1], &work[1], info);
  759. if (wantq) {
  760. /* Update Q( 1:N,1:N-L ) = Q( 1:N,1:N-L )*Z1**T */
  761. i__1 = *n - *l;
  762. sormr2_("Right", "Transpose", n, &i__1, k, &a[a_offset], lda, &
  763. tau[1], &q[q_offset], ldq, &work[1], info);
  764. }
  765. /* Clean up A */
  766. i__1 = *n - *l - *k;
  767. slaset_("Full", k, &i__1, &c_b14, &c_b14, &a[a_offset], lda);
  768. i__1 = *n - *l;
  769. for (j = *n - *l - *k + 1; j <= i__1; ++j) {
  770. i__2 = *k;
  771. for (i__ = j - *n + *l + *k + 1; i__ <= i__2; ++i__) {
  772. a[i__ + j * a_dim1] = 0.f;
  773. /* L110: */
  774. }
  775. /* L120: */
  776. }
  777. }
  778. if (*m > *k) {
  779. /* QR factorization of A( K+1:M,N-L+1:N ) */
  780. i__1 = *m - *k;
  781. sgeqr2_(&i__1, l, &a[*k + 1 + (*n - *l + 1) * a_dim1], lda, &tau[1], &
  782. work[1], info);
  783. if (wantu) {
  784. /* Update U(:,K+1:M) := U(:,K+1:M)*U1 */
  785. i__1 = *m - *k;
  786. /* Computing MIN */
  787. i__3 = *m - *k;
  788. i__2 = f2cmin(i__3,*l);
  789. sorm2r_("Right", "No transpose", m, &i__1, &i__2, &a[*k + 1 + (*n
  790. - *l + 1) * a_dim1], lda, &tau[1], &u[(*k + 1) * u_dim1 +
  791. 1], ldu, &work[1], info);
  792. }
  793. /* Clean up */
  794. i__1 = *n;
  795. for (j = *n - *l + 1; j <= i__1; ++j) {
  796. i__2 = *m;
  797. for (i__ = j - *n + *k + *l + 1; i__ <= i__2; ++i__) {
  798. a[i__ + j * a_dim1] = 0.f;
  799. /* L130: */
  800. }
  801. /* L140: */
  802. }
  803. }
  804. work[1] = (real) lwkopt;
  805. return;
  806. /* End of SGGSVP3 */
  807. } /* sggsvp3_ */