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.

cbdsqr.c 41 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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]/df(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 doublereal c_b15 = -.125;
  489. static integer c__1 = 1;
  490. static real c_b49 = 1.f;
  491. static real c_b72 = -1.f;
  492. /* > \brief \b CBDSQR */
  493. /* =========== DOCUMENTATION =========== */
  494. /* Online html documentation available at */
  495. /* http://www.netlib.org/lapack/explore-html/ */
  496. /* > \htmlonly */
  497. /* > Download CBDSQR + dependencies */
  498. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cbdsqr.
  499. f"> */
  500. /* > [TGZ]</a> */
  501. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cbdsqr.
  502. f"> */
  503. /* > [ZIP]</a> */
  504. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cbdsqr.
  505. f"> */
  506. /* > [TXT]</a> */
  507. /* > \endhtmlonly */
  508. /* Definition: */
  509. /* =========== */
  510. /* SUBROUTINE CBDSQR( UPLO, N, NCVT, NRU, NCC, D, E, VT, LDVT, U, */
  511. /* LDU, C, LDC, RWORK, INFO ) */
  512. /* CHARACTER UPLO */
  513. /* INTEGER INFO, LDC, LDU, LDVT, N, NCC, NCVT, NRU */
  514. /* REAL D( * ), E( * ), RWORK( * ) */
  515. /* COMPLEX C( LDC, * ), U( LDU, * ), VT( LDVT, * ) */
  516. /* > \par Purpose: */
  517. /* ============= */
  518. /* > */
  519. /* > \verbatim */
  520. /* > */
  521. /* > CBDSQR computes the singular values and, optionally, the right and/or */
  522. /* > left singular vectors from the singular value decomposition (SVD) of */
  523. /* > a real N-by-N (upper or lower) bidiagonal matrix B using the implicit */
  524. /* > zero-shift QR algorithm. The SVD of B has the form */
  525. /* > */
  526. /* > B = Q * S * P**H */
  527. /* > */
  528. /* > where S is the diagonal matrix of singular values, Q is an orthogonal */
  529. /* > matrix of left singular vectors, and P is an orthogonal matrix of */
  530. /* > right singular vectors. If left singular vectors are requested, this */
  531. /* > subroutine actually returns U*Q instead of Q, and, if right singular */
  532. /* > vectors are requested, this subroutine returns P**H*VT instead of */
  533. /* > P**H, for given complex input matrices U and VT. When U and VT are */
  534. /* > the unitary matrices that reduce a general matrix A to bidiagonal */
  535. /* > form: A = U*B*VT, as computed by CGEBRD, then */
  536. /* > */
  537. /* > A = (U*Q) * S * (P**H*VT) */
  538. /* > */
  539. /* > is the SVD of A. Optionally, the subroutine may also compute Q**H*C */
  540. /* > for a given complex input matrix C. */
  541. /* > */
  542. /* > See "Computing Small Singular Values of Bidiagonal Matrices With */
  543. /* > Guaranteed High Relative Accuracy," by J. Demmel and W. Kahan, */
  544. /* > LAPACK Working Note #3 (or SIAM J. Sci. Statist. Comput. vol. 11, */
  545. /* > no. 5, pp. 873-912, Sept 1990) and */
  546. /* > "Accurate singular values and differential qd algorithms," by */
  547. /* > B. Parlett and V. Fernando, Technical Report CPAM-554, Mathematics */
  548. /* > Department, University of California at Berkeley, July 1992 */
  549. /* > for a detailed description of the algorithm. */
  550. /* > \endverbatim */
  551. /* Arguments: */
  552. /* ========== */
  553. /* > \param[in] UPLO */
  554. /* > \verbatim */
  555. /* > UPLO is CHARACTER*1 */
  556. /* > = 'U': B is upper bidiagonal; */
  557. /* > = 'L': B is lower bidiagonal. */
  558. /* > \endverbatim */
  559. /* > */
  560. /* > \param[in] N */
  561. /* > \verbatim */
  562. /* > N is INTEGER */
  563. /* > The order of the matrix B. N >= 0. */
  564. /* > \endverbatim */
  565. /* > */
  566. /* > \param[in] NCVT */
  567. /* > \verbatim */
  568. /* > NCVT is INTEGER */
  569. /* > The number of columns of the matrix VT. NCVT >= 0. */
  570. /* > \endverbatim */
  571. /* > */
  572. /* > \param[in] NRU */
  573. /* > \verbatim */
  574. /* > NRU is INTEGER */
  575. /* > The number of rows of the matrix U. NRU >= 0. */
  576. /* > \endverbatim */
  577. /* > */
  578. /* > \param[in] NCC */
  579. /* > \verbatim */
  580. /* > NCC is INTEGER */
  581. /* > The number of columns of the matrix C. NCC >= 0. */
  582. /* > \endverbatim */
  583. /* > */
  584. /* > \param[in,out] D */
  585. /* > \verbatim */
  586. /* > D is REAL array, dimension (N) */
  587. /* > On entry, the n diagonal elements of the bidiagonal matrix B. */
  588. /* > On exit, if INFO=0, the singular values of B in decreasing */
  589. /* > order. */
  590. /* > \endverbatim */
  591. /* > */
  592. /* > \param[in,out] E */
  593. /* > \verbatim */
  594. /* > E is REAL array, dimension (N-1) */
  595. /* > On entry, the N-1 offdiagonal elements of the bidiagonal */
  596. /* > matrix B. */
  597. /* > On exit, if INFO = 0, E is destroyed; if INFO > 0, D and E */
  598. /* > will contain the diagonal and superdiagonal elements of a */
  599. /* > bidiagonal matrix orthogonally equivalent to the one given */
  600. /* > as input. */
  601. /* > \endverbatim */
  602. /* > */
  603. /* > \param[in,out] VT */
  604. /* > \verbatim */
  605. /* > VT is COMPLEX array, dimension (LDVT, NCVT) */
  606. /* > On entry, an N-by-NCVT matrix VT. */
  607. /* > On exit, VT is overwritten by P**H * VT. */
  608. /* > Not referenced if NCVT = 0. */
  609. /* > \endverbatim */
  610. /* > */
  611. /* > \param[in] LDVT */
  612. /* > \verbatim */
  613. /* > LDVT is INTEGER */
  614. /* > The leading dimension of the array VT. */
  615. /* > LDVT >= f2cmax(1,N) if NCVT > 0; LDVT >= 1 if NCVT = 0. */
  616. /* > \endverbatim */
  617. /* > */
  618. /* > \param[in,out] U */
  619. /* > \verbatim */
  620. /* > U is COMPLEX array, dimension (LDU, N) */
  621. /* > On entry, an NRU-by-N matrix U. */
  622. /* > On exit, U is overwritten by U * Q. */
  623. /* > Not referenced if NRU = 0. */
  624. /* > \endverbatim */
  625. /* > */
  626. /* > \param[in] LDU */
  627. /* > \verbatim */
  628. /* > LDU is INTEGER */
  629. /* > The leading dimension of the array U. LDU >= f2cmax(1,NRU). */
  630. /* > \endverbatim */
  631. /* > */
  632. /* > \param[in,out] C */
  633. /* > \verbatim */
  634. /* > C is COMPLEX array, dimension (LDC, NCC) */
  635. /* > On entry, an N-by-NCC matrix C. */
  636. /* > On exit, C is overwritten by Q**H * C. */
  637. /* > Not referenced if NCC = 0. */
  638. /* > \endverbatim */
  639. /* > */
  640. /* > \param[in] LDC */
  641. /* > \verbatim */
  642. /* > LDC is INTEGER */
  643. /* > The leading dimension of the array C. */
  644. /* > LDC >= f2cmax(1,N) if NCC > 0; LDC >=1 if NCC = 0. */
  645. /* > \endverbatim */
  646. /* > */
  647. /* > \param[out] RWORK */
  648. /* > \verbatim */
  649. /* > RWORK is REAL array, dimension (4*N) */
  650. /* > \endverbatim */
  651. /* > */
  652. /* > \param[out] INFO */
  653. /* > \verbatim */
  654. /* > INFO is INTEGER */
  655. /* > = 0: successful exit */
  656. /* > < 0: If INFO = -i, the i-th argument had an illegal value */
  657. /* > > 0: the algorithm did not converge; D and E contain the */
  658. /* > elements of a bidiagonal matrix which is orthogonally */
  659. /* > similar to the input matrix B; if INFO = i, i */
  660. /* > elements of E have not converged to zero. */
  661. /* > \endverbatim */
  662. /* > \par Internal Parameters: */
  663. /* ========================= */
  664. /* > */
  665. /* > \verbatim */
  666. /* > TOLMUL REAL, default = f2cmax(10,f2cmin(100,EPS**(-1/8))) */
  667. /* > TOLMUL controls the convergence criterion of the QR loop. */
  668. /* > If it is positive, TOLMUL*EPS is the desired relative */
  669. /* > precision in the computed singular values. */
  670. /* > If it is negative, abs(TOLMUL*EPS*sigma_max) is the */
  671. /* > desired absolute accuracy in the computed singular */
  672. /* > values (corresponds to relative accuracy */
  673. /* > abs(TOLMUL*EPS) in the largest singular value. */
  674. /* > abs(TOLMUL) should be between 1 and 1/EPS, and preferably */
  675. /* > between 10 (for fast convergence) and .1/EPS */
  676. /* > (for there to be some accuracy in the results). */
  677. /* > Default is to lose at either one eighth or 2 of the */
  678. /* > available decimal digits in each computed singular value */
  679. /* > (whichever is smaller). */
  680. /* > */
  681. /* > MAXITR INTEGER, default = 6 */
  682. /* > MAXITR controls the maximum number of passes of the */
  683. /* > algorithm through its inner loop. The algorithms stops */
  684. /* > (and so fails to converge) if the number of passes */
  685. /* > through the inner loop exceeds MAXITR*N**2. */
  686. /* > \endverbatim */
  687. /* Authors: */
  688. /* ======== */
  689. /* > \author Univ. of Tennessee */
  690. /* > \author Univ. of California Berkeley */
  691. /* > \author Univ. of Colorado Denver */
  692. /* > \author NAG Ltd. */
  693. /* > \date December 2016 */
  694. /* > \ingroup complexOTHERcomputational */
  695. /* ===================================================================== */
  696. /* Subroutine */ void cbdsqr_(char *uplo, integer *n, integer *ncvt, integer *
  697. nru, integer *ncc, real *d__, real *e, complex *vt, integer *ldvt,
  698. complex *u, integer *ldu, complex *c__, integer *ldc, real *rwork,
  699. integer *info)
  700. {
  701. /* System generated locals */
  702. integer c_dim1, c_offset, u_dim1, u_offset, vt_dim1, vt_offset, i__1,
  703. i__2;
  704. real r__1, r__2, r__3, r__4;
  705. doublereal d__1;
  706. /* Local variables */
  707. real abse;
  708. integer idir;
  709. real abss;
  710. integer oldm;
  711. real cosl;
  712. integer isub, iter;
  713. real unfl, sinl, cosr, smin, smax, sinr;
  714. extern /* Subroutine */ void slas2_(real *, real *, real *, real *, real *)
  715. ;
  716. real f, g, h__;
  717. integer i__, j, m;
  718. real r__;
  719. extern logical lsame_(char *, char *);
  720. real oldcs;
  721. extern /* Subroutine */ void clasr_(char *, char *, char *, integer *,
  722. integer *, real *, real *, complex *, integer *);
  723. integer oldll;
  724. real shift, sigmn, oldsn;
  725. extern /* Subroutine */ void cswap_(integer *, complex *, integer *,
  726. complex *, integer *);
  727. integer maxit;
  728. real sminl, sigmx;
  729. logical lower;
  730. extern /* Subroutine */ void csrot_(integer *, complex *, integer *,
  731. complex *, integer *, real *, real *), slasq1_(integer *, real *,
  732. real *, real *, integer *), slasv2_(real *, real *, real *, real *
  733. , real *, real *, real *, real *, real *);
  734. real cs;
  735. integer ll;
  736. real sn, mu;
  737. extern real slamch_(char *);
  738. extern /* Subroutine */ void csscal_(integer *, real *, complex *, integer
  739. *);
  740. extern int xerbla_(char *, integer *, ftnlen);
  741. real sminoa;
  742. extern /* Subroutine */ void slartg_(real *, real *, real *, real *, real *
  743. );
  744. real thresh;
  745. logical rotate;
  746. integer nm1;
  747. real tolmul;
  748. integer nm12, nm13, lll;
  749. real eps, sll, tol;
  750. /* -- LAPACK computational routine (version 3.7.0) -- */
  751. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  752. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  753. /* December 2016 */
  754. /* ===================================================================== */
  755. /* Test the input parameters. */
  756. /* Parameter adjustments */
  757. --d__;
  758. --e;
  759. vt_dim1 = *ldvt;
  760. vt_offset = 1 + vt_dim1 * 1;
  761. vt -= vt_offset;
  762. u_dim1 = *ldu;
  763. u_offset = 1 + u_dim1 * 1;
  764. u -= u_offset;
  765. c_dim1 = *ldc;
  766. c_offset = 1 + c_dim1 * 1;
  767. c__ -= c_offset;
  768. --rwork;
  769. /* Function Body */
  770. *info = 0;
  771. lower = lsame_(uplo, "L");
  772. if (! lsame_(uplo, "U") && ! lower) {
  773. *info = -1;
  774. } else if (*n < 0) {
  775. *info = -2;
  776. } else if (*ncvt < 0) {
  777. *info = -3;
  778. } else if (*nru < 0) {
  779. *info = -4;
  780. } else if (*ncc < 0) {
  781. *info = -5;
  782. } else if (*ncvt == 0 && *ldvt < 1 || *ncvt > 0 && *ldvt < f2cmax(1,*n)) {
  783. *info = -9;
  784. } else if (*ldu < f2cmax(1,*nru)) {
  785. *info = -11;
  786. } else if (*ncc == 0 && *ldc < 1 || *ncc > 0 && *ldc < f2cmax(1,*n)) {
  787. *info = -13;
  788. }
  789. if (*info != 0) {
  790. i__1 = -(*info);
  791. xerbla_("CBDSQR", &i__1, (ftnlen)6);
  792. return;
  793. }
  794. if (*n == 0) {
  795. return;
  796. }
  797. if (*n == 1) {
  798. goto L160;
  799. }
  800. /* ROTATE is true if any singular vectors desired, false otherwise */
  801. rotate = *ncvt > 0 || *nru > 0 || *ncc > 0;
  802. /* If no singular vectors desired, use qd algorithm */
  803. if (! rotate) {
  804. slasq1_(n, &d__[1], &e[1], &rwork[1], info);
  805. /* If INFO equals 2, dqds didn't finish, try to finish */
  806. if (*info != 2) {
  807. return;
  808. }
  809. *info = 0;
  810. }
  811. nm1 = *n - 1;
  812. nm12 = nm1 + nm1;
  813. nm13 = nm12 + nm1;
  814. idir = 0;
  815. /* Get machine constants */
  816. eps = slamch_("Epsilon");
  817. unfl = slamch_("Safe minimum");
  818. /* If matrix lower bidiagonal, rotate to be upper bidiagonal */
  819. /* by applying Givens rotations on the left */
  820. if (lower) {
  821. i__1 = *n - 1;
  822. for (i__ = 1; i__ <= i__1; ++i__) {
  823. slartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
  824. d__[i__] = r__;
  825. e[i__] = sn * d__[i__ + 1];
  826. d__[i__ + 1] = cs * d__[i__ + 1];
  827. rwork[i__] = cs;
  828. rwork[nm1 + i__] = sn;
  829. /* L10: */
  830. }
  831. /* Update singular vectors if desired */
  832. if (*nru > 0) {
  833. clasr_("R", "V", "F", nru, n, &rwork[1], &rwork[*n], &u[u_offset],
  834. ldu);
  835. }
  836. if (*ncc > 0) {
  837. clasr_("L", "V", "F", n, ncc, &rwork[1], &rwork[*n], &c__[
  838. c_offset], ldc);
  839. }
  840. }
  841. /* Compute singular values to relative accuracy TOL */
  842. /* (By setting TOL to be negative, algorithm will compute */
  843. /* singular values to absolute accuracy ABS(TOL)*norm(input matrix)) */
  844. /* Computing MAX */
  845. /* Computing MIN */
  846. d__1 = (doublereal) eps;
  847. r__3 = 100.f, r__4 = pow_dd(&d__1, &c_b15);
  848. r__1 = 10.f, r__2 = f2cmin(r__3,r__4);
  849. tolmul = f2cmax(r__1,r__2);
  850. tol = tolmul * eps;
  851. /* Compute approximate maximum, minimum singular values */
  852. smax = 0.f;
  853. i__1 = *n;
  854. for (i__ = 1; i__ <= i__1; ++i__) {
  855. /* Computing MAX */
  856. r__2 = smax, r__3 = (r__1 = d__[i__], abs(r__1));
  857. smax = f2cmax(r__2,r__3);
  858. /* L20: */
  859. }
  860. i__1 = *n - 1;
  861. for (i__ = 1; i__ <= i__1; ++i__) {
  862. /* Computing MAX */
  863. r__2 = smax, r__3 = (r__1 = e[i__], abs(r__1));
  864. smax = f2cmax(r__2,r__3);
  865. /* L30: */
  866. }
  867. sminl = 0.f;
  868. if (tol >= 0.f) {
  869. /* Relative accuracy desired */
  870. sminoa = abs(d__[1]);
  871. if (sminoa == 0.f) {
  872. goto L50;
  873. }
  874. mu = sminoa;
  875. i__1 = *n;
  876. for (i__ = 2; i__ <= i__1; ++i__) {
  877. mu = (r__2 = d__[i__], abs(r__2)) * (mu / (mu + (r__1 = e[i__ - 1]
  878. , abs(r__1))));
  879. sminoa = f2cmin(sminoa,mu);
  880. if (sminoa == 0.f) {
  881. goto L50;
  882. }
  883. /* L40: */
  884. }
  885. L50:
  886. sminoa /= sqrt((real) (*n));
  887. /* Computing MAX */
  888. r__1 = tol * sminoa, r__2 = *n * 6 * *n * unfl;
  889. thresh = f2cmax(r__1,r__2);
  890. } else {
  891. /* Absolute accuracy desired */
  892. /* Computing MAX */
  893. r__1 = abs(tol) * smax, r__2 = *n * 6 * *n * unfl;
  894. thresh = f2cmax(r__1,r__2);
  895. }
  896. /* Prepare for main iteration loop for the singular values */
  897. /* (MAXIT is the maximum number of passes through the inner */
  898. /* loop permitted before nonconvergence signalled.) */
  899. maxit = *n * 6 * *n;
  900. iter = 0;
  901. oldll = -1;
  902. oldm = -1;
  903. /* M points to last element of unconverged part of matrix */
  904. m = *n;
  905. /* Begin main iteration loop */
  906. L60:
  907. /* Check for convergence or exceeding iteration count */
  908. if (m <= 1) {
  909. goto L160;
  910. }
  911. if (iter > maxit) {
  912. goto L200;
  913. }
  914. /* Find diagonal block of matrix to work on */
  915. if (tol < 0.f && (r__1 = d__[m], abs(r__1)) <= thresh) {
  916. d__[m] = 0.f;
  917. }
  918. smax = (r__1 = d__[m], abs(r__1));
  919. smin = smax;
  920. i__1 = m - 1;
  921. for (lll = 1; lll <= i__1; ++lll) {
  922. ll = m - lll;
  923. abss = (r__1 = d__[ll], abs(r__1));
  924. abse = (r__1 = e[ll], abs(r__1));
  925. if (tol < 0.f && abss <= thresh) {
  926. d__[ll] = 0.f;
  927. }
  928. if (abse <= thresh) {
  929. goto L80;
  930. }
  931. smin = f2cmin(smin,abss);
  932. /* Computing MAX */
  933. r__1 = f2cmax(smax,abss);
  934. smax = f2cmax(r__1,abse);
  935. /* L70: */
  936. }
  937. ll = 0;
  938. goto L90;
  939. L80:
  940. e[ll] = 0.f;
  941. /* Matrix splits since E(LL) = 0 */
  942. if (ll == m - 1) {
  943. /* Convergence of bottom singular value, return to top of loop */
  944. --m;
  945. goto L60;
  946. }
  947. L90:
  948. ++ll;
  949. /* E(LL) through E(M-1) are nonzero, E(LL-1) is zero */
  950. if (ll == m - 1) {
  951. /* 2 by 2 block, handle separately */
  952. slasv2_(&d__[m - 1], &e[m - 1], &d__[m], &sigmn, &sigmx, &sinr, &cosr,
  953. &sinl, &cosl);
  954. d__[m - 1] = sigmx;
  955. e[m - 1] = 0.f;
  956. d__[m] = sigmn;
  957. /* Compute singular vectors, if desired */
  958. if (*ncvt > 0) {
  959. csrot_(ncvt, &vt[m - 1 + vt_dim1], ldvt, &vt[m + vt_dim1], ldvt, &
  960. cosr, &sinr);
  961. }
  962. if (*nru > 0) {
  963. csrot_(nru, &u[(m - 1) * u_dim1 + 1], &c__1, &u[m * u_dim1 + 1], &
  964. c__1, &cosl, &sinl);
  965. }
  966. if (*ncc > 0) {
  967. csrot_(ncc, &c__[m - 1 + c_dim1], ldc, &c__[m + c_dim1], ldc, &
  968. cosl, &sinl);
  969. }
  970. m += -2;
  971. goto L60;
  972. }
  973. /* If working on new submatrix, choose shift direction */
  974. /* (from larger end diagonal element towards smaller) */
  975. if (ll > oldm || m < oldll) {
  976. if ((r__1 = d__[ll], abs(r__1)) >= (r__2 = d__[m], abs(r__2))) {
  977. /* Chase bulge from top (big end) to bottom (small end) */
  978. idir = 1;
  979. } else {
  980. /* Chase bulge from bottom (big end) to top (small end) */
  981. idir = 2;
  982. }
  983. }
  984. /* Apply convergence tests */
  985. if (idir == 1) {
  986. /* Run convergence test in forward direction */
  987. /* First apply standard test to bottom of matrix */
  988. if ((r__2 = e[m - 1], abs(r__2)) <= abs(tol) * (r__1 = d__[m], abs(
  989. r__1)) || tol < 0.f && (r__3 = e[m - 1], abs(r__3)) <= thresh)
  990. {
  991. e[m - 1] = 0.f;
  992. goto L60;
  993. }
  994. if (tol >= 0.f) {
  995. /* If relative accuracy desired, */
  996. /* apply convergence criterion forward */
  997. mu = (r__1 = d__[ll], abs(r__1));
  998. sminl = mu;
  999. i__1 = m - 1;
  1000. for (lll = ll; lll <= i__1; ++lll) {
  1001. if ((r__1 = e[lll], abs(r__1)) <= tol * mu) {
  1002. e[lll] = 0.f;
  1003. goto L60;
  1004. }
  1005. mu = (r__2 = d__[lll + 1], abs(r__2)) * (mu / (mu + (r__1 = e[
  1006. lll], abs(r__1))));
  1007. sminl = f2cmin(sminl,mu);
  1008. /* L100: */
  1009. }
  1010. }
  1011. } else {
  1012. /* Run convergence test in backward direction */
  1013. /* First apply standard test to top of matrix */
  1014. if ((r__2 = e[ll], abs(r__2)) <= abs(tol) * (r__1 = d__[ll], abs(r__1)
  1015. ) || tol < 0.f && (r__3 = e[ll], abs(r__3)) <= thresh) {
  1016. e[ll] = 0.f;
  1017. goto L60;
  1018. }
  1019. if (tol >= 0.f) {
  1020. /* If relative accuracy desired, */
  1021. /* apply convergence criterion backward */
  1022. mu = (r__1 = d__[m], abs(r__1));
  1023. sminl = mu;
  1024. i__1 = ll;
  1025. for (lll = m - 1; lll >= i__1; --lll) {
  1026. if ((r__1 = e[lll], abs(r__1)) <= tol * mu) {
  1027. e[lll] = 0.f;
  1028. goto L60;
  1029. }
  1030. mu = (r__2 = d__[lll], abs(r__2)) * (mu / (mu + (r__1 = e[lll]
  1031. , abs(r__1))));
  1032. sminl = f2cmin(sminl,mu);
  1033. /* L110: */
  1034. }
  1035. }
  1036. }
  1037. oldll = ll;
  1038. oldm = m;
  1039. /* Compute shift. First, test if shifting would ruin relative */
  1040. /* accuracy, and if so set the shift to zero. */
  1041. /* Computing MAX */
  1042. r__1 = eps, r__2 = tol * .01f;
  1043. if (tol >= 0.f && *n * tol * (sminl / smax) <= f2cmax(r__1,r__2)) {
  1044. /* Use a zero shift to avoid loss of relative accuracy */
  1045. shift = 0.f;
  1046. } else {
  1047. /* Compute the shift from 2-by-2 block at end of matrix */
  1048. if (idir == 1) {
  1049. sll = (r__1 = d__[ll], abs(r__1));
  1050. slas2_(&d__[m - 1], &e[m - 1], &d__[m], &shift, &r__);
  1051. } else {
  1052. sll = (r__1 = d__[m], abs(r__1));
  1053. slas2_(&d__[ll], &e[ll], &d__[ll + 1], &shift, &r__);
  1054. }
  1055. /* Test if shift negligible, and if so set to zero */
  1056. if (sll > 0.f) {
  1057. /* Computing 2nd power */
  1058. r__1 = shift / sll;
  1059. if (r__1 * r__1 < eps) {
  1060. shift = 0.f;
  1061. }
  1062. }
  1063. }
  1064. /* Increment iteration count */
  1065. iter = iter + m - ll;
  1066. /* If SHIFT = 0, do simplified QR iteration */
  1067. if (shift == 0.f) {
  1068. if (idir == 1) {
  1069. /* Chase bulge from top to bottom */
  1070. /* Save cosines and sines for later singular vector updates */
  1071. cs = 1.f;
  1072. oldcs = 1.f;
  1073. i__1 = m - 1;
  1074. for (i__ = ll; i__ <= i__1; ++i__) {
  1075. r__1 = d__[i__] * cs;
  1076. slartg_(&r__1, &e[i__], &cs, &sn, &r__);
  1077. if (i__ > ll) {
  1078. e[i__ - 1] = oldsn * r__;
  1079. }
  1080. r__1 = oldcs * r__;
  1081. r__2 = d__[i__ + 1] * sn;
  1082. slartg_(&r__1, &r__2, &oldcs, &oldsn, &d__[i__]);
  1083. rwork[i__ - ll + 1] = cs;
  1084. rwork[i__ - ll + 1 + nm1] = sn;
  1085. rwork[i__ - ll + 1 + nm12] = oldcs;
  1086. rwork[i__ - ll + 1 + nm13] = oldsn;
  1087. /* L120: */
  1088. }
  1089. h__ = d__[m] * cs;
  1090. d__[m] = h__ * oldcs;
  1091. e[m - 1] = h__ * oldsn;
  1092. /* Update singular vectors */
  1093. if (*ncvt > 0) {
  1094. i__1 = m - ll + 1;
  1095. clasr_("L", "V", "F", &i__1, ncvt, &rwork[1], &rwork[*n], &vt[
  1096. ll + vt_dim1], ldvt);
  1097. }
  1098. if (*nru > 0) {
  1099. i__1 = m - ll + 1;
  1100. clasr_("R", "V", "F", nru, &i__1, &rwork[nm12 + 1], &rwork[
  1101. nm13 + 1], &u[ll * u_dim1 + 1], ldu);
  1102. }
  1103. if (*ncc > 0) {
  1104. i__1 = m - ll + 1;
  1105. clasr_("L", "V", "F", &i__1, ncc, &rwork[nm12 + 1], &rwork[
  1106. nm13 + 1], &c__[ll + c_dim1], ldc);
  1107. }
  1108. /* Test convergence */
  1109. if ((r__1 = e[m - 1], abs(r__1)) <= thresh) {
  1110. e[m - 1] = 0.f;
  1111. }
  1112. } else {
  1113. /* Chase bulge from bottom to top */
  1114. /* Save cosines and sines for later singular vector updates */
  1115. cs = 1.f;
  1116. oldcs = 1.f;
  1117. i__1 = ll + 1;
  1118. for (i__ = m; i__ >= i__1; --i__) {
  1119. r__1 = d__[i__] * cs;
  1120. slartg_(&r__1, &e[i__ - 1], &cs, &sn, &r__);
  1121. if (i__ < m) {
  1122. e[i__] = oldsn * r__;
  1123. }
  1124. r__1 = oldcs * r__;
  1125. r__2 = d__[i__ - 1] * sn;
  1126. slartg_(&r__1, &r__2, &oldcs, &oldsn, &d__[i__]);
  1127. rwork[i__ - ll] = cs;
  1128. rwork[i__ - ll + nm1] = -sn;
  1129. rwork[i__ - ll + nm12] = oldcs;
  1130. rwork[i__ - ll + nm13] = -oldsn;
  1131. /* L130: */
  1132. }
  1133. h__ = d__[ll] * cs;
  1134. d__[ll] = h__ * oldcs;
  1135. e[ll] = h__ * oldsn;
  1136. /* Update singular vectors */
  1137. if (*ncvt > 0) {
  1138. i__1 = m - ll + 1;
  1139. clasr_("L", "V", "B", &i__1, ncvt, &rwork[nm12 + 1], &rwork[
  1140. nm13 + 1], &vt[ll + vt_dim1], ldvt);
  1141. }
  1142. if (*nru > 0) {
  1143. i__1 = m - ll + 1;
  1144. clasr_("R", "V", "B", nru, &i__1, &rwork[1], &rwork[*n], &u[
  1145. ll * u_dim1 + 1], ldu);
  1146. }
  1147. if (*ncc > 0) {
  1148. i__1 = m - ll + 1;
  1149. clasr_("L", "V", "B", &i__1, ncc, &rwork[1], &rwork[*n], &c__[
  1150. ll + c_dim1], ldc);
  1151. }
  1152. /* Test convergence */
  1153. if ((r__1 = e[ll], abs(r__1)) <= thresh) {
  1154. e[ll] = 0.f;
  1155. }
  1156. }
  1157. } else {
  1158. /* Use nonzero shift */
  1159. if (idir == 1) {
  1160. /* Chase bulge from top to bottom */
  1161. /* Save cosines and sines for later singular vector updates */
  1162. f = ((r__1 = d__[ll], abs(r__1)) - shift) * (r_sign(&c_b49, &d__[
  1163. ll]) + shift / d__[ll]);
  1164. g = e[ll];
  1165. i__1 = m - 1;
  1166. for (i__ = ll; i__ <= i__1; ++i__) {
  1167. slartg_(&f, &g, &cosr, &sinr, &r__);
  1168. if (i__ > ll) {
  1169. e[i__ - 1] = r__;
  1170. }
  1171. f = cosr * d__[i__] + sinr * e[i__];
  1172. e[i__] = cosr * e[i__] - sinr * d__[i__];
  1173. g = sinr * d__[i__ + 1];
  1174. d__[i__ + 1] = cosr * d__[i__ + 1];
  1175. slartg_(&f, &g, &cosl, &sinl, &r__);
  1176. d__[i__] = r__;
  1177. f = cosl * e[i__] + sinl * d__[i__ + 1];
  1178. d__[i__ + 1] = cosl * d__[i__ + 1] - sinl * e[i__];
  1179. if (i__ < m - 1) {
  1180. g = sinl * e[i__ + 1];
  1181. e[i__ + 1] = cosl * e[i__ + 1];
  1182. }
  1183. rwork[i__ - ll + 1] = cosr;
  1184. rwork[i__ - ll + 1 + nm1] = sinr;
  1185. rwork[i__ - ll + 1 + nm12] = cosl;
  1186. rwork[i__ - ll + 1 + nm13] = sinl;
  1187. /* L140: */
  1188. }
  1189. e[m - 1] = f;
  1190. /* Update singular vectors */
  1191. if (*ncvt > 0) {
  1192. i__1 = m - ll + 1;
  1193. clasr_("L", "V", "F", &i__1, ncvt, &rwork[1], &rwork[*n], &vt[
  1194. ll + vt_dim1], ldvt);
  1195. }
  1196. if (*nru > 0) {
  1197. i__1 = m - ll + 1;
  1198. clasr_("R", "V", "F", nru, &i__1, &rwork[nm12 + 1], &rwork[
  1199. nm13 + 1], &u[ll * u_dim1 + 1], ldu);
  1200. }
  1201. if (*ncc > 0) {
  1202. i__1 = m - ll + 1;
  1203. clasr_("L", "V", "F", &i__1, ncc, &rwork[nm12 + 1], &rwork[
  1204. nm13 + 1], &c__[ll + c_dim1], ldc);
  1205. }
  1206. /* Test convergence */
  1207. if ((r__1 = e[m - 1], abs(r__1)) <= thresh) {
  1208. e[m - 1] = 0.f;
  1209. }
  1210. } else {
  1211. /* Chase bulge from bottom to top */
  1212. /* Save cosines and sines for later singular vector updates */
  1213. f = ((r__1 = d__[m], abs(r__1)) - shift) * (r_sign(&c_b49, &d__[m]
  1214. ) + shift / d__[m]);
  1215. g = e[m - 1];
  1216. i__1 = ll + 1;
  1217. for (i__ = m; i__ >= i__1; --i__) {
  1218. slartg_(&f, &g, &cosr, &sinr, &r__);
  1219. if (i__ < m) {
  1220. e[i__] = r__;
  1221. }
  1222. f = cosr * d__[i__] + sinr * e[i__ - 1];
  1223. e[i__ - 1] = cosr * e[i__ - 1] - sinr * d__[i__];
  1224. g = sinr * d__[i__ - 1];
  1225. d__[i__ - 1] = cosr * d__[i__ - 1];
  1226. slartg_(&f, &g, &cosl, &sinl, &r__);
  1227. d__[i__] = r__;
  1228. f = cosl * e[i__ - 1] + sinl * d__[i__ - 1];
  1229. d__[i__ - 1] = cosl * d__[i__ - 1] - sinl * e[i__ - 1];
  1230. if (i__ > ll + 1) {
  1231. g = sinl * e[i__ - 2];
  1232. e[i__ - 2] = cosl * e[i__ - 2];
  1233. }
  1234. rwork[i__ - ll] = cosr;
  1235. rwork[i__ - ll + nm1] = -sinr;
  1236. rwork[i__ - ll + nm12] = cosl;
  1237. rwork[i__ - ll + nm13] = -sinl;
  1238. /* L150: */
  1239. }
  1240. e[ll] = f;
  1241. /* Test convergence */
  1242. if ((r__1 = e[ll], abs(r__1)) <= thresh) {
  1243. e[ll] = 0.f;
  1244. }
  1245. /* Update singular vectors if desired */
  1246. if (*ncvt > 0) {
  1247. i__1 = m - ll + 1;
  1248. clasr_("L", "V", "B", &i__1, ncvt, &rwork[nm12 + 1], &rwork[
  1249. nm13 + 1], &vt[ll + vt_dim1], ldvt);
  1250. }
  1251. if (*nru > 0) {
  1252. i__1 = m - ll + 1;
  1253. clasr_("R", "V", "B", nru, &i__1, &rwork[1], &rwork[*n], &u[
  1254. ll * u_dim1 + 1], ldu);
  1255. }
  1256. if (*ncc > 0) {
  1257. i__1 = m - ll + 1;
  1258. clasr_("L", "V", "B", &i__1, ncc, &rwork[1], &rwork[*n], &c__[
  1259. ll + c_dim1], ldc);
  1260. }
  1261. }
  1262. }
  1263. /* QR iteration finished, go back and check convergence */
  1264. goto L60;
  1265. /* All singular values converged, so make them positive */
  1266. L160:
  1267. i__1 = *n;
  1268. for (i__ = 1; i__ <= i__1; ++i__) {
  1269. if (d__[i__] < 0.f) {
  1270. d__[i__] = -d__[i__];
  1271. /* Change sign of singular vectors, if desired */
  1272. if (*ncvt > 0) {
  1273. csscal_(ncvt, &c_b72, &vt[i__ + vt_dim1], ldvt);
  1274. }
  1275. }
  1276. /* L170: */
  1277. }
  1278. /* Sort the singular values into decreasing order (insertion sort on */
  1279. /* singular values, but only one transposition per singular vector) */
  1280. i__1 = *n - 1;
  1281. for (i__ = 1; i__ <= i__1; ++i__) {
  1282. /* Scan for smallest D(I) */
  1283. isub = 1;
  1284. smin = d__[1];
  1285. i__2 = *n + 1 - i__;
  1286. for (j = 2; j <= i__2; ++j) {
  1287. if (d__[j] <= smin) {
  1288. isub = j;
  1289. smin = d__[j];
  1290. }
  1291. /* L180: */
  1292. }
  1293. if (isub != *n + 1 - i__) {
  1294. /* Swap singular values and vectors */
  1295. d__[isub] = d__[*n + 1 - i__];
  1296. d__[*n + 1 - i__] = smin;
  1297. if (*ncvt > 0) {
  1298. cswap_(ncvt, &vt[isub + vt_dim1], ldvt, &vt[*n + 1 - i__ +
  1299. vt_dim1], ldvt);
  1300. }
  1301. if (*nru > 0) {
  1302. cswap_(nru, &u[isub * u_dim1 + 1], &c__1, &u[(*n + 1 - i__) *
  1303. u_dim1 + 1], &c__1);
  1304. }
  1305. if (*ncc > 0) {
  1306. cswap_(ncc, &c__[isub + c_dim1], ldc, &c__[*n + 1 - i__ +
  1307. c_dim1], ldc);
  1308. }
  1309. }
  1310. /* L190: */
  1311. }
  1312. goto L220;
  1313. /* Maximum number of iterations exceeded, failure to converge */
  1314. L200:
  1315. *info = 0;
  1316. i__1 = *n - 1;
  1317. for (i__ = 1; i__ <= i__1; ++i__) {
  1318. if (e[i__] != 0.f) {
  1319. ++(*info);
  1320. }
  1321. /* L210: */
  1322. }
  1323. L220:
  1324. return;
  1325. /* End of CBDSQR */
  1326. } /* cbdsqr_ */