/// Bilinear interpolation for u given k*rs and c double cosmology::ukinterp(double krs, double c) { // Bilinear interpolation is not defined in GSL. So code this up yourself if(!bool_inituk) { // Init ukm ukinit(); } double xc=log10(c); double xkrs=log10(krs); double result; int i,j; i=j=-99; // Complain for out of bound krs and c values if(xkrs>uk_krs[Nuk-1]){ if(verbose){ std::cout<<"krs out of bound in ukinterp, log10[krs]="<<xkrs<<std::endl; std::cout<<"Extrapolating to "<<xkrs<<std::endl; } j=Nuk-2; //exit(0); } if(xc>uk_c[Nuk-1]||xc<uk_c[0]){ if(verbose){ std::cout<<"c out of bound in ukinterp, log10[c]="<<xc<<std::endl; std::cout<<"Extrapolating to "<<xc<<std::endl; } i=(xc>uk_c[Nuk-1])?(Nuk-2):0; //exit(0); } if(xkrs<uk_krs[0]){ // For small k u(k|M)=1.0 result=1.0; return result; } // Look up con and krs index if(i==-99) i=gsl_interp_accel_find (uk_c_acc,uk_c,Nuk,xc); if(j==-99) j=gsl_interp_accel_find (uk_krs_acc,uk_krs,Nuk,xkrs); double y11=ukrsc[ i ][ j ]; double y12=ukrsc[ i ][j+1]; double y21=ukrsc[i+1][ j ]; double y22=ukrsc[i+1][j+1]; double kdis=(xkrs-uk_krs[j])/(uk_krs[j+1]-uk_krs[j]); double cdis=(xc-uk_c[i])/(uk_c[i+1]-uk_c[i]); double r1=y11+cdis*(y21-y11); double r2=y12+cdis*(y22-y12); result=r1+kdis*(r2-r1); return pow(10.,result); }
static int akima_eval_integ (const void * vstate, const double x_array[], const double y_array[], size_t size, gsl_interp_accel * acc, double a, double b, double * result) { const akima_state_t *state = (const akima_state_t *) vstate; size_t i, index_a, index_b; if (acc != 0) { index_a = gsl_interp_accel_find (acc, x_array, size, a); index_b = gsl_interp_accel_find (acc, x_array, size, b); } else { index_a = gsl_interp_bsearch (x_array, a, 0, size - 1); index_b = gsl_interp_bsearch (x_array, b, 0, size - 1); } *result = 0.0; /* interior intervals */ for(i=index_a; i<=index_b; i++) { const double x_hi = x_array[i + 1]; const double x_lo = x_array[i]; const double y_lo = y_array[i]; const double dx = x_hi - x_lo; if(dx != 0.0) { if (i == index_a || i == index_b) { double x1 = (i == index_a) ? a : x_lo; double x2 = (i == index_b) ? b : x_hi; *result += integ_eval (y_lo, state->b[i], state->c[i], state->d[i], x_lo, x1, x2); } else { *result += dx * (y_lo + dx*(0.5*state->b[i] + dx*(state->c[i]/3.0 + 0.25*state->d[i]*dx))); } } else { *result = 0.0; return GSL_FAILURE; } } return GSL_SUCCESS; }
static int cspline_eval_integ (const void * vstate, const double x_array[], const double y_array[], size_t size, gsl_interp_accel * acc, double a, double b, double * result) { const cspline_state_t *state = (const cspline_state_t *) vstate; size_t i, index_a, index_b; if (acc != 0) { index_a = gsl_interp_accel_find (acc, x_array, size, a); index_b = gsl_interp_accel_find (acc, x_array, size, b); } else { index_a = gsl_interp_bsearch (x_array, a, 0, size - 1); index_b = gsl_interp_bsearch (x_array, b, 0, size - 1); } *result = 0.0; /* interior intervals */ for(i=index_a; i<=index_b; i++) { const double x_hi = x_array[i + 1]; const double x_lo = x_array[i]; const double y_lo = y_array[i]; const double y_hi = y_array[i + 1]; const double dx = x_hi - x_lo; const double dy = y_hi - y_lo; if(dx != 0.0) { double b_i, c_i, d_i; coeff_calc(state->c, dy, dx, i, &b_i, &c_i, &d_i); if (i == index_a || i == index_b) { double x1 = (i == index_a) ? a : x_lo; double x2 = (i == index_b) ? b : x_hi; *result += integ_eval(y_lo, b_i, c_i, d_i, x_lo, x1, x2); } else { *result += dx * (y_lo + dx*(0.5*b_i + dx*(c_i/3.0 + 0.25*d_i*dx))); } } else { *result = 0.0; return GSL_EINVAL; } } return GSL_SUCCESS; }
static int linear_eval_integ (const void * vstate, const double x_array[], const double y_array[], size_t size, gsl_interp_accel * acc, double a, double b, double * result) { size_t i, index_a, index_b; if (acc != 0) { index_a = gsl_interp_accel_find (acc, x_array, size, a); index_b = gsl_interp_accel_find (acc, x_array, size, b); } else { index_a = gsl_interp_bsearch (x_array, a, 0, size - 1); index_b = gsl_interp_bsearch (x_array, b, 0, size - 1); } /* endpoints span more than one interval */ *result = 0.0; /* interior intervals */ for(i=index_a; i<=index_b; i++) { const double x_hi = x_array[i + 1]; const double x_lo = x_array[i]; const double y_lo = y_array[i]; const double y_hi = y_array[i + 1]; const double dx = x_hi - x_lo; if(dx != 0.0) { if (i == index_a || i == index_b) { double x1 = (i == index_a) ? a : x_lo; double x2 = (i == index_b) ? b : x_hi; const double D = (y_hi-y_lo)/dx; *result += (x2-x1) * (y_lo + 0.5*D*((x2-x_lo)+(x1-x_lo))); } else { *result += 0.5 * dx * (y_lo + y_hi); } } } return GSL_SUCCESS; }
static int akima_eval_deriv2 (const void * vstate, const double x_array[], const double y_array[], size_t size, double x, gsl_interp_accel * a, double *y_pp) { const akima_state_t *state = (const akima_state_t *) vstate; size_t index; DISCARD_POINTER(y_array); /* prevent warning about unused parameter */ if (a != 0) { index = gsl_interp_accel_find (a, x_array, size, x); } else { index = gsl_interp_bsearch (x_array, x, 0, size - 1); } /* evaluate */ { const double x_lo = x_array[index]; const double delx = x - x_lo; const double c = state->c[index]; const double d = state->d[index]; *y_pp = 2.0 * c + 6.0 * d * delx; return GSL_SUCCESS; } }
static int akima_eval (const void * vstate, const double x_array[], const double y_array[], size_t size, double x, gsl_interp_accel * a, double *y) { const akima_state_t *state = (const akima_state_t *) vstate; size_t index; if (a != 0) { index = gsl_interp_accel_find (a, x_array, size, x); } else { index = gsl_interp_bsearch (x_array, x, 0, size - 1); } /* evaluate */ { const double x_lo = x_array[index]; const double delx = x - x_lo; const double b = state->b[index]; const double c = state->c[index]; const double d = state->d[index]; *y = y_array[index] + delx * (b + delx * (c + d * delx)); return GSL_SUCCESS; } }
static VALUE rb_gsl_spline_find(VALUE obj, VALUE vv, VALUE xx) { rb_gsl_spline *sp = NULL; double *ptr = NULL, x; size_t size, stride; Data_Get_Struct(obj, rb_gsl_spline, sp); ptr = get_vector_ptr(vv, &stride, &size); x = RFLOAT_VALUE(xx); return INT2FIX(gsl_interp_accel_find(sp->a, ptr, size, x)); }
static VALUE rb_gsl_interp_find(VALUE obj, VALUE vv, VALUE xx) { rb_gsl_interp *rgi = NULL; double *ptr = NULL, x; size_t size, stride; Need_Float(xx); Data_Get_Struct(obj, rb_gsl_interp, rgi); ptr = get_vector_ptr(vv, &stride, &size); x = NUM2DBL(xx); return INT2FIX(gsl_interp_accel_find(rgi->a, ptr, size, x)); }
static VALUE rb_gsl_interp_accel_find(VALUE obj, VALUE vv, VALUE xx) { gsl_interp_accel *a = NULL; double x, *ptr = NULL; size_t size, stride; Need_Float(xx); Data_Get_Struct(obj, gsl_interp_accel, a); ptr = get_vector_ptr(vv, &stride, &size); Need_Float(xx); x = NUM2DBL(xx); return INT2FIX(gsl_interp_accel_find(a, ptr, size, x)); }
static int bilinear_deriv_x(const void * state, const double xarr[], const double yarr[], const double zarr[], size_t xsize, size_t ysize, double x, double y, gsl_interp_accel * xa, gsl_interp_accel * ya, double * z_p) { double xmin, xmax, ymin, ymax, zminmin, zminmax, zmaxmin, zmaxmax; double dx, dy; double dt, u; size_t xi, yi; if (xa != NULL) xi = gsl_interp_accel_find(xa, xarr, xsize, x); else xi = gsl_interp_bsearch(xarr, x, 0, xsize - 1); if (ya != NULL) yi = gsl_interp_accel_find(ya, yarr, ysize, y); else yi = gsl_interp_bsearch(yarr, y, 0, ysize - 1); xmin = xarr[xi]; xmax = xarr[xi + 1]; ymin = yarr[yi]; ymax = yarr[yi + 1]; zminmin = zarr[IDX2D(xi, yi, xsize, ysize)]; zminmax = zarr[IDX2D(xi, yi + 1, xsize, ysize)]; zmaxmin = zarr[IDX2D(xi + 1, yi, xsize, ysize)]; zmaxmax = zarr[IDX2D(xi + 1, yi + 1, xsize, ysize)]; dx = xmax - xmin; dy = ymax - ymin; dt = 1./dx; /* partial t / partial x */ u = (y - ymin)/dy; *z_p = dt*(-(1.-u)*zminmin + (1.-u)*zmaxmin - u*zminmax + u*zmaxmax); return GSL_SUCCESS; }
static int bilinear_eval(const void * state, const double xarr[], const double yarr[], const double zarr[], size_t xsize, size_t ysize, double x, double y, gsl_interp_accel * xa, gsl_interp_accel * ya, double * z) { double xmin, xmax, ymin, ymax, zminmin, zminmax, zmaxmin, zmaxmax; double dx, dy; double t, u; size_t xi, yi; if (xa != NULL) xi = gsl_interp_accel_find(xa, xarr, xsize, x); else xi = gsl_interp_bsearch(xarr, x, 0, xsize - 1); if (ya != NULL) yi = gsl_interp_accel_find(ya, yarr, ysize, y); else yi = gsl_interp_bsearch(yarr, y, 0, ysize - 1); xmin = xarr[xi]; xmax = xarr[xi + 1]; ymin = yarr[yi]; ymax = yarr[yi + 1]; zminmin = zarr[IDX2D(xi, yi, xsize, ysize)]; zminmax = zarr[IDX2D(xi, yi + 1, xsize, ysize)]; zmaxmin = zarr[IDX2D(xi + 1, yi, xsize, ysize)]; zmaxmax = zarr[IDX2D(xi + 1, yi + 1, xsize, ysize)]; dx = xmax - xmin; dy = ymax - ymin; t = (x - xmin)/dx; u = (y - ymin)/dy; *z = (1.-t)*(1.-u)*zminmin + t*(1.-u)*zmaxmin + (1.-t)*u*zminmax + t*u*zmaxmax; return GSL_SUCCESS; }
static int cspline_eval_deriv2 (const void * vstate, const double x_array[], const double y_array[], size_t size, double x, gsl_interp_accel * a, double * y_pp) { const cspline_state_t *state = (const cspline_state_t *) vstate; double x_lo, x_hi; double dx; size_t index; if (a != 0) { index = gsl_interp_accel_find (a, x_array, size, x); } else { index = gsl_interp_bsearch (x_array, x, 0, size - 1); } /* evaluate */ x_hi = x_array[index + 1]; x_lo = x_array[index]; dx = x_hi - x_lo; if (dx > 0.0) { const double y_lo = y_array[index]; const double y_hi = y_array[index + 1]; const double dy = y_hi - y_lo; double delx = x - x_lo; double b_i, c_i, d_i; coeff_calc(state->c, dy, dx, index, &b_i, &c_i, &d_i); *y_pp = 2.0 * c_i + 6.0 * d_i * delx; return GSL_SUCCESS; } else { *y_pp = 0.0; return GSL_EINVAL; } }
static int linear_eval_deriv (const void * vstate, const double x_array[], const double y_array[], size_t size, double x, gsl_interp_accel * a, double *dydx) { double x_lo, x_hi; double y_lo, y_hi; double dx; double dy; size_t index; if (a != 0) { index = gsl_interp_accel_find (a, x_array, size, x); } else { index = gsl_interp_bsearch (x_array, x, 0, size - 1); } /* evaluate */ x_lo = x_array[index]; x_hi = x_array[index + 1]; y_lo = y_array[index]; y_hi = y_array[index + 1]; dx = x_hi - x_lo; dy = y_hi - y_lo; if (dx > 0.0) { *dydx = dy / dx;; return GSL_SUCCESS; } else { *dydx = 0.0; return GSL_EINVAL; } }
static int bicubic_deriv_yy(const void * vstate, const double xarr[], const double yarr[], const double zarr[], size_t xsize, size_t ysize, double x, double y, gsl_interp_accel * xa, gsl_interp_accel * ya, double * z_pp) { bicubic_state_t *state = (bicubic_state_t *) vstate; double xmin, xmax, ymin, ymax; double zminmin, zminmax, zmaxmin, zmaxmax; double zxminmin, zxminmax, zxmaxmin, zxmaxmax; double zyminmin, zyminmax, zymaxmin, zymaxmax; double zxyminmin, zxyminmax, zxymaxmin, zxymaxmax; double dx, dy; /* size of the grid cell */ double dt, du; /* * t and u are the positions within the grid cell at which we are computing * the interpolation, in units of grid cell size */ double t, u; double t0, t1, t2, t3, u0, u1; double v; size_t xi, yi; /* first compute the indices into the data arrays where we are interpolating */ if (xa != NULL) xi = gsl_interp_accel_find(xa, xarr, xsize, x); else xi = gsl_interp_bsearch(xarr, x, 0, xsize - 1); if (ya != NULL) yi = gsl_interp_accel_find(ya, yarr, ysize, y); else yi = gsl_interp_bsearch(yarr, y, 0, ysize - 1); /* find the minimum and maximum values on the grid cell in each dimension */ xmin = xarr[xi]; xmax = xarr[xi + 1]; ymin = yarr[yi]; ymax = yarr[yi + 1]; zminmin = zarr[IDX2D(xi, yi, state)]; zminmax = zarr[IDX2D(xi, yi + 1, state)]; zmaxmin = zarr[IDX2D(xi + 1, yi, state)]; zmaxmax = zarr[IDX2D(xi + 1, yi + 1, state)]; /* get the width and height of the grid cell */ dx = xmax - xmin; dy = ymax - ymin; t = (x - xmin)/dx; u = (y - ymin)/dy; dt = 1./dx; /* partial t / partial x */ du = 1./dy; /* partial u / partial y */ zxminmin = state->zx[IDX2D(xi, yi, state)]/dt; zxminmax = state->zx[IDX2D(xi, yi + 1, state)]/dt; zxmaxmin = state->zx[IDX2D(xi + 1, yi, state)]/dt; zxmaxmax = state->zx[IDX2D(xi + 1, yi + 1, state)]/dt; zyminmin = state->zy[IDX2D(xi, yi, state)]/du; zyminmax = state->zy[IDX2D(xi, yi + 1, state)]/du; zymaxmin = state->zy[IDX2D(xi + 1, yi, state)]/du; zymaxmax = state->zy[IDX2D(xi + 1, yi + 1, state)]/du; zxyminmin = state->zxy[IDX2D(xi, yi, state)]/(dt*du); zxyminmax = state->zxy[IDX2D(xi, yi + 1, state)]/(dt*du); zxymaxmin = state->zxy[IDX2D(xi + 1, yi, state)]/(dt*du); zxymaxmax = state->zxy[IDX2D(xi + 1, yi + 1, state)]/(dt*du); t0 = 1; t1 = t; t2 = t*t; t3 = t*t2; u0 = 1; u1 = u; *z_pp = 0; v = -3*zminmin + 3*zminmax - 2*zyminmin - zyminmax; *z_pp += 2*v*t0*u0; v = 2*zminmin-2*zminmax + zyminmin + zyminmax; *z_pp += 6*v*t0*u1; v = -3*zxminmin + 3*zxminmax - 2*zxyminmin - zxyminmax; *z_pp += 2*v*t1*u0; v = 2*zxminmin - 2*zxminmax + zxyminmin + zxyminmax; *z_pp += 6*v*t1*u1; v = 9*zminmin - 9*zmaxmin + 9*zmaxmax - 9*zminmax + 6*zxminmin + 3*zxmaxmin - 3*zxmaxmax - 6*zxminmax + 6*zyminmin - 6*zymaxmin - 3*zymaxmax + 3*zyminmax + 4*zxyminmin + 2*zxymaxmin + zxymaxmax + 2*zxyminmax; *z_pp += 2*v*t2*u0; v = -6*zminmin + 6*zmaxmin - 6*zmaxmax + 6*zminmax - 4*zxminmin - 2*zxmaxmin + 2*zxmaxmax + 4*zxminmax - 3*zyminmin + 3*zymaxmin + 3*zymaxmax - 3*zyminmax - 2*zxyminmin - zxymaxmin - zxymaxmax - 2*zxyminmax; *z_pp += 6*v*t2*u1; v = -6*zminmin + 6*zmaxmin - 6*zmaxmax + 6*zminmax - 3*zxminmin - 3*zxmaxmin + 3*zxmaxmax + 3*zxminmax - 4*zyminmin + 4*zymaxmin + 2*zymaxmax - 2*zyminmax - 2*zxyminmin - 2*zxymaxmin - zxymaxmax - zxyminmax; *z_pp += 2*v*t3*u0; v = 4*zminmin - 4*zmaxmin + 4*zmaxmax - 4*zminmax + 2*zxminmin + 2*zxmaxmin - 2*zxmaxmax - 2*zxminmax + 2*zyminmin - 2*zymaxmin - 2*zymaxmax + 2*zyminmax + zxyminmin + zxymaxmin + zxymaxmax + zxyminmax; *z_pp += 6*v*t3*u1; *z_pp *= du*du; return GSL_SUCCESS; }
/** * find the nearest index i to frequency freq in fft such that * f[i] < freq <f[i+1] */ int BandPassFilterFFT::ffind(double freq){ return gsl_interp_accel_find (faccel, f, freqs, freq ); }