static gnm_float lgammacor(gnm_float x) { static const gnm_float algmcs[15] = { GNM_const(+.1666389480451863247205729650822e+0), GNM_const(-.1384948176067563840732986059135e-4), GNM_const(+.9810825646924729426157171547487e-8), GNM_const(-.1809129475572494194263306266719e-10), GNM_const(+.6221098041892605227126015543416e-13), GNM_const(-.3399615005417721944303330599666e-15), GNM_const(+.2683181998482698748957538846666e-17), GNM_const(-.2868042435334643284144622399999e-19), GNM_const(+.3962837061046434803679306666666e-21), GNM_const(-.6831888753985766870111999999999e-23), GNM_const(+.1429227355942498147573333333333e-24), GNM_const(-.3547598158101070547199999999999e-26), GNM_const(+.1025680058010470912000000000000e-27), GNM_const(-.3401102254316748799999999999999e-29), GNM_const(+.1276642195630062933333333333333e-30) }; gnm_float tmp; #ifdef NOMORE_FOR_THREADS static int nalgm = 0; static gnm_float xbig = 0, xmax = 0; /* Initialize machine dependent constants, the first time gamma() is called. FIXME for threads ! */ if (nalgm == 0) { /* For IEEE gnm_float precision : nalgm = 5 */ nalgm = chebyshev_init(algmcs, 15, GNM_EPSILON/2);/*was d1mach(3)*/ xbig = 1 / gnm_sqrt(GNM_EPSILON/2); /* ~ 94906265.6 for IEEE gnm_float */ xmax = gnm_exp(fmin2(gnm_log(GNM_MAX / 12), -gnm_log(12 * GNM_MIN))); /* = GNM_MAX / 48 ~= 3.745e306 for IEEE gnm_float */ } #else /* For IEEE gnm_float precision GNM_EPSILON = 2^-52 = GNM_const(2.220446049250313e-16) : * xbig = 2 ^ 26.5 * xmax = GNM_MAX / 48 = 2^1020 / 3 */ # define nalgm 5 # define xbig GNM_const(94906265.62425156) # define xmax GNM_const(3.745194030963158e306) #endif if (x < 10) ML_ERR_return_NAN else if (x >= xmax) { ML_ERROR(ME_UNDERFLOW); return ML_UNDERFLOW; } else if (x < xbig) { tmp = 10 / x; return chebyshev_eval(tmp * tmp * 2 - 1, algmcs, nalgm) / x; } else return 1 / (x * 12); }
void bessel_cheb( double x, double *gam1, double *gam2, double *gampl, double *gammi ) { double xx; static double c1[] = { -1.142022680371168e0, 6.5165112670737e-3, 3.087090173086e-4, -3.4706269649e-6, 6.9437664e-9, 3.67765e-11, -1.356e-13 }; static double c2[] = { 1.843740587300905e0, -7.86528408447867e-2, 1.2719271366546e-3, -4.9717367042e-6, -3.31261198e-8, 2.423096e-10, -1.702e-13, -1.49e-15 }; xx = 8.0*x*x - 1.0; *gam1 = chebyshev_eval( -1.0, +1.0, c1, 7, xx ); *gam2 = chebyshev_eval( -1.0, +1.0, c2, 8, xx ); *gampl = *gam2 - x*(*gam1); *gammi = *gam2 + x*(*gam1); }
double attribute_hidden lgammacor(double x) { const static double algmcs[15] = { +.1666389480451863247205729650822e+0, -.1384948176067563840732986059135e-4, +.9810825646924729426157171547487e-8, -.1809129475572494194263306266719e-10, +.6221098041892605227126015543416e-13, -.3399615005417721944303330599666e-15, +.2683181998482698748957538846666e-17, -.2868042435334643284144622399999e-19, +.3962837061046434803679306666666e-21, -.6831888753985766870111999999999e-23, +.1429227355942498147573333333333e-24, -.3547598158101070547199999999999e-26, +.1025680058010470912000000000000e-27, -.3401102254316748799999999999999e-29, +.1276642195630062933333333333333e-30 }; double tmp; /* For IEEE double precision DBL_EPSILON = 2^-52 = 2.220446049250313e-16 : * xbig = 2 ^ 26.5 * xmax = DBL_MAX / 48 = 2^1020 / 3 */ #define nalgm 5 #define xbig 94906265.62425156 #define xmax 3.745194030963158e306 if (x < 10) ML_ERR_return_NAN else if (x >= xmax) { ML_ERROR(ME_UNDERFLOW, "lgammacor"); /* allow to underflow below */ } else if (x < xbig) { tmp = 10 / x; return chebyshev_eval(tmp * tmp * 2 - 1, algmcs, nalgm) / x; } return 1 / (x * 12); }
double gammafn(double x) { const static double gamcs[42] = { +.8571195590989331421920062399942e-2, +.4415381324841006757191315771652e-2, +.5685043681599363378632664588789e-1, -.4219835396418560501012500186624e-2, +.1326808181212460220584006796352e-2, -.1893024529798880432523947023886e-3, +.3606925327441245256578082217225e-4, -.6056761904460864218485548290365e-5, +.1055829546302283344731823509093e-5, -.1811967365542384048291855891166e-6, +.3117724964715322277790254593169e-7, -.5354219639019687140874081024347e-8, +.9193275519859588946887786825940e-9, -.1577941280288339761767423273953e-9, +.2707980622934954543266540433089e-10, -.4646818653825730144081661058933e-11, +.7973350192007419656460767175359e-12, -.1368078209830916025799499172309e-12, +.2347319486563800657233471771688e-13, -.4027432614949066932766570534699e-14, +.6910051747372100912138336975257e-15, -.1185584500221992907052387126192e-15, +.2034148542496373955201026051932e-16, -.3490054341717405849274012949108e-17, +.5987993856485305567135051066026e-18, -.1027378057872228074490069778431e-18, +.1762702816060529824942759660748e-19, -.3024320653735306260958772112042e-20, +.5188914660218397839717833550506e-21, -.8902770842456576692449251601066e-22, +.1527474068493342602274596891306e-22, -.2620731256187362900257328332799e-23, +.4496464047830538670331046570666e-24, -.7714712731336877911703901525333e-25, +.1323635453126044036486572714666e-25, -.2270999412942928816702313813333e-26, +.3896418998003991449320816639999e-27, -.6685198115125953327792127999999e-28, +.1146998663140024384347613866666e-28, -.1967938586345134677295103999999e-29, +.3376448816585338090334890666666e-30, -.5793070335782135784625493333333e-31 }; int i, n; double y; double sinpiy, value; #ifdef NOMORE_FOR_THREADS static int ngam = 0; static double xmin = 0, xmax = 0., xsml = 0., dxrel = 0.; /* Initialize machine dependent constants, the first time gamma() is called. FIXME for threads ! */ if (ngam == 0) { ngam = chebyshev_init(gamcs, 42, DBL_EPSILON/20);/*was .1*d1mach(3)*/ gammalims(&xmin, &xmax);/*-> ./gammalims.c */ xsml = exp(fmax2(log(DBL_MIN), -log(DBL_MAX)) + 0.01); /* = exp(.01)*DBL_MIN = 2.247e-308 for IEEE */ dxrel = sqrt(DBL_EPSILON);/*was sqrt(d1mach(4)) */ } #else /* For IEEE double precision DBL_EPSILON = 2^-52 = 2.220446049250313e-16 : * (xmin, xmax) are non-trivial, see ./gammalims.c * xsml = exp(.01)*DBL_MIN * dxrel = sqrt(DBL_EPSILON) = 2 ^ -26 */ # define ngam 22 # define xmin -170.5674972726612 # define xmax 171.61447887182298 # define xsml 2.2474362225598545e-308 # define dxrel 1.490116119384765696e-8 #endif if(ISNAN(x)) return x; /* If the argument is exactly zero or a negative integer * then return NaN. */ if (x == 0 || (x < 0 && x == (long)x)) { ML_ERROR(ME_DOMAIN, "gammafn"); return ML_NAN; } y = fabs(x); if (y <= 10) { /* Compute gamma(x) for -10 <= x <= 10 * Reduce the interval and find gamma(1 + y) for 0 <= y < 1 * first of all. */ n = (int) x; if(x < 0) --n; y = x - n;/* n = floor(x) ==> y in [ 0, 1 ) */ --n; value = chebyshev_eval(y * 2 - 1, gamcs, ngam) + .9375; if (n == 0) return value;/* x = 1.dddd = 1+y */ if (n < 0) { /* compute gamma(x) for -10 <= x < 1 */ /* exact 0 or "-n" checked already above */ /* The answer is less than half precision */ /* because x too near a negative integer. */ if (x < -0.5 && fabs(x - (int)(x - 0.5) / x) < dxrel) { ML_ERROR(ME_PRECISION, "gammafn"); } /* The argument is so close to 0 that the result would overflow. */ if (y < xsml) { ML_ERROR(ME_RANGE, "gammafn"); if(x > 0) return ML_POSINF; else return ML_NEGINF; } n = -n; for (i = 0; i < n; i++) { value /= (x + i); } return value; } else { /* gamma(x) for 2 <= x <= 10 */ for (i = 1; i <= n; i++) { value *= (y + i); } return value; } } else { /* gamma(x) for y = |x| > 10. */ if (x > xmax) { /* Overflow */ ML_ERROR(ME_RANGE, "gammafn"); return ML_POSINF; } if (x < xmin) { /* Underflow */ ML_ERROR(ME_UNDERFLOW, "gammafn"); return 0.; } if(y <= 50 && y == (int)y) { /* compute (n - 1)! */ value = 1.; for (i = 2; i < y; i++) value *= i; } else { /* normal case */ value = exp((y - 0.5) * log(y) - y + M_LN_SQRT_2PI + ((2*y == (int)2*y)? stirlerr(y) : lgammacor(y))); } if (x > 0) return value; if (fabs((x - (int)(x - 0.5))/x) < dxrel){ /* The answer is less than half precision because */ /* the argument is too near a negative integer. */ ML_ERROR(ME_PRECISION, "gammafn"); } sinpiy = sin(M_PI * y); if (sinpiy == 0) { /* Negative integer arg - overflow */ ML_ERROR(ME_RANGE, "gammafn"); return ML_POSINF; } return -M_PI / (y * sinpiy * value); } }
void chebyshev_fit_sat(float *X, float *Y, int size, float nsigma, float *C, int order, int *RFI, float RA) { /* satellite RFI fitting function: fit + outlier removal */ int i, M, outlier, outliercount = 0; float mean, sigma; float *x = (float*)malloc(size * sizeof(float)); float *y = (float*)malloc(size * sizeof(float)); float *p = (float*)malloc(size * sizeof(float)); float *diff = (float*)malloc(size * sizeof(float)); for(i=0; i<size; i++){x[i] = X[i]; y[i] = Y[i];} do { //chebyshev_cholesky(order, C, size, x, y); chebyshev_qrgsm(order, C, size, x, y); mean = 0.0; for(i=0; i<size; i++) { p[i] = chebyshev_eval(x[i], C, order); diff[i] = y[i] - p[i]; mean += diff[i]; } mean = mean/size; sigma = 0.0; for(i=0; i<size; i++) { diff[i] = diff[i] - mean; sigma += diff[i]*diff[i]; } sigma = nsigma*sqrt(sigma/size); //printf("size = %d ", size ); /*if ( fabs(RA - 71.390589) < 0.001 ) { char filename[50]; sprintf(filename, "fit_%f.dat", RA); FILE *fit = fopen( filename , "w" ); for(i=0; i<size; i++) { fprintf(fit, "%f", diff[i] ); fprintf(fit, "\n"); } fclose( fit ); } */ outlier = 0; for(i=0; i<size; i++) { if(fabs(diff[i]) > sigma) { outlier = 1; outliercount++; RFI[i] = 1.0; size --; x[i] = x[size]; y[i] = y[size]; } } }while(outlier && size > order); free(x); free(y); free(diff); free(p); //printf("Chevy outlier = %d\n", outliercount ); }
void chebyshev_fit_dec(float *X, float *Y, int size, float nsigma, float *C, int order) { /* DEC background fitting function: fit + outlier removal */ int i, M, outlier; float mean, sigma; float *xx = (float*)malloc(size * sizeof(float)); float *yy = (float*)malloc(size * sizeof(float)); float *x = (float*)malloc(size * sizeof(float)); float *y = (float*)malloc(size * sizeof(float)); float *p = (float*)malloc(size * sizeof(float)); float *diff = (float*)malloc(size * sizeof(float)); /* Extract lower envelope of data */ //for(i=0; i<size; i++){x[i] = X[i]; y[i] = Y[i];} xx[0] = X[0]; yy[0] = Y[0]; M = 1; for(i=1; i<size-1; i++) if(Y[i]-Y[i-1]<0 && Y[i+1]-Y[i]>0){xx[M] = X[i]; yy[M] = Y[i]; M++;} xx[M] = X[size-1]; yy[M] = Y[size-1]; size = M + 1; x[0] = xx[0]; y[0] = yy[0]; M = 1; for(i=1; i<size-1; i++) if(yy[i]-yy[i-1]>0 && yy[i+1]-yy[i]<0){x[M] = xx[i]; y[M] = yy[i]; M++;} x[M] = xx[size-1]; y[M] = yy[size-1]; size = M + 1; do { //chebyshev_cholesky(order, C, size, x, y); chebyshev_qrgsm(order, C, size, x, y); mean = 0.0; for(i=0; i<size; i++) { p[i] = chebyshev_eval(x[i], C, order); diff[i] = y[i] - p[i]; mean += diff[i]; } mean = mean/size; sigma = 0.0; for(i=0; i<size; i++) { diff[i] = diff[i] - mean; sigma += diff[i]*diff[i]; } sigma = nsigma*sqrt(sigma/size); outlier = 0; for(i=0; i<size; i++) { if(fabs(diff[i]) > sigma) { outlier = 1; size --; x[i] = x[size]; y[i] = y[size]; } } }while(outlier && size > order); free(xx); free(yy); free(x); free(y); free(diff); free(p); }
void chebyshev_fit_bw(float *X, float *Y, int size, float nsigma, float *C, int order) { /* Basketweaving fitting function: fit + outlier removal */ int i, outlier; float mean, sigma; float *x = (float*)malloc(size * sizeof(float)); float *y = (float*)malloc(size * sizeof(float)); float *p = (float*)malloc(size * sizeof(float)); float *diff = (float*)malloc(size * sizeof(float)); if(x == NULL || y == NULL || p == NULL || diff == NULL) printf("ERROR: malloc failed in chebyshev_fit_bw()\n"); for(i=0; i<size; i++){x[i] = X[i]; y[i] = Y[i];} int iter=0; do { //chebyshev_cholesky(order, C, size, x, y); chebyshev_qrgsm(order, C, size, x, y); mean = 0.0; for(i=0; i<size; i++) { p[i] = chebyshev_eval(x[i], C, order); diff[i] = y[i] - p[i]; mean += diff[i]; } mean = mean/size; sigma = 0.0; for(i=0; i<size; i++) { diff[i] = diff[i] - mean; sigma += diff[i]*diff[i]; } sigma = nsigma*sqrt(sigma/size); outlier = 0; for(i=0; i<size; i++) { if(fabs(diff[i]) > sigma) { outlier = 1; size --; x[i] = x[size]; y[i] = y[size]; } } iter++; }while(outlier && size > order); if(size <= order) printf("WARNING: size is %d order is %d\n",size,order); free(x); free(y); free(diff); free(p); }
double log1p(double x) { /* series for log1p on the interval -.375 to .375 * with weighted error 6.35e-32 * log weighted error 31.20 * significant figures required 30.93 * decimal places required 32.01 */ const static double alnrcs[43] = { +.10378693562743769800686267719098e+1, -.13364301504908918098766041553133e+0, +.19408249135520563357926199374750e-1, -.30107551127535777690376537776592e-2, +.48694614797154850090456366509137e-3, -.81054881893175356066809943008622e-4, +.13778847799559524782938251496059e-4, -.23802210894358970251369992914935e-5, +.41640416213865183476391859901989e-6, -.73595828378075994984266837031998e-7, +.13117611876241674949152294345011e-7, -.23546709317742425136696092330175e-8, +.42522773276034997775638052962567e-9, -.77190894134840796826108107493300e-10, +.14075746481359069909215356472191e-10, -.25769072058024680627537078627584e-11, +.47342406666294421849154395005938e-12, -.87249012674742641745301263292675e-13, +.16124614902740551465739833119115e-13, -.29875652015665773006710792416815e-14, +.55480701209082887983041321697279e-15, -.10324619158271569595141333961932e-15, +.19250239203049851177878503244868e-16, -.35955073465265150011189707844266e-17, +.67264542537876857892194574226773e-18, -.12602624168735219252082425637546e-18, +.23644884408606210044916158955519e-19, -.44419377050807936898878389179733e-20, +.83546594464034259016241293994666e-21, -.15731559416479562574899253521066e-21, +.29653128740247422686154369706666e-22, -.55949583481815947292156013226666e-23, +.10566354268835681048187284138666e-23, -.19972483680670204548314999466666e-24, +.37782977818839361421049855999999e-25, -.71531586889081740345038165333333e-26, +.13552488463674213646502024533333e-26, -.25694673048487567430079829333333e-27, +.48747756066216949076459519999999e-28, -.92542112530849715321132373333333e-29, +.17578597841760239233269760000000e-29, -.33410026677731010351377066666666e-30, +.63533936180236187354180266666666e-31, }; #ifdef NOMORE_FOR_THREADS static int nlnrel = 0; static double xmin = 0.0; if (xmin == 0.0) xmin = -1 + sqrt(DBL_EPSILON);/*was sqrt(d1mach(4)); */ if (nlnrel == 0) /* initialize chebychev coefficients */ nlnrel = chebyshev_init(alnrcs, 43, DBL_EPSILON/20);/*was .1*d1mach(3)*/ #else # define nlnrel 22 const static double xmin = -0.999999985; /* 22: for IEEE double precision where DBL_EPSILON = 2.22044604925031e-16 */ #endif if (x == 0.) return 0.;/* speed */ if (x == -1) return(ML_NEGINF); if (x < -1) ML_ERR_return_NAN; if (fabs(x) <= .375) { /* Improve on speed (only); again give result accurate to IEEE double precision: */ if(fabs(x) < .5 * DBL_EPSILON) return x; if( (0 < x && x < 1e-8) || (-1e-9 < x && x < 0)) return x * (1 - .5 * x); /* else */ return x * (1 - x * chebyshev_eval(x / .375, alnrcs, nlnrel)); } /* else */ if (x < xmin) { /* answer less than half precision because x too near -1 */ ML_ERROR(ME_PRECISION, "log1p"); } return log(1 + x); }
main() { double ans, exact, err; int m; double eps = 1.0e-6; double a = 0.0; double b = PI; int n_jac = 12; std::vector<double> x_jac(n_jac); std::vector<double> w_jac(n_jac); gauss_jacobi(x_jac, w_jac, n_jac, 1.5, -0.5); printf("\n\n Abscissas and wights from gauss_jacobi with alpha = 1.5, beta = -0.5.\n"); for (int i = 0; i < n_jac; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i, x_jac[i], i, w_jac[i]); int n_leg = 12; std::vector<double> x_leg(n_leg); std::vector<double> w_leg(n_leg); gauss_legendre(n_leg, a, b, x_leg, w_leg); printf("\n\n Abscissas and wights from gauss_legendre.\n"); for (int i = 0; i < n_leg; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i, x_leg[i] / PIO2 - 1.0, i, w_leg[i] / PIO2); int n_jac2 = 12; std::vector<double> x_jac2(n_jac2); std::vector<double> w_jac2(n_jac2); gauss_jacobi(x_jac2, w_jac2, n_jac2, 0.0, 0.0); printf("\n\n Abscissas and wights from gauss_jacobi with alpha = beta = 0.0.\n"); for (int i = 0; i < n_jac2; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i, x_jac2[i], i, w_jac2[i]); int n_cheb = 12; std::vector<double> x_cheb(n_cheb); std::vector<double> w_cheb(n_cheb); gauss_chebyshev(x_cheb, w_cheb, n_cheb); printf("\n\n Abscissas and wights from gauss_chebyshev.\n"); for (int i = 0; i < (n_cheb + 1) / 2; ++i) printf("\n x[%d] = -x[%d] = %16.12f w[%d] = w[%d] = %16.12f", i, n_cheb + 1 - i , x_cheb[i], i, n_cheb + 1 - i, w_cheb[i]); int n_jac3 = 12; std::vector<double> x_jac3(n_jac3); std::vector<double> w_jac3(n_jac3); gauss_jacobi(x_jac3, w_jac3, n_jac3, -0.5, -0.5); printf("\n\n Abscissas and wights from gauss_jacobi with alpha = beta = -0.5.\n"); for (int i = 0; i < n_jac3; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i, x_jac3[i], i, w_jac3[i]); int n_herm = 12; std::vector<double> x_herm(n_herm); std::vector<double> w_herm(n_herm); gauss_hermite(x_herm, w_herm, n_herm); printf("\n\n Abscissas and wights from gauss_hermite.\n"); for (int i = 0; i < (n_herm + 1) / 2; ++i) printf("\n x[%d] = -x[%d] = %16.12f w[%d] = w[%d] = %16.12f", i, n_herm + 1 - i , x_herm[i], i, n_herm + 1 - i, w_herm[i]); n_lag = 12; std::vector<double> x_lag(n_lag); std::vector<double> w_lag(n_lag); gauss_laguerre(x_lag, w_lag, n_lag, 1.0); printf("\n\n Abscissas and wights from gauss_laguerre.\n"); for (int i = 0; i < n_lag; ++i) printf("\n x[%d] = %16.12f w[%d] = %16.12f", i , x_lag[i], i, w_lag[i]); m = 40; std::vector<double> c(m); std::vector<double> cint(m); std::vector<double> cder(m); printf("\n\n\n\n Test of integration routines..."); printf("\n\n"); printf("\n\n %-40s %g", "Input requested error", eps); printf("\n\n %-40s %d", "Input order of Gaussian quadrature", n_leg); printf("\n\n %-40s %d", "Input order of Chebyshev fit", m); printf("\n\n"); a = 0.0; b = PI; printf("\n\n Integrate cos(x) from a = %f to b = %f . . .", a, PI); exact = 0.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(std::cos, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(std::cos, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's rule", ans, err); ans = quad_romberg(std::cos, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(std::cos, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, cos); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate sin(x) from a = %f to b = %f . . .", a, b); exact = 2.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(sin, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(sin, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's rule", ans, err); ans = quad_romberg(sin, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(sin, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, sin); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate cos^2(x) from a = %f to b = %f . . .", a, b); exact = PI/2.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(cos2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(cos2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's rule", ans, err); ans = quad_romberg(cos2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(cos2, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, cos2); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate sin^2(x) from a = %f to b = %f . . .", a, b); exact = PI/2.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(sin2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(sin2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's integral", ans, err); ans = quad_romberg(sin2, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(sin2, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, sin2); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate J_1(x) from a = %f to b = %f . . .", a, b); exact = bessel_j0(0.0) - bessel_j0(PI); printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(bessel_j1, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(bessel_j1, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's integral", ans, err); ans = quad_romberg(bessel_j1, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(bessel_j1, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); chebyshev_fit(a, b, c, m, bessel_j1); chebyshev_integ(a, b, c, cint, m); chebyshev_deriv(a, b, c, cder, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = 10.0*PI; printf("\n\n Integrate foo(x) = (1 - x)exp(-x/2) from a = %f to b = %f . . .", a, b); exact = 2.0*(1.0 + b)*exp(-b/2.0) - 2.0*(1.0 + a)*exp(-a/2.0); printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_trapezoid(foo, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Trapezoid rule", ans, err); ans = quad_simpson(foo, a, b, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Simpson's rule", ans, err); ans = quad_romberg_open(foo, a, b, eps, midpoint_exp); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg integration", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(foo, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); gauss_laguerre(x_lag, w_lag, n_lag, 0.0); ans = 0.0; for (int i = 0; i < n_lag; ++i) ans += 2 * w_lag[i] * foonum(2.0 * x_lag[i]); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Laguerre quadrature", ans, err); chebyshev_fit(a, b, c, m, foo); chebyshev_integ(a, b, c, cint, m); chebyshev_deriv(a, b, c, cder, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = clenshaw_curtis_quad(a, b, c, m, eps); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Clenshaw - Curtis quadrature", ans, err); a = 0.0; b = PI; printf("\n\n Integrate funk1(x) = cos(x)/sqrt(x(PI - x)) from a = %f to b = %f . . .", a, b); exact = 0.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_romberg_open(funk1, a, b, eps, midpoint); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg quadrature with midpoint", ans, err); ans = quad_romberg_open(funk1, a, (a+b)/2, eps, midpoint_inv_sqrt_lower) + quad_romberg_open(funk1, (a+b)/2, b, eps, midpoint_inv_sqrt_upper); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg with inverse sqrt step", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(funk1, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); ans = quad_gauss(funk1num, a, b, x_cheb, w_cheb, n_cheb); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Chebyshev quadrature", ans, err); ans = quad_gauss(funk1num, a, b, x_jac, w_jac, n_jac); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Jacobi quadrature", ans, err); chebyshev_fit(a, b, c, m, funk1); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = dumb_gauss_crap(funk1num, a, b, 8); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Chebyshev quadrature", ans, err); printf("\n\n"); plot_func(funk1, a+0.1, b-0.1, "", "", "", ""); a = 0.0; b = PI; printf("\n\n Integrate funk2(x) = (2.0+sin(x))/sqrt(x(PI - x)) from a = %f to b = %f . . .", a, b); exact = 0.0; printf("\n %-40s %16.12f", "Exact answer", exact); ans = quad_romberg_open(funk2, a, b, eps, midpoint); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg quadrature with midpoint", ans, err); ans = quad_romberg_open(funk2, a, (a+b)/2, eps, midpoint_inv_sqrt_lower) + quad_romberg_open(funk2, (a+b)/2, b, eps, midpoint_inv_sqrt_upper); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Open Romberg with inverse sqrt step", ans, err); gauss_legendre(n_leg, a, b, x_leg, w_leg); ans = quad_gauss_legendre(funk2, x_leg, w_leg, n_leg); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Legendre quadrature", ans, err); ans = quad_gauss(funk2num, a, b, x_cheb, w_cheb, n_cheb); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Chebyshev quadrature", ans, err); ans = quad_gauss(funk2num, a, b, x_jac, w_jac, n_jac); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Gauss - Jacobi quadrature", ans, err); chebyshev_fit(a, b, c, m, funk2); chebyshev_integ(a, b, c, cint, m); ans = chebyshev_eval(a, b, cint, m, b); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Chebyshev evaluation of integral", ans, err); ans = dumb_gauss_crap(funk2num, a, b, 8); err = ans - exact; printf("\n %-40s %16.12f %16.12f", "Adaptive Gauss - Chebyshev quadrature", ans, err); printf("\n\n"); plot_func(funk2, a+0.1, b-0.1, "", "", "", ""); printf("\n\n"); }
//---------------------------------------------------------------------------------------------------------- void linear_fit_cal(SpecRecord dataset[], int size, int lowchan, int highchan, int RFIF,int fwindow) { int n, chan, order = 1; float C[order + 1]; float nsigma = 2.0; float *XRA, *Yxx, *Yyy, *Yxy, *Yyx; float *Cxx, *Cyy, *Cxy, *Cyx; float min, max; int count; XRA = (float*)calloc(size,sizeof(float)); Yxx = (float*)calloc(size,sizeof(float)); Yyy = (float*)calloc(size,sizeof(float)); Yxy = (float*)calloc(size,sizeof(float)); Yyx = (float*)calloc(size,sizeof(float)); Cxx = (float*)calloc((highchan-lowchan),sizeof(float)); Cyy = (float*)calloc((highchan-lowchan),sizeof(float)); Cxy = (float*)calloc((highchan-lowchan),sizeof(float)); Cyx = (float*)calloc((highchan-lowchan),sizeof(float)); for(chan=lowchan; chan<highchan; chan++) { count = 0; for(n=0; n<size; n++) { if(dataset[n].flagBAD) continue; if(!RFIF || dataset[n].flagRFI[chan] == RFI_NONE) { XRA[count] = dataset[n].RA; Yxx[count] += dataset[n].cal.xx[chan]; Yyy[count] += dataset[n].cal.yy[chan]; count++; Cxx[chan-lowchan] += dataset[n].cal.xx[chan]; Cyy[chan-lowchan] += dataset[n].cal.yy[chan]; Cxy[chan-lowchan] += dataset[n].cal.xy[chan]; Cyx[chan-lowchan] += dataset[n].cal.yx[chan]; } } if(count) { Cxx[chan-lowchan]/=count; Cyy[chan-lowchan]/=count; Cxy[chan-lowchan]/=count; Cyx[chan-lowchan]/=count; } } //printf("Count is %d\n",count); FILE *f = fopen("avgtimecal.raw","w"); for(chan=lowchan; chan<highchan; chan++) { fprintf(f,"%d %f %f %f %f\n",chan,Cxx[chan-lowchan],Cyy[chan-lowchan],Cxy[chan-lowchan],Cyx[chan-lowchan]); } fclose(f); diffusion_filter(Cxx, highchan - lowchan, fwindow); diffusion_filter(Cyy, highchan - lowchan, fwindow); diffusion_filter(Cxy, highchan - lowchan, fwindow); diffusion_filter(Cyx, highchan - lowchan, fwindow); float avgxx=0.0,avgyy=0.0,avgxy=0.0,avgyx=0.0; for(chan=lowchan; chan<highchan; chan++) { avgxx += Cxx[chan-lowchan]; avgyy += Cyy[chan-lowchan]; } avgxx/=(highchan-lowchan); avgyy/=(highchan-lowchan); f = fopen("avgtimecal.dat","w"); for(chan=lowchan; chan<highchan; chan++) { Cxx[chan-lowchan]/=avgxx; Cyy[chan-lowchan]/=avgyy; fprintf(f,"%d %f %f %f %f\n",chan,Cxx[chan-lowchan],Cyy[chan-lowchan],Cxy[chan-lowchan],Cyx[chan-lowchan]); } fclose(f); for(n=0; n<count; n++) { Yxx[n] /=(highchan-lowchan); Yyy[n] /=(highchan-lowchan); } if(count) { chebyshev_minmax(XRA, count, &min, &max); chebyshev_normalize(XRA, count, min, max); } //XX C[0] = 0.0; C[1] = 0.0; if(count) chebyshev_fit_bw(XRA, Yxx, count, nsigma, C, order); printf("XX M %2.6f C %2.6f\n",C[1],C[0]); for(chan=lowchan; chan<highchan; chan++) { for(n=0; n<size; n++) { if(dataset[n].flagBAD) continue; if(!RFIF || dataset[n].flagRFI[chan] == RFI_NONE) { dataset[n].cal.xx[chan] = chebyshev_eval(CNORMALIZE(dataset[n].RA,min,max), C, order)*Cxx[chan-lowchan]; } } } //YY C[0] = 0.0; C[1] = 0.0; if(count) chebyshev_fit_bw(XRA, Yyy, count, nsigma, C, order); printf("YY M %2.6f C %2.6f\n",C[1],C[0]); for(chan=lowchan; chan<highchan; chan++) { for(n=0; n<size; n++) { if(dataset[n].flagBAD) continue; if(!RFIF || dataset[n].flagRFI[chan] == RFI_NONE) { dataset[n].cal.yy[chan] = chebyshev_eval(CNORMALIZE(dataset[n].RA,min,max), C, order)*Cyy[chan-lowchan]; } } } //XY for(chan=lowchan; chan<highchan; chan++) { for(n=0; n<size; n++) { if(dataset[n].flagBAD) continue; if(!RFIF || dataset[n].flagRFI[chan] == RFI_NONE) { dataset[n].cal.xy[chan] = Cxy[chan-lowchan]; } } } //YX for(chan=lowchan; chan<highchan; chan++) { for(n=0; n<size; n++) { if(dataset[n].flagBAD) continue; if(!RFIF || dataset[n].flagRFI[chan] == RFI_NONE) { dataset[n].cal.yx[chan] = Cyx[chan-lowchan]; } } } //printf("\n"); free(XRA); free(Yxx); free(Yyy); free(Yxy); free(Yyx); free(Cxx); free(Cyy); free(Cxy); free(Cyx); // fclose(f); return; }
main() { int i, n, m5, ma, mf; double a, b, h, x, f, err; double *c, *cint, *cder; a = 0.0; b = 5.0; h = (b-a)/10.0; n = 40; m5 = 5; ma = 10; mf = 15; c = dvector( 0, n-1 ); cder = dvector( 0, n-1 ); cint = dvector( 0, n-1 ); printf( "\n\n\n Test Chebyshev fitting.\n" ); chebyshev_fit( a, b, c, n, one ); chebyshev_deriv( a, b, c, cder, mf ); chebyshev_integ( a, b, c, cint, mf ); printf( "\n\n Fit of f(x) = 1 from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, n ); for ( i = 0; i < n/2; ++i ) printf( "\n c[%2d] = %16.12f c[%2d] = %16.12f", i, c[i], i+n/2, c[i+n/2] ); printf( "\n\n Evaluation of f(x) = 1 from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, m5 ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, c, m5, x ); err = f - one(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation of f(x) = 1 from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, ma ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, c, ma, x ); err = f - one(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation of derivative of f(x) from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, ma ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, cder, ma, x ); err = f - one_der(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation of integral of f(x) from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, ma ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, cint, ma, x ); err = f - one_int(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } chebyshev_fit( a, b, c, n, ex ); chebyshev_deriv( a, b, c, cder, mf ); chebyshev_integ( a, b, c, cint, mf ); printf( "\n\n Fit of f(x) = x from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, n ); for ( i = 0; i < n/2; ++i ) printf( "\n c[%2d] = %16.12f c[%2d] = %16.12f", i, c[i], i+n/2, c[i+n/2] ); printf( "\n\n Evaluation of f(x) = x from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, m5 ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, c, m5, x ); err = f - ex(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation of f(x) = x from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, ma ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, c, ma, x ); err = f - ex(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation of derivative of f(x) from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, ma ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, cder, ma, x ); err = f - ex_der(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation of integral of f(x) from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, ma ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, cint, ma, x ); err = f - ex_int(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } chebyshev_fit( a, b, c, n, foo ); chebyshev_deriv( a, b, c, cder, mf ); chebyshev_integ( a, b, c, cint, n ); printf( "\n\n Fit of f(x) = (1 - x)exp(-x/2) from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, n ); for ( i = 0; i < n/2; ++i ) printf( "\n c[%2d] = %16.12f c[%2d] = %16.12f", i, c[i], i+n/2, c[i+n/2] ); printf( "\n\n Fit of f'(x) = -(3 - x)exp(-x/2)/2 from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, n ); for ( i = 0; i < n/2; ++i ) printf( "\n cder[%2d] = %16.12f cder[%2d] = %16.12f", i, cder[i], i+n/2, cder[i+n/2] ); printf( "\n\n Fit of F(x) = 2(1 + x)exp(-x/2) - 2 from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, n ); for ( i = 0; i < n/2; ++i ) printf( "\n cint[%2d] = %16.12f cint[%2d] = %16.12f", i, cint[i], i+n/2, cint[i+n/2] ); printf( "\n\n Evaluation of f(x) = (1 - x)exp(-x/2) from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, m5 ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, c, m5, x ); err = f - foo(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation of f(x) = (1 - x)exp(-x/2) from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, ma ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, c, ma, x ); err = f - foo(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation of f(x) = (1 - x)exp(-x/2) from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, mf ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, c, mf, x ); err = f - foo(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation of f'(x) = -(3 - x)exp(-x/2)/2 from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, mf ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, cder, mf, x ); err = f - foo_der(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n Evaluation F(x) = 2(1 + x)exp(-x/2) - 2 from a = %3.1f to b = %3.1f with %d coefficients . . .", a, b, mf ); x = a; while ( x <= b ) { f = chebyshev_eval( a, b, cint, mf, x ); err = f - foo_int(x); printf( "\n f(%3.1f) = %16.12f error = %16.12f", x, f, err ); x += h; } printf( "\n\n" ); free_dvector( cint, 0, n-1 ); free_dvector( cder, 0, n-1 ); free_dvector( c, 0, n-1 ); }