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.

sgedmd.c 62 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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. static integer c__1 = 1;
  490. static integer c__0 = 0;
  491. static integer c__2 = 2;
  492. /* Subroutine */ int sgedmd_(char *jobs, char *jobz, char *jobr, char *jobf,
  493. integer *whtsvd, integer *m, integer *n, real *x, integer *ldx, real *
  494. y, integer *ldy, integer *nrnk, real *tol, integer *k, real *reig,
  495. real *imeig, real *z__, integer *ldz, real *res, real *b, integer *
  496. ldb, real *w, integer *ldw, real *s, integer *lds, real *work,
  497. integer *lwork, integer *iwork, integer *liwork, integer *info)
  498. {
  499. /* System generated locals */
  500. integer x_dim1, x_offset, y_dim1, y_offset, z_dim1, z_offset, b_dim1,
  501. b_offset, w_dim1, w_offset, s_dim1, s_offset, i__1, i__2;
  502. real r__1, r__2;
  503. /* Local variables */
  504. real zero, ssum;
  505. integer info1, info2;
  506. real xscl1, xscl2;
  507. extern real snrm2_(integer *, real *, integer *);
  508. integer i__, j;
  509. real scale;
  510. extern logical lsame_(char *, char *);
  511. extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
  512. logical badxy;
  513. real small;
  514. extern /* Subroutine */ int sgemm_(char *, char *, integer *, integer *,
  515. integer *, real *, real *, integer *, real *, integer *, real *,
  516. real *, integer *), sgeev_(char *, char *,
  517. integer *, real *, integer *, real *, real *, real *, integer *,
  518. real *, integer *, real *, integer *, integer *);
  519. char jobzl[1];
  520. extern /* Subroutine */ int saxpy_(integer *, real *, real *, integer *,
  521. real *, integer *);
  522. logical wntex;
  523. real ab[4] /* was [2][2] */;
  524. extern real slamch_(char *), slange_(char *, integer *, integer *,
  525. real *, integer *, real *);
  526. extern /* Subroutine */ int sgesdd_(char *, integer *, integer *, real *,
  527. integer *, real *, real *, integer *, real *, integer *, real *,
  528. integer *, integer *, integer *), xerbla_(char *, integer
  529. *);
  530. char t_or_n__[1];
  531. extern /* Subroutine */ int slascl_(char *, integer *, integer *, real *,
  532. real *, integer *, integer *, real *, integer *, integer *);
  533. extern integer isamax_(integer *, real *, integer *);
  534. logical sccolx, sccoly;
  535. extern logical sisnan_(real *);
  536. extern /* Subroutine */ int sgesvd_(char *, char *, integer *, integer *,
  537. real *, integer *, real *, real *, integer *, real *, integer *,
  538. real *, integer *, integer *);
  539. integer lwrsdd, mwrsdd;
  540. extern /* Subroutine */ int sgejsv_(char *, char *, char *, char *, char *
  541. , char *, integer *, integer *, real *, integer *, real *, real *,
  542. integer *, real *, integer *, real *, integer *, integer *,
  543. integer *),
  544. slacpy_(char *, integer *, integer *, real *, integer *, real *,
  545. integer *);
  546. integer iminwr;
  547. logical wntref, wntvec;
  548. real rootsc;
  549. integer lwrkev, mlwork, mwrkev, numrnk, olwork;
  550. real rdummy[2];
  551. integer lwrsvd, mwrsvd;
  552. logical lquery, wntres;
  553. char jsvopt[1];
  554. extern /* Subroutine */ int slassq_(integer *, real *, integer *, real *,
  555. real *), mecago_();
  556. integer mwrsvj, lwrsvq, mwrsvq;
  557. real rdummy2[2], ofl, one;
  558. extern /* Subroutine */ int sgesvdq_(char *, char *, char *, char *, char
  559. *, integer *, integer *, real *, integer *, real *, real *,
  560. integer *, real *, integer *, integer *, integer *, integer *,
  561. real *, integer *, real *, integer *, integer *);
  562. /* March 2023 */
  563. /* ..... */
  564. /* USE iso_fortran_env */
  565. /* INTEGER, PARAMETER :: WP = real32 */
  566. /* ..... */
  567. /* Scalar arguments */
  568. /* Array arguments */
  569. /* ............................................................ */
  570. /* Purpose */
  571. /* ======= */
  572. /* SGEDMD computes the Dynamic Mode Decomposition (DMD) for */
  573. /* a pair of data snapshot matrices. For the input matrices */
  574. /* X and Y such that Y = A*X with an unaccessible matrix */
  575. /* A, SGEDMD computes a certain number of Ritz pairs of A using */
  576. /* the standard Rayleigh-Ritz extraction from a subspace of */
  577. /* range(X) that is determined using the leading left singular */
  578. /* vectors of X. Optionally, SGEDMD returns the residuals */
  579. /* of the computed Ritz pairs, the information needed for */
  580. /* a refinement of the Ritz vectors, or the eigenvectors of */
  581. /* the Exact DMD. */
  582. /* For further details see the references listed */
  583. /* below. For more details of the implementation see [3]. */
  584. /* References */
  585. /* ========== */
  586. /* [1] P. Schmid: Dynamic mode decomposition of numerical */
  587. /* and experimental data, */
  588. /* Journal of Fluid Mechanics 656, 5-28, 2010. */
  589. /* [2] Z. Drmac, I. Mezic, R. Mohr: Data driven modal */
  590. /* decompositions: analysis and enhancements, */
  591. /* SIAM J. on Sci. Comp. 40 (4), A2253-A2285, 2018. */
  592. /* [3] Z. Drmac: A LAPACK implementation of the Dynamic */
  593. /* Mode Decomposition I. Technical report. AIMDyn Inc. */
  594. /* and LAPACK Working Note 298. */
  595. /* [4] J. Tu, C. W. Rowley, D. M. Luchtenburg, S. L. */
  596. /* Brunton, N. Kutz: On Dynamic Mode Decomposition: */
  597. /* Theory and Applications, Journal of Computational */
  598. /* Dynamics 1(2), 391 -421, 2014. */
  599. /* ...................................................................... */
  600. /* Developed and supported by: */
  601. /* =========================== */
  602. /* Developed and coded by Zlatko Drmac, Faculty of Science, */
  603. /* University of Zagreb; drmac@math.hr */
  604. /* In cooperation with */
  605. /* AIMdyn Inc., Santa Barbara, CA. */
  606. /* and supported by */
  607. /* - DARPA SBIR project "Koopman Operator-Based Forecasting */
  608. /* for Nonstationary Processes from Near-Term, Limited */
  609. /* Observational Data" Contract No: W31P4Q-21-C-0007 */
  610. /* - DARPA PAI project "Physics-Informed Machine Learning */
  611. /* Methodologies" Contract No: HR0011-18-9-0033 */
  612. /* - DARPA MoDyL project "A Data-Driven, Operator-Theoretic */
  613. /* Framework for Space-Time Analysis of Process Dynamics" */
  614. /* Contract No: HR0011-16-C-0116 */
  615. /* Any opinions, findings and conclusions or recommendations */
  616. /* expressed in this material are those of the author and */
  617. /* do not necessarily reflect the views of the DARPA SBIR */
  618. /* Program Office */
  619. /* ============================================================ */
  620. /* Distribution Statement A: */
  621. /* Approved for Public Release, Distribution Unlimited. */
  622. /* Cleared by DARPA on September 29, 2022 */
  623. /* ============================================================ */
  624. /* ...................................................................... */
  625. /* Arguments */
  626. /* ========= */
  627. /* JOBS (input) CHARACTER*1 */
  628. /* Determines whether the initial data snapshots are scaled */
  629. /* by a diagonal matrix. */
  630. /* 'S' :: The data snapshots matrices X and Y are multiplied */
  631. /* with a diagonal matrix D so that X*D has unit */
  632. /* nonzero columns (in the Euclidean 2-norm) */
  633. /* 'C' :: The snapshots are scaled as with the 'S' option. */
  634. /* If it is found that an i-th column of X is zero */
  635. /* vector and the corresponding i-th column of Y is */
  636. /* non-zero, then the i-th column of Y is set to */
  637. /* zero and a warning flag is raised. */
  638. /* 'Y' :: The data snapshots matrices X and Y are multiplied */
  639. /* by a diagonal matrix D so that Y*D has unit */
  640. /* nonzero columns (in the Euclidean 2-norm) */
  641. /* 'N' :: No data scaling. */
  642. /* ..... */
  643. /* JOBZ (input) CHARACTER*1 */
  644. /* Determines whether the eigenvectors (Koopman modes) will */
  645. /* be computed. */
  646. /* 'V' :: The eigenvectors (Koopman modes) will be computed */
  647. /* and returned in the matrix Z. */
  648. /* See the description of Z. */
  649. /* 'F' :: The eigenvectors (Koopman modes) will be returned */
  650. /* in factored form as the product X(:,1:K)*W, where X */
  651. /* contains a POD basis (leading left singular vectors */
  652. /* of the data matrix X) and W contains the eigenvectors */
  653. /* of the corresponding Rayleigh quotient. */
  654. /* See the descriptions of K, X, W, Z. */
  655. /* 'N' :: The eigenvectors are not computed. */
  656. /* ..... */
  657. /* JOBR (input) CHARACTER*1 */
  658. /* Determines whether to compute the residuals. */
  659. /* 'R' :: The residuals for the computed eigenpairs will be */
  660. /* computed and stored in the array RES. */
  661. /* See the description of RES. */
  662. /* For this option to be legal, JOBZ must be 'V'. */
  663. /* 'N' :: The residuals are not computed. */
  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. /* ..... */
  676. /* WHTSVD (input) INTEGER, WHSTVD in { 1, 2, 3, 4 } */
  677. /* Allows for a selection of the SVD algorithm from the */
  678. /* LAPACK library. */
  679. /* 1 :: SGESVD (the QR SVD algorithm) */
  680. /* 2 :: SGESDD (the Divide and Conquer algorithm; if enough */
  681. /* workspace available, this is the fastest option) */
  682. /* 3 :: SGESVDQ (the preconditioned QR SVD ; this and 4 */
  683. /* are the most accurate options) */
  684. /* 4 :: SGEJSV (the preconditioned Jacobi SVD; this and 3 */
  685. /* are the most accurate options) */
  686. /* For the four methods above, a significant difference in */
  687. /* the accuracy of small singular values is possible if */
  688. /* the snapshots vary in norm so that X is severely */
  689. /* ill-conditioned. If small (smaller than EPS*||X||) */
  690. /* singular values are of interest and JOBS=='N', then */
  691. /* the options (3, 4) give the most accurate results, where */
  692. /* the option 4 is slightly better and with stronger */
  693. /* theoretical background. */
  694. /* If JOBS=='S', i.e. the columns of X will be normalized, */
  695. /* then all methods give nearly equally accurate results. */
  696. /* ..... */
  697. /* M (input) INTEGER, M>= 0 */
  698. /* The state space dimension (the row dimension of X, Y). */
  699. /* ..... */
  700. /* N (input) INTEGER, 0 <= N <= M */
  701. /* The number of data snapshot pairs */
  702. /* (the number of columns of X and Y). */
  703. /* ..... */
  704. /* X (input/output) REAL(KIND=WP) M-by-N array */
  705. /* > On entry, X contains the data snapshot matrix X. It is */
  706. /* assumed that the column norms of X are in the range of */
  707. /* the normalized floating point numbers. */
  708. /* < On exit, the leading K columns of X contain a POD basis, */
  709. /* i.e. the leading K left singular vectors of the input */
  710. /* data matrix X, U(:,1:K). All N columns of X contain all */
  711. /* left singular vectors of the input matrix X. */
  712. /* See the descriptions of K, Z and W. */
  713. /* ..... */
  714. /* LDX (input) INTEGER, LDX >= M */
  715. /* The leading dimension of the array X. */
  716. /* ..... */
  717. /* Y (input/workspace/output) REAL(KIND=WP) M-by-N array */
  718. /* > On entry, Y contains the data snapshot matrix Y */
  719. /* < On exit, */
  720. /* If JOBR == 'R', the leading K columns of Y contain */
  721. /* the residual vectors for the computed Ritz pairs. */
  722. /* See the description of RES. */
  723. /* If JOBR == 'N', Y contains the original input data, */
  724. /* scaled according to the value of JOBS. */
  725. /* ..... */
  726. /* LDY (input) INTEGER , LDY >= M */
  727. /* The leading dimension of the array Y. */
  728. /* ..... */
  729. /* NRNK (input) INTEGER */
  730. /* Determines the mode how to compute the numerical rank, */
  731. /* i.e. how to truncate small singular values of the input */
  732. /* matrix X. On input, if */
  733. /* NRNK = -1 :: i-th singular value sigma(i) is truncated */
  734. /* if sigma(i) <= TOL*sigma(1) */
  735. /* This option is recommended. */
  736. /* NRNK = -2 :: i-th singular value sigma(i) is truncated */
  737. /* if sigma(i) <= TOL*sigma(i-1) */
  738. /* This option is included for R&D purposes. */
  739. /* It requires highly accurate SVD, which */
  740. /* may not be feasible. */
  741. /* The numerical rank can be enforced by using positive */
  742. /* value of NRNK as follows: */
  743. /* 0 < NRNK <= N :: at most NRNK largest singular values */
  744. /* will be used. If the number of the computed nonzero */
  745. /* singular values is less than NRNK, then only those */
  746. /* nonzero values will be used and the actually used */
  747. /* dimension is less than NRNK. The actual number of */
  748. /* the nonzero singular values is returned in the variable */
  749. /* K. See the descriptions of TOL and K. */
  750. /* ..... */
  751. /* TOL (input) REAL(KIND=WP), 0 <= TOL < 1 */
  752. /* The tolerance for truncating small singular values. */
  753. /* See the description of NRNK. */
  754. /* ..... */
  755. /* K (output) INTEGER, 0 <= K <= N */
  756. /* The dimension of the POD basis for the data snapshot */
  757. /* matrix X and the number of the computed Ritz pairs. */
  758. /* The value of K is determined according to the rule set */
  759. /* by the parameters NRNK and TOL. */
  760. /* See the descriptions of NRNK and TOL. */
  761. /* ..... */
  762. /* REIG (output) REAL(KIND=WP) N-by-1 array */
  763. /* The leading K (K<=N) entries of REIG contain */
  764. /* the real parts of the computed eigenvalues */
  765. /* REIG(1:K) + sqrt(-1)*IMEIG(1:K). */
  766. /* See the descriptions of K, IMEIG, and Z. */
  767. /* ..... */
  768. /* IMEIG (output) REAL(KIND=WP) N-by-1 array */
  769. /* The leading K (K<=N) entries of IMEIG contain */
  770. /* the imaginary parts of the computed eigenvalues */
  771. /* REIG(1:K) + sqrt(-1)*IMEIG(1:K). */
  772. /* The eigenvalues are determined as follows: */
  773. /* If IMEIG(i) == 0, then the corresponding eigenvalue is */
  774. /* real, LAMBDA(i) = REIG(i). */
  775. /* If IMEIG(i)>0, then the corresponding complex */
  776. /* conjugate pair of eigenvalues reads */
  777. /* LAMBDA(i) = REIG(i) + sqrt(-1)*IMAG(i) */
  778. /* LAMBDA(i+1) = REIG(i) - sqrt(-1)*IMAG(i) */
  779. /* That is, complex conjugate pairs have consecutive */
  780. /* indices (i,i+1), with the positive imaginary part */
  781. /* listed first. */
  782. /* See the descriptions of K, REIG, and Z. */
  783. /* ..... */
  784. /* Z (workspace/output) REAL(KIND=WP) M-by-N array */
  785. /* If JOBZ =='V' then */
  786. /* Z contains real Ritz vectors as follows: */
  787. /* If IMEIG(i)=0, then Z(:,i) is an eigenvector of */
  788. /* the i-th Ritz value; ||Z(:,i)||_2=1. */
  789. /* If IMEIG(i) > 0 (and IMEIG(i+1) < 0) then */
  790. /* [Z(:,i) Z(:,i+1)] span an invariant subspace and */
  791. /* the Ritz values extracted from this subspace are */
  792. /* REIG(i) + sqrt(-1)*IMEIG(i) and */
  793. /* REIG(i) - sqrt(-1)*IMEIG(i). */
  794. /* The corresponding eigenvectors are */
  795. /* Z(:,i) + sqrt(-1)*Z(:,i+1) and */
  796. /* Z(:,i) - sqrt(-1)*Z(:,i+1), respectively. */
  797. /* || Z(:,i:i+1)||_F = 1. */
  798. /* If JOBZ == 'F', then the above descriptions hold for */
  799. /* the columns of X(:,1:K)*W(1:K,1:K), where the columns */
  800. /* of W(1:k,1:K) are the computed eigenvectors of the */
  801. /* K-by-K Rayleigh quotient. The columns of W(1:K,1:K) */
  802. /* are similarly structured: If IMEIG(i) == 0 then */
  803. /* X(:,1:K)*W(:,i) is an eigenvector, and if IMEIG(i)>0 */
  804. /* then X(:,1:K)*W(:,i)+sqrt(-1)*X(:,1:K)*W(:,i+1) and */
  805. /* X(:,1:K)*W(:,i)-sqrt(-1)*X(:,1:K)*W(:,i+1) */
  806. /* are the eigenvectors of LAMBDA(i), LAMBDA(i+1). */
  807. /* See the descriptions of REIG, IMEIG, X and W. */
  808. /* ..... */
  809. /* LDZ (input) INTEGER , LDZ >= M */
  810. /* The leading dimension of the array Z. */
  811. /* ..... */
  812. /* RES (output) REAL(KIND=WP) N-by-1 array */
  813. /* RES(1:K) contains the residuals for the K computed */
  814. /* Ritz pairs. */
  815. /* If LAMBDA(i) is real, then */
  816. /* RES(i) = || A * Z(:,i) - LAMBDA(i)*Z(:,i))||_2. */
  817. /* If [LAMBDA(i), LAMBDA(i+1)] is a complex conjugate pair */
  818. /* then */
  819. /* RES(i)=RES(i+1) = || A * Z(:,i:i+1) - Z(:,i:i+1) *B||_F */
  820. /* where B = [ real(LAMBDA(i)) imag(LAMBDA(i)) ] */
  821. /* [-imag(LAMBDA(i)) real(LAMBDA(i)) ]. */
  822. /* It holds that */
  823. /* RES(i) = || A*ZC(:,i) - LAMBDA(i) *ZC(:,i) ||_2 */
  824. /* RES(i+1) = || A*ZC(:,i+1) - LAMBDA(i+1)*ZC(:,i+1) ||_2 */
  825. /* where ZC(:,i) = Z(:,i) + sqrt(-1)*Z(:,i+1) */
  826. /* ZC(:,i+1) = Z(:,i) - sqrt(-1)*Z(:,i+1) */
  827. /* See the description of REIG, IMEIG and Z. */
  828. /* ..... */
  829. /* B (output) REAL(KIND=WP) M-by-N array. */
  830. /* IF JOBF =='R', B(1:M,1:K) contains A*U(:,1:K), and can */
  831. /* be used for computing the refined vectors; see further */
  832. /* details in the provided references. */
  833. /* If JOBF == 'E', B(1:M,1;K) contains */
  834. /* A*U(:,1:K)*W(1:K,1:K), which are the vectors from the */
  835. /* Exact DMD, up to scaling by the inverse eigenvalues. */
  836. /* If JOBF =='N', then B is not referenced. */
  837. /* See the descriptions of X, W, K. */
  838. /* ..... */
  839. /* LDB (input) INTEGER, LDB >= M */
  840. /* The leading dimension of the array B. */
  841. /* ..... */
  842. /* W (workspace/output) REAL(KIND=WP) N-by-N array */
  843. /* On exit, W(1:K,1:K) contains the K computed */
  844. /* eigenvectors of the matrix Rayleigh quotient (real and */
  845. /* imaginary parts for each complex conjugate pair of the */
  846. /* eigenvalues). The Ritz vectors (returned in Z) are the */
  847. /* product of X (containing a POD basis for the input */
  848. /* matrix X) and W. See the descriptions of K, S, X and Z. */
  849. /* W is also used as a workspace to temporarily store the */
  850. /* left singular vectors of X. */
  851. /* ..... */
  852. /* LDW (input) INTEGER, LDW >= N */
  853. /* The leading dimension of the array W. */
  854. /* ..... */
  855. /* S (workspace/output) REAL(KIND=WP) N-by-N array */
  856. /* The array S(1:K,1:K) is used for the matrix Rayleigh */
  857. /* quotient. This content is overwritten during */
  858. /* the eigenvalue decomposition by SGEEV. */
  859. /* See the description of K. */
  860. /* ..... */
  861. /* LDS (input) INTEGER, LDS >= N */
  862. /* The leading dimension of the array S. */
  863. /* ..... */
  864. /* WORK (workspace/output) REAL(KIND=WP) LWORK-by-1 array */
  865. /* On exit, WORK(1:N) contains the singular values of */
  866. /* X (for JOBS=='N') or column scaled X (JOBS=='S', 'C'). */
  867. /* If WHTSVD==4, then WORK(N+1) and WORK(N+2) contain */
  868. /* scaling factor WORK(N+2)/WORK(N+1) used to scale X */
  869. /* and Y to avoid overflow in the SVD of X. */
  870. /* This may be of interest if the scaling option is off */
  871. /* and as many as possible smallest eigenvalues are */
  872. /* desired to the highest feasible accuracy. */
  873. /* If the call to SGEDMD is only workspace query, then */
  874. /* WORK(1) contains the minimal workspace length and */
  875. /* WORK(2) is the optimal workspace length. Hence, the */
  876. /* length of work is at least 2. */
  877. /* See the description of LWORK. */
  878. /* ..... */
  879. /* LWORK (input) INTEGER */
  880. /* The minimal length of the workspace vector WORK. */
  881. /* LWORK is calculated as follows: */
  882. /* If WHTSVD == 1 :: */
  883. /* If JOBZ == 'V', then */
  884. /* LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,4*N)). */
  885. /* If JOBZ == 'N' then */
  886. /* LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,3*N)). */
  887. /* Here LWORK_SVD = MAX(1,3*N+M,5*N) is the minimal */
  888. /* workspace length of SGESVD. */
  889. /* If WHTSVD == 2 :: */
  890. /* If JOBZ == 'V', then */
  891. /* LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,4*N)) */
  892. /* If JOBZ == 'N', then */
  893. /* LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,3*N)) */
  894. /* Here LWORK_SVD = MAX(M, 5*N*N+4*N)+3*N*N is the */
  895. /* minimal workspace length of SGESDD. */
  896. /* If WHTSVD == 3 :: */
  897. /* If JOBZ == 'V', then */
  898. /* LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,4*N)) */
  899. /* If JOBZ == 'N', then */
  900. /* LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,3*N)) */
  901. /* Here LWORK_SVD = N+M+MAX(3*N+1, */
  902. /* MAX(1,3*N+M,5*N),MAX(1,N)) */
  903. /* is the minimal workspace length of SGESVDQ. */
  904. /* If WHTSVD == 4 :: */
  905. /* If JOBZ == 'V', then */
  906. /* LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,4*N)) */
  907. /* If JOBZ == 'N', then */
  908. /* LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,3*N)) */
  909. /* Here LWORK_SVD = MAX(7,2*M+N,6*N+2*N*N) is the */
  910. /* minimal workspace length of SGEJSV. */
  911. /* The above expressions are not simplified in order to */
  912. /* make the usage of WORK more transparent, and for */
  913. /* easier checking. In any case, LWORK >= 2. */
  914. /* If on entry LWORK = -1, then a workspace query is */
  915. /* assumed and the procedure only computes the minimal */
  916. /* and the optimal workspace lengths for both WORK and */
  917. /* IWORK. See the descriptions of WORK and IWORK. */
  918. /* ..... */
  919. /* IWORK (workspace/output) INTEGER LIWORK-by-1 array */
  920. /* Workspace that is required only if WHTSVD equals */
  921. /* 2 , 3 or 4. (See the description of WHTSVD). */
  922. /* If on entry LWORK =-1 or LIWORK=-1, then the */
  923. /* minimal length of IWORK is computed and returned in */
  924. /* IWORK(1). See the description of LIWORK. */
  925. /* ..... */
  926. /* LIWORK (input) INTEGER */
  927. /* The minimal length of the workspace vector IWORK. */
  928. /* If WHTSVD == 1, then only IWORK(1) is used; LIWORK >=1 */
  929. /* If WHTSVD == 2, then LIWORK >= MAX(1,8*MIN(M,N)) */
  930. /* If WHTSVD == 3, then LIWORK >= MAX(1,M+N-1) */
  931. /* If WHTSVD == 4, then LIWORK >= MAX(3,M+3*N) */
  932. /* If on entry LIWORK = -1, then a workspace 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 arrays */
  961. /* ~~~~~~~~~~~~ */
  962. /* External functions (BLAS and LAPACK) */
  963. /* ~~~~~~~~~~~~~~~~~ */
  964. /* External subroutines (BLAS and LAPACK) */
  965. /* ~~~~~~~~~~~~~~~~~~~~ */
  966. /* Intrinsic functions */
  967. /* ~~~~~~~~~~~~~~~~~~~ */
  968. /* ............................................................ */
  969. /* Parameter adjustments */
  970. x_dim1 = *ldx;
  971. x_offset = 1 + x_dim1 * 1;
  972. x -= x_offset;
  973. y_dim1 = *ldy;
  974. y_offset = 1 + y_dim1 * 1;
  975. y -= y_offset;
  976. --reig;
  977. --imeig;
  978. z_dim1 = *ldz;
  979. z_offset = 1 + z_dim1 * 1;
  980. z__ -= z_offset;
  981. --res;
  982. b_dim1 = *ldb;
  983. b_offset = 1 + b_dim1 * 1;
  984. b -= b_offset;
  985. w_dim1 = *ldw;
  986. w_offset = 1 + w_dim1 * 1;
  987. w -= w_offset;
  988. s_dim1 = *lds;
  989. s_offset = 1 + s_dim1 * 1;
  990. s -= s_offset;
  991. --work;
  992. --iwork;
  993. /* Function Body */
  994. one = 1.f;
  995. zero = 0.f;
  996. /* Test the input arguments */
  997. wntres = lsame_(jobr, "R");
  998. sccolx = lsame_(jobs, "S") || lsame_(jobs, "C");
  999. sccoly = lsame_(jobs, "Y");
  1000. wntvec = lsame_(jobz, "V");
  1001. wntref = lsame_(jobf, "R");
  1002. wntex = lsame_(jobf, "E");
  1003. *info = 0;
  1004. lquery = *lwork == -1 || *liwork == -1;
  1005. if (! (sccolx || sccoly || lsame_(jobs, "N"))) {
  1006. *info = -1;
  1007. } else if (! (wntvec || lsame_(jobz, "N") || lsame_(
  1008. jobz, "F"))) {
  1009. *info = -2;
  1010. } else if (! (wntres || lsame_(jobr, "N")) ||
  1011. wntres && ! wntvec) {
  1012. *info = -3;
  1013. } else if (! (wntref || wntex || lsame_(jobf, "N")))
  1014. {
  1015. *info = -4;
  1016. } else if (! (*whtsvd == 1 || *whtsvd == 2 || *whtsvd == 3 || *whtsvd ==
  1017. 4)) {
  1018. *info = -5;
  1019. } else if (*m < 0) {
  1020. *info = -6;
  1021. } else if (*n < 0 || *n > *m) {
  1022. *info = -7;
  1023. } else if (*ldx < *m) {
  1024. *info = -9;
  1025. } else if (*ldy < *m) {
  1026. *info = -11;
  1027. } else if (! (*nrnk == -2 || *nrnk == -1 || *nrnk >= 1 && *nrnk <= *n)) {
  1028. *info = -12;
  1029. } else if (*tol < zero || *tol >= one) {
  1030. *info = -13;
  1031. } else if (*ldz < *m) {
  1032. *info = -18;
  1033. } else if ((wntref || wntex) && *ldb < *m) {
  1034. *info = -21;
  1035. } else if (*ldw < *n) {
  1036. *info = -23;
  1037. } else if (*lds < *n) {
  1038. *info = -25;
  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) {
  1046. /* Quick return. All output except K is void. */
  1047. /* INFO=1 signals the void input. */
  1048. /* In case of a workspace query, the default */
  1049. /* minimal workspace lengths are returned. */
  1050. if (lquery) {
  1051. iwork[1] = 1;
  1052. work[1] = 2.f;
  1053. work[2] = 2.f;
  1054. } else {
  1055. *k = 0;
  1056. }
  1057. *info = 1;
  1058. return 0;
  1059. }
  1060. mlwork = f2cmax(2,*n);
  1061. olwork = f2cmax(2,*n);
  1062. iminwr = 1;
  1063. /* SELECT CASE ( WHTSVD ) */
  1064. if (*whtsvd == 1) {
  1065. /* The following is specified as the minimal */
  1066. /* length of WORK in the definition of SGESVD: */
  1067. /* MWRSVD = MAX(1,3*MIN(M,N)+MAX(M,N),5*MIN(M,N)) */
  1068. /* Computing MAX */
  1069. i__1 = 1, i__2 = f2cmin(*m,*n) * 3 + f2cmax(*m,*n), i__1 = f2cmax(i__1,
  1070. i__2), i__2 = f2cmin(*m,*n) * 5;
  1071. mwrsvd = f2cmax(i__1,i__2);
  1072. /* Computing MAX */
  1073. i__1 = mlwork, i__2 = *n + mwrsvd;
  1074. mlwork = f2cmax(i__1,i__2);
  1075. if (lquery) {
  1076. sgesvd_("O", "S", m, n, &x[x_offset], ldx, &work[1], &b[
  1077. b_offset], ldb, &w[w_offset], ldw, rdummy, &c_n1, &
  1078. info1);
  1079. /* Computing MAX */
  1080. i__1 = mwrsvd, i__2 = (integer) rdummy[0];
  1081. lwrsvd = f2cmax(i__1,i__2);
  1082. /* Computing MAX */
  1083. i__1 = olwork, i__2 = *n + lwrsvd;
  1084. olwork = f2cmax(i__1,i__2);
  1085. }
  1086. } else if (*whtsvd == 2) {
  1087. /* The following is specified as the minimal */
  1088. /* length of WORK in the definition of SGESDD: */
  1089. /* MWRSDD = 3*MIN(M,N)*MIN(M,N) + */
  1090. /* MAX( MAX(M,N),5*MIN(M,N)*MIN(M,N)+4*MIN(M,N) ) */
  1091. /* IMINWR = 8*MIN(M,N) */
  1092. /* Computing MAX */
  1093. i__1 = f2cmax(*m,*n), i__2 = f2cmin(*m,*n) * 5 * f2cmin(*m,*n) + (f2cmin(*m,*
  1094. n) << 2);
  1095. mwrsdd = f2cmin(*m,*n) * 3 * f2cmin(*m,*n) + f2cmax(i__1,i__2);
  1096. /* Computing MAX */
  1097. i__1 = mlwork, i__2 = *n + mwrsdd;
  1098. mlwork = f2cmax(i__1,i__2);
  1099. iminwr = f2cmin(*m,*n) << 3;
  1100. if (lquery) {
  1101. sgesdd_("O", m, n, &x[x_offset], ldx, &work[1], &b[b_offset],
  1102. ldb, &w[w_offset], ldw, rdummy, &c_n1, &iwork[1], &
  1103. info1);
  1104. /* Computing MAX */
  1105. i__1 = mwrsdd, i__2 = (integer) rdummy[0];
  1106. lwrsdd = f2cmax(i__1,i__2);
  1107. /* Computing MAX */
  1108. i__1 = olwork, i__2 = *n + lwrsdd;
  1109. olwork = f2cmax(i__1,i__2);
  1110. }
  1111. } else if (*whtsvd == 3) {
  1112. /* LWQP3 = 3*N+1 */
  1113. /* LWORQ = MAX(N, 1) */
  1114. /* MWRSVD = MAX(1,3*MIN(M,N)+MAX(M,N),5*MIN(M,N)) */
  1115. /* MWRSVQ = N + MAX( LWQP3, MWRSVD, LWORQ )+ MAX(M,2) */
  1116. /* MLWORK = N + MWRSVQ */
  1117. /* IMINWR = M+N-1 */
  1118. sgesvdq_("H", "P", "N", "R", "R", m, n, &x[x_offset], ldx, &work[
  1119. 1], &z__[z_offset], ldz, &w[w_offset], ldw, &numrnk, &
  1120. iwork[1], &c_n1, rdummy, &c_n1, rdummy2, &c_n1, &info1);
  1121. iminwr = iwork[1];
  1122. mwrsvq = (integer) rdummy[1];
  1123. /* Computing MAX */
  1124. i__1 = mlwork, i__2 = *n + mwrsvq + (integer) rdummy2[0];
  1125. mlwork = f2cmax(i__1,i__2);
  1126. if (lquery) {
  1127. lwrsvq = (integer) rdummy[0];
  1128. /* Computing MAX */
  1129. i__1 = olwork, i__2 = *n + lwrsvq + (integer) rdummy2[0];
  1130. olwork = f2cmax(i__1,i__2);
  1131. }
  1132. } else if (*whtsvd == 4) {
  1133. *(unsigned char *)jsvopt = 'J';
  1134. /* MWRSVJ = MAX( 7, 2*M+N, 6*N+2*N*N )! for JSVOPT='V' */
  1135. /* Computing MAX */
  1136. i__1 = 7, i__2 = (*m << 1) + *n, i__1 = f2cmax(i__1,i__2), i__2 = (*
  1137. n << 2) + *n * *n, i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1)
  1138. + *n * *n + 6;
  1139. mwrsvj = f2cmax(i__1,i__2);
  1140. /* Computing MAX */
  1141. i__1 = mlwork, i__2 = *n + mwrsvj;
  1142. mlwork = f2cmax(i__1,i__2);
  1143. /* Computing MAX */
  1144. i__1 = 3, i__2 = *m + *n * 3;
  1145. iminwr = f2cmax(i__1,i__2);
  1146. if (lquery) {
  1147. /* Computing MAX */
  1148. i__1 = olwork, i__2 = *n + mwrsvj;
  1149. olwork = f2cmax(i__1,i__2);
  1150. }
  1151. }
  1152. /* END SELECT */
  1153. if (wntvec || wntex || lsame_(jobz, "F")) {
  1154. *(unsigned char *)jobzl = 'V';
  1155. } else {
  1156. *(unsigned char *)jobzl = 'N';
  1157. }
  1158. /* Workspace calculation to the SGEEV call */
  1159. if (lsame_(jobzl, "V")) {
  1160. /* Computing MAX */
  1161. i__1 = 1, i__2 = *n << 2;
  1162. mwrkev = f2cmax(i__1,i__2);
  1163. } else {
  1164. /* Computing MAX */
  1165. i__1 = 1, i__2 = *n * 3;
  1166. mwrkev = f2cmax(i__1,i__2);
  1167. }
  1168. /* Computing MAX */
  1169. i__1 = mlwork, i__2 = *n + mwrkev;
  1170. mlwork = f2cmax(i__1,i__2);
  1171. if (lquery) {
  1172. sgeev_("N", jobzl, n, &s[s_offset], lds, &reig[1], &imeig[1], &w[
  1173. w_offset], ldw, &w[w_offset], ldw, rdummy, &c_n1, &info1);
  1174. /* Computing MAX */
  1175. i__1 = mwrkev, i__2 = (integer) rdummy[0];
  1176. lwrkev = f2cmax(i__1,i__2);
  1177. /* Computing MAX */
  1178. i__1 = olwork, i__2 = *n + lwrkev;
  1179. olwork = f2cmax(i__1,i__2);
  1180. }
  1181. if (*liwork < iminwr && ! lquery) {
  1182. *info = -29;
  1183. }
  1184. if (*lwork < mlwork && ! lquery) {
  1185. *info = -27;
  1186. }
  1187. }
  1188. if (*info != 0) {
  1189. i__1 = -(*info);
  1190. xerbla_("SGEDMD", &i__1);
  1191. return 0;
  1192. } else if (lquery) {
  1193. /* Return minimal and optimal workspace sizes */
  1194. iwork[1] = iminwr;
  1195. work[1] = (real) mlwork;
  1196. work[2] = (real) olwork;
  1197. return 0;
  1198. }
  1199. /* ............................................................ */
  1200. ofl = slamch_("O");
  1201. small = slamch_("S");
  1202. badxy = FALSE_;
  1203. /* <1> Optional scaling of the snapshots (columns of X, Y) */
  1204. /* ========================================================== */
  1205. if (sccolx) {
  1206. /* The columns of X will be normalized. */
  1207. /* To prevent overflows, the column norms of X are */
  1208. /* carefully computed using SLASSQ. */
  1209. *k = 0;
  1210. i__1 = *n;
  1211. for (i__ = 1; i__ <= i__1; ++i__) {
  1212. /* WORK(i) = DNRM2( M, X(1,i), 1 ) */
  1213. scale = zero;
  1214. slassq_(m, &x[i__ * x_dim1 + 1], &c__1, &scale, &ssum);
  1215. if (sisnan_(&scale) || sisnan_(&ssum)) {
  1216. *k = 0;
  1217. *info = -8;
  1218. i__2 = -(*info);
  1219. xerbla_("SGEDMD", &i__2);
  1220. }
  1221. if (scale != zero && ssum != zero) {
  1222. rootsc = sqrt(ssum);
  1223. if (scale >= ofl / rootsc) {
  1224. /* Norm of X(:,i) overflows. First, X(:,i) */
  1225. /* is scaled by */
  1226. /* ( ONE / ROOTSC ) / SCALE = 1/||X(:,i)||_2. */
  1227. /* Next, the norm of X(:,i) is stored without */
  1228. /* overflow as WORK(i) = - SCALE * (ROOTSC/M), */
  1229. /* the minus sign indicating the 1/M factor. */
  1230. /* Scaling is performed without overflow, and */
  1231. /* underflow may occur in the smallest entries */
  1232. /* of X(:,i). The relative backward and forward */
  1233. /* errors are small in the ell_2 norm. */
  1234. r__1 = one / rootsc;
  1235. slascl_("G", &c__0, &c__0, &scale, &r__1, m, &c__1, &x[
  1236. i__ * x_dim1 + 1], m, &info2);
  1237. work[i__] = -scale * (rootsc / (real) (*m));
  1238. } else {
  1239. /* X(:,i) will be scaled to unit 2-norm */
  1240. work[i__] = scale * rootsc;
  1241. slascl_("G", &c__0, &c__0, &work[i__], &one, m, &c__1, &x[
  1242. i__ * x_dim1 + 1], m, &info2);
  1243. /* X(1:M,i) = (ONE/WORK(i)) * X(1:M,i) ! INTRINSIC */
  1244. /* LAPACK */
  1245. }
  1246. } else {
  1247. work[i__] = zero;
  1248. ++(*k);
  1249. }
  1250. }
  1251. if (*k == *n) {
  1252. /* All columns of X are zero. Return error code -8. */
  1253. /* (the 8th input variable had an illegal value) */
  1254. *k = 0;
  1255. *info = -8;
  1256. i__1 = -(*info);
  1257. xerbla_("SGEDMD", &i__1);
  1258. return 0;
  1259. }
  1260. i__1 = *n;
  1261. for (i__ = 1; i__ <= i__1; ++i__) {
  1262. /* Now, apply the same scaling to the columns of Y. */
  1263. if (work[i__] > zero) {
  1264. r__1 = one / work[i__];
  1265. sscal_(m, &r__1, &y[i__ * y_dim1 + 1], &c__1);
  1266. /* Y(1:M,i) = (ONE/WORK(i)) * Y(1:M,i) ! INTRINSIC */
  1267. /* BLAS CALL */
  1268. } else if (work[i__] < zero) {
  1269. r__1 = -work[i__];
  1270. r__2 = one / (real) (*m);
  1271. slascl_("G", &c__0, &c__0, &r__1, &r__2, m, &c__1, &y[i__ *
  1272. y_dim1 + 1], m, &info2);
  1273. /* LAPACK CA */
  1274. } else if (y[isamax_(m, &y[i__ * y_dim1 + 1], &c__1) + i__ *
  1275. y_dim1] != zero) {
  1276. /* X(:,i) is zero vector. For consistency, */
  1277. /* Y(:,i) should also be zero. If Y(:,i) is not */
  1278. /* zero, then the data might be inconsistent or */
  1279. /* corrupted. If JOBS == 'C', Y(:,i) is set to */
  1280. /* zero and a warning flag is raised. */
  1281. /* The computation continues but the */
  1282. /* situation will be reported in the output. */
  1283. badxy = TRUE_;
  1284. if (lsame_(jobs, "C")) {
  1285. sscal_(m, &zero, &y[i__ * y_dim1 + 1], &c__1);
  1286. }
  1287. /* BLAS CALL */
  1288. }
  1289. }
  1290. }
  1291. if (sccoly) {
  1292. /* The columns of Y will be normalized. */
  1293. /* To prevent overflows, the column norms of Y are */
  1294. /* carefully computed using SLASSQ. */
  1295. i__1 = *n;
  1296. for (i__ = 1; i__ <= i__1; ++i__) {
  1297. /* WORK(i) = DNRM2( M, Y(1,i), 1 ) */
  1298. scale = zero;
  1299. slassq_(m, &y[i__ * y_dim1 + 1], &c__1, &scale, &ssum);
  1300. if (sisnan_(&scale) || sisnan_(&ssum)) {
  1301. *k = 0;
  1302. *info = -10;
  1303. i__2 = -(*info);
  1304. xerbla_("SGEDMD", &i__2);
  1305. }
  1306. if (scale != zero && ssum != zero) {
  1307. rootsc = sqrt(ssum);
  1308. if (scale >= ofl / rootsc) {
  1309. /* Norm of Y(:,i) overflows. First, Y(:,i) */
  1310. /* is scaled by */
  1311. /* ( ONE / ROOTSC ) / SCALE = 1/||Y(:,i)||_2. */
  1312. /* Next, the norm of Y(:,i) is stored without */
  1313. /* overflow as WORK(i) = - SCALE * (ROOTSC/M), */
  1314. /* the minus sign indicating the 1/M factor. */
  1315. /* Scaling is performed without overflow, and */
  1316. /* underflow may occur in the smallest entries */
  1317. /* of Y(:,i). The relative backward and forward */
  1318. /* errors are small in the ell_2 norm. */
  1319. r__1 = one / rootsc;
  1320. slascl_("G", &c__0, &c__0, &scale, &r__1, m, &c__1, &y[
  1321. i__ * y_dim1 + 1], m, &info2);
  1322. work[i__] = -scale * (rootsc / (real) (*m));
  1323. } else {
  1324. /* X(:,i) will be scaled to unit 2-norm */
  1325. work[i__] = scale * rootsc;
  1326. slascl_("G", &c__0, &c__0, &work[i__], &one, m, &c__1, &y[
  1327. i__ * y_dim1 + 1], m, &info2);
  1328. /* Y(1:M,i) = (ONE/WORK(i)) * Y(1:M,i) ! INTRINSIC */
  1329. /* LAPACK */
  1330. }
  1331. } else {
  1332. work[i__] = zero;
  1333. }
  1334. }
  1335. i__1 = *n;
  1336. for (i__ = 1; i__ <= i__1; ++i__) {
  1337. /* Now, apply the same scaling to the columns of X. */
  1338. if (work[i__] > zero) {
  1339. r__1 = one / work[i__];
  1340. sscal_(m, &r__1, &x[i__ * x_dim1 + 1], &c__1);
  1341. /* X(1:M,i) = (ONE/WORK(i)) * X(1:M,i) ! INTRINSIC */
  1342. /* BLAS CALL */
  1343. } else if (work[i__] < zero) {
  1344. r__1 = -work[i__];
  1345. r__2 = one / (real) (*m);
  1346. slascl_("G", &c__0, &c__0, &r__1, &r__2, m, &c__1, &x[i__ *
  1347. x_dim1 + 1], m, &info2);
  1348. /* LAPACK CA */
  1349. } else if (x[isamax_(m, &x[i__ * x_dim1 + 1], &c__1) + i__ *
  1350. x_dim1] != zero) {
  1351. /* Y(:,i) is zero vector. If X(:,i) is not */
  1352. /* zero, then a warning flag is raised. */
  1353. /* The computation continues but the */
  1354. /* situation will be reported in the output. */
  1355. badxy = TRUE_;
  1356. }
  1357. }
  1358. }
  1359. /* <2> SVD of the data snapshot matrix X. */
  1360. /* ===================================== */
  1361. /* The left singular vectors are stored in the array X. */
  1362. /* The right singular vectors are in the array W. */
  1363. /* The array W will later on contain the eigenvectors */
  1364. /* of a Rayleigh quotient. */
  1365. numrnk = *n;
  1366. /* SELECT CASE ( WHTSVD ) */
  1367. if (*whtsvd == 1) {
  1368. i__1 = *lwork - *n;
  1369. sgesvd_("O", "S", m, n, &x[x_offset], ldx, &work[1], &b[b_offset],
  1370. ldb, &w[w_offset], ldw, &work[*n + 1], &i__1, &info1);
  1371. /* LAPACK CAL */
  1372. *(unsigned char *)t_or_n__ = 'T';
  1373. } else if (*whtsvd == 2) {
  1374. i__1 = *lwork - *n;
  1375. sgesdd_("O", m, n, &x[x_offset], ldx, &work[1], &b[b_offset], ldb, &w[
  1376. w_offset], ldw, &work[*n + 1], &i__1, &iwork[1], &info1);
  1377. /* LAPACK CAL */
  1378. *(unsigned char *)t_or_n__ = 'T';
  1379. } else if (*whtsvd == 3) {
  1380. i__1 = *lwork - *n - f2cmax(2,*m);
  1381. i__2 = f2cmax(2,*m);
  1382. sgesvdq_("H", "P", "N", "R", "R", m, n, &x[x_offset], ldx, &work[1], &
  1383. z__[z_offset], ldz, &w[w_offset], ldw, &numrnk, &iwork[1],
  1384. liwork, &work[*n + f2cmax(2,*m) + 1], &i__1, &work[*n + 1], &
  1385. i__2, &info1);
  1386. slacpy_("A", m, &numrnk, &z__[z_offset], ldz, &x[x_offset], ldx);
  1387. /* LAPACK C */
  1388. *(unsigned char *)t_or_n__ = 'T';
  1389. } else if (*whtsvd == 4) {
  1390. i__1 = *lwork - *n;
  1391. sgejsv_("F", "U", jsvopt, "N", "N", "P", m, n, &x[x_offset], ldx, &
  1392. work[1], &z__[z_offset], ldz, &w[w_offset], ldw, &work[*n + 1]
  1393. , &i__1, &iwork[1], &info1);
  1394. /* LAPACK CALL */
  1395. slacpy_("A", m, n, &z__[z_offset], ldz, &x[x_offset], ldx);
  1396. /* LAPACK CALL */
  1397. *(unsigned char *)t_or_n__ = 'N';
  1398. xscl1 = work[*n + 1];
  1399. xscl2 = work[*n + 2];
  1400. if (xscl1 != xscl2) {
  1401. /* This is an exceptional situation. If the */
  1402. /* data matrices are not scaled and the */
  1403. /* largest singular value of X overflows. */
  1404. /* In that case SGEJSV can return the SVD */
  1405. /* in scaled form. The scaling factor can be used */
  1406. /* to rescale the data (X and Y). */
  1407. slascl_("G", &c__0, &c__0, &xscl1, &xscl2, m, n, &y[y_offset],
  1408. ldy, &info2);
  1409. }
  1410. /* END SELECT */
  1411. }
  1412. if (info1 > 0) {
  1413. /* The SVD selected subroutine did not converge. */
  1414. /* Return with an error code. */
  1415. *info = 2;
  1416. return 0;
  1417. }
  1418. if (work[1] == zero) {
  1419. /* The largest computed singular value of (scaled) */
  1420. /* X is zero. Return error code -8 */
  1421. /* (the 8th input variable had an illegal value). */
  1422. *k = 0;
  1423. *info = -8;
  1424. i__1 = -(*info);
  1425. xerbla_("SGEDMD", &i__1);
  1426. return 0;
  1427. }
  1428. /* <3> Determine the numerical rank of the data */
  1429. /* snapshots matrix X. This depends on the */
  1430. /* parameters NRNK and TOL. */
  1431. /* SELECT CASE ( NRNK ) */
  1432. if (*nrnk == -1) {
  1433. *k = 1;
  1434. i__1 = numrnk;
  1435. for (i__ = 2; i__ <= i__1; ++i__) {
  1436. if (work[i__] <= work[1] * *tol || work[i__] <= small) {
  1437. myexit_();
  1438. }
  1439. ++(*k);
  1440. }
  1441. } else if (*nrnk == -2) {
  1442. *k = 1;
  1443. i__1 = numrnk - 1;
  1444. for (i__ = 1; i__ <= i__1; ++i__) {
  1445. if (work[i__ + 1] <= work[i__] * *tol || work[i__] <= small) {
  1446. myexit_();
  1447. }
  1448. ++(*k);
  1449. }
  1450. } else {
  1451. *k = 1;
  1452. i__1 = *nrnk;
  1453. for (i__ = 2; i__ <= i__1; ++i__) {
  1454. if (work[i__] <= small) {
  1455. myexit_();
  1456. }
  1457. ++(*k);
  1458. }
  1459. /* END SELECT */
  1460. }
  1461. /* Now, U = X(1:M,1:K) is the SVD/POD basis for the */
  1462. /* snapshot data in the input matrix X. */
  1463. /* <4> Compute the Rayleigh quotient S = U^T * A * U. */
  1464. /* Depending on the requested outputs, the computation */
  1465. /* is organized to compute additional auxiliary */
  1466. /* matrices (for the residuals and refinements). */
  1467. /* In all formulas below, we need V_k*Sigma_k^(-1) */
  1468. /* where either V_k is in W(1:N,1:K), or V_k^T is in */
  1469. /* W(1:K,1:N). Here Sigma_k=diag(WORK(1:K)). */
  1470. if (lsame_(t_or_n__, "N")) {
  1471. i__1 = *k;
  1472. for (i__ = 1; i__ <= i__1; ++i__) {
  1473. r__1 = one / work[i__];
  1474. sscal_(n, &r__1, &w[i__ * w_dim1 + 1], &c__1);
  1475. /* W(1:N,i) = (ONE/WORK(i)) * W(1:N,i) ! INTRINSIC */
  1476. /* BLAS CALL */
  1477. }
  1478. } else {
  1479. /* This non-unit stride access is due to the fact */
  1480. /* that SGESVD, SGESVDQ and SGESDD return the */
  1481. /* transposed matrix of the right singular vectors. */
  1482. /* DO i = 1, K */
  1483. /* CALL SSCAL( N, ONE/WORK(i), W(i,1), LDW ) ! BLAS CALL */
  1484. /* ! W(i,1:N) = (ONE/WORK(i)) * W(i,1:N) ! INTRINSIC */
  1485. /* END DO */
  1486. i__1 = *k;
  1487. for (i__ = 1; i__ <= i__1; ++i__) {
  1488. work[*n + i__] = one / work[i__];
  1489. }
  1490. i__1 = *n;
  1491. for (j = 1; j <= i__1; ++j) {
  1492. i__2 = *k;
  1493. for (i__ = 1; i__ <= i__2; ++i__) {
  1494. w[i__ + j * w_dim1] = work[*n + i__] * w[i__ + j * w_dim1];
  1495. }
  1496. }
  1497. }
  1498. if (wntref) {
  1499. /* Need A*U(:,1:K)=Y*V_k*inv(diag(WORK(1:K))) */
  1500. /* for computing the refined Ritz vectors */
  1501. /* (optionally, outside SGEDMD). */
  1502. sgemm_("N", t_or_n__, m, k, n, &one, &y[y_offset], ldy, &w[w_offset],
  1503. ldw, &zero, &z__[z_offset], ldz);
  1504. /* Z(1:M,1:K)=MATMUL(Y(1:M,1:N),TRANSPOSE(W(1:K,1:N))) ! INTRI */
  1505. /* Z(1:M,1:K)=MATMUL(Y(1:M,1:N),W(1:N,1:K)) ! INTRI */
  1506. /* At this point Z contains */
  1507. /* A * U(:,1:K) = Y * V_k * Sigma_k^(-1), and */
  1508. /* this is needed for computing the residuals. */
  1509. /* This matrix is returned in the array B and */
  1510. /* it can be used to compute refined Ritz vectors. */
  1511. /* BLAS */
  1512. slacpy_("A", m, k, &z__[z_offset], ldz, &b[b_offset], ldb);
  1513. /* B(1:M,1:K) = Z(1:M,1:K) ! INTRINSIC */
  1514. /* BLAS CALL */
  1515. sgemm_("T", "N", k, k, m, &one, &x[x_offset], ldx, &z__[z_offset],
  1516. ldz, &zero, &s[s_offset], lds);
  1517. /* S(1:K,1:K) = MATMUL(TANSPOSE(X(1:M,1:K)),Z(1:M,1:K)) ! INTRI */
  1518. /* At this point S = U^T * A * U is the Rayleigh quotient. */
  1519. /* BLAS */
  1520. } else {
  1521. /* A * U(:,1:K) is not explicitly needed and the */
  1522. /* computation is organized differently. The Rayleigh */
  1523. /* quotient is computed more efficiently. */
  1524. sgemm_("T", "N", k, n, m, &one, &x[x_offset], ldx, &y[y_offset], ldy,
  1525. &zero, &z__[z_offset], ldz);
  1526. /* Z(1:K,1:N) = MATMUL( TRANSPOSE(X(1:M,1:K)), Y(1:M,1:N) ) ! IN */
  1527. /* In the two SGEMM calls here, can use K for LDZ */
  1528. /* B */
  1529. sgemm_("N", t_or_n__, k, k, n, &one, &z__[z_offset], ldz, &w[w_offset]
  1530. , ldw, &zero, &s[s_offset], lds);
  1531. /* S(1:K,1:K) = MATMUL(Z(1:K,1:N),TRANSPOSE(W(1:K,1:N))) ! INTRIN */
  1532. /* S(1:K,1:K) = MATMUL(Z(1:K,1:N),(W(1:N,1:K))) ! INTRIN */
  1533. /* At this point S = U^T * A * U is the Rayleigh quotient. */
  1534. /* If the residuals are requested, save scaled V_k into Z. */
  1535. /* Recall that V_k or V_k^T is stored in W. */
  1536. /* BLAS */
  1537. if (wntres || wntex) {
  1538. if (lsame_(t_or_n__, "N")) {
  1539. slacpy_("A", n, k, &w[w_offset], ldw, &z__[z_offset], ldz);
  1540. } else {
  1541. slacpy_("A", k, n, &w[w_offset], ldw, &z__[z_offset], ldz);
  1542. }
  1543. }
  1544. }
  1545. /* <5> Compute the Ritz values and (if requested) the */
  1546. /* right eigenvectors of the Rayleigh quotient. */
  1547. i__1 = *lwork - *n;
  1548. sgeev_("N", jobzl, k, &s[s_offset], lds, &reig[1], &imeig[1], &w[w_offset]
  1549. , ldw, &w[w_offset], ldw, &work[*n + 1], &i__1, &info1);
  1550. /* W(1:K,1:K) contains the eigenvectors of the Rayleigh */
  1551. /* quotient. Even in the case of complex spectrum, all */
  1552. /* computation is done in real arithmetic. REIG and */
  1553. /* IMEIG are the real and the imaginary parts of the */
  1554. /* eigenvalues, so that the spectrum is given as */
  1555. /* REIG(:) + sqrt(-1)*IMEIG(:). Complex conjugate pairs */
  1556. /* are listed at consecutive positions. For such a */
  1557. /* complex conjugate pair of the eigenvalues, the */
  1558. /* corresponding eigenvectors are also a complex */
  1559. /* conjugate pair with the real and imaginary parts */
  1560. /* stored column-wise in W at the corresponding */
  1561. /* consecutive column indices. See the description of Z. */
  1562. /* Also, see the description of SGEEV. */
  1563. /* LAPACK C */
  1564. if (info1 > 0) {
  1565. /* SGEEV failed to compute the eigenvalues and */
  1566. /* eigenvectors of the Rayleigh quotient. */
  1567. *info = 3;
  1568. return 0;
  1569. }
  1570. /* <6> Compute the eigenvectors (if requested) and, */
  1571. /* the residuals (if requested). */
  1572. if (wntvec || wntex) {
  1573. if (wntres) {
  1574. if (wntref) {
  1575. /* Here, if the refinement is requested, we have */
  1576. /* A*U(:,1:K) already computed and stored in Z. */
  1577. /* For the residuals, need Y = A * U(:,1;K) * W. */
  1578. sgemm_("N", "N", m, k, k, &one, &z__[z_offset], ldz, &w[
  1579. w_offset], ldw, &zero, &y[y_offset], ldy);
  1580. /* Y(1:M,1:K) = Z(1:M,1:K) * W(1:K,1:K) ! INTRINSIC */
  1581. /* This frees Z; Y contains A * U(:,1:K) * W. */
  1582. /* BLAS CALL */
  1583. } else {
  1584. /* Compute S = V_k * Sigma_k^(-1) * W, where */
  1585. /* V_k * Sigma_k^(-1) is stored in Z */
  1586. sgemm_(t_or_n__, "N", n, k, k, &one, &z__[z_offset], ldz, &w[
  1587. w_offset], ldw, &zero, &s[s_offset], lds);
  1588. /* Then, compute Z = Y * S = */
  1589. /* = Y * V_k * Sigma_k^(-1) * W(1:K,1:K) = */
  1590. /* = A * U(:,1:K) * W(1:K,1:K) */
  1591. sgemm_("N", "N", m, k, n, &one, &y[y_offset], ldy, &s[
  1592. s_offset], lds, &zero, &z__[z_offset], ldz);
  1593. /* Save a copy of Z into Y and free Z for holding */
  1594. /* the Ritz vectors. */
  1595. slacpy_("A", m, k, &z__[z_offset], ldz, &y[y_offset], ldy);
  1596. if (wntex) {
  1597. slacpy_("A", m, k, &z__[z_offset], ldz, &b[b_offset], ldb);
  1598. }
  1599. }
  1600. } else if (wntex) {
  1601. /* Compute S = V_k * Sigma_k^(-1) * W, where */
  1602. /* V_k * Sigma_k^(-1) is stored in Z */
  1603. sgemm_(t_or_n__, "N", n, k, k, &one, &z__[z_offset], ldz, &w[
  1604. w_offset], ldw, &zero, &s[s_offset], lds);
  1605. /* Then, compute Z = Y * S = */
  1606. /* = Y * V_k * Sigma_k^(-1) * W(1:K,1:K) = */
  1607. /* = A * U(:,1:K) * W(1:K,1:K) */
  1608. sgemm_("N", "N", m, k, n, &one, &y[y_offset], ldy, &s[s_offset],
  1609. lds, &zero, &b[b_offset], ldb);
  1610. /* The above call replaces the following two calls */
  1611. /* that were used in the developing-testing phase. */
  1612. /* CALL SGEMM( 'N', 'N', M, K, N, ONE, Y, LDY, S, & */
  1613. /* LDS, ZERO, Z, LDZ) */
  1614. /* Save a copy of Z into B and free Z for holding */
  1615. /* the Ritz vectors. */
  1616. /* CALL SLACPY( 'A', M, K, Z, LDZ, B, LDB ) */
  1617. }
  1618. /* Compute the real form of the Ritz vectors */
  1619. if (wntvec) {
  1620. sgemm_("N", "N", m, k, k, &one, &x[x_offset], ldx, &w[w_offset],
  1621. ldw, &zero, &z__[z_offset], ldz);
  1622. }
  1623. /* Z(1:M,1:K) = MATMUL(X(1:M,1:K), W(1:K,1:K)) ! INTRINSIC */
  1624. /* BLAS CALL */
  1625. if (wntres) {
  1626. i__ = 1;
  1627. while(i__ <= *k) {
  1628. if (imeig[i__] == zero) {
  1629. /* have a real eigenvalue with real eigenvector */
  1630. r__1 = -reig[i__];
  1631. saxpy_(m, &r__1, &z__[i__ * z_dim1 + 1], &c__1, &y[i__ *
  1632. y_dim1 + 1], &c__1);
  1633. /* Y(1:M,i) = Y(1:M,i) - REIG(i) * Z(1:M,i) ! */
  1634. res[i__] = snrm2_(m, &y[i__ * y_dim1 + 1], &c__1);
  1635. ++i__;
  1636. } else {
  1637. /* Have a complex conjugate pair */
  1638. /* REIG(i) +- sqrt(-1)*IMEIG(i). */
  1639. /* Since all computation is done in real */
  1640. /* arithmetic, the formula for the residual */
  1641. /* is recast for real representation of the */
  1642. /* complex conjugate eigenpair. See the */
  1643. /* description of RES. */
  1644. ab[0] = reig[i__];
  1645. ab[1] = -imeig[i__];
  1646. ab[2] = imeig[i__];
  1647. ab[3] = reig[i__];
  1648. r__1 = -one;
  1649. sgemm_("N", "N", m, &c__2, &c__2, &r__1, &z__[i__ *
  1650. z_dim1 + 1], ldz, ab, &c__2, &one, &y[i__ *
  1651. y_dim1 + 1], ldy);
  1652. /* Y(1:M,i:i+1) = Y(1:M,i:i+1) - Z(1:M,i:i+1) * AB ! INT */
  1653. /* BL */
  1654. res[i__] = slange_("F", m, &c__2, &y[i__ * y_dim1 + 1],
  1655. ldy, &work[*n + 1]);
  1656. /* LA */
  1657. res[i__ + 1] = res[i__];
  1658. i__ += 2;
  1659. }
  1660. }
  1661. }
  1662. }
  1663. if (*whtsvd == 4) {
  1664. work[*n + 1] = xscl1;
  1665. work[*n + 2] = xscl2;
  1666. }
  1667. /* Successful exit. */
  1668. if (! badxy) {
  1669. *info = 0;
  1670. } else {
  1671. /* A warning on possible data inconsistency. */
  1672. /* This should be a rare event. */
  1673. *info = 4;
  1674. }
  1675. /* ............................................................ */
  1676. return 0;
  1677. /* ...... */
  1678. } /* sgedmd_ */