1 /* interpolation/linear.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_interp.h>
28 linear_init (void * vstate,
29 const double x_array[],
30 const double y_array[],
38 linear_eval (const void * vstate,
39 const double x_array[], const double y_array[], size_t size,
51 index = gsl_interp_accel_find (a, x_array, size, x);
55 index = gsl_interp_bsearch (x_array, x, 0, size - 1);
59 x_lo = x_array[index];
60 x_hi = x_array[index + 1];
61 y_lo = y_array[index];
62 y_hi = y_array[index + 1];
66 *y = y_lo + (x - x_lo) / dx * (y_hi - y_lo);
79 linear_eval_deriv (const void * vstate,
80 const double x_array[], const double y_array[], size_t size,
93 index = gsl_interp_accel_find (a, x_array, size, x);
97 index = gsl_interp_bsearch (x_array, x, 0, size - 1);
101 x_lo = x_array[index];
102 x_hi = x_array[index + 1];
103 y_lo = y_array[index];
104 y_hi = y_array[index + 1];
122 linear_eval_deriv2 (const void * vstate,
123 const double x_array[], const double y_array[], size_t size,
125 gsl_interp_accel * a,
136 linear_eval_integ (const void * vstate,
137 const double x_array[], const double y_array[], size_t size,
138 gsl_interp_accel * acc,
142 size_t i, index_a, index_b;
146 index_a = gsl_interp_accel_find (acc, x_array, size, a);
147 index_b = gsl_interp_accel_find (acc, x_array, size, b);
151 index_a = gsl_interp_bsearch (x_array, a, 0, size - 1);
152 index_b = gsl_interp_bsearch (x_array, b, 0, size - 1);
155 /* endpoints span more than one interval */
159 /* interior intervals */
160 for(i=index_a; i<=index_b; i++) {
161 const double x_hi = x_array[i + 1];
162 const double x_lo = x_array[i];
163 const double y_lo = y_array[i];
164 const double y_hi = y_array[i + 1];
165 const double dx = x_hi - x_lo;
168 if (i == index_a || i == index_b)
170 double x1 = (i == index_a) ? a : x_lo;
171 double x2 = (i == index_b) ? b : x_hi;
172 const double D = (y_hi-y_lo)/dx;
173 *result += (x2-x1) * (y_lo + 0.5*D*((x2-x_lo)+(x1-x_lo)));
177 *result += 0.5 * dx * (y_lo + y_hi);
185 static const gsl_interp_type linear_type =
189 NULL, /* alloc, not applicable */
195 NULL, /* free, not applicable */
198 const gsl_interp_type * gsl_interp_linear = &linear_type;