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.

sgedmdq.c 47 kB

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