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