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 int logical;
  52. typedef short int shortlogical;
  53. typedef char logical1;
  54. typedef char integer1;
  55. #define TRUE_ (1)
  56. #define FALSE_ (0)
  57. /* Extern is for use with -E */
  58. #ifndef Extern
  59. #define Extern extern
  60. #endif
  61. /* I/O stuff */
  62. typedef int flag;
  63. typedef int ftnlen;
  64. typedef int ftnint;
  65. /*external read, write*/
  66. typedef struct
  67. { flag cierr;
  68. ftnint ciunit;
  69. flag ciend;
  70. char *cifmt;
  71. ftnint cirec;
  72. } cilist;
  73. /*internal read, write*/
  74. typedef struct
  75. { flag icierr;
  76. char *iciunit;
  77. flag iciend;
  78. char *icifmt;
  79. ftnint icirlen;
  80. ftnint icirnum;
  81. } icilist;
  82. /*open*/
  83. typedef struct
  84. { flag oerr;
  85. ftnint ounit;
  86. char *ofnm;
  87. ftnlen ofnmlen;
  88. char *osta;
  89. char *oacc;
  90. char *ofm;
  91. ftnint orl;
  92. char *oblnk;
  93. } olist;
  94. /*close*/
  95. typedef struct
  96. { flag cerr;
  97. ftnint cunit;
  98. char *csta;
  99. } cllist;
  100. /*rewind, backspace, endfile*/
  101. typedef struct
  102. { flag aerr;
  103. ftnint aunit;
  104. } alist;
  105. /* inquire */
  106. typedef struct
  107. { flag inerr;
  108. ftnint inunit;
  109. char *infile;
  110. ftnlen infilen;
  111. ftnint *inex; /*parameters in standard's order*/
  112. ftnint *inopen;
  113. ftnint *innum;
  114. ftnint *innamed;
  115. char *inname;
  116. ftnlen innamlen;
  117. char *inacc;
  118. ftnlen inacclen;
  119. char *inseq;
  120. ftnlen inseqlen;
  121. char *indir;
  122. ftnlen indirlen;
  123. char *infmt;
  124. ftnlen infmtlen;
  125. char *inform;
  126. ftnint informlen;
  127. char *inunf;
  128. ftnlen inunflen;
  129. ftnint *inrecl;
  130. ftnint *innrec;
  131. char *inblank;
  132. ftnlen inblanklen;
  133. } inlist;
  134. #define VOID void
  135. union Multitype { /* for multiple entry points */
  136. integer1 g;
  137. shortint h;
  138. integer i;
  139. /* longint j; */
  140. real r;
  141. doublereal d;
  142. complex c;
  143. doublecomplex z;
  144. };
  145. typedef union Multitype Multitype;
  146. struct Vardesc { /* for Namelist */
  147. char *name;
  148. char *addr;
  149. ftnlen *dims;
  150. int type;
  151. };
  152. typedef struct Vardesc Vardesc;
  153. struct Namelist {
  154. char *name;
  155. Vardesc **vars;
  156. int nvars;
  157. };
  158. typedef struct Namelist Namelist;
  159. #define abs(x) ((x) >= 0 ? (x) : -(x))
  160. #define dabs(x) (fabs(x))
  161. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  162. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  163. #define dmin(a,b) (f2cmin(a,b))
  164. #define dmax(a,b) (f2cmax(a,b))
  165. #define bit_test(a,b) ((a) >> (b) & 1)
  166. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  167. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  168. #define abort_() { sig_die("Fortran abort routine called", 1); }
  169. #define c_abs(z) (cabsf(Cf(z)))
  170. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  171. #ifdef _MSC_VER
  172. #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
  173. #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/Cd(b)._Val[1]);}
  174. #else
  175. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  176. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  177. #endif
  178. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  179. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  180. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  181. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  182. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  183. #define d_abs(x) (fabs(*(x)))
  184. #define d_acos(x) (acos(*(x)))
  185. #define d_asin(x) (asin(*(x)))
  186. #define d_atan(x) (atan(*(x)))
  187. #define d_atn2(x, y) (atan2(*(x),*(y)))
  188. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  189. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  190. #define d_cos(x) (cos(*(x)))
  191. #define d_cosh(x) (cosh(*(x)))
  192. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  193. #define d_exp(x) (exp(*(x)))
  194. #define d_imag(z) (cimag(Cd(z)))
  195. #define r_imag(z) (cimagf(Cf(z)))
  196. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  198. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  200. #define d_log(x) (log(*(x)))
  201. #define d_mod(x, y) (fmod(*(x), *(y)))
  202. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  203. #define d_nint(x) u_nint(*(x))
  204. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  205. #define d_sign(a,b) u_sign(*(a),*(b))
  206. #define r_sign(a,b) u_sign(*(a),*(b))
  207. #define d_sin(x) (sin(*(x)))
  208. #define d_sinh(x) (sinh(*(x)))
  209. #define d_sqrt(x) (sqrt(*(x)))
  210. #define d_tan(x) (tan(*(x)))
  211. #define d_tanh(x) (tanh(*(x)))
  212. #define i_abs(x) abs(*(x))
  213. #define i_dnnt(x) ((integer)u_nint(*(x)))
  214. #define i_len(s, n) (n)
  215. #define i_nint(x) ((integer)u_nint(*(x)))
  216. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  217. #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
  218. #define pow_si(B,E) spow_ui(*(B),*(E))
  219. #define pow_ri(B,E) spow_ui(*(B),*(E))
  220. #define pow_di(B,E) dpow_ui(*(B),*(E))
  221. #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
  222. #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
  223. #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
  224. #define s_cat(lpp, rpp, rnp, np, llp) { ftnlen i, nc, ll; char *f__rp, *lp; ll = (llp); lp = (lpp); for(i=0; i < (int)*(np); ++i) { nc = ll; if((rnp)[i] < nc) nc = (rnp)[i]; ll -= nc; f__rp = (rpp)[i]; while(--nc >= 0) *lp++ = *(f__rp)++; } while(--ll >= 0) *lp++ = ' '; }
  225. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  226. #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
  227. #define sig_die(s, kill) { exit(1); }
  228. #define s_stop(s, n) {exit(0);}
  229. static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
  230. #define z_abs(z) (cabs(Cd(z)))
  231. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  232. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  233. #define myexit_() break;
  234. #define mycycle_() continue;
  235. #define myceiling_(w) {ceil(w)}
  236. #define myhuge_(w) {HUGE_VAL}
  237. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  238. #define mymaxloc_(w,s,e,n) dmaxloc_(w,*(s),*(e),n)
  239. /* procedure parameter types for -A and -C++ */
  240. #define F2C_proc_par_types 1
  241. #ifdef __cplusplus
  242. typedef logical (*L_fp)(...);
  243. #else
  244. typedef logical (*L_fp)();
  245. #endif
  246. static float spow_ui(float x, integer n) {
  247. float pow=1.0; unsigned long int u;
  248. if(n != 0) {
  249. if(n < 0) n = -n, x = 1/x;
  250. for(u = n; ; ) {
  251. if(u & 01) pow *= x;
  252. if(u >>= 1) x *= x;
  253. else break;
  254. }
  255. }
  256. return pow;
  257. }
  258. static double dpow_ui(double x, integer n) {
  259. double pow=1.0; unsigned long int u;
  260. if(n != 0) {
  261. if(n < 0) n = -n, x = 1/x;
  262. for(u = n; ; ) {
  263. if(u & 01) pow *= x;
  264. if(u >>= 1) x *= x;
  265. else break;
  266. }
  267. }
  268. return pow;
  269. }
  270. #ifdef _MSC_VER
  271. static _Fcomplex cpow_ui(complex x, integer n) {
  272. complex pow={1.0,0.0}; unsigned long int u;
  273. if(n != 0) {
  274. if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
  275. for(u = n; ; ) {
  276. if(u & 01) pow.r *= x.r, pow.i *= x.i;
  277. if(u >>= 1) x.r *= x.r, x.i *= x.i;
  278. else break;
  279. }
  280. }
  281. _Fcomplex p={pow.r, pow.i};
  282. return p;
  283. }
  284. #else
  285. static _Complex float cpow_ui(_Complex float x, integer n) {
  286. _Complex float pow=1.0; unsigned long int u;
  287. if(n != 0) {
  288. if(n < 0) n = -n, x = 1/x;
  289. for(u = n; ; ) {
  290. if(u & 01) pow *= x;
  291. if(u >>= 1) x *= x;
  292. else break;
  293. }
  294. }
  295. return pow;
  296. }
  297. #endif
  298. #ifdef _MSC_VER
  299. static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
  300. _Dcomplex pow={1.0,0.0}; unsigned long int u;
  301. if(n != 0) {
  302. if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
  303. for(u = n; ; ) {
  304. if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
  305. if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
  306. else break;
  307. }
  308. }
  309. _Dcomplex p = {pow._Val[0], pow._Val[1]};
  310. return p;
  311. }
  312. #else
  313. static _Complex double zpow_ui(_Complex double x, integer n) {
  314. _Complex double pow=1.0; unsigned long int u;
  315. if(n != 0) {
  316. if(n < 0) n = -n, x = 1/x;
  317. for(u = n; ; ) {
  318. if(u & 01) pow *= x;
  319. if(u >>= 1) x *= x;
  320. else break;
  321. }
  322. }
  323. return pow;
  324. }
  325. #endif
  326. static integer pow_ii(integer x, integer n) {
  327. integer pow; unsigned long int u;
  328. if (n <= 0) {
  329. if (n == 0 || x == 1) pow = 1;
  330. else if (x != -1) pow = x == 0 ? 1/x : 0;
  331. else n = -n;
  332. }
  333. if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
  334. u = n;
  335. for(pow = 1; ; ) {
  336. if(u & 01) pow *= x;
  337. if(u >>= 1) x *= x;
  338. else break;
  339. }
  340. }
  341. return pow;
  342. }
  343. static integer dmaxloc_(double *w, integer s, integer e, integer *n)
  344. {
  345. double m; integer i, mi;
  346. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  347. if (w[i-1]>m) mi=i ,m=w[i-1];
  348. return mi-s+1;
  349. }
  350. static integer smaxloc_(float *w, integer s, integer e, integer *n)
  351. {
  352. float m; integer i, mi;
  353. for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
  354. if (w[i-1]>m) mi=i ,m=w[i-1];
  355. return mi-s+1;
  356. }
  357. static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  358. integer n = *n_, incx = *incx_, incy = *incy_, i;
  359. #ifdef _MSC_VER
  360. _Fcomplex zdotc = {0.0, 0.0};
  361. if (incx == 1 && incy == 1) {
  362. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  363. zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
  364. zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
  365. }
  366. } else {
  367. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  368. zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
  369. zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
  370. }
  371. }
  372. pCf(z) = zdotc;
  373. }
  374. #else
  375. _Complex float zdotc = 0.0;
  376. if (incx == 1 && incy == 1) {
  377. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  378. zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
  379. }
  380. } else {
  381. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  382. zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
  383. }
  384. }
  385. pCf(z) = zdotc;
  386. }
  387. #endif
  388. static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  389. integer n = *n_, incx = *incx_, incy = *incy_, i;
  390. #ifdef _MSC_VER
  391. _Dcomplex zdotc = {0.0, 0.0};
  392. if (incx == 1 && incy == 1) {
  393. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  394. zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
  395. zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
  396. }
  397. } else {
  398. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  399. zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
  400. zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
  401. }
  402. }
  403. pCd(z) = zdotc;
  404. }
  405. #else
  406. _Complex double zdotc = 0.0;
  407. if (incx == 1 && incy == 1) {
  408. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  409. zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
  410. }
  411. } else {
  412. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  413. zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
  414. }
  415. }
  416. pCd(z) = zdotc;
  417. }
  418. #endif
  419. static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
  420. integer n = *n_, incx = *incx_, incy = *incy_, i;
  421. #ifdef _MSC_VER
  422. _Fcomplex zdotc = {0.0, 0.0};
  423. if (incx == 1 && incy == 1) {
  424. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  425. zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
  426. zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
  427. }
  428. } else {
  429. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  430. zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
  431. zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
  432. }
  433. }
  434. pCf(z) = zdotc;
  435. }
  436. #else
  437. _Complex float zdotc = 0.0;
  438. if (incx == 1 && incy == 1) {
  439. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  440. zdotc += Cf(&x[i]) * Cf(&y[i]);
  441. }
  442. } else {
  443. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  444. zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
  445. }
  446. }
  447. pCf(z) = zdotc;
  448. }
  449. #endif
  450. static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
  451. integer n = *n_, incx = *incx_, incy = *incy_, i;
  452. #ifdef _MSC_VER
  453. _Dcomplex zdotc = {0.0, 0.0};
  454. if (incx == 1 && incy == 1) {
  455. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  456. zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
  457. zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
  458. }
  459. } else {
  460. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  461. zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
  462. zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
  463. }
  464. }
  465. pCd(z) = zdotc;
  466. }
  467. #else
  468. _Complex double zdotc = 0.0;
  469. if (incx == 1 && incy == 1) {
  470. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  471. zdotc += Cd(&x[i]) * Cd(&y[i]);
  472. }
  473. } else {
  474. for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
  475. zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
  476. }
  477. }
  478. pCd(z) = zdotc;
  479. }
  480. #endif
  481. /* -- translated by f2c (version 20000121).
  482. You must link the resulting object file with the libraries:
  483. -lf2c -lm (in that order)
  484. */
  485. /* -- translated by f2c (version 20000121).
  486. You must link the resulting object file with the libraries:
  487. -lf2c -lm (in that order)
  488. */
  489. /* Table of constant values */
  490. static integer c_n1 = -1;
  491. static integer c__1 = 1;
  492. static integer c__0 = 0;
  493. static integer c__2 = 2;
  494. /* Subroutine */ int sgedmd_(char *jobs, char *jobz, char *jobr, char *jobf,
  495. integer *whtsvd, integer *m, integer *n, real *x, integer *ldx, real *
  496. y, integer *ldy, integer *nrnk, real *tol, integer *k, real *reig,
  497. real *imeig, real *z__, integer *ldz, real *res, real *b, integer *
  498. ldb, real *w, integer *ldw, real *s, integer *lds, real *work,
  499. integer *lwork, integer *iwork, integer *liwork, integer *info)
  500. {
  501. /* System generated locals */
  502. integer x_dim1, x_offset, y_dim1, y_offset, z_dim1, z_offset, b_dim1,
  503. b_offset, w_dim1, w_offset, s_dim1, s_offset, i__1, i__2;
  504. real r__1, r__2;
  505. /* Local variables */
  506. real zero, ssum;
  507. integer info1, info2;
  508. real xscl1, xscl2;
  509. extern real snrm2_(integer *, real *, integer *);
  510. integer i__, j;
  511. real scale;
  512. extern logical lsame_(char *, char *);
  513. extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
  514. logical badxy;
  515. real small;
  516. extern /* Subroutine */ int sgemm_(char *, char *, integer *, integer *,
  517. integer *, real *, real *, integer *, real *, integer *, real *,
  518. real *, integer *), sgeev_(char *, char *,
  519. integer *, real *, integer *, real *, real *, real *, integer *,
  520. real *, integer *, real *, integer *, integer *);
  521. char jobzl[1];
  522. extern /* Subroutine */ int saxpy_(integer *, real *, real *, integer *,
  523. real *, integer *);
  524. logical wntex;
  525. real ab[4] /* was [2][2] */;
  526. extern real slamch_(char *), slange_(char *, integer *, integer *,
  527. real *, integer *, real *);
  528. extern /* Subroutine */ int sgesdd_(char *, integer *, integer *, real *,
  529. integer *, real *, real *, integer *, real *, integer *, real *,
  530. integer *, integer *, integer *), xerbla_(char *, integer
  531. *);
  532. char t_or_n__[1];
  533. extern /* Subroutine */ int slascl_(char *, integer *, integer *, real *,
  534. real *, integer *, integer *, real *, integer *, integer *);
  535. extern integer isamax_(integer *, real *, integer *);
  536. logical sccolx, sccoly;
  537. extern logical sisnan_(real *);
  538. extern /* Subroutine */ int sgesvd_(char *, char *, integer *, integer *,
  539. real *, integer *, real *, real *, integer *, real *, integer *,
  540. real *, integer *, integer *);
  541. integer lwrsdd, mwrsdd;
  542. extern /* Subroutine */ int sgejsv_(char *, char *, char *, char *, char *
  543. , char *, integer *, integer *, real *, integer *, real *, real *,
  544. integer *, real *, integer *, real *, integer *, integer *,
  545. integer *),
  546. slacpy_(char *, integer *, integer *, real *, integer *, real *,
  547. integer *);
  548. integer iminwr;
  549. logical wntref, wntvec;
  550. real rootsc;
  551. integer lwrkev, mlwork, mwrkev, numrnk, olwork;
  552. real rdummy[2];
  553. integer lwrsvd, mwrsvd;
  554. logical lquery, wntres;
  555. char jsvopt[1];
  556. extern /* Subroutine */ int slassq_(integer *, real *, integer *, real *,
  557. real *), mecago_();
  558. integer mwrsvj, lwrsvq, mwrsvq;
  559. real rdummy2[2], ofl, one;
  560. extern /* Subroutine */ int sgesvdq_(char *, char *, char *, char *, char
  561. *, integer *, integer *, real *, integer *, real *, real *,
  562. integer *, real *, integer *, integer *, integer *, integer *,
  563. real *, integer *, real *, integer *, integer *);
  564. /* March 2023 */
  565. /* ..... */
  566. /* USE iso_fortran_env */
  567. /* INTEGER, PARAMETER :: WP = real32 */
  568. /* ..... */
  569. /* Scalar arguments */
  570. /* Array arguments */
  571. /* ............................................................ */
  572. /* Purpose */
  573. /* ======= */
  574. /* SGEDMD computes the Dynamic Mode Decomposition (DMD) for */
  575. /* a pair of data snapshot matrices. For the input matrices */
  576. /* X and Y such that Y = A*X with an unaccessible matrix */
  577. /* A, SGEDMD computes a certain number of Ritz pairs of A using */
  578. /* the standard Rayleigh-Ritz extraction from a subspace of */
  579. /* range(X) that is determined using the leading left singular */
  580. /* vectors of X. Optionally, SGEDMD returns the residuals */
  581. /* of the computed Ritz pairs, the information needed for */
  582. /* a refinement of the Ritz vectors, or the eigenvectors of */
  583. /* the Exact DMD. */
  584. /* For further details see the references listed */
  585. /* below. For more details of the implementation see [3]. */
  586. /* References */
  587. /* ========== */
  588. /* [1] P. Schmid: Dynamic mode decomposition of numerical */
  589. /* and experimental data, */
  590. /* Journal of Fluid Mechanics 656, 5-28, 2010. */
  591. /* [2] Z. Drmac, I. Mezic, R. Mohr: Data driven modal */
  592. /* decompositions: analysis and enhancements, */
  593. /* SIAM J. on Sci. Comp. 40 (4), A2253-A2285, 2018. */
  594. /* [3] Z. Drmac: A LAPACK implementation of the Dynamic */
  595. /* Mode Decomposition I. Technical report. AIMDyn Inc. */
  596. /* and LAPACK Working Note 298. */
  597. /* [4] J. Tu, C. W. Rowley, D. M. Luchtenburg, S. L. */
  598. /* Brunton, N. Kutz: On Dynamic Mode Decomposition: */
  599. /* Theory and Applications, Journal of Computational */
  600. /* Dynamics 1(2), 391 -421, 2014. */
  601. /* ...................................................................... */
  602. /* Developed and supported by: */
  603. /* =========================== */
  604. /* Developed and coded by Zlatko Drmac, Faculty of Science, */
  605. /* University of Zagreb; drmac@math.hr */
  606. /* In cooperation with */
  607. /* AIMdyn Inc., Santa Barbara, CA. */
  608. /* and supported by */
  609. /* - DARPA SBIR project "Koopman Operator-Based Forecasting */
  610. /* for Nonstationary Processes from Near-Term, Limited */
  611. /* Observational Data" Contract No: W31P4Q-21-C-0007 */
  612. /* - DARPA PAI project "Physics-Informed Machine Learning */
  613. /* Methodologies" Contract No: HR0011-18-9-0033 */
  614. /* - DARPA MoDyL project "A Data-Driven, Operator-Theoretic */
  615. /* Framework for Space-Time Analysis of Process Dynamics" */
  616. /* Contract No: HR0011-16-C-0116 */
  617. /* Any opinions, findings and conclusions or recommendations */
  618. /* expressed in this material are those of the author and */
  619. /* do not necessarily reflect the views of the DARPA SBIR */
  620. /* Program Office */
  621. /* ============================================================ */
  622. /* Distribution Statement A: */
  623. /* Approved for Public Release, Distribution Unlimited. */
  624. /* Cleared by DARPA on September 29, 2022 */
  625. /* ============================================================ */
  626. /* ...................................................................... */
  627. /* Arguments */
  628. /* ========= */
  629. /* JOBS (input) CHARACTER*1 */
  630. /* Determines whether the initial data snapshots are scaled */
  631. /* by a diagonal matrix. */
  632. /* 'S' :: The data snapshots matrices X and Y are multiplied */
  633. /* with a diagonal matrix D so that X*D has unit */
  634. /* nonzero columns (in the Euclidean 2-norm) */
  635. /* 'C' :: The snapshots are scaled as with the 'S' option. */
  636. /* If it is found that an i-th column of X is zero */
  637. /* vector and the corresponding i-th column of Y is */
  638. /* non-zero, then the i-th column of Y is set to */
  639. /* zero and a warning flag is raised. */
  640. /* 'Y' :: The data snapshots matrices X and Y are multiplied */
  641. /* by a diagonal matrix D so that Y*D has unit */
  642. /* nonzero columns (in the Euclidean 2-norm) */
  643. /* 'N' :: No data scaling. */
  644. /* ..... */
  645. /* JOBZ (input) CHARACTER*1 */
  646. /* Determines whether the eigenvectors (Koopman modes) will */
  647. /* be computed. */
  648. /* 'V' :: The eigenvectors (Koopman modes) will be computed */
  649. /* and returned in the matrix Z. */
  650. /* See the description of Z. */
  651. /* 'F' :: The eigenvectors (Koopman modes) will be returned */
  652. /* in factored form as the product X(:,1:K)*W, where X */
  653. /* contains a POD basis (leading left singular vectors */
  654. /* of the data matrix X) and W contains the eigenvectors */
  655. /* of the corresponding Rayleigh quotient. */
  656. /* See the descriptions of K, X, W, Z. */
  657. /* 'N' :: The eigenvectors are not computed. */
  658. /* ..... */
  659. /* JOBR (input) CHARACTER*1 */
  660. /* Determines whether to compute the residuals. */
  661. /* 'R' :: The residuals for the computed eigenpairs will be */
  662. /* computed and stored in the array RES. */
  663. /* See the description of RES. */
  664. /* For this option to be legal, JOBZ must be 'V'. */
  665. /* 'N' :: The residuals are not computed. */
  666. /* ..... */
  667. /* JOBF (input) CHARACTER*1 */
  668. /* Specifies whether to store information needed for post- */
  669. /* processing (e.g. computing refined Ritz vectors) */
  670. /* 'R' :: The matrix needed for the refinement of the Ritz */
  671. /* vectors is computed and stored in the array B. */
  672. /* See the description of B. */
  673. /* 'E' :: The unscaled eigenvectors of the Exact DMD are */
  674. /* computed and returned in the array B. See the */
  675. /* description of B. */
  676. /* 'N' :: No eigenvector refinement data is computed. */
  677. /* ..... */
  678. /* WHTSVD (input) INTEGER, WHSTVD in { 1, 2, 3, 4 } */
  679. /* Allows for a selection of the SVD algorithm from the */
  680. /* LAPACK library. */
  681. /* 1 :: SGESVD (the QR SVD algorithm) */
  682. /* 2 :: SGESDD (the Divide and Conquer algorithm; if enough */
  683. /* workspace available, this is the fastest option) */
  684. /* 3 :: SGESVDQ (the preconditioned QR SVD ; this and 4 */
  685. /* are the most accurate options) */
  686. /* 4 :: SGEJSV (the preconditioned Jacobi SVD; this and 3 */
  687. /* are the most accurate options) */
  688. /* For the four methods above, a significant difference in */
  689. /* the accuracy of small singular values is possible if */
  690. /* the snapshots vary in norm so that X is severely */
  691. /* ill-conditioned. If small (smaller than EPS*||X||) */
  692. /* singular values are of interest and JOBS=='N', then */
  693. /* the options (3, 4) give the most accurate results, where */
  694. /* the option 4 is slightly better and with stronger */
  695. /* theoretical background. */
  696. /* If JOBS=='S', i.e. the columns of X will be normalized, */
  697. /* then all methods give nearly equally accurate results. */
  698. /* ..... */
  699. /* M (input) INTEGER, M>= 0 */
  700. /* The state space dimension (the row dimension of X, Y). */
  701. /* ..... */
  702. /* N (input) INTEGER, 0 <= N <= M */
  703. /* The number of data snapshot pairs */
  704. /* (the number of columns of X and Y). */
  705. /* ..... */
  706. /* X (input/output) REAL(KIND=WP) M-by-N array */
  707. /* > On entry, X contains the data snapshot matrix X. It is */
  708. /* assumed that the column norms of X are in the range of */
  709. /* the normalized floating point numbers. */
  710. /* < On exit, the leading K columns of X contain a POD basis, */
  711. /* i.e. the leading K left singular vectors of the input */
  712. /* data matrix X, U(:,1:K). All N columns of X contain all */
  713. /* left singular vectors of the input matrix X. */
  714. /* See the descriptions of K, Z and W. */
  715. /* ..... */
  716. /* LDX (input) INTEGER, LDX >= M */
  717. /* The leading dimension of the array X. */
  718. /* ..... */
  719. /* Y (input/workspace/output) REAL(KIND=WP) M-by-N array */
  720. /* > On entry, Y contains the data snapshot matrix Y */
  721. /* < On exit, */
  722. /* If JOBR == 'R', the leading K columns of Y contain */
  723. /* the residual vectors for the computed Ritz pairs. */
  724. /* See the description of RES. */
  725. /* If JOBR == 'N', Y contains the original input data, */
  726. /* scaled according to the value of JOBS. */
  727. /* ..... */
  728. /* LDY (input) INTEGER , LDY >= M */
  729. /* The leading dimension of the array Y. */
  730. /* ..... */
  731. /* NRNK (input) INTEGER */
  732. /* Determines the mode how to compute the numerical rank, */
  733. /* i.e. how to truncate small singular values of the input */
  734. /* matrix X. On input, if */
  735. /* NRNK = -1 :: i-th singular value sigma(i) is truncated */
  736. /* if sigma(i) <= TOL*sigma(1) */
  737. /* This option is recommended. */
  738. /* NRNK = -2 :: i-th singular value sigma(i) is truncated */
  739. /* if sigma(i) <= TOL*sigma(i-1) */
  740. /* This option is included for R&D purposes. */
  741. /* It requires highly accurate SVD, which */
  742. /* may not be feasible. */
  743. /* The numerical rank can be enforced by using positive */
  744. /* value of NRNK as follows: */
  745. /* 0 < NRNK <= N :: at most NRNK largest singular values */
  746. /* will be used. If the number of the computed nonzero */
  747. /* singular values is less than NRNK, then only those */
  748. /* nonzero values will be used and the actually used */
  749. /* dimension is less than NRNK. The actual number of */
  750. /* the nonzero singular values is returned in the variable */
  751. /* K. See the descriptions of TOL and K. */
  752. /* ..... */
  753. /* TOL (input) REAL(KIND=WP), 0 <= TOL < 1 */
  754. /* The tolerance for truncating small singular values. */
  755. /* See the description of NRNK. */
  756. /* ..... */
  757. /* K (output) INTEGER, 0 <= K <= N */
  758. /* The dimension of the POD basis for the data snapshot */
  759. /* matrix X and the number of the computed Ritz pairs. */
  760. /* The value of K is determined according to the rule set */
  761. /* by the parameters NRNK and TOL. */
  762. /* See the descriptions of NRNK and TOL. */
  763. /* ..... */
  764. /* REIG (output) REAL(KIND=WP) N-by-1 array */
  765. /* The leading K (K<=N) entries of REIG contain */
  766. /* the real parts of the computed eigenvalues */
  767. /* REIG(1:K) + sqrt(-1)*IMEIG(1:K). */
  768. /* See the descriptions of K, IMEIG, and Z. */
  769. /* ..... */
  770. /* IMEIG (output) REAL(KIND=WP) N-by-1 array */
  771. /* The leading K (K<=N) entries of IMEIG contain */
  772. /* the imaginary parts of the computed eigenvalues */
  773. /* REIG(1:K) + sqrt(-1)*IMEIG(1:K). */
  774. /* The eigenvalues are determined as follows: */
  775. /* If IMEIG(i) == 0, then the corresponding eigenvalue is */
  776. /* real, LAMBDA(i) = REIG(i). */
  777. /* If IMEIG(i)>0, then the corresponding complex */
  778. /* conjugate pair of eigenvalues reads */
  779. /* LAMBDA(i) = REIG(i) + sqrt(-1)*IMAG(i) */
  780. /* LAMBDA(i+1) = REIG(i) - sqrt(-1)*IMAG(i) */
  781. /* That is, complex conjugate pairs have consecutive */
  782. /* indices (i,i+1), with the positive imaginary part */
  783. /* listed first. */
  784. /* See the descriptions of K, REIG, and Z. */
  785. /* ..... */
  786. /* Z (workspace/output) REAL(KIND=WP) M-by-N array */
  787. /* If JOBZ =='V' then */
  788. /* Z contains real Ritz vectors as follows: */
  789. /* If IMEIG(i)=0, then Z(:,i) is an eigenvector of */
  790. /* the i-th Ritz value; ||Z(:,i)||_2=1. */
  791. /* If IMEIG(i) > 0 (and IMEIG(i+1) < 0) then */
  792. /* [Z(:,i) Z(:,i+1)] span an invariant subspace and */
  793. /* the Ritz values extracted from this subspace are */
  794. /* REIG(i) + sqrt(-1)*IMEIG(i) and */
  795. /* REIG(i) - sqrt(-1)*IMEIG(i). */
  796. /* The corresponding eigenvectors are */
  797. /* Z(:,i) + sqrt(-1)*Z(:,i+1) and */
  798. /* Z(:,i) - sqrt(-1)*Z(:,i+1), respectively. */
  799. /* || Z(:,i:i+1)||_F = 1. */
  800. /* If JOBZ == 'F', then the above descriptions hold for */
  801. /* the columns of X(:,1:K)*W(1:K,1:K), where the columns */
  802. /* of W(1:k,1:K) are the computed eigenvectors of the */
  803. /* K-by-K Rayleigh quotient. The columns of W(1:K,1:K) */
  804. /* are similarly structured: If IMEIG(i) == 0 then */
  805. /* X(:,1:K)*W(:,i) is an eigenvector, and if IMEIG(i)>0 */
  806. /* then X(:,1:K)*W(:,i)+sqrt(-1)*X(:,1:K)*W(:,i+1) and */
  807. /* X(:,1:K)*W(:,i)-sqrt(-1)*X(:,1:K)*W(:,i+1) */
  808. /* are the eigenvectors of LAMBDA(i), LAMBDA(i+1). */
  809. /* See the descriptions of REIG, IMEIG, X and W. */
  810. /* ..... */
  811. /* LDZ (input) INTEGER , LDZ >= M */
  812. /* The leading dimension of the array Z. */
  813. /* ..... */
  814. /* RES (output) REAL(KIND=WP) N-by-1 array */
  815. /* RES(1:K) contains the residuals for the K computed */
  816. /* Ritz pairs. */
  817. /* If LAMBDA(i) is real, then */
  818. /* RES(i) = || A * Z(:,i) - LAMBDA(i)*Z(:,i))||_2. */
  819. /* If [LAMBDA(i), LAMBDA(i+1)] is a complex conjugate pair */
  820. /* then */
  821. /* RES(i)=RES(i+1) = || A * Z(:,i:i+1) - Z(:,i:i+1) *B||_F */
  822. /* where B = [ real(LAMBDA(i)) imag(LAMBDA(i)) ] */
  823. /* [-imag(LAMBDA(i)) real(LAMBDA(i)) ]. */
  824. /* It holds that */
  825. /* RES(i) = || A*ZC(:,i) - LAMBDA(i) *ZC(:,i) ||_2 */
  826. /* RES(i+1) = || A*ZC(:,i+1) - LAMBDA(i+1)*ZC(:,i+1) ||_2 */
  827. /* where ZC(:,i) = Z(:,i) + sqrt(-1)*Z(:,i+1) */
  828. /* ZC(:,i+1) = Z(:,i) - sqrt(-1)*Z(:,i+1) */
  829. /* See the description of REIG, IMEIG and Z. */
  830. /* ..... */
  831. /* B (output) REAL(KIND=WP) M-by-N array. */
  832. /* IF JOBF =='R', B(1:M,1:K) contains A*U(:,1:K), and can */
  833. /* be used for computing the refined vectors; see further */
  834. /* details in the provided references. */
  835. /* If JOBF == 'E', B(1:M,1;K) contains */
  836. /* A*U(:,1:K)*W(1:K,1:K), which are the vectors from the */
  837. /* Exact DMD, up to scaling by the inverse eigenvalues. */
  838. /* If JOBF =='N', then B is not referenced. */
  839. /* See the descriptions of X, W, K. */
  840. /* ..... */
  841. /* LDB (input) INTEGER, LDB >= M */
  842. /* The leading dimension of the array B. */
  843. /* ..... */
  844. /* W (workspace/output) REAL(KIND=WP) N-by-N array */
  845. /* On exit, W(1:K,1:K) contains the K computed */
  846. /* eigenvectors of the matrix Rayleigh quotient (real and */
  847. /* imaginary parts for each complex conjugate pair of the */
  848. /* eigenvalues). The Ritz vectors (returned in Z) are the */
  849. /* product of X (containing a POD basis for the input */
  850. /* matrix X) and W. See the descriptions of K, S, X and Z. */
  851. /* W is also used as a workspace to temporarily store the */
  852. /* left singular vectors of X. */
  853. /* ..... */
  854. /* LDW (input) INTEGER, LDW >= N */
  855. /* The leading dimension of the array W. */
  856. /* ..... */
  857. /* S (workspace/output) REAL(KIND=WP) N-by-N array */
  858. /* The array S(1:K,1:K) is used for the matrix Rayleigh */
  859. /* quotient. This content is overwritten during */
  860. /* the eigenvalue decomposition by SGEEV. */
  861. /* See the description of K. */
  862. /* ..... */
  863. /* LDS (input) INTEGER, LDS >= N */
  864. /* The leading dimension of the array S. */
  865. /* ..... */
  866. /* WORK (workspace/output) REAL(KIND=WP) LWORK-by-1 array */
  867. /* On exit, WORK(1:N) contains the singular values of */
  868. /* X (for JOBS=='N') or column scaled X (JOBS=='S', 'C'). */
  869. /* If WHTSVD==4, then WORK(N+1) and WORK(N+2) contain */
  870. /* scaling factor WORK(N+2)/WORK(N+1) used to scale X */
  871. /* and Y to avoid overflow in the SVD of X. */
  872. /* This may be of interest if the scaling option is off */
  873. /* and as many as possible smallest eigenvalues are */
  874. /* desired to the highest feasible accuracy. */
  875. /* If the call to SGEDMD is only workspace query, then */
  876. /* WORK(1) contains the minimal workspace length and */
  877. /* WORK(2) is the optimal workspace length. Hence, the */
  878. /* length of work is at least 2. */
  879. /* See the description of LWORK. */
  880. /* ..... */
  881. /* LWORK (input) INTEGER */
  882. /* The minimal length of the workspace vector WORK. */
  883. /* LWORK is calculated as follows: */
  884. /* If WHTSVD == 1 :: */
  885. /* If JOBZ == 'V', then */
  886. /* LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,4*N)). */
  887. /* If JOBZ == 'N' then */
  888. /* LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,3*N)). */
  889. /* Here LWORK_SVD = MAX(1,3*N+M,5*N) is the minimal */
  890. /* workspace length of SGESVD. */
  891. /* If WHTSVD == 2 :: */
  892. /* If JOBZ == 'V', then */
  893. /* LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,4*N)) */
  894. /* If JOBZ == 'N', then */
  895. /* LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,3*N)) */
  896. /* Here LWORK_SVD = MAX(M, 5*N*N+4*N)+3*N*N is the */
  897. /* minimal workspace length of SGESDD. */
  898. /* If WHTSVD == 3 :: */
  899. /* If JOBZ == 'V', then */
  900. /* LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,4*N)) */
  901. /* If JOBZ == 'N', then */
  902. /* LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,3*N)) */
  903. /* Here LWORK_SVD = N+M+MAX(3*N+1, */
  904. /* MAX(1,3*N+M,5*N),MAX(1,N)) */
  905. /* is the minimal workspace length of SGESVDQ. */
  906. /* If WHTSVD == 4 :: */
  907. /* If JOBZ == 'V', then */
  908. /* LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,4*N)) */
  909. /* If JOBZ == 'N', then */
  910. /* LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,3*N)) */
  911. /* Here LWORK_SVD = MAX(7,2*M+N,6*N+2*N*N) is the */
  912. /* minimal workspace length of SGEJSV. */
  913. /* The above expressions are not simplified in order to */
  914. /* make the usage of WORK more transparent, and for */
  915. /* easier checking. In any case, LWORK >= 2. */
  916. /* If on entry LWORK = -1, then a workspace query is */
  917. /* assumed and the procedure only computes the minimal */
  918. /* and the optimal workspace lengths for both WORK and */
  919. /* IWORK. See the descriptions of WORK and IWORK. */
  920. /* ..... */
  921. /* IWORK (workspace/output) INTEGER LIWORK-by-1 array */
  922. /* Workspace that is required only if WHTSVD equals */
  923. /* 2 , 3 or 4. (See the description of WHTSVD). */
  924. /* If on entry LWORK =-1 or LIWORK=-1, then the */
  925. /* minimal length of IWORK is computed and returned in */
  926. /* IWORK(1). See the description of LIWORK. */
  927. /* ..... */
  928. /* LIWORK (input) INTEGER */
  929. /* The minimal length of the workspace vector IWORK. */
  930. /* If WHTSVD == 1, then only IWORK(1) is used; LIWORK >=1 */
  931. /* If WHTSVD == 2, then LIWORK >= MAX(1,8*MIN(M,N)) */
  932. /* If WHTSVD == 3, then LIWORK >= MAX(1,M+N-1) */
  933. /* If WHTSVD == 4, then LIWORK >= MAX(3,M+3*N) */
  934. /* If on entry LIWORK = -1, then a workspace query is */
  935. /* assumed and the procedure only computes the minimal */
  936. /* and the optimal workspace lengths for both WORK and */
  937. /* IWORK. See the descriptions of WORK and IWORK. */
  938. /* ..... */
  939. /* INFO (output) INTEGER */
  940. /* -i < 0 :: On entry, the i-th argument had an */
  941. /* illegal value */
  942. /* = 0 :: Successful return. */
  943. /* = 1 :: Void input. Quick exit (M=0 or N=0). */
  944. /* = 2 :: The SVD computation of X did not converge. */
  945. /* Suggestion: Check the input data and/or */
  946. /* repeat with different WHTSVD. */
  947. /* = 3 :: The computation of the eigenvalues did not */
  948. /* converge. */
  949. /* = 4 :: If data scaling was requested on input and */
  950. /* the procedure found inconsistency in the data */
  951. /* such that for some column index i, */
  952. /* X(:,i) = 0 but Y(:,i) /= 0, then Y(:,i) is set */
  953. /* to zero if JOBS=='C'. The computation proceeds */
  954. /* with original or modified data and warning */
  955. /* flag is set with INFO=4. */
  956. /* ............................................................. */
  957. /* ............................................................. */
  958. /* Parameters */
  959. /* ~~~~~~~~~~ */
  960. /* Local scalars */
  961. /* ~~~~~~~~~~~~~ */
  962. /* Local arrays */
  963. /* ~~~~~~~~~~~~ */
  964. /* External functions (BLAS and LAPACK) */
  965. /* ~~~~~~~~~~~~~~~~~ */
  966. /* External subroutines (BLAS and LAPACK) */
  967. /* ~~~~~~~~~~~~~~~~~~~~ */
  968. /* Intrinsic functions */
  969. /* ~~~~~~~~~~~~~~~~~~~ */
  970. /* ............................................................ */
  971. /* Parameter adjustments */
  972. x_dim1 = *ldx;
  973. x_offset = 1 + x_dim1 * 1;
  974. x -= x_offset;
  975. y_dim1 = *ldy;
  976. y_offset = 1 + y_dim1 * 1;
  977. y -= y_offset;
  978. --reig;
  979. --imeig;
  980. z_dim1 = *ldz;
  981. z_offset = 1 + z_dim1 * 1;
  982. z__ -= z_offset;
  983. --res;
  984. b_dim1 = *ldb;
  985. b_offset = 1 + b_dim1 * 1;
  986. b -= b_offset;
  987. w_dim1 = *ldw;
  988. w_offset = 1 + w_dim1 * 1;
  989. w -= w_offset;
  990. s_dim1 = *lds;
  991. s_offset = 1 + s_dim1 * 1;
  992. s -= s_offset;
  993. --work;
  994. --iwork;
  995. /* Function Body */
  996. one = 1.f;
  997. zero = 0.f;
  998. /* Test the input arguments */
  999. wntres = lsame_(jobr, "R");
  1000. sccolx = lsame_(jobs, "S") || lsame_(jobs, "C");
  1001. sccoly = lsame_(jobs, "Y");
  1002. wntvec = lsame_(jobz, "V");
  1003. wntref = lsame_(jobf, "R");
  1004. wntex = lsame_(jobf, "E");
  1005. *info = 0;
  1006. lquery = *lwork == -1 || *liwork == -1;
  1007. if (! (sccolx || sccoly || lsame_(jobs, "N"))) {
  1008. *info = -1;
  1009. } else if (! (wntvec || lsame_(jobz, "N") || lsame_(
  1010. jobz, "F"))) {
  1011. *info = -2;
  1012. } else if (! (wntres || lsame_(jobr, "N")) ||
  1013. wntres && ! wntvec) {
  1014. *info = -3;
  1015. } else if (! (wntref || wntex || lsame_(jobf, "N")))
  1016. {
  1017. *info = -4;
  1018. } else if (! (*whtsvd == 1 || *whtsvd == 2 || *whtsvd == 3 || *whtsvd ==
  1019. 4)) {
  1020. *info = -5;
  1021. } else if (*m < 0) {
  1022. *info = -6;
  1023. } else if (*n < 0 || *n > *m) {
  1024. *info = -7;
  1025. } else if (*ldx < *m) {
  1026. *info = -9;
  1027. } else if (*ldy < *m) {
  1028. *info = -11;
  1029. } else if (! (*nrnk == -2 || *nrnk == -1 || *nrnk >= 1 && *nrnk <= *n)) {
  1030. *info = -12;
  1031. } else if (*tol < zero || *tol >= one) {
  1032. *info = -13;
  1033. } else if (*ldz < *m) {
  1034. *info = -18;
  1035. } else if ((wntref || wntex) && *ldb < *m) {
  1036. *info = -21;
  1037. } else if (*ldw < *n) {
  1038. *info = -23;
  1039. } else if (*lds < *n) {
  1040. *info = -25;
  1041. }
  1042. if (*info == 0) {
  1043. /* Compute the minimal and the optimal workspace */
  1044. /* requirements. Simulate running the code and */
  1045. /* determine minimal and optimal sizes of the */
  1046. /* workspace at any moment of the run. */
  1047. if (*n == 0) {
  1048. /* Quick return. All output except K is void. */
  1049. /* INFO=1 signals the void input. */
  1050. /* In case of a workspace query, the default */
  1051. /* minimal workspace lengths are returned. */
  1052. if (lquery) {
  1053. iwork[1] = 1;
  1054. work[1] = 2.f;
  1055. work[2] = 2.f;
  1056. } else {
  1057. *k = 0;
  1058. }
  1059. *info = 1;
  1060. return 0;
  1061. }
  1062. mlwork = f2cmax(2,*n);
  1063. olwork = f2cmax(2,*n);
  1064. iminwr = 1;
  1065. /* SELECT CASE ( WHTSVD ) */
  1066. if (*whtsvd == 1) {
  1067. /* The following is specified as the minimal */
  1068. /* length of WORK in the definition of SGESVD: */
  1069. /* MWRSVD = MAX(1,3*MIN(M,N)+MAX(M,N),5*MIN(M,N)) */
  1070. /* Computing MAX */
  1071. i__1 = 1, i__2 = f2cmin(*m,*n) * 3 + f2cmax(*m,*n), i__1 = f2cmax(i__1,
  1072. i__2), i__2 = f2cmin(*m,*n) * 5;
  1073. mwrsvd = f2cmax(i__1,i__2);
  1074. /* Computing MAX */
  1075. i__1 = mlwork, i__2 = *n + mwrsvd;
  1076. mlwork = f2cmax(i__1,i__2);
  1077. if (lquery) {
  1078. sgesvd_("O", "S", m, n, &x[x_offset], ldx, &work[1], &b[
  1079. b_offset], ldb, &w[w_offset], ldw, rdummy, &c_n1, &
  1080. info1);
  1081. /* Computing MAX */
  1082. i__1 = mwrsvd, i__2 = (integer) rdummy[0];
  1083. lwrsvd = f2cmax(i__1,i__2);
  1084. /* Computing MAX */
  1085. i__1 = olwork, i__2 = *n + lwrsvd;
  1086. olwork = f2cmax(i__1,i__2);
  1087. }
  1088. } else if (*whtsvd == 2) {
  1089. /* The following is specified as the minimal */
  1090. /* length of WORK in the definition of SGESDD: */
  1091. /* MWRSDD = 3*MIN(M,N)*MIN(M,N) + */
  1092. /* MAX( MAX(M,N),5*MIN(M,N)*MIN(M,N)+4*MIN(M,N) ) */
  1093. /* IMINWR = 8*MIN(M,N) */
  1094. /* Computing MAX */
  1095. i__1 = f2cmax(*m,*n), i__2 = f2cmin(*m,*n) * 5 * f2cmin(*m,*n) + (f2cmin(*m,*
  1096. n) << 2);
  1097. mwrsdd = f2cmin(*m,*n) * 3 * f2cmin(*m,*n) + f2cmax(i__1,i__2);
  1098. /* Computing MAX */
  1099. i__1 = mlwork, i__2 = *n + mwrsdd;
  1100. mlwork = f2cmax(i__1,i__2);
  1101. iminwr = f2cmin(*m,*n) << 3;
  1102. if (lquery) {
  1103. sgesdd_("O", m, n, &x[x_offset], ldx, &work[1], &b[b_offset],
  1104. ldb, &w[w_offset], ldw, rdummy, &c_n1, &iwork[1], &
  1105. info1);
  1106. /* Computing MAX */
  1107. i__1 = mwrsdd, i__2 = (integer) rdummy[0];
  1108. lwrsdd = f2cmax(i__1,i__2);
  1109. /* Computing MAX */
  1110. i__1 = olwork, i__2 = *n + lwrsdd;
  1111. olwork = f2cmax(i__1,i__2);
  1112. }
  1113. } else if (*whtsvd == 3) {
  1114. /* LWQP3 = 3*N+1 */
  1115. /* LWORQ = MAX(N, 1) */
  1116. /* MWRSVD = MAX(1,3*MIN(M,N)+MAX(M,N),5*MIN(M,N)) */
  1117. /* MWRSVQ = N + MAX( LWQP3, MWRSVD, LWORQ )+ MAX(M,2) */
  1118. /* MLWORK = N + MWRSVQ */
  1119. /* IMINWR = M+N-1 */
  1120. sgesvdq_("H", "P", "N", "R", "R", m, n, &x[x_offset], ldx, &work[
  1121. 1], &z__[z_offset], ldz, &w[w_offset], ldw, &numrnk, &
  1122. iwork[1], &c_n1, rdummy, &c_n1, rdummy2, &c_n1, &info1);
  1123. iminwr = iwork[1];
  1124. mwrsvq = (integer) rdummy[1];
  1125. /* Computing MAX */
  1126. i__1 = mlwork, i__2 = *n + mwrsvq + (integer) rdummy2[0];
  1127. mlwork = f2cmax(i__1,i__2);
  1128. if (lquery) {
  1129. lwrsvq = (integer) rdummy[0];
  1130. /* Computing MAX */
  1131. i__1 = olwork, i__2 = *n + lwrsvq + (integer) rdummy2[0];
  1132. olwork = f2cmax(i__1,i__2);
  1133. }
  1134. } else if (*whtsvd == 4) {
  1135. *(unsigned char *)jsvopt = 'J';
  1136. /* MWRSVJ = MAX( 7, 2*M+N, 6*N+2*N*N )! for JSVOPT='V' */
  1137. /* Computing MAX */
  1138. i__1 = 7, i__2 = (*m << 1) + *n, i__1 = f2cmax(i__1,i__2), i__2 = (*
  1139. n << 2) + *n * *n, i__1 = f2cmax(i__1,i__2), i__2 = (*n << 1)
  1140. + *n * *n + 6;
  1141. mwrsvj = f2cmax(i__1,i__2);
  1142. /* Computing MAX */
  1143. i__1 = mlwork, i__2 = *n + mwrsvj;
  1144. mlwork = f2cmax(i__1,i__2);
  1145. /* Computing MAX */
  1146. i__1 = 3, i__2 = *m + *n * 3;
  1147. iminwr = f2cmax(i__1,i__2);
  1148. if (lquery) {
  1149. /* Computing MAX */
  1150. i__1 = olwork, i__2 = *n + mwrsvj;
  1151. olwork = f2cmax(i__1,i__2);
  1152. }
  1153. }
  1154. /* END SELECT */
  1155. if (wntvec || wntex || lsame_(jobz, "F")) {
  1156. *(unsigned char *)jobzl = 'V';
  1157. } else {
  1158. *(unsigned char *)jobzl = 'N';
  1159. }
  1160. /* Workspace calculation to the SGEEV call */
  1161. if (lsame_(jobzl, "V")) {
  1162. /* Computing MAX */
  1163. i__1 = 1, i__2 = *n << 2;
  1164. mwrkev = f2cmax(i__1,i__2);
  1165. } else {
  1166. /* Computing MAX */
  1167. i__1 = 1, i__2 = *n * 3;
  1168. mwrkev = f2cmax(i__1,i__2);
  1169. }
  1170. /* Computing MAX */
  1171. i__1 = mlwork, i__2 = *n + mwrkev;
  1172. mlwork = f2cmax(i__1,i__2);
  1173. if (lquery) {
  1174. sgeev_("N", jobzl, n, &s[s_offset], lds, &reig[1], &imeig[1], &w[
  1175. w_offset], ldw, &w[w_offset], ldw, rdummy, &c_n1, &info1);
  1176. /* Computing MAX */
  1177. i__1 = mwrkev, i__2 = (integer) rdummy[0];
  1178. lwrkev = f2cmax(i__1,i__2);
  1179. /* Computing MAX */
  1180. i__1 = olwork, i__2 = *n + lwrkev;
  1181. olwork = f2cmax(i__1,i__2);
  1182. }
  1183. if (*liwork < iminwr && ! lquery) {
  1184. *info = -29;
  1185. }
  1186. if (*lwork < mlwork && ! lquery) {
  1187. *info = -27;
  1188. }
  1189. }
  1190. if (*info != 0) {
  1191. i__1 = -(*info);
  1192. xerbla_("SGEDMD", &i__1);
  1193. return 0;
  1194. } else if (lquery) {
  1195. /* Return minimal and optimal workspace sizes */
  1196. iwork[1] = iminwr;
  1197. work[1] = (real) mlwork;
  1198. work[2] = (real) olwork;
  1199. return 0;
  1200. }
  1201. /* ............................................................ */
  1202. ofl = slamch_("O");
  1203. small = slamch_("S");
  1204. badxy = FALSE_;
  1205. /* <1> Optional scaling of the snapshots (columns of X, Y) */
  1206. /* ========================================================== */
  1207. if (sccolx) {
  1208. /* The columns of X will be normalized. */
  1209. /* To prevent overflows, the column norms of X are */
  1210. /* carefully computed using SLASSQ. */
  1211. *k = 0;
  1212. i__1 = *n;
  1213. for (i__ = 1; i__ <= i__1; ++i__) {
  1214. /* WORK(i) = DNRM2( M, X(1,i), 1 ) */
  1215. scale = zero;
  1216. slassq_(m, &x[i__ * x_dim1 + 1], &c__1, &scale, &ssum);
  1217. if (sisnan_(&scale) || sisnan_(&ssum)) {
  1218. *k = 0;
  1219. *info = -8;
  1220. i__2 = -(*info);
  1221. xerbla_("SGEDMD", &i__2);
  1222. }
  1223. if (scale != zero && ssum != zero) {
  1224. rootsc = sqrt(ssum);
  1225. if (scale >= ofl / rootsc) {
  1226. /* Norm of X(:,i) overflows. First, X(:,i) */
  1227. /* is scaled by */
  1228. /* ( ONE / ROOTSC ) / SCALE = 1/||X(:,i)||_2. */
  1229. /* Next, the norm of X(:,i) is stored without */
  1230. /* overflow as WORK(i) = - SCALE * (ROOTSC/M), */
  1231. /* the minus sign indicating the 1/M factor. */
  1232. /* Scaling is performed without overflow, and */
  1233. /* underflow may occur in the smallest entries */
  1234. /* of X(:,i). The relative backward and forward */
  1235. /* errors are small in the ell_2 norm. */
  1236. r__1 = one / rootsc;
  1237. slascl_("G", &c__0, &c__0, &scale, &r__1, m, &c__1, &x[
  1238. i__ * x_dim1 + 1], m, &info2);
  1239. work[i__] = -scale * (rootsc / (real) (*m));
  1240. } else {
  1241. /* X(:,i) will be scaled to unit 2-norm */
  1242. work[i__] = scale * rootsc;
  1243. slascl_("G", &c__0, &c__0, &work[i__], &one, m, &c__1, &x[
  1244. i__ * x_dim1 + 1], m, &info2);
  1245. /* X(1:M,i) = (ONE/WORK(i)) * X(1:M,i) ! INTRINSIC */
  1246. /* LAPACK */
  1247. }
  1248. } else {
  1249. work[i__] = zero;
  1250. ++(*k);
  1251. }
  1252. }
  1253. if (*k == *n) {
  1254. /* All columns of X are zero. Return error code -8. */
  1255. /* (the 8th input variable had an illegal value) */
  1256. *k = 0;
  1257. *info = -8;
  1258. i__1 = -(*info);
  1259. xerbla_("SGEDMD", &i__1);
  1260. return 0;
  1261. }
  1262. i__1 = *n;
  1263. for (i__ = 1; i__ <= i__1; ++i__) {
  1264. /* Now, apply the same scaling to the columns of Y. */
  1265. if (work[i__] > zero) {
  1266. r__1 = one / work[i__];
  1267. sscal_(m, &r__1, &y[i__ * y_dim1 + 1], &c__1);
  1268. /* Y(1:M,i) = (ONE/WORK(i)) * Y(1:M,i) ! INTRINSIC */
  1269. /* BLAS CALL */
  1270. } else if (work[i__] < zero) {
  1271. r__1 = -work[i__];
  1272. r__2 = one / (real) (*m);
  1273. slascl_("G", &c__0, &c__0, &r__1, &r__2, m, &c__1, &y[i__ *
  1274. y_dim1 + 1], m, &info2);
  1275. /* LAPACK CA */
  1276. } else if (y[isamax_(m, &y[i__ * y_dim1 + 1], &c__1) + i__ *
  1277. y_dim1] != zero) {
  1278. /* X(:,i) is zero vector. For consistency, */
  1279. /* Y(:,i) should also be zero. If Y(:,i) is not */
  1280. /* zero, then the data might be inconsistent or */
  1281. /* corrupted. If JOBS == 'C', Y(:,i) is set to */
  1282. /* zero and a warning flag is raised. */
  1283. /* The computation continues but the */
  1284. /* situation will be reported in the output. */
  1285. badxy = TRUE_;
  1286. if (lsame_(jobs, "C")) {
  1287. sscal_(m, &zero, &y[i__ * y_dim1 + 1], &c__1);
  1288. }
  1289. /* BLAS CALL */
  1290. }
  1291. }
  1292. }
  1293. if (sccoly) {
  1294. /* The columns of Y will be normalized. */
  1295. /* To prevent overflows, the column norms of Y are */
  1296. /* carefully computed using SLASSQ. */
  1297. i__1 = *n;
  1298. for (i__ = 1; i__ <= i__1; ++i__) {
  1299. /* WORK(i) = DNRM2( M, Y(1,i), 1 ) */
  1300. scale = zero;
  1301. slassq_(m, &y[i__ * y_dim1 + 1], &c__1, &scale, &ssum);
  1302. if (sisnan_(&scale) || sisnan_(&ssum)) {
  1303. *k = 0;
  1304. *info = -10;
  1305. i__2 = -(*info);
  1306. xerbla_("SGEDMD", &i__2);
  1307. }
  1308. if (scale != zero && ssum != zero) {
  1309. rootsc = sqrt(ssum);
  1310. if (scale >= ofl / rootsc) {
  1311. /* Norm of Y(:,i) overflows. First, Y(:,i) */
  1312. /* is scaled by */
  1313. /* ( ONE / ROOTSC ) / SCALE = 1/||Y(:,i)||_2. */
  1314. /* Next, the norm of Y(:,i) is stored without */
  1315. /* overflow as WORK(i) = - SCALE * (ROOTSC/M), */
  1316. /* the minus sign indicating the 1/M factor. */
  1317. /* Scaling is performed without overflow, and */
  1318. /* underflow may occur in the smallest entries */
  1319. /* of Y(:,i). The relative backward and forward */
  1320. /* errors are small in the ell_2 norm. */
  1321. r__1 = one / rootsc;
  1322. slascl_("G", &c__0, &c__0, &scale, &r__1, m, &c__1, &y[
  1323. i__ * y_dim1 + 1], m, &info2);
  1324. work[i__] = -scale * (rootsc / (real) (*m));
  1325. } else {
  1326. /* X(:,i) will be scaled to unit 2-norm */
  1327. work[i__] = scale * rootsc;
  1328. slascl_("G", &c__0, &c__0, &work[i__], &one, m, &c__1, &y[
  1329. i__ * y_dim1 + 1], m, &info2);
  1330. /* Y(1:M,i) = (ONE/WORK(i)) * Y(1:M,i) ! INTRINSIC */
  1331. /* LAPACK */
  1332. }
  1333. } else {
  1334. work[i__] = zero;
  1335. }
  1336. }
  1337. i__1 = *n;
  1338. for (i__ = 1; i__ <= i__1; ++i__) {
  1339. /* Now, apply the same scaling to the columns of X. */
  1340. if (work[i__] > zero) {
  1341. r__1 = one / work[i__];
  1342. sscal_(m, &r__1, &x[i__ * x_dim1 + 1], &c__1);
  1343. /* X(1:M,i) = (ONE/WORK(i)) * X(1:M,i) ! INTRINSIC */
  1344. /* BLAS CALL */
  1345. } else if (work[i__] < zero) {
  1346. r__1 = -work[i__];
  1347. r__2 = one / (real) (*m);
  1348. slascl_("G", &c__0, &c__0, &r__1, &r__2, m, &c__1, &x[i__ *
  1349. x_dim1 + 1], m, &info2);
  1350. /* LAPACK CA */
  1351. } else if (x[isamax_(m, &x[i__ * x_dim1 + 1], &c__1) + i__ *
  1352. x_dim1] != zero) {
  1353. /* Y(:,i) is zero vector. If X(:,i) is not */
  1354. /* zero, then a warning flag is raised. */
  1355. /* The computation continues but the */
  1356. /* situation will be reported in the output. */
  1357. badxy = TRUE_;
  1358. }
  1359. }
  1360. }
  1361. /* <2> SVD of the data snapshot matrix X. */
  1362. /* ===================================== */
  1363. /* The left singular vectors are stored in the array X. */
  1364. /* The right singular vectors are in the array W. */
  1365. /* The array W will later on contain the eigenvectors */
  1366. /* of a Rayleigh quotient. */
  1367. numrnk = *n;
  1368. /* SELECT CASE ( WHTSVD ) */
  1369. if (*whtsvd == 1) {
  1370. i__1 = *lwork - *n;
  1371. sgesvd_("O", "S", m, n, &x[x_offset], ldx, &work[1], &b[b_offset],
  1372. ldb, &w[w_offset], ldw, &work[*n + 1], &i__1, &info1);
  1373. /* LAPACK CAL */
  1374. *(unsigned char *)t_or_n__ = 'T';
  1375. } else if (*whtsvd == 2) {
  1376. i__1 = *lwork - *n;
  1377. sgesdd_("O", m, n, &x[x_offset], ldx, &work[1], &b[b_offset], ldb, &w[
  1378. w_offset], ldw, &work[*n + 1], &i__1, &iwork[1], &info1);
  1379. /* LAPACK CAL */
  1380. *(unsigned char *)t_or_n__ = 'T';
  1381. } else if (*whtsvd == 3) {
  1382. i__1 = *lwork - *n - f2cmax(2,*m);
  1383. i__2 = f2cmax(2,*m);
  1384. sgesvdq_("H", "P", "N", "R", "R", m, n, &x[x_offset], ldx, &work[1], &
  1385. z__[z_offset], ldz, &w[w_offset], ldw, &numrnk, &iwork[1],
  1386. liwork, &work[*n + f2cmax(2,*m) + 1], &i__1, &work[*n + 1], &
  1387. i__2, &info1);
  1388. slacpy_("A", m, &numrnk, &z__[z_offset], ldz, &x[x_offset], ldx);
  1389. /* LAPACK C */
  1390. *(unsigned char *)t_or_n__ = 'T';
  1391. } else if (*whtsvd == 4) {
  1392. i__1 = *lwork - *n;
  1393. sgejsv_("F", "U", jsvopt, "N", "N", "P", m, n, &x[x_offset], ldx, &
  1394. work[1], &z__[z_offset], ldz, &w[w_offset], ldw, &work[*n + 1]
  1395. , &i__1, &iwork[1], &info1);
  1396. /* LAPACK CALL */
  1397. slacpy_("A", m, n, &z__[z_offset], ldz, &x[x_offset], ldx);
  1398. /* LAPACK CALL */
  1399. *(unsigned char *)t_or_n__ = 'N';
  1400. xscl1 = work[*n + 1];
  1401. xscl2 = work[*n + 2];
  1402. if (xscl1 != xscl2) {
  1403. /* This is an exceptional situation. If the */
  1404. /* data matrices are not scaled and the */
  1405. /* largest singular value of X overflows. */
  1406. /* In that case SGEJSV can return the SVD */
  1407. /* in scaled form. The scaling factor can be used */
  1408. /* to rescale the data (X and Y). */
  1409. slascl_("G", &c__0, &c__0, &xscl1, &xscl2, m, n, &y[y_offset],
  1410. ldy, &info2);
  1411. }
  1412. /* END SELECT */
  1413. }
  1414. if (info1 > 0) {
  1415. /* The SVD selected subroutine did not converge. */
  1416. /* Return with an error code. */
  1417. *info = 2;
  1418. return 0;
  1419. }
  1420. if (work[1] == zero) {
  1421. /* The largest computed singular value of (scaled) */
  1422. /* X is zero. Return error code -8 */
  1423. /* (the 8th input variable had an illegal value). */
  1424. *k = 0;
  1425. *info = -8;
  1426. i__1 = -(*info);
  1427. xerbla_("SGEDMD", &i__1);
  1428. return 0;
  1429. }
  1430. /* <3> Determine the numerical rank of the data */
  1431. /* snapshots matrix X. This depends on the */
  1432. /* parameters NRNK and TOL. */
  1433. /* SELECT CASE ( NRNK ) */
  1434. if (*nrnk == -1) {
  1435. *k = 1;
  1436. i__1 = numrnk;
  1437. for (i__ = 2; i__ <= i__1; ++i__) {
  1438. if (work[i__] <= work[1] * *tol || work[i__] <= small) {
  1439. myexit_();
  1440. }
  1441. ++(*k);
  1442. }
  1443. } else if (*nrnk == -2) {
  1444. *k = 1;
  1445. i__1 = numrnk - 1;
  1446. for (i__ = 1; i__ <= i__1; ++i__) {
  1447. if (work[i__ + 1] <= work[i__] * *tol || work[i__] <= small) {
  1448. myexit_();
  1449. }
  1450. ++(*k);
  1451. }
  1452. } else {
  1453. *k = 1;
  1454. i__1 = *nrnk;
  1455. for (i__ = 2; i__ <= i__1; ++i__) {
  1456. if (work[i__] <= small) {
  1457. myexit_();
  1458. }
  1459. ++(*k);
  1460. }
  1461. /* END SELECT */
  1462. }
  1463. /* Now, U = X(1:M,1:K) is the SVD/POD basis for the */
  1464. /* snapshot data in the input matrix X. */
  1465. /* <4> Compute the Rayleigh quotient S = U^T * A * U. */
  1466. /* Depending on the requested outputs, the computation */
  1467. /* is organized to compute additional auxiliary */
  1468. /* matrices (for the residuals and refinements). */
  1469. /* In all formulas below, we need V_k*Sigma_k^(-1) */
  1470. /* where either V_k is in W(1:N,1:K), or V_k^T is in */
  1471. /* W(1:K,1:N). Here Sigma_k=diag(WORK(1:K)). */
  1472. if (lsame_(t_or_n__, "N")) {
  1473. i__1 = *k;
  1474. for (i__ = 1; i__ <= i__1; ++i__) {
  1475. r__1 = one / work[i__];
  1476. sscal_(n, &r__1, &w[i__ * w_dim1 + 1], &c__1);
  1477. /* W(1:N,i) = (ONE/WORK(i)) * W(1:N,i) ! INTRINSIC */
  1478. /* BLAS CALL */
  1479. }
  1480. } else {
  1481. /* This non-unit stride access is due to the fact */
  1482. /* that SGESVD, SGESVDQ and SGESDD return the */
  1483. /* transposed matrix of the right singular vectors. */
  1484. /* DO i = 1, K */
  1485. /* CALL SSCAL( N, ONE/WORK(i), W(i,1), LDW ) ! BLAS CALL */
  1486. /* ! W(i,1:N) = (ONE/WORK(i)) * W(i,1:N) ! INTRINSIC */
  1487. /* END DO */
  1488. i__1 = *k;
  1489. for (i__ = 1; i__ <= i__1; ++i__) {
  1490. work[*n + i__] = one / work[i__];
  1491. }
  1492. i__1 = *n;
  1493. for (j = 1; j <= i__1; ++j) {
  1494. i__2 = *k;
  1495. for (i__ = 1; i__ <= i__2; ++i__) {
  1496. w[i__ + j * w_dim1] = work[*n + i__] * w[i__ + j * w_dim1];
  1497. }
  1498. }
  1499. }
  1500. if (wntref) {
  1501. /* Need A*U(:,1:K)=Y*V_k*inv(diag(WORK(1:K))) */
  1502. /* for computing the refined Ritz vectors */
  1503. /* (optionally, outside SGEDMD). */
  1504. sgemm_("N", t_or_n__, m, k, n, &one, &y[y_offset], ldy, &w[w_offset],
  1505. ldw, &zero, &z__[z_offset], ldz);
  1506. /* Z(1:M,1:K)=MATMUL(Y(1:M,1:N),TRANSPOSE(W(1:K,1:N))) ! INTRI */
  1507. /* Z(1:M,1:K)=MATMUL(Y(1:M,1:N),W(1:N,1:K)) ! INTRI */
  1508. /* At this point Z contains */
  1509. /* A * U(:,1:K) = Y * V_k * Sigma_k^(-1), and */
  1510. /* this is needed for computing the residuals. */
  1511. /* This matrix is returned in the array B and */
  1512. /* it can be used to compute refined Ritz vectors. */
  1513. /* BLAS */
  1514. slacpy_("A", m, k, &z__[z_offset], ldz, &b[b_offset], ldb);
  1515. /* B(1:M,1:K) = Z(1:M,1:K) ! INTRINSIC */
  1516. /* BLAS CALL */
  1517. sgemm_("T", "N", k, k, m, &one, &x[x_offset], ldx, &z__[z_offset],
  1518. ldz, &zero, &s[s_offset], lds);
  1519. /* S(1:K,1:K) = MATMUL(TANSPOSE(X(1:M,1:K)),Z(1:M,1:K)) ! INTRI */
  1520. /* At this point S = U^T * A * U is the Rayleigh quotient. */
  1521. /* BLAS */
  1522. } else {
  1523. /* A * U(:,1:K) is not explicitly needed and the */
  1524. /* computation is organized differently. The Rayleigh */
  1525. /* quotient is computed more efficiently. */
  1526. sgemm_("T", "N", k, n, m, &one, &x[x_offset], ldx, &y[y_offset], ldy,
  1527. &zero, &z__[z_offset], ldz);
  1528. /* Z(1:K,1:N) = MATMUL( TRANSPOSE(X(1:M,1:K)), Y(1:M,1:N) ) ! IN */
  1529. /* In the two SGEMM calls here, can use K for LDZ */
  1530. /* B */
  1531. sgemm_("N", t_or_n__, k, k, n, &one, &z__[z_offset], ldz, &w[w_offset]
  1532. , ldw, &zero, &s[s_offset], lds);
  1533. /* S(1:K,1:K) = MATMUL(Z(1:K,1:N),TRANSPOSE(W(1:K,1:N))) ! INTRIN */
  1534. /* S(1:K,1:K) = MATMUL(Z(1:K,1:N),(W(1:N,1:K))) ! INTRIN */
  1535. /* At this point S = U^T * A * U is the Rayleigh quotient. */
  1536. /* If the residuals are requested, save scaled V_k into Z. */
  1537. /* Recall that V_k or V_k^T is stored in W. */
  1538. /* BLAS */
  1539. if (wntres || wntex) {
  1540. if (lsame_(t_or_n__, "N")) {
  1541. slacpy_("A", n, k, &w[w_offset], ldw, &z__[z_offset], ldz);
  1542. } else {
  1543. slacpy_("A", k, n, &w[w_offset], ldw, &z__[z_offset], ldz);
  1544. }
  1545. }
  1546. }
  1547. /* <5> Compute the Ritz values and (if requested) the */
  1548. /* right eigenvectors of the Rayleigh quotient. */
  1549. i__1 = *lwork - *n;
  1550. sgeev_("N", jobzl, k, &s[s_offset], lds, &reig[1], &imeig[1], &w[w_offset]
  1551. , ldw, &w[w_offset], ldw, &work[*n + 1], &i__1, &info1);
  1552. /* W(1:K,1:K) contains the eigenvectors of the Rayleigh */
  1553. /* quotient. Even in the case of complex spectrum, all */
  1554. /* computation is done in real arithmetic. REIG and */
  1555. /* IMEIG are the real and the imaginary parts of the */
  1556. /* eigenvalues, so that the spectrum is given as */
  1557. /* REIG(:) + sqrt(-1)*IMEIG(:). Complex conjugate pairs */
  1558. /* are listed at consecutive positions. For such a */
  1559. /* complex conjugate pair of the eigenvalues, the */
  1560. /* corresponding eigenvectors are also a complex */
  1561. /* conjugate pair with the real and imaginary parts */
  1562. /* stored column-wise in W at the corresponding */
  1563. /* consecutive column indices. See the description of Z. */
  1564. /* Also, see the description of SGEEV. */
  1565. /* LAPACK C */
  1566. if (info1 > 0) {
  1567. /* SGEEV failed to compute the eigenvalues and */
  1568. /* eigenvectors of the Rayleigh quotient. */
  1569. *info = 3;
  1570. return 0;
  1571. }
  1572. /* <6> Compute the eigenvectors (if requested) and, */
  1573. /* the residuals (if requested). */
  1574. if (wntvec || wntex) {
  1575. if (wntres) {
  1576. if (wntref) {
  1577. /* Here, if the refinement is requested, we have */
  1578. /* A*U(:,1:K) already computed and stored in Z. */
  1579. /* For the residuals, need Y = A * U(:,1;K) * W. */
  1580. sgemm_("N", "N", m, k, k, &one, &z__[z_offset], ldz, &w[
  1581. w_offset], ldw, &zero, &y[y_offset], ldy);
  1582. /* Y(1:M,1:K) = Z(1:M,1:K) * W(1:K,1:K) ! INTRINSIC */
  1583. /* This frees Z; Y contains A * U(:,1:K) * W. */
  1584. /* BLAS CALL */
  1585. } else {
  1586. /* Compute S = V_k * Sigma_k^(-1) * W, where */
  1587. /* V_k * Sigma_k^(-1) is stored in Z */
  1588. sgemm_(t_or_n__, "N", n, k, k, &one, &z__[z_offset], ldz, &w[
  1589. w_offset], ldw, &zero, &s[s_offset], lds);
  1590. /* Then, compute Z = Y * S = */
  1591. /* = Y * V_k * Sigma_k^(-1) * W(1:K,1:K) = */
  1592. /* = A * U(:,1:K) * W(1:K,1:K) */
  1593. sgemm_("N", "N", m, k, n, &one, &y[y_offset], ldy, &s[
  1594. s_offset], lds, &zero, &z__[z_offset], ldz);
  1595. /* Save a copy of Z into Y and free Z for holding */
  1596. /* the Ritz vectors. */
  1597. slacpy_("A", m, k, &z__[z_offset], ldz, &y[y_offset], ldy);
  1598. if (wntex) {
  1599. slacpy_("A", m, k, &z__[z_offset], ldz, &b[b_offset], ldb);
  1600. }
  1601. }
  1602. } else if (wntex) {
  1603. /* Compute S = V_k * Sigma_k^(-1) * W, where */
  1604. /* V_k * Sigma_k^(-1) is stored in Z */
  1605. sgemm_(t_or_n__, "N", n, k, k, &one, &z__[z_offset], ldz, &w[
  1606. w_offset], ldw, &zero, &s[s_offset], lds);
  1607. /* Then, compute Z = Y * S = */
  1608. /* = Y * V_k * Sigma_k^(-1) * W(1:K,1:K) = */
  1609. /* = A * U(:,1:K) * W(1:K,1:K) */
  1610. sgemm_("N", "N", m, k, n, &one, &y[y_offset], ldy, &s[s_offset],
  1611. lds, &zero, &b[b_offset], ldb);
  1612. /* The above call replaces the following two calls */
  1613. /* that were used in the developing-testing phase. */
  1614. /* CALL SGEMM( 'N', 'N', M, K, N, ONE, Y, LDY, S, & */
  1615. /* LDS, ZERO, Z, LDZ) */
  1616. /* Save a copy of Z into B and free Z for holding */
  1617. /* the Ritz vectors. */
  1618. /* CALL SLACPY( 'A', M, K, Z, LDZ, B, LDB ) */
  1619. }
  1620. /* Compute the real form of the Ritz vectors */
  1621. if (wntvec) {
  1622. sgemm_("N", "N", m, k, k, &one, &x[x_offset], ldx, &w[w_offset],
  1623. ldw, &zero, &z__[z_offset], ldz);
  1624. }
  1625. /* Z(1:M,1:K) = MATMUL(X(1:M,1:K), W(1:K,1:K)) ! INTRINSIC */
  1626. /* BLAS CALL */
  1627. if (wntres) {
  1628. i__ = 1;
  1629. while(i__ <= *k) {
  1630. if (imeig[i__] == zero) {
  1631. /* have a real eigenvalue with real eigenvector */
  1632. r__1 = -reig[i__];
  1633. saxpy_(m, &r__1, &z__[i__ * z_dim1 + 1], &c__1, &y[i__ *
  1634. y_dim1 + 1], &c__1);
  1635. /* Y(1:M,i) = Y(1:M,i) - REIG(i) * Z(1:M,i) ! */
  1636. res[i__] = snrm2_(m, &y[i__ * y_dim1 + 1], &c__1);
  1637. ++i__;
  1638. } else {
  1639. /* Have a complex conjugate pair */
  1640. /* REIG(i) +- sqrt(-1)*IMEIG(i). */
  1641. /* Since all computation is done in real */
  1642. /* arithmetic, the formula for the residual */
  1643. /* is recast for real representation of the */
  1644. /* complex conjugate eigenpair. See the */
  1645. /* description of RES. */
  1646. ab[0] = reig[i__];
  1647. ab[1] = -imeig[i__];
  1648. ab[2] = imeig[i__];
  1649. ab[3] = reig[i__];
  1650. r__1 = -one;
  1651. sgemm_("N", "N", m, &c__2, &c__2, &r__1, &z__[i__ *
  1652. z_dim1 + 1], ldz, ab, &c__2, &one, &y[i__ *
  1653. y_dim1 + 1], ldy);
  1654. /* Y(1:M,i:i+1) = Y(1:M,i:i+1) - Z(1:M,i:i+1) * AB ! INT */
  1655. /* BL */
  1656. res[i__] = slange_("F", m, &c__2, &y[i__ * y_dim1 + 1],
  1657. ldy, &work[*n + 1]);
  1658. /* LA */
  1659. res[i__ + 1] = res[i__];
  1660. i__ += 2;
  1661. }
  1662. }
  1663. }
  1664. }
  1665. if (*whtsvd == 4) {
  1666. work[*n + 1] = xscl1;
  1667. work[*n + 2] = xscl2;
  1668. }
  1669. /* Successful exit. */
  1670. if (! badxy) {
  1671. *info = 0;
  1672. } else {
  1673. /* A warning on possible data inconsistency. */
  1674. /* This should be a rare event. */
  1675. *info = 4;
  1676. }
  1677. /* ............................................................ */
  1678. return 0;
  1679. /* ...... */
  1680. } /* sgedmd_ */