1 /* interpolation/interp_poly.c
3 * Copyright (C) 2001 DAN, HO-JIN
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.
22 #include <gsl/gsl_errno.h>
23 #include <gsl/gsl_poly.h>
24 #include <gsl/gsl_interp.h>
35 polynomial_alloc (size_t size)
37 polynomial_state_t *state =
38 (polynomial_state_t *) malloc (sizeof (polynomial_state_t));
42 GSL_ERROR_NULL ("failed to allocate space for polynomial state",
46 state->d = (double *) malloc (sizeof (double) * size);
51 GSL_ERROR_NULL ("failed to allocate space for d", GSL_ENOMEM);
54 state->coeff = (double *) malloc (sizeof (double) * size);
56 if (state->coeff == 0)
60 GSL_ERROR_NULL ("failed to allocate space for d", GSL_ENOMEM);
63 state->work = (double *) malloc (sizeof (double) * size);
70 GSL_ERROR_NULL ("failed to allocate space for d", GSL_ENOMEM);
77 polynomial_init (void *vstate,
78 const double xa[], const double ya[], size_t size)
80 polynomial_state_t *state = (polynomial_state_t *) vstate;
82 int status = gsl_poly_dd_init (state->d, xa, ya, size);
88 polynomial_eval (const void *vstate,
89 const double xa[], const double ya[], size_t size, double x,
90 gsl_interp_accel * acc, double *y)
92 const polynomial_state_t *state = (const polynomial_state_t *) vstate;
94 *y = gsl_poly_dd_eval (state->d, xa, size, x);
101 polynomial_deriv (const void *vstate,
102 const double xa[], const double ya[], size_t size, double x,
103 gsl_interp_accel * acc, double *y)
105 const polynomial_state_t *state = (const polynomial_state_t *) vstate;
107 gsl_poly_dd_taylor (state->coeff, x, state->d, xa, size, state->work);
109 *y = state->coeff[1];
115 polynomial_deriv2 (const void *vstate,
116 const double xa[], const double ya[], size_t size,
117 double x, gsl_interp_accel * acc, double *y)
119 const polynomial_state_t *state = (const polynomial_state_t *) vstate;
121 gsl_poly_dd_taylor (state->coeff, x, state->d, xa, size, state->work);
123 *y = 2.0 * state->coeff[2];
129 polynomial_integ (const void *vstate, const double xa[], const double ya[],
130 size_t size, gsl_interp_accel * acc, double a, double b,
133 const polynomial_state_t *state = (const polynomial_state_t *) vstate;
137 gsl_poly_dd_taylor (state->coeff, 0.0, state->d, xa, size, state->work);
139 sum = state->coeff[0] * (b - a);
141 for (i = 1; i < size; i++)
143 sum += state->coeff[i] * (pow (b, i + 1) - pow (a, i + 1)) / (i + 1.0);
152 polynomial_free (void *vstate)
154 polynomial_state_t *state = (polynomial_state_t *) vstate;
161 static const gsl_interp_type polynomial_type = {
173 const gsl_interp_type *gsl_interp_polynomial = &polynomial_type;