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.

cgedmdq.c 46 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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 cgedmdq_(char *jobs, char *jobz, char *jobr, char *jobq,
  490. char *jobt, char *jobf, integer *whtsvd, integer *m, integer *n,
  491. complex *f, integer *ldf, complex *x, integer *ldx, complex *y,
  492. integer *ldy, integer *nrnk, real *tol, integer *k, complex *eigs,
  493. complex *z__, integer *ldz, real *res, complex *b, integer *ldb,
  494. complex *v, integer *ldv, complex *s, integer *lds, complex *zwork,
  495. integer *lzwork, real *work, integer *lwork, integer *iwork, integer *
  496. liwork, integer *info)
  497. {
  498. /* System generated locals */
  499. integer f_dim1, f_offset, x_dim1, x_offset, y_dim1, y_offset, z_dim1,
  500. z_offset, b_dim1, b_offset, v_dim1, v_offset, s_dim1, s_offset,
  501. i__1, i__2;
  502. /* Local variables */
  503. real zero;
  504. integer info1;
  505. extern logical lsame_(char *, char *);
  506. char jobvl[1];
  507. integer minmn;
  508. logical wantq;
  509. integer mlwqr, olwqr;
  510. logical wntex;
  511. complex zzero;
  512. extern /* Subroutine */ int cgedmd_(char *, char *, char *, char *,
  513. integer *, integer *, integer *, complex *, integer *, complex *,
  514. integer *, integer *, real *, integer *, complex *, complex *,
  515. integer *, real *, complex *, integer *, complex *, integer *,
  516. complex *, integer *, complex *, integer *, real *, integer *,
  517. integer *, integer *, integer *),
  518. cgeqrf_(integer *, integer *, complex *, integer *, complex *,
  519. complex *, integer *, integer *), clacpy_(char *, integer *,
  520. integer *, complex *, integer *, complex *, integer *),
  521. claset_(char *, integer *, integer *, complex *, complex *,
  522. complex *, integer *), xerbla_(char *, integer *);
  523. integer mlwdmd, olwdmd;
  524. logical sccolx, sccoly;
  525. extern /* Subroutine */ int cungqr_(integer *, integer *, integer *,
  526. complex *, integer *, complex *, complex *, integer *, integer *);
  527. integer iminwr;
  528. logical wntvec, wntvcf;
  529. integer mlwgqr;
  530. logical wntref;
  531. integer mlwork, olwgqr, olwork, mlrwrk, mlwmqr, olwmqr;
  532. logical lquery, wntres, wnttrf, wntvcq;
  533. extern /* Subroutine */ int cunmqr_(char *, char *, integer *, integer *,
  534. integer *, complex *, integer *, complex *, complex *, integer *,
  535. complex *, integer *, integer *);
  536. real one;
  537. /* March 2023 */
  538. /* ..... */
  539. /* USE iso_fortran_env */
  540. /* INTEGER, PARAMETER :: WP = real32 */
  541. /* ..... */
  542. /* Scalar arguments */
  543. /* Array arguments */
  544. /* ..... */
  545. /* Purpose */
  546. /* ======= */
  547. /* CGEDMDQ computes the Dynamic Mode Decomposition (DMD) for */
  548. /* a pair of data snapshot matrices, using a QR factorization */
  549. /* based compression of the data. For the input matrices */
  550. /* X and Y such that Y = A*X with an unaccessible matrix */
  551. /* A, CGEDMDQ computes a certain number of Ritz pairs of A using */
  552. /* the standard Rayleigh-Ritz extraction from a subspace of */
  553. /* range(X) that is determined using the leading left singular */
  554. /* vectors of X. Optionally, CGEDMDQ returns the residuals */
  555. /* of the computed Ritz pairs, the information needed for */
  556. /* a refinement of the Ritz vectors, or the eigenvectors of */
  557. /* the Exact DMD. */
  558. /* For further details see the references listed */
  559. /* below. For more details of the implementation see [3]. */
  560. /* References */
  561. /* ========== */
  562. /* [1] P. Schmid: Dynamic mode decomposition of numerical */
  563. /* and experimental data, */
  564. /* Journal of Fluid Mechanics 656, 5-28, 2010. */
  565. /* [2] Z. Drmac, I. Mezic, R. Mohr: Data driven modal */
  566. /* decompositions: analysis and enhancements, */
  567. /* SIAM J. on Sci. Comp. 40 (4), A2253-A2285, 2018. */
  568. /* [3] Z. Drmac: A LAPACK implementation of the Dynamic */
  569. /* Mode Decomposition I. Technical report. AIMDyn Inc. */
  570. /* and LAPACK Working Note 298. */
  571. /* [4] J. Tu, C. W. Rowley, D. M. Luchtenburg, S. L. */
  572. /* Brunton, N. Kutz: On Dynamic Mode Decomposition: */
  573. /* Theory and Applications, Journal of Computational */
  574. /* Dynamics 1(2), 391 -421, 2014. */
  575. /* Developed and supported by: */
  576. /* =========================== */
  577. /* Developed and coded by Zlatko Drmac, Faculty of Science, */
  578. /* University of Zagreb; drmac@math.hr */
  579. /* In cooperation with */
  580. /* AIMdyn Inc., Santa Barbara, CA. */
  581. /* and supported by */
  582. /* - DARPA SBIR project "Koopman Operator-Based Forecasting */
  583. /* for Nonstationary Processes from Near-Term, Limited */
  584. /* Observational Data" Contract No: W31P4Q-21-C-0007 */
  585. /* - DARPA PAI project "Physics-Informed Machine Learning */
  586. /* Methodologies" Contract No: HR0011-18-9-0033 */
  587. /* - DARPA MoDyL project "A Data-Driven, Operator-Theoretic */
  588. /* Framework for Space-Time Analysis of Process Dynamics" */
  589. /* Contract No: HR0011-16-C-0116 */
  590. /* Any opinions, findings and conclusions or recommendations */
  591. /* expressed in this material are those of the author and */
  592. /* do not necessarily reflect the views of the DARPA SBIR */
  593. /* Program Office. */
  594. /* ============================================================ */
  595. /* Distribution Statement A: */
  596. /* Approved for Public Release, Distribution Unlimited. */
  597. /* Cleared by DARPA on September 29, 2022 */
  598. /* ============================================================ */
  599. /* ...................................................................... */
  600. /* Arguments */
  601. /* ========= */
  602. /* JOBS (input) CHARACTER*1 */
  603. /* Determines whether the initial data snapshots are scaled */
  604. /* by a diagonal matrix. The data snapshots are the columns */
  605. /* of F. The leading N-1 columns of F are denoted X and the */
  606. /* trailing N-1 columns are denoted Y. */
  607. /* 'S' :: The data snapshots matrices X and Y are multiplied */
  608. /* with a diagonal matrix D so that X*D has unit */
  609. /* nonzero columns (in the Euclidean 2-norm) */
  610. /* 'C' :: The snapshots are scaled as with the 'S' option. */
  611. /* If it is found that an i-th column of X is zero */
  612. /* vector and the corresponding i-th column of Y is */
  613. /* non-zero, then the i-th column of Y is set to */
  614. /* zero and a warning flag is raised. */
  615. /* 'Y' :: The data snapshots matrices X and Y are multiplied */
  616. /* by a diagonal matrix D so that Y*D has unit */
  617. /* nonzero columns (in the Euclidean 2-norm) */
  618. /* 'N' :: No data scaling. */
  619. /* ..... */
  620. /* JOBZ (input) CHARACTER*1 */
  621. /* Determines whether the eigenvectors (Koopman modes) will */
  622. /* be computed. */
  623. /* 'V' :: The eigenvectors (Koopman modes) will be computed */
  624. /* and returned in the matrix Z. */
  625. /* See the description of Z. */
  626. /* 'F' :: The eigenvectors (Koopman modes) will be returned */
  627. /* in factored form as the product Z*V, where Z */
  628. /* is orthonormal and V contains the eigenvectors */
  629. /* of the corresponding Rayleigh quotient. */
  630. /* See the descriptions of F, V, Z. */
  631. /* 'Q' :: The eigenvectors (Koopman modes) will be returned */
  632. /* in factored form as the product Q*Z, where Z */
  633. /* contains the eigenvectors of the compression of the */
  634. /* underlying discretised operator onto the span of */
  635. /* the data snapshots. See the descriptions of F, V, Z. */
  636. /* Q is from the inital QR facorization. */
  637. /* 'N' :: The eigenvectors are not computed. */
  638. /* ..... */
  639. /* JOBR (input) CHARACTER*1 */
  640. /* Determines whether to compute the residuals. */
  641. /* 'R' :: The residuals for the computed eigenpairs will */
  642. /* be computed and stored in the array RES. */
  643. /* See the description of RES. */
  644. /* For this option to be legal, JOBZ must be 'V'. */
  645. /* 'N' :: The residuals are not computed. */
  646. /* ..... */
  647. /* JOBQ (input) CHARACTER*1 */
  648. /* Specifies whether to explicitly compute and return the */
  649. /* unitary matrix from the QR factorization. */
  650. /* 'Q' :: The matrix Q of the QR factorization of the data */
  651. /* snapshot matrix is computed and stored in the */
  652. /* array F. See the description of F. */
  653. /* 'N' :: The matrix Q is not explicitly computed. */
  654. /* ..... */
  655. /* JOBT (input) CHARACTER*1 */
  656. /* Specifies whether to return the upper triangular factor */
  657. /* from the QR factorization. */
  658. /* 'R' :: The matrix R of the QR factorization of the data */
  659. /* snapshot matrix F is returned in the array Y. */
  660. /* See the description of Y and Further details. */
  661. /* 'N' :: The matrix R is not returned. */
  662. /* ..... */
  663. /* JOBF (input) CHARACTER*1 */
  664. /* Specifies whether to store information needed for post- */
  665. /* processing (e.g. computing refined Ritz vectors) */
  666. /* 'R' :: The matrix needed for the refinement of the Ritz */
  667. /* vectors is computed and stored in the array B. */
  668. /* See the description of B. */
  669. /* 'E' :: The unscaled eigenvectors of the Exact DMD are */
  670. /* computed and returned in the array B. See the */
  671. /* description of B. */
  672. /* 'N' :: No eigenvector refinement data is computed. */
  673. /* To be useful on exit, this option needs JOBQ='Q'. */
  674. /* ..... */
  675. /* WHTSVD (input) INTEGER, WHSTVD in { 1, 2, 3, 4 } */
  676. /* Allows for a selection of the SVD algorithm from the */
  677. /* LAPACK library. */
  678. /* 1 :: CGESVD (the QR SVD algorithm) */
  679. /* 2 :: CGESDD (the Divide and Conquer algorithm; if enough */
  680. /* workspace available, this is the fastest option) */
  681. /* 3 :: CGESVDQ (the preconditioned QR SVD ; this and 4 */
  682. /* are the most accurate options) */
  683. /* 4 :: CGEJSV (the preconditioned Jacobi SVD; this and 3 */
  684. /* are the most accurate options) */
  685. /* For the four methods above, a significant difference in */
  686. /* the accuracy of small singular values is possible if */
  687. /* the snapshots vary in norm so that X is severely */
  688. /* ill-conditioned. If small (smaller than EPS*||X||) */
  689. /* singular values are of interest and JOBS=='N', then */
  690. /* the options (3, 4) give the most accurate results, where */
  691. /* the option 4 is slightly better and with stronger */
  692. /* theoretical background. */
  693. /* If JOBS=='S', i.e. the columns of X will be normalized, */
  694. /* then all methods give nearly equally accurate results. */
  695. /* ..... */
  696. /* M (input) INTEGER, M >= 0 */
  697. /* The state space dimension (the number of rows of F). */
  698. /* ..... */
  699. /* N (input) INTEGER, 0 <= N <= M */
  700. /* The number of data snapshots from a single trajectory, */
  701. /* taken at equidistant discrete times. This is the */
  702. /* number of columns of F. */
  703. /* ..... */
  704. /* F (input/output) COMPLEX(KIND=WP) M-by-N array */
  705. /* > On entry, */
  706. /* the columns of F are the sequence of data snapshots */
  707. /* from a single trajectory, taken at equidistant discrete */
  708. /* times. It is assumed that the column norms of F are */
  709. /* in the range of the normalized floating point numbers. */
  710. /* < On exit, */
  711. /* If JOBQ == 'Q', the array F contains the orthogonal */
  712. /* matrix/factor of the QR factorization of the initial */
  713. /* data snapshots matrix F. See the description of JOBQ. */
  714. /* If JOBQ == 'N', the entries in F strictly below the main */
  715. /* diagonal contain, column-wise, the information on the */
  716. /* Householder vectors, as returned by CGEQRF. The */
  717. /* remaining information to restore the orthogonal matrix */
  718. /* of the initial QR factorization is stored in ZWORK(1:MIN(M,N)). */
  719. /* See the description of ZWORK. */
  720. /* ..... */
  721. /* LDF (input) INTEGER, LDF >= M */
  722. /* The leading dimension of the array F. */
  723. /* ..... */
  724. /* X (workspace/output) COMPLEX(KIND=WP) MIN(M,N)-by-(N-1) array */
  725. /* X is used as workspace to hold representations of the */
  726. /* leading N-1 snapshots in the orthonormal basis computed */
  727. /* in the QR factorization of F. */
  728. /* On exit, the leading K columns of X contain the leading */
  729. /* K left singular vectors of the above described content */
  730. /* of X. To lift them to the space of the left singular */
  731. /* vectors U(:,1:K) of the input data, pre-multiply with the */
  732. /* Q factor from the initial QR factorization. */
  733. /* See the descriptions of F, K, V and Z. */
  734. /* ..... */
  735. /* LDX (input) INTEGER, LDX >= N */
  736. /* The leading dimension of the array X. */
  737. /* ..... */
  738. /* Y (workspace/output) COMPLEX(KIND=WP) MIN(M,N)-by-(N) array */
  739. /* Y is used as workspace to hold representations of the */
  740. /* trailing N-1 snapshots in the orthonormal basis computed */
  741. /* in the QR factorization of F. */
  742. /* On exit, */
  743. /* If JOBT == 'R', Y contains the MIN(M,N)-by-N upper */
  744. /* triangular factor from the QR factorization of the data */
  745. /* snapshot matrix F. */
  746. /* ..... */
  747. /* LDY (input) INTEGER , LDY >= N */
  748. /* The leading dimension of the array Y. */
  749. /* ..... */
  750. /* NRNK (input) INTEGER */
  751. /* Determines the mode how to compute the numerical rank, */
  752. /* i.e. how to truncate small singular values of the input */
  753. /* matrix X. On input, if */
  754. /* NRNK = -1 :: i-th singular value sigma(i) is truncated */
  755. /* if sigma(i) <= TOL*sigma(1) */
  756. /* This option is recommended. */
  757. /* NRNK = -2 :: i-th singular value sigma(i) is truncated */
  758. /* if sigma(i) <= TOL*sigma(i-1) */
  759. /* This option is included for R&D purposes. */
  760. /* It requires highly accurate SVD, which */
  761. /* may not be feasible. */
  762. /* The numerical rank can be enforced by using positive */
  763. /* value of NRNK as follows: */
  764. /* 0 < NRNK <= N-1 :: at most NRNK largest singular values */
  765. /* will be used. If the number of the computed nonzero */
  766. /* singular values is less than NRNK, then only those */
  767. /* nonzero values will be used and the actually used */
  768. /* dimension is less than NRNK. The actual number of */
  769. /* the nonzero singular values is returned in the variable */
  770. /* K. See the description of K. */
  771. /* ..... */
  772. /* TOL (input) REAL(KIND=WP), 0 <= TOL < 1 */
  773. /* The tolerance for truncating small singular values. */
  774. /* See the description of NRNK. */
  775. /* ..... */
  776. /* K (output) INTEGER, 0 <= K <= N */
  777. /* The dimension of the SVD/POD basis for the leading N-1 */
  778. /* data snapshots (columns of F) and the number of the */
  779. /* computed Ritz pairs. The value of K is determined */
  780. /* according to the rule set by the parameters NRNK and */
  781. /* TOL. See the descriptions of NRNK and TOL. */
  782. /* ..... */
  783. /* EIGS (output) COMPLEX(KIND=WP) (N-1)-by-1 array */
  784. /* The leading K (K<=N-1) entries of EIGS contain */
  785. /* the computed eigenvalues (Ritz values). */
  786. /* See the descriptions of K, and Z. */
  787. /* ..... */
  788. /* Z (workspace/output) COMPLEX(KIND=WP) M-by-(N-1) array */
  789. /* If JOBZ =='V' then Z contains the Ritz vectors. Z(:,i) */
  790. /* is an eigenvector of the i-th Ritz value; ||Z(:,i)||_2=1. */
  791. /* If JOBZ == 'F', then the Z(:,i)'s are given implicitly as */
  792. /* Z*V, where Z contains orthonormal matrix (the product of */
  793. /* Q from the initial QR factorization and the SVD/POD_basis */
  794. /* returned by CGEDMD in X) and the second factor (the */
  795. /* eigenvectors of the Rayleigh quotient) is in the array V, */
  796. /* as returned by CGEDMD. That is, X(:,1:K)*V(:,i) */
  797. /* is an eigenvector corresponding to EIGS(i). The columns */
  798. /* of V(1:K,1:K) are the computed eigenvectors of the */
  799. /* K-by-K Rayleigh quotient. */
  800. /* See the descriptions of EIGS, X and V. */
  801. /* ..... */
  802. /* LDZ (input) INTEGER , LDZ >= M */
  803. /* The leading dimension of the array Z. */
  804. /* ..... */
  805. /* RES (output) REAL(KIND=WP) (N-1)-by-1 array */
  806. /* RES(1:K) contains the residuals for the K computed */
  807. /* Ritz pairs, */
  808. /* RES(i) = || A * Z(:,i) - EIGS(i)*Z(:,i))||_2. */
  809. /* See the description of EIGS and Z. */
  810. /* ..... */
  811. /* B (output) COMPLEX(KIND=WP) MIN(M,N)-by-(N-1) array. */
  812. /* IF JOBF =='R', B(1:N,1:K) contains A*U(:,1:K), and can */
  813. /* be used for computing the refined vectors; see further */
  814. /* details in the provided references. */
  815. /* If JOBF == 'E', B(1:N,1;K) contains */
  816. /* A*U(:,1:K)*W(1:K,1:K), which are the vectors from the */
  817. /* Exact DMD, up to scaling by the inverse eigenvalues. */
  818. /* In both cases, the content of B can be lifted to the */
  819. /* original dimension of the input data by pre-multiplying */
  820. /* with the Q factor from the initial QR factorization. */
  821. /* Here A denotes a compression of the underlying operator. */
  822. /* See the descriptions of F and X. */
  823. /* If JOBF =='N', then B is not referenced. */
  824. /* ..... */
  825. /* LDB (input) INTEGER, LDB >= MIN(M,N) */
  826. /* The leading dimension of the array B. */
  827. /* ..... */
  828. /* V (workspace/output) COMPLEX(KIND=WP) (N-1)-by-(N-1) array */
  829. /* On exit, V(1:K,1:K) V contains the K eigenvectors of */
  830. /* the Rayleigh quotient. The Ritz vectors */
  831. /* (returned in Z) are the product of Q from the initial QR */
  832. /* factorization (see the description of F) X (see the */
  833. /* description of X) and V. */
  834. /* ..... */
  835. /* LDV (input) INTEGER, LDV >= N-1 */
  836. /* The leading dimension of the array V. */
  837. /* ..... */
  838. /* S (output) COMPLEX(KIND=WP) (N-1)-by-(N-1) array */
  839. /* The array S(1:K,1:K) is used for the matrix Rayleigh */
  840. /* quotient. This content is overwritten during */
  841. /* the eigenvalue decomposition by CGEEV. */
  842. /* See the description of K. */
  843. /* ..... */
  844. /* LDS (input) INTEGER, LDS >= N-1 */
  845. /* The leading dimension of the array S. */
  846. /* ..... */
  847. /* ZWORK (workspace/output) COMPLEX(KIND=WP) LWORK-by-1 array */
  848. /* On exit, */
  849. /* ZWORK(1:MIN(M,N)) contains the scalar factors of the */
  850. /* elementary reflectors as returned by CGEQRF of the */
  851. /* M-by-N input matrix F. */
  852. /* If the call to CGEDMDQ is only workspace query, then */
  853. /* ZWORK(1) contains the minimal complex workspace length and */
  854. /* ZWORK(2) is the optimal complex workspace length. */
  855. /* Hence, the length of work is at least 2. */
  856. /* See the description of LZWORK. */
  857. /* ..... */
  858. /* LZWORK (input) INTEGER */
  859. /* The minimal length of the workspace vector ZWORK. */
  860. /* LZWORK is calculated as follows: */
  861. /* Let MLWQR = N (minimal workspace for CGEQRF[M,N]) */
  862. /* MLWDMD = minimal workspace for CGEDMD (see the */
  863. /* description of LWORK in CGEDMD) */
  864. /* MLWMQR = N (minimal workspace for */
  865. /* ZUNMQR['L','N',M,N,N]) */
  866. /* MLWGQR = N (minimal workspace for ZUNGQR[M,N,N]) */
  867. /* MINMN = MIN(M,N) */
  868. /* Then */
  869. /* LZWORK = MAX(2, MIN(M,N)+MLWQR, MINMN+MLWDMD) */
  870. /* is further updated as follows: */
  871. /* if JOBZ == 'V' or JOBZ == 'F' THEN */
  872. /* LZWORK = MAX( LZWORK, MINMN+MLWMQR ) */
  873. /* if JOBQ == 'Q' THEN */
  874. /* LZWORK = MAX( ZLWORK, MINMN+MLWGQR) */
  875. /* ..... */
  876. /* WORK (workspace/output) REAL(KIND=WP) LWORK-by-1 array */
  877. /* On exit, */
  878. /* WORK(1:N-1) contains the singular values of */
  879. /* the input submatrix F(1:M,1:N-1). */
  880. /* If the call to CGEDMDQ is only workspace query, then */
  881. /* WORK(1) contains the minimal workspace length and */
  882. /* WORK(2) is the optimal workspace length. hence, the */
  883. /* length of work is at least 2. */
  884. /* See the description of LWORK. */
  885. /* ..... */
  886. /* LWORK (input) INTEGER */
  887. /* The minimal length of the workspace vector WORK. */
  888. /* LWORK is the same as in CGEDMD, because in CGEDMDQ */
  889. /* only CGEDMD requires real workspace for snapshots */
  890. /* of dimensions MIN(M,N)-by-(N-1). */
  891. /* If on entry LWORK = -1, then a workspace query is */
  892. /* assumed and the procedure only computes the minimal */
  893. /* and the optimal workspace lengths for both WORK and */
  894. /* IWORK. See the descriptions of WORK and IWORK. */
  895. /* ..... */
  896. /* IWORK (workspace/output) INTEGER LIWORK-by-1 array */
  897. /* Workspace that is required only if WHTSVD equals */
  898. /* 2 , 3 or 4. (See the description of WHTSVD). */
  899. /* If on entry LWORK =-1 or LIWORK=-1, then the */
  900. /* minimal length of IWORK is computed and returned in */
  901. /* IWORK(1). See the description of LIWORK. */
  902. /* ..... */
  903. /* LIWORK (input) INTEGER */
  904. /* The minimal length of the workspace vector IWORK. */
  905. /* If WHTSVD == 1, then only IWORK(1) is used; LIWORK >=1 */
  906. /* Let M1=MIN(M,N), N1=N-1. Then */
  907. /* If WHTSVD == 2, then LIWORK >= MAX(1,8*MIN(M,N)) */
  908. /* If WHTSVD == 3, then LIWORK >= MAX(1,M+N-1) */
  909. /* If WHTSVD == 4, then LIWORK >= MAX(3,M+3*N) */
  910. /* If on entry LIWORK = -1, then a workspace query is */
  911. /* assumed and the procedure only computes the minimal */
  912. /* and the optimal workspace lengths for both WORK and */
  913. /* IWORK. See the descriptions of WORK and IWORK. */
  914. /* ..... */
  915. /* INFO (output) INTEGER */
  916. /* -i < 0 :: On entry, the i-th argument had an */
  917. /* illegal value */
  918. /* = 0 :: Successful return. */
  919. /* = 1 :: Void input. Quick exit (M=0 or N=0). */
  920. /* = 2 :: The SVD computation of X did not converge. */
  921. /* Suggestion: Check the input data and/or */
  922. /* repeat with different WHTSVD. */
  923. /* = 3 :: The computation of the eigenvalues did not */
  924. /* converge. */
  925. /* = 4 :: If data scaling was requested on input and */
  926. /* the procedure found inconsistency in the data */
  927. /* such that for some column index i, */
  928. /* X(:,i) = 0 but Y(:,i) /= 0, then Y(:,i) is set */
  929. /* to zero if JOBS=='C'. The computation proceeds */
  930. /* with original or modified data and warning */
  931. /* flag is set with INFO=4. */
  932. /* ............................................................. */
  933. /* ............................................................. */
  934. /* Parameters */
  935. /* ~~~~~~~~~~ */
  936. /* COMPLEX(KIND=WP), PARAMETER :: ZONE = ( 1.0_WP, 0.0_WP ) */
  937. /* Local scalars */
  938. /* ~~~~~~~~~~~~~ */
  939. /* External functions (BLAS and LAPACK) */
  940. /* ~~~~~~~~~~~~~~~~~ */
  941. /* External subroutines (BLAS and LAPACK) */
  942. /* ~~~~~~~~~~~~~~~~~~~~ */
  943. /* External subroutines */
  944. /* ~~~~~~~~~~~~~~~~~~~~ */
  945. /* Intrinsic functions */
  946. /* ~~~~~~~~~~~~~~~~~~~ */
  947. /* .......................................................... */
  948. /* Parameter adjustments */
  949. f_dim1 = *ldf;
  950. f_offset = 1 + f_dim1 * 1;
  951. f -= f_offset;
  952. x_dim1 = *ldx;
  953. x_offset = 1 + x_dim1 * 1;
  954. x -= x_offset;
  955. y_dim1 = *ldy;
  956. y_offset = 1 + y_dim1 * 1;
  957. y -= y_offset;
  958. --eigs;
  959. z_dim1 = *ldz;
  960. z_offset = 1 + z_dim1 * 1;
  961. z__ -= z_offset;
  962. --res;
  963. b_dim1 = *ldb;
  964. b_offset = 1 + b_dim1 * 1;
  965. b -= b_offset;
  966. v_dim1 = *ldv;
  967. v_offset = 1 + v_dim1 * 1;
  968. v -= v_offset;
  969. s_dim1 = *lds;
  970. s_offset = 1 + s_dim1 * 1;
  971. s -= s_offset;
  972. --zwork;
  973. --work;
  974. --iwork;
  975. /* Function Body */
  976. one = 1.f;
  977. zero = 0.f;
  978. zzero.r = 0.f, zzero.i = 0.f;
  979. /* Test the input arguments */
  980. wntres = lsame_(jobr, "R");
  981. sccolx = lsame_(jobs, "S") || lsame_(jobs, "C");
  982. sccoly = lsame_(jobs, "Y");
  983. wntvec = lsame_(jobz, "V");
  984. wntvcf = lsame_(jobz, "F");
  985. wntvcq = lsame_(jobz, "Q");
  986. wntref = lsame_(jobf, "R");
  987. wntex = lsame_(jobf, "E");
  988. wantq = lsame_(jobq, "Q");
  989. wnttrf = lsame_(jobt, "R");
  990. minmn = f2cmin(*m,*n);
  991. *info = 0;
  992. lquery = *lwork == -1 || *liwork == -1;
  993. if (! (sccolx || sccoly || lsame_(jobs, "N"))) {
  994. *info = -1;
  995. } else if (! (wntvec || wntvcf || wntvcq || lsame_(jobz, "N"))) {
  996. *info = -2;
  997. } else if (! (wntres || lsame_(jobr, "N")) ||
  998. wntres && lsame_(jobz, "N")) {
  999. *info = -3;
  1000. } else if (! (wantq || lsame_(jobq, "N"))) {
  1001. *info = -4;
  1002. } else if (! (wnttrf || lsame_(jobt, "N"))) {
  1003. *info = -5;
  1004. } else if (! (wntref || wntex || lsame_(jobf, "N")))
  1005. {
  1006. *info = -6;
  1007. } else if (! (*whtsvd == 1 || *whtsvd == 2 || *whtsvd == 3 || *whtsvd ==
  1008. 4)) {
  1009. *info = -7;
  1010. } else if (*m < 0) {
  1011. *info = -8;
  1012. } else if (*n < 0 || *n > *m + 1) {
  1013. *info = -9;
  1014. } else if (*ldf < *m) {
  1015. *info = -11;
  1016. } else if (*ldx < minmn) {
  1017. *info = -13;
  1018. } else if (*ldy < minmn) {
  1019. *info = -15;
  1020. } else if (! (*nrnk == -2 || *nrnk == -1 || *nrnk >= 1 && *nrnk <= *n)) {
  1021. *info = -16;
  1022. } else if (*tol < zero || *tol >= one) {
  1023. *info = -17;
  1024. } else if (*ldz < *m) {
  1025. *info = -21;
  1026. } else if ((wntref || wntex) && *ldb < minmn) {
  1027. *info = -24;
  1028. } else if (*ldv < *n - 1) {
  1029. *info = -26;
  1030. } else if (*lds < *n - 1) {
  1031. *info = -28;
  1032. }
  1033. if (wntvec || wntvcf || wntvcq) {
  1034. *(unsigned char *)jobvl = 'V';
  1035. } else {
  1036. *(unsigned char *)jobvl = 'N';
  1037. }
  1038. if (*info == 0) {
  1039. /* Compute the minimal and the optimal workspace */
  1040. /* requirements. Simulate running the code and */
  1041. /* determine minimal and optimal sizes of the */
  1042. /* workspace at any moment of the run. */
  1043. if (*n == 0 || *n == 1) {
  1044. /* All output except K is void. INFO=1 signals */
  1045. /* the void input. In case of a workspace query, */
  1046. /* the minimal workspace lengths are returned. */
  1047. if (lquery) {
  1048. iwork[1] = 1;
  1049. work[1] = 2.f;
  1050. work[2] = 2.f;
  1051. } else {
  1052. *k = 0;
  1053. }
  1054. *info = 1;
  1055. return 0;
  1056. }
  1057. mlrwrk = 2;
  1058. mlwork = 2;
  1059. olwork = 2;
  1060. iminwr = 1;
  1061. mlwqr = f2cmax(1,*n);
  1062. /* Minimal workspace length for CGEQRF. */
  1063. /* Computing MAX */
  1064. i__1 = mlwork, i__2 = minmn + mlwqr;
  1065. mlwork = f2cmax(i__1,i__2);
  1066. if (lquery) {
  1067. cgeqrf_(m, n, &f[f_offset], ldf, &zwork[1], &zwork[1], &c_n1, &
  1068. info1);
  1069. olwqr = (integer) zwork[1].r;
  1070. /* Computing MAX */
  1071. i__1 = olwork, i__2 = minmn + olwqr;
  1072. olwork = f2cmax(i__1,i__2);
  1073. }
  1074. i__1 = *n - 1;
  1075. cgedmd_(jobs, jobvl, jobr, jobf, whtsvd, &minmn, &i__1, &x[x_offset],
  1076. ldx, &y[y_offset], ldy, nrnk, tol, k, &eigs[1], &z__[z_offset]
  1077. , ldz, &res[1], &b[b_offset], ldb, &v[v_offset], ldv, &s[
  1078. s_offset], lds, &zwork[1], lzwork, &work[1], &c_n1, &iwork[1],
  1079. liwork, &info1);
  1080. mlwdmd = (integer) zwork[1].r;
  1081. /* Computing MAX */
  1082. i__1 = mlwork, i__2 = minmn + mlwdmd;
  1083. mlwork = f2cmax(i__1,i__2);
  1084. /* Computing MAX */
  1085. i__1 = mlrwrk, i__2 = (integer) work[1];
  1086. mlrwrk = f2cmax(i__1,i__2);
  1087. iminwr = f2cmax(iminwr,iwork[1]);
  1088. if (lquery) {
  1089. olwdmd = (integer) zwork[2].r;
  1090. /* Computing MAX */
  1091. i__1 = olwork, i__2 = minmn + olwdmd;
  1092. olwork = f2cmax(i__1,i__2);
  1093. }
  1094. if (wntvec || wntvcf) {
  1095. mlwmqr = f2cmax(1,*n);
  1096. /* Computing MAX */
  1097. i__1 = mlwork, i__2 = minmn + mlwmqr;
  1098. mlwork = f2cmax(i__1,i__2);
  1099. if (lquery) {
  1100. cunmqr_("L", "N", m, n, &minmn, &f[f_offset], ldf, &zwork[1],
  1101. &z__[z_offset], ldz, &zwork[1], &c_n1, &info1);
  1102. olwmqr = (integer) zwork[1].r;
  1103. /* Computing MAX */
  1104. i__1 = olwork, i__2 = minmn + olwmqr;
  1105. olwork = f2cmax(i__1,i__2);
  1106. }
  1107. }
  1108. if (wantq) {
  1109. mlwgqr = f2cmax(1,*n);
  1110. /* Computing MAX */
  1111. i__1 = mlwork, i__2 = minmn + mlwgqr;
  1112. mlwork = f2cmax(i__1,i__2);
  1113. if (lquery) {
  1114. cungqr_(m, &minmn, &minmn, &f[f_offset], ldf, &zwork[1], &
  1115. zwork[1], &c_n1, &info1);
  1116. olwgqr = (integer) zwork[1].r;
  1117. /* Computing MAX */
  1118. i__1 = olwork, i__2 = minmn + olwgqr;
  1119. olwork = f2cmax(i__1,i__2);
  1120. }
  1121. }
  1122. if (*liwork < iminwr && ! lquery) {
  1123. *info = -34;
  1124. }
  1125. if (*lwork < mlrwrk && ! lquery) {
  1126. *info = -32;
  1127. }
  1128. if (*lzwork < mlwork && ! lquery) {
  1129. *info = -30;
  1130. }
  1131. }
  1132. if (*info != 0) {
  1133. i__1 = -(*info);
  1134. xerbla_("CGEDMDQ", &i__1);
  1135. return 0;
  1136. } else if (lquery) {
  1137. /* Return minimal and optimal workspace sizes */
  1138. iwork[1] = iminwr;
  1139. zwork[1].r = (real) mlwork, zwork[1].i = 0.f;
  1140. zwork[2].r = (real) olwork, zwork[2].i = 0.f;
  1141. work[1] = (real) mlrwrk;
  1142. work[2] = (real) mlrwrk;
  1143. return 0;
  1144. }
  1145. /* ..... */
  1146. /* Initial QR factorization that is used to represent the */
  1147. /* snapshots as elements of lower dimensional subspace. */
  1148. /* For large scale computation with M >>N , at this place */
  1149. /* one can use an out of core QRF. */
  1150. i__1 = *lzwork - minmn;
  1151. cgeqrf_(m, n, &f[f_offset], ldf, &zwork[1], &zwork[minmn + 1], &i__1, &
  1152. info1);
  1153. /* Define X and Y as the snapshots representations in the */
  1154. /* orthogonal basis computed in the QR factorization. */
  1155. /* X corresponds to the leading N-1 and Y to the trailing */
  1156. /* N-1 snapshots. */
  1157. i__1 = *n - 1;
  1158. claset_("L", &minmn, &i__1, &zzero, &zzero, &x[x_offset], ldx);
  1159. i__1 = *n - 1;
  1160. clacpy_("U", &minmn, &i__1, &f[f_offset], ldf, &x[x_offset], ldx);
  1161. i__1 = *n - 1;
  1162. clacpy_("A", &minmn, &i__1, &f[(f_dim1 << 1) + 1], ldf, &y[y_offset], ldy);
  1163. if (*m >= 3) {
  1164. i__1 = minmn - 2;
  1165. i__2 = *n - 2;
  1166. claset_("L", &i__1, &i__2, &zzero, &zzero, &y[y_dim1 + 3], ldy);
  1167. }
  1168. /* Compute the DMD of the projected snapshot pairs (X,Y) */
  1169. i__1 = *n - 1;
  1170. i__2 = *lzwork - minmn;
  1171. cgedmd_(jobs, jobvl, jobr, jobf, whtsvd, &minmn, &i__1, &x[x_offset], ldx,
  1172. &y[y_offset], ldy, nrnk, tol, k, &eigs[1], &z__[z_offset], ldz, &
  1173. res[1], &b[b_offset], ldb, &v[v_offset], ldv, &s[s_offset], lds, &
  1174. zwork[minmn + 1], &i__2, &work[1], lwork, &iwork[1], liwork, &
  1175. info1);
  1176. if (info1 == 2 || info1 == 3) {
  1177. /* Return with error code. See CGEDMD for details. */
  1178. *info = info1;
  1179. return 0;
  1180. } else {
  1181. *info = info1;
  1182. }
  1183. /* The Ritz vectors (Koopman modes) can be explicitly */
  1184. /* formed or returned in factored form. */
  1185. if (wntvec) {
  1186. /* Compute the eigenvectors explicitly. */
  1187. if (*m > minmn) {
  1188. i__1 = *m - minmn;
  1189. claset_("A", &i__1, k, &zzero, &zzero, &z__[minmn + 1 + z_dim1],
  1190. ldz);
  1191. }
  1192. i__1 = *lzwork - minmn;
  1193. cunmqr_("L", "N", m, k, &minmn, &f[f_offset], ldf, &zwork[1], &z__[
  1194. z_offset], ldz, &zwork[minmn + 1], &i__1, &info1);
  1195. } else if (wntvcf) {
  1196. /* Return the Ritz vectors (eigenvectors) in factored */
  1197. /* form Z*V, where Z contains orthonormal matrix (the */
  1198. /* product of Q from the initial QR factorization and */
  1199. /* the SVD/POD_basis returned by CGEDMD in X) and the */
  1200. /* second factor (the eigenvectors of the Rayleigh */
  1201. /* quotient) is in the array V, as returned by CGEDMD. */
  1202. clacpy_("A", n, k, &x[x_offset], ldx, &z__[z_offset], ldz);
  1203. if (*m > *n) {
  1204. i__1 = *m - *n;
  1205. claset_("A", &i__1, k, &zzero, &zzero, &z__[*n + 1 + z_dim1], ldz);
  1206. }
  1207. i__1 = *lzwork - minmn;
  1208. cunmqr_("L", "N", m, k, &minmn, &f[f_offset], ldf, &zwork[1], &z__[
  1209. z_offset], ldz, &zwork[minmn + 1], &i__1, &info1);
  1210. }
  1211. /* Some optional output variables: */
  1212. /* The upper triangular factor R in the initial QR */
  1213. /* factorization is optionally returned in the array Y. */
  1214. /* This is useful if this call to CGEDMDQ is to be */
  1215. /* followed by a streaming DMD that is implemented in a */
  1216. /* QR compressed form. */
  1217. if (wnttrf) {
  1218. /* Return the upper triangular R in Y */
  1219. claset_("A", &minmn, n, &zzero, &zzero, &y[y_offset], ldy);
  1220. clacpy_("U", &minmn, n, &f[f_offset], ldf, &y[y_offset], ldy);
  1221. }
  1222. /* The orthonormal/unitary factor Q in the initial QR */
  1223. /* factorization is optionally returned in the array F. */
  1224. /* Same as with the triangular factor above, this is */
  1225. /* useful in a streaming DMD. */
  1226. if (wantq) {
  1227. /* Q overwrites F */
  1228. i__1 = *lzwork - minmn;
  1229. cungqr_(m, &minmn, &minmn, &f[f_offset], ldf, &zwork[1], &zwork[minmn
  1230. + 1], &i__1, &info1);
  1231. }
  1232. return 0;
  1233. } /* cgedmdq_ */