1 /* interpolation/akima.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.
25 #include <gsl/gsl_errno.h>
26 #include "integ_eval.h"
27 #include <gsl/gsl_interp.h>
40 akima_alloc (size_t size)
42 akima_state_t *state = (akima_state_t *) malloc (sizeof (akima_state_t));
46 GSL_ERROR_NULL("failed to allocate space for state", GSL_ENOMEM);
49 state->b = (double *) malloc (size * sizeof (double));
54 GSL_ERROR_NULL("failed to allocate space for b", GSL_ENOMEM);
57 state->c = (double *) malloc (size * sizeof (double));
63 GSL_ERROR_NULL("failed to allocate space for c", GSL_ENOMEM);
66 state->d = (double *) malloc (size * sizeof (double));
73 GSL_ERROR_NULL("failed to allocate space for d", GSL_ENOMEM);
76 state->_m = (double *) malloc ((size + 4) * sizeof (double));
78 if (state->_m == NULL)
84 GSL_ERROR_NULL("failed to allocate space for _m", GSL_ENOMEM);
91 /* common calculation */
93 akima_calc (const double x_array[], double b[], double c[], double d[], size_t size, double m[])
97 for (i = 0; i < (size - 1); i++)
99 const double NE = fabs (m[i + 1] - m[i]) + fabs (m[i - 1] - m[i - 2]);
108 const double h_i = x_array[i + 1] - x_array[i];
109 const double NE_next = fabs (m[i + 2] - m[i + 1]) + fabs (m[i] - m[i - 1]);
110 const double alpha_i = fabs (m[i - 1] - m[i - 2]) / NE;
119 alpha_ip1 = fabs (m[i] - m[i - 1]) / NE_next;
120 tL_ip1 = (1.0 - alpha_ip1) * m[i] + alpha_ip1 * m[i + 1];
122 b[i] = (1.0 - alpha_i) * m[i - 1] + alpha_i * m[i];
123 c[i] = (3.0 * m[i] - 2.0 * b[i] - tL_ip1) / h_i;
124 d[i] = (b[i] + tL_ip1 - 2.0 * m[i]) / (h_i * h_i);
131 akima_init (void * vstate, const double x_array[], const double y_array[],
134 akima_state_t *state = (akima_state_t *) vstate;
136 double * m = state->_m + 2; /* offset so we can address the -1,-2
140 for (i = 0; i <= size - 2; i++)
142 m[i] = (y_array[i + 1] - y_array[i]) / (x_array[i + 1] - x_array[i]);
145 /* non-periodic boundary conditions */
146 m[-2] = 3.0 * m[0] - 2.0 * m[1];
147 m[-1] = 2.0 * m[0] - m[1];
148 m[size - 1] = 2.0 * m[size - 2] - m[size - 3];
149 m[size] = 3.0 * m[size - 2] - 2.0 * m[size - 3];
151 akima_calc (x_array, state->b, state->c, state->d, size, m);
158 akima_init_periodic (void * vstate,
159 const double x_array[],
160 const double y_array[],
163 akima_state_t *state = (akima_state_t *) vstate;
165 double * m = state->_m + 2; /* offset so we can address the -1,-2
169 for (i = 0; i <= size - 2; i++)
171 m[i] = (y_array[i + 1] - y_array[i]) / (x_array[i + 1] - x_array[i]);
174 /* periodic boundary conditions */
175 m[-2] = m[size - 1 - 2];
176 m[-1] = m[size - 1 - 1];
180 akima_calc (x_array, state->b, state->c, state->d, size, m);
186 akima_free (void * vstate)
188 akima_state_t *state = (akima_state_t *) vstate;
200 akima_eval (const void * vstate,
201 const double x_array[], const double y_array[], size_t size,
203 gsl_interp_accel * a,
206 const akima_state_t *state = (const akima_state_t *) vstate;
212 index = gsl_interp_accel_find (a, x_array, size, x);
216 index = gsl_interp_bsearch (x_array, x, 0, size - 1);
221 const double x_lo = x_array[index];
222 const double delx = x - x_lo;
223 const double b = state->b[index];
224 const double c = state->c[index];
225 const double d = state->d[index];
226 *y = y_array[index] + delx * (b + delx * (c + d * delx));
233 akima_eval_deriv (const void * vstate,
234 const double x_array[], const double y_array[], size_t size,
236 gsl_interp_accel * a,
239 const akima_state_t *state = (const akima_state_t *) vstate;
243 DISCARD_POINTER(y_array); /* prevent warning about unused parameter */
247 index = gsl_interp_accel_find (a, x_array, size, x);
251 index = gsl_interp_bsearch (x_array, x, 0, size - 1);
256 double x_lo = x_array[index];
257 double delx = x - x_lo;
258 double b = state->b[index];
259 double c = state->c[index];
260 double d = state->d[index];
261 *dydx = b + delx * (2.0 * c + 3.0 * d * delx);
269 akima_eval_deriv2 (const void * vstate,
270 const double x_array[], const double y_array[], size_t size,
272 gsl_interp_accel * a,
275 const akima_state_t *state = (const akima_state_t *) vstate;
279 DISCARD_POINTER(y_array); /* prevent warning about unused parameter */
283 index = gsl_interp_accel_find (a, x_array, size, x);
287 index = gsl_interp_bsearch (x_array, x, 0, size - 1);
292 const double x_lo = x_array[index];
293 const double delx = x - x_lo;
294 const double c = state->c[index];
295 const double d = state->d[index];
296 *y_pp = 2.0 * c + 6.0 * d * delx;
304 akima_eval_integ (const void * vstate,
305 const double x_array[], const double y_array[], size_t size,
306 gsl_interp_accel * acc,
310 const akima_state_t *state = (const akima_state_t *) vstate;
312 size_t i, index_a, index_b;
316 index_a = gsl_interp_accel_find (acc, x_array, size, a);
317 index_b = gsl_interp_accel_find (acc, x_array, size, b);
321 index_a = gsl_interp_bsearch (x_array, a, 0, size - 1);
322 index_b = gsl_interp_bsearch (x_array, b, 0, size - 1);
327 /* interior intervals */
329 for(i=index_a; i<=index_b; i++) {
330 const double x_hi = x_array[i + 1];
331 const double x_lo = x_array[i];
332 const double y_lo = y_array[i];
333 const double dx = x_hi - x_lo;
336 if (i == index_a || i == index_b)
338 double x1 = (i == index_a) ? a : x_lo;
339 double x2 = (i == index_b) ? b : x_hi;
340 *result += integ_eval (y_lo, state->b[i], state->c[i], state->d[i],
345 *result += dx * (y_lo
346 + dx*(0.5*state->b[i]
347 + dx*(state->c[i]/3.0
348 + 0.25*state->d[i]*dx)));
361 static const gsl_interp_type akima_type =
374 const gsl_interp_type * gsl_interp_akima = &akima_type;
376 static const gsl_interp_type akima_periodic_type =
381 &akima_init_periodic,
389 const gsl_interp_type * gsl_interp_akima_periodic = &akima_periodic_type;