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.

zgedmdq.c 46 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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]/Cd(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. static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
  230. #define z_abs(z) (cabs(Cd(z)))
  231. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  232. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  233. #define myexit_() break;
  234. #define mycycle_() continue;
  235. #define myceiling_(w) {ceil(w)}
  236. #define myhuge_(w) {HUGE_VAL}
  237. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  238. #define mymaxloc_(w,s,e,n) dmaxloc_(w,*(s),*(e),n)
  239. /* procedure parameter types for -A and -C++ */
  240. #define F2C_proc_par_types 1
  241. #ifdef __cplusplus
  242. typedef logical (*L_fp)(...);
  243. #else
  244. typedef logical (*L_fp)();
  245. #endif
  246. static float spow_ui(float x, integer n) {
  247. float pow=1.0; unsigned long int u;
  248. if(n != 0) {
  249. if(n < 0) n = -n, x = 1/x;
  250. for(u = n; ; ) {
  251. if(u & 01) pow *= x;
  252. if(u >>= 1) x *= x;
  253. else break;
  254. }
  255. }
  256. return pow;
  257. }
  258. static double dpow_ui(double x, integer n) {
  259. double pow=1.0; unsigned long int u;
  260. if(n != 0) {
  261. if(n < 0) n = -n, x = 1/x;
  262. for(u = n; ; ) {
  263. if(u & 01) pow *= x;
  264. if(u >>= 1) x *= x;
  265. else break;
  266. }
  267. }
  268. return pow;
  269. }
  270. #ifdef _MSC_VER
  271. static _Fcomplex cpow_ui(complex x, integer n) {
  272. complex pow={1.0,0.0}; unsigned long int u;
  273. if(n != 0) {
  274. if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
  275. for(u = n; ; ) {
  276. if(u & 01) pow.r *= x.r, pow.i *= x.i;
  277. if(u >>= 1) x.r *= x.r, x.i *= x.i;
  278. else break;
  279. }
  280. }
  281. _Fcomplex p={pow.r, pow.i};
  282. return p;
  283. }
  284. #else
  285. static _Complex float cpow_ui(_Complex float x, integer n) {
  286. _Complex float pow=1.0; unsigned long int u;
  287. if(n != 0) {
  288. if(n < 0) n = -n, x = 1/x;
  289. for(u = n; ; ) {
  290. if(u & 01) pow *= x;
  291. if(u >>= 1) x *= x;
  292. else break;
  293. }
  294. }
  295. return pow;
  296. }
  297. #endif
  298. #ifdef _MSC_VER
  299. static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
  300. _Dcomplex pow={1.0,0.0}; unsigned long int u;
  301. if(n != 0) {
  302. if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
  303. for(u = n; ; ) {
  304. if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
  305. if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
  306. else break;
  307. }
  308. }
  309. _Dcomplex p = {pow._Val[0], pow._Val[1]};
  310. return p;
  311. }
  312. #else
  313. static _Complex double zpow_ui(_Complex double x, integer n) {
  314. _Complex double pow=1.0; unsigned long int u;
  315. if(n != 0) {
  316. if(n < 0) n = -n, x = 1/x;
  317. for(u = n; ; ) {
  318. if(u & 01) pow *= x;
  319. if(u >>= 1) x *= x;
  320. else break;
  321. }
  322. }
  323. return pow;
  324. }
  325. #endif
  326. static integer pow_ii(integer x, integer n) {
  327. integer pow; unsigned long int u;
  328. if (n <= 0) {
  329. if (n == 0 || x == 1) pow = 1;
  330. else if (x != -1) pow = x == 0 ? 1/x : 0;
  331. else n = -n;
  332. }
  333. if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
  334. u = n;
  335. for(pow = 1; ; ) {
  336. if(u & 01) pow *= x;
  337. if(u >>= 1) x *= x;
  338. else break;
  339. }
  340. }
  341. return pow;
  342. }
  343. static integer dmaxloc_(double *w, integer s, integer e, integer *n)
  344. {
  345. double m; integer i, mi;
  346. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  347. if (w[i-1]>m) mi=i ,m=w[i-1];
  348. return mi-s+1;
  349. }
  350. static integer smaxloc_(float *w, integer s, integer e, integer *n)
  351. {
  352. float m; integer i, mi;
  353. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  354. if (w[i-1]>m) mi=i ,m=w[i-1];
  355. return mi-s+1;
  356. }
  357. static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  358. integer n = *n_, incx = *incx_, incy = *incy_, i;
  359. #ifdef _MSC_VER
  360. _Fcomplex zdotc = {0.0, 0.0};
  361. if (incx == 1 && incy == 1) {
  362. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  363. zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
  364. zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
  365. }
  366. } else {
  367. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  368. zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
  369. zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
  370. }
  371. }
  372. pCf(z) = zdotc;
  373. }
  374. #else
  375. _Complex float zdotc = 0.0;
  376. if (incx == 1 && incy == 1) {
  377. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  378. zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
  379. }
  380. } else {
  381. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  382. zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
  383. }
  384. }
  385. pCf(z) = zdotc;
  386. }
  387. #endif
  388. static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  389. integer n = *n_, incx = *incx_, incy = *incy_, i;
  390. #ifdef _MSC_VER
  391. _Dcomplex zdotc = {0.0, 0.0};
  392. if (incx == 1 && incy == 1) {
  393. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  394. zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
  395. zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
  396. }
  397. } else {
  398. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  399. zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
  400. zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
  401. }
  402. }
  403. pCd(z) = zdotc;
  404. }
  405. #else
  406. _Complex double zdotc = 0.0;
  407. if (incx == 1 && incy == 1) {
  408. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  409. zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
  410. }
  411. } else {
  412. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  413. zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
  414. }
  415. }
  416. pCd(z) = zdotc;
  417. }
  418. #endif
  419. static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  420. integer n = *n_, incx = *incx_, incy = *incy_, i;
  421. #ifdef _MSC_VER
  422. _Fcomplex zdotc = {0.0, 0.0};
  423. if (incx == 1 && incy == 1) {
  424. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  425. zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
  426. zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
  427. }
  428. } else {
  429. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  430. zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
  431. zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
  432. }
  433. }
  434. pCf(z) = zdotc;
  435. }
  436. #else
  437. _Complex float zdotc = 0.0;
  438. if (incx == 1 && incy == 1) {
  439. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  440. zdotc += Cf(&x[i]) * Cf(&y[i]);
  441. }
  442. } else {
  443. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  444. zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
  445. }
  446. }
  447. pCf(z) = zdotc;
  448. }
  449. #endif
  450. static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  451. integer n = *n_, incx = *incx_, incy = *incy_, i;
  452. #ifdef _MSC_VER
  453. _Dcomplex zdotc = {0.0, 0.0};
  454. if (incx == 1 && incy == 1) {
  455. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  456. zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
  457. zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
  458. }
  459. } else {
  460. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  461. zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
  462. zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
  463. }
  464. }
  465. pCd(z) = zdotc;
  466. }
  467. #else
  468. _Complex double zdotc = 0.0;
  469. if (incx == 1 && incy == 1) {
  470. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  471. zdotc += Cd(&x[i]) * Cd(&y[i]);
  472. }
  473. } else {
  474. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  475. zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
  476. }
  477. }
  478. pCd(z) = zdotc;
  479. }
  480. #endif
  481. /* -- translated by f2c (version 20000121).
  482. You must link the resulting object file with the libraries:
  483. -lf2c -lm (in that order)
  484. */
  485. /* -- translated by f2c (version 20000121).
  486. You must link the resulting object file with the libraries:
  487. -lf2c -lm (in that order)
  488. */
  489. /* Table of constant values */
  490. static integer c_n1 = -1;
  491. /* Subroutine */ int zgedmdq_(char *jobs, char *jobz, char *jobr, char *jobq,
  492. char *jobt, char *jobf, integer *whtsvd, integer *m, integer *n,
  493. doublecomplex *f, integer *ldf, doublecomplex *x, integer *ldx,
  494. doublecomplex *y, integer *ldy, integer *nrnk, doublereal *tol,
  495. integer *k, doublecomplex *eigs, doublecomplex *z__, integer *ldz,
  496. doublereal *res, doublecomplex *b, integer *ldb, doublecomplex *v,
  497. integer *ldv, doublecomplex *s, integer *lds, doublecomplex *zwork,
  498. integer *lzwork, doublereal *work, integer *lwork, integer *iwork,
  499. integer *liwork, integer *info)
  500. {
  501. /* System generated locals */
  502. integer f_dim1, f_offset, x_dim1, x_offset, y_dim1, y_offset, z_dim1,
  503. z_offset, b_dim1, b_offset, v_dim1, v_offset, s_dim1, s_offset,
  504. i__1, i__2;
  505. /* Local variables */
  506. doublereal zero;
  507. integer info1;
  508. extern logical lsame_(char *, char *);
  509. char jobvl[1];
  510. integer minmn;
  511. logical wantq;
  512. integer mlwqr, olwqr;
  513. logical wntex;
  514. doublecomplex zzero;
  515. extern /* Subroutine */ int zgedmd_(char *, char *, char *, char *,
  516. integer *, integer *, integer *, doublecomplex *, integer *,
  517. doublecomplex *, integer *, integer *, doublereal *, integer *,
  518. doublecomplex *, doublecomplex *, integer *, doublereal *,
  519. doublecomplex *, integer *, doublecomplex *, integer *,
  520. doublecomplex *, integer *, doublecomplex *, integer *,
  521. doublereal *, integer *, integer *, integer *, integer *), xerbla_(char *, integer *);
  522. integer mlwdmd, olwdmd;
  523. logical sccolx, sccoly;
  524. extern /* Subroutine */ int zgeqrf_(integer *, integer *, doublecomplex *,
  525. integer *, doublecomplex *, doublecomplex *, integer *, integer *
  526. ), zlacpy_(char *, integer *, integer *, doublecomplex *, integer
  527. *, doublecomplex *, integer *), zlaset_(char *, integer *,
  528. integer *, doublecomplex *, doublecomplex *, doublecomplex *,
  529. integer *);
  530. integer iminwr;
  531. logical wntvec, wntvcf;
  532. integer mlwgqr;
  533. logical wntref;
  534. integer mlwork, olwgqr, olwork, mlrwrk, mlwmqr, olwmqr;
  535. logical lquery, wntres, wnttrf, wntvcq;
  536. extern /* Subroutine */ int zungqr_(integer *, integer *, integer *,
  537. doublecomplex *, integer *, doublecomplex *, doublecomplex *,
  538. integer *, integer *), zunmqr_(char *, char *, integer *, integer
  539. *, integer *, doublecomplex *, integer *, doublecomplex *,
  540. doublecomplex *, integer *, doublecomplex *, integer *, integer *);
  541. doublereal one;
  542. /* March 2023 */
  543. /* ..... */
  544. /* USE iso_fortran_env */
  545. /* INTEGER, PARAMETER :: WP = real64 */
  546. /* ..... */
  547. /* Scalar arguments */
  548. /* Array arguments */
  549. /* ..... */
  550. /* Purpose */
  551. /* ======= */
  552. /* ZGEDMDQ computes the Dynamic Mode Decomposition (DMD) for */
  553. /* a pair of data snapshot matrices, using a QR factorization */
  554. /* based compression of the data. For the input matrices */
  555. /* X and Y such that Y = A*X with an unaccessible matrix */
  556. /* A, ZGEDMDQ computes a certain number of Ritz pairs of A using */
  557. /* the standard Rayleigh-Ritz extraction from a subspace of */
  558. /* range(X) that is determined using the leading left singular */
  559. /* vectors of X. Optionally, ZGEDMDQ returns the residuals */
  560. /* of the computed Ritz pairs, the information needed for */
  561. /* a refinement of the Ritz vectors, or the eigenvectors of */
  562. /* the Exact DMD. */
  563. /* For further details see the references listed */
  564. /* below. For more details of the implementation see [3]. */
  565. /* References */
  566. /* ========== */
  567. /* [1] P. Schmid: Dynamic mode decomposition of numerical */
  568. /* and experimental data, */
  569. /* Journal of Fluid Mechanics 656, 5-28, 2010. */
  570. /* [2] Z. Drmac, I. Mezic, R. Mohr: Data driven modal */
  571. /* decompositions: analysis and enhancements, */
  572. /* SIAM J. on Sci. Comp. 40 (4), A2253-A2285, 2018. */
  573. /* [3] Z. Drmac: A LAPACK implementation of the Dynamic */
  574. /* Mode Decomposition I. Technical report. AIMDyn Inc. */
  575. /* and LAPACK Working Note 298. */
  576. /* [4] J. Tu, C. W. Rowley, D. M. Luchtenburg, S. L. */
  577. /* Brunton, N. Kutz: On Dynamic Mode Decomposition: */
  578. /* Theory and Applications, Journal of Computational */
  579. /* Dynamics 1(2), 391 -421, 2014. */
  580. /* Developed and supported by: */
  581. /* =========================== */
  582. /* Developed and coded by Zlatko Drmac, Faculty of Science, */
  583. /* University of Zagreb; drmac@math.hr */
  584. /* In cooperation with */
  585. /* AIMdyn Inc., Santa Barbara, CA. */
  586. /* and supported by */
  587. /* - DARPA SBIR project "Koopman Operator-Based Forecasting */
  588. /* for Nonstationary Processes from Near-Term, Limited */
  589. /* Observational Data" Contract No: W31P4Q-21-C-0007 */
  590. /* - DARPA PAI project "Physics-Informed Machine Learning */
  591. /* Methodologies" Contract No: HR0011-18-9-0033 */
  592. /* - DARPA MoDyL project "A Data-Driven, Operator-Theoretic */
  593. /* Framework for Space-Time Analysis of Process Dynamics" */
  594. /* Contract No: HR0011-16-C-0116 */
  595. /* Any opinions, findings and conclusions or recommendations */
  596. /* expressed in this material are those of the author and */
  597. /* do not necessarily reflect the views of the DARPA SBIR */
  598. /* Program Office. */
  599. /* ============================================================ */
  600. /* Distribution Statement A: */
  601. /* Approved for Public Release, Distribution Unlimited. */
  602. /* Cleared by DARPA on September 29, 2022 */
  603. /* ============================================================ */
  604. /* ...................................................................... */
  605. /* Arguments */
  606. /* ========= */
  607. /* JOBS (input) CHARACTER*1 */
  608. /* Determines whether the initial data snapshots are scaled */
  609. /* by a diagonal matrix. The data snapshots are the columns */
  610. /* of F. The leading N-1 columns of F are denoted X and the */
  611. /* trailing N-1 columns are denoted Y. */
  612. /* 'S' :: The data snapshots matrices X and Y are multiplied */
  613. /* with a diagonal matrix D so that X*D has unit */
  614. /* nonzero columns (in the Euclidean 2-norm) */
  615. /* 'C' :: The snapshots are scaled as with the 'S' option. */
  616. /* If it is found that an i-th column of X is zero */
  617. /* vector and the corresponding i-th column of Y is */
  618. /* non-zero, then the i-th column of Y is set to */
  619. /* zero and a warning flag is raised. */
  620. /* 'Y' :: The data snapshots matrices X and Y are multiplied */
  621. /* by a diagonal matrix D so that Y*D has unit */
  622. /* nonzero columns (in the Euclidean 2-norm) */
  623. /* 'N' :: No data scaling. */
  624. /* ..... */
  625. /* JOBZ (input) CHARACTER*1 */
  626. /* Determines whether the eigenvectors (Koopman modes) will */
  627. /* be computed. */
  628. /* 'V' :: The eigenvectors (Koopman modes) will be computed */
  629. /* and returned in the matrix Z. */
  630. /* See the description of Z. */
  631. /* 'F' :: The eigenvectors (Koopman modes) will be returned */
  632. /* in factored form as the product Z*V, where Z */
  633. /* is orthonormal and V contains the eigenvectors */
  634. /* of the corresponding Rayleigh quotient. */
  635. /* See the descriptions of F, V, Z. */
  636. /* 'Q' :: The eigenvectors (Koopman modes) will be returned */
  637. /* in factored form as the product Q*Z, where Z */
  638. /* contains the eigenvectors of the compression of the */
  639. /* underlying discretized operator onto the span of */
  640. /* the data snapshots. See the descriptions of F, V, Z. */
  641. /* Q is from the initial QR factorization. */
  642. /* 'N' :: The eigenvectors are not computed. */
  643. /* ..... */
  644. /* JOBR (input) CHARACTER*1 */
  645. /* Determines whether to compute the residuals. */
  646. /* 'R' :: The residuals for the computed eigenpairs will */
  647. /* be computed and stored in the array RES. */
  648. /* See the description of RES. */
  649. /* For this option to be legal, JOBZ must be 'V'. */
  650. /* 'N' :: The residuals are not computed. */
  651. /* ..... */
  652. /* JOBQ (input) CHARACTER*1 */
  653. /* Specifies whether to explicitly compute and return the */
  654. /* unitary matrix from the QR factorization. */
  655. /* 'Q' :: The matrix Q of the QR factorization of the data */
  656. /* snapshot matrix is computed and stored in the */
  657. /* array F. See the description of F. */
  658. /* 'N' :: The matrix Q is not explicitly computed. */
  659. /* ..... */
  660. /* JOBT (input) CHARACTER*1 */
  661. /* Specifies whether to return the upper triangular factor */
  662. /* from the QR factorization. */
  663. /* 'R' :: The matrix R of the QR factorization of the data */
  664. /* snapshot matrix F is returned in the array Y. */
  665. /* See the description of Y and Further details. */
  666. /* 'N' :: The matrix R is not returned. */
  667. /* ..... */
  668. /* JOBF (input) CHARACTER*1 */
  669. /* Specifies whether to store information needed for post- */
  670. /* processing (e.g. computing refined Ritz vectors) */
  671. /* 'R' :: The matrix needed for the refinement of the Ritz */
  672. /* vectors is computed and stored in the array B. */
  673. /* See the description of B. */
  674. /* 'E' :: The unscaled eigenvectors of the Exact DMD are */
  675. /* computed and returned in the array B. See the */
  676. /* description of B. */
  677. /* 'N' :: No eigenvector refinement data is computed. */
  678. /* To be useful on exit, this option needs JOBQ='Q'. */
  679. /* ..... */
  680. /* WHTSVD (input) INTEGER, WHSTVD in { 1, 2, 3, 4 } */
  681. /* Allows for a selection of the SVD algorithm from the */
  682. /* LAPACK library. */
  683. /* 1 :: ZGESVD (the QR SVD algorithm) */
  684. /* 2 :: ZGESDD (the Divide and Conquer algorithm; if enough */
  685. /* workspace available, this is the fastest option) */
  686. /* 3 :: ZGESVDQ (the preconditioned QR SVD ; this and 4 */
  687. /* are the most accurate options) */
  688. /* 4 :: ZGEJSV (the preconditioned Jacobi SVD; this and 3 */
  689. /* are the most accurate options) */
  690. /* For the four methods above, a significant difference in */
  691. /* the accuracy of small singular values is possible if */
  692. /* the snapshots vary in norm so that X is severely */
  693. /* ill-conditioned. If small (smaller than EPS*||X||) */
  694. /* singular values are of interest and JOBS=='N', then */
  695. /* the options (3, 4) give the most accurate results, where */
  696. /* the option 4 is slightly better and with stronger */
  697. /* theoretical background. */
  698. /* If JOBS=='S', i.e. the columns of X will be normalized, */
  699. /* then all methods give nearly equally accurate results. */
  700. /* ..... */
  701. /* M (input) INTEGER, M >= 0 */
  702. /* The state space dimension (the number of rows of F). */
  703. /* ..... */
  704. /* N (input) INTEGER, 0 <= N <= M */
  705. /* The number of data snapshots from a single trajectory, */
  706. /* taken at equidistant discrete times. This is the */
  707. /* number of columns of F. */
  708. /* ..... */
  709. /* F (input/output) COMPLEX(KIND=WP) M-by-N array */
  710. /* > On entry, */
  711. /* the columns of F are the sequence of data snapshots */
  712. /* from a single trajectory, taken at equidistant discrete */
  713. /* times. It is assumed that the column norms of F are */
  714. /* in the range of the normalized floating point numbers. */
  715. /* < On exit, */
  716. /* If JOBQ == 'Q', the array F contains the orthogonal */
  717. /* matrix/factor of the QR factorization of the initial */
  718. /* data snapshots matrix F. See the description of JOBQ. */
  719. /* If JOBQ == 'N', the entries in F strictly below the main */
  720. /* diagonal contain, column-wise, the information on the */
  721. /* Householder vectors, as returned by ZGEQRF. The */
  722. /* remaining information to restore the orthogonal matrix */
  723. /* of the initial QR factorization is stored in ZWORK(1:MIN(M,N)). */
  724. /* See the description of ZWORK. */
  725. /* ..... */
  726. /* LDF (input) INTEGER, LDF >= M */
  727. /* The leading dimension of the array F. */
  728. /* ..... */
  729. /* X (workspace/output) COMPLEX(KIND=WP) MIN(M,N)-by-(N-1) array */
  730. /* X is used as workspace to hold representations of the */
  731. /* leading N-1 snapshots in the orthonormal basis computed */
  732. /* in the QR factorization of F. */
  733. /* On exit, the leading K columns of X contain the leading */
  734. /* K left singular vectors of the above described content */
  735. /* of X. To lift them to the space of the left singular */
  736. /* vectors U(:,1:K) of the input data, pre-multiply with the */
  737. /* Q factor from the initial QR factorization. */
  738. /* See the descriptions of F, K, V and Z. */
  739. /* ..... */
  740. /* LDX (input) INTEGER, LDX >= N */
  741. /* The leading dimension of the array X. */
  742. /* ..... */
  743. /* Y (workspace/output) COMPLEX(KIND=WP) MIN(M,N)-by-(N) array */
  744. /* Y is used as workspace to hold representations of the */
  745. /* trailing N-1 snapshots in the orthonormal basis computed */
  746. /* in the QR factorization of F. */
  747. /* On exit, */
  748. /* If JOBT == 'R', Y contains the MIN(M,N)-by-N upper */
  749. /* triangular factor from the QR factorization of the data */
  750. /* snapshot matrix F. */
  751. /* ..... */
  752. /* LDY (input) INTEGER , LDY >= N */
  753. /* The leading dimension of the array Y. */
  754. /* ..... */
  755. /* NRNK (input) INTEGER */
  756. /* Determines the mode how to compute the numerical rank, */
  757. /* i.e. how to truncate small singular values of the input */
  758. /* matrix X. On input, if */
  759. /* NRNK = -1 :: i-th singular value sigma(i) is truncated */
  760. /* if sigma(i) <= TOL*sigma(1) */
  761. /* This option is recommended. */
  762. /* NRNK = -2 :: i-th singular value sigma(i) is truncated */
  763. /* if sigma(i) <= TOL*sigma(i-1) */
  764. /* This option is included for R&D purposes. */
  765. /* It requires highly accurate SVD, which */
  766. /* may not be feasible. */
  767. /* The numerical rank can be enforced by using positive */
  768. /* value of NRNK as follows: */
  769. /* 0 < NRNK <= N-1 :: at most NRNK largest singular values */
  770. /* will be used. If the number of the computed nonzero */
  771. /* singular values is less than NRNK, then only those */
  772. /* nonzero values will be used and the actually used */
  773. /* dimension is less than NRNK. The actual number of */
  774. /* the nonzero singular values is returned in the variable */
  775. /* K. See the description of K. */
  776. /* ..... */
  777. /* TOL (input) REAL(KIND=WP), 0 <= TOL < 1 */
  778. /* The tolerance for truncating small singular values. */
  779. /* See the description of NRNK. */
  780. /* ..... */
  781. /* K (output) INTEGER, 0 <= K <= N */
  782. /* The dimension of the SVD/POD basis for the leading N-1 */
  783. /* data snapshots (columns of F) and the number of the */
  784. /* computed Ritz pairs. The value of K is determined */
  785. /* according to the rule set by the parameters NRNK and */
  786. /* TOL. See the descriptions of NRNK and TOL. */
  787. /* ..... */
  788. /* EIGS (output) COMPLEX(KIND=WP) (N-1)-by-1 array */
  789. /* The leading K (K<=N-1) entries of EIGS contain */
  790. /* the computed eigenvalues (Ritz values). */
  791. /* See the descriptions of K, and Z. */
  792. /* ..... */
  793. /* Z (workspace/output) COMPLEX(KIND=WP) M-by-(N-1) array */
  794. /* If JOBZ =='V' then Z contains the Ritz vectors. Z(:,i) */
  795. /* is an eigenvector of the i-th Ritz value; ||Z(:,i)||_2=1. */
  796. /* If JOBZ == 'F', then the Z(:,i)'s are given implicitly as */
  797. /* Z*V, where Z contains orthonormal matrix (the product of */
  798. /* Q from the initial QR factorization and the SVD/POD_basis */
  799. /* returned by ZGEDMD in X) and the second factor (the */
  800. /* eigenvectors of the Rayleigh quotient) is in the array V, */
  801. /* as returned by ZGEDMD. That is, X(:,1:K)*V(:,i) */
  802. /* is an eigenvector corresponding to EIGS(i). The columns */
  803. /* of V(1:K,1:K) are the computed eigenvectors of the */
  804. /* K-by-K Rayleigh quotient. */
  805. /* See the descriptions of EIGS, X and V. */
  806. /* ..... */
  807. /* LDZ (input) INTEGER , LDZ >= M */
  808. /* The leading dimension of the array Z. */
  809. /* ..... */
  810. /* RES (output) REAL(KIND=WP) (N-1)-by-1 array */
  811. /* RES(1:K) contains the residuals for the K computed */
  812. /* Ritz pairs, */
  813. /* RES(i) = || A * Z(:,i) - EIGS(i)*Z(:,i))||_2. */
  814. /* See the description of EIGS and Z. */
  815. /* ..... */
  816. /* B (output) COMPLEX(KIND=WP) MIN(M,N)-by-(N-1) array. */
  817. /* IF JOBF =='R', B(1:N,1:K) contains A*U(:,1:K), and can */
  818. /* be used for computing the refined vectors; see further */
  819. /* details in the provided references. */
  820. /* If JOBF == 'E', B(1:N,1;K) contains */
  821. /* A*U(:,1:K)*W(1:K,1:K), which are the vectors from the */
  822. /* Exact DMD, up to scaling by the inverse eigenvalues. */
  823. /* In both cases, the content of B can be lifted to the */
  824. /* original dimension of the input data by pre-multiplying */
  825. /* with the Q factor from the initial QR factorization. */
  826. /* Here A denotes a compression of the underlying operator. */
  827. /* See the descriptions of F and X. */
  828. /* If JOBF =='N', then B is not referenced. */
  829. /* ..... */
  830. /* LDB (input) INTEGER, LDB >= MIN(M,N) */
  831. /* The leading dimension of the array B. */
  832. /* ..... */
  833. /* V (workspace/output) COMPLEX(KIND=WP) (N-1)-by-(N-1) array */
  834. /* On exit, V(1:K,1:K) V contains the K eigenvectors of */
  835. /* the Rayleigh quotient. The Ritz vectors */
  836. /* (returned in Z) are the product of Q from the initial QR */
  837. /* factorization (see the description of F) X (see the */
  838. /* description of X) and V. */
  839. /* ..... */
  840. /* LDV (input) INTEGER, LDV >= N-1 */
  841. /* The leading dimension of the array V. */
  842. /* ..... */
  843. /* S (output) COMPLEX(KIND=WP) (N-1)-by-(N-1) array */
  844. /* The array S(1:K,1:K) is used for the matrix Rayleigh */
  845. /* quotient. This content is overwritten during */
  846. /* the eigenvalue decomposition by ZGEEV. */
  847. /* See the description of K. */
  848. /* ..... */
  849. /* LDS (input) INTEGER, LDS >= N-1 */
  850. /* The leading dimension of the array S. */
  851. /* ..... */
  852. /* ZWORK (workspace/output) COMPLEX(KIND=WP) LWORK-by-1 array */
  853. /* On exit, */
  854. /* ZWORK(1:MIN(M,N)) contains the scalar factors of the */
  855. /* elementary reflectors as returned by ZGEQRF of the */
  856. /* M-by-N input matrix F. */
  857. /* If the call to ZGEDMDQ is only workspace query, then */
  858. /* ZWORK(1) contains the minimal complex workspace length and */
  859. /* ZWORK(2) is the optimal complex workspace length. */
  860. /* Hence, the length of work is at least 2. */
  861. /* See the description of LZWORK. */
  862. /* ..... */
  863. /* LZWORK (input) INTEGER */
  864. /* The minimal length of the workspace vector ZWORK. */
  865. /* LZWORK is calculated as follows: */
  866. /* Let MLWQR = N (minimal workspace for ZGEQRF[M,N]) */
  867. /* MLWDMD = minimal workspace for ZGEDMD (see the */
  868. /* description of LWORK in ZGEDMD) */
  869. /* MLWMQR = N (minimal workspace for */
  870. /* ZUNMQR['L','N',M,N,N]) */
  871. /* MLWGQR = N (minimal workspace for ZUNGQR[M,N,N]) */
  872. /* MINMN = MIN(M,N) */
  873. /* Then */
  874. /* LZWORK = MAX(2, MIN(M,N)+MLWQR, MINMN+MLWDMD) */
  875. /* is further updated as follows: */
  876. /* if JOBZ == 'V' or JOBZ == 'F' THEN */
  877. /* LZWORK = MAX(LZWORK, MINMN+MLWMQR) */
  878. /* if JOBQ == 'Q' THEN */
  879. /* LZWORK = MAX(ZLWORK, MINMN+MLWGQR) */
  880. /* ..... */
  881. /* WORK (workspace/output) REAL(KIND=WP) LWORK-by-1 array */
  882. /* On exit, */
  883. /* WORK(1:N-1) contains the singular values of */
  884. /* the input submatrix F(1:M,1:N-1). */
  885. /* If the call to ZGEDMDQ is only workspace query, then */
  886. /* WORK(1) contains the minimal workspace length and */
  887. /* WORK(2) is the optimal workspace length. hence, the */
  888. /* length of work is at least 2. */
  889. /* See the description of LWORK. */
  890. /* ..... */
  891. /* LWORK (input) INTEGER */
  892. /* The minimal length of the workspace vector WORK. */
  893. /* LWORK is the same as in ZGEDMD, because in ZGEDMDQ */
  894. /* only ZGEDMD requires real workspace for snapshots */
  895. /* of dimensions MIN(M,N)-by-(N-1). */
  896. /* If on entry LWORK = -1, then a workspace query is */
  897. /* assumed and the procedure only computes the minimal */
  898. /* and the optimal workspace length for WORK. */
  899. /* ..... */
  900. /* IWORK (workspace/output) INTEGER LIWORK-by-1 array */
  901. /* Workspace that is required only if WHTSVD equals */
  902. /* 2 , 3 or 4. (See the description of WHTSVD). */
  903. /* If on entry LWORK =-1 or LIWORK=-1, then the */
  904. /* minimal length of IWORK is computed and returned in */
  905. /* IWORK(1). See the description of LIWORK. */
  906. /* ..... */
  907. /* LIWORK (input) INTEGER */
  908. /* The minimal length of the workspace vector IWORK. */
  909. /* If WHTSVD == 1, then only IWORK(1) is used; LIWORK >=1 */
  910. /* Let M1=MIN(M,N), N1=N-1. Then */
  911. /* If WHTSVD == 2, then LIWORK >= MAX(1,8*MIN(M1,N1)) */
  912. /* If WHTSVD == 3, then LIWORK >= MAX(1,M1+N1-1) */
  913. /* If WHTSVD == 4, then LIWORK >= MAX(3,M1+3*N1) */
  914. /* If on entry LIWORK = -1, then a workspace query is */
  915. /* assumed and the procedure only computes the minimal */
  916. /* and the optimal workspace lengths for both WORK and */
  917. /* IWORK. See the descriptions of WORK and IWORK. */
  918. /* ..... */
  919. /* INFO (output) INTEGER */
  920. /* -i < 0 :: On entry, the i-th argument had an */
  921. /* illegal value */
  922. /* = 0 :: Successful return. */
  923. /* = 1 :: Void input. Quick exit (M=0 or N=0). */
  924. /* = 2 :: The SVD computation of X did not converge. */
  925. /* Suggestion: Check the input data and/or */
  926. /* repeat with different WHTSVD. */
  927. /* = 3 :: The computation of the eigenvalues did not */
  928. /* converge. */
  929. /* = 4 :: If data scaling was requested on input and */
  930. /* the procedure found inconsistency in the data */
  931. /* such that for some column index i, */
  932. /* X(:,i) = 0 but Y(:,i) /= 0, then Y(:,i) is set */
  933. /* to zero if JOBS=='C'. The computation proceeds */
  934. /* with original or modified data and warning */
  935. /* flag is set with INFO=4. */
  936. /* ............................................................. */
  937. /* ............................................................. */
  938. /* Parameters */
  939. /* ~~~~~~~~~~ */
  940. /* COMPLEX(KIND=WP), PARAMETER :: ZONE = ( 1.0_WP, 0.0_WP ) */
  941. /* Local scalars */
  942. /* ~~~~~~~~~~~~~ */
  943. /* External functions (BLAS and LAPACK) */
  944. /* ~~~~~~~~~~~~~~~~~ */
  945. /* External subroutines (BLAS and LAPACK) */
  946. /* ~~~~~~~~~~~~~~~~~~~~ */
  947. /* External subroutines */
  948. /* ~~~~~~~~~~~~~~~~~~~~ */
  949. /* Intrinsic functions */
  950. /* ~~~~~~~~~~~~~~~~~~~ */
  951. /* .......................................................... */
  952. /* Parameter adjustments */
  953. f_dim1 = *ldf;
  954. f_offset = 1 + f_dim1 * 1;
  955. f -= f_offset;
  956. x_dim1 = *ldx;
  957. x_offset = 1 + x_dim1 * 1;
  958. x -= x_offset;
  959. y_dim1 = *ldy;
  960. y_offset = 1 + y_dim1 * 1;
  961. y -= y_offset;
  962. --eigs;
  963. z_dim1 = *ldz;
  964. z_offset = 1 + z_dim1 * 1;
  965. z__ -= z_offset;
  966. --res;
  967. b_dim1 = *ldb;
  968. b_offset = 1 + b_dim1 * 1;
  969. b -= b_offset;
  970. v_dim1 = *ldv;
  971. v_offset = 1 + v_dim1 * 1;
  972. v -= v_offset;
  973. s_dim1 = *lds;
  974. s_offset = 1 + s_dim1 * 1;
  975. s -= s_offset;
  976. --zwork;
  977. --work;
  978. --iwork;
  979. /* Function Body */
  980. one = 1.f;
  981. zero = 0.f;
  982. zzero.r = 0.f, zzero.i = 0.f;
  983. /* Test the input arguments */
  984. wntres = lsame_(jobr, "R");
  985. sccolx = lsame_(jobs, "S") || lsame_(jobs, "C");
  986. sccoly = lsame_(jobs, "Y");
  987. wntvec = lsame_(jobz, "V");
  988. wntvcf = lsame_(jobz, "F");
  989. wntvcq = lsame_(jobz, "Q");
  990. wntref = lsame_(jobf, "R");
  991. wntex = lsame_(jobf, "E");
  992. wantq = lsame_(jobq, "Q");
  993. wnttrf = lsame_(jobt, "R");
  994. minmn = f2cmin(*m,*n);
  995. *info = 0;
  996. lquery = *lzwork == -1 || *lwork == -1 || *liwork == -1;
  997. if (! (sccolx || sccoly || lsame_(jobs, "N"))) {
  998. *info = -1;
  999. } else if (! (wntvec || wntvcf || wntvcq || lsame_(jobz, "N"))) {
  1000. *info = -2;
  1001. } else if (! (wntres || lsame_(jobr, "N")) ||
  1002. wntres && lsame_(jobz, "N")) {
  1003. *info = -3;
  1004. } else if (! (wantq || lsame_(jobq, "N"))) {
  1005. *info = -4;
  1006. } else if (! (wnttrf || lsame_(jobt, "N"))) {
  1007. *info = -5;
  1008. } else if (! (wntref || wntex || lsame_(jobf, "N")))
  1009. {
  1010. *info = -6;
  1011. } else if (! (*whtsvd == 1 || *whtsvd == 2 || *whtsvd == 3 || *whtsvd ==
  1012. 4)) {
  1013. *info = -7;
  1014. } else if (*m < 0) {
  1015. *info = -8;
  1016. } else if (*n < 0 || *n > *m + 1) {
  1017. *info = -9;
  1018. } else if (*ldf < *m) {
  1019. *info = -11;
  1020. } else if (*ldx < minmn) {
  1021. *info = -13;
  1022. } else if (*ldy < minmn) {
  1023. *info = -15;
  1024. } else if (! (*nrnk == -2 || *nrnk == -1 || *nrnk >= 1 && *nrnk <= *n)) {
  1025. *info = -16;
  1026. } else if (*tol < zero || *tol >= one) {
  1027. *info = -17;
  1028. } else if (*ldz < *m) {
  1029. *info = -21;
  1030. } else if ((wntref || wntex) && *ldb < minmn) {
  1031. *info = -24;
  1032. } else if (*ldv < *n - 1) {
  1033. *info = -26;
  1034. } else if (*lds < *n - 1) {
  1035. *info = -28;
  1036. }
  1037. if (wntvec || wntvcf || wntvcq) {
  1038. *(unsigned char *)jobvl = 'V';
  1039. } else {
  1040. *(unsigned char *)jobvl = 'N';
  1041. }
  1042. if (*info == 0) {
  1043. /* Compute the minimal and the optimal workspace */
  1044. /* requirements. Simulate running the code and */
  1045. /* determine minimal and optimal sizes of the */
  1046. /* workspace at any moment of the run. */
  1047. if (*n == 0 || *n == 1) {
  1048. /* All output except K is void. INFO=1 signals */
  1049. /* the void input. In case of a workspace query, */
  1050. /* the minimal workspace lengths are returned. */
  1051. if (lquery) {
  1052. iwork[1] = 1;
  1053. zwork[1].r = 2., zwork[1].i = 0.;
  1054. zwork[2].r = 2., zwork[2].i = 0.;
  1055. work[1] = 2.;
  1056. work[2] = 2.;
  1057. } else {
  1058. *k = 0;
  1059. }
  1060. *info = 1;
  1061. return 0;
  1062. }
  1063. mlrwrk = 2;
  1064. mlwork = 2;
  1065. olwork = 2;
  1066. iminwr = 1;
  1067. mlwqr = f2cmax(1,*n);
  1068. /* Minimal workspace length for ZGEQRF. */
  1069. /* Computing MAX */
  1070. i__1 = mlwork, i__2 = minmn + mlwqr;
  1071. mlwork = f2cmax(i__1,i__2);
  1072. if (lquery) {
  1073. zgeqrf_(m, n, &f[f_offset], ldf, &zwork[1], &zwork[1], &c_n1, &
  1074. info1);
  1075. olwqr = (integer) zwork[1].r;
  1076. /* Computing MAX */
  1077. i__1 = olwork, i__2 = minmn + olwqr;
  1078. olwork = f2cmax(i__1,i__2);
  1079. }
  1080. i__1 = *n - 1;
  1081. zgedmd_(jobs, jobvl, jobr, jobf, whtsvd, &minmn, &i__1, &x[x_offset],
  1082. ldx, &y[y_offset], ldy, nrnk, tol, k, &eigs[1], &z__[z_offset]
  1083. , ldz, &res[1], &b[b_offset], ldb, &v[v_offset], ldv, &s[
  1084. s_offset], lds, &zwork[1], &c_n1, &work[1], &c_n1, &iwork[1],
  1085. &c_n1, &info1);
  1086. mlwdmd = (integer) zwork[1].r;
  1087. /* Computing MAX */
  1088. i__1 = mlwork, i__2 = minmn + mlwdmd;
  1089. mlwork = f2cmax(i__1,i__2);
  1090. /* Computing MAX */
  1091. i__1 = mlrwrk, i__2 = (integer) work[1];
  1092. mlrwrk = f2cmax(i__1,i__2);
  1093. iminwr = f2cmax(iminwr,iwork[1]);
  1094. if (lquery) {
  1095. olwdmd = (integer) zwork[2].r;
  1096. /* Computing MAX */
  1097. i__1 = olwork, i__2 = minmn + olwdmd;
  1098. olwork = f2cmax(i__1,i__2);
  1099. }
  1100. if (wntvec || wntvcf) {
  1101. mlwmqr = f2cmax(1,*n);
  1102. /* Computing MAX */
  1103. i__1 = mlwork, i__2 = minmn + mlwmqr;
  1104. mlwork = f2cmax(i__1,i__2);
  1105. if (lquery) {
  1106. zunmqr_("L", "N", m, n, &minmn, &f[f_offset], ldf, &zwork[1],
  1107. &z__[z_offset], ldz, &zwork[1], &c_n1, &info1);
  1108. olwmqr = (integer) zwork[1].r;
  1109. /* Computing MAX */
  1110. i__1 = olwork, i__2 = minmn + olwmqr;
  1111. olwork = f2cmax(i__1,i__2);
  1112. }
  1113. }
  1114. if (wantq) {
  1115. mlwgqr = f2cmax(1,*n);
  1116. /* Computing MAX */
  1117. i__1 = mlwork, i__2 = minmn + mlwgqr;
  1118. mlwork = f2cmax(i__1,i__2);
  1119. if (lquery) {
  1120. zungqr_(m, &minmn, &minmn, &f[f_offset], ldf, &zwork[1], &
  1121. zwork[1], &c_n1, &info1);
  1122. olwgqr = (integer) zwork[1].r;
  1123. /* Computing MAX */
  1124. i__1 = olwork, i__2 = minmn + olwgqr;
  1125. olwork = f2cmax(i__1,i__2);
  1126. }
  1127. }
  1128. if (*liwork < iminwr && ! lquery) {
  1129. *info = -34;
  1130. }
  1131. if (*lwork < mlrwrk && ! lquery) {
  1132. *info = -32;
  1133. }
  1134. if (*lzwork < mlwork && ! lquery) {
  1135. *info = -30;
  1136. }
  1137. }
  1138. if (*info != 0) {
  1139. i__1 = -(*info);
  1140. xerbla_("ZGEDMDQ", &i__1);
  1141. return 0;
  1142. } else if (lquery) {
  1143. /* Return minimal and optimal workspace sizes */
  1144. iwork[1] = iminwr;
  1145. zwork[1].r = (doublereal) mlwork, zwork[1].i = 0.;
  1146. zwork[2].r = (doublereal) olwork, zwork[2].i = 0.;
  1147. work[1] = (doublereal) mlrwrk;
  1148. work[2] = (doublereal) mlrwrk;
  1149. return 0;
  1150. }
  1151. /* ..... */
  1152. /* Initial QR factorization that is used to represent the */
  1153. /* snapshots as elements of lower dimensional subspace. */
  1154. /* For large scale computation with M >> N, at this place */
  1155. /* one can use an out of core QRF. */
  1156. i__1 = *lzwork - minmn;
  1157. zgeqrf_(m, n, &f[f_offset], ldf, &zwork[1], &zwork[minmn + 1], &i__1, &
  1158. info1);
  1159. /* Define X and Y as the snapshots representations in the */
  1160. /* orthogonal basis computed in the QR factorization. */
  1161. /* X corresponds to the leading N-1 and Y to the trailing */
  1162. /* N-1 snapshots. */
  1163. i__1 = *n - 1;
  1164. zlaset_("L", &minmn, &i__1, &zzero, &zzero, &x[x_offset], ldx);
  1165. i__1 = *n - 1;
  1166. zlacpy_("U", &minmn, &i__1, &f[f_offset], ldf, &x[x_offset], ldx);
  1167. i__1 = *n - 1;
  1168. zlacpy_("A", &minmn, &i__1, &f[(f_dim1 << 1) + 1], ldf, &y[y_offset], ldy);
  1169. if (*m >= 3) {
  1170. i__1 = minmn - 2;
  1171. i__2 = *n - 2;
  1172. zlaset_("L", &i__1, &i__2, &zzero, &zzero, &y[y_dim1 + 3], ldy);
  1173. }
  1174. /* Compute the DMD of the projected snapshot pairs (X,Y) */
  1175. i__1 = *n - 1;
  1176. i__2 = *lzwork - minmn;
  1177. zgedmd_(jobs, jobvl, jobr, jobf, whtsvd, &minmn, &i__1, &x[x_offset], ldx,
  1178. &y[y_offset], ldy, nrnk, tol, k, &eigs[1], &z__[z_offset], ldz, &
  1179. res[1], &b[b_offset], ldb, &v[v_offset], ldv, &s[s_offset], lds, &
  1180. zwork[minmn + 1], &i__2, &work[1], lwork, &iwork[1], liwork, &
  1181. info1);
  1182. if (info1 == 2 || info1 == 3) {
  1183. /* Return with error code. See ZGEDMD for details. */
  1184. *info = info1;
  1185. return 0;
  1186. } else {
  1187. *info = info1;
  1188. }
  1189. /* The Ritz vectors (Koopman modes) can be explicitly */
  1190. /* formed or returned in factored form. */
  1191. if (wntvec) {
  1192. /* Compute the eigenvectors explicitly. */
  1193. if (*m > minmn) {
  1194. i__1 = *m - minmn;
  1195. zlaset_("A", &i__1, k, &zzero, &zzero, &z__[minmn + 1 + z_dim1],
  1196. ldz);
  1197. }
  1198. i__1 = *lzwork - minmn;
  1199. zunmqr_("L", "N", m, k, &minmn, &f[f_offset], ldf, &zwork[1], &z__[
  1200. z_offset], ldz, &zwork[minmn + 1], &i__1, &info1);
  1201. } else if (wntvcf) {
  1202. /* Return the Ritz vectors (eigenvectors) in factored */
  1203. /* form Z*V, where Z contains orthonormal matrix (the */
  1204. /* product of Q from the initial QR factorization and */
  1205. /* the SVD/POD_basis returned by ZGEDMD in X) and the */
  1206. /* second factor (the eigenvectors of the Rayleigh */
  1207. /* quotient) is in the array V, as returned by ZGEDMD. */
  1208. zlacpy_("A", n, k, &x[x_offset], ldx, &z__[z_offset], ldz);
  1209. if (*m > *n) {
  1210. i__1 = *m - *n;
  1211. zlaset_("A", &i__1, k, &zzero, &zzero, &z__[*n + 1 + z_dim1], ldz);
  1212. }
  1213. i__1 = *lzwork - minmn;
  1214. zunmqr_("L", "N", m, k, &minmn, &f[f_offset], ldf, &zwork[1], &z__[
  1215. z_offset], ldz, &zwork[minmn + 1], &i__1, &info1);
  1216. }
  1217. /* Some optional output variables: */
  1218. /* The upper triangular factor R in the initial QR */
  1219. /* factorization is optionally returned in the array Y. */
  1220. /* This is useful if this call to ZGEDMDQ is to be */
  1221. /* followed by a streaming DMD that is implemented in a */
  1222. /* QR compressed form. */
  1223. if (wnttrf) {
  1224. /* Return the upper triangular R in Y */
  1225. zlaset_("A", &minmn, n, &zzero, &zzero, &y[y_offset], ldy);
  1226. zlacpy_("U", &minmn, n, &f[f_offset], ldf, &y[y_offset], ldy);
  1227. }
  1228. /* The orthonormal/unitary factor Q in the initial QR */
  1229. /* factorization is optionally returned in the array F. */
  1230. /* Same as with the triangular factor above, this is */
  1231. /* useful in a streaming DMD. */
  1232. if (wantq) {
  1233. /* Q overwrites F */
  1234. i__1 = *lzwork - minmn;
  1235. zungqr_(m, &minmn, &minmn, &f[f_offset], ldf, &zwork[1], &zwork[minmn
  1236. + 1], &i__1, &info1);
  1237. }
  1238. return 0;
  1239. } /* zgedmdq_ */