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_interp_bsearch(int argc, VALUE *argv, VALUE obj) { gsl_vector *v = NULL; double x; size_t indexl, indexh; switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: switch (argc) { case 2: CHECK_VECTOR(argv[0]); Need_Float(argv[1]); Data_Get_Struct(argv[0], gsl_vector, v); x = NUM2DBL(argv[1]); indexl = gsl_vector_get(v, 0); indexh = gsl_vector_get(v, v->size-1); break; case 4: CHECK_VECTOR(argv[0]); Need_Float(argv[1]); Need_Float(argv[2]); Need_Float(argv[3]); Data_Get_Struct(argv[0], gsl_vector, v); x = NUM2DBL(argv[1]); indexl = NUM2DBL(argv[2]); indexh = NUM2DBL(argv[3]); break; default: rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 4)", argc); break; } break; default: Data_Get_Struct(obj, gsl_vector, v); switch (argc) { case 1: Need_Float(argv[0]); x = NUM2DBL(argv[0]); indexl = gsl_vector_get(v, 0); indexh = gsl_vector_get(v, v->size-1); break; case 3: Need_Float(argv[0]); Need_Float(argv[1]); Need_Float(argv[2]); x = NUM2DBL(argv[0]); indexl = NUM2DBL(argv[1]); indexh = NUM2DBL(argv[2]); break; default: rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 3)", argc); break; } break; } return INT2FIX(gsl_interp_bsearch(v->data, x, indexl, indexh)); }
int gsl_interp_accel_find (gsl_interp_accel * a, const double xa[], int len, double x) { int x_index = a->cache; if (x < xa[x_index]) { a->miss_count++; a->cache = gsl_interp_bsearch (xa, x, 0, x_index); } else if (x > xa[x_index + 1]) { a->miss_count++; a->cache = gsl_interp_bsearch (xa, x, x_index, len - 1); } else { a->hit_count++; } return a->cache; }
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; } }
int test_bsearch(void) { double x_array[5] = { 0.0, 1.0, 2.0, 3.0, 4.0 }; size_t index_result; int status = 0; int s; /* check an interior point */ index_result = gsl_interp_bsearch(x_array, 1.5, 0, 4); s = (index_result != 1); status += s; gsl_test (s, "simple bsearch"); /* check that we get the last interval if x == last value */ index_result = gsl_interp_bsearch(x_array, 4.0, 0, 4); s = (index_result != 3); status += s; gsl_test (s, "upper endpoint bsearch"); /* check that we get the first interval if x == first value */ index_result = gsl_interp_bsearch(x_array, 0.0, 0, 4); s = (index_result != 0); status += s; gsl_test (s, "lower endpoint bsearch"); /* check that we get correct interior boundary behaviour */ index_result = gsl_interp_bsearch(x_array, 2.0, 0, 4); s = (index_result != 2); status += s; gsl_test (s, "degenerate bsearch"); /* check out of bounds above */ index_result = gsl_interp_bsearch(x_array, 10.0, 0, 4); s = (index_result != 3); status += s; gsl_test (s, "out of bounds bsearch +"); /* check out of bounds below */ index_result = gsl_interp_bsearch(x_array, -10.0, 0, 4); s = (index_result != 0); status += s; gsl_test (s, "out of bounds bsearch -"); return status; }
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; }