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.

cgedmd.c 59 kB

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