num_rec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. //Useful mathematical functions from numerical recipes library
  2. //Square function for floats and doubles
  3. #ifndef SQR_H
  4. #define SQR_H
  5. #ifdef __GNUC__
  6. #define SQR_ATTR __attribute__((__const__))
  7. #else
  8. #define SQR_ATTR
  9. #endif
  10. SQR_ATTR double sqr(double a)
  11. {
  12. return a*a;
  13. }
  14. SQR_ATTR float fsqr(float a)
  15. {
  16. return a*a;
  17. }
  18. #undef SQR_ATTR
  19. #endif
  20. #ifndef _NR_UTILS_H_
  21. #define _NR_UTILS_H_
  22. static float sqrarg;
  23. #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)
  24. static double dsqrarg;
  25. #define DSQR(a) ((dsqrarg=(a)) == 0.0 ? 0.0 : dsqrarg*dsqrarg)
  26. static double dmaxarg1,dmaxarg2;
  27. #define DMAX(a,b) (dmaxarg1=(a),dmaxarg2=(b),(dmaxarg1) > (dmaxarg2) ?\
  28. (dmaxarg1) : (dmaxarg2))
  29. static double dminarg1,dminarg2;
  30. #define DMIN(a,b) (dminarg1=(a),dminarg2=(b),(dminarg1) < (dminarg2) ?\
  31. (dminarg1) : (dminarg2))
  32. static float maxarg1,maxarg2;
  33. #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ?\
  34. (maxarg1) : (maxarg2))
  35. static float minarg1,minarg2;
  36. #define FMIN(a,b) (minarg1=(a),minarg2=(b),(minarg1) < (minarg2) ?\
  37. (minarg1) : (minarg2))
  38. static long lmaxarg1,lmaxarg2;
  39. #define LMAX(a,b) (lmaxarg1=(a),lmaxarg2=(b),(lmaxarg1) > (lmaxarg2) ?\
  40. (lmaxarg1) : (lmaxarg2))
  41. static long lminarg1,lminarg2;
  42. #define LMIN(a,b) (lminarg1=(a),lminarg2=(b),(lminarg1) < (lminarg2) ?\
  43. (lminarg1) : (lminarg2))
  44. static int imaxarg1,imaxarg2;
  45. #define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ?\
  46. (imaxarg1) : (imaxarg2))
  47. static int iminarg1,iminarg2;
  48. #define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ?\
  49. (iminarg1) : (iminarg2))
  50. #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
  51. void nrerror(char error_text[]);
  52. float *vector(long nl, long nh);
  53. int *ivector(long nl, long nh);
  54. unsigned char *cvector(long nl, long nh);
  55. unsigned long *lvector(long nl, long nh);
  56. double *dvector(long nl, long nh);
  57. float **matrix(long nrl, long nrh, long ncl, long nch);
  58. double **dmatrix(long nrl, long nrh, long ncl, long nch);
  59. int **imatrix(long nrl, long nrh, long ncl, long nch);
  60. float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
  61. long newrl, long newcl);
  62. float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch);
  63. float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
  64. void free_vector(float *v, long nl, long nh);
  65. void free_ivector(int *v, long nl, long nh);
  66. void free_cvector(unsigned char *v, long nl, long nh);
  67. void free_lvector(unsigned long *v, long nl, long nh);
  68. void free_dvector(double *v, long nl, long nh);
  69. void free_matrix(float **m, long nrl, long nrh, long ncl, long nch);
  70. void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch);
  71. void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch);
  72. void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch);
  73. void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch);
  74. void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
  75. long ndl, long ndh);
  76. #endif /* _NR_UTILS_H_ */
  77. #include <stdio.h>
  78. #include <stddef.h>
  79. #include <stdlib.h>
  80. #define NR_END 1
  81. #define FREE_ARG char*
  82. void nrerror(char error_text[])
  83. /* Numerical Recipes standard error handler */
  84. {
  85. fprintf(stderr,"Numerical Recipes run-time error...\n");
  86. fprintf(stderr,"%s\n",error_text);
  87. fprintf(stderr,"...now exiting to system...\n");
  88. exit(1);
  89. }
  90. float *vector(long nl, long nh)
  91. /* allocate a float vector with subscript range v[nl..nh] */
  92. {
  93. float *v;
  94. v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
  95. if (!v) nrerror("allocation failure in vector()");
  96. return v-nl+NR_END;
  97. }
  98. int *ivector(long nl, long nh)
  99. /* allocate an int vector with subscript range v[nl..nh] */
  100. {
  101. int *v;
  102. v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
  103. if (!v) nrerror("allocation failure in ivector()");
  104. return v-nl+NR_END;
  105. }
  106. unsigned char *cvector(long nl, long nh)
  107. /* allocate an unsigned char vector with subscript range v[nl..nh] */
  108. {
  109. unsigned char *v;
  110. v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
  111. if (!v) nrerror("allocation failure in cvector()");
  112. return v-nl+NR_END;
  113. }
  114. unsigned long *lvector(long nl, long nh)
  115. /* allocate an unsigned long vector with subscript range v[nl..nh] */
  116. {
  117. unsigned long *v;
  118. v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
  119. if (!v) nrerror("allocation failure in lvector()");
  120. return v-nl+NR_END;
  121. }
  122. double *dvector(long nl, long nh)
  123. /* allocate a double vector with subscript range v[nl..nh] */
  124. {
  125. double *v;
  126. v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
  127. if (!v) nrerror("allocation failure in dvector()");
  128. return v-nl+NR_END;
  129. }
  130. float **matrix(long nrl, long nrh, long ncl, long nch)
  131. /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
  132. {
  133. long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  134. float **m;
  135. /* allocate pointers to rows */
  136. m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
  137. if (!m) nrerror("allocation failure 1 in matrix()");
  138. m += NR_END;
  139. m -= nrl;
  140. /* allocate rows and set pointers to them */
  141. m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
  142. if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  143. m[nrl] += NR_END;
  144. m[nrl] -= ncl;
  145. for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  146. /* return pointer to array of pointers to rows */
  147. return m;
  148. }
  149. double **dmatrix(long nrl, long nrh, long ncl, long nch)
  150. /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
  151. {
  152. long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  153. double **m;
  154. /* allocate pointers to rows */
  155. m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
  156. if (!m) nrerror("allocation failure 1 in matrix()");
  157. m += NR_END;
  158. m -= nrl;
  159. /* allocate rows and set pointers to them */
  160. m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
  161. if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  162. m[nrl] += NR_END;
  163. m[nrl] -= ncl;
  164. for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  165. /* return pointer to array of pointers to rows */
  166. return m;
  167. }
  168. int **imatrix(long nrl, long nrh, long ncl, long nch)
  169. /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
  170. {
  171. long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  172. int **m;
  173. /* allocate pointers to rows */
  174. m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
  175. if (!m) nrerror("allocation failure 1 in matrix()");
  176. m += NR_END;
  177. m -= nrl;
  178. /* allocate rows and set pointers to them */
  179. m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
  180. if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  181. m[nrl] += NR_END;
  182. m[nrl] -= ncl;
  183. for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  184. /* return pointer to array of pointers to rows */
  185. return m;
  186. }
  187. float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
  188. long newrl, long newcl)
  189. /* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
  190. {
  191. long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
  192. float **m;
  193. /* allocate array of pointers to rows */
  194. m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
  195. if (!m) nrerror("allocation failure in submatrix()");
  196. m += NR_END;
  197. m -= newrl;
  198. /* set pointers to rows */
  199. for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
  200. /* return pointer to array of pointers to rows */
  201. return m;
  202. }
  203. float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
  204. /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
  205. declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
  206. and ncol=nch-ncl+1. The routine should be called with the address
  207. &a[0][0] as the first argument. */
  208. {
  209. long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
  210. float **m;
  211. /* allocate pointers to rows */
  212. m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
  213. if (!m) nrerror("allocation failure in convert_matrix()");
  214. m += NR_END;
  215. m -= nrl;
  216. /* set pointers to rows */
  217. m[nrl]=a-ncl;
  218. for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
  219. /* return pointer to array of pointers to rows */
  220. return m;
  221. }
  222. float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
  223. /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
  224. {
  225. long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
  226. float ***t;
  227. /* allocate pointers to pointers to rows */
  228. t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
  229. if (!t) nrerror("allocation failure 1 in f3tensor()");
  230. t += NR_END;
  231. t -= nrl;
  232. /* allocate pointers to rows and set pointers to them */
  233. t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
  234. if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
  235. t[nrl] += NR_END;
  236. t[nrl] -= ncl;
  237. /* allocate rows and set pointers to them */
  238. t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
  239. if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
  240. t[nrl][ncl] += NR_END;
  241. t[nrl][ncl] -= ndl;
  242. for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
  243. for(i=nrl+1;i<=nrh;i++) {
  244. t[i]=t[i-1]+ncol;
  245. t[i][ncl]=t[i-1][ncl]+ncol*ndep;
  246. for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
  247. }
  248. /* return pointer to array of pointers to rows */
  249. return t;
  250. }
  251. void free_vector(float *v, long nl, long nh)
  252. /* free a float vector allocated with vector() */
  253. {
  254. free((FREE_ARG) (v+nl-NR_END));
  255. }
  256. void free_ivector(int *v, long nl, long nh)
  257. /* free an int vector allocated with ivector() */
  258. {
  259. free((FREE_ARG) (v+nl-NR_END));
  260. }
  261. void free_cvector(unsigned char *v, long nl, long nh)
  262. /* free an unsigned char vector allocated with cvector() */
  263. {
  264. free((FREE_ARG) (v+nl-NR_END));
  265. }
  266. void free_lvector(unsigned long *v, long nl, long nh)
  267. /* free an unsigned long vector allocated with lvector() */
  268. {
  269. free((FREE_ARG) (v+nl-NR_END));
  270. }
  271. void free_dvector(double *v, long nl, long nh)
  272. /* free a double vector allocated with dvector() */
  273. {
  274. free((FREE_ARG) (v+nl-NR_END));
  275. }
  276. void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
  277. /* free a float matrix allocated by matrix() */
  278. {
  279. free((FREE_ARG) (m[nrl]+ncl-NR_END));
  280. free((FREE_ARG) (m+nrl-NR_END));
  281. }
  282. void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
  283. /* free a double matrix allocated by dmatrix() */
  284. {
  285. free((FREE_ARG) (m[nrl]+ncl-NR_END));
  286. free((FREE_ARG) (m+nrl-NR_END));
  287. }
  288. void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
  289. /* free an int matrix allocated by imatrix() */
  290. {
  291. free((FREE_ARG) (m[nrl]+ncl-NR_END));
  292. free((FREE_ARG) (m+nrl-NR_END));
  293. }
  294. void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
  295. /* free a submatrix allocated by submatrix() */
  296. {
  297. free((FREE_ARG) (b+nrl-NR_END));
  298. }
  299. void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
  300. /* free a matrix allocated by convert_matrix() */
  301. {
  302. free((FREE_ARG) (b+nrl-NR_END));
  303. }
  304. void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
  305. long ndl, long ndh)
  306. /* free a float f3tensor allocated by f3tensor() */
  307. {
  308. free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
  309. free((FREE_ARG) (t[nrl]+ncl-NR_END));
  310. free((FREE_ARG) (t+nrl-NR_END));
  311. }
  312. #include <math.h>
  313. #define NRANSI
  314. float pythag(float a, float b)
  315. {
  316. float absa,absb;
  317. absa=fabs(a);
  318. absb=fabs(b);
  319. if (absa > absb) return absa*sqrt(1.0+SQR(absb/absa));
  320. else return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+SQR(absa/absb)));
  321. }
  322. #undef NRANSI
  323. /* note #undef's at end of file */
  324. #define IM1 2147483563
  325. #define IM2 2147483399
  326. #define AM (1.0/IM1)
  327. #define IMM1 (IM1-1)
  328. #define IA1 40014
  329. #define IA2 40692
  330. #define IQ1 53668
  331. #define IQ2 52774
  332. #define IR1 12211
  333. #define IR2 3791
  334. #define NTAB 32
  335. #define NDIV (1+IMM1/NTAB)
  336. #define EPS 1.2e-7
  337. #define RNMX (1.0-EPS)
  338. float ran2(long *idum)
  339. {
  340. int j;
  341. long k;
  342. static long idum2=123456789;
  343. static long iy=0;
  344. static long iv[NTAB];
  345. float temp;
  346. if (*idum <= 0) {
  347. if (-(*idum) < 1) *idum=1;
  348. else *idum = -(*idum);
  349. idum2=(*idum);
  350. for (j=NTAB+7;j>=0;j--) {
  351. k=(*idum)/IQ1;
  352. *idum=IA1*(*idum-k*IQ1)-k*IR1;
  353. if (*idum < 0) *idum += IM1;
  354. if (j < NTAB) iv[j] = *idum;
  355. }
  356. iy=iv[0];
  357. }
  358. k=(*idum)/IQ1;
  359. *idum=IA1*(*idum-k*IQ1)-k*IR1;
  360. if (*idum < 0) *idum += IM1;
  361. k=idum2/IQ2;
  362. idum2=IA2*(idum2-k*IQ2)-k*IR2;
  363. if (idum2 < 0) idum2 += IM2;
  364. j=iy/NDIV;
  365. iy=iv[j]-idum2;
  366. iv[j] = *idum;
  367. if (iy < 1) iy += IMM1;
  368. if ((temp=AM*iy) > RNMX) return RNMX;
  369. else return temp;
  370. }
  371. #undef IM1
  372. #undef IM2
  373. #undef AM
  374. #undef IMM1
  375. #undef IA1
  376. #undef IA2
  377. #undef IQ1
  378. #undef IQ2
  379. #undef IR1
  380. #undef IR2
  381. #undef NTAB
  382. #undef NDIV
  383. #undef EPS
  384. #undef RNMX
  385. #include <math.h>
  386. #define NRANSI
  387. int tqli(float d[], float e[], int n, float **z)
  388. {
  389. float pythag(float a, float b);
  390. int m,l,iter,i,k;
  391. float s,r,p,g,f,dd,c,b;
  392. for (i=2;i<=n;i++) e[i-1]=e[i];
  393. e[n]=0.0;
  394. for (l=1;l<=n;l++) {
  395. iter=0;
  396. do {
  397. for (m=l;m<=n-1;m++) {
  398. dd=fabs(d[m])+fabs(d[m+1]);
  399. if ((float)(fabs(e[m])+dd) == dd) break;
  400. }
  401. if (m != l) {
  402. if (iter++ == 30){
  403. nrerror("Too many iterations in tqli");
  404. return 1;
  405. }
  406. g=(d[l+1]-d[l])/(2.0*e[l]);
  407. r=pythag(g,1.0);
  408. g=d[m]-d[l]+e[l]/(g+SIGN(r,g));
  409. s=c=1.0;
  410. p=0.0;
  411. for (i=m-1;i>=l;i--) {
  412. f=s*e[i];
  413. b=c*e[i];
  414. e[i+1]=(r=pythag(f,g));
  415. if (r == 0.0) {
  416. d[i+1] -= p;
  417. e[m]=0.0;
  418. break;
  419. }
  420. s=f/r;
  421. c=g/r;
  422. g=d[i+1]-p;
  423. r=(d[i]-g)*s+2.0*c*b;
  424. d[i+1]=g+(p=s*r);
  425. g=c*r-b;
  426. for (k=1;k<=n;k++) {
  427. f=z[k][i+1];
  428. z[k][i+1]=s*z[k][i]+c*f;
  429. z[k][i]=c*z[k][i]-s*f;
  430. }
  431. }
  432. if (r == 0.0 && i >= l) continue;
  433. d[l] -= p;
  434. e[l]=g;
  435. e[m]=0.0;
  436. }
  437. } while (m != l);
  438. }
  439. return 0;
  440. }
  441. #undef NRANSI
  442. #include <math.h>
  443. void tred2(float **a, int n, float d[], float e[])
  444. {
  445. int l,k,j,i;
  446. float scale,hh,h,g,f;
  447. for (i=n;i>=2;i--) {
  448. l=i-1;
  449. h=scale=0.0;
  450. if (l > 1) {
  451. for (k=1;k<=l;k++)
  452. scale += fabs(a[i][k]);
  453. if (scale == 0.0)
  454. e[i]=a[i][l];
  455. else {
  456. for (k=1;k<=l;k++) {
  457. a[i][k] /= scale;
  458. h += a[i][k]*a[i][k];
  459. }
  460. f=a[i][l];
  461. g=(f >= 0.0 ? -sqrt(h) : sqrt(h));
  462. e[i]=scale*g;
  463. h -= f*g;
  464. a[i][l]=f-g;
  465. f=0.0;
  466. for (j=1;j<=l;j++) {
  467. a[j][i]=a[i][j]/h;
  468. g=0.0;
  469. for (k=1;k<=j;k++)
  470. g += a[j][k]*a[i][k];
  471. for (k=j+1;k<=l;k++)
  472. g += a[k][j]*a[i][k];
  473. e[j]=g/h;
  474. f += e[j]*a[i][j];
  475. }
  476. hh=f/(h+h);
  477. for (j=1;j<=l;j++) {
  478. f=a[i][j];
  479. e[j]=g=e[j]-hh*f;
  480. for (k=1;k<=j;k++)
  481. a[j][k] -= (f*e[k]+g*a[i][k]);
  482. }
  483. }
  484. } else
  485. e[i]=a[i][l];
  486. d[i]=h;
  487. }
  488. d[1]=0.0;
  489. e[1]=0.0;
  490. /* Contents of this loop can be omitted if eigenvectors not
  491. wanted except for statement d[i]=a[i][i]; */
  492. for (i=1;i<=n;i++) {
  493. l=i-1;
  494. if (d[i]) {
  495. for (j=1;j<=l;j++) {
  496. g=0.0;
  497. for (k=1;k<=l;k++)
  498. g += a[i][k]*a[k][j];
  499. for (k=1;k<=l;k++)
  500. a[k][j] -= g*a[k][i];
  501. }
  502. }
  503. d[i]=a[i][i];
  504. a[i][i]=1.0;
  505. for (j=1;j<=l;j++) a[j][i]=a[i][j]=0.0;
  506. }
  507. }