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.

zgedmd.c 59 kB

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