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 int logical;
  52. typedef short int shortlogical;
  53. typedef char logical1;
  54. typedef char integer1;
  55. #define TRUE_ (1)
  56. #define FALSE_ (0)
  57. /* Extern is for use with -E */
  58. #ifndef Extern
  59. #define Extern extern
  60. #endif
  61. /* I/O stuff */
  62. typedef int flag;
  63. typedef int ftnlen;
  64. typedef int ftnint;
  65. /*external read, write*/
  66. typedef struct
  67. { flag cierr;
  68. ftnint ciunit;
  69. flag ciend;
  70. char *cifmt;
  71. ftnint cirec;
  72. } cilist;
  73. /*internal read, write*/
  74. typedef struct
  75. { flag icierr;
  76. char *iciunit;
  77. flag iciend;
  78. char *icifmt;
  79. ftnint icirlen;
  80. ftnint icirnum;
  81. } icilist;
  82. /*open*/
  83. typedef struct
  84. { flag oerr;
  85. ftnint ounit;
  86. char *ofnm;
  87. ftnlen ofnmlen;
  88. char *osta;
  89. char *oacc;
  90. char *ofm;
  91. ftnint orl;
  92. char *oblnk;
  93. } olist;
  94. /*close*/
  95. typedef struct
  96. { flag cerr;
  97. ftnint cunit;
  98. char *csta;
  99. } cllist;
  100. /*rewind, backspace, endfile*/
  101. typedef struct
  102. { flag aerr;
  103. ftnint aunit;
  104. } alist;
  105. /* inquire */
  106. typedef struct
  107. { flag inerr;
  108. ftnint inunit;
  109. char *infile;
  110. ftnlen infilen;
  111. ftnint *inex; /*parameters in standard's order*/
  112. ftnint *inopen;
  113. ftnint *innum;
  114. ftnint *innamed;
  115. char *inname;
  116. ftnlen innamlen;
  117. char *inacc;
  118. ftnlen inacclen;
  119. char *inseq;
  120. ftnlen inseqlen;
  121. char *indir;
  122. ftnlen indirlen;
  123. char *infmt;
  124. ftnlen infmtlen;
  125. char *inform;
  126. ftnint informlen;
  127. char *inunf;
  128. ftnlen inunflen;
  129. ftnint *inrecl;
  130. ftnint *innrec;
  131. char *inblank;
  132. ftnlen inblanklen;
  133. } inlist;
  134. #define VOID void
  135. union Multitype { /* for multiple entry points */
  136. integer1 g;
  137. shortint h;
  138. integer i;
  139. /* longint j; */
  140. real r;
  141. doublereal d;
  142. complex c;
  143. doublecomplex z;
  144. };
  145. typedef union Multitype Multitype;
  146. struct Vardesc { /* for Namelist */
  147. char *name;
  148. char *addr;
  149. ftnlen *dims;
  150. int type;
  151. };
  152. typedef struct Vardesc Vardesc;
  153. struct Namelist {
  154. char *name;
  155. Vardesc **vars;
  156. int nvars;
  157. };
  158. typedef struct Namelist Namelist;
  159. #define abs(x) ((x) >= 0 ? (x) : -(x))
  160. #define dabs(x) (fabs(x))
  161. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  162. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  163. #define dmin(a,b) (f2cmin(a,b))
  164. #define dmax(a,b) (f2cmax(a,b))
  165. #define bit_test(a,b) ((a) >> (b) & 1)
  166. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  167. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  168. #define abort_() { sig_die("Fortran abort routine called", 1); }
  169. #define c_abs(z) (cabsf(Cf(z)))
  170. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  171. #ifdef _MSC_VER
  172. #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
  173. #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/Cd(b)._Val[1]);}
  174. #else
  175. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  176. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  177. #endif
  178. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  179. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  180. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  181. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  182. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  183. #define d_abs(x) (fabs(*(x)))
  184. #define d_acos(x) (acos(*(x)))
  185. #define d_asin(x) (asin(*(x)))
  186. #define d_atan(x) (atan(*(x)))
  187. #define d_atn2(x, y) (atan2(*(x),*(y)))
  188. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  189. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  190. #define d_cos(x) (cos(*(x)))
  191. #define d_cosh(x) (cosh(*(x)))
  192. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  193. #define d_exp(x) (exp(*(x)))
  194. #define d_imag(z) (cimag(Cd(z)))
  195. #define r_imag(z) (cimagf(Cf(z)))
  196. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  198. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  200. #define d_log(x) (log(*(x)))
  201. #define d_mod(x, y) (fmod(*(x), *(y)))
  202. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  203. #define d_nint(x) u_nint(*(x))
  204. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  205. #define d_sign(a,b) u_sign(*(a),*(b))
  206. #define r_sign(a,b) u_sign(*(a),*(b))
  207. #define d_sin(x) (sin(*(x)))
  208. #define d_sinh(x) (sinh(*(x)))
  209. #define d_sqrt(x) (sqrt(*(x)))
  210. #define d_tan(x) (tan(*(x)))
  211. #define d_tanh(x) (tanh(*(x)))
  212. #define i_abs(x) abs(*(x))
  213. #define i_dnnt(x) ((integer)u_nint(*(x)))
  214. #define i_len(s, n) (n)
  215. #define i_nint(x) ((integer)u_nint(*(x)))
  216. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  217. #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
  218. #define pow_si(B,E) spow_ui(*(B),*(E))
  219. #define pow_ri(B,E) spow_ui(*(B),*(E))
  220. #define pow_di(B,E) dpow_ui(*(B),*(E))
  221. #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
  222. #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
  223. #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
  224. #define s_cat(lpp, rpp, rnp, np, llp) { ftnlen i, nc, ll; char *f__rp, *lp; ll = (llp); lp = (lpp); for(i=0; i < (int)*(np); ++i) { nc = ll; if((rnp)[i] < nc) nc = (rnp)[i]; ll -= nc; f__rp = (rpp)[i]; while(--nc >= 0) *lp++ = *(f__rp)++; } while(--ll >= 0) *lp++ = ' '; }
  225. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  226. #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
  227. #define sig_die(s, kill) { exit(1); }
  228. #define s_stop(s, n) {exit(0);}
  229. static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
  230. #define z_abs(z) (cabs(Cd(z)))
  231. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  232. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  233. #define myexit_() break;
  234. #define mycycle_() continue;
  235. #define myceiling_(w) {ceil(w)}
  236. #define myhuge_(w) {HUGE_VAL}
  237. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  238. #define mymaxloc_(w,s,e,n) dmaxloc_(w,*(s),*(e),n)
  239. /* procedure parameter types for -A and -C++ */
  240. #define F2C_proc_par_types 1
  241. #ifdef __cplusplus
  242. typedef logical (*L_fp)(...);
  243. #else
  244. typedef logical (*L_fp)();
  245. #endif
  246. static float spow_ui(float x, integer n) {
  247. float pow=1.0; unsigned long int u;
  248. if(n != 0) {
  249. if(n < 0) n = -n, x = 1/x;
  250. for(u = n; ; ) {
  251. if(u & 01) pow *= x;
  252. if(u >>= 1) x *= x;
  253. else break;
  254. }
  255. }
  256. return pow;
  257. }
  258. static double dpow_ui(double x, integer n) {
  259. double pow=1.0; unsigned long int u;
  260. if(n != 0) {
  261. if(n < 0) n = -n, x = 1/x;
  262. for(u = n; ; ) {
  263. if(u & 01) pow *= x;
  264. if(u >>= 1) x *= x;
  265. else break;
  266. }
  267. }
  268. return pow;
  269. }
  270. #ifdef _MSC_VER
  271. static _Fcomplex cpow_ui(complex x, integer n) {
  272. complex pow={1.0,0.0}; unsigned long int u;
  273. if(n != 0) {
  274. if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
  275. for(u = n; ; ) {
  276. if(u & 01) pow.r *= x.r, pow.i *= x.i;
  277. if(u >>= 1) x.r *= x.r, x.i *= x.i;
  278. else break;
  279. }
  280. }
  281. _Fcomplex p={pow.r, pow.i};
  282. return p;
  283. }
  284. #else
  285. static _Complex float cpow_ui(_Complex float x, integer n) {
  286. _Complex float pow=1.0; unsigned long int u;
  287. if(n != 0) {
  288. if(n < 0) n = -n, x = 1/x;
  289. for(u = n; ; ) {
  290. if(u & 01) pow *= x;
  291. if(u >>= 1) x *= x;
  292. else break;
  293. }
  294. }
  295. return pow;
  296. }
  297. #endif
  298. #ifdef _MSC_VER
  299. static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
  300. _Dcomplex pow={1.0,0.0}; unsigned long int u;
  301. if(n != 0) {
  302. if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
  303. for(u = n; ; ) {
  304. if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
  305. if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
  306. else break;
  307. }
  308. }
  309. _Dcomplex p = {pow._Val[0], pow._Val[1]};
  310. return p;
  311. }
  312. #else
  313. static _Complex double zpow_ui(_Complex double x, integer n) {
  314. _Complex double pow=1.0; unsigned long int u;
  315. if(n != 0) {
  316. if(n < 0) n = -n, x = 1/x;
  317. for(u = n; ; ) {
  318. if(u & 01) pow *= x;
  319. if(u >>= 1) x *= x;
  320. else break;
  321. }
  322. }
  323. return pow;
  324. }
  325. #endif
  326. static integer pow_ii(integer x, integer n) {
  327. integer pow; unsigned long int u;
  328. if (n <= 0) {
  329. if (n == 0 || x == 1) pow = 1;
  330. else if (x != -1) pow = x == 0 ? 1/x : 0;
  331. else n = -n;
  332. }
  333. if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
  334. u = n;
  335. for(pow = 1; ; ) {
  336. if(u & 01) pow *= x;
  337. if(u >>= 1) x *= x;
  338. else break;
  339. }
  340. }
  341. return pow;
  342. }
  343. static integer dmaxloc_(double *w, integer s, integer e, integer *n)
  344. {
  345. double m; integer i, mi;
  346. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  347. if (w[i-1]>m) mi=i ,m=w[i-1];
  348. return mi-s+1;
  349. }
  350. static integer smaxloc_(float *w, integer s, integer e, integer *n)
  351. {
  352. float m; integer i, mi;
  353. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  354. if (w[i-1]>m) mi=i ,m=w[i-1];
  355. return mi-s+1;
  356. }
  357. static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  358. integer n = *n_, incx = *incx_, incy = *incy_, i;
  359. #ifdef _MSC_VER
  360. _Fcomplex zdotc = {0.0, 0.0};
  361. if (incx == 1 && incy == 1) {
  362. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  363. zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
  364. zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
  365. }
  366. } else {
  367. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  368. zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
  369. zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
  370. }
  371. }
  372. pCf(z) = zdotc;
  373. }
  374. #else
  375. _Complex float zdotc = 0.0;
  376. if (incx == 1 && incy == 1) {
  377. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  378. zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
  379. }
  380. } else {
  381. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  382. zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
  383. }
  384. }
  385. pCf(z) = zdotc;
  386. }
  387. #endif
  388. static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  389. integer n = *n_, incx = *incx_, incy = *incy_, i;
  390. #ifdef _MSC_VER
  391. _Dcomplex zdotc = {0.0, 0.0};
  392. if (incx == 1 && incy == 1) {
  393. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  394. zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
  395. zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
  396. }
  397. } else {
  398. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  399. zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
  400. zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
  401. }
  402. }
  403. pCd(z) = zdotc;
  404. }
  405. #else
  406. _Complex double zdotc = 0.0;
  407. if (incx == 1 && incy == 1) {
  408. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  409. zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
  410. }
  411. } else {
  412. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  413. zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
  414. }
  415. }
  416. pCd(z) = zdotc;
  417. }
  418. #endif
  419. static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  420. integer n = *n_, incx = *incx_, incy = *incy_, i;
  421. #ifdef _MSC_VER
  422. _Fcomplex zdotc = {0.0, 0.0};
  423. if (incx == 1 && incy == 1) {
  424. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  425. zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
  426. zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
  427. }
  428. } else {
  429. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  430. zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
  431. zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
  432. }
  433. }
  434. pCf(z) = zdotc;
  435. }
  436. #else
  437. _Complex float zdotc = 0.0;
  438. if (incx == 1 && incy == 1) {
  439. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  440. zdotc += Cf(&x[i]) * Cf(&y[i]);
  441. }
  442. } else {
  443. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  444. zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
  445. }
  446. }
  447. pCf(z) = zdotc;
  448. }
  449. #endif
  450. static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  451. integer n = *n_, incx = *incx_, incy = *incy_, i;
  452. #ifdef _MSC_VER
  453. _Dcomplex zdotc = {0.0, 0.0};
  454. if (incx == 1 && incy == 1) {
  455. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  456. zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
  457. zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
  458. }
  459. } else {
  460. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  461. zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
  462. zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
  463. }
  464. }
  465. pCd(z) = zdotc;
  466. }
  467. #else
  468. _Complex double zdotc = 0.0;
  469. if (incx == 1 && incy == 1) {
  470. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  471. zdotc += Cd(&x[i]) * Cd(&y[i]);
  472. }
  473. } else {
  474. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  475. zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
  476. }
  477. }
  478. pCd(z) = zdotc;
  479. }
  480. #endif
  481. /* -- translated by f2c (version 20000121).
  482. You must link the resulting object file with the libraries:
  483. -lf2c -lm (in that order)
  484. */
  485. /* -- translated by f2c (version 20000121).
  486. You must link the resulting object file with the libraries:
  487. -lf2c -lm (in that order)
  488. */
  489. /* Table of constant values */
  490. static integer c_n1 = -1;
  491. /* Subroutine */ int cgedmdq_(char *jobs, char *jobz, char *jobr, char *jobq,
  492. char *jobt, char *jobf, integer *whtsvd, integer *m, integer *n,
  493. complex *f, integer *ldf, complex *x, integer *ldx, complex *y,
  494. integer *ldy, integer *nrnk, real *tol, integer *k, complex *eigs,
  495. complex *z__, integer *ldz, real *res, complex *b, integer *ldb,
  496. complex *v, integer *ldv, complex *s, integer *lds, complex *zwork,
  497. integer *lzwork, real *work, integer *lwork, integer *iwork, integer *
  498. liwork, integer *info)
  499. {
  500. /* System generated locals */
  501. integer f_dim1, f_offset, x_dim1, x_offset, y_dim1, y_offset, z_dim1,
  502. z_offset, b_dim1, b_offset, v_dim1, v_offset, s_dim1, s_offset,
  503. i__1, i__2;
  504. /* Local variables */
  505. real zero;
  506. integer info1;
  507. extern logical lsame_(char *, char *);
  508. char jobvl[1];
  509. integer minmn;
  510. logical wantq;
  511. integer mlwqr, olwqr;
  512. logical wntex;
  513. complex zzero;
  514. extern /* Subroutine */ int cgedmd_(char *, char *, char *, char *,
  515. integer *, integer *, integer *, complex *, integer *, complex *,
  516. integer *, integer *, real *, integer *, complex *, complex *,
  517. integer *, real *, complex *, integer *, complex *, integer *,
  518. complex *, integer *, complex *, integer *, real *, integer *,
  519. integer *, integer *, integer *),
  520. cgeqrf_(integer *, integer *, complex *, integer *, complex *,
  521. complex *, integer *, integer *), clacpy_(char *, integer *,
  522. integer *, complex *, integer *, complex *, integer *),
  523. claset_(char *, integer *, integer *, complex *, complex *,
  524. complex *, integer *), xerbla_(char *, integer *);
  525. integer mlwdmd, olwdmd;
  526. logical sccolx, sccoly;
  527. extern /* Subroutine */ int cungqr_(integer *, integer *, integer *,
  528. complex *, integer *, complex *, complex *, integer *, integer *);
  529. integer iminwr;
  530. logical wntvec, wntvcf;
  531. integer mlwgqr;
  532. logical wntref;
  533. integer mlwork, olwgqr, olwork, mlrwrk, mlwmqr, olwmqr;
  534. logical lquery, wntres, wnttrf, wntvcq;
  535. extern /* Subroutine */ int cunmqr_(char *, char *, integer *, integer *,
  536. integer *, complex *, integer *, complex *, complex *, integer *,
  537. complex *, integer *, integer *);
  538. real one;
  539. /* March 2023 */
  540. /* ..... */
  541. /* USE iso_fortran_env */
  542. /* INTEGER, PARAMETER :: WP = real32 */
  543. /* ..... */
  544. /* Scalar arguments */
  545. /* Array arguments */
  546. /* ..... */
  547. /* Purpose */
  548. /* ======= */
  549. /* CGEDMDQ computes the Dynamic Mode Decomposition (DMD) for */
  550. /* a pair of data snapshot matrices, using a QR factorization */
  551. /* based compression of the data. For the input matrices */
  552. /* X and Y such that Y = A*X with an unaccessible matrix */
  553. /* A, CGEDMDQ computes a certain number of Ritz pairs of A using */
  554. /* the standard Rayleigh-Ritz extraction from a subspace of */
  555. /* range(X) that is determined using the leading left singular */
  556. /* vectors of X. Optionally, CGEDMDQ returns the residuals */
  557. /* of the computed Ritz pairs, the information needed for */
  558. /* a refinement of the Ritz vectors, or the eigenvectors of */
  559. /* the Exact DMD. */
  560. /* For further details see the references listed */
  561. /* below. For more details of the implementation see [3]. */
  562. /* References */
  563. /* ========== */
  564. /* [1] P. Schmid: Dynamic mode decomposition of numerical */
  565. /* and experimental data, */
  566. /* Journal of Fluid Mechanics 656, 5-28, 2010. */
  567. /* [2] Z. Drmac, I. Mezic, R. Mohr: Data driven modal */
  568. /* decompositions: analysis and enhancements, */
  569. /* SIAM J. on Sci. Comp. 40 (4), A2253-A2285, 2018. */
  570. /* [3] Z. Drmac: A LAPACK implementation of the Dynamic */
  571. /* Mode Decomposition I. Technical report. AIMDyn Inc. */
  572. /* and LAPACK Working Note 298. */
  573. /* [4] J. Tu, C. W. Rowley, D. M. Luchtenburg, S. L. */
  574. /* Brunton, N. Kutz: On Dynamic Mode Decomposition: */
  575. /* Theory and Applications, Journal of Computational */
  576. /* Dynamics 1(2), 391 -421, 2014. */
  577. /* Developed and supported by: */
  578. /* =========================== */
  579. /* Developed and coded by Zlatko Drmac, Faculty of Science, */
  580. /* University of Zagreb; drmac@math.hr */
  581. /* In cooperation with */
  582. /* AIMdyn Inc., Santa Barbara, CA. */
  583. /* and supported by */
  584. /* - DARPA SBIR project "Koopman Operator-Based Forecasting */
  585. /* for Nonstationary Processes from Near-Term, Limited */
  586. /* Observational Data" Contract No: W31P4Q-21-C-0007 */
  587. /* - DARPA PAI project "Physics-Informed Machine Learning */
  588. /* Methodologies" Contract No: HR0011-18-9-0033 */
  589. /* - DARPA MoDyL project "A Data-Driven, Operator-Theoretic */
  590. /* Framework for Space-Time Analysis of Process Dynamics" */
  591. /* Contract No: HR0011-16-C-0116 */
  592. /* Any opinions, findings and conclusions or recommendations */
  593. /* expressed in this material are those of the author and */
  594. /* do not necessarily reflect the views of the DARPA SBIR */
  595. /* Program Office. */
  596. /* ============================================================ */
  597. /* Distribution Statement A: */
  598. /* Approved for Public Release, Distribution Unlimited. */
  599. /* Cleared by DARPA on September 29, 2022 */
  600. /* ============================================================ */
  601. /* ...................................................................... */
  602. /* Arguments */
  603. /* ========= */
  604. /* JOBS (input) CHARACTER*1 */
  605. /* Determines whether the initial data snapshots are scaled */
  606. /* by a diagonal matrix. The data snapshots are the columns */
  607. /* of F. The leading N-1 columns of F are denoted X and the */
  608. /* trailing N-1 columns are denoted Y. */
  609. /* 'S' :: The data snapshots matrices X and Y are multiplied */
  610. /* with a diagonal matrix D so that X*D has unit */
  611. /* nonzero columns (in the Euclidean 2-norm) */
  612. /* 'C' :: The snapshots are scaled as with the 'S' option. */
  613. /* If it is found that an i-th column of X is zero */
  614. /* vector and the corresponding i-th column of Y is */
  615. /* non-zero, then the i-th column of Y is set to */
  616. /* zero and a warning flag is raised. */
  617. /* 'Y' :: The data snapshots matrices X and Y are multiplied */
  618. /* by a diagonal matrix D so that Y*D has unit */
  619. /* nonzero columns (in the Euclidean 2-norm) */
  620. /* 'N' :: No data scaling. */
  621. /* ..... */
  622. /* JOBZ (input) CHARACTER*1 */
  623. /* Determines whether the eigenvectors (Koopman modes) will */
  624. /* be computed. */
  625. /* 'V' :: The eigenvectors (Koopman modes) will be computed */
  626. /* and returned in the matrix Z. */
  627. /* See the description of Z. */
  628. /* 'F' :: The eigenvectors (Koopman modes) will be returned */
  629. /* in factored form as the product Z*V, where Z */
  630. /* is orthonormal and V contains the eigenvectors */
  631. /* of the corresponding Rayleigh quotient. */
  632. /* See the descriptions of F, V, Z. */
  633. /* 'Q' :: The eigenvectors (Koopman modes) will be returned */
  634. /* in factored form as the product Q*Z, where Z */
  635. /* contains the eigenvectors of the compression of the */
  636. /* underlying discretised operator onto the span of */
  637. /* the data snapshots. See the descriptions of F, V, Z. */
  638. /* Q is from the inital QR facorization. */
  639. /* 'N' :: The eigenvectors are not computed. */
  640. /* ..... */
  641. /* JOBR (input) CHARACTER*1 */
  642. /* Determines whether to compute the residuals. */
  643. /* 'R' :: The residuals for the computed eigenpairs will */
  644. /* be computed and stored in the array RES. */
  645. /* See the description of RES. */
  646. /* For this option to be legal, JOBZ must be 'V'. */
  647. /* 'N' :: The residuals are not computed. */
  648. /* ..... */
  649. /* JOBQ (input) CHARACTER*1 */
  650. /* Specifies whether to explicitly compute and return the */
  651. /* unitary matrix from the QR factorization. */
  652. /* 'Q' :: The matrix Q of the QR factorization of the data */
  653. /* snapshot matrix is computed and stored in the */
  654. /* array F. See the description of F. */
  655. /* 'N' :: The matrix Q is not explicitly computed. */
  656. /* ..... */
  657. /* JOBT (input) CHARACTER*1 */
  658. /* Specifies whether to return the upper triangular factor */
  659. /* from the QR factorization. */
  660. /* 'R' :: The matrix R of the QR factorization of the data */
  661. /* snapshot matrix F is returned in the array Y. */
  662. /* See the description of Y and Further details. */
  663. /* 'N' :: The matrix R is not returned. */
  664. /* ..... */
  665. /* JOBF (input) CHARACTER*1 */
  666. /* Specifies whether to store information needed for post- */
  667. /* processing (e.g. computing refined Ritz vectors) */
  668. /* 'R' :: The matrix needed for the refinement of the Ritz */
  669. /* vectors is computed and stored in the array B. */
  670. /* See the description of B. */
  671. /* 'E' :: The unscaled eigenvectors of the Exact DMD are */
  672. /* computed and returned in the array B. See the */
  673. /* description of B. */
  674. /* 'N' :: No eigenvector refinement data is computed. */
  675. /* To be useful on exit, this option needs JOBQ='Q'. */
  676. /* ..... */
  677. /* WHTSVD (input) INTEGER, WHSTVD in { 1, 2, 3, 4 } */
  678. /* Allows for a selection of the SVD algorithm from the */
  679. /* LAPACK library. */
  680. /* 1 :: CGESVD (the QR SVD algorithm) */
  681. /* 2 :: CGESDD (the Divide and Conquer algorithm; if enough */
  682. /* workspace available, this is the fastest option) */
  683. /* 3 :: CGESVDQ (the preconditioned QR SVD ; this and 4 */
  684. /* are the most accurate options) */
  685. /* 4 :: CGEJSV (the preconditioned Jacobi SVD; this and 3 */
  686. /* are the most accurate options) */
  687. /* For the four methods above, a significant difference in */
  688. /* the accuracy of small singular values is possible if */
  689. /* the snapshots vary in norm so that X is severely */
  690. /* ill-conditioned. If small (smaller than EPS*||X||) */
  691. /* singular values are of interest and JOBS=='N', then */
  692. /* the options (3, 4) give the most accurate results, where */
  693. /* the option 4 is slightly better and with stronger */
  694. /* theoretical background. */
  695. /* If JOBS=='S', i.e. the columns of X will be normalized, */
  696. /* then all methods give nearly equally accurate results. */
  697. /* ..... */
  698. /* M (input) INTEGER, M >= 0 */
  699. /* The state space dimension (the number of rows of F). */
  700. /* ..... */
  701. /* N (input) INTEGER, 0 <= N <= M */
  702. /* The number of data snapshots from a single trajectory, */
  703. /* taken at equidistant discrete times. This is the */
  704. /* number of columns of F. */
  705. /* ..... */
  706. /* F (input/output) COMPLEX(KIND=WP) M-by-N array */
  707. /* > On entry, */
  708. /* the columns of F are the sequence of data snapshots */
  709. /* from a single trajectory, taken at equidistant discrete */
  710. /* times. It is assumed that the column norms of F are */
  711. /* in the range of the normalized floating point numbers. */
  712. /* < On exit, */
  713. /* If JOBQ == 'Q', the array F contains the orthogonal */
  714. /* matrix/factor of the QR factorization of the initial */
  715. /* data snapshots matrix F. See the description of JOBQ. */
  716. /* If JOBQ == 'N', the entries in F strictly below the main */
  717. /* diagonal contain, column-wise, the information on the */
  718. /* Householder vectors, as returned by CGEQRF. The */
  719. /* remaining information to restore the orthogonal matrix */
  720. /* of the initial QR factorization is stored in ZWORK(1:MIN(M,N)). */
  721. /* See the description of ZWORK. */
  722. /* ..... */
  723. /* LDF (input) INTEGER, LDF >= M */
  724. /* The leading dimension of the array F. */
  725. /* ..... */
  726. /* X (workspace/output) COMPLEX(KIND=WP) MIN(M,N)-by-(N-1) array */
  727. /* X is used as workspace to hold representations of the */
  728. /* leading N-1 snapshots in the orthonormal basis computed */
  729. /* in the QR factorization of F. */
  730. /* On exit, the leading K columns of X contain the leading */
  731. /* K left singular vectors of the above described content */
  732. /* of X. To lift them to the space of the left singular */
  733. /* vectors U(:,1:K) of the input data, pre-multiply with the */
  734. /* Q factor from the initial QR factorization. */
  735. /* See the descriptions of F, K, V and Z. */
  736. /* ..... */
  737. /* LDX (input) INTEGER, LDX >= N */
  738. /* The leading dimension of the array X. */
  739. /* ..... */
  740. /* Y (workspace/output) COMPLEX(KIND=WP) MIN(M,N)-by-(N) array */
  741. /* Y is used as workspace to hold representations of the */
  742. /* trailing N-1 snapshots in the orthonormal basis computed */
  743. /* in the QR factorization of F. */
  744. /* On exit, */
  745. /* If JOBT == 'R', Y contains the MIN(M,N)-by-N upper */
  746. /* triangular factor from the QR factorization of the data */
  747. /* snapshot matrix F. */
  748. /* ..... */
  749. /* LDY (input) INTEGER , LDY >= N */
  750. /* The leading dimension of the array Y. */
  751. /* ..... */
  752. /* NRNK (input) INTEGER */
  753. /* Determines the mode how to compute the numerical rank, */
  754. /* i.e. how to truncate small singular values of the input */
  755. /* matrix X. On input, if */
  756. /* NRNK = -1 :: i-th singular value sigma(i) is truncated */
  757. /* if sigma(i) <= TOL*sigma(1) */
  758. /* This option is recommended. */
  759. /* NRNK = -2 :: i-th singular value sigma(i) is truncated */
  760. /* if sigma(i) <= TOL*sigma(i-1) */
  761. /* This option is included for R&D purposes. */
  762. /* It requires highly accurate SVD, which */
  763. /* may not be feasible. */
  764. /* The numerical rank can be enforced by using positive */
  765. /* value of NRNK as follows: */
  766. /* 0 < NRNK <= N-1 :: at most NRNK largest singular values */
  767. /* will be used. If the number of the computed nonzero */
  768. /* singular values is less than NRNK, then only those */
  769. /* nonzero values will be used and the actually used */
  770. /* dimension is less than NRNK. The actual number of */
  771. /* the nonzero singular values is returned in the variable */
  772. /* K. See the description of K. */
  773. /* ..... */
  774. /* TOL (input) REAL(KIND=WP), 0 <= TOL < 1 */
  775. /* The tolerance for truncating small singular values. */
  776. /* See the description of NRNK. */
  777. /* ..... */
  778. /* K (output) INTEGER, 0 <= K <= N */
  779. /* The dimension of the SVD/POD basis for the leading N-1 */
  780. /* data snapshots (columns of F) and the number of the */
  781. /* computed Ritz pairs. The value of K is determined */
  782. /* according to the rule set by the parameters NRNK and */
  783. /* TOL. See the descriptions of NRNK and TOL. */
  784. /* ..... */
  785. /* EIGS (output) COMPLEX(KIND=WP) (N-1)-by-1 array */
  786. /* The leading K (K<=N-1) entries of EIGS contain */
  787. /* the computed eigenvalues (Ritz values). */
  788. /* See the descriptions of K, and Z. */
  789. /* ..... */
  790. /* Z (workspace/output) COMPLEX(KIND=WP) M-by-(N-1) array */
  791. /* If JOBZ =='V' then Z contains the Ritz vectors. Z(:,i) */
  792. /* is an eigenvector of the i-th Ritz value; ||Z(:,i)||_2=1. */
  793. /* If JOBZ == 'F', then the Z(:,i)'s are given implicitly as */
  794. /* Z*V, where Z contains orthonormal matrix (the product of */
  795. /* Q from the initial QR factorization and the SVD/POD_basis */
  796. /* returned by CGEDMD in X) and the second factor (the */
  797. /* eigenvectors of the Rayleigh quotient) is in the array V, */
  798. /* as returned by CGEDMD. That is, X(:,1:K)*V(:,i) */
  799. /* is an eigenvector corresponding to EIGS(i). The columns */
  800. /* of V(1:K,1:K) are the computed eigenvectors of the */
  801. /* K-by-K Rayleigh quotient. */
  802. /* See the descriptions of EIGS, X and V. */
  803. /* ..... */
  804. /* LDZ (input) INTEGER , LDZ >= M */
  805. /* The leading dimension of the array Z. */
  806. /* ..... */
  807. /* RES (output) REAL(KIND=WP) (N-1)-by-1 array */
  808. /* RES(1:K) contains the residuals for the K computed */
  809. /* Ritz pairs, */
  810. /* RES(i) = || A * Z(:,i) - EIGS(i)*Z(:,i))||_2. */
  811. /* See the description of EIGS and Z. */
  812. /* ..... */
  813. /* B (output) COMPLEX(KIND=WP) MIN(M,N)-by-(N-1) array. */
  814. /* IF JOBF =='R', B(1:N,1:K) contains A*U(:,1:K), and can */
  815. /* be used for computing the refined vectors; see further */
  816. /* details in the provided references. */
  817. /* If JOBF == 'E', B(1:N,1;K) contains */
  818. /* A*U(:,1:K)*W(1:K,1:K), which are the vectors from the */
  819. /* Exact DMD, up to scaling by the inverse eigenvalues. */
  820. /* In both cases, the content of B can be lifted to the */
  821. /* original dimension of the input data by pre-multiplying */
  822. /* with the Q factor from the initial QR factorization. */
  823. /* Here A denotes a compression of the underlying operator. */
  824. /* See the descriptions of F and X. */
  825. /* If JOBF =='N', then B is not referenced. */
  826. /* ..... */
  827. /* LDB (input) INTEGER, LDB >= MIN(M,N) */
  828. /* The leading dimension of the array B. */
  829. /* ..... */
  830. /* V (workspace/output) COMPLEX(KIND=WP) (N-1)-by-(N-1) array */
  831. /* On exit, V(1:K,1:K) V contains the K eigenvectors of */
  832. /* the Rayleigh quotient. The Ritz vectors */
  833. /* (returned in Z) are the product of Q from the initial QR */
  834. /* factorization (see the description of F) X (see the */
  835. /* description of X) and V. */
  836. /* ..... */
  837. /* LDV (input) INTEGER, LDV >= N-1 */
  838. /* The leading dimension of the array V. */
  839. /* ..... */
  840. /* S (output) COMPLEX(KIND=WP) (N-1)-by-(N-1) array */
  841. /* The array S(1:K,1:K) is used for the matrix Rayleigh */
  842. /* quotient. This content is overwritten during */
  843. /* the eigenvalue decomposition by CGEEV. */
  844. /* See the description of K. */
  845. /* ..... */
  846. /* LDS (input) INTEGER, LDS >= N-1 */
  847. /* The leading dimension of the array S. */
  848. /* ..... */
  849. /* ZWORK (workspace/output) COMPLEX(KIND=WP) LWORK-by-1 array */
  850. /* On exit, */
  851. /* ZWORK(1:MIN(M,N)) contains the scalar factors of the */
  852. /* elementary reflectors as returned by CGEQRF of the */
  853. /* M-by-N input matrix F. */
  854. /* If the call to CGEDMDQ is only workspace query, then */
  855. /* ZWORK(1) contains the minimal complex workspace length and */
  856. /* ZWORK(2) is the optimal complex workspace length. */
  857. /* Hence, the length of work is at least 2. */
  858. /* See the description of LZWORK. */
  859. /* ..... */
  860. /* LZWORK (input) INTEGER */
  861. /* The minimal length of the workspace vector ZWORK. */
  862. /* LZWORK is calculated as follows: */
  863. /* Let MLWQR = N (minimal workspace for CGEQRF[M,N]) */
  864. /* MLWDMD = minimal workspace for CGEDMD (see the */
  865. /* description of LWORK in CGEDMD) */
  866. /* MLWMQR = N (minimal workspace for */
  867. /* ZUNMQR['L','N',M,N,N]) */
  868. /* MLWGQR = N (minimal workspace for ZUNGQR[M,N,N]) */
  869. /* MINMN = MIN(M,N) */
  870. /* Then */
  871. /* LZWORK = MAX(2, MIN(M,N)+MLWQR, MINMN+MLWDMD) */
  872. /* is further updated as follows: */
  873. /* if JOBZ == 'V' or JOBZ == 'F' THEN */
  874. /* LZWORK = MAX( LZWORK, MINMN+MLWMQR ) */
  875. /* if JOBQ == 'Q' THEN */
  876. /* LZWORK = MAX( ZLWORK, MINMN+MLWGQR) */
  877. /* ..... */
  878. /* WORK (workspace/output) REAL(KIND=WP) LWORK-by-1 array */
  879. /* On exit, */
  880. /* WORK(1:N-1) contains the singular values of */
  881. /* the input submatrix F(1:M,1:N-1). */
  882. /* If the call to CGEDMDQ is only workspace query, then */
  883. /* WORK(1) contains the minimal workspace length and */
  884. /* WORK(2) is the optimal workspace length. hence, the */
  885. /* length of work is at least 2. */
  886. /* See the description of LWORK. */
  887. /* ..... */
  888. /* LWORK (input) INTEGER */
  889. /* The minimal length of the workspace vector WORK. */
  890. /* LWORK is the same as in CGEDMD, because in CGEDMDQ */
  891. /* only CGEDMD requires real workspace for snapshots */
  892. /* of dimensions MIN(M,N)-by-(N-1). */
  893. /* If on entry LWORK = -1, then a workspace query is */
  894. /* assumed and the procedure only computes the minimal */
  895. /* and the optimal workspace lengths for both WORK and */
  896. /* IWORK. See the descriptions of WORK and IWORK. */
  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(M,N)) */
  910. /* If WHTSVD == 3, then LIWORK >= MAX(1,M+N-1) */
  911. /* If WHTSVD == 4, then LIWORK >= MAX(3,M+3*N) */
  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 = *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. work[1] = 2.f;
  1052. work[2] = 2.f;
  1053. } else {
  1054. *k = 0;
  1055. }
  1056. *info = 1;
  1057. return 0;
  1058. }
  1059. mlrwrk = 2;
  1060. mlwork = 2;
  1061. olwork = 2;
  1062. iminwr = 1;
  1063. mlwqr = f2cmax(1,*n);
  1064. /* Minimal workspace length for CGEQRF. */
  1065. /* Computing MAX */
  1066. i__1 = mlwork, i__2 = minmn + mlwqr;
  1067. mlwork = f2cmax(i__1,i__2);
  1068. if (lquery) {
  1069. cgeqrf_(m, n, &f[f_offset], ldf, &zwork[1], &zwork[1], &c_n1, &
  1070. info1);
  1071. olwqr = (integer) zwork[1].r;
  1072. /* Computing MAX */
  1073. i__1 = olwork, i__2 = minmn + olwqr;
  1074. olwork = f2cmax(i__1,i__2);
  1075. }
  1076. i__1 = *n - 1;
  1077. cgedmd_(jobs, jobvl, jobr, jobf, whtsvd, &minmn, &i__1, &x[x_offset],
  1078. ldx, &y[y_offset], ldy, nrnk, tol, k, &eigs[1], &z__[z_offset]
  1079. , ldz, &res[1], &b[b_offset], ldb, &v[v_offset], ldv, &s[
  1080. s_offset], lds, &zwork[1], lzwork, &work[1], &c_n1, &iwork[1],
  1081. liwork, &info1);
  1082. mlwdmd = (integer) zwork[1].r;
  1083. /* Computing MAX */
  1084. i__1 = mlwork, i__2 = minmn + mlwdmd;
  1085. mlwork = f2cmax(i__1,i__2);
  1086. /* Computing MAX */
  1087. i__1 = mlrwrk, i__2 = (integer) work[1];
  1088. mlrwrk = f2cmax(i__1,i__2);
  1089. iminwr = f2cmax(iminwr,iwork[1]);
  1090. if (lquery) {
  1091. olwdmd = (integer) zwork[2].r;
  1092. /* Computing MAX */
  1093. i__1 = olwork, i__2 = minmn + olwdmd;
  1094. olwork = f2cmax(i__1,i__2);
  1095. }
  1096. if (wntvec || wntvcf) {
  1097. mlwmqr = f2cmax(1,*n);
  1098. /* Computing MAX */
  1099. i__1 = mlwork, i__2 = minmn + mlwmqr;
  1100. mlwork = f2cmax(i__1,i__2);
  1101. if (lquery) {
  1102. cunmqr_("L", "N", m, n, &minmn, &f[f_offset], ldf, &zwork[1],
  1103. &z__[z_offset], ldz, &zwork[1], &c_n1, &info1);
  1104. olwmqr = (integer) zwork[1].r;
  1105. /* Computing MAX */
  1106. i__1 = olwork, i__2 = minmn + olwmqr;
  1107. olwork = f2cmax(i__1,i__2);
  1108. }
  1109. }
  1110. if (wantq) {
  1111. mlwgqr = f2cmax(1,*n);
  1112. /* Computing MAX */
  1113. i__1 = mlwork, i__2 = minmn + mlwgqr;
  1114. mlwork = f2cmax(i__1,i__2);
  1115. if (lquery) {
  1116. cungqr_(m, &minmn, &minmn, &f[f_offset], ldf, &zwork[1], &
  1117. zwork[1], &c_n1, &info1);
  1118. olwgqr = (integer) zwork[1].r;
  1119. /* Computing MAX */
  1120. i__1 = olwork, i__2 = minmn + olwgqr;
  1121. olwork = f2cmax(i__1,i__2);
  1122. }
  1123. }
  1124. if (*liwork < iminwr && ! lquery) {
  1125. *info = -34;
  1126. }
  1127. if (*lwork < mlrwrk && ! lquery) {
  1128. *info = -32;
  1129. }
  1130. if (*lzwork < mlwork && ! lquery) {
  1131. *info = -30;
  1132. }
  1133. }
  1134. if (*info != 0) {
  1135. i__1 = -(*info);
  1136. xerbla_("CGEDMDQ", &i__1);
  1137. return 0;
  1138. } else if (lquery) {
  1139. /* Return minimal and optimal workspace sizes */
  1140. iwork[1] = iminwr;
  1141. zwork[1].r = (real) mlwork, zwork[1].i = 0.f;
  1142. zwork[2].r = (real) olwork, zwork[2].i = 0.f;
  1143. work[1] = (real) mlrwrk;
  1144. work[2] = (real) mlrwrk;
  1145. return 0;
  1146. }
  1147. /* ..... */
  1148. /* Initial QR factorization that is used to represent the */
  1149. /* snapshots as elements of lower dimensional subspace. */
  1150. /* For large scale computation with M >>N , at this place */
  1151. /* one can use an out of core QRF. */
  1152. i__1 = *lzwork - minmn;
  1153. cgeqrf_(m, n, &f[f_offset], ldf, &zwork[1], &zwork[minmn + 1], &i__1, &
  1154. info1);
  1155. /* Define X and Y as the snapshots representations in the */
  1156. /* orthogonal basis computed in the QR factorization. */
  1157. /* X corresponds to the leading N-1 and Y to the trailing */
  1158. /* N-1 snapshots. */
  1159. i__1 = *n - 1;
  1160. claset_("L", &minmn, &i__1, &zzero, &zzero, &x[x_offset], ldx);
  1161. i__1 = *n - 1;
  1162. clacpy_("U", &minmn, &i__1, &f[f_offset], ldf, &x[x_offset], ldx);
  1163. i__1 = *n - 1;
  1164. clacpy_("A", &minmn, &i__1, &f[(f_dim1 << 1) + 1], ldf, &y[y_offset], ldy);
  1165. if (*m >= 3) {
  1166. i__1 = minmn - 2;
  1167. i__2 = *n - 2;
  1168. claset_("L", &i__1, &i__2, &zzero, &zzero, &y[y_dim1 + 3], ldy);
  1169. }
  1170. /* Compute the DMD of the projected snapshot pairs (X,Y) */
  1171. i__1 = *n - 1;
  1172. i__2 = *lzwork - minmn;
  1173. cgedmd_(jobs, jobvl, jobr, jobf, whtsvd, &minmn, &i__1, &x[x_offset], ldx,
  1174. &y[y_offset], ldy, nrnk, tol, k, &eigs[1], &z__[z_offset], ldz, &
  1175. res[1], &b[b_offset], ldb, &v[v_offset], ldv, &s[s_offset], lds, &
  1176. zwork[minmn + 1], &i__2, &work[1], lwork, &iwork[1], liwork, &
  1177. info1);
  1178. if (info1 == 2 || info1 == 3) {
  1179. /* Return with error code. See CGEDMD for details. */
  1180. *info = info1;
  1181. return 0;
  1182. } else {
  1183. *info = info1;
  1184. }
  1185. /* The Ritz vectors (Koopman modes) can be explicitly */
  1186. /* formed or returned in factored form. */
  1187. if (wntvec) {
  1188. /* Compute the eigenvectors explicitly. */
  1189. if (*m > minmn) {
  1190. i__1 = *m - minmn;
  1191. claset_("A", &i__1, k, &zzero, &zzero, &z__[minmn + 1 + z_dim1],
  1192. ldz);
  1193. }
  1194. i__1 = *lzwork - minmn;
  1195. cunmqr_("L", "N", m, k, &minmn, &f[f_offset], ldf, &zwork[1], &z__[
  1196. z_offset], ldz, &zwork[minmn + 1], &i__1, &info1);
  1197. } else if (wntvcf) {
  1198. /* Return the Ritz vectors (eigenvectors) in factored */
  1199. /* form Z*V, where Z contains orthonormal matrix (the */
  1200. /* product of Q from the initial QR factorization and */
  1201. /* the SVD/POD_basis returned by CGEDMD in X) and the */
  1202. /* second factor (the eigenvectors of the Rayleigh */
  1203. /* quotient) is in the array V, as returned by CGEDMD. */
  1204. clacpy_("A", n, k, &x[x_offset], ldx, &z__[z_offset], ldz);
  1205. if (*m > *n) {
  1206. i__1 = *m - *n;
  1207. claset_("A", &i__1, k, &zzero, &zzero, &z__[*n + 1 + z_dim1], ldz);
  1208. }
  1209. i__1 = *lzwork - minmn;
  1210. cunmqr_("L", "N", m, k, &minmn, &f[f_offset], ldf, &zwork[1], &z__[
  1211. z_offset], ldz, &zwork[minmn + 1], &i__1, &info1);
  1212. }
  1213. /* Some optional output variables: */
  1214. /* The upper triangular factor R in the initial QR */
  1215. /* factorization is optionally returned in the array Y. */
  1216. /* This is useful if this call to CGEDMDQ is to be */
  1217. /* followed by a streaming DMD that is implemented in a */
  1218. /* QR compressed form. */
  1219. if (wnttrf) {
  1220. /* Return the upper triangular R in Y */
  1221. claset_("A", &minmn, n, &zzero, &zzero, &y[y_offset], ldy);
  1222. clacpy_("U", &minmn, n, &f[f_offset], ldf, &y[y_offset], ldy);
  1223. }
  1224. /* The orthonormal/unitary factor Q in the initial QR */
  1225. /* factorization is optionally returned in the array F. */
  1226. /* Same as with the triangular factor above, this is */
  1227. /* useful in a streaming DMD. */
  1228. if (wantq) {
  1229. /* Q overwrites F */
  1230. i__1 = *lzwork - minmn;
  1231. cungqr_(m, &minmn, &minmn, &f[f_offset], ldf, &zwork[1], &zwork[minmn
  1232. + 1], &i__1, &info1);
  1233. }
  1234. return 0;
  1235. } /* cgedmdq_ */