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